我正在尝试通过SPI设置esp32(主设备)和stm32(从设备)之间的通信。 esp32在micropython下运行,并发送四个字节,例如
spi.write_readinto(b'\x31\x32\x33\x34',buf)
stm32'代码在这里(而不是我使用SPI_InitDef.SPI_NSS = SPI_NSS_Soft;
)
void SPI_Init(void) {
...
// initialize SPI slave
// for slave,no need to define SPI_BaudRatePrescaler
SPI_InitDef.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitDef.SPI_Mode = SPI_Mode_Slave;
SPI_InitDef.SPI_DataSize = SPI_DataSize_8b; // 8-bit transactions
SPI_InitDef.SPI_FirstBit = SPI_FirstBit_MSB; // MSB first
SPI_InitDef.SPI_CPOL = SPI_CPOL_Low; // CPOL = 0,clock idle low
SPI_InitDef.SPI_CPHA = SPI_CPHA_2Edge; // CPHA = 1
SPI_InitDef.SPI_NSS = SPI_NSS_Hard; // use hardware SS
SPI_InitDef.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64; // APB2 72/64 = 1.125 MHz
SPI_InitDef.SPI_CRCPolynomial = 7;
SPI_Init(SPI1,&SPI_InitDef);
SPI_Cmd(SPI1,ENABLE);
NVIC_EnableIRQ(SPI1_IRQn);
//Тут мы разрешаем прерывание по приему
SPI_I2S_ITConfig(SPI1,SPI_I2S_IT_RXNE,ENABLE);
}
void main() {
/* Setup SysTick Timer for 10ms interrupts */
if (SysTick_Config(SystemCoreclock / 100))
{
/* Capture error */
while (1);
}
/* Configure the SysTick handler priority */
NVIC_SetPriority(SysTick_IRQn,0x0);
SPI_Init();
while(1) {
while(!SPI_I2S_GetflagStatus(SPI1,SPI_I2S_flaG_RXNE));
for (u8 i=0; i<4; i++) {
printf("0x%02x ",SPI_I2S_ReceiveData(SPI1));
}
printf("\r\n");
}
}
但是当我发送四个字节0x31 0x32 0x33 0x34
(分析器确认已发送字节)并且我的stm仅获得0x31 0x32 0x31 0x32
UPD 我使用std周线库,而SPI_I2S_ReceiveData是从SPI读取字节的本地方法。
uint16_t SPI_I2S_ReceiveData ( SPI_TypeDef * SPIx )
Returns the most recent received data by the SPIx/I2Sx peripheral.
Parameters:
SPIx,: To select the SPIx/I2Sx peripheral,where x can be: 1,2 or 3 in SPI mode or 2 or 3 in I2S mode or I2Sxext for I2S full duplex mode.
Return values:
The value of the received data.
uint16_t SPI_I2S_ReceiveData ( SPI_TypeDef * SPIx )
Returns the most recent received data by the SPIx/I2Sx peripheral.
Parameters:
SPIx,2 or 3 in SPI mode or 2 or 3 in I2S mode or I2Sxext for I2S full duplex mode.
Return values:
The value of the received data.
但是也许我在读取所有数据之前退出了IRQ。我发现运行while循环,直到最后一个字节的传输完成为止