Hello,
I am trying to copy the function to RAM and then call it using a function pointer like below:
It forced a system restart and it should have encountered an error.
Can you please help me deal with it? Thanks in advance!
Fullscreen
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
uint8_t ram_func(uint8_t data_in)
{
data_in++;
return data_in;
}
/*---------------------------------------------------------------*/
int main() {
typedef uint8_t (*func_ptr)(uint8_t);
uint8_t func_buffer[256];
memset(func_buffer, 0, 256);
// Copy the machine code of func into func_buffer
memcpy(&func_buffer, (uint8_t*)ram_func, 255);
// Create a function pointer and cast the buffer address to it
func_ptr fp = (func_ptr)func_buffer;
// Call the function through the function pointer
uint8_t data_out = fp(3);
printf("Result = %d\n", data_out);
return 0;