Skip to main content
Visitor II
January 8, 2015
Question

How to make program start from a customized address?

  • January 8, 2015
  • 14 replies
  • 5571 views
Posted on January 08, 2015 at 04:51

I understand this question has been asked before. I searched the forum and view these three threads: 

''How to program the application flash file (hex file) in chosen flash area by using ST-LINK for STM32F0''

''Flash Address setting to write main program starting from specific address''

''How start running STM32 firmware from IROM2 ?''

However, I still have some doubts. For STM32F407, there are 12 flash sectors. If I define the IROM1 address to be 0x080E0000, which is the 12th sector, can I then store data in the first 11 sectors, including sector 0 starting from 0x08000000? From the threads, it seems that I can't because some vectors are located in sector 0. Does it mean I can only use sector 1~11?

#know-your-tools #flash #boot #stm32f4
    This topic has been closed for replies.

    14 replies

    Graduate II
    January 8, 2015
    Posted on January 08, 2015 at 15:54

    Thing is, the processor loads the initial SP and PC from address ZERO, 0x08000000 is mapped there via the BOOTx pins. Sector 0 would need to have 32-bit words committed to getting the processor going, you could use the rest, although honestly I'd use the 16KB for a boot loader so I wouldn't brick the processor.

    The reason to use the 16K and 64K sectors is the erase speed. If you need to store lots of data the 128K ones would be fine.

    songAuthor
    Visitor II
    January 13, 2015
    Posted on January 13, 2015 at 03:38

    Hi, clivel, thanks for the info. I understand sector0 should be preserved for system usage, so it only leaves the question how to save the program to a customized starting address. I am using Keil, so is it true that I only need to define the IROM1 address to be 0x080E0000? Do I also need to set ST-Link Debugger -> Settings -> Flash download?

    Graduate II
    January 13, 2015
    Posted on January 13, 2015 at 04:31

    Correct you'd change the IROM location and size, either via the GUI, or a scatter file. This will define how the linker places the code generated by the compiler. The debugger will take and flash whatever memory it is directed to use.

    Some examples to look at would the the IAP (In Application Programming) ones.

    songAuthor
    Visitor II
    January 13, 2015
    Posted on January 13, 2015 at 04:54

    Hi, clivel, I just did some test. If I keep default address settings, the debugging works normal. However, when I change IROM1 to 0x80E000 and size to 0x20000 (128K), the debugging failed to jump to main entry. I captured the screen and you can see almost all debugging buttons are greyed out. Any idea why and how to solve it?

    LR_IROM1 0x080E0000 0x00020000  {    ; load region size_region

      ER_IROM1 0x080E0000 0x00020000  {  ; load address = execution address

       *.o (RESET, +First)

       *(InRoot$$Sections)

       .ANY (+RO)

      }

      RW_IRAM1 0x20000000 0x00020000  {  ; RW data

       .ANY (+RW +ZI)

      }

    }

    0690X00000602wgQAA.jpg

    Graduate II
    January 13, 2015
    Posted on January 13, 2015 at 06:31

    Yes, you'd need code, or vector entries, at the base of the flash to transfer control to new location.

    From the debugger perspective you could assign SP and PC to the appropriate values you could get these from the vectors at the base of your image. You code in SystemInit() would also want to set the vector table address (SCB->VTOR) to point to the new base address.
    songAuthor
    Visitor II
    January 13, 2015
    Posted on January 13, 2015 at 08:55

    From the post ''How to program the application flash file (hex file) in chosen flash area by using ST-LINK for STM32F0'' and ''Need advices for separating flash memory for STM32F0 and STM32F1'', I understand the code placement is the job of linker. In the linker setting of Keil project, I check ''Use memory layout from Target Dialog''. So other than this, do I need to manually add code to any source file? If yes, would you please advise how to do it? 

    Graduate II
    January 13, 2015
    Posted on January 13, 2015 at 15:09

    And you've probably changed the address in the linker, check the .MAP file to confirm the placement.

    The linker address doesn't magically change the silicon in the processor. It does very specific things when it starts, and unless you have to code or table to direct execution to your new location it's not going to go there.

    Suggest you brush up on you microprocessor basics, with perhaps one of Joseph Yiu's Cortex-Mx books, and the concepts of compilers, linkers and loaders.

    Review the IAP examples which provide a Loader + Application model, where the application code is placed at a higher address.
    Visitor II
    January 14, 2015
    Graduate II
    January 14, 2015
    songAuthor
    Visitor II
    January 14, 2015
    Posted on January 14, 2015 at 06:49

    I think I understand the concepts and what I should do, my question is how to do it. I already went through  the example code with AN3965. This example program still starts from 0x08000000 and it can perform a jump to user code that starts from 0x08004000, but the problem I had is that the program can't even reach main entry if it is located at the customized address. I have tried Keil's configuration for IROM1, linker memory layout, flash download memory and changed VECT_TAB_OFFSET in SystemInit(), but none of them works. So when the MCU can't find the main entry, I can't use code in main() to change table address or jump, as the example does. That only leaves me Keil target setup and linker configurations, or maybe bootload handling which I currently don't know much. So I really need some advice about HOW to solve the problem.