Skip to content

Commit 6855541

Browse files
authored
Merge pull request adafruit#780 from sommersoft/esp8266_gpio16
Fix GPIO16 Board Reset; Updated Usage
2 parents 83e3907 + 396e4ff commit 6855541

4 files changed

Lines changed: 92 additions & 16 deletions

File tree

ports/esp8266/common-hal/analogio/AnalogIn.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,12 @@
3636

3737
#include "user_interface.h"
3838

39-
volatile bool adc_in_use __attribute__((aligned(4))) = false;
40-
4139
void common_hal_analogio_analogin_construct(analogio_analogin_obj_t* self,
4240
const mcu_pin_obj_t *pin) {
4341
if (pin != &pin_TOUT) {
4442
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "Pin %q does not have ADC capabilities", pin->name));
4543
}
46-
adc_in_use = true;
44+
claim_pin(pin);
4745
}
4846

4947
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) {
5452
if (common_hal_analogio_analogin_deinited(self)) {
5553
return;
5654
}
57-
adc_in_use = false;
55+
reset_pin(&pin_TOUT);
5856
self->deinited = true;
5957
}
6058

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

Lines changed: 50 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+
claim_pin(pin);
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+
reset_pin(self->pin);
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 && value) {
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=1
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,19 @@ 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+
if (self->open_drain && READ_PERI_REG(RTC_GPIO_ENABLE) == 0) {
159+
return true;
160+
} else {
161+
return READ_PERI_REG(RTC_GPIO_OUT) & 1;
162+
}
131163
} else {
132-
return ((*PIN_OUT) & pin_mask) != 0;
164+
uint32_t pin_mask = 1 << self->pin->gpio_number;
165+
if (self->open_drain && ((*PIN_DIR) & pin_mask) == 0) {
166+
return true;
167+
} else {
168+
return ((*PIN_OUT) & pin_mask) != 0;
169+
}
133170
}
134171
}
135172
}
@@ -163,8 +200,14 @@ void common_hal_digitalio_digitalinout_set_pull(
163200
return;
164201
}
165202
if (self->pin->gpio_number == 16) {
166-
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError,
167-
"Pin does not support pull."));
203+
// PULL_DOWN is the only hardware pull direction available on GPIO16.
204+
// since we don't support pull down, just return without attempting
205+
// to set pull (which won't work anyway). If PULL_UP is requested,
206+
// raise the exception so the user knows PULL_UP is not available
207+
if (pull != PULL_NONE){
208+
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError,
209+
"GPIO16 does not support pull up."));
210+
}
168211
return;
169212
}
170213
if (pull == PULL_NONE) {

ports/esp8266/common-hal/microcontroller/Pin.c

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,49 @@
3232

3333
#include "eagle_soc.h"
3434

35-
extern volatile bool adc_in_use;
35+
bool adc_in_use;
36+
bool gpio16_in_use;
3637

3738
bool common_hal_mcu_pin_is_free(const mcu_pin_obj_t* pin) {
3839
if (pin == &pin_TOUT) {
3940
return !adc_in_use;
4041
}
41-
if (pin->gpio_number == NO_GPIO || pin->gpio_number == SPECIAL_CASE) {
42+
if (pin == &pin_XPD_DCDC) {
43+
return !gpio16_in_use;
44+
}
45+
if (pin->gpio_number == NO_GPIO) {
4246
return false;
4347
}
4448
return (READ_PERI_REG(pin->peripheral) &
4549
(PERIPHS_IO_MUX_FUNC<<PERIPHS_IO_MUX_FUNC_S)) == 0 &&
4650
(GPIO_REG_READ(GPIO_ENABLE_ADDRESS) & (1 << pin->gpio_number)) == 0 &&
47-
(READ_PERI_REG(pin->peripheral) & PERIPHS_IO_MUX_PULLUP) == 0;
51+
(READ_PERI_REG(pin->peripheral) & PERIPHS_IO_MUX_PULLUP) == 0;
52+
}
53+
54+
void claim_pin(const mcu_pin_obj_t* pin) {
55+
if (pin == &pin_XPD_DCDC) {
56+
gpio16_in_use = true;
57+
}
58+
if (pin == &pin_TOUT) {
59+
adc_in_use = true;
60+
}
61+
}
62+
63+
void reset_pin(const mcu_pin_obj_t* pin) {
64+
if (pin == &pin_XPD_DCDC) {
65+
// Set GPIO16 as input
66+
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
67+
WRITE_PERI_REG(RTC_GPIO_CONF, READ_PERI_REG(RTC_GPIO_CONF) & ~1); //mux configuration for out enable
68+
WRITE_PERI_REG(RTC_GPIO_ENABLE, READ_PERI_REG(RTC_GPIO_ENABLE) & ~1); //out disable
69+
gpio16_in_use = false;
70+
}
71+
if (pin == &pin_TOUT) {
72+
adc_in_use = false;
73+
}
4874
}
4975

5076
void reset_pins(void) {
51-
for (int i = 0; i < 17; i++) {
77+
for (int i = 0; i < 16; i++) {
5278
// 5 is RXD, 6 is TXD
5379
if ((i > 4 && i < 13) || i == 12) {
5480
continue;
@@ -59,4 +85,11 @@ void reset_pins(void) {
5985
// Disable the pin.
6086
gpio_output_set(0x0, 0x0, 0x0, 1 << i);
6187
}
62-
}
88+
// Set GPIO16 as input
89+
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
90+
WRITE_PERI_REG(RTC_GPIO_CONF, READ_PERI_REG(RTC_GPIO_CONF) & ~1); //mux configuration for out enable
91+
WRITE_PERI_REG(RTC_GPIO_ENABLE, READ_PERI_REG(RTC_GPIO_ENABLE) & ~1); //out disable
92+
93+
adc_in_use = false;
94+
gpio16_in_use = false;
95+
}

ports/esp8266/common-hal/microcontroller/Pin.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ typedef struct {
4141
#define NO_GPIO 0xff
4242
#define SPECIAL_CASE 0xfe
4343

44+
void claim_pin(const mcu_pin_obj_t* pin);
45+
void reset_pin(const mcu_pin_obj_t* pin);
4446
void reset_pins(void);
4547

4648
#endif // MICROPY_INCLUDED_ESP8266_COMMON_HAL_MICROCONTROLLER_PIN_H

0 commit comments

Comments
 (0)