Skip to main content
barmy
Associate II
December 11, 2019
Question

Hard fault occur during first byte receive

  • December 11, 2019
  • 2 replies
  • 1094 views

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.

This topic has been closed for replies.

2 replies

Bob S
Super User
December 11, 2019

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.

barmy
barmyAuthor
Associate II
December 11, 2019

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.

Tesla DeLorean
Guru
December 11, 2019

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 VenmoUp vote any posts that you find helpful, it shows what's working..