From 607d9c673e232fcaed23894cd613303780682c8a Mon Sep 17 00:00:00 2001 From: Gunar Schorcht Date: Thu, 1 Aug 2019 15:21:38 +0200 Subject: [PATCH 1/2] boards/esp32: corrected comment While testing the Arduino example for ESP32 boards, the problem of gpio_read for output ports was figured out. During these tests, also the wrong comment has been fixed. --- boards/esp32-wemos-lolin-d32-pro/include/arduino_board.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/boards/esp32-wemos-lolin-d32-pro/include/arduino_board.h b/boards/esp32-wemos-lolin-d32-pro/include/arduino_board.h index 162ff6a340..a1d9e0d5c8 100644 --- a/boards/esp32-wemos-lolin-d32-pro/include/arduino_board.h +++ b/boards/esp32-wemos-lolin-d32-pro/include/arduino_board.h @@ -29,7 +29,7 @@ extern "C" { #endif /** - * @brief The on-board LED is connected to pin 2 on this board + * @brief The on-board LED is connected to Arduino pin 10 on this board */ #define ARDUINO_LED (10) From 9772562359720831aa4fbe29eeba722567861d76 Mon Sep 17 00:00:00 2001 From: Gunar Schorcht Date: Thu, 1 Aug 2019 15:22:02 +0200 Subject: [PATCH 2/2] cpu/esp32: fix of gpio_read for output ports Although it isn't explicitly specified in API, gpio_read should return the last written output value for output ports. Since the handling of inputs and outputs is strictly separated by several registers in ESP32, gpio_read returned always the initial value of the input register. Therefore, a case distinction had to make. While for input ports the real value has to be read from the input register, the last written value for the output port has to be read from the output register. --- cpu/esp32/periph/gpio.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/cpu/esp32/periph/gpio.c b/cpu/esp32/periph/gpio.c index bf1070261c..d3cf21ed2c 100644 --- a/cpu/esp32/periph/gpio.c +++ b/cpu/esp32/periph/gpio.c @@ -239,6 +239,7 @@ const char* _gpio_pin_usage_str[] = #define GPIO_PIN_SET(b) if (b < 32) GPIO.out_w1ts = BIT(b); else GPIO.out1_w1ts.val = BIT(b-32) #define GPIO_PIN_CLR(b) if (b < 32) GPIO.out_w1tc = BIT(b); else GPIO.out1_w1tc.val = BIT(b-32) +#define GPIO_PIN_GET(b) (b < 32) ? (GPIO.out >> b) & 1 : (GPIO.out1.val >> (b-32)) & 1 #define GPIO_REG_BIT_GET(l,h,b) ((b < 32) ? GPIO.l & BIT(b) : GPIO.h.val & BIT(b-32)) #define GPIO_REG_BIT_SET(l,h,b) if (b < 32) GPIO.l |= BIT(b); else GPIO.h.val |= BIT(b-32) @@ -447,7 +448,18 @@ void gpio_irq_disable (gpio_t pin) int gpio_read (gpio_t pin) { CHECK_PARAM_RET(pin < GPIO_PIN_NUMOF, -1); - return GPIO_REG_BIT_GET(in, in1, pin) ? 1 : 0; + int value; + + if (REG_GET_BIT(_gpio_to_iomux_reg[pin], FUN_IE)) { + /* in case the pin is any kind of input, read from input register */ + value = GPIO_REG_BIT_GET(in, in1, pin) ? 1 : 0; + } + else { + /* otherwise read the last value written to the output register */ + value = GPIO_PIN_GET(pin); + } + DEBUG("%s gpio=%u val=%d\n", __func__, pin, value); + return value; } void gpio_write (gpio_t pin, int value)