Skip to content

Commit c4fb5f7

Browse files
committed
Allow ESP boards to customize how a pin is reset
This allows board code to override the default pull up reset state. It is useful for pins that are already externally connected, pulled or otherwise used by the board. Fixes #5931
1 parent fe6e03f commit c4fb5f7

File tree

5 files changed

+58
-20
lines changed

5 files changed

+58
-20
lines changed

ports/espressif/boards/adafruit_feather_esp32s2_tft/board.c

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,6 @@ uint8_t display_init_sequence[] = {
7171

7272

7373
void board_init(void) {
74-
// Never reset the I2C/TFT power pin because doing so will reset the display.
75-
// Instead, on reset set the default value and free the pin for user use.
76-
// Relying on the normal pin reset would briefly float/pull the pin that
77-
// could lead to a power brownout.
78-
common_hal_never_reset_pin(&pin_GPIO21);
79-
80-
reset_board();
81-
8274
busio_spi_obj_t *spi = common_hal_board_create_spi(0);
8375
displayio_fourwire_obj_t *bus = &displays[0].fourwire_bus;
8476
bus->base.type = &displayio_fourwire_type;
@@ -99,7 +91,6 @@ void board_init(void) {
9991
// workaround as board_init() is called before reset_port() in main.c
10092
pwmout_reset();
10193

102-
10394
common_hal_displayio_display_construct(
10495
display,
10596
bus,
@@ -138,12 +129,18 @@ bool board_requests_safe_mode(void) {
138129
return false;
139130
}
140131

141-
void reset_board(void) {
142-
// Turn on TFT and I2C
143-
gpio_set_direction(21, GPIO_MODE_DEF_OUTPUT);
144-
gpio_set_level(21, true);
132+
bool espressif_board_reset_pin_number(gpio_num_t pin_number) {
133+
// Override the I2C/TFT power pin reset to prevent resetting the display.
134+
if (pin_number == 21) {
135+
// Turn on TFT and I2C
136+
gpio_set_direction(21, GPIO_MODE_DEF_OUTPUT);
137+
gpio_set_level(21, true);
138+
return true;
139+
}
140+
return false;
141+
}
145142

146-
free_pin_number(21);
143+
void reset_board(void) {
147144
}
148145

149146
void board_deinit(void) {

ports/espressif/boards/adafruit_magtag_2.9_grayscale/board.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,25 @@ void reset_board(void) {
172172

173173
}
174174

175+
bool espressif_board_reset_pin_number(gpio_num_t pin_number) {
176+
// Pin 16 is speaker enable and it's pulled down on the board. We don't want
177+
// to pull it high because then we'll compete with the external pull down.
178+
// So, reset without any pulls internally.
179+
if (pin_number == 16) {
180+
gpio_config_t cfg = {
181+
.pin_bit_mask = BIT64(16),
182+
.mode = GPIO_MODE_DISABLE,
183+
// The pin is externally pulled down, so we don't need to pull it.
184+
.pull_up_en = false,
185+
.pull_down_en = false,
186+
.intr_type = GPIO_INTR_DISABLE,
187+
};
188+
gpio_config(&cfg);
189+
return true;
190+
}
191+
return false;
192+
}
193+
175194
void board_deinit(void) {
176195
displayio_epaperdisplay_obj_t *display = &displays[0].epaper_display;
177196
if (display->base.type == &displayio_epaperdisplay_type) {

ports/espressif/boards/microdev_micro_s2/board.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#include "mpconfigboard.h"
2929
#include "shared-bindings/microcontroller/Pin.h"
3030

31+
#include "components/driver/include/driver/gpio.h"
32+
3133
void board_init(void) {
3234
// Debug UART
3335
#ifdef DEBUG
@@ -40,6 +42,17 @@ bool board_requests_safe_mode(void) {
4042
return false;
4143
}
4244

45+
bool espressif_board_reset_pin_number(gpio_num_t pin_number) {
46+
// Pin 21 is a high side LED so pull it down to prevent lighting the LED.
47+
if (pin_number == 21) {
48+
gpio_reset_pin(21);
49+
gpio_pullup_dis(21);
50+
gpio_pulldown_en(21);
51+
return true;
52+
}
53+
return false;
54+
}
55+
4356
void reset_board(void) {
4457
}
4558

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ void common_hal_never_reset_pin(const mcu_pin_obj_t *pin) {
5050
never_reset_pin_number(pin->number);
5151
}
5252

53+
MP_WEAK bool espressif_board_reset_pin_number(gpio_num_t pin_number) {
54+
return false;
55+
}
56+
5357
STATIC void _reset_pin(gpio_num_t pin_number) {
5458
#if defined(CONFIG_IDF_TARGET_ESP32S2) || defined(CONFIG_IDF_TARGET_ESP32S3)
5559
// Never ever reset pins used for flash and RAM.
@@ -76,6 +80,11 @@ STATIC void _reset_pin(gpio_num_t pin_number) {
7680
}
7781
#endif
7882

83+
// Give the board a chance to reset the pin in a particular way.
84+
if (espressif_board_reset_pin_number(pin_number)) {
85+
return;
86+
}
87+
7988
gpio_reset_pin(pin_number);
8089

8190
#ifdef DOUBLE_TAP_PIN
@@ -133,10 +142,6 @@ void claim_pin(const mcu_pin_obj_t *pin) {
133142
in_use[pin->number / 32] |= (1 << (pin->number % 32));
134143
}
135144

136-
void free_pin_number(gpio_num_t pin_number) {
137-
in_use[pin_number / 32] &= ~(1 << (pin_number % 32));
138-
}
139-
140145
void common_hal_mcu_pin_claim(const mcu_pin_obj_t *pin) {
141146
claim_pin(pin);
142147
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,13 @@ void common_hal_reset_pin(const mcu_pin_obj_t *pin);
3939
void common_hal_never_reset_pin(const mcu_pin_obj_t *pin);
4040
void claim_pin(const mcu_pin_obj_t *pin);
4141
void claim_pin_number(gpio_num_t pin_number);
42-
// Free the pin without resetting it.
43-
void free_pin_number(gpio_num_t pin_number);
4442
bool pin_number_is_free(gpio_num_t pin_number);
4543
void never_reset_pin_number(gpio_num_t pin_number);
4644

45+
// Allow the board to reset a pin in a board-specific way. This can be used
46+
// for LEDs or enable pins to put them in a state beside the default pull-up.
47+
// Return true to indicate that the pin was reset. Returning false will lead to
48+
// the port-default reset behavior.
49+
bool espressif_board_reset_pin_number(gpio_num_t pin_number);
50+
4751
#endif // MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_MICROCONTROLLER_PIN_H

0 commit comments

Comments
 (0)