From f53a398bfb70a43914bb9447b3dbba20d457692c Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Sat, 20 Nov 2021 23:04:59 +0100 Subject: [PATCH] riotboot: serial: enter bootloader mode by pin --- sys/riotboot/serial.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/sys/riotboot/serial.c b/sys/riotboot/serial.c index 8301137af4..454a27ab92 100644 --- a/sys/riotboot/serial.c +++ b/sys/riotboot/serial.c @@ -23,11 +23,13 @@ #include "stdio_uart.h" #include "periph/uart.h" +#include "periph/gpio.h" #include "periph/flashpage.h" #include "unaligned.h" #include "checksum/crc8.h" #include "riotboot/serial.h" #include "riotboot/magic.h" +#include "riotboot/bootloader_selection.h" #include "board.h" @@ -53,6 +55,20 @@ static inline void uart_write_byte(uart_t uart, uint8_t data) uart_write(uart, &data, 1); } +static inline bool _boot_pin(void) +{ +#ifdef BTN_BOOTLOADER_PIN + if (BTN_BOOTLOADER_INVERTED) { + return !gpio_read(BTN_BOOTLOADER_PIN); + } + else { + return gpio_read(BTN_BOOTLOADER_PIN); + } +#else + return false; +#endif +} + /* send 'hello' byte until we get enter bootloader byte or timeout */ static bool _bootdelay(unsigned tries, volatile bool *boot_default) { @@ -69,6 +85,9 @@ static bool _bootdelay(unsigned tries, volatile bool *boot_default) while (--tries && *boot_default) { uart_write_byte(RIOTBOOT_UART_DEV, 0); + if (_boot_pin()) { + return false; + } } return *boot_default; @@ -180,9 +199,9 @@ error: static void _get_page(uintptr_t addr) { - uart_write_byte(RIOTBOOT_UART_DEV, RIOTBOOT_STAT_OK); uint32_t page = flashpage_page((void *)addr); + uart_write_byte(RIOTBOOT_UART_DEV, RIOTBOOT_STAT_OK); uart_write(RIOTBOOT_UART_DEV, (void *)&page, sizeof(page)); } @@ -216,6 +235,10 @@ int riotboot_serial_loader(void) { volatile bool reading = true; +#ifdef BTN_BOOTLOADER_PIN + gpio_init(BTN_BOOTLOADER_PIN, BTN_BOOTLOADER_MODE); +#endif + uart_init(RIOTBOOT_UART_DEV, RIOTBOOT_UART_BAUDRATE, _uart_rx_cmd, (void *)&reading);