Dedicated GPIO - Assembly code for reading pin

srjasz
Posts: 46
Joined: Wed Apr 03, 2024 4:29 pm

Dedicated GPIO - Assembly code for reading pin

Postby srjasz » Wed Jun 05, 2024 8:07 pm

Does anyone know where to get the assembly code for reading Dedicated GPIO input pins? I have been successful using Dedicated GPIO, using bundles, to increase the speed of setting output pins. It requires the use of a line of assembly code to get the actual increase in speed. I am now trying to do the same for reading input pins. I was fortunate to find the assembly code for output pins on this forum but not for input pins.

Thanks

MicroController
Posts: 1726
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Dedicated GPIO - Assembly code for reading pin

Postby MicroController » Thu Jun 06, 2024 10:57 am

ESP32-S3:

Code: Select all

static uint32_t read_dedic_gpio() {
  uint32_t r;
  asm volatile (
    "EE.GET_GPIO_IN %[r]"
    : [r] "=r" (r)
  );
  return r;
}
Alternatively,

Code: Select all

#include "hal/dedic_gpio_cpu_ll.h"

uint32_t input = dedic_gpio_cpu_ll_read_in();

srjasz
Posts: 46
Joined: Wed Apr 03, 2024 4:29 pm

Re: Dedicated GPIO - Assembly code for reading pin

Postby srjasz » Thu Jun 06, 2024 2:32 pm

Excellent, exactly what I was looking for, thank you so much.

srjasz
Posts: 46
Joined: Wed Apr 03, 2024 4:29 pm

Re: Dedicated GPIO - Assembly code for reading pin

Postby srjasz » Thu Jun 06, 2024 5:25 pm

Do you know how to flag this as solved?

MicroController
Posts: 1726
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Dedicated GPIO - Assembly code for reading pin

Postby MicroController » Thu Jun 06, 2024 5:38 pm

There's no official way to mark threads as 'solved' in the forum :)
Some people edit the title of their post to prefix it "[SOLVED] ...".

Ravenholem
Posts: 20
Joined: Wed Nov 10, 2021 7:13 pm

Re: Dedicated GPIO - Assembly code for reading pin

Postby Ravenholem » Thu Jun 06, 2024 10:05 pm

MicroController wrote:
Thu Jun 06, 2024 10:57 am
ESP32-S3:

Code: Select all

static uint32_t read_dedic_gpio() {
  uint32_t r;
  asm volatile (
    "EE.GET_GPIO_IN %[r]"
    : [r] "=r" (r)
  );
  return r;
}
Alternatively,

Code: Select all

#include "hal/dedic_gpio_cpu_ll.h"

uint32_t input = dedic_gpio_cpu_ll_read_in();
I have not used ASM for GPIO pins but does it give a boost in speed compared to using GPIO.out_w1ts = (1 << num)
During some more sensitive stuff I discovered the provided functions were to slow and traced it back to writing directly to this GPIO struct.
It dramatically dropped the time it took to do GPIO pin twiddling. Was wondering if the ASM gives any more gains over the ones I already got.

Thanks

srjasz
Posts: 46
Joined: Wed Apr 03, 2024 4:29 pm

Re: Dedicated GPIO - Assembly code for reading pin

Postby srjasz » Mon Jun 10, 2024 3:30 pm

I was able to get switching time of 12ns on IO pins by using assembly code. That is fast enough to generate an 80mhz square wave by turning a pin on and off.

You need to set up the IO pins as 'Dedicated GPIO'. It is not described in the reference manual, but is described in the programming users guide. You can find it in the users guide at API Reference->Peripherals API->Dedicated GPIO, which is different from 'GPIO and RTC GPIO'.

In this code I have set IO 46 as the first member of the group in the Dedicated GPIO setup. The sample I was working from had
4 nops which add 12ns each. I have removed them and have had no problem. If you leave the nops in you will end up with 60ns which is no faster than what you are doing now.

__attribute__((always_inline))
inline void CmGpioDedicatedSet46(void)
{
__asm__ __volatile__ ("ee.set_bit_gpio_out %0" : : "I"(0x1) : ); // 0x01 indicates the first member which is selected by bit field.
// __asm__ __volatile__ ("nop");
// __asm__ __volatile__ ("nop");
// __asm__ __volatile__ ("nop");
// __asm__ __volatile__ ("nop");
}

__attribute__((always_inline))
inline void CmGpioDedicatedClr46(void)
{
__asm__ __volatile__ ("ee.clr_bit_gpio_out %0" : : "I"(0x1) : );
// __asm__ __volatile__ ("nop");
// __asm__ __volatile__ ("nop");
// __asm__ __volatile__ ("nop");
// __asm__ __volatile__ ("nop");
}


The read will return the state of the pins in the group according to their bit field positions so you have to sort through them after the read, assuming you have more than 1.

static uint32_t read_dedic_gpioX() {
uint32_t r;
asm volatile ("ee.get_gpio_in %[r]" : [r] "=r" (r));
return r;
}

srjasz
Posts: 46
Joined: Wed Apr 03, 2024 4:29 pm

Re: Dedicated GPIO - Assembly code for reading pin

Postby srjasz » Mon Jun 10, 2024 3:46 pm

Sorry, I was using the word Group when I should have been using Bundle.

BillBill
Posts: 3
Joined: Fri Sep 22, 2023 10:40 am

Re: Dedicated GPIO - Assembly code for reading pin

Postby BillBill » Mon Jul 01, 2024 1:47 pm

Hi Guys,
I'm going round in circles trying to read & write to gpio pins.

I'm using ESP32 in Arduino IDE, which I know is sort of C++.

Have tried copying the code examples posted here, but they do not work.
The Expressif documentation & examples are way over my head.

Is there a simple example of gpio manipulation anywhere, which I can put straight into Arduino IDE to get me going?

I'm making a lightening sensor for a camera. Using an Arduino light sensor module, when this sees lightening, it grounds an input pin on the ESP32.
In turn the ESP32 grounds an output pin, which via an optoisolator, operates the camera cable release.

(I know there will be latency in the light module, ESP32 & camera but hoping lightening stays visible for a relatively long time, so should be able to photograph it, also using pre-capture, which continually takes photos before the shutter is pressed & saves the last 30 seconds of photos when the shutter button is pressed).

Currently I' using
while (digitalRead(TriggerLowPin) == LOW) { // wait for lightning
}
to wait for the light sensor to trigger

and

REG_WRITE(0x3ff44008, shutterBit); // open shutter
REG_WRITE(0x3ff44008, greenLedBit); // turn on yellow LED
to open shutter & turn on a LED

Thanks for your help, appreciated.

MicroController
Posts: 1726
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Dedicated GPIO - Assembly code for reading pin

Postby MicroController » Tue Jul 02, 2024 12:46 pm

BillBill wrote:
Mon Jul 01, 2024 1:47 pm
Is there a simple example of gpio manipulation anywhere, which I can put straight into Arduino IDE to get me going?
Just use digitalWrite().

Who is online

Users browsing this forum: Majestic-12 [Bot] and 131 guests