spi2半双工模式,主模式下发送和接收部分,官方有例程么???
调试不通单线spi2:
hspi2.Instance = SPI2;
hspi2.Init.Mode = SPI_MODE_MASTER;
hspi2.Init.Direction = SPI_DIRECTION_1LINE;
hspi2.Init.DataSize = SPI_DATASIZE_8BIT;
hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi2.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi2.Init.NSS = SPI_NSS_SOFT;
hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi2.Init.CRCPolynomial = 7;
if (HAL_SPI_Init(&hspi2) != HAL_OK)
{
Error_Handler();
}
HAL_Delay(100);
LL_SPI_SetTransferDirection(hspi2.Instance, LL_SPI_HALF_DUPLEX_TX);
/* Enable SPI before start transmission */
LL_SPI_Enable(hspi2.Instance);
while(ubNbDataToTransmit > 0)
{
/* Check TXE flag to transmit data */
if(LL_SPI_IsActiveFlag_TXE(hspi2.Instance))
{
/* Transmit 8bit Data */
LL_SPI_TransmitData8(hspi2.Instance, aTxBuffer[ubNbDataTransmitted++]);
ubNbDataToTransmit--;
}
}
/* Wait End Of Transmission: TXE set and Tx Fifo empty */
while((LL_SPI_IsActiveFlag_TXE(hspi2.Instance) != 1));
/*rx*/
LL_SPI_SetTransferDirection(hspi2.Instance, LL_SPI_HALF_DUPLEX_RX);
while(LL_SPI_IsActiveFlag_RXNE(hspi2.Instance) != 1);
SPI_FLASH_CS_HIGH();
while(LL_SPI_IsActiveFlag_RXNE(hspi2.Instance))
{
aRxBuffer[i] = LL_SPI_ReceiveData8(hspi2.Instance);
if(i < 10)
{
i++;
}
}
/* Disable SPI after End of Transmission */
LL_SPI_Disable(hspi2.Instance);
printf("\r\nFlashID is %s\r\n", aRxBuffer); |