cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F7508 Board crash when using SPI recieved variable in a simple math formula.

DElli.1
Associate III

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.

8 REPLIES 8

Try debugging

Where does it crash?

Get to Hard Fault Handler?

Perhaps have the handler output actionable data.

Have you enabled the FPU?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

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.

TDK
Guru

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.

If you feel a post has answered your question, please click "Accept as Solution".

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.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
S.Ma
Principal

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.

berendi
Principal

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.

https://godbolt.org/z/MmgNqk

Make sure that you are using the right compiler options and libraries for your MCU, and that FPU is enabled.

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.

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.