cancel
Showing results for 
Search instead for 
Did you mean: 

IAP for STM32F103C8T6

JBond.1
Senior

Hi, does library example of in-application programming (IAP) for STM32F103C8T6 exist?

I would like to load software updates from MicroSD card.

Also as I understand I will need 2 projects? One as bootloader to load software and another as application which will be executed. But in such case how is it possible to debug Application? Is it possible to avoid losing debugging code feature with Keil?

6 REPLIES 6

Pretty sure the HAL and SPL had IAP examples of various forms. The Nucleo BSP has SPI code for SDCard on the Adafruit shield, if not for the F1 definitely others that would port.

Debugging is going to be a little more challenging, Keil can step through the transition, and the disassembly view can be used to understand what's happening. The application can be debugged provided the loader is functioning, it will "Run to main()" so should still work. The loader can also be debugged so it works properly. Write code robustly, and output telemetry to understand what's happening without sticking your fingers in the gears. Debug one thing at the source level at a time.

Have effective output on Hard Fault and Error Handlers so you can understand why things die or fail.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Its interesting that no one did this already. STM32F103 is quite popular chip. Anyway here is my attempt do to it. I am trying to port it from original ST example.

Bootloader project:

https://github.com/JuMalIO/STM32F103-IAP-Bootloader

Application project:

https://github.com/JuMalIO/STM32F103-IAP-Application

Bootloader seems to work and writes "update.bin" from MicroSD card correctly. After writing application to flash it executes it. Application should blink PC13 LED with TIM2, but LED lights up and never blinks, I guess application crashes? How to debug it?

I guess my problem is that I need somehow free all resources which bootloader uses (SPI1, FAT_FS ram)? How to do that?

In other bootloader examples I see NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x3800); function. Maybe I need to execute it too? But I cant find this function in new STM32 HAL drivers.

Any ideas?

>>Its interesting that no one did this already.

That's a particularly shallow interpretation of things. These parts are over 12 years old, most professional programmers just get the job done and move on, and stuff isn't pushed on to public repositories.

>>I guess my problem is that I need somehow free all resources which bootloader uses (SPI1, FAT_FS ram)? How to do that?

The RAM gets reallocated by the linker the app should be freestanding in that regard.

It will however inherit the peripheral and interrupt mess you leave. You should put the peripherals into a safe state, by disabling individual interrupts, especially things like SysTick.

If the clocks and RCC/PLL are already up there's no need to tear these down and do-over.

To debug the app, have the loader transfer control immediately, and have Keil run to main()

Have the loader check the app integrity, and don't update it unless the image is corrupt, or you have a user initiated update, ie a button, or a special reset from the app flagging a request to update.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

One of the issues was that I needed to add this line to bootloader before jumping to application:

SCB->VTOR = APPLICATION_ADDRESS; /* Vector Table Relocation in Internal FLASH. */

Blinking application now seems to work.

But when I try more complex application (blinking led + microSD card reading) it seems to crash... Tried jumping to application on bootloader start and still this complex application fails... so Its not peripherals or interrupts issue correct? (since bootloader just jumps to application and does not do anything else) What else could be wrong?

It seems to work now! On application initialization it was resetting SCB->VTOR again.

I needed to change:

#define VECT_TAB_OFFSET 0x00003800U /*!< Vector Table base offset field.

This value must be a multiple of 0x200. */

Not sure if I deinitialize all peripherals and interrupts, but application seems to work.

In ST example they do not deInitialize anything before jumping to application. Do I need to deinitialize?

Do I need to execute all of this before jumping to application?

f_close(&FlashFile);
f_mount(0, "", 0);
SD_disk_initialize(0);
FATFS_UnLinkDriver(USERPath);
HAL_SPI_DeInit(&hspi1);
HAL_DeInit();
 
JumpToApplication();