Skip to main content
Iivan.1
Associate II
May 20, 2022
Solved

Executing code from RAM problems

  • May 20, 2022
  • 3 replies
  • 2665 views

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.

This topic has been closed for replies.
Best answer by Iivan.1

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.

3 replies

Tesla DeLorean
Guru
May 20, 2022

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 VenmoUp vote any posts that you find helpful, it shows what's working..
Iivan.1
Iivan.1Author
Associate II
May 23, 2022

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

Piranha
Principal III
May 20, 2022

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.

Iivan.1
Iivan.1AuthorBest answer
Associate II
May 24, 2022

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.