Devan Lai / Mbed 2 deprecated STM32F103C8T6_WebUSBDFU Featured

Dependencies:   USBDevice_STM32F103 mbed-STM32F103C8T6 mbed

Fork of STM32F103C8T6_USBDFU by Devan Lai

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 "WebUSBDFU.h"
00004 /*
00005  * This is an example program demonstrating a WebUSB DFU runtime that can be
00006  * combined with a bootloader that does not require an offset.
00007  * 
00008  * It normally blinks an LED at 1Hz, but when it receives a DFU detach
00009  * request over USB, it blinks 3 times rapidly and resets into the bootloader.
00010  *
00011  * The companion bootloader source and prebuilt binaries can be found on GitHub:
00012  * https://github.com/devanlai/dapboot/tree/highboot
00013  * https://github.com/devanlai/dapboot/releases/tag/v0.1
00014  *
00015  * On Chrome 54 and later (or Chrome 52 with certain flags set), the browser can
00016  * access the application / bootloader to directly upload new firmware.
00017  *
00018  * The generic WebUSB dfu-util demo is available here:
00019  * https://devanlai.github.io/webdfu/dfu-util/
00020  *
00021  * An experimental demo to build and download mbed binaries over WebUSB is available here:
00022  * https://devanlai.github.io/webdfu/mbed-download/
00023  *
00024  */
00025 
00026 DigitalOut  myled(LED1);
00027 
00028 bool detached = false;
00029 void onDetachRequested() {
00030     detached = true;
00031 }
00032 
00033 void resetIntoBootloader() {
00034     // Turn on write access to the backup registers
00035     __PWR_CLK_ENABLE();
00036     HAL_PWR_EnableBkUpAccess();
00037     
00038     // Write the magic value to force the bootloader to run
00039     BKP->DR2 = 0x544F;
00040     BKP->DR1 = 0x4F42;
00041     
00042     HAL_PWR_DisableBkUpAccess();
00043     
00044     // Reset and let the bootloader run
00045     NVIC_SystemReset();
00046 }
00047 
00048 int main() {
00049     confSysClock();     //Configure system clock (72MHz HSE clock, 48MHz USB clock)
00050     
00051     /* Note: 1209:0001 is a test VID/PID pair - it should be changed before using in
00052      * a real application */
00053     WebUSBDFU usbDFU(0x1209, 0x0001, 0x0001, false);
00054     usbDFU.attach(onDetachRequested);
00055     
00056     while(1) {
00057         // Check the DFU status
00058         if (!usbDFU.configured()) {
00059             usbDFU.connect(false);
00060         }
00061         if (detached) {
00062             for (int i=0; i < 3; i++) {
00063                 myled = 1;
00064                 wait_ms(100);
00065                 myled = 0;
00066                 wait_ms(100);
00067             }
00068             resetIntoBootloader();
00069         }
00070         
00071         // Do normal stuff
00072         myled = !myled;
00073         wait_ms(500);
00074     }
00075 }
00076