From 45da07326dab3a36988c45bf3e9bf6e5a0c5b9e1 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Mon, 24 Feb 2020 15:28:38 +0100 Subject: [PATCH] Library: Generic DFU boot-loader --- USB-IDS | 1 + bsb/bootloader/Makefile | 4 +-- bsb/bootloader/config.h | 10 ++++++ bsb/bootloader/main.c => lib/dfu-bootloader.c | 35 ++++++++++--------- 4 files changed, 31 insertions(+), 19 deletions(-) rename bsb/bootloader/main.c => lib/dfu-bootloader.c (92%) diff --git a/USB-IDS b/USB-IDS index 4c4a6ca..5beefcb 100644 --- a/USB-IDS +++ b/USB-IDS @@ -3,5 +3,6 @@ USB IDs used by our gadgets 4242:0001 Experiments with clock 4242:0002 SSR module 4242:0003 BSB gateway +4242:0004 BSB gateway bootloader cafe:cafe KSP Space Alert thermometer cafe:caff KSP Space Alert accelerometer diff --git a/bsb/bootloader/Makefile b/bsb/bootloader/Makefile index 4c9abf3..1f14b90 100644 --- a/bsb/bootloader/Makefile +++ b/bsb/bootloader/Makefile @@ -1,6 +1,6 @@ ROOT=../.. BINARY=bootloader -OBJS=main.o -LIB_OBJS=util-debug.o +OBJS= +LIB_OBJS=util-debug.o dfu-bootloader.o include $(ROOT)/mk/bluepill.mk diff --git a/bsb/bootloader/config.h b/bsb/bootloader/config.h index dcb9ac7..232fd4a 100644 --- a/bsb/bootloader/config.h +++ b/bsb/bootloader/config.h @@ -12,3 +12,13 @@ #define DEBUG_USART USART1 #define DEBUG_LED_BLUEPILL + +// Bootloader settings + +#define BOOTLOADER_DEBUG +#define BOOTLOADER_APP_START 0x08002000 +#define BOOTLOADER_MFG_ID 0x4242 +#define BOOTLOADER_DEV_ID 0x0004 +#define BOOTLOADER_DEV_VERSION 0x0100 +#define BOOTLOADER_MFG_NAME "United Computer Wizards" +#define BOOTLOADER_DEV_NAME "BSB Interface (boot-loader)" diff --git a/bsb/bootloader/main.c b/lib/dfu-bootloader.c similarity index 92% rename from bsb/bootloader/main.c rename to lib/dfu-bootloader.c index 5071122..4f792ce 100644 --- a/bsb/bootloader/main.c +++ b/lib/dfu-bootloader.c @@ -1,5 +1,5 @@ /* - * Bootloader for the BSB Interface + * Generic DFU Bootloader * * (c) 2020 Martin Mareš * @@ -25,16 +25,12 @@ #include -#define DEBUG_BOOTLOADER - -#ifdef DEBUG_BOOTLOADER +#ifdef BOOTLOADER_DEBUG #define DEBUG(x...) debug_printf(x) #else #define DEBUG(x...) do { } while (0) #endif -#define APP_ADDRESS 0x08002000 - byte usbd_control_buffer[1024]; static enum dfu_state usbdfu_state = STATE_DFU_IDLE; @@ -55,8 +51,8 @@ enum usb_string { }; static const char *usb_strings[] = { - "United Computer Wizards", - "Boot Loader", + BOOTLOADER_MFG_NAME, + BOOTLOADER_DEV_NAME, usb_serial_number, }; @@ -68,9 +64,9 @@ const struct usb_device_descriptor dev = { .bDeviceSubClass = 0, .bDeviceProtocol = 0, .bMaxPacketSize0 = 64, - .idVendor = 0x4242, - .idProduct = 0xdead, - .bcdDevice = 0x0100, + .idVendor = BOOTLOADER_MFG_ID, + .idProduct = BOOTLOADER_DEV_ID, + .bcdDevice = BOOTLOADER_DEV_VERSION, .iManufacturer = STR_MANUFACTURER, .iProduct = STR_DEVICE, .iSerialNumber = STR_SERIAL, @@ -112,7 +108,7 @@ const struct usb_config_descriptor config = { .bConfigurationValue = 1, .iConfiguration = 0, .bmAttributes = USB_CONFIG_ATTR_DEFAULT, // bus-powered - .bMaxPower = 50, // multiplied by 2 mA + .bMaxPower = 50, // multiplied by 2 mA .interface = ifaces, }; @@ -137,7 +133,7 @@ static void usbdfu_getstatus_complete(usbd_device *usbd_dev UNUSED, struct usb_s switch (usbdfu_state) { case STATE_DFU_DNBUSY: { flash_unlock(); - u32 baseaddr = APP_ADDRESS + prog.blocknum * dfu_function.wTransferSize; + u32 baseaddr = BOOTLOADER_APP_START + prog.blocknum * dfu_function.wTransferSize; DEBUG("DFU: Block %u -> %08x\n", prog.blocknum, (uint) baseaddr); flash_erase_page(baseaddr); for (uint i = 0; i < prog.len; i += 2) { @@ -243,7 +239,8 @@ int main(void) rcc_periph_reset_pulse(RST_GPIOC); rcc_periph_reset_pulse(RST_USB); -#ifdef DEBUG_BOOTLOADER +#ifdef DEBUG_USART + // Currently, only USART1 is supported rcc_periph_clock_enable(RCC_USART1); rcc_periph_reset_pulse(RST_USART1); gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO9); @@ -258,9 +255,11 @@ int main(void) usart_enable(USART1); #endif +#ifdef DEBUG_LED_BLUEPILL // BluePill LED gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13); debug_led(1); +#endif // Systick: set to overflow in 1 ms, will use only the overflow flag, no interrupts systick_set_frequency(1000, CPU_CLOCK_MHZ * 1000000); @@ -269,7 +268,7 @@ int main(void) desig_get_unique_id_as_dfu(usb_serial_number); - DEBUG("Entered boot-loader\n"); + DEBUG("DFU: Entered boot-loader (SN %s)\n", usb_serial_number); // Simulate USB disconnect gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO11 | GPIO12); @@ -298,7 +297,7 @@ restart: ; } } - volatile u32 *app = (volatile u32 *) APP_ADDRESS; + volatile u32 *app = (volatile u32 *) BOOTLOADER_APP_START; u32 sp = app[0]; u32 pc = app[1]; @@ -308,13 +307,15 @@ restart: ; goto restart; } +#ifdef DEBUG_USART while (!usart_get_flag(DEBUG_USART, USART_FLAG_TC)) ; +#endif debug_led(0); cm_disable_interrupts(); /* Set vector table base address. */ - SCB_VTOR = APP_ADDRESS & 0xFFFF; + SCB_VTOR = BOOTLOADER_APP_START & 0xFFFF; /* Initialize master stack pointer. */ asm volatile("msr msp, %0"::"g" (sp)); -- 2.39.2