January 15th 2023

The built-in Bootloader of the chip updates the program to the Flash through the burner, via the chip manufacturer’s agreement.

I have done many Bootloader upgrade products, and I feel that Bootloader is still very interesting. You can update the remaining programs through a section of the program on the chip, in order to update the product software. The built-in Bootloader of the chip updates the program to the Flash through the burner, via the chip manufacturer’s agreement. Then, why do we need part of the space to write a Bootloader? Because we want to upgrade the software in the chip in our own way.

There are too many ways of transmission. Theoretically, almost all communication methods can be upgraded, including CAN, SPI, IIC, USART, etc. I have done CAN upgrade, USART upgrade, SD card upgrade, and OTA upgrade on cars. Among them, CAN upgrade and USART upgrade are both online upgrades, and the SD card upgrade can be called offline upgrade, which means you can upgrade an SD card offline without using PC terminal. (If there are hardware upgrade devices, CAN and USART can also complete offline upgrades).

Speaking of SD card upgration, if you want to upgrade it offline, the device needs to have an SD card slot. When upgrading, put the upgration file into the SD card, and then insert the card in the card slot to complete the upgration. (SPI driver is used by the SD card, so it can theoretically be called SPI upgration…)

Here we use STM32F103RCT6 chip to make SD card Bootloader.


The one I used here has Atom mini development board, STM32F103RCT6 chip, and SD card slot with SPI driver.


(register coding, in order to reduce the space occupied by Bootloader code)

1) The SD card uses SPI driver.

SPI1_Init() isto initialized, and SPI1_ReadWriteByte for reading and writing.

ReadWriteByte for reading and writing


2)SD card driver and FATFS file system.

The programs for reading SD card, writing SD card, initializing SD card, etc., are all already written and can be used directly. FATFS file system is also used here, and two files need to be transplanted. Those steps will not be introduced here.

initializing SD card

3)The operation of chip Flash.

Since we need to use the flash operation during APP program unpgration, so the flash erasing and writing process cannot be missed.

APP program unpgration

4)APP program jump in.

When the updated program is written in Flash, it needs to jump to the starting address of Flash, so a function needed to be run.

starting address of Flash

5) Application part:

First, select a starting address, which is the starting address of the program that needs to be flashed. As long as it is greater than the total length of the Bootloader program, and the length of the APP program does not exceed (the chip size-Bootloader program size), than the address shall be fine.

Then select an address in the Flash as a flag point, to determine whether the program needs to be updated.

The last part is to read the file in the SD card, write the material into the corresponding Flash, and complete the jump.


Main function, first check the flag point of the address 0x08002FFE, if it is 0x02, than it means that there is an APP program and does not need to be updated, so directly use iap_load_app(FLASH_APP_ADDR) to jump to the starting address of APP, run the program, and the BOOT program ends here.

If it is not 0x02, it means that it is a new chip, or it has not been upgraded before. Start the upgrading process with the SD_Update() function of the BOOT program.

operations for opening and mounting the SD card

Here are some operations for opening and mounting the SD card. If successful, proceed to the following.

STMFLASH_ErasePage function

f_read function can read the file in the SD card and assigns it to the buffer array, STMFLASH_ErasePage function erases a FLash area, and STMFLASH_WriteHalfWord function can write in. After writing, PageOffest += STM_PAGE_SIZE function can be used to record the number of pieces written, and continue to read the files of the SD card (note that files are not read at once here, it reads the size of STM_PAGE_SIZE each time, and then write). It continues to erase and write . . . Until (res || br = 0), which means there is no readable context, break, and exit the while(1) loop. Erase the flagged address of STMFLASH_ErasePage(0x08002FFE), and write 0xFF02,*(vu16*)(u16)(0x08000000 + 0x2FFE)=0xFF02 (so the next time the card is powered on, this place will be marked as 0x02 indicates that there is an APP program. , Just jump directly to the APP address).

Finally, iap_load_app(FLASH_APP_ADDR) means the end of the Bootloader process of the SD card, and it enters the APP program to run.

The Keil project is attached for your reference. (For attachments, please go to the original post of the forum to obtain)

Original title: SD card Bootloader offline upgration based on STM32F103

Leave a comment

Back to Top
Product has been added to your cart