ADVES / Mbed 2 deprecated STM32F103C8T6_USBDFU

Dependencies:   mbed mbed-STM32F103C8T6 USBDevice_STM32F103

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "stm32f103c8t6.h"
00002 #include "mbed.h"
00003 #include "USBDFU.h"
00004 
00005 /*
00006  * This is an example program demonstrating a USB DFU runtime combined with
00007  * a USB DFU bootloader that does not require an offset.
00008  * 
00009  * It normally blinks an LED at 1Hz, but when it receives a DFU detach
00010  * request over USB (eg, by running "dfu-util -d 1234:0006 -e"), it
00011  * blinks 3 times rapidly and resets into the bootloader.
00012  *
00013  * New programs can be uploaded by running
00014  * dfu-util -d 1234:0006,1209:db42 -D your_firmware.bin
00015  *
00016  * The companion bootloader source can be found on GitHub:
00017  * https://github.com/devanlai/dapboot/tree/highboot
00018  *
00019  */
00020 
00021 DigitalOut  myled(LED1);
00022 
00023 bool detached = false;
00024 void onDetachRequested() {
00025     detached = true;
00026 }
00027 
00028 void resetIntoBootloader() {
00029     // Turn on write access to the backup registers
00030     __PWR_CLK_ENABLE();
00031     HAL_PWR_EnableBkUpAccess();
00032     
00033     // Write the magic value to force the bootloader to run
00034     BKP->DR2 = 0x544F;
00035     BKP->DR1 = 0x4F42;
00036     
00037     HAL_PWR_DisableBkUpAccess();
00038     
00039     // Reset and let the bootloader run
00040     NVIC_SystemReset();
00041 }
00042 
00043 int main() {
00044     confSysClock();     //Configure system clock (72MHz HSE clock, 48MHz USB clock)
00045     
00046     USBDFU usbDFU(0x1234, 0x0006, 0x0001, false);
00047     usbDFU.attach(onDetachRequested);
00048     
00049     while(1) {
00050         // Check the DFU status
00051         if (!usbDFU.configured()) {
00052             usbDFU.connect(false);
00053         }
00054         if (detached) {
00055             for (int i=0; i < 5; i++) {
00056                 myled = 1;
00057                 wait_ms(100);
00058                 myled = 0;
00059                 wait_ms(100);
00060             }
00061             resetIntoBootloader();
00062         }
00063         
00064         // Do normal stuff
00065         myled = !myled;
00066         wait_ms(1000);
00067     }
00068 }
00069