cancel
Showing results for 
Search instead for 
Did you mean: 

A general question about bootloader and more than one firmware in flash.

Posted on June 21, 2017 at 10:43

Hi. I have not so much experience with bootloaders.

I need to store in flash 2 firmwares (without to replace the ST original bootloader).

My idea is: at startup time, if a particular signal is low the first firmware has to run (as a tipical situatuion).

But if the signal il high, the first firmware have to jump and run the second firmware, stored in a particular location of the flash.

It is a possible scenario with a stm32?

Many thanks

8 REPLIES 8
Zt Liu
Senior III
Posted on June 21, 2017 at 17:26

Hi, Denis!

Your need is just a small part of  IAP (In application programming)...

That is fairly easy, you just need to build up two projects with separate ROM.

For example, if I am using L011K4T6. This is lovely little fellow has 16KB(0x4000) Flash.

Suppose I need to separate this ROM into 2 applications equally. (8KB for each)

You may build your first project (in your project setting) with ROM map starts from 0x80000000 with Size 0x2000;

Similarly, build your second project with ROM start from 0x80002000 with Size 0x2000;

Finally you may build a simple mechanism to jump from 1st application to 2nd application.

For that, you may use following ST style codes (extracted from IAP example codes)

(For 1st application to jump to 2nd application)

/* APP2 Address */

#define APPLICATION2_ADDRESS (uint32_t)0x08002000 

typedef void (*pFunction)(void);

pFunction JumpToApplication;

static uint32_t JumpAddress;

...

...

...

if(your external signal on)

{

   JumpAddress = (__IO uint32_t*) (APPLICATION2_ADDRESS + 4);

   JumpToApplication = (pFunction)JumpAddress;

   __set_MSP(*(__IO uint32_t*) APPLICATION_ADDRESS);

   JumpToApplication();

}

else

{

   /*stay in 1st application*/

}

Hope you like this!

Posted on June 24, 2017 at 22:19

Hi and many thanks for your reply.

My function in the follow:

[code]void MainClass::FlashJumpTo() {

    typedef void(*pFunction)(void);

    pFunction JumpToApplication;

    uint32_t JumpAddress= *(__IO uint32_t*)(FLASH_APPLICATION_BEGIN+ 4);

    JumpToApplication= (pFunction)JumpAddress;

    __set_MSP(*(__IO uint32_t*)FLASH_APPLICATION_BEGIN);

    JumpToApplication();

}[/code]

When I run it nothing happens. After a while the watchdog resets the microcontroller but the firmware running is still the firmware of my microcontroller.

Have you got any idea?
Posted on June 25, 2017 at 15:54

You may first turn off the watchdog for testing purpose.

But have you already programmed your second application in the flash at 

FLASH_APPLICATION_BEGIN?

Make some outputs to indicate that the 2nd application is running,  like blinking a led or print a message to console.

What stm32 you are using?

Posted on June 25, 2017 at 18:27

Isn't be easier to have just single program with two applications inside to avoid tricks with compilation of two programs in differen ROM regions, etc. I guess for simple apps it should work:

int testExternalSignal(void)

{

// put the code to test external pin to select specific app

return X   // 1 for the first app, 2 for the 2nd app

}

void firstApp(void)

{

}

void secondApp(void)

{

}

int main(void)

{

if(testExternalSignal()==1)

{

   firstApp();

}

else

{

   secondApp();

}

}

Posted on June 25, 2017 at 20:07

I'm sure that at FLASH_APPLICATION_BEGIN there is the firmware of the second application. I'm a little bit unsure if i have built it this the right rom parameter.

But if there is or not an application at FLASH_APPLICATION_BEGIN the bootloader should run it or to die? Is it right?

But the bootloader goes only to freeze and the bootloader watchdog (still alive) resets the micro.
Posted on June 26, 2017 at 03:20

I think you really should turn off your Watch dog first for testing purpose.

If your second app does NOT start at you 

FLASH_APPLICATION_BEGIN address, the watch dog surely reset your core.

If your second app starts and this app doesn't reset the Watch dog immediately, the watch dog reset your core too.

I am not sure why you want to do this, it would be cumbersome if you have to maintain two different projects.

(Not to mention later how you want to program them into your flash: merge these hex files and program once or simply program twice)

Nevertheless, 

please take a look at 

http://www.st.com/content/ccc/resource/technical/document/application_note/27/38/37/58/c2/8c/40/07/DM00161366.pdf/files/DM00161366.pdf/jcr:content/translations/en.DM00161366.pdf

 

and 

http://www.st.com/en/embedded-software/x-cube-iap-usart.html

There are necessary ingredients you need to achieve your need.

(Developers build their their own boot loaders using this simple trick )

Or you just tell me your STM32 Series,

I think I can easily setup a simple demo for you.(If I have similar STM32 at hands)

Zt.

Posted on June 26, 2017 at 09:12

Many thanks.

The last question: when the bootloader starts the app and the app runs, the bootloader is still in action?
Posted on June 26, 2017 at 09:33

Should not anymore in action.