STM32 SPI接收数据问题
STM32和ADS8328一块AD转换芯片进行通讯,使用SPI。问题是,接收数据时RXNE始终为reset状态,部分程序如下,望各位高人指点,急用,多谢
void ADS8328_SPI_Init(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable SPI1 clocks */
RCC_APB2PeriphResetCmd(RCC_APB2Periph_SPI1, DISABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1 ,ENABLE);
/* Configure SPI1 pins: SCK, MISO and MOSI */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7; //SCK MISO MOSI
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure PA4 as Output push-pull, used as select */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; //片选
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure PA9 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //采样启动信号CONVEST
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure PA8 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //采样结束标志信号
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* SPI1 configuration */
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //全双工模式
SPI_InitStructure.SPI_Mode = SPI_Mode_Master; //主模式
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; //8位数据
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; //空闲状态为高电平
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge; //数据在第一个边沿采样
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; //NSS为软件控制
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; //波特率预分频为4
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; //高位在前
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);
/* Enable SPI1 */
SPI_Cmd(SPI1, ENABLE); //开启SPI设备
}
/*******************************************************************************
* Function Name :
* Description : Reads a byte from the SPI Flash.
* This function must be used only if the Start_Read_Sequence
* function has been previously called.
* Input : None
* Output : None
* Return : Byte Read from the SPI Flash.
*******************************************************************************/
u8 ADS8328_ReadByte()
{
ADS8328WriteHalfword(Dummy_Byte);
/* Loop while DR register in not emplty */
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); //等待发送缓冲器为空
/* Send byte through the SPI1 peripheral */
SPI_I2S_SendData(SPI1, 0x00); //发送一个数据
// Wait to receive a byte
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); // 此处运行不下去了
SPI_I2S_ClearFlag(SPI1,SPI_I2S_FLAG_RXNE); //直接软件清除标志位。
return SPI_I2S_ReceiveData(SPI1);
//return ();
}
[解决办法]
void SPI2_WRByte(INT8U* pWBuffer, INT16U wNumByte, INT8U* pRBuffer, INT16U rNumByte)
{
INT8U* writeBuffer = pWBuffer;
INT8U* readBuffer = pRBuffer;
if( g_spi2_device.state.init==FALSE || g_spi2_device.state.open==FALSE )
return;
//send datas to device
if( pWBuffer && wNumByte )
{
while(wNumByte)
{
/*!< Loop while DR register in not emplty */
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
/*!< Send byte through the SPI1 peripheral */
SPI_I2S_SendData( SPI2, *(writeBuffer++) );
wNumByte--;
}
}
//read datas from device
if( pRBuffer && rNumByte )
{
while(rNumByte)
{
/*!< Loop while DR register in not emplty */
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_TXE) == RESET);
/*!< Send byte through the SPI1 peripheral */
SPI_I2S_SendData( SPI2, 0 );
/*!< Wait to receive a byte */
while (SPI_I2S_GetFlagStatus(SPI2, SPI_I2S_FLAG_RXNE) == RESET);
/*!< Return the byte read from the SPI bus */
*(readBuffer++) = SPI_I2S_ReceiveData(SPI2);
rNumByte--;
}
}//
}
因为spi是全双工的,在主机发送数据的时候才会产生spi的时钟,所以接收数据时也要写空数据到spi总线上,才能产生读数据的时序。