[SOLVED] Enter F103 Serial bootloader via software - Serial upload without toggling boot pins

Post Reply
BenjiHansell
Posts: 18
Joined: Sun Mar 06, 2016 12:21 am

[SOLVED] Enter F103 Serial bootloader via software - Serial upload without toggling boot pins

Post by BenjiHansell » Tue Aug 29, 2017 10:12 pm

Does anyone know if it is possible to create a hardware configuration for an F1 chip in which a USB to serial chip is (permanently) connected to the F1 chip for use programming and debugging, but where the user does not need to toggle boot pins to use their programmed chip (like an Arduino Nano)?

This functionality is present in (e.g. Naze32) F1 flight controller boards. To update the firmware you simply open the Cleanflight Configurator, plug in your board via USB and click flash. There is no need for the user to toggle boot pins and the permanent UART bootloader is definitely used.

On the schematic I cannot see anything special surrounding the CP2102 USB to UART bridge. Both BOOT0 and BOOT1 are pulled down, which as far as I know should allow booting into firmware but not flashing.

P.S. I am aware that there is an excellent USB bootloader available.

User avatar
RogerClark
Posts: 7174
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Serial upload without toggling boot pins

Post by RogerClark » Tue Aug 29, 2017 10:39 pm

Look in the firmware that is installed onto that flight controller.

It's possible it monitors the hardware Serial lines and when it receives a command, it reboots the MCU into its built in serial bootloader

This would save some space as you don't need a bootloader to upload as the code for the serial bootloader is part of the MCUs core functionality.

But it has all the same problems as a bootloader e.g. If your code crashes, it's won't be listening for the command to go into download mode

devan
Posts: 50
Joined: Sat May 14, 2016 1:45 am

Re: Serial upload without toggling boot pins

Post by devan » Wed Aug 30, 2017 4:55 am

Roger's guess looks pretty accurate to me. In case you don't know exactly what to look for, here's how it works in the Cleanflight code:

1. The serial port reads a command that tells it to switch to the bootloader.

Code: Select all

void serialEvaluateNonMspData(serialPort_t *serialPort, uint8_t receivedChar)
{
#ifndef USE_CLI
    UNUSED(serialPort);
#else
    if (receivedChar == '#') {
        cliEnter(serialPort);
    }
#endif
    if (receivedChar == serialConfig()->reboot_character) {
        systemResetToBootloader();
    }
}
2. It sets a flag and then resets itself to run the bootloader. It resets itself because bootloaders often assume that nothing has been initialized (ie, no interrupts, watchdogs, etc) and resetting is the easiest way to get back to clean slate. The flag is often some magic value in RAM or in a register that is guaranteed to survive a soft reset.

Code: Select all

void systemResetToBootloader(void)
{
    // 1FFFF000 -> 20000200 -> SP
    // 1FFFF004 -> 1FFFF021 -> PC

    *((uint32_t *)0x20004FF0) = 0xDEADBEEF; // 20KB STM32F103
    systemReset();
}
3. Early in the boot process, it checks for the bootloader flag. If the flag is set, it stops initializing the hardware and instead jumps to the ROM bootloader.

Code: Select all

void systemInit(void)
{
    checkForBootLoaderRequest();

    SetSysClock(false);
    ... snip ...

Code: Select all

void checkForBootLoaderRequest(void)
{
    void(*bootJump)(void);

    if (*((uint32_t *)0x20004FF0) == 0xDEADBEEF) {

        *((uint32_t *)0x20004FF0) = 0x0;

        __enable_irq();
        __set_MSP(*((uint32_t *)0x1FFFF000));

        bootJump = (void(*)(void))(*((uint32_t *) 0x1FFFF004));
        bootJump();
        while (1);
    }
}
As Roger mentioned, it's convenient to enter the bootloader without fiddling with the pins, but you should make provisions for recovering for that 1 in 50 times you mess up and flash completely broken / wrong firmware onto it. The schematic that you linked has a jumper on BOOT0 so that it's still possible to manually trigger the bootloader without working firmware.

BenjiHansell
Posts: 18
Joined: Sun Mar 06, 2016 12:21 am

Re: Serial upload without toggling boot pins

Post by BenjiHansell » Wed Aug 30, 2017 11:53 am

Okay this makes sense now, thank you both for the explanation.

Post Reply