7.5.1 Execute from RAM

BMX provides support for the CPU to execute from RAM. Execute from RAM will use the same priority and arbitration scheme as described earlier in this chapter.

Program Execution from RAM executes user code from RAM instead of Flash. The code snippet copies code from Flash to RAM. BMX registers are configured to set up execution from RAM. Once the configuration is complete, the code branches to the RAM space where the Flash code is copied.

Program Execution from RAM

int EnableRAMExecution(void);
 
// Reserve RAM space for an IVT in RAM + all functions that need to execute from RAM
uint32_t __attribute__((noload, aligned(0x40), section(".RAMExecutionSpace"))) RAMExecutionSpace[1024];
 
int main(void)
{
    int isRamExecEnabled;
    isRamExecEnabled = EnableRAMExecution();
    if(isRamExecEnabled)
    {
        // Call the start address of RAM code execution stored in BMXIRAML here
    }
    while(1);
}
 
void __attribute__((aligned(0x4), section(".RAMExeFuncs"))) RamFunc(void)
{
      // Code that executes from RAM goes here
}
 
int EnableRAMExecution(void)
{
         // Code size to be executed from RAM
    const uint32_t codeLen    = __builtin_section_size(".RAMExeFuncs");    
        // Base address of functions stored in flash, but intended to be copied and executed from RAM at runtime    
    const uint32_t flashFuncs = __builtin_section_begin(".RAMExeFuncs");
        // Abort if RAMExecutionSpace[] is declared too small
    if(codeLen > sizeof(RAMExecutionSpace))
        return -1;
       // If BMX already configured for RAM execution and ISR vectoring, do nothing
    if(BMXIRAML != BMXIRAMH)
        return 0;
    /*Copy flash code to RAM*/
    __builtin_memcpy(&RAMExecutionSpace[0], (const uint32_t*)flashFuncs, codeLen);
	// Configure BMX registers to execute code from RAM
    BMXIRAML = ((uint32_t)RAMExecutionSpace);
    BMXIRAMH = ((uint32_t)RAMExecutionSpace);
    BMXIRAMH += sizeof(RAMExecutionSpace);    
    return 1;
}