新手入门
- 积分
- 18
- 金钱
- 18
- 注册时间
- 2020-1-7
- 在线时间
- 3 小时
|
1金钱
我用STM32F0xx通过SPI通信连接Si4438.
SPI已经配置好了,发送PART_INFO指令到Si4438,但是返回的数据不对。请各位大神指导下,谢谢。
************Si4438函数****************************
void Si44xx_Wait_Cts(u8 cnt)
{
u8 l_ReadCtsTimes = 0;
SPI_CS_LOW();
while(1)
{
delay_us(2);
SPI_ExchangeByte( READ_CMD_BUFF );
if( SPI_ExchangeByte(0xFF)==0xFF )
{
break;
}else if( cnt<l_ReadCtsTimes++ )
{
break;
}
}
SPI_CS_HIGH();
}
void Si44xx_Write_Cmds( u8 *cmd, u8 len )
{
Si44xx_Wait_Cts(2);
SPI_CS_LOW();
delay_us(2);
while(len--)
{
SPI_ExchangeByte( *cmd );
cmd++;
}
SPI_CS_HIGH();
Si44xx_Wait_Cts(50);
}
void Si44xx_Read_Response( u8 *pRead, u8 Length )
{
SPI_CS_LOW();
delay_us(2);
SPI_ExchangeByte( READ_CMD_BUFF );
while( Length-- )
{
*pRead = SPI_ExchangeByte( 0xFF );
pRead++;
}
SPI_CS_HIGH();
}
uint8_t Si44xx_Get_Device_Status( void )
{
SI44xxCmd[0] = REQUEST_DEVICE_STATE;
SI44xxCmd[1] = 0x00;
SI44xxCmd[2] = 0x00;
Si44xx_Write_Cmds( SI44xxCmd, 1 );
Si44xx_Read_Response( SI44xxCmd, 3 );
return (SI44xxCmd[1]&0x0F);
}
void SI446X_PART_INFO( u8 *buffer )
{
u8 cmd = PART_INFO;
Si44xx_Write_Cmds( &cmd, 1 );
Si44xx_Read_Response( buffer, 8 );
}
**********************************************
**************SPI*****************************
uint8_t SPI_ExchangeByte( uint8_t TxByte )
{
uint8_t l_Data = 0;
uint16_t l_WaitTime = 0;
while( RESET == SPI_I2S_GetFlagStatus( SPI1, SPI_I2S_FLAG_TXE ) ) //等待发送缓冲区为空
{
if( SPI_WAIT_TIMEOUT == ++l_WaitTime )
{
break; //如果等待超时则退出
}
}
l_WaitTime = SPI_WAIT_TIMEOUT / 2; //重新设置接收等待时间(因为SPI的速度很快,正常情况下在发送完成之后会立即收到数据,等待时间不需要过长)
SPI1->DR = TxByte; //发送数据
while( RESET == SPI_I2S_GetFlagStatus( SPI1, SPI_I2S_FLAG_RXNE ) ) //等待接收缓冲区非空
{
if( SPI_WAIT_TIMEOUT == ++l_WaitTime )
{
break; //如果等待超时则退出
}
}
l_Data = (uint8_t)SPI1->DR; //读取接收数据
return l_Data; //返回
}
*******************************************************************
返回来的数据都是0xff
|
|