Skip to main content
Martynas Mickus
Associate
October 19, 2017
Question

Shared memory between BL and Main application

  • October 19, 2017
  • 3 replies
  • 3368 views
Posted on October 19, 2017 at 10:46

I am working on the project with STM32F030R8T6. So far I have bootloader and main application with memory space allocated. My problem is that currently I am jumping between main and bootleader using address of reset handler and I need to somehow let BL know if it is startup or jump from main. I have been trying to allocate 4 bytes to store this information in memory and access using the address, but it seems that value at the address get reset after jump. My question is — what would be the best approach to have some flag between BL and Main application ?

Note: this post was migrated and contained many threaded conversations, some content may be missing.
    This topic has been closed for replies.

    3 replies

    AvaTar
    Senior III
    October 19, 2017
    Posted on October 19, 2017 at 12:08

    I have been trying to allocate 4 bytes to store this information in memory and access using the address, but it seems that value at the address get reset after jump.

    Most probably in the startup code, that runs from reset vector to main.

    You can modify the startup (assembler code) or the linker map t not initialize certain areas, but that depends on the toolchain.

    I guess you speak about a custom BL, not the system BL in ROM.

    Tesla DeLorean
    Guru
    October 19, 2017
    Posted on October 19, 2017 at 12:25

    Shrink the RAM size slightly in the linker script or scatter file so you can use the last 4 or 16 bytes, or whatever you need to pass state information between the two.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    Martynas Mickus
    Associate
    October 25, 2017
    Posted on October 25, 2017 at 09:34

    Ok, I have left last 4 bytes of RAM out of both linker scripts, but it seems that this value still get reset when jumping. There is no power cycle reset, so I am not sure why this behavior exists.

    In linker script I just do 

    RAM (xrw)       : ORIGIN = 0x20000000, LENGTH =  8K - 4 

    And then I use address

    0x20000000 + 8K - 4 to store 32 bit int, it seems I am successful but after the jump it gets reset even if this 4 byte area is not allocated in any linker script and should not be touched by reset handlers.

    AvaTar
    Senior III
    October 25, 2017
    Posted on October 25, 2017 at 09:42

    Debugging ?

    I would set a data breakpoint at your reserved location.

    Depending on your IDE, single-step might only be possible in instruction/assembler mode, since the application is started the BL.

    waclawek.jan
    Super User
    October 27, 2017
    Posted on October 27, 2017 at 11:58

    Okay so this is the usual GNU setup.

    In the startup code the vector table is defined, and from there the processor picks SP upon reset:

    g_pfnVectors:

    .word _estack

    It's also (here unnecessarily) reloaded as the very first thing in the startup:

    Reset_Handler:

    ldr r0, =_estack

    mov sp, r0 /* set stack pointer */

    As you see it's not defined anywhere in the startup code so it's an external symbol.

    And, sure enough, it's defined in both linker scripts:

    _estack = 0x20002000; /* end of RAM */

    so the word at 0x20001FFC gets overwritten by the first thing which goes onto the stack.

    JW

    Martynas Mickus
    Associate
    October 27, 2017
    Posted on October 27, 2017 at 12:03

    So, what approach would be the best way to establish some space that would not be overwritten at the startup? Shrink stack by 4 bytes ? 

    waclawek.jan
    Super User
    October 27, 2017
    Posted on October 27, 2017 at 12:54

    Shrink? The stack goes down as far as the bottom of the RAM. _Min_Stack_Size is just a placeholder so that the linker warns if variables would reach too far - but that number is just randomly chosen by whomever who wrote that linker script - ultimately it's your (the programmer's) responsibility to find out how much stack is really needed.  There's no enforcement of it in hardware/runtime.

    So just change _estack, that's all.

    JW