Skip to content

Commit bd5a5da

Browse files
committed
updated GPIO16 construct and handling
1 parent e70ece4 commit bd5a5da

1 file changed

Lines changed: 41 additions & 7 deletions

File tree

ports/esp8266/common-hal/digitalio/DigitalInOut.c

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,21 @@
3333
#include "py/mphal.h"
3434

3535
#include "shared-bindings/digitalio/DigitalInOut.h"
36+
#include "common-hal/microcontroller/Pin.h"
37+
38+
extern volatile bool gpio16_in_use;
3639

3740
digitalinout_result_t common_hal_digitalio_digitalinout_construct(
3841
digitalio_digitalinout_obj_t* self, const mcu_pin_obj_t* pin) {
3942
self->pin = pin;
40-
PIN_FUNC_SELECT(self->pin->peripheral, self->pin->gpio_function);
43+
if (self->pin->gpio_number == 16) {
44+
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
45+
WRITE_PERI_REG(RTC_GPIO_CONF, READ_PERI_REG(RTC_GPIO_CONF) & ~1); //mux configuration for out enable
46+
WRITE_PERI_REG(RTC_GPIO_ENABLE, READ_PERI_REG(RTC_GPIO_ENABLE) & ~1); //out disable
47+
gpio16_in_use = true;
48+
} else {
49+
PIN_FUNC_SELECT(self->pin->peripheral, self->pin->gpio_function);
50+
}
4151
return DIGITALINOUT_OK;
4252
}
4353

@@ -54,6 +64,8 @@ void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t* self
5464
gpio_output_set(0x0, 0x0, 0x0, pin_mask);
5565
PIN_FUNC_SELECT(self->pin->peripheral, 0);
5666
PIN_PULLUP_DIS(self->pin->peripheral);
67+
} else {
68+
gpio16_in_use = false;
5769
}
5870
self->pin = mp_const_none;
5971
}
@@ -96,6 +108,23 @@ digitalio_direction_t common_hal_digitalio_digitalinout_get_direction(
96108

97109
void common_hal_digitalio_digitalinout_set_value(
98110
digitalio_digitalinout_obj_t* self, bool value) {
111+
if (self->pin->gpio_number == 16) {
112+
if (self->open_drain) {
113+
// configure GPIO16 as input with output register holding 0
114+
WRITE_PERI_REG(PAD_XPD_DCDC_CONF, (READ_PERI_REG(PAD_XPD_DCDC_CONF) & 0xffffffbc) | 1);
115+
WRITE_PERI_REG(RTC_GPIO_CONF, READ_PERI_REG(RTC_GPIO_CONF) & ~1);
116+
WRITE_PERI_REG(RTC_GPIO_ENABLE, (READ_PERI_REG(RTC_GPIO_ENABLE) & ~1)); // input
117+
WRITE_PERI_REG(RTC_GPIO_OUT, (READ_PERI_REG(RTC_GPIO_OUT) & ~1)); // out=0
118+
return;
119+
} else {
120+
int out_en = self->output;
121+
WRITE_PERI_REG(PAD_XPD_DCDC_CONF, (READ_PERI_REG(PAD_XPD_DCDC_CONF) & 0xffffffbc) | 1);
122+
WRITE_PERI_REG(RTC_GPIO_CONF, READ_PERI_REG(RTC_GPIO_CONF) & ~1);
123+
WRITE_PERI_REG(RTC_GPIO_ENABLE, (READ_PERI_REG(RTC_GPIO_ENABLE) & ~1) | out_en);
124+
WRITE_PERI_REG(RTC_GPIO_OUT, (READ_PERI_REG(RTC_GPIO_OUT) & ~1) | value);
125+
return;
126+
}
127+
}
99128
if (value) {
100129
if (self->open_drain) {
101130
// Disable output.
@@ -125,11 +154,15 @@ bool common_hal_digitalio_digitalinout_get_value(
125154
}
126155
return GPIO_INPUT_GET(self->pin->gpio_number);
127156
} else {
128-
uint32_t pin_mask = 1 << self->pin->gpio_number;
129-
if (self->open_drain && ((*PIN_DIR) & pin_mask) == 0) {
130-
return true;
157+
if (self->pin->gpio_number == 16) {
158+
return READ_PERI_REG(RTC_GPIO_OUT) & 1;
131159
} else {
132-
return ((*PIN_OUT) & pin_mask) != 0;
160+
uint32_t pin_mask = 1 << self->pin->gpio_number;
161+
if (self->open_drain && ((*PIN_DIR) & pin_mask) == 0) {
162+
return true;
163+
} else {
164+
return ((*PIN_OUT) & pin_mask) != 0;
165+
}
133166
}
134167
}
135168
}
@@ -163,8 +196,9 @@ void common_hal_digitalio_digitalinout_set_pull(
163196
return;
164197
}
165198
if (self->pin->gpio_number == 16) {
166-
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError,
167-
"Pin does not support pull."));
199+
// PULL_DOWN is the only hardware pull direction available on GPIO16
200+
// since we don't support pull down, just return without attempting
201+
// to set pull (which won't work anyway).
168202
return;
169203
}
170204
if (pull == PULL_NONE) {

0 commit comments

Comments
 (0)