Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
VT
Level 1
Level 1
First reply posted First question asked Welcome!

Hi,

I need loading function from into SRAM and execute it
But when execute, It make Exception.
Should I doing some setting before execute it?

  • Chip: CYT2B65BA CM0+
  • Test function:

====================================================================
typedef struct
{
void (*functionPtr)(void);
} test_Function;
test_Function g_testcmd;
extern uint32_t g_command_buf[144];

__STATIC_INLINE void test_api(void)
{
//Do something. e.g. Sending CAN message
}

void exec_test_api(void)
{
memcpy((void *)&g_command_buf[0], (void *)&test_api, 0x100);
g_testcmd.functionPtr = (void (*)(void))&g_command_buf[0];
g_testcmd.functionPtr();  //Exception here!!
}

====================================================================

  • Test Result:

HardFault Exception
Exception occurred at PC = 0x8000e6c, LR = 0x1000271d
(g_command_buf address is 0x8000e6c and test_api size is 0xc, smaller than g_command_buf.)

 

Thanks
Vivian

0 Likes
1 Solution
yanf
Moderator
Moderator
Moderator
50 replies posted 50 sign-ins 25 solutions authored

Hi Vivian,

Okay, I try to give g_command_buf some valid command such as 0xf04f_0001 (MOV R0, #1) but the errors continue to show up.

I further checked the disassembly code of your C function. It shows some suspicious commands as showing here:

yanf_0-1641352061015.png

If you look at the last line of my screenshot, there is a BLX instruction, which asks the M4 core to change execution state. This can not be done because M4 core only executes in Thumb state. And next cycle, the processor did claim an hardFault according to my test.

I am not sure why you want to do such a low-level operation to manually move instruction' s position and execute code at arbitrary position. If you insist to do so, please write in assembly language in case of any misinterpretations caused by C compiler.

If you only want the code to be executed in SRAM, you can simply use the SRAM demo project located in SDL library "T2G_Sample_Driver_Library_7.3.0\tviibe1m\tools\iar\sram"

Way much simpler and tested with no errors.

 

Best Regards,

Finn

View solution in original post

0 Likes
3 Replies
yanf
Moderator
Moderator
Moderator
50 replies posted 50 sign-ins 25 solutions authored

Hi Vivian,

I have recreated your problem in my test environment

The hardFault seems to be trigger by false commands in your g_command_buf.  The command buffer is declared but not defined. 

Do you define it elsewhere? If so, please provide it so that I can do further inspection. If not, please define valid commands in your command buffer.

Best Regards,

Finn

0 Likes
VT
Level 1
Level 1
First reply posted First question asked Welcome!

Hi Finn

Thanks for your reply.
g_command_buf defined in other C file in previous test.
I also trying to define g_command_buf in the same C file.

The result is not change, HardFault Exception in g_command_buf.

g_command_buf  existed in map file, I think it can be used to  save data.

 

  • TDS_cm0.map:

...
*******************************************************************************
*** ENTRY LIST
***

Entry Address Size Type Object
----- ------- ---- ---- ------
g_command_buf 0x800'0e6c 0x240 Data Gb bl_cmd.o [2]

 

  • Test function:

====================================================================
typedef struct
{
void (*functionPtr)(void);
} test_Function;
test_Function g_testcmd;
uint32_t g_command_buf[144];  //Update this Line

__STATIC_INLINE void test_api(void)
{
//Do something. e.g. Sending CAN message
}

void exec_test_api(void)
{
memcpy((void *)&g_command_buf[0], (void *)&test_api, 0x100);
g_testcmd.functionPtr = (void (*)(void))&g_command_buf[0];
g_testcmd.functionPtr(); //Exception here!!
}

====================================================================

  • Test Result:

HardFault Exception

Exception occurred at PC = 0x8000e6c, LR = 0x10002747

Thanks
Vivian

0 Likes
yanf
Moderator
Moderator
Moderator
50 replies posted 50 sign-ins 25 solutions authored

Hi Vivian,

Okay, I try to give g_command_buf some valid command such as 0xf04f_0001 (MOV R0, #1) but the errors continue to show up.

I further checked the disassembly code of your C function. It shows some suspicious commands as showing here:

yanf_0-1641352061015.png

If you look at the last line of my screenshot, there is a BLX instruction, which asks the M4 core to change execution state. This can not be done because M4 core only executes in Thumb state. And next cycle, the processor did claim an hardFault according to my test.

I am not sure why you want to do such a low-level operation to manually move instruction' s position and execute code at arbitrary position. If you insist to do so, please write in assembly language in case of any misinterpretations caused by C compiler.

If you only want the code to be executed in SRAM, you can simply use the SRAM demo project located in SDL library "T2G_Sample_Driver_Library_7.3.0\tviibe1m\tools\iar\sram"

Way much simpler and tested with no errors.

 

Best Regards,

Finn

0 Likes