Blinky example with USB Device Firmware Upgrade (DFU) detach support for use with a no-offset bootloader

Dependencies:   USBDevice_STM32F103 mbed-STM32F103C8T6 mbed

Fork of STM32F103C8T6_USBSerial by Zoltan Hudak

Overview

This is an example application that runs on an STM32F103C8 "bluepill" board using a USB DFU bootloader. Once the bootloader and this application have been uploaded, it blinks just like any other blinky example. However, it also enumerates as a USB DFU runtime device, making it possible to re-enable the bootloader and upload new code over USB.

Installing the bootloader

A pre-built copy of the bootloader can be downloaded from GitHub.

Note that this bootloader primarily lives in the upper 8KiB of flash, so no application offset is needed (and the online compiler doesn't let you set an application offset, anyways).

See Zoltan Hudak's page for an example of using an Nucleo board to program a standalone board.

Uploading programs

Once the bootloader has been flashed, it will enumerate as a USB DFU device. dfu-util can be used to upload new programs to the board over USB. Note that on Windows, you may need to use a program such as Zadig to associate the WinUSB driver with the DFU interface.

You can check if the bootloader was uploaded successfully by seeing if dfu-util can find it using dfu-util -l

dfu-util 0.8

Copyright 2005-2009 Weston Schmidt, Harald Welte and OpenMoko Inc.
Copyright 2010-2014 Tormod Volden and Stefan Schmidt
This program is Free Software and has ABSOLUTELY NO WARRANTY
Please report bugs to http://sourceforge.net/p/dfu-util/tickets/

dfu-util: Cannot open DFU device 0a5c:21e6
Found DFU: [1209:db42] ver=0100, devnum=25, cfg=1, intf=0, alt=0, name="DAPBoot DFU", serial="463112677267505055FF7206"

If you get something like "dfu-util: Cannot open DFU device 1209:db42", you may need to fix your permissions.

You can then upload your code over USB using:

dfu-util -d 1209:db42 -D STM32F103C8T6_USBDFU_NUCLEO_F103RB.bin

If all goes well, you should see something along the lines of

dfu-util 0.8

Copyright 2005-2009 Weston Schmidt, Harald Welte and OpenMoko Inc.
Copyright 2010-2014 Tormod Volden and Stefan Schmidt
This program is Free Software and has ABSOLUTELY NO WARRANTY
Please report bugs to http://sourceforge.net/p/dfu-util/tickets/

dfu-util: Invalid DFU suffix signature
dfu-util: A valid DFU suffix will be required in a future dfu-util release!!!
Opening DFU capable USB device...
ID 1209:db42
Run-time device DFU version 0110
Claiming USB DFU Interface...
Setting Alternate Setting #0 ...
Determining device status: state = dfuIDLE, status = 0
dfuIDLE, continuing
DFU mode device DFU version 0110
Device returned transfer size 1024
Copying data from PC to DFU device
Download	[=========================] 100%        29716 bytes
Download done.
state(7) = dfuMANIFEST, status(0) = No error condition is present
dfu-util: unable to read DFU status after completion

The board should now blink once per second. If you run "dfu-util -l" again, it should now show the DFU runtime:

Found Runtime: [1234:0006] ver=0001, devnum=26, cfg=1, intf=0, alt=0, name="DFU", serial="0123456789"

Since it has a different USB VID/PID pair than the bootloader, if you ran into any permissions/drivers issues with the bootloader, you will need to solve them again for the application.

You can switch from the application to the back to bootloader by running

dfu-util -d 1234:0006 -e

dfu-util can also handle switching from the application to the bootloader and uploading code in one step:

dfu-util -d 1234:0006,1209:db42 -D your_firmware.bin

Files at this revision

API Documentation at this revision

Comitter:
hudakz
Date:
Fri Jul 08 21:19:45 2016 +0000
Child:
1:0e1269402efd
Commit message:
Initial revision

Changed in this revision

USBDevice_STM32F103.lib Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-STM32F103C8T6.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/USBDevice_STM32F103.lib	Fri Jul 08 21:19:45 2016 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/users/hudakz/code/USBDevice_STM32F103/#48fe9050cb4a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Jul 08 21:19:45 2016 +0000
@@ -0,0 +1,21 @@
+#include "stm32f103c8t6.h"
+#include "mbed.h"
+#include "USBSerial.h"
+
+Serial      pc(PA_2, PA_3);
+DigitalOut  myled(LED1);
+
+int main() {
+    confSysClock(48);     // Configuring system clock for USB
+    USBSerial usbSerial;
+    
+    pc.baud(9600);
+
+    while(1) {
+        myled = !myled;
+        pc.printf("I am a serial port\r\n");
+        usbSerial.printf("I am an USB serial port\r\n");
+        wait_ms(1000);
+    }
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-STM32F103C8T6.lib	Fri Jul 08 21:19:45 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/hudakz/code/mbed-STM32F103C8T6/#306609fe9dc8
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Fri Jul 08 21:19:45 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/6c34061e7c34
\ No newline at end of file