Skip to content

Commit

Permalink
Allow ESP boards to customize how a pin is reset
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tannewt committed Feb 18, 2022
1 parent fe6e03f commit c4fb5f7
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 20 deletions.
25 changes: 11 additions & 14 deletions ports/espressif/boards/adafruit_feather_esp32s2_tft/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,6 @@ uint8_t display_init_sequence[] = {


void board_init(void) {
// Never reset the I2C/TFT power pin because doing so will reset the display.
// Instead, on reset set the default value and free the pin for user use.
// Relying on the normal pin reset would briefly float/pull the pin that
// could lead to a power brownout.
common_hal_never_reset_pin(&pin_GPIO21);

reset_board();

busio_spi_obj_t *spi = common_hal_board_create_spi(0);
displayio_fourwire_obj_t *bus = &displays[0].fourwire_bus;
bus->base.type = &displayio_fourwire_type;
Expand All @@ -99,7 +91,6 @@ void board_init(void) {
// workaround as board_init() is called before reset_port() in main.c
pwmout_reset();


common_hal_displayio_display_construct(
display,
bus,
Expand Down Expand Up @@ -138,12 +129,18 @@ bool board_requests_safe_mode(void) {
return false;
}

void reset_board(void) {
// Turn on TFT and I2C
gpio_set_direction(21, GPIO_MODE_DEF_OUTPUT);
gpio_set_level(21, true);
bool espressif_board_reset_pin_number(gpio_num_t pin_number) {
// Override the I2C/TFT power pin reset to prevent resetting the display.
if (pin_number == 21) {
// Turn on TFT and I2C
gpio_set_direction(21, GPIO_MODE_DEF_OUTPUT);
gpio_set_level(21, true);
return true;
}
return false;
}

free_pin_number(21);
void reset_board(void) {
}

void board_deinit(void) {
Expand Down
19 changes: 19 additions & 0 deletions ports/espressif/boards/adafruit_magtag_2.9_grayscale/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,25 @@ void reset_board(void) {

}

bool espressif_board_reset_pin_number(gpio_num_t pin_number) {
// Pin 16 is speaker enable and it's pulled down on the board. We don't want
// to pull it high because then we'll compete with the external pull down.
// So, reset without any pulls internally.
if (pin_number == 16) {
gpio_config_t cfg = {
.pin_bit_mask = BIT64(16),
.mode = GPIO_MODE_DISABLE,
// The pin is externally pulled down, so we don't need to pull it.
.pull_up_en = false,
.pull_down_en = false,
.intr_type = GPIO_INTR_DISABLE,
};
gpio_config(&cfg);
return true;
}
return false;
}

void board_deinit(void) {
displayio_epaperdisplay_obj_t *display = &displays[0].epaper_display;
if (display->base.type == &displayio_epaperdisplay_type) {
Expand Down
13 changes: 13 additions & 0 deletions ports/espressif/boards/microdev_micro_s2/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include "mpconfigboard.h"
#include "shared-bindings/microcontroller/Pin.h"

#include "components/driver/include/driver/gpio.h"

void board_init(void) {
// Debug UART
#ifdef DEBUG
Expand All @@ -40,6 +42,17 @@ bool board_requests_safe_mode(void) {
return false;
}

bool espressif_board_reset_pin_number(gpio_num_t pin_number) {
// Pin 21 is a high side LED so pull it down to prevent lighting the LED.
if (pin_number == 21) {
gpio_reset_pin(21);
gpio_pullup_dis(21);
gpio_pulldown_en(21);
return true;
}
return false;
}

void reset_board(void) {
}

Expand Down
13 changes: 9 additions & 4 deletions ports/espressif/common-hal/microcontroller/Pin.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ void common_hal_never_reset_pin(const mcu_pin_obj_t *pin) {
never_reset_pin_number(pin->number);
}

MP_WEAK bool espressif_board_reset_pin_number(gpio_num_t pin_number) {
return false;
}

STATIC void _reset_pin(gpio_num_t pin_number) {
#if defined(CONFIG_IDF_TARGET_ESP32S2) || defined(CONFIG_IDF_TARGET_ESP32S3)
// Never ever reset pins used for flash and RAM.
Expand All @@ -76,6 +80,11 @@ STATIC void _reset_pin(gpio_num_t pin_number) {
}
#endif

// Give the board a chance to reset the pin in a particular way.
if (espressif_board_reset_pin_number(pin_number)) {
return;
}

gpio_reset_pin(pin_number);

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

void free_pin_number(gpio_num_t pin_number) {
in_use[pin_number / 32] &= ~(1 << (pin_number % 32));
}

void common_hal_mcu_pin_claim(const mcu_pin_obj_t *pin) {
claim_pin(pin);
}
Expand Down
8 changes: 6 additions & 2 deletions ports/espressif/common-hal/microcontroller/Pin.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ void common_hal_reset_pin(const mcu_pin_obj_t *pin);
void common_hal_never_reset_pin(const mcu_pin_obj_t *pin);
void claim_pin(const mcu_pin_obj_t *pin);
void claim_pin_number(gpio_num_t pin_number);
// Free the pin without resetting it.
void free_pin_number(gpio_num_t pin_number);
bool pin_number_is_free(gpio_num_t pin_number);
void never_reset_pin_number(gpio_num_t pin_number);

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

#endif // MICROPY_INCLUDED_ESPRESSIF_COMMON_HAL_MICROCONTROLLER_PIN_H

0 comments on commit c4fb5f7

Please sign in to comment.