cancel
Showing results for 
Search instead for 
Did you mean: 

SPI Master receive DMA always go to HAL_SPI_ErrorCallback() on STM32F407-disco

victagayun
Senior III

I am struggling on SPI Master receive on STM32F407-disco board.

I plan to interface to the microphone using SPI2 master receive.

I setup the DMA but I always get to HAL_SPI_ErrorCallback().

I setup the same setup using Nucleo-G474 but it can go to HAL_SPI_RxCpltCallback().

I used the same codes for both G474 and F407.

HAL_SPI_Receive_DMA(&hspi2, receive_data, 5);
 
void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi)
{
	printf("SPI Received! \n");
}
 
void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi)
{
	printf("TRANSFER_ERROR! \n");
}

I am using CubeMX to setup F407 as shown below.

F407 parameter setting

0693W00000FDHQbQAP.png 

F407 DMA setting

0693W00000FDHQqQAP.png 

I used the same on G474 as shown below.

G474 parameter setting

0693W00000FDHRKQA5.png 

G474 DMA setting

0693W00000FDHReQAP.png 

Basically everything is almost the same.

When debug, I checked the before and after HAL_SPI_Receive_DMA() of both SPI and DMA register data as shown below.

G474 DMA1 before HAL_SPI_Receive_DMA()

0693W00000FDHSSQA5.png 

G474 DMA1 after HAL_SPI_Receive_DMA()

0693W00000FDHQ8QAP.png 

G474 SPI2 before HAL_SPI_Receive_DMA()

0693W00000FDHTaQAP.png 

G474 SPI2 after HAL_SPI_Receive_DMA()

0693W00000FDHUnQAP.png 

F407 DMA1 before HAL_SPI_Receive_DMA()

0693W00000FDHVRQA5.png 

F407 DMA1 after HAL_SPI_Receive_DMA()

0693W00000FDHVqQAP.png 

Any idea what did I miss? I am using CubeIDE 1.7.0 and STM32Cube_FW_F4_V1.26.2 and STM32Cube_FW_G4_V1.4.0.

Here are the IOC files for both devices.

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

Within HAL_SPI_ErrorCallback, what is the error code stored in hspi->ErrorCode?

Receive-only mode on the SPI is odd and could be causing issues. Using two-way transfer using MOSI uninitialized may work better.

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

View solution in original post

4 REPLIES 4
TDK
Guru

Within HAL_SPI_ErrorCallback, what is the error code stored in hspi->ErrorCode?

Receive-only mode on the SPI is odd and could be causing issues. Using two-way transfer using MOSI uninitialized may work better.

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

So the error code is 32 using this code.

void HAL_SPI_ErrorCallback(SPI_HandleTypeDef *hspi)
{
	UNUSED(hspi);
 
	printf("HAL_SPI_ErrorCallback : Error code %d \n", hspi->ErrorCode);
 
}

When I changed to FullDuplex Master, it is working, though I wont be using the MOSI.

HAL_SPI_TransmitReceive_DMA(&hspi2, send_data, receive_data, 5);

Goes to

void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)
{
  UNUSED(hspi);
 
  printf("HAL_SPI_TxRxCpltCallback \n");
}

while

HAL_SPI_Receive_DMA(&hspi2, receive_data, 5);

goes to

void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi)
{
	UNUSED(hspi);
 
	printf("HAL_SPI_RxCpltCallback \n");
}

So I guess the errors would be

#define HAL_SPI_ERROR_NONE              (0x00000000U)   /*!< No error                               */
#define HAL_SPI_ERROR_MODF              (0x00000001U)   /*!< MODF error                             */
#define HAL_SPI_ERROR_CRC               (0x00000002U)   /*!< CRC error                              */
#define HAL_SPI_ERROR_OVR               (0x00000004U)   /*!< OVR error                              */
#define HAL_SPI_ERROR_FRE               (0x00000008U)   /*!< FRE error                              */
#define HAL_SPI_ERROR_DMA               (0x00000010U)   /*!< DMA transfer error                     */
#define HAL_SPI_ERROR_FLAG              (0x00000020U)   /*!< Error on RXNE/TXE/BSY Flag             */
#define HAL_SPI_ERROR_ABORT             (0x00000040U)   /*!< Error during SPI Abort procedure       */

HAL_SPI_ERROR_CRC + HAL_SPI_ERROR_DMA + HAL_SPI_ERROR_FLAG?

In case I would need the extra IO and still pursue Master Receive DMA only, how to tackle this problem?

TDK
Guru

> HAL_SPI_ERROR_CRC + HAL_SPI_ERROR_DMA + HAL_SPI_ERROR_FLAG?

The error is 32, which is 0x20, which is HAL_SPI_ERROR_FLAG. Most like due to the RXNE flag due to the clock not stopping at the end of the data you want to receive.

> In case I would need the extra IO and still pursue Master Receive DMA only, how to tackle this problem?

You can just leave the pin uninitialized since you don't care about that data.

I do think you'll need an additional DMA stream for the TX data, but it doesn't matter what you're sending, as long as it's valid memory. One option would be to use the same buffer for TX and RX.

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

> The error is 32, which is 0x20, which is HAL_SPI_ERROR_FLAG. Most like due to the RXNE flag due to the clock not stopping at the end of the data you want to receive.

Sorry my bad, yes I can see the CLK continuously pulsing and giving the clock/waveform.

> You can just leave the pin uninitialized since you don't care about that data.

I may not use the IO but ST should at least take a look at why there is an error.

Thanks for the help btw.