2020-07-07 12:17 PM
When I receive the SPI variable I save this as an int like this:
uint8_t spiRxBuf;
uint32_t highByte;
uint32_t lowByte;
HAL_SPI_Receive(&hspi2, &spiRxBuf, 1, 10);
highByte = spiRxBuf;
HAL_SPI_Receive(&hspi2, &spiRxBuf, 1, 10);
lowByte = spiRxBuf;
int result = ((highByte & 0x1f) << 7) | ((lowByte >> 1));
At this point I know the int is extracted properly because I can display it like this:
Unicode::snprintf(measurementTextCH2Buffer, 50, "%d", result);
But this number is supposed to be used in a formula to be useful to me.
So I need to change it using:
float finalData = (float)result*3.22;
finalData /= 4096;
But as soon as I do it and try to display it using:
Unicode::snprintfFloat(measurementTextCH1Buffer, 50, "%.3f", finalData);
The board crashes.
If anyone can see the issue I would love to hear how to fix this.
2020-07-07 12:45 PM
Try debugging
Where does it crash?
Get to Hard Fault Handler?
Perhaps have the handler output actionable data.
Have you enabled the FPU?
2020-07-07 12:53 PM
I cant see where it crashes as I am programming with stm32cubeIDE, but I am uploading to the board using TouchGFX, which shows me if there are issues with running the target, but since this crash only occurs when I press the button that initiates the SPI receive and formula.
I can deduce that the crash only happens when I use the formula though since I can take it out and display result as an int on the screen.
I am assuming FPU is enabled since I can use this:
int x = 2007;
float finalData = (float)x*3.22;
finalData /= 4096;
and display the result fine with no issues.
2020-07-07 07:22 PM
I find it hard to believe that TouchGFX projects has no debugging capability.
Working on getting debugging up and running is something that is going to pay off in the long run.
2020-07-07 07:27 PM
Debugging takes many forms.
Having the Hard Fault and Error Handler output some actionable information to a serial port might be more enlightening than a while(1) of silent death.
2020-07-07 08:56 PM
try to print the float valur as decimal in multiplied say by 1000. Usually float required more libraries and code space, could have compile enable flags to activate.
2020-07-08 01:55 AM
I am assuming FPU is enabled since I can use this:
int x = 2007;
float finalData = (float)x*3.22;
finalData /= 4096;
They are all constant values, so it is computed by the compiler.
Make sure that you are using the right compiler options and libraries for your MCU, and that FPU is enabled.
2020-07-08 06:24 AM
I was hoping the FPU was not enabled as then my problem would be solved, but alas I have looked and I currently have single precision floating point enabled.
I will look into if there are other libraries that I may need.
2020-07-08 07:00 AM
I have solved the issue.
I was using an FPU for single point precision, but my numbers in the formula ie. 3.2 is a double by default which causes the formula to take up far too much memory and causes the crash.
Now my formula looks like this:
static float finalData = result*(3.22f/4096.0f);
and it works.
Thank you for your patience.