Browse Source

Add dfu bootloader update application and use absolute delay for stlink

Hello,

appended 3 patches
- adds a dfu-bootloader appliaction
- uses absolute delays when waiting for pull-up delays on the STLINK
(hopefully fixes issue #30)

Updating the dfu-bootloader by additional application is helpfull for the
STLINK, as for flashing the bootloader by SWDb otherwise jumpers need to be
soldered or external SWD is not possible.
Use like:
- dfu-util -s 0x08002000:leave -D dfu_upgrade.bin
- dfu-util -s 0x08000000:leave -D blackmagic_dfu.bin
- Push reset buttom and reconnect to enter new dfu bootloader
- dfu-util -s 0x08002000:leave -D blackmagic.bin
--
Uwe Bonnes                bon@elektron.ikp.physik.tu-darmstadt.de

Institut fuer Kernphysik  Schlossgartenstrasse 9  64289 Darmstadt
--------- Tel. 06151 162516 -------- Fax. 06151 164321 ----------
>From fae5022c304a8866f056ea66660ac7ce3809dcf8 Mon Sep 17 00:00:00 2001
From: Uwe Bonnes <bon@elektron.ikp.physik.tu-darmstadt.de>
Date: Sun, 20 Oct 2013 15:00:36 +0200
Subject: F1: Prepare to update dfu bootloader via DFU with additional
 application.
pull/36/merge
bon@elektron.ikp.physik.tu-darmstadt.de 11 years ago
committed by Gareth McMullin
parent
commit
0c9d5d8166
  1. 6
      .gitignore
  2. 2
      src/platforms/native/platform.h
  3. 6
      src/platforms/native/usbdfu.c
  4. 13
      src/platforms/stlink/Makefile.inc
  5. 127
      src/platforms/stlink/dfu_upgrade.c
  6. 2
      src/platforms/stlink/platform.h
  7. 33
      src/platforms/stlink/usbdfu.c
  8. 17
      src/platforms/stm32/dfu_f1.c
  9. 17
      src/platforms/stm32/dfucore.c
  10. 16
      src/platforms/stm32/usbdfu.h
  11. 2
      src/platforms/swlink/platform.h
  12. 6
      src/platforms/swlink/usbdfu.c

6
.gitignore

@ -1,8 +1,8 @@
blackmagic
blackmagic.bin
*.bin
*.hex
blackmagic_dfu
blackmagic_dfu.bin
blackmagic_dfu.hex
dfu_upgrade
mapfile
*.o
*.d

2
src/platforms/native/platform.h

@ -37,8 +37,10 @@
#define PLATFORM_HAS_TRACESWO
#define BOARD_IDENT "Black Magic Probe"
#define BOARD_IDENT_DFU "Black Magic Probe (Upgrade)"
#define BOARD_IDENT_UPD "Black Magic Probe (DFU Upgrade)"
#define DFU_IDENT "Black Magic Firmware Upgrade"
#define DFU_IFACE_STRING "@Internal Flash /0x08000000/8*001Ka,120*001Kg"
#define UPD_IFACE_STRING "@Internal Flash /0x08000000/8*001Kg"
extern usbd_device *usbdev;
#define CDCACM_GDB_ENDPOINT 1

6
src/platforms/native/usbdfu.c

@ -25,6 +25,8 @@
#include "usbdfu.h"
uint32_t app_address = 0x08002000;
void dfu_detach(void)
{
/* USB device must detach, we just reset... */
@ -38,7 +40,7 @@ int main(void)
if(gpio_get(GPIOB, GPIO12))
dfu_jump_app_if_valid();
dfu_protect_enable();
dfu_protect(DFU_MODE);
rcc_clock_setup_in_hse_8mhz_out_72mhz();
systick_set_clocksource(STK_CSR_CLKSOURCE_AHB_DIV8);
@ -56,7 +58,7 @@ int main(void)
gpio_set_mode(GPIOB, GPIO_MODE_INPUT,
GPIO_CNF_INPUT_FLOAT, GPIO2 | GPIO10);
dfu_init(&stm32f103_usb_driver);
dfu_init(&stm32f103_usb_driver, DFU_MODE);
gpio_set(GPIOA, GPIO8);
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,

13
src/platforms/stlink/Makefile.inc

@ -18,7 +18,7 @@ SRC += cdcacm.c \
platform.c \
usbuart.c \
all: blackmagic.bin blackmagic_dfu.bin blackmagic_dfu.hex
all: blackmagic.bin blackmagic_dfu.bin blackmagic_dfu.hex dfu_upgrade.bin dfu_upgrade.hex
blackmagic.bin: blackmagic
$(OBJCOPY) -O binary $^ $@
@ -32,6 +32,15 @@ blackmagic_dfu.bin: blackmagic_dfu
blackmagic_dfu.hex: blackmagic_dfu
$(OBJCOPY) -O ihex $^ $@
dfu_upgrade: dfu_upgrade.o dfucore.o dfu_f1.o
$(CC) $^ -o $@ $(LDFLAGS)
dfu_upgrade.bin: dfu_upgrade
$(OBJCOPY) -O binary $^ $@
dfu_upgrade.hex: dfu_upgrade
$(OBJCOPY) -O ihex $^ $@
host_clean:
-rm blackmagic.bin blackmagic_dfu blackmagic_dfu.bin blackmagic_dfu.hex
-rm blackmagic.bin blackmagic_dfu blackmagic_dfu.bin blackmagic_dfu.hex dfu_upgrade dfu_upgrade.bin dfu_upgrade.hex

127
src/platforms/stlink/dfu_upgrade.c

@ -0,0 +1,127 @@
/*
* This file is part of the Black Magic Debug project.
*
* Copyright (C) 2013 Gareth McMullin <gareth@blacksphere.co.nz>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string.h>
#include <libopencm3/cm3/systick.h>
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/cm3/scb.h>
#include "usbdfu.h"
uint32_t app_address = 0x08000000;
static uint8_t rev;
static uint16_t led_idle_run;
static uint32_t led2_state = 0;
void dfu_detach(void)
{
/* Disconnect USB cable by resetting USB Device
and pulling USB_DP low*/
rcc_peripheral_reset(&RCC_APB1RSTR, RCC_APB1ENR_USBEN);
rcc_peripheral_clear_reset(&RCC_APB1RSTR, RCC_APB1ENR_USBEN);
rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USBEN);
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN);
gpio_clear(GPIOA, GPIO12);
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
GPIO_CNF_OUTPUT_OPENDRAIN, GPIO12);
/* Pull PB0 (T_NRST) low
*/
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPBEN);
gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ,
GPIO_CNF_OUTPUT_OPENDRAIN, GPIO0);
gpio_clear(GPIOB, GPIO0);
SCB_VTOR = 0;
scb_reset_core();
}
void stlink_set_rev(void)
{
int i;
/* First, get Board revision by pulling PC13/14 up. Read
* 11 for ST-Link V1, e.g. on VL Discovery, tag as rev 0
* 10 for ST-Link V2, e.g. on F4 Discovery, tag as rev 1
*/
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN);
gpio_set_mode(GPIOC, GPIO_MODE_INPUT,
GPIO_CNF_INPUT_PULL_UPDOWN, GPIO14 | GPIO13);
gpio_set(GPIOC, GPIO14 | GPIO13);
for (i = 0; i < 100; i++)
rev = (~(gpio_get(GPIOC, GPIO14 | GPIO13)) >> 13) & 3;
switch (rev) {
case 0:
led_idle_run = GPIO8;
break;
default:
led_idle_run = GPIO9;
}
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
GPIO_CNF_OUTPUT_PUSHPULL, led_idle_run);
}
int main(void)
{
rcc_clock_setup_in_hse_8mhz_out_72mhz();
stlink_set_rev();
systick_set_clocksource(STK_CTRL_CLKSOURCE_AHB_DIV8);
systick_set_reload(900000);
dfu_protect(UPD_MODE);
/* Handle USB disconnect/connect */
/* Just in case: Disconnect USB cable by resetting USB Device
* and pulling USB_DP low
* Device will reconnect automatically as Pull-Up is hard wired*/
rcc_peripheral_reset(&RCC_APB1RSTR, RCC_APB1ENR_USBEN);
rcc_peripheral_clear_reset(&RCC_APB1RSTR, RCC_APB1ENR_USBEN);
rcc_peripheral_enable_clock(&RCC_APB1ENR, RCC_APB1ENR_USBEN);
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPAEN);
gpio_clear(GPIOA, GPIO12);
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
GPIO_CNF_OUTPUT_OPENDRAIN, GPIO12);
systick_interrupt_enable();
systick_counter_enable();
dfu_init(&stm32f103_usb_driver, UPD_MODE);
dfu_main();
}
void sys_tick_handler(void)
{
if (rev == 0) {
gpio_toggle(GPIOA, led_idle_run);
} else {
if (led2_state & 1) {
gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ,
GPIO_CNF_OUTPUT_PUSHPULL, led_idle_run);
gpio_set(GPIOA, led_idle_run);
} else {
gpio_set_mode(GPIOA, GPIO_MODE_INPUT,
GPIO_CNF_INPUT_ANALOG, led_idle_run);
}
led2_state++;
}
}

2
src/platforms/stlink/platform.h

@ -36,8 +36,10 @@
#define CDCACM_PACKET_SIZE 64
#define BOARD_IDENT "Black Magic Probe (STLINK), (Firmware 1.5" VERSION_SUFFIX ", build " BUILDDATE ")"
#define BOARD_IDENT_DFU "Black Magic (Upgrade) for STLink/Discovery, (Firmware 1.5" VERSION_SUFFIX ", build " BUILDDATE ")"
#define BOARD_IDENT_UPD "Black Magic (DFU Upgrade) for STLink/Discovery, (Firmware 1.5" VERSION_SUFFIX ", build " BUILDDATE ")"
#define DFU_IDENT "Black Magic Firmware Upgrade (STLINK)"
#define DFU_IFACE_STRING "@Internal Flash /0x08000000/8*001Ka,56*001Kg"
#define UPD_IFACE_STRING "@Internal Flash /0x08000000/8*001Kg"
extern usbd_device *usbdev;
#define CDCACM_GDB_ENDPOINT 1

33
src/platforms/stlink/usbdfu.c

@ -29,24 +29,31 @@ static uint8_t rev;
static uint16_t led_idle_run;
static uint32_t led2_state = 0;
uint32_t app_address = 0x08002000;
static int stlink_test_nrst(void)
{
/* Test if JRST/NRST is pulled down*/
int i;
uint16_t nrst;
uint16_t pin;
uint32_t systick_value;
systick_set_clocksource(STK_CTRL_CLKSOURCE_AHB_DIV8);
systick_set_reload(0xffffff); /* no underflow for about 16.7 seconds*/
systick_counter_enable();
/* systick ist now running with 1 MHz, systick counts down */
/* First, get Board revision by pulling PC13/14 up. Read
* 11 for ST-Link V1, e.g. on VL Discovery, tag as rev 0
* 10 for ST-Link V2, e.g. on F4 Discovery, tag as rev 1
*/
for (i = 0; i < 0x200; i++) {
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN);
gpio_set_mode(GPIOC, GPIO_MODE_INPUT,
GPIO_CNF_INPUT_PULL_UPDOWN, GPIO14 | GPIO13);
gpio_set(GPIOC, GPIO14 | GPIO13);
rev = (~(gpio_get(GPIOC, GPIO14 | GPIO13)) >> 13) & 3;
}
rcc_peripheral_enable_clock(&RCC_APB2ENR, RCC_APB2ENR_IOPCEN);
gpio_set_mode(GPIOC, GPIO_MODE_INPUT,
GPIO_CNF_INPUT_PULL_UPDOWN, GPIO14 | GPIO13);
gpio_set(GPIOC, GPIO14 | GPIO13);
systick_value = systick_get_value();
while (systick_get_value() > (systick_value - 1000)); /* Wait 1 msec*/
rev = (~(gpio_get(GPIOC, GPIO14 | GPIO13)) >> 13) & 3;
switch (rev) {
case 0:
pin = GPIO1;
@ -62,8 +69,10 @@ static int stlink_test_nrst(void)
gpio_set_mode(GPIOB, GPIO_MODE_INPUT,
GPIO_CNF_INPUT_PULL_UPDOWN, pin);
gpio_set(GPIOB, pin);
for (i = 0; i < 100; i++)
nrst = gpio_get(GPIOB, pin);
systick_value = systick_get_value();
while (systick_get_value() > (systick_value - 20000)); /* Wait 20 msec*/
nrst = gpio_get(GPIOB, pin);
systick_counter_disable();
return (nrst) ? 1 : 0;
}
@ -92,7 +101,7 @@ int main(void)
if(((GPIOA_CRL & 0x40) == 0x40) && stlink_test_nrst())
dfu_jump_app_if_valid();
dfu_protect_enable();
dfu_protect(DFU_MODE);
rcc_clock_setup_in_hse_8mhz_out_72mhz();
systick_set_clocksource(STK_CSR_CLKSOURCE_AHB_DIV8);
@ -113,7 +122,7 @@ int main(void)
systick_interrupt_enable();
systick_counter_enable();
dfu_init(&stm32f103_usb_driver);
dfu_init(&stm32f103_usb_driver, DFU_MODE);
dfu_main();
}

17
src/platforms/stm32/dfu_f1.c

@ -59,8 +59,9 @@ uint32_t dfu_poll_timeout(uint8_t cmd, uint32_t addr, uint16_t blocknum)
return 100;
}
void dfu_protect_enable(void)
void dfu_protect(dfu_mode_t mode)
{
if (mode == DFU_MODE) {
#ifdef DFU_SELF_PROTECT
if ((FLASH_WRPR & 0x03) != 0x00) {
flash_unlock();
@ -72,19 +73,25 @@ void dfu_protect_enable(void)
flash_program_option_bytes(FLASH_OBP_WRP10, 0x03FC);
}
#endif
}
else if (mode == UPD_MODE) {
flash_unlock();
FLASH_CR = 0;
flash_erase_option_bytes();
}
}
void dfu_jump_app_if_valid(void)
{
/* Boot the application if it's valid */
if((*(volatile uint32_t*)APP_ADDRESS & 0x2FFE0000) == 0x20000000) {
if((*(volatile uint32_t*)app_address & 0x2FFE0000) == 0x20000000) {
/* Set vector table base address */
SCB_VTOR = APP_ADDRESS & 0x1FFFFF; /* Max 2 MByte Flash*/
SCB_VTOR = app_address & 0x1FFFFF; /* Max 2 MByte Flash*/
/* Initialise master stack pointer */
asm volatile ("msr msp, %0"::"g"
(*(volatile uint32_t*)APP_ADDRESS));
(*(volatile uint32_t*)app_address));
/* Jump to application */
(*(void(**)())(APP_ADDRESS + 4))();
(*(void(**)())(app_address + 4))();
}
}

17
src/platforms/stm32/dfucore.c

@ -121,6 +121,14 @@ static const char *usb_strings[] = {
DFU_IFACE_STRING,
};
static const char *usb_strings_upd[] = {
"Black Sphere Technologies",
BOARD_IDENT_UPD,
serial_no,
/* This string is used by ST Microelectronics' DfuSe utility */
UPD_IFACE_STRING,
};
static uint32_t get_le32(const void *vp)
{
const uint8_t *p = vp;
@ -158,8 +166,8 @@ usbdfu_getstatus_complete(usbd_device *dev, struct usb_setup_data *req)
flash_unlock();
if(prog.blocknum == 0) {
uint32_t addr = get_le32(prog.buf + 1);
if (addr < APP_ADDRESS ||
(addr >= max_address)) {
if (addr < app_address ||
(addr >= max_address)) {
flash_lock();
usbd_ep_stall_set(dev, 0, 1);
return;
@ -251,11 +259,12 @@ static int usbdfu_control_request(usbd_device *dev,
return 0;
}
void dfu_init(const usbd_driver *driver)
void dfu_init(const usbd_driver *driver, dfu_mode_t mode)
{
get_dev_unique_id(serial_no);
usbdev = usbd_init(driver, &dev, &config, usb_strings, 4,
usbdev = usbd_init(driver, &dev, &config,
(mode == DFU_MODE)?usb_strings:usb_strings_upd, 4,
usbd_control_buffer, sizeof(usbd_control_buffer));
usbd_register_control_callback(usbdev,

16
src/platforms/stm32/usbdfu.h

@ -19,25 +19,25 @@
#include <libopencm3/usb/usbd.h>
#ifdef STM32F4
# define APP_ADDRESS 0x08010000
#else
# define APP_ADDRESS 0x08002000
#endif
/* Commands sent with wBlockNum == 0 as per ST implementation. */
#define CMD_SETADDR 0x21
#define CMD_ERASE 0x41
extern uint32_t app_address;
typedef enum {
DFU_MODE = 0,
UPD_MODE = 1
} dfu_mode_t;
/* dfucore.c - DFU core, common to libopencm3 platforms. */
void dfu_init(const usbd_driver *driver);
void dfu_init(const usbd_driver *driver, dfu_mode_t mode);
void dfu_main(void);
/* Device specific functions */
void dfu_check_and_do_sector_erase(uint32_t sector);
void dfu_flash_program_buffer(uint32_t baseaddr, void *buf, int len);
uint32_t dfu_poll_timeout(uint8_t cmd, uint32_t addr, uint16_t blocknum);
void dfu_protect_enable(void);
void dfu_protect(dfu_mode_t mode);
void dfu_jump_app_if_valid(void);
/* Platform specific function */

2
src/platforms/swlink/platform.h

@ -36,8 +36,10 @@
#define CDCACM_PACKET_SIZE 64
#define BOARD_IDENT "Black Magic Probe (SWLINK), (Firmware 1.5" VERSION_SUFFIX ", build " BUILDDATE ")"
#define BOARD_IDENT_DFU "Black Magic (Upgrade), STM8S Discovery, (Firmware 1.5" VERSION_SUFFIX ", build " BUILDDATE ")"
#define BOARD_IDENT_UPD "Black Magic (DFU Upgrade), STM8S Discovery, (Firmware 1.5" VERSION_SUFFIX ", build " BUILDDATE ")"
#define DFU_IDENT "Black Magic Firmware Upgrade (SWLINK)"
#define DFU_IFACE_STRING "@Internal Flash /0x08000000/8*001Ka,56*001Kg"
#define UPD_IFACE_STRING "@Internal Flash /0x08000000/8*001Kg"
extern usbd_device *usbdev;
#define CDCACM_GDB_ENDPOINT 1

6
src/platforms/swlink/usbdfu.c

@ -25,6 +25,8 @@
#include "usbdfu.h"
uint32_t app_address = 0x08002000;
void dfu_detach(void)
{
/* Disconnect USB cable by resetting USB Device
@ -58,7 +60,7 @@ int main(void)
if(((GPIOA_CRL & 0x40) == 0x40) && pin_b)
dfu_jump_app_if_valid();
dfu_protect_enable();
dfu_protect(DFU_MODE);
rcc_clock_setup_in_hse_8mhz_out_72mhz();
systick_set_clocksource(STK_CSR_CLKSOURCE_AHB_DIV8);
@ -83,7 +85,7 @@ int main(void)
systick_interrupt_enable();
systick_counter_enable();
dfu_init(&stm32f103_usb_driver);
dfu_init(&stm32f103_usb_driver, DFU_MODE);
dfu_main();
}

Loading…
Cancel
Save