Reflashing the Bootloader

Reflashing the Bootloader

When I first set-up Eclipse on Mac to allow me to do some debugging on my Crazyflie 2.0, I built and flashed a debug version of the firmware for the first time. The “debug” target in Eclipse creates a build with the parameter “CLOAD=0.” This means that the debug firmware is flashed at the start of the Crazyflie 2.0’s STM32 flash memory. This where the radio bootloader is normally written so, if one existed before flashing debug firmware, it doesn’t exist after it. In any case, I spent a while scratching my head months later when, while trying to update my Crazyflie 2.0 firmware over-the-air (OTA), I found the Crazyradio couldn’t connect to the bootloader! Of course! There wasn’t one there any longer.

For a moment I panicked, thinking I’d smashed something important when I built my debug firmware, then I remembered an obscure thing I’d read on the wiki about DFU
The Crazyflie 2.0 allows for device firmware update (DFU) of the STM32F4 over a USB connection using the open-source dfu-util application. It’s a nice way to flash the STM32 firmware without using a Crazyradio OTA. Fortunately, when you want your over-the-air bootloading capabilities back, DFU comes in handy there as well!

No matter how you got in the situation, you need to reflash the radio bootloader. Bitcraze has a GitHub repository for the Crazyflie 2.0 STM32F4 Bootloader firmware but lucky for us it rarely ever changes and the only formal release is 1.0 available at GitHub and also hosted here for posterity.

Getting the Crazyflie 2.0 into DFU mode is a little tricky but not impossible. Follow these instructions exactly and you should be good.

1) Plug a USB to micro-USB cable into your computer.
2) Disconnect the battery from your Crazyflie 2.0.
3) Push in the power button on your Crazyflie 2.0. It’s small so don’t apply too much pressure. If you break it off, you can fix it! While continuing to hold in the power button (this is the tricky part), plug the micro-USB connector end of the cable into the Crazyflie.
4) Continue holding the power button for a few more seconds (about five seconds since you plugged the USB cable in). Watch the blue LED on the M2 arm until it displays a secondary (faster) blink rate (flashes about once a second). Then you can release the button.
5) Use the dfu-util to flash the bootloader. The ‘cf2loader-1.0.bin’ parameter to ‘-D’ is the file path/name to the bootloader binary. The other important bits here are the identifiers after the ‘-d’ argument and the address after the ‘-s’ argument. Keep those in mind as I will discuss them later.

sudo dfu-util -d 0483:df11 -a 0 -s 0x08000000 -D cf2loader-1.0.bin

This is enough to get the bootloader flashed again. You can also use DFU to update the STM32 firmware but I won’t go into that in this post.
6) Remove the USB cable from the Crazyflie 2.0, then reconnect the battery and power on using the on/off button.

The final test of course, is to build some firmware and flash it OTA. You can either flash it using the Python client or “make cload” in the firmware Makefile. Either way, if it works (you connect successfully to the bootloader and execute a flash), you’re golden.

Now, for the important bits in the dfu-util command in step 5 above, here’s the details on those. The ‘-d’ is the device (Crazyflie 2.0) vendor and product id numbers in hexadecimal. Those are 0483 and df11 respectively. The arguments are passed as 0483:df11 to the ‘-d’ switch. The ‘-s’ is the start address for where to put the firmware in the STM32 flash memory. In this case it is 0x08000000.

Now, if you lost your bootloader, you know how to get it back again!

Comments are closed.