我对 ST32F030 和 Slave SPI 有疑问
我试图让它响应一个 spi 命令:-
主机发送 0x05,从机以 7 字节数组响应。它第一次工作,但随后失去同步,并设置 OVR 位。我不知道为什么。
继承人的代码: -
int main(void)
{
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_SPI1_Init();
spi_init();
while (1)
{
HAL_SPI_Receive_IT(&hspi1, &spiData[0], 1);
}
}
/* SPI1 init function */
static void MX_SPI1_Init(void)
{
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_SLAVE;
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.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;
/* SPi priority interrupt configuration */
HAL_NVIC_SetPriority(SPI1_IRQn, 1, 1);
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
Error_Handler();
}
}
void spi_init()
{
HAL_SPI_Receive_IT(&hspi1, spiData, 1);
}
void SPI1_IRQHandler(void)
{
HAL_SPI_IRQHandler(&hspi1);
}
uint8_t spiData[8];
uint8_t edgesmsg[7];
void spi_interrupt()
{
uint8_t c;
c = spiData[0];
if ( c == 0x05)
HAL_SPI_Transmit(&hspi1, edgesmsg, 7, 1000);
}
任何建议都感激不尽