From e9335fe222f11f2ba873c22876e1449bd30c7c97 Mon Sep 17 00:00:00 2001 From: josesimoes Date: Fri, 23 Oct 2020 00:53:56 +0100 Subject: [PATCH] Fix Gpio.Toggle for ESP32 - Add storage for output state because IDF doesn't allow reading current output state. - Rework Toggle accordingly. --- .../nanoCLR/Windows.Devices.Gpio/cpu_gpio.cpp | 23 +++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/targets/FreeRTOS_ESP32/ESP32_WROOM_32/nanoCLR/Windows.Devices.Gpio/cpu_gpio.cpp b/targets/FreeRTOS_ESP32/ESP32_WROOM_32/nanoCLR/Windows.Devices.Gpio/cpu_gpio.cpp index 1e550c5a8f..9091c6cb6f 100644 --- a/targets/FreeRTOS_ESP32/ESP32_WROOM_32/nanoCLR/Windows.Devices.Gpio/cpu_gpio.cpp +++ b/targets/FreeRTOS_ESP32/ESP32_WROOM_32/nanoCLR/Windows.Devices.Gpio/cpu_gpio.cpp @@ -39,6 +39,9 @@ struct gpio_input_state : public HAL_DblLinkedNode static HAL_DblLinkedList gpioInputList; // Doulble LInkedlist for GPIO input status static uint16_t pinReserved[TOTAL_GPIO_PORTS]; // reserved - 1 bit per pin +// memory for pin state +static uint16_t pinStateStore[TOTAL_GPIO_PORTS]; + // Get pointer to gpio_input_state for Gpio pin // return NULL if not found gpio_input_state *GetInputState(GPIO_PIN pinNumber) @@ -218,8 +221,24 @@ void CPU_GPIO_TogglePinState(GPIO_PIN pinNumber) { // platform DOES NOT support toggle // need to do it "the hard way" - GpioPinValue newState = (GpioPinValue)(gpio_get_level((gpio_num_t)pinNumber) ^ GpioPinValue_High); - gpio_set_level((gpio_num_t)pinNumber, (uint32_t)newState); + + uint32_t newValue; + + // need to store pin state + int port = pinNumber >> 4, bit = 1 << (pinNumber & 0x0F); + + if (pinStateStore[port] & bit) + { + pinStateStore[port] &= ~bit; + newValue = 0; + } + else + { + pinStateStore[port] |= bit; + newValue = 1; + } + + gpio_set_level((gpio_num_t)pinNumber, newValue); } // ISR called by IDF