cancel
Showing results for 
Search instead for 
Did you mean: 

Hard fault occur during first byte receive

barmy
Associate II

I am trying to receive 16bit data(at once) through SPI and transmit 8 bit data through USART.

clock frequency 16MHz.

All initialization done using STM32CubeMX. and build was successful .

but after writing following codes , control enters in Hard Fault at first byte receive.

variables declared:

uint16_t buffer_t[2];

uint8_t buffer_r[8];

uint8_t data_tx8[16];

codes written in while loop:

HAL_SPI_Receive(&hspi2,buffer_t,8,10000);

  uint16_t j=buffer_t[1];

   uint16_t k=buffer_t[0];

I am not able to understand why hard fault occurs here. (Same code runs properly if the code is configured only for SPI receive). I am using STM32L072 discovery kit.

thanks in advance.

4 REPLIES 4
Bob S
Principal

Did you copy/paste your actual code above, or did you type it from memory? Because you are telling HAL_SPI_Receive() to receive 8 "things" of data, where a "thing" is either 8-bits or 16-bits depending on how you configured the SPI port, and the buffer you are passing is only 2 words long. Perhaps you meant "buffer_r" instead of "buffer_t".

Search these forms for "hard fault" and you should find LOTS of posts about tracking down the cause. Specifically, avagadro/clive @Community member​ has posted code samples to read/display the fault registers.

Yes , I should have given 2 in place of 8 things. this might be the reason for hard fault. Tomorrow I will run it and confirm.

Thanks for so quick response.

Get a proper Hard Fault Handler running.

The L0 (CM0) is going to be particularly sensitive to unaligned reads/writes. So look for code in the SPI driver, or wherever, that is casting a pointer to (uint16_t *) or (uint32_t *). This have a high probability in faulting.

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

yes it is working perfectly fine . thanks for help Bob S.