Skip to main content
Visitor II
February 6, 2025
Solved

STM32L476, HAL library SPI3 write lead to hard reset

  • February 6, 2025
  • 3 replies
  • 544 views

I have the below code, which is supposed to update DAC with sinewave via SPI3 bus. It was configured and working fine via discrete command, but when I used timer7 to generate an event and update DAC16 via SPI3 via the HAL library, it went to hard fault. I have never seen this before. What caused the hard fault to occur?

I use Nucleo STM32L476 and updated IDE and drivers.

Note I have not included the /CS code (yet) because I was trying to figure out what caused the hard fault. I use optimization -Ofast.

 

 

#define DAC16_CENTER (32768)
bool FID_DAC16_Timer7_Sinewave_Event(void)
{
volatile static uint16_t DACSine_Pointer=0;

//--------------------------------------------------
zFID_Obj *zFIDObj = (zFID_Obj *)zFIDHandle; // assign the object (local) utobj->
//--------------------------------------------------
uint16_t dacdata = FID_SineWaveTable[DACSine_Pointer];
if (DACSine_Pointer >= FID_SinewaveSise)
{
DACSine_Pointer= 0;
}
else
{
DACSine_Pointer++;
}
//------------------------------- Process amplitude adjustment while keeping center center.

int32_t centered = (int32_t)dacdata - DAC16_CENTER;
centered = centered / zFIDObj->DACSine_Amp;
dacdata = (uint16_t)(centered + DAC16_CENTER);
//------------------------------- Write data to SPI
uint8_t data[] = { dacdata & 0xFF, dacdata >> 8 }; //flipped for proper ordering of MSD.
HAL_SPI_Transmit(&hspi3, data, 1, HAL_MAX_DELAY); // Hard fault crashed event.

//FID_SPI_WriteData16Simple(FID_CS1_DAC16A,dacdata);
return true;
}

 

 

 

    This topic has been closed for replies.

    3 replies

    Riscy24Author
    Visitor II
    February 6, 2025

    I forgot to mention I use 16-bit SPI data, but the HAL SPI transmit and received are working fine when not running from Timer7. 

    Riscy24Author
    Visitor II
    February 17, 2025

    Thank for the link, very useful. It turned out it was divided by zero hard fault. Nothing to do with HAL library which is a relief. 

    Lesson learnt and moving on!

    R.