cancel
Showing results for 
Search instead for 
Did you mean: 

SPI Receives junk

con3
Senior

Hey everyone,

I've got an SPI line setup on my stm32f722ze.

Cache is not enabled. It worked and I'm not sure whether the code was changed, but it receives junk now.

The DMA is triggered which shows that when I send data, it is receiving it, but not the correct data.

I Have verified the data that's being sent via an oscilloscope.

The stm's spi line is coupled to a esp32. The esp32 is the master and the stm is running as slave

Here's the relevant code for the stm32:

Hal_msp.c:

void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
{
 
  GPIO_InitTypeDef GPIO_InitStruct;
  if(hspi->Instance==SPI1)
  {
  /* USER CODE BEGIN SPI1_MspInit 0 */
 
  /* USER CODE END SPI1_MspInit 0 */
    /* Peripheral clock enable */
    __HAL_RCC_SPI1_CLK_ENABLE();
 
    /**SPI1 GPIO Configuration
    PA4     ------> SPI1_NSS
    PA5     ------> SPI1_SCK
    PA6     ------> SPI1_MISO
    PA7     ------> SPI1_MOSI
    */
    GPIO_InitStruct.Pin = GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
    /* SPI1 DMA Init */
    /* SPI1_RX Init */
    hdma_spi1_rx.Instance = DMA2_Stream0;
    hdma_spi1_rx.Init.Channel = DMA_CHANNEL_3;
    hdma_spi1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
    hdma_spi1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
    hdma_spi1_rx.Init.MemInc = DMA_MINC_ENABLE;
    hdma_spi1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
    hdma_spi1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
    hdma_spi1_rx.Init.Mode = DMA_CIRCULAR;
    hdma_spi1_rx.Init.Priority = DMA_PRIORITY_HIGH;
    hdma_spi1_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
    if (HAL_DMA_Init(&hdma_spi1_rx) != HAL_OK)
    {
      _Error_Handler(__FILE__, __LINE__);
    }
 
    __HAL_LINKDMA(hspi,hdmarx,hdma_spi1_rx);
 
    /* SPI1_TX Init */
    hdma_spi1_tx.Instance = DMA2_Stream5;
    hdma_spi1_tx.Init.Channel = DMA_CHANNEL_3;
    hdma_spi1_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
    hdma_spi1_tx.Init.PeriphInc = DMA_PINC_DISABLE;
    hdma_spi1_tx.Init.MemInc = DMA_MINC_ENABLE;
    hdma_spi1_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
    hdma_spi1_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
    hdma_spi1_tx.Init.Mode = DMA_CIRCULAR;
    hdma_spi1_tx.Init.Priority = DMA_PRIORITY_HIGH;
    hdma_spi1_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
    if (HAL_DMA_Init(&hdma_spi1_tx) != HAL_OK)
    {
      _Error_Handler(__FILE__, __LINE__);
    }
 
    __HAL_LINKDMA(hspi,hdmatx,hdma_spi1_tx);
 
  /* USER CODE BEGIN SPI1_MspInit 1 */
 
  /* USER CODE END SPI1_MspInit 1 */

my main.c file:

char FromESP1[15];
static void MX_SPI1_Init(void) {
 
	/* SPI1 parameter configuration*/
	hspi1.Instance = SPI1;
	hspi1.Init.Mode = SPI_MODE_SLAVE;
	hspi1.Init.Direction = SPI_DIRECTION_2LINES;
	hspi1.Init.DataSize = SPI_DATASIZE_15BIT;
	hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
	hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
	hspi1.Init.NSS = SPI_NSS_HARD_INPUT;
	hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
	hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
	hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
	hspi1.Init.CRCPolynomial = 7;
	hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
	hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
	if (HAL_SPI_Init(&hspi1) != HAL_OK) {
		_Error_Handler(__FILE__, __LINE__);
	}
 
}
static void MX_DMA_Init(void) {
	/* DMA controller clock enable */
	__HAL_RCC_DMA2_CLK_ENABLE()
	;
 
	/* DMA interrupt init */
	/* DMA2_Stream1_IRQn interrupt configuration */
	HAL_NVIC_SetPriority(DMA2_Stream0_IRQn, 0, 1);
	HAL_NVIC_EnableIRQ(DMA2_Stream0_IRQn);
	HAL_NVIC_SetPriority(DMA2_Stream1_IRQn, 1, 1);
	HAL_NVIC_EnableIRQ(DMA2_Stream1_IRQn);
	/* DMA2_Stream2_IRQn interrupt configuration */
	HAL_NVIC_SetPriority(DMA2_Stream2_IRQn, 0, 1);
	HAL_NVIC_EnableIRQ(DMA2_Stream2_IRQn);
	/* DMA2_Stream3_IRQn interrupt configuration */
	HAL_NVIC_SetPriority(DMA2_Stream3_IRQn, 1, 1);
	HAL_NVIC_EnableIRQ(DMA2_Stream3_IRQn);
	/* DMA2_Stream5_IRQn interrupt configuration */
	HAL_NVIC_SetPriority(DMA2_Stream5_IRQn, 1, 1);
	HAL_NVIC_EnableIRQ(DMA2_Stream5_IRQn);
 
}
	__HAL_RCC_GPIOA_CLK_ENABLE()
	;
	//SPI1 ALTERNATE FUNCTION PIN SETUP
	GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_7;
	GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
	GPIO_InitStruct.Pull = GPIO_NOPULL;
	GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
	GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
	HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
	HAL_SPI_Receive_DMA(&hspi1, (uint8_t *) FromESP1, sizeof(FromESP1));
 
void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi) {
	if (hspi->Instance == SPI1) {
		if (FromESP1[0] == 'B') {
//code
}
}
}

my interrupt.c file:

void DMA2_Stream5_IRQHandler(void)
{
  /* USER CODE BEGIN DMA2_Stream5_IRQn 0 */
 
  /* USER CODE END DMA2_Stream5_IRQn 0 */
  HAL_DMA_IRQHandler(&hdma_spi1_tx);
  /* USER CODE BEGIN DMA2_Stream5_IRQn 1 */
 
  /* USER CODE END DMA2_Stream5_IRQn 1 */
}
/* USER CODE END 1 */
 
void DMA2_Stream0_IRQHandler(void)
{
  /* USER CODE BEGIN DMA2_Stream0_IRQn 0 */
 
 
  /* USER CODE END DMA2_Stream0_IRQn 0 */
  HAL_DMA_IRQHandler(&hdma_spi1_rx);
  /* USER CODE BEGIN DMA2_Stream0_IRQn 1 */
 
  /* USER CODE END DMA2_Stream0_IRQn 1 */
}

The code for the esp is written in micropython and here is the configuration and send code:

spi = machine.SPI(spihost=machine.SPI.HSPI, baudrate= 1000000, polarity=0, phase=0, firstbit = machine.SPI.MSB, sck=12, mosi=21, miso=14, cs=22, duplex =  False)
 
 
spi.write('BBBBBBBBBBBBBBB')

The wiki of the micropython:

https://github.com/loboris/MicroPython_ESP32_psRAM_LoBo/wiki/spi

Here are some pictures of me sending data to the stm from the esp.

The probe's are pretty crappy and I'm probing with long cables, so please ignore the signal integrity issues, there not seen when measuring correctly.

CS line, mosi and Sclk:

0690X000006ClvoQAC.jpg

 The Sclk and MOSI line when I send 'BB'

0690X000006ClvtQAC.jpg

I'm not sure what's causing this, but I have a feeling it's an incorrect configuration somewhere. Any help will really really be appreciated.

thanks in advance!

Oh no error code is returned once the spi has received data and the dma callback is done

Oh I also regenerated the code with cube for a stm32f446re and I see the same error, so I'm pretty sure I'm not configuring something correctly. The callback is called once I send data from the ESP, although the data is junk.

1 ACCEPTED SOLUTION

Accepted Solutions

Hey @Community member​ ,

I did some rubber duck debugging and I don't know why, but the datasize in the SPI init was set to 15 bits, but my buffer type is a char. setting this to 8 bits, completely fixed the issue. Small stupid mistake.

View solution in original post

4 REPLIES 4

> The DMA is triggered which shows that when I send data, it is receiving it, but not the correct data.

And what does it receive?

Read out the content of relevant SPI and GPIO registers and check.

JW

Hi @Community member​ ,

Thank you for the reply.

I can't see anything odd concerning the registers. Here's the register values:

After setup and prior to sending any data:

The SPI registers

0690X000006Cm05QAC.png

0690X000006Cm0FQAS.png

The DMA registers:

0690X000006Cm0KQAS.png

0690X000006Cm0UQAS.png

0690X000006Cm0ZQAS.png

0690X000006Cm0eQAC.png

0690X000006Cm0jQAC.png

0690X000006Cm0oQAC.png

Here I send 15 characters all the same value which is 'Z' and this is the received data (Prior to sending, the buffer was filled with 0):

0690X000006Cm0AQAS.png

And these are the registers that changed after the data was received:

0690X000006Cm0tQAC.png

I'm still not sure why this is happening..

Hey @Community member​ ,

I did some rubber duck debugging and I don't know why, but the datasize in the SPI init was set to 15 bits, but my buffer type is a char. setting this to 8 bits, completely fixed the issue. Small stupid mistake.

Please mark your answer as Best to indicate that issue is solved.