ST意法半导体
直播中

贾小龙

7年用户 1587经验值
私信 关注
[问答]

使用Raspberry Pi和STM32G0B1进行通信,过早/过晚导致读取不应该存在的位怎么解决?

我有一个外围设备,我正在使用 Raspberry Pi 和 STM32G0B1(两种不同的设备,两种不同的设置)进行通信

我基本上是在尝试重现我们在 STM32 平台上对这个外围设备所做的成功通信。我有一些奇怪的结果。

CS 线似乎变低或变高,过早/过晚导致读取不应该存在的位。

我正在发送 3 个单独的帧。4字节,10字节,1字节。没有正在读取的输入,因此可以忽略这些行。

下面我展示了 RPi 和 STM32 之间的输出比较。
下图显示了以下信号:
SDO
SDI  <-- no traffic on any signal
CS
CLK

4 bytes frame on Rpi, matches code:

4 bytes on STM32 (should match above):

It can be seen that it appears the bits are shifted 1 to the left here.

The 10 byte frame:
Same again with the STM32:

The final frame is a single byte, the number '8'
On the Rpi it looks like this:
The STM32 reads this as 11

Here it looks like the CS line line stay low for too long causing a 1 to be read, where in the RPI it can be seen that CS goes high right at the end of the last byte.

My code uses the HAL libraries and the sending function is simple:
  • void spi_transfer(uint8_t board, uint8_t *pData, uint16_t Size) {
  •         HAL_StatusTypeDef  status;
  •         HAL_GPIO_WritePin(SPI_CE0_GPIO_Port, SPI_CE0_Pin, GPIO_PIN_RESET);
  •         status = HAL_SPI_Transmit(&hspi1, pData, Size, 100);
  •         HAL_GPIO_WritePin(SPI_CE0_GPIO_Port, SPI_CE0_Pin, GPIO_PIN_SET);
  •         if (status != HAL_OK) {
  •                 serprintf("Error writing to SPI on board: %i. (%i)
    ", board, status);
  •         }
  • }
I've experimented with the clock speed, and some other settings.  But nothing seemed to help.

  •   hspi1.Instance = SPI1;
  •   hspi1.Init.Mode = SPI_MODE_MASTER;
  •   hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  •   hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  •   hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  •   hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  •   hspi1.Init.NSS = SPI_NSS_SOFT;
  •   hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
  •   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;
Thanks for any tips


回帖(1)

李德鹏

2022-12-26 15:09:17
SPI有4种模式,由2位设置,传统上称为CPOL和CPHA。阅读 RM 中的 SPI 章节。

你设置它们
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
即CPOL=0,CPHA=0,SPI波形符合。

RPi波形不符合,明显是CPOL=0 CPHA=1,LA也设置成这个模式。
举报

更多回帖

发帖
×
20
完善资料,
赚取积分