Skip to content

Commit

Permalink
feat: Add support for inverting reset and boot pins
Browse files Browse the repository at this point in the history
Add two new config variables SERIAL_FLASHER_RESET_INVERT and
SERIAL_FLASHER_BOOT_INVERT that invert the output of their respective
pins when resetting the target device.

Only implemented for UART interface.
  • Loading branch information
shiona committed Nov 7, 2024
1 parent 8ece286 commit 848a0ab
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 20 deletions.
16 changes: 16 additions & 0 deletions Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,20 @@ menu "ESP serial flasher"
int "Number of retries when writing blocks either to target flash or RAM"
default 3

config SERIAL_FLASHER_RESET_INVERT
bool "Invert reset signal"
default n
depends on SERIAL_FLASHER_INTERFACE_UART
help
Enable this option if there is an inverting connection between
the output of the serial-flasher and the reset pin of the ESP chip.

config SERIAL_FLASHER_BOOT_INVERT
bool "Invert boot signal"
default n
depends on SERIAL_FLASHER_INTERFACE_UART
help
Enable this option if there is an inverting connection between
the output of the serial-flasher and the boot pin of the ESP chip.

endmenu
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ This is the time for which the boot pin is asserted when doing a hard reset in m

Default: 50

* `SERIAL_FLASHER_RESET_INVERT`

This inverts the output of the reset gpio pin. Useful if the hardware has inverting connection
between the host and the target reset pin. Implemented only for UART interface.

Default: n

* `SERIAL_FLASHER_BOOT_INVERT`
This inverts the output of the boot (IO0) gpio pin. Useful if the hardware has inverting connection
between the host and the target boot pin. Implemented only for UART interface.

Default: n

Configuration can be passed to `cmake` via command line:

```
Expand Down
8 changes: 4 additions & 4 deletions port/esp32_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,18 +142,18 @@ esp_loader_error_t loader_port_read(uint8_t *data, uint16_t size, uint32_t timeo
// assert reset pin for 50 milliseconds.
void loader_port_enter_bootloader(void)
{
gpio_set_level(s_gpio0_trigger_pin, 0);
gpio_set_level(s_gpio0_trigger_pin, SERIAL_FLASHER_BOOT_INVERT ? 1 : 0);
loader_port_reset_target();
loader_port_delay_ms(SERIAL_FLASHER_BOOT_HOLD_TIME_MS);
gpio_set_level(s_gpio0_trigger_pin, 1);
gpio_set_level(s_gpio0_trigger_pin, SERIAL_FLASHER_BOOT_INVERT ? 0 : 1);
}


void loader_port_reset_target(void)
{
gpio_set_level(s_reset_trigger_pin, 0);
gpio_set_level(s_reset_trigger_pin, SERIAL_FLASHER_RESET_INVERT ? 1 : 0);
loader_port_delay_ms(SERIAL_FLASHER_RESET_HOLD_TIME_MS);
gpio_set_level(s_reset_trigger_pin, 1);
gpio_set_level(s_reset_trigger_pin, SERIAL_FLASHER_RESET_INVERT ? 0 : 1);
}


Expand Down
8 changes: 4 additions & 4 deletions port/pi_pico_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,20 @@ void loader_port_pi_pico_deinit(void)

void loader_port_enter_bootloader(void)
{
gpio_put(s_boot_pin_num, 0);
gpio_put(s_boot_pin_num, SERIAL_FLASHER_BOOT_INVERT ? 1 : 0);

loader_port_reset_target();
loader_port_delay_ms(SERIAL_FLASHER_BOOT_HOLD_TIME_MS);

gpio_put(s_boot_pin_num, 1);
gpio_put(s_boot_pin_num, SERIAL_FLASHER_BOOT_INVERT ? 0 : 1);
}


void loader_port_reset_target(void)
{
gpio_put(s_reset_trigger_pin_num, 0);
gpio_put(s_reset_trigger_pin_num, SERIAL_FLASHER_RESET_INVERT ? 1 : 0);
loader_port_delay_ms(SERIAL_FLASHER_RESET_HOLD_TIME_MS);
gpio_put(s_reset_trigger_pin_num, 1);
gpio_put(s_reset_trigger_pin_num, SERIAL_FLASHER_RESET_INVERT ? 0 : 1);
}


Expand Down
8 changes: 4 additions & 4 deletions port/raspberry_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -262,18 +262,18 @@ esp_loader_error_t loader_port_read(uint8_t *data, uint16_t size, uint32_t timeo
// Set GPIO0 LOW, then assert reset pin for 50 milliseconds.
void loader_port_enter_bootloader(void)
{
gpioWrite(s_gpio0_trigger_pin, 0);
gpioWrite(s_gpio0_trigger_pin, SERIAL_FLASHER_BOOT_INVERT ? 1 : 0);
loader_port_reset_target();
loader_port_delay_ms(SERIAL_FLASHER_BOOT_HOLD_TIME_MS);
gpioWrite(s_gpio0_trigger_pin, 1);
gpioWrite(s_gpio0_trigger_pin, SERIAL_FLASHER_BOOT_INVERT ? 0 : 1);
}


void loader_port_reset_target(void)
{
gpioWrite(s_reset_trigger_pin, 0);
gpioWrite(s_reset_trigger_pin, SERIAL_FLASHER_RESET_INVERT ? 1 : 0);
loader_port_delay_ms(SERIAL_FLASHER_RESET_HOLD_TIME_MS);
gpioWrite(s_reset_trigger_pin, 1);
gpioWrite(s_reset_trigger_pin, SERIAL_FLASHER_RESET_INVERT ? 0 : 1);
}


Expand Down
8 changes: 4 additions & 4 deletions port/stm32_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,18 @@ void loader_port_stm32_init(loader_stm32_config_t *config)
// assert reset pin for 100 milliseconds.
void loader_port_enter_bootloader(void)
{
HAL_GPIO_WritePin(gpio_port_io0, gpio_num_io0, GPIO_PIN_RESET);
HAL_GPIO_WritePin(gpio_port_io0, gpio_num_io0, SERIAL_FLASHER_BOOT_INVERT ? GPIO_PIN_SET : GPIO_PIN_RESET);
loader_port_reset_target();
HAL_Delay(SERIAL_FLASHER_BOOT_HOLD_TIME_MS);
HAL_GPIO_WritePin(gpio_port_io0, gpio_num_io0, GPIO_PIN_SET);
HAL_GPIO_WritePin(gpio_port_io0, gpio_num_io0, SERIAL_FLASHER_BOOT_INVERT ? GPIO_PIN_RESET : GPIO_PIN_SET);
}


void loader_port_reset_target(void)
{
HAL_GPIO_WritePin(gpio_port_rst, gpio_num_rst, GPIO_PIN_RESET);
HAL_GPIO_WritePin(gpio_port_rst, gpio_num_rst, SERIAL_FLASHER_RESET_INVERT ? GPIO_PIN_SET : GPIO_PIN_RESET);
HAL_Delay(SERIAL_FLASHER_RESET_HOLD_TIME_MS);
HAL_GPIO_WritePin(gpio_port_rst, gpio_num_rst, GPIO_PIN_SET);
HAL_GPIO_WritePin(gpio_port_rst, gpio_num_rst, SERIAL_FLASHER_RESET_INVERT ? GPIO_PIN_RESET : GPIO_PIN_SET);
}


Expand Down
8 changes: 4 additions & 4 deletions port/zephyr_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,17 @@ esp_loader_error_t loader_port_zephyr_init(const loader_zephyr_config_t *config)

void loader_port_reset_target(void)
{
gpio_pin_set_dt(&enable_spec, false);
gpio_pin_set_dt(&enable_spec, CONFIG_SERIAL_FLASHER_RESET_INVERT ? true : false);
loader_port_delay_ms(CONFIG_SERIAL_FLASHER_RESET_HOLD_TIME_MS);
gpio_pin_set_dt(&enable_spec, true);
gpio_pin_set_dt(&enable_spec, CONFIG_SERIAL_FLASHER_RESET_INVERT ? false : true);
}

void loader_port_enter_bootloader(void)
{
gpio_pin_set_dt(&boot_spec, false);
gpio_pin_set_dt(&boot_spec, CONFIG_SERIAL_FLASHER_BOOT_INVERT ? true : false);
loader_port_reset_target();
loader_port_delay_ms(CONFIG_SERIAL_FLASHER_BOOT_HOLD_TIME_MS);
gpio_pin_set_dt(&boot_spec, true);
gpio_pin_set_dt(&boot_spec, CONFIG_SERIAL_FLASHER_BOOT_INVERT ? false : true);
}

void loader_port_delay_ms(uint32_t ms)
Expand Down

0 comments on commit 848a0ab

Please sign in to comment.