Andrew Clink

  • iOS
  • web
  • print

ESP8266 Hacking: GPIO Read Delay

12 May 2016

An ongoing project I'm working on is being migrated to the ESP8266 platform for wifi control. The project uses I2C for communication between different modules. Getting this to work on the ESP platform is crucial to the success of the project.

Several i2c/TWI  drivers have been released, so it seems that plenty of people have had great success in implementing bit-banged I2C (why did Espressif not provide a hardware i2c module in this chip? Especially given the ADC module?).

The problem for me this time is that reading the value of a GPIO pin does not return the value that my logic analyzer shows. Letting go of a pin (with a standard 4.7kΩ pull-up resistor) and then reading the value results in a 0 or LOW. Writing LOW results (correctly) as a LOW. The 'scope and logic analyzer both show the correct values (although I can't sync either to the code execution to determine when the value is being latched and when it becomes available to the processor).

I wrote a test program that initializes a GPIO to open-drain, then toggles it and reads the value repeatedly, delaying slightly after each read. The first read has no delay and clearly reads the value before the line has come high when releasing it. Setting it low reads correctly each time.

high: 0
low: 0

When measuring the delay I found that a simple delay of 1µs was enough. Adding a pull-up resistor of 1.5kΩ did not affect this timing. In my test program, adding the delay before reading did effect the correct reading. 

printf("high: ");
delay_us = 0;
gpio_write(TEST_PIN, 1);
sdk_os_delay_us(1); // Remove to measure
while(gpio_read(TEST_PIN) != 1)
printf("read %d in %dus\n", gpio_read(TEST_PIN), delay_us);

In my main application actually talking to a chip, however, a delay of even 50µs did not comport with the logic analyzer's results.