cancel
Showing results for 
Search instead for 
Did you mean: 

How to reset SPI using HAL functions using Nucleo - L073RZ

JPaz.1
Associate II

I am working on a SPI project (using DMA) as a slave and things seems to work fine but after an hour and maybe more in some cases I see one error and that locks the dma ....I can detect when that happens but I have problems to reset the SPI interface...I tried several things what were posted before like:

HAL_SPI_Abort_IT(&hspi3);

HAL_SPI_Abort(&hspi3);

HAL_SPI_AbortCpltCallback(&hspi3);

HAL_SPIEx_FlushRxFifo(&hspi3);

HAL_SPI_DeInit(&hspi3);

HAL_SPI_Init(&hspi3);

but it does not fix it ...I am using HAL functions ...in a post 2 years ago from from Gone Aug 06 he included the following:

  1. SPIP_SlaveConfigureSpi(pSPIP->hspi);
  2. SPIP_SlaveConfigureDma(pSPIP->hspi);

I do not think these are HAL functions I wonder if you anybody has the code for the functions mentioned above ?

I have been struggling with this for some time ......not sure if it is a noise issue (working with NUCLEO L073RZ ) I am using the highest priority for the SPi and also trying to add pull downs to clock and mosi to try to reduce noise but does not seem to help ....

Thanks for your help.

12 REPLIES 12

I found this code but for init and de-init but it has a lot of manual things like dealing directly with all GPIO's, initializing all hdma_rx.Init.*** and hdma_tx.Init.*** structures and the enable/ disable and set again priority to interrupts, is all of that needed to reset SPI I wanted to use only HAL functions for that

void HAL_SPI_MspInit(SPI_HandleTypeDef *hspi)

{

 static DMA_HandleTypeDef hdma_tx;

 static DMA_HandleTypeDef hdma_rx;

  

 GPIO_InitTypeDef GPIO_InitStruct;

  

 /*##-1- Enable peripherals and GPIO Clocks #################################*/

 /* Enable GPIO TX/RX clock */

 SPIx_SCK_GPIO_CLK_ENABLE();

 SPIx_MISO_GPIO_CLK_ENABLE();

 SPIx_MOSI_GPIO_CLK_ENABLE();

 /* Enable SPI3 clock */

 SPIx_CLK_ENABLE(); 

 /* Enable DMA1 clock */

 DMAx_CLK_ENABLE();  

  

 /*##-2- Configure peripheral GPIO ##########################################*/  

 /* SPI SCK GPIO pin configuration */

 GPIO_InitStruct.Pin    = SPIx_SCK_PIN;

 GPIO_InitStruct.Mode   = GPIO_MODE_AF_PP;

 GPIO_InitStruct.Pull   = GPIO_PULLUP;

 GPIO_InitStruct.Speed   = GPIO_SPEED_FAST;

 GPIO_InitStruct.Alternate = SPIx_SCK_AF;

  

 HAL_GPIO_Init(SPIx_SCK_GPIO_PORT, &GPIO_InitStruct);

   

 /* SPI MISO GPIO pin configuration */

 GPIO_InitStruct.Pin = SPIx_MISO_PIN;

 GPIO_InitStruct.Alternate = SPIx_MISO_AF;

  

 HAL_GPIO_Init(SPIx_MISO_GPIO_PORT, &GPIO_InitStruct);

  

 /* SPI MOSI GPIO pin configuration */

 GPIO_InitStruct.Pin = SPIx_MOSI_PIN;

 GPIO_InitStruct.Alternate = SPIx_MOSI_AF;

   

 HAL_GPIO_Init(SPIx_MOSI_GPIO_PORT, &GPIO_InitStruct);

   

 /*##-3- Configure the DMA streams ##########################################*/

 /* Configure the DMA handler for Transmission process */

 hdma_tx.Instance         = SPIx_TX_DMA_STREAM;

  

 hdma_tx.Init.Channel       = SPIx_TX_DMA_CHANNEL;

 hdma_tx.Init.Direction      = DMA_MEMORY_TO_PERIPH;

 hdma_tx.Init.PeriphInc      = DMA_PINC_DISABLE;

 hdma_tx.Init.MemInc       = DMA_MINC_ENABLE;

 hdma_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;

 hdma_tx.Init.MemDataAlignment  = DMA_MDATAALIGN_BYTE;

 hdma_tx.Init.Mode        = DMA_NORMAL;

 hdma_tx.Init.Priority      = DMA_PRIORITY_LOW;

 hdma_tx.Init.FIFOMode      = DMA_FIFOMODE_DISABLE;     

 hdma_tx.Init.FIFOThreshold    = DMA_FIFO_THRESHOLD_FULL;

 hdma_tx.Init.MemBurst      = DMA_MBURST_INC4;

 hdma_tx.Init.PeriphBurst     = DMA_PBURST_INC4;

  

 HAL_DMA_Init(&hdma_tx);  

  

 /* Associate the initialized DMA handle to the the SPI handle */

 __HAL_LINKDMA(hspi, hdmatx, hdma_tx);

   

 /* Configure the DMA handler for Transmission process */

 hdma_rx.Instance         = SPIx_RX_DMA_STREAM;

  

 hdma_rx.Init.Channel       = SPIx_RX_DMA_CHANNEL;

 hdma_rx.Init.Direction      = DMA_PERIPH_TO_MEMORY;

 hdma_rx.Init.PeriphInc      = DMA_PINC_DISABLE;

 hdma_rx.Init.MemInc       = DMA_MINC_ENABLE;

 hdma_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;

 hdma_rx.Init.MemDataAlignment  = DMA_MDATAALIGN_BYTE;

 hdma_rx.Init.Mode        = DMA_NORMAL;

 hdma_rx.Init.Priority      = DMA_PRIORITY_HIGH;

 hdma_rx.Init.FIFOMode      = DMA_FIFOMODE_DISABLE;     

 hdma_rx.Init.FIFOThreshold    = DMA_FIFO_THRESHOLD_FULL;

 hdma_rx.Init.MemBurst      = DMA_MBURST_INC4;

 hdma_rx.Init.PeriphBurst     = DMA_PBURST_INC4; 

 HAL_DMA_Init(&hdma_rx);

   

 /* Associate the initialized DMA handle to the the SPI handle */

 __HAL_LINKDMA(hspi, hdmarx, hdma_rx);

   

 /*##-4- Configure the NVIC for DMA #########################################*/ 

 /* NVIC configuration for DMA transfer complete interrupt (SPI3_TX) */

 HAL_NVIC_SetPriority(SPIx_DMA_TX_IRQn, 0, 1);

 HAL_NVIC_EnableIRQ(SPIx_DMA_TX_IRQn);

   

 /* NVIC configuration for DMA transfer complete interrupt (SPI3_RX) */

 HAL_NVIC_SetPriority(SPIx_DMA_RX_IRQn, 0, 0);  

 HAL_NVIC_EnableIRQ(SPIx_DMA_RX_IRQn);

}

void HAL_SPI_MspDeInit(SPI_HandleTypeDef *hspi)

{

  

 static DMA_HandleTypeDef hdma_tx;

 static DMA_HandleTypeDef hdma_rx;

 /*##-1- Reset peripherals ##################################################*/

 SPIx_FORCE_RESET();

 SPIx_RELEASE_RESET();

 /*##-2- Disable peripherals and GPIO Clocks ################################*/

 /* Configure SPI SCK as alternate function */

 HAL_GPIO_DeInit(SPIx_SCK_GPIO_PORT, SPIx_SCK_PIN);

 /* Configure SPI MISO as alternate function */

 HAL_GPIO_DeInit(SPIx_MISO_GPIO_PORT, SPIx_MISO_PIN);

 /* Configure SPI MOSI as alternate function */

 HAL_GPIO_DeInit(SPIx_MOSI_GPIO_PORT, SPIx_MOSI_PIN);

  

 /*##-3- Disable the DMA Streams ############################################*/

 /* De-Initialize the DMA Stream associate to transmission process */

 HAL_DMA_DeInit(&hdma_tx); 

 /* De-Initialize the DMA Stream associate to reception process */

 HAL_DMA_DeInit(&hdma_rx);

  

 /*##-4- Disable the NVIC for DMA ###########################################*/

 HAL_NVIC_DisableIRQ(SPIx_DMA_TX_IRQn);

 HAL_NVIC_DisableIRQ(SPIx_DMA_RX_IRQn);

}