hi,I write an algorithm running in cortex M0. It has some floating point multiplication.But the program always resets when encounter a floating point multiplication.And the code showed below:
for (k = 0; k < 4; k++) { tmpx = dbuffer[0] * a[k + 1]; dbuffer[k + 1] = dbuffer[k + 1] - tmpx; }
dbuffer[] and a[] are float array. tmpx is a float variable. When I debug the program, the M0 resets at line "tmpx = dbuffer[0] * a[k + 1];" .And the value of dbuffer[0] is 1429983,a[k+1] is 0.00974910986.I have tested this multiplication in a new project.And the code goes well and result is right.The test code is like below:
float a = 1429983; float b = 0.00974910986; float c; c = a* b;
So, I know M0 use softVFP. I have seen asm code such as __aeabi_fmul corresponding to my code.Could anybody tell me what's wrong with my code?Thank you very much.
Hi and welcome to the community!
I assume you use GCC, because of the '__aeabi_fmul' ... but it's still hard to guess what's wrong.
Because the second attempt works, I believe the difference might be in the way the code is built.
I will suggest that you create a new test-project, then first put your test-code in there, run it and see if it works.
If the first test works, then change the new test-project to use the code with the for-loop.
If it works now, without reseting, then I think you need to find the difference in how the code is built.
...Assuming you get it all working, you might want to consider changing for(k = 0; k < 4; k++) to for(k = 1; k < 5; k++) and then remove the '+1' from the index.
It won't really do much difference unless you've turned off optimization, but it will make your code shorter and easier to read.
hi,Jensbauer, Thanks for your reply.:)
I have solved this bug by resizing stack size. And I also learn a lot from your reply.
Does your main() end in an infinite loop ?
-If not, it should; becaues on a microcontroller, main() is not supposed to return.
I'm only asking in case your for-loop is placed inside main; if it's outside main, then a stack overflow of a single element will erase the return-address, if the array is the first (or only) variable on the stack.
This is because the return-address is the last register pushed onto the stack, thus the last element.
Note: If you have very little stack space available, then it might be helpful to save a few bytes of stack-space by adding __attribute__((naked)) in front of void main(); -but only for main or entry(), because those subroutines should never return.
When a subroutine never returns, there's no reason for it to save the registers on the stack.
Thanks,Jensbauer.
Your suggestion is very helpful. I will adopt in my project.