Content originally posted in LPCWare by curtvm on Thu May 20 10:14:40 MST 2010
It appears to me, if you want to try creating ram functions using C code for M0, you will run into a problem with the linker (I think this is the source of the problem) which appears to be using BLX incorrectly for the M0.
For example, placing a function in ram like-
void test_func(void) __attribute__((section(".data.test")));
void test_func(void){
//some code here
}
it seems will place the function correctly in ram (.data),. But it also creates 'veneer' code which loads the pc with the address of the ram function. The problem is an incorrect use of BLX is used to get to the 'veneer' code when the ram function is called, and you end up with a hard fault.
Any 'calls' to ram from flash or to flash from ram will end up using this mechanism it seems.
An alternative may be to use something like this to call a ram function (and call any flash functions from ram)-
( (void(*)(void)) &test_func + 1 )();
changing the typecast and parameters as needed (a macro would make it easier). The +1 will get the lsb of the target address set, and also prevents the use of the 'veneer' code. (I'm assuming the ram function gets aligned correctly by the linker)
I have been trying to run a function from ram (just to test), and have been stuck with the blx/linker problem, but it looks like the alternative may work (I'm away from the hardware now, so can only compile and view code).
To get the answer for the original question of this thread, it may require just trying it both ways and comparing the results.
update-
I have placed a function in ram, and 'called' it via the methods described above. It works, but the debugger does not like debugging functions in ram (get anywhere near that function, the debugger will have a problem). So in the end, until the linker(?) is corrected for the M0, and the debugger is ready to deal with ram functions, this may all be more effort than its worth (at least for speed gains).
Is there some setting I'm missing for the debugger to be able to debug ram functions?
Is the incorrect use of BLX for the M0 a linker problem?