From beb0fb752c5d0b3a4bff534e0a778786f647c26f Mon Sep 17 00:00:00 2001 From: dylad Date: Tue, 15 Dec 2020 17:14:18 +0100 Subject: [PATCH] sys/riotboot: add DFU support to riotboot --- sys/Makefile.dep | 6 +++ sys/include/riotboot/usb_dfu.h | 69 ++++++++++++++++++++++++++++++++++ sys/riotboot/usb_dfu.c | 41 ++++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 sys/include/riotboot/usb_dfu.h create mode 100644 sys/riotboot/usb_dfu.c diff --git a/sys/Makefile.dep b/sys/Makefile.dep index adef24f7cb..575c7db8da 100644 --- a/sys/Makefile.dep +++ b/sys/Makefile.dep @@ -1002,6 +1002,12 @@ ifneq (,$(filter riotboot_hdr, $(USEMODULE))) USEMODULE += riotboot endif +ifneq (,$(filter riotboot_usb_dfu, $(USEMODULE))) + USEMODULE += usbus_dfu + USEMODULE += riotboot_flashwrite + FEATURES_REQUIRED += no_idle_thread +endif + ifneq (,$(filter irq_handler,$(USEMODULE))) USEMODULE += event endif diff --git a/sys/include/riotboot/usb_dfu.h b/sys/include/riotboot/usb_dfu.h new file mode 100644 index 0000000000..3eab034041 --- /dev/null +++ b/sys/include/riotboot/usb_dfu.h @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2020 Mesotic SAS + * + * 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. + */ + +/** + * @defgroup sys_riotboot_usb_dfu Initialization of USB Device Firmware + * Upgrade for riotboot + * @ingroup sys + * @{ + * + * @file + * @brief USB DFU initialization for riotboot + * + * @author Dylan Laduranty + * + * @} + */ + +#ifndef RIOTBOOT_USB_DFU_H +#define RIOTBOOT_USB_DFU_H + +#include "riotboot/hdr.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @name USB DFU RAM information for riotboot + * @{ + */ +#ifndef RIOTBOOT_DFU_ADDR +#define RIOTBOOT_DFU_ADDR (CPU_RAM_BASE + CPU_RAM_SIZE - 4) /**< DFU default magic address */ +#endif +#ifndef RIOTBOOT_MAGIC_NUMBER +#define RIOTBOOT_MAGIC_NUMBER RIOTBOOT_MAGIC /**< DFU default magic value */ +#endif +/** @} */ + +/** + * @name USB DFU Default slots name + * @{ + */ +#ifndef USB_DFU_MODE_SLOT0_NAME +#define USB_DFU_MODE_SLOT0_NAME "RIOT-OS Slot 0" +#endif +#ifndef USB_DFU_MODE_SLOT1_NAME +#define USB_DFU_MODE_SLOT1_NAME "RIOT-OS Slot 1" +#endif +#ifndef USB_APP_MODE_SLOT_NAME +#define USB_APP_MODE_SLOT_NAME "RIOT-OS bootloader" +#endif + +/** @} */ + +/** + * @brief Initialize usbus DFU for riotboot bootloader + */ +void riotboot_usb_dfu_init(unsigned forced); + +#ifdef __cplusplus +} +#endif + +#endif /* RIOTBOOT_USB_DFU_H */ diff --git a/sys/riotboot/usb_dfu.c b/sys/riotboot/usb_dfu.c new file mode 100644 index 0000000000..5d9028ad1b --- /dev/null +++ b/sys/riotboot/usb_dfu.c @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2020 Mesotic SAS + * + * 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_usb_dfu + * @{ + * + * @file + * @brief USB Device Firmware Upgrade initialization for riotboot + * + * @author Dylan Laduranty + * + * @} + */ + +#include "cpu.h" +#include "thread.h" + +#include "usb/usbus.h" +#include "usb/dfu.h" +#include "usb/usbus/dfu.h" +#include "riotboot/usb_dfu.h" + +static usbus_dfu_device_t dfu; +static char _stack[USBUS_STACKSIZE]; +static usbus_t usbus; + +void riotboot_usb_dfu_init(unsigned forced) { + uint32_t *reset_addr = (uint32_t *)RIOTBOOT_DFU_ADDR; + if (forced == 1 || *reset_addr == RIOTBOOT_MAGIC_NUMBER) { + *reset_addr = 0; + usbus_init(&usbus, usbdev_get_ctx(0)); + usbus_dfu_init(&usbus, &dfu, USB_DFU_PROTOCOL_DFU_MODE); + usbus_create(_stack, USBUS_STACKSIZE, USBUS_PRIO, USBUS_TNAME, &usbus); + } +}