diff --git a/Kconfig b/Kconfig index 7f3172d..e0e9151 100644 --- a/Kconfig +++ b/Kconfig @@ -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 diff --git a/README.md b/README.md index 6fa1cc7..2c2012e 100644 --- a/README.md +++ b/README.md @@ -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: ``` diff --git a/port/esp32_port.c b/port/esp32_port.c index af119ab..2a3ab2e 100644 --- a/port/esp32_port.c +++ b/port/esp32_port.c @@ -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); } diff --git a/port/pi_pico_port.c b/port/pi_pico_port.c index 47cd5c7..203930a 100644 --- a/port/pi_pico_port.c +++ b/port/pi_pico_port.c @@ -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); } diff --git a/port/raspberry_port.c b/port/raspberry_port.c index 71b0ec7..e0722b7 100644 --- a/port/raspberry_port.c +++ b/port/raspberry_port.c @@ -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); } diff --git a/port/stm32_port.c b/port/stm32_port.c index e8157f9..eb52794 100644 --- a/port/stm32_port.c +++ b/port/stm32_port.c @@ -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); } diff --git a/port/zephyr_port.c b/port/zephyr_port.c index c53a612..1bb8c1c 100644 --- a/port/zephyr_port.c +++ b/port/zephyr_port.c @@ -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)