Page 1 of 1

Fast digital read

Posted: Fri Sep 22, 2023 4:46 pm
by adamalfath
Currently, I'm using GPIO.out method to get a fast digital write operation.

Code: Select all

gpio_config_t io_output_control;

io_output_control.pin_bit_mask = (1 << PIN_OUT_0) | (1 << PIN_OUT_1) | (1 << PIN_OUT_2) | (1 << PIN_OUT_3);
io_output_control.mode = GPIO_MODE_OUTPUT;
gpio_config(&io_output_control);

GPIO.out_w1ts = output_pin_bitmask_value;
GPIO.out_w1tc = ~output_pin_bitmask_value;
I'm looking for a similar thing for a fast digital read operation, in addition to the previous write operation. I was thinking of using:

Code: Select all

gpio_config_t io_input_control;

io_input_control.pin_bit_mask = (1 << PIN_IN_0) | (1 << PIN_IN_1) | (1 << PIN_IN_2) | (1 << PIN_IN_3);
io_input_control.mode = GPIO_MODE_INPUT;
gpio_config(&io_input_control);

input_pin_bitmask_value = GPIO.in;
Is this valid? I'm not sure when GPIO.in is called, does it know which config structure it will be used? Will the second gpio_config() call override the first one?

My final implementation would be like this, set some I/O state and read the input pin, in an infinite loop:

Code: Select all

loop() {
    /* preparing output bit mask */
    
    /* fast GPIO write & read operation */
    GPIO.out_w1ts = output_pin_bitmask_value;
    GPIO.out_w1tc = ~output_pin_bitmask_value;
    input_pin_bitmask_value = GPIO.in;
    
    /* some other operation */
}

Re: Fast digital read

Posted: Sat Sep 23, 2023 9:39 pm
by MicroController
I'm not sure when GPIO.in is called, does it know which config structure it will be used? Will the second gpio_config() call override the first one?
When configuring the GPIO via gpio_config(), you specify which pins the settings apply to. Only the pins you specify will be configured; other pins are unaffected. So it's perfectly fine to use multiple gpio_config() calls to set up different sets of IO pins.

When reading the "in" register, you get the current logic levels for IO pins 0 through 31 in one go, irrespective of which of the pins are inputs or outputs; you'll have to get the bits for the pins you're interested in from that 32-bit value yourself.

Re: Fast digital read

Posted: Mon Sep 25, 2023 11:51 am
by username
#define PIN_TO_READ GPIO_NUM_1

// If pin # is <32 use this
#define PIN_READ() (REG_READ(GPIO_IN_REG) >> (PIN_TO_READ));
// Otherwise, use this
#define PIN_READ() (REG_READ(GPIO_IN1_REG) >> (PIN_TO_READ- 32))


// Check state of pin here.
uint8_t busy = (uint8_t)PIN_READ();

Re: Fast digital read

Posted: Tue Sep 26, 2023 2:48 pm
by adamalfath
Thanks all for the information, really helpful.

I'm proceeding with GPIO.in method, and also REG_READ(GPIO_IN_REG) is giving more or less the same performance