Do you mean your bootloader has flash programming feature, and want to copy the boot loader from flash to SRAM when the startup code is executed?regards,Joseph
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 ENDPYou 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 copyCopyFlashToSRAMLoop 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 ENDPIn 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
Hi Rohit,In this case you could develop the bootloader and flash programming code as two separate projects.The flash programming project is compiled and link using SRAM as program address.Then you can generate hex image, and insert this into your boot loader project as a constant array.Inside your boot loader, you can copy this constant array into SRAM.To jump from boot loader to flash programming code, personally I got used to use embedded assembly:/* ARM RVDS or Keil MDK */__asm void jump_to_sram(void){ LDR R0,=0x20000000 ; starting address of program in SRAM. LDR R1,[R0] ; Get initial MSP value MOV SP, R1 ; Set SP value LDR R1,[R0, #4] ; Get initial PC value BX R1}You can use function pointer to do the same.regards,Joseph
For Cortex-M3 the low bit of the address needs to be set to indicate Thumb state, so if the starting address in RAM was say 0x20000100 (i.e. 0x20000101 is stored at 0x20000004 in the asm above).In C it would betypedef void RAM_fn_t(void);RAM_fn_t* fp = (RAM_fn_t*)0x20000101;(*fp)();It doesn't set SP the way the asm above does. The cast is technically undefined behavior in C, but pragmatically it does what it looks like.Equivalently, if you prefer terseness/parentesis:(*((void (*)(void))0x20000101))();
typedef void RAM_fn_t(void);RAM_fn_t* fp = (RAM_fn_t*)0x20000101;(*fp)();
(*((void (*)(void))0x20000101))();
Hi Kent,It depends on how you want the project to look like.If the program code in RAM is created as a completely independent project, and then merged into your other project running in flash using a hex file editor, then you don't have to use scatter files. In this way you will have two separated images in your running system. This can make sure you don't accidentally call a function in flash or read it when you are programming the flash.If you are trying to create a project that contains code running in flash as well as code running in SRAM, then you need to define the code running in SRAM with a section name, and then use linker script to specify this section is to be copied to and executed from SRAM.regards,Joseph
You can use function pointer to do the same.