[SPI] STM32 SPI dual-machine communication, SPI slave mode use

Recently, I will use the SPI slave mode of STM32, read SPI data from other boards, step on 2 sinkholes, and record the process.

(Because the hal library provides three functions, my debugging process is to debug and learn by blocking, interrupting, and DMA in turn. This code is the code using DMA. )

Software : keil5, STM32CubeMX

Hardware : Two STM32F103C8T6 minimum systems

Realized function : Two boards perform SPI communication, one master and one slave, both of which use SPI1.

Code download github : https://github.com/wyfroom/SPI_Master_Slave_STM32 (use git to manage code during the first programming process)

Code download Lanzout Cloud : https://wwzr.lanzout.com/b04885ouf Password: 372j

emphasize! ! ! ! ! ! ! ! ! ! ! emphasize! ! ! ! ! ! ! ! ! ! ! ! emphasize! ! ! ! ! ! ! ! ! ! ! ! !

If your SPI is connected, but the data is inexplicably garbled, whether it can be sent or not received, etc., don’t do anything, first replace the Dupont wire with a new one , a total of 5 wires, one common ground wire, and four SPI wires.

image-20230628105858537

1. SPI host configuration

For the basic project configuration, there is no map here, you can see the detailed steps here: http://t.csdn.cn/Wpcpk

image-20230627210413145

Look directly at the configuration of spi, mainly pay attention to the following points:

  • SPI mode selection
  • chip selection
  • big endian little endian selection
  • communication rate
  • Sampling mode configuration (CPOL, CPHA)
  • CRC selection

image-20230627211841705

If you want to use SPI interrupt, check the interrupt.

image-20230627213345312

If you want to use DMA, you need to configure the DMA.

image-20230627212414628

Supplement: Chip selection still needs to be selected, otherwise there will be problems in high-speed transmission.

image-20230628091705132

2. SPI slave configuration

Only the mode selection of the slave machine is different from the master machine , and the others are the same and must be the same.

image-20230627213119402

If interrupt is used, tick:

image-2023062721311940123

DMA configuration: (same as host)

image-20230627213520055

Supplement: Chip selection is still necessary, otherwise there will be problems during high-speed transmission .

image-20230628091658095

Three, two-way communication

The HAL library provides three SPI calling methods. I tried it all, and finally chose DMA, which is efficient and concise.

//阻塞方式使用SPI
HAL_SPI_Transmit()
HAL_SPI_Receive()
HAL_SPI_TransmitReceive()
//中断方式使用SPI
HAL_SPI_Transmit_IT()
HAL_SPI_Receive_IT()
HAL_SPI_TransmitReceive_IT()
//DMA方式使用SPI
HAL_SPI_Transmit_DMA()
HAL_SPI_Receive_DMA()
HAL_SPI_TransmitReceive_DMA()

image-20230627215942390

1 polling + interrupt (low speed)

Master polls, slave interrupts.

I will not demonstrate this, just the normal process, just pay attention, start the interrupt in the main, and start the interrupt again in the interrupt function.

//main里启动中断
HAL_SPI_TransmitReceive_IT(&hspi1, sendData, receiveData, 8);

//中断回调函数
void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)
{
  // 数据发送完成回调函数
	if (hspi == &hspi1)
	{
		HAL_SPI_TransmitReceive_IT(&hspi1, sendData, receiveData, 8);
	}
}

2 Polling + DMA (low speed)

The master uses the blocking function, and the slave uses the DMA function. (The reason is that if I tried to use DMA for both master and slave, the speed is too fast, it will be stuck and the data transmission will be garbled. It is solved, you can see problem 1, this part is still a demonstration of polling + DMA )

Host:
image-20230628094249979

Slave:
image-20230628094315973

debug test:

image-20230628094523589

3 DMA+DMA (high speed)

Before high-speed use, the program was stuck and the data was garbled. (High-speed use: use the blocking function while without delay, use interrupts for both master and slave, and use DMA for both master and slave)

The solution is as follows:

  • 1 chip select to be turned on . I directly turned on the hardware chip select of the two boards, and both sides used DMA to send and read in full duplex. The data was not garbled, but the first data received was not necessarily the first data sent.
  • 2 The slave starts the program before the master. Solve the problem of incorrect data order in the previous step. Add a delay of 2s before the host DMA program starts.

image-20230628092722959

image-20230628092806945

Direct Debug: (simulate the actual power supply, debug first click the slave to start, and then click the host to start)

image-20230628093140796

4 Enable CRC check (optional)

The check value generated by CRC calculation can be used to detect whether data is wrong or damaged during transmission or storage.
image-20230627220307966

After the CRC is enabled, if the DMA function is used, the SIZE should be increased by one:

HAL_SPI_TransmitReceive_DMA(&hspi1, sendData, receiveData, 8+1);

image-20230627220412238

4. Problems encountered

1 When using at high speed, the program is stuck, or the data is wrong (solved)

  • The master interrupt mode sends, the slave machine interrupt mode receives, and the data will be messed up if the clear flag is sent and received continuously. Maybe the transmission is too fast? ? Do you think it is sending and receiving at full speed? The host has to use polling method to read at an interval of 1ms or something, so how do you calculate the SPI speed?

  • The host machine DMA sends, the slave machine DMA receives, it crashes directly, the DMA interrupt is maxed out, the program in the while has no chance to execute, you can shield the DMA interrupt, the program will not crash, but the data will be messed up.

    image-20230627215523991

  • Adding CRC seems to be a little better, but there are still a lot of garbled characters, just luck. can this work? Isn't that what SPI is used for? Or is it a common problem of high-speed transmission.

  • Finally solved :

    • 1 chip select to be turned on . I directly turned on the hardware chip select of the two boards, and both sides used DMA to send and read in full duplex. The data was not garbled, but the first data received was not necessarily the first data sent.
    • 2 The slave starts the program before the master. Solve the problem of incorrect data order in the previous step. Add a delay of 2s before the host DMA program starts.
    • 3 CRC , it doesn't matter whether it is turned on or not.

    image-20230628091658095

    image-20230628091705132

    image-20230628091830064

2 The data is inexplicably garbled, the host sends normally, receives garbled characters, etc.

  • Dupont wires will really cause problems, so replace them with new ones, and replace all 5 wires with new ones. try again

Guess you like

Origin blog.csdn.net/weixin_44029896/article/details/131432366
SPI