Locating code higher in flash above bootloader

Share code examples or discuss embedded software, including device drivers, interrupt handlers, middleware and application code.

Moderators: Markus Girdland, Mattias Norlander

jprinster
Posts: 1
Joined: Wed Jul 11, 2018 11:01 pm

Locating code higher in flash above bootloader

Postby jprinster » Wed Jul 11, 2018 11:17 pm

I am running on an STM32F051C8. I have a bootloader that is located at the bottom of the flash at 0x08000000. I want to place the main application at 0x08003000 and be able to run the debugger with the code located there. What do I need to modify to have TrueStudio place the code there and run?
Thanks.

MaartenDV
Posts: 4
Joined: Mon Jul 09, 2018 1:48 pm

Re: Locating code higher in flash above bootloader

Postby MaartenDV » Wed Jul 18, 2018 12:20 pm

I have/had the same problem. Within Atollic I had no luck. With srecord 1.64 and a batch file I've managed to do this.
Also I had to manual edit the linker script to do this.

kind regards,

mdv

Georgy.Moshkin
Posts: 12
Joined: Sun Feb 18, 2018 6:05 am

Re: Locating code higher in flash above bootloader

Postby Georgy.Moshkin » Thu Jul 19, 2018 10:37 am

In my understanding two things must be done:
1. find *.c file which contains "VECT_TAB_OFFSET" (in your project files)
In my case it is system_stm32f4xx.c for STM32F4:

Code: Select all

...
#define VECT_TAB_OFFSET  0x00 /*!< Vector Table base offset field.
                                      This value must be a multiple of 0x200. */
...


2. change this value from 0x00 to required address, for example 0x6000:

Code: Select all

#define VECT_TAB_OFFSET  0x6000


3. find *.ld linker script file used by your project (not tried this with TrueSTUDIO yet):
In my case it is STM32F407VG_FLASH.ld for STM32F4:

Code: Select all

...
/* Specify the memory areas */
MEMORY
{
RAM (xrw)      : ORIGIN = 0x20000000, LENGTH = 128K
CCMRAM (rw)      : ORIGIN = 0x10000000, LENGTH = 64K
FLASH (rx)      : ORIGIN = 0x8000000, LENGTH = 1024K
}
...

change FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 1024K
to 0x08006000

If bootloader is compiled for VECT_TAB_OFFSET=0x00 and ORIGIN=0x8000000
And main program is compiled for VECT_TAB_OFFSET=0x6000 and ORIGIN=0x08006000
then main program may be called from bootloader this way:

Code: Select all

#define APPLICATION_ADDRESS     (uint32_t)0x08006000

uint32_t JumpAddress;
pFunction Jump_To_Application;

if (boot)
{
      JumpAddress = *(__IO uint32_t*) (APPLICATION_ADDRESS + 4);
      Jump_To_Application = (pFunction) JumpAddress;
      __set_MSP(*(__IO uint32_t*) APPLICATION_ADDRESS);
      HAL_DeInit();
      Jump_To_Application();
}


I did not tried this in TrueSTUDIO yet, but similar approach worked in other IDE.
Not sure if it is possible to run debug this way. At least it may be debugged in 0x8000000 and then final build is made using 0x08006000.

Georgy.Moshkin
Posts: 12
Joined: Sun Feb 18, 2018 6:05 am

Re: Locating code higher in flash above bootloader

Postby Georgy.Moshkin » Tue Sep 04, 2018 4:00 am

I just started to rewrite my old bootloader. This method works.
Few things to remember:
put main program on multiples of sector size. For example, for stm32f407 it is 0x4000, 0x8000, ... because if your bootloader occupies flash from 0x000 to 0x5000, putting main program at 0x6000 will erase part of bootloader from 0x4000 to 0x5000.

main.c of bootloader:

Code: Select all

#define APPLICATION_ADDRESS     (uint32_t)0x08008000
uint32_t JumpAddress;
typedef  void (*pFunction)(void);

if (loadapp)
{
pFunction Jump_To_Application;
JumpAddress = *(__IO uint32_t*) (APPLICATION_ADDRESS + 4);
Jump_To_Application = (pFunction) JumpAddress;
__set_MSP(*(__IO uint32_t*) APPLICATION_ADDRESS);
HAL_DeInit();
Jump_To_Application();
}


for main application at 0x8000 change two values:
in flash.ld:

Code: Select all

FLASH (rx)      : ORIGIN = 0x8008000, LENGTH = 1024K

in system_stm32..:

Code: Select all

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


Return to “Embedded target code development discussions”

Who is online

Users browsing this forum: No registered users and 0 guests