cancel
Showing results for 
Search instead for 
Did you mean: 

Executing code from RAM problems

Iivan.1
Associate II

I'm developing application on stm32H7 and a need to execute small function from RAM. I add RamFunc derective, and describe in linker *(.RamFunc) in .data section. As a result, i certainly see, that function code moved to RAM address space. I compare byte to byte content of FLASH address with no RamFunc derective and RAM code with RamFunc. As A result it's clear that startup function move my code from FLASH to RAM.

Than i try just to execute my function from main just calling "fucntionName();", but then i get an interrupt

/**

 * @brief This is the code that gets called when the processor receives an 

 *     unexpected interrupt. This simply enters an infinite loop, preserving

 *     the system state for examination by a debugger.

 * @param None   

 * @retval None    

*/

  .section .text.Default_Handler,"ax",%progbits

Default_Handler:

Infinite_Loop:

 b Infinite_Loop

I try to disable interrupts just before calling my RAM function and it's have no result. Also this problem ocurs if i stop before my RAM function and then from debugger step into. Next moment i get error and can not see what is in the PC register.

In my function i just init volatile variable and do some arithmetic above it.

I think, that i get problem with alignment. I'm not sure, how start address must be alignment, but location in FLASH is also not alignment to 32, 16, 8, 4.

Another moment that possible i need another way to call my function. Maybe not just writing myFunc(); from main, and do some additional movements with PC and SP registers.

I will be thankful for any ideas.

1 ACCEPTED SOLUTION

Accepted Solutions
Iivan.1
Associate II

Solved ! The problem was that by default there was some kind of MPU. Maybe mbed OS protect all memory, i'm not sure. But i try to add HAL_MPU_Disable(); and all start work. Then i configure mpu for my memory region as MPU_INSTRUCTION_ACCESS_ENABLE and with region number 3+ (i set it to 7 to be sure) function becomes executable from RAM.

View solution in original post

6 REPLIES 6

Figure out which Interrupt or Fault is going to the "Default_Handler", one you haven't assigned to something else in the vector table.

Perhaps use a bisection method to narrow it down?

You've got a Hard Fault and Bus Fault Handler?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Piranha
Chief II

Maybe the cache is enabled and the code is not actually copied to memory? Take a note that the debugger shows the data from cache, not the backing memory.

How can I determine what Interrupt or Faulе caused Default_Handler ?

i just watched assembler layer and found, that in step of GO_INTO my RAM function we take some address from FLASH, put it in r3 register and then jump to this address. Content of r3 register is 0x10000e09 but my function locates by 0x10000e08 addr. I dont know how its happend, but i think, that its the reason for Fault cause processor pipeline cannot decode instruction.

Also i add handlers for hard fault and buss fault and determine, that there HardFault exeption ocurs.

"Some instructions use the least significant bit to determine whether the code being branched to is Thumb code or ARM code."

https://developer.arm.com/documentation/dui0068/b/Writing-ARM-and-Thumb-Assembly-Language/Overview-of-the-ARM-architecture/ARM-instruction-set-overview

Iivan.1
Associate II

Solved ! The problem was that by default there was some kind of MPU. Maybe mbed OS protect all memory, i'm not sure. But i try to add HAL_MPU_Disable(); and all start work. Then i configure mpu for my memory region as MPU_INSTRUCTION_ACCESS_ENABLE and with region number 3+ (i set it to 7 to be sure) function becomes executable from RAM.