Fast digital read

adamalfath
Posts: 5
Joined: Sun Dec 20, 2020 3:55 pm

Fast digital read

Postby adamalfath » Fri Sep 22, 2023 4:46 pm

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 */
}

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

Re: Fast digital read

Postby MicroController » Sat Sep 23, 2023 9:39 pm

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.

username
Posts: 528
Joined: Thu May 03, 2018 1:18 pm

Re: Fast digital read

Postby username » Mon Sep 25, 2023 11:51 am

#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();

adamalfath
Posts: 5
Joined: Sun Dec 20, 2020 3:55 pm

Re: Fast digital read

Postby adamalfath » Tue Sep 26, 2023 2:48 pm

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

Who is online

Users browsing this forum: Bing [Bot] and 204 guests