This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

[Cortex-M3] Execution code from RAM during Flash Programming

I am writing a bootloader for Cortex M3 Toshiba controller.

During Flash Programming I need to execute code from RAM as entire Flash is inaccessible during write/erase operations

I wish to know how to copy code from FLASH to RAM and execute that code from RAM.

Thanks...
Parents
  • Note: This was originally posted on 14th March 2012 at http://forums.arm.com

    Thank you very much for the reply...

    This method is quite helpful. I will first try this out. Thanks alot...
    But I am not sure of the Bootloader size as I have Diagnostic services running in bootloader too. So copying entire bootloader will be difficult so I need to copy portion of code which is most required during Flash Programming. I am able to copy the code to SRAM but I am unaware of the method of changing the Program counter value to SRAM address while flash programming.

    The functions that are copied to SRAM must be executed from SRAM during Flash Programming. I am trying this with the help of function pointers but have not succeeded yet. I wish to know whether this can be done in any other way. Or function pointers will solve my purpose.

    Please suggest regarding this..

    Regards,
    Rohit










    In that case, it would be easier to copy the whole boot loader (rather than a portion of it) from flash to SRAM in the assembly startup code.
    The boot loader, assumed to be mostly written in C, will have to linked to use the SRAM address as program memory range.

    In the assembly startup code you will have a reset handler, and normally you can see something like:

    Reset_Handler   PROC
                 EXPORT  Reset_Handler    [WEAK]
                 IMPORT  __main
                 LDR  R0,=__main
                 BX     R0   ; this branch to C startup code
                 ENDP

    You can insert an assembly code to copy the memory from flash to SRAM, before jumping to the C startup code which will be in SRAM.

    Reset_Handler   PROC
                 EXPORT  Reset_Handler    [WEAK]
                 LDR  R4,=0x00000000 ; Source starting address
                 LDR  R5,=0x20000000 ; Destination starting address
                 LDR  R6,=0x4000  ; 16K program size to copy
    CopyFlashToSRAMLoop
                 LDMIA R4!,{R0-R3}
                 STMIA R5!,{R0-R3}   
                 LDMIA R4!,{R0-R3}
                 STMIA R5!,{R0-R3}   
                 LDMIA R4!,{R0-R3}
                 STMIA R5!,{R0-R3}   
                 LDMIA R4!,{R0-R3}
                 STMIA R5!,{R0-R3}
                 SUBS R6,#64 ; Each time 64 bytes are copied
                 BNE CopyFlashToSRAMLoop
                 IMPORT  __main
                 LDR  R0,=__main
                 BX     R0   ; this branch to C startup code  
                 ENDP

    In this way, the whole boot loader will be running in SRAM.  You need to be careful to make sure that the data use by the boot loader is not in the same address as the instruction memory.

    regards,
    Joseph
Reply
  • Note: This was originally posted on 14th March 2012 at http://forums.arm.com

    Thank you very much for the reply...

    This method is quite helpful. I will first try this out. Thanks alot...
    But I am not sure of the Bootloader size as I have Diagnostic services running in bootloader too. So copying entire bootloader will be difficult so I need to copy portion of code which is most required during Flash Programming. I am able to copy the code to SRAM but I am unaware of the method of changing the Program counter value to SRAM address while flash programming.

    The functions that are copied to SRAM must be executed from SRAM during Flash Programming. I am trying this with the help of function pointers but have not succeeded yet. I wish to know whether this can be done in any other way. Or function pointers will solve my purpose.

    Please suggest regarding this..

    Regards,
    Rohit










    In that case, it would be easier to copy the whole boot loader (rather than a portion of it) from flash to SRAM in the assembly startup code.
    The boot loader, assumed to be mostly written in C, will have to linked to use the SRAM address as program memory range.

    In the assembly startup code you will have a reset handler, and normally you can see something like:

    Reset_Handler   PROC
                 EXPORT  Reset_Handler    [WEAK]
                 IMPORT  __main
                 LDR  R0,=__main
                 BX     R0   ; this branch to C startup code
                 ENDP

    You can insert an assembly code to copy the memory from flash to SRAM, before jumping to the C startup code which will be in SRAM.

    Reset_Handler   PROC
                 EXPORT  Reset_Handler    [WEAK]
                 LDR  R4,=0x00000000 ; Source starting address
                 LDR  R5,=0x20000000 ; Destination starting address
                 LDR  R6,=0x4000  ; 16K program size to copy
    CopyFlashToSRAMLoop
                 LDMIA R4!,{R0-R3}
                 STMIA R5!,{R0-R3}   
                 LDMIA R4!,{R0-R3}
                 STMIA R5!,{R0-R3}   
                 LDMIA R4!,{R0-R3}
                 STMIA R5!,{R0-R3}   
                 LDMIA R4!,{R0-R3}
                 STMIA R5!,{R0-R3}
                 SUBS R6,#64 ; Each time 64 bytes are copied
                 BNE CopyFlashToSRAMLoop
                 IMPORT  __main
                 LDR  R0,=__main
                 BX     R0   ; this branch to C startup code  
                 ENDP

    In this way, the whole boot loader will be running in SRAM.  You need to be careful to make sure that the data use by the boot loader is not in the same address as the instruction memory.

    regards,
    Joseph
Children
No data