Page 1 of 2

Dedicated GPIO - Assembly code for reading pin

Posted: Wed Jun 05, 2024 8:07 pm
by srjasz
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

Re: Dedicated GPIO - Assembly code for reading pin

Posted: Thu Jun 06, 2024 10:57 am
by MicroController
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();

Re: Dedicated GPIO - Assembly code for reading pin

Posted: Thu Jun 06, 2024 2:32 pm
by srjasz
Excellent, exactly what I was looking for, thank you so much.

Re: Dedicated GPIO - Assembly code for reading pin

Posted: Thu Jun 06, 2024 5:25 pm
by srjasz
Do you know how to flag this as solved?

Re: Dedicated GPIO - Assembly code for reading pin

Posted: Thu Jun 06, 2024 5:38 pm
by MicroController
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] ...".

Re: Dedicated GPIO - Assembly code for reading pin

Posted: Thu Jun 06, 2024 10:05 pm
by Ravenholem
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

Re: Dedicated GPIO - Assembly code for reading pin

Posted: Mon Jun 10, 2024 3:30 pm
by srjasz
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;
}

Re: Dedicated GPIO - Assembly code for reading pin

Posted: Mon Jun 10, 2024 3:46 pm
by srjasz
Sorry, I was using the word Group when I should have been using Bundle.

Re: Dedicated GPIO - Assembly code for reading pin

Posted: Mon Jul 01, 2024 1:47 pm
by BillBill
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.

Re: Dedicated GPIO - Assembly code for reading pin

Posted: Tue Jul 02, 2024 12:46 pm
by MicroController
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().