From a05723e5cbf92e6894cb73b8042a07dcc64b7d09 Mon Sep 17 00:00:00 2001 From: Benjamin Valentin Date: Sun, 22 Nov 2020 19:38:21 +0100 Subject: [PATCH] rioboot: add option to reset to riotboot --- sys/Makefile.dep | 7 ++++++- sys/Makefile.include | 2 +- sys/riotboot/reset.c | 32 ++++++++++++++++++++++++++++++++ sys/riotboot/serial.c | 8 ++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 sys/riotboot/reset.c diff --git a/sys/Makefile.dep b/sys/Makefile.dep index 9f38ee91d2..1025750b82 100644 --- a/sys/Makefile.dep +++ b/sys/Makefile.dep @@ -625,10 +625,15 @@ endif ifneq (,$(filter riotboot_serial, $(USEMODULE))) FEATURES_REQUIRED += periph_flashpage FEATURES_REQUIRED += periph_uart - USEMODULE += riotboot + USEMODULE += riotboot_reset USEMODULE += checksum endif +ifneq (,$(filter riotboot_reset, $(USEMODULE))) + USEMODULE += riotboot + USEMODULE += usb_board_reset +endif + ifneq (,$(filter riotboot_hdr, $(USEMODULE))) USEMODULE += checksum USEMODULE += riotboot diff --git a/sys/Makefile.include b/sys/Makefile.include index 2ca8bdce07..06b8f32190 100644 --- a/sys/Makefile.include +++ b/sys/Makefile.include @@ -141,7 +141,7 @@ ifneq (,$(filter prng,$(USEMODULE))) include $(RIOTBASE)/sys/random/Makefile.include endif -ifneq (,$(filter usbus_dfu,$(USEMODULE))) +ifneq (,$(filter usbus_dfu riotboot_reset,$(USEMODULE))) CFLAGS += -DCPU_RAM_BASE=$(RAM_START_ADDR) CFLAGS += -DCPU_RAM_SIZE=$(RAM_LEN) endif diff --git a/sys/riotboot/reset.c b/sys/riotboot/reset.c new file mode 100644 index 0000000000..ff1bb597b5 --- /dev/null +++ b/sys/riotboot/reset.c @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2020 Benjamin Valentin + * + * This file is subject to the terms and conditions of the GNU Lesser General + * Public License v2.1. See the file LICENSE in the top level directory for more + * details. + */ + +/** + * @ingroup sys_riotboot_serial + * @{ + * + * @file + * @brief Trigger reset to riotboot. + * + * @author Benjamin Valentin + * @} + */ + +#include +#include "periph/pm.h" +#include "riotboot/magic.h" + +__attribute__((weak)) +void usb_board_reset_in_bootloader(void) +{ + uint32_t *magic = (void *)(uintptr_t)RIOTBOOT_MAGIC_ADDR; + + irq_disable(); + *magic = RIOTBOOT_MAGIC; + pm_reboot(); +} diff --git a/sys/riotboot/serial.c b/sys/riotboot/serial.c index 292516d2b2..69180f69cb 100644 --- a/sys/riotboot/serial.c +++ b/sys/riotboot/serial.c @@ -27,6 +27,7 @@ #include "unaligned.h" #include "checksum/crc8.h" #include "riotboot/serial.h" +#include "riotboot/magic.h" #include "board.h" @@ -55,6 +56,13 @@ static inline void uart_write_byte(uart_t uart, uint8_t data) /* send 'hello' byte until we get enter bootloader byte or timeout */ static bool _bootdelay(unsigned tries, volatile bool *boot_default) { + uint32_t *magic = (void *)(uintptr_t)RIOTBOOT_MAGIC_ADDR; + + if (*magic == RIOTBOOT_MAGIC) { + *magic = 0; + return false; + } + if (tries == 0) { return true; }