diff --git a/ports/esp8266/common-hal/analogio/AnalogIn.c b/ports/esp8266/common-hal/analogio/AnalogIn.c index d3a81272ee..e01eceabd7 100644 --- a/ports/esp8266/common-hal/analogio/AnalogIn.c +++ b/ports/esp8266/common-hal/analogio/AnalogIn.c @@ -36,14 +36,12 @@ #include "user_interface.h" -volatile bool adc_in_use __attribute__((aligned(4))) = false; - void common_hal_analogio_analogin_construct(analogio_analogin_obj_t* self, const mcu_pin_obj_t *pin) { if (pin != &pin_TOUT) { nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Pin %q does not have ADC capabilities", pin->name)); } - adc_in_use = true; + claim_pin(pin); } bool common_hal_analogio_analogin_deinited(analogio_analogin_obj_t* self) { @@ -54,7 +52,7 @@ void common_hal_analogio_analogin_deinit(analogio_analogin_obj_t* self) { if (common_hal_analogio_analogin_deinited(self)) { return; } - adc_in_use = false; + reset_pin(&pin_TOUT); self->deinited = true; } diff --git a/ports/esp8266/common-hal/digitalio/DigitalInOut.c b/ports/esp8266/common-hal/digitalio/DigitalInOut.c index bc1ae990f5..96945737b3 100644 --- a/ports/esp8266/common-hal/digitalio/DigitalInOut.c +++ b/ports/esp8266/common-hal/digitalio/DigitalInOut.c @@ -44,7 +44,7 @@ digitalinout_result_t common_hal_digitalio_digitalinout_construct( WRITE_PERI_REG(PAD_XPD_DCDC_CONF, (READ_PERI_REG(PAD_XPD_DCDC_CONF) & 0xffffffbc) | 1); // mux configuration for XPD_DCDC and rtc_gpio0 connection WRITE_PERI_REG(RTC_GPIO_CONF, READ_PERI_REG(RTC_GPIO_CONF) & ~1); //mux configuration for out enable WRITE_PERI_REG(RTC_GPIO_ENABLE, READ_PERI_REG(RTC_GPIO_ENABLE) & ~1); //out disable - gpio16_in_use = true; + claim_pin(pin); } else { PIN_FUNC_SELECT(self->pin->peripheral, self->pin->gpio_function); } @@ -65,10 +65,7 @@ void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t* self PIN_FUNC_SELECT(self->pin->peripheral, 0); PIN_PULLUP_DIS(self->pin->peripheral); } else { - WRITE_PERI_REG(PAD_XPD_DCDC_CONF, (READ_PERI_REG(PAD_XPD_DCDC_CONF) & 0xffffffbc) | 1); // mux configuration for XPD_DCDC and rtc_gpio0 connection - WRITE_PERI_REG(RTC_GPIO_CONF, READ_PERI_REG(RTC_GPIO_CONF) & ~1); //mux configuration for out enable - WRITE_PERI_REG(RTC_GPIO_ENABLE, READ_PERI_REG(RTC_GPIO_ENABLE) & ~1); //out disable - gpio16_in_use = false; + reset_pin(self->pin); } self->pin = mp_const_none; } @@ -113,12 +110,12 @@ void common_hal_digitalio_digitalinout_set_value( digitalio_digitalinout_obj_t* self, bool value) { if (self->pin->gpio_number == 16) { if (self->open_drain && value) { - // configure GPIO16 as input with output register holding 0 - WRITE_PERI_REG(PAD_XPD_DCDC_CONF, (READ_PERI_REG(PAD_XPD_DCDC_CONF) & 0xffffffbc) | 1); - WRITE_PERI_REG(RTC_GPIO_CONF, READ_PERI_REG(RTC_GPIO_CONF) & ~1); - WRITE_PERI_REG(RTC_GPIO_ENABLE, (READ_PERI_REG(RTC_GPIO_ENABLE) & ~1)); // input - WRITE_PERI_REG(RTC_GPIO_OUT, (READ_PERI_REG(RTC_GPIO_OUT) & ~1)); // out=0 - return; + // configure GPIO16 as input with output register holding 0 + WRITE_PERI_REG(PAD_XPD_DCDC_CONF, (READ_PERI_REG(PAD_XPD_DCDC_CONF) & 0xffffffbc) | 1); + WRITE_PERI_REG(RTC_GPIO_CONF, READ_PERI_REG(RTC_GPIO_CONF) & ~1); + WRITE_PERI_REG(RTC_GPIO_ENABLE, (READ_PERI_REG(RTC_GPIO_ENABLE) & ~1)); // input + WRITE_PERI_REG(RTC_GPIO_OUT, (READ_PERI_REG(RTC_GPIO_OUT) & 1)); // out=1 + return; } else { int out_en = self->output; WRITE_PERI_REG(PAD_XPD_DCDC_CONF, (READ_PERI_REG(PAD_XPD_DCDC_CONF) & 0xffffffbc) | 1); @@ -158,7 +155,7 @@ bool common_hal_digitalio_digitalinout_get_value( return GPIO_INPUT_GET(self->pin->gpio_number); } else { if (self->pin->gpio_number == 16) { - if (self->open_drain && (READ_PERI_REG(RTC_GPIO_OUT) | READ_PERI_REG(RTC_GPIO_ENABLE)) == 0) { + if (self->open_drain && READ_PERI_REG(RTC_GPIO_ENABLE) == 0) { return true; } else { return READ_PERI_REG(RTC_GPIO_OUT) & 1; diff --git a/ports/esp8266/common-hal/microcontroller/Pin.c b/ports/esp8266/common-hal/microcontroller/Pin.c index 4b6efb428d..6d2af2d2e8 100644 --- a/ports/esp8266/common-hal/microcontroller/Pin.c +++ b/ports/esp8266/common-hal/microcontroller/Pin.c @@ -32,23 +32,45 @@ #include "eagle_soc.h" -extern volatile bool adc_in_use; -volatile bool gpio16_in_use __attribute__((aligned(4))) = false; +bool adc_in_use; +bool gpio16_in_use; bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t* pin) { if (pin == &pin_TOUT) { return !adc_in_use; } + if (pin == &pin_XPD_DCDC) { + return !gpio16_in_use; + } if (pin->gpio_number == NO_GPIO) { return false; } - if (pin->gpio_number == 16) { - return !gpio16_in_use; - } return (READ_PERI_REG(pin->peripheral) & (PERIPHS_IO_MUX_FUNC<gpio_number)) == 0 && - (READ_PERI_REG(pin->peripheral) & PERIPHS_IO_MUX_PULLUP) == 0; + (READ_PERI_REG(pin->peripheral) & PERIPHS_IO_MUX_PULLUP) == 0; +} + +void claim_pin(const mcu_pin_obj_t* pin) { + if (pin == &pin_XPD_DCDC) { + gpio16_in_use = true; + } + if (pin == &pin_TOUT) { + adc_in_use = true; + } +} + +void reset_pin(const mcu_pin_obj_t* pin) { + if (pin == &pin_XPD_DCDC) { + // Set GPIO16 as input + WRITE_PERI_REG(PAD_XPD_DCDC_CONF, (READ_PERI_REG(PAD_XPD_DCDC_CONF) & 0xffffffbc) | 1); // mux configuration for XPD_DCDC and rtc_gpio0 connection + WRITE_PERI_REG(RTC_GPIO_CONF, READ_PERI_REG(RTC_GPIO_CONF) & ~1); //mux configuration for out enable + WRITE_PERI_REG(RTC_GPIO_ENABLE, READ_PERI_REG(RTC_GPIO_ENABLE) & ~1); //out disable + gpio16_in_use = false; + } + if (pin == &pin_TOUT) { + adc_in_use = false; + } } void reset_pins(void) { @@ -67,5 +89,7 @@ void reset_pins(void) { WRITE_PERI_REG(PAD_XPD_DCDC_CONF, (READ_PERI_REG(PAD_XPD_DCDC_CONF) & 0xffffffbc) | 1); // mux configuration for XPD_DCDC and rtc_gpio0 connection WRITE_PERI_REG(RTC_GPIO_CONF, READ_PERI_REG(RTC_GPIO_CONF) & ~1); //mux configuration for out enable WRITE_PERI_REG(RTC_GPIO_ENABLE, READ_PERI_REG(RTC_GPIO_ENABLE) & ~1); //out disable + + adc_in_use = false; gpio16_in_use = false; -} +} \ No newline at end of file diff --git a/ports/esp8266/common-hal/microcontroller/Pin.h b/ports/esp8266/common-hal/microcontroller/Pin.h index 405557ddc9..3aa290bbd3 100644 --- a/ports/esp8266/common-hal/microcontroller/Pin.h +++ b/ports/esp8266/common-hal/microcontroller/Pin.h @@ -41,6 +41,8 @@ typedef struct { #define NO_GPIO 0xff #define SPECIAL_CASE 0xfe +void claim_pin(const mcu_pin_obj_t* pin); +void reset_pin(const mcu_pin_obj_t* pin); void reset_pins(void); #endif // MICROPY_INCLUDED_ESP8266_COMMON_HAL_MICROCONTROLLER_PIN_H