cancel
Showing results for 
Search instead for 
Did you mean: 

Custom Bootloader

contact239955_stm1_st
Associate II
Posted on March 23, 2012 at 17:54

Hi everybody!

I'm trying to make a bootloader that fetch a program from an SD card. I finally manage to parse the HEX file correctly and write the content in the flash memory but when I try to jump to the program, a Hard Fault is generated and I don't know wh

y.

There's my Flash related code :

FLASH_Unlock();
...
FLASH_ErasePage(ApplicationAddress + (n-1) * PAGE_SIZE); 
FLASH_PageWrite(ApplicationAddress + (n-1) * PAGE_SIZE, page32);
...
uint32_t startAddress = *(__IO uint32_t*)(ApplicationAddress + 4);
pFunction Start = (pFunction)startAddress;
__set_MSP(*(__IO uint32_t*)ApplicationAddress);
Start();

Where 'n'

is the page number to be written, page32 a pointer to the page content in RAM and 'FLASH_PageWrite()' a custom function to write an entire page by successive calls to 'FLASH_ProgramWord()'

ApplicationAddress = 0x800F000 (My bootloader uses pages up to 0x800A800) Thanks #iap #bootloader #flash
36 REPLIES 36
AlexKern
Associate II
Posted on October 10, 2013 at 10:45

Hi all.

I have a little bit different problem. My custom bootloader shall be able to run in an user thread (embOS using).

Working this way it is possible to set MSP, but after jumping to ResetHandler of application image, the stack in use is still from the bootloader user thread. And the OS initialisation code of application image fails.

I have read, that it is nessecary to change to priveleged mode. The try with SVC has failed. Can the user thread elevate itself to SYS?

Thanks in advance

Alex

Posted on October 10, 2013 at 15:58

Perhaps a new thread would have been in order?

I'd imagine it could be done with BKPT, or through some indirect reset (IWDG)

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
GUIRAT.Youssef
Associate III
Posted on September 20, 2016 at 23:52

Hi Clive , 

I have a similar problem when trying to load an application stored @0x08003000. 

In fact , I am using an STM32L486RG and what I aim to do is to load IAP application @0x08000000 with VECT_TAB_OFFSET equal to 0x00 ( default value) and to load my application @0x08003000 with VECT_TAB_OFFSET equal to 0x3000 (I have checked with code by ST firmware concerning the IAP application).

I'm using IAR 7.40.3 as IDE and I have got a strange behaviour . To explain more, when first of all I download my application @0x08003000 (using debug mode the green button in IAR IDE) and I disconnect from the debug mode in order to download the IAP code @0x08000000 , I get normal execution which means that I execute the IAP code and then I jump to execute my app loaded @0x08003000.

Now, the problem appears once I try to load my app @0x08003000 using the download active application option for IAR ( go to project > Download  > Download active application ) ( P.S: when I check using the STLink utility my app is succesfully stored in this address @0x08003000) . After that, I load the IAP code @0x08000000 using the debug mode. It start executing but when trying to jump to my code stored @0x08003000 a hardfault occured.

I really doesn't understand why it works fine when using the debug mode while it fails with the download active application .

Posted on September 21, 2016 at 00:59

I'm not an IAR user, I've don't similar things successfully in Keil.

Your option here is to get a Hard Fault Handler that can decompose the failing state, and understand why you are getting the fault. It could be that running in the debugger changes the starting conditions, and avoids running the loader and the way it sets the system up. Execution could be reviewed by outputting messages via the USART or SWV

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
GUIRAT.Youssef
Associate III
Posted on September 21, 2016 at 10:24

 Thanks for your response  Clive!

 To be more precise the problem occurs at the startup code before launching the app.

Posted on September 21, 2016 at 18:11

To be more precise the problem occurs at the startup code before launching the app.

You'll need to present that in a way I can review, I have zero visibility through the key hole.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
GUIRAT.Youssef
Associate III
Posted on September 22, 2016 at 13:52

Hi again Clive,

So I have FREERTOS application that I want to store @0x08003000 with VECT_TAB_OFFSET equal to 0x3000 then I load the IAP main code @0x08000000 at 0x00 VECT_TAB_OFFSET.

When I load my RTOS app with the download active app then I debug my IAP app , I get a HARD_Fault error when jumping to may rtos app. I wanted to know if it can point the main when jumping ( I have checked the main address of the RTOS app with the diassambly then I put a break point to this address when debbuging the IAP but it cannot even reach this address meaning that the HARD_fault could be occured at the startupcode).

However, when I normally bebug the RTOS app at its address then I debug the IAP app at the default adress @0x08000000, NO PROBLEM occurs . In fact, I could jump to my RTOS app and I run it too.

So , my first question : '' Which are the differences between the download active app and the normal debug mode ? Can the debugger use some instructions that affects the memory mapping ? ''

I went back to my code trying to see if this problem is linked to RTOS bad configuration. I commented the osSystickHandler which obviously lead to not switch between threads just to see if I can execute the main and reach my first thread. I compile my code and load it through the Download active app and then I debug the IAP code. Following these steps , it works meaning that I jump to my code but I just get stuck to the first thread.

If I uncomment the osSystickHandler  and I follow previous loading steps , I get the hard fault error again.

So my second question is : '' If I have an RTOS app that would be loaded in a different place than the 0x08000000  (e.g : 0x08003000 ) shall I do a modification on the osSystickHandler  to avoid any misbehaviour ?

I would like to share with you 2 .map files the first one ''1.map'' wherethe osSystickHandler is present and the second oene ''2.map'' where theosSystickHandler is commented.

Posted on September 22, 2016 at 15:28

As I said I'm not an IAR user, you'll need to discuss peculiarities with someone from IAR support.

When jumping around in RTOS's one must consider the system/user state of the machine, and it is important not to transfer control from an interrupt context. The Hard Fault suggests you might be using system instructions in a user state. However all things considered a proper Hard Fault Handler will tell you exactly what instruction is faulting and the context in which it occurs, a while(1) loop will not. Review posts on the forum related to that, and Joseph Yiu's examples. Also step the transition, don't rely on ''run to main()'' debugger settings if it doesn't get to main(), and step the code there.

The debuggers normally fiddle with the system to get it in a state they can uses, this can include enabling clocks, pins, internal registers, and can be invasive. Instrument you code sufficiently using GPIO, USART, or whatever so you can understand what is happening in your system without the debugger attached.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
GUIRAT.Youssef
Associate III
Posted on September 26, 2016 at 11:16

Hi again , 

Unfortunately the problem still exist !! :( But I come with new observations. In fact , my RTOS app has 3 tasks . When I debug this app at @0x8003000 it works fine but when I load my IAP app @0x8000000 in order to jump for my code and run it , the hole app crushs and I get the hardfault.

I went back to my RTOS code and I comment two of my tasks and I left only one task to be run then I load my IAP code , It works ! So, it came to my mind to compare .map files for each config and I found that when only one task is present , I have 87 bytes of readonly  data memory . This memory increases when I define another task to reach 195 bytes of readonly  data memory. 

I wonder if the problem could be linked to this difference of Readonly memory size. Maybe I'm not using the appropriate freertos configuration ( for the moment configTOTAL_HEAP_SIZE is set to 25Ko / configMINIMAL_STACK_SIZE is set to 128 are used ) ?

Any idea please ! 

Posted on September 26, 2016 at 14:27

The processor provides all the information needed to understand why it Hard Faulted, start by reviewing that and understand the WHY/WHERE, apply that knowledge to what your code is doing.

Is the 128-byte stack sufficient for the call depth and local/auto variables that your code/functions consume? A malloc() will return NULL if the heap is insufficient.

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