From e17cce41b48446b0d8687c520f97a3ad99c5254f Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Sat, 20 Nov 2021 22:59:09 +0100 Subject: [PATCH 1/4] bootloaders/riotboot_dfu: move bootloader_selection.h to common place --- bootloaders/riotboot_dfu/main.c | 2 +- .../include/riotboot}/bootloader_selection.h | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) rename {bootloaders/riotboot_dfu => sys/include/riotboot}/bootloader_selection.h (94%) diff --git a/bootloaders/riotboot_dfu/main.c b/bootloaders/riotboot_dfu/main.c index 91606817b5..26dd2bf64f 100644 --- a/bootloaders/riotboot_dfu/main.c +++ b/bootloaders/riotboot_dfu/main.c @@ -28,7 +28,7 @@ #include "riotboot/usb_dfu.h" #include "ztimer.h" -#include "bootloader_selection.h" +#include "riotboot/bootloader_selection.h" #ifdef BTN_BOOTLOADER_PIN #include "periph/gpio.h" diff --git a/bootloaders/riotboot_dfu/bootloader_selection.h b/sys/include/riotboot/bootloader_selection.h similarity index 94% rename from bootloaders/riotboot_dfu/bootloader_selection.h rename to sys/include/riotboot/bootloader_selection.h index e552852109..81f089a4e5 100644 --- a/bootloaders/riotboot_dfu/bootloader_selection.h +++ b/sys/include/riotboot/bootloader_selection.h @@ -19,8 +19,8 @@ * to the riotboot_dfu application that isn't written in C++ and not included * from anywhere else either, but still here for consistency (and because * otherwise the checks complain) */ -#ifndef BOOTLOADER_SELECTION_H -#define BOOTLOADER_SELECTION_H +#ifndef RIOTBOOT_BOOTLOADER_SELECTION_H +#define RIOTBOOT_BOOTLOADER_SELECTION_H #ifdef __cplusplus extern "C" { @@ -72,6 +72,6 @@ extern "C" { } #endif -#endif /* BOOTLOADER_SELECTION_H */ +#endif /* RIOTBOOT_BOOTLOADER_SELECTION_H */ /** @} */ From 4be28295374dbc2b2ce89327cd92613b8fc81e14 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Sat, 20 Nov 2021 23:03:10 +0100 Subject: [PATCH 2/4] makefiles: riotboot_serial: default to STDIO BAUD for PROG_BAUD If $(PROG_BAUD) is not set, default to the boards $(BAUD). --- makefiles/tools/riotboot_serial.inc.mk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/makefiles/tools/riotboot_serial.inc.mk b/makefiles/tools/riotboot_serial.inc.mk index 46d9bd7c9d..d42e9ac1ed 100644 --- a/makefiles/tools/riotboot_serial.inc.mk +++ b/makefiles/tools/riotboot_serial.inc.mk @@ -2,7 +2,7 @@ RIOTBOOT_SERIAL := $(RIOTTOOLS)/riotboot_serial/riotboot_serial FLASHER ?= $(RIOTBOOT_SERIAL) FLASHFILE ?= $(HEXFILE) -PROG_BAUD ?= 115200 +PROG_BAUD ?= $(BAUD) FFLAGS ?= $(FLASHFILE) $(PORT) $(PROG_BAUD) ROM_OFFSET ?= $(RIOTBOOT_LEN) From f53a398bfb70a43914bb9447b3dbba20d457692c Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Sat, 20 Nov 2021 23:04:59 +0100 Subject: [PATCH 3/4] 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); From e6a06501b8e31f6acd694f4d2cd110b09bc49035 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Sun, 21 Nov 2021 15:40:06 +0100 Subject: [PATCH 4/4] sys/riotboot: serial: add bootloader LED --- bootloaders/riotboot_serial/Makefile.ci | 3 +++ sys/include/riotboot/bootloader_selection.h | 12 ++++++++++++ sys/riotboot/serial.c | 21 +++++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/bootloaders/riotboot_serial/Makefile.ci b/bootloaders/riotboot_serial/Makefile.ci index e2184eae00..f77aecd2a5 100644 --- a/bootloaders/riotboot_serial/Makefile.ci +++ b/bootloaders/riotboot_serial/Makefile.ci @@ -1,3 +1,6 @@ BOARD_INSUFFICIENT_MEMORY := \ + frdm-kw41z \ + pba-d-01-kw2x \ phynode-kw41z \ + usb-kw41z \ # diff --git a/sys/include/riotboot/bootloader_selection.h b/sys/include/riotboot/bootloader_selection.h index 81f089a4e5..52ba313307 100644 --- a/sys/include/riotboot/bootloader_selection.h +++ b/sys/include/riotboot/bootloader_selection.h @@ -68,6 +68,18 @@ extern "C" { #define BTN_BOOTLOADER_INVERTED true #endif +/** @brief LED pin for bootloader indication + * + * This pin (typically connected to a LED) will be toggled while the bootloader is active. + * It can be used to communicate the current bootloader state to the user. + */ +#if !defined(LED_BOOTLOADER_PIN) && defined(LED0_PIN) && !defined(LED_BOOTLOADER_NONE) || DOXYGEN +#define LED_BOOTLOADER_PIN LED0_PIN +#define LED_BOOTLOADER_ON LED0_ON /**< Turn the bootloader LED on */ +#define LED_BOOTLOADER_OFF LED0_OFF /**< Turn the bootloader LED off */ +#define LED_BOOTLOADER_TOGGLE LED0_TOGGLE /**< Toggle the bootloader LED */ +#endif + #ifdef __cplusplus } #endif diff --git a/sys/riotboot/serial.c b/sys/riotboot/serial.c index 454a27ab92..502f204e9d 100644 --- a/sys/riotboot/serial.c +++ b/sys/riotboot/serial.c @@ -69,6 +69,18 @@ static inline bool _boot_pin(void) #endif } +static inline void _boot_led_toggle(void) +{ +#ifdef LED_BOOTLOADER_PIN + static unsigned count = RIOTBOOT_DELAY / 10; + + if (--count == 0) { + count = RIOTBOOT_DELAY / 10; + LED_BOOTLOADER_TOGGLE; + } +#endif +} + /* send 'hello' byte until we get enter bootloader byte or timeout */ static bool _bootdelay(unsigned tries, volatile bool *boot_default) { @@ -88,6 +100,7 @@ static bool _bootdelay(unsigned tries, volatile bool *boot_default) if (_boot_pin()) { return false; } + _boot_led_toggle(); } return *boot_default; @@ -238,6 +251,10 @@ int riotboot_serial_loader(void) #ifdef BTN_BOOTLOADER_PIN gpio_init(BTN_BOOTLOADER_PIN, BTN_BOOTLOADER_MODE); #endif +#ifdef LED_BOOTLOADER_PIN + gpio_init(LED_BOOTLOADER_PIN, GPIO_OUT); + LED_BOOTLOADER_OFF; +#endif uart_init(RIOTBOOT_UART_DEV, RIOTBOOT_UART_BAUDRATE, _uart_rx_cmd, (void *)&reading); @@ -247,6 +264,10 @@ int riotboot_serial_loader(void) return -1; } +#ifdef LED_BOOTLOADER_ON + LED_BOOTLOADER_ON; +#endif + while (1) { /* we can't use mutex in riotboot */