中级会员
- 积分
- 255
- 金钱
- 255
- 注册时间
- 2014-12-26
- 在线时间
- 117 小时
|
10金钱
本帖最后由 xiong2088 于 2021-9-29 17:15 编辑
我用STM32F103ZE的SPI3连接CH376,怎么弄都初始化不了CH376,请高手指点。
void SPI3_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE );
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOG, ENABLE );
RCC_APB1PeriphClockCmd( RCC_APB1Periph_SPI3, ENABLE );
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); //把和JTAG复用的管脚变成IO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5); //PB3/4/5éÏà-
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //PG11--复位
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOG, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PG9--中断
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOG, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; //PG15--片选
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOG, &GPIO_InitStructure);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI3, &SPI_InitStructure);
SPI_Cmd(SPI3, ENABLE);
SPI3_ReadWriteByte(0xff);
CH376T_CS_HIGH; //片选拉高
CH376T_RST_LOW; //复位拉低 (复位高电平有效)
}
u8 SPI3_ReadWriteByte(u8 TxData)
{
u8 retry=0;
while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_TXE) == RESET)
{
retry++;
if(retry>200)return 0;
}
SPI_I2S_SendData(SPI3, TxData);
retry=0;
while (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE) == RESET)
{
retry++;
if(retry>200)return 0;
}
return SPI_I2S_ReceiveData(SPI3);
}
主函数里初始化SPI3后,调用下面的函数初始化:
u8 mInitCH376Host( void ) /* 初始化CH376 */
{
u8 res;
CH376_PORT_INIT( ); /* 拉高片选 */
xWriteCH376Cmd( CMD11_CHECK_EXIST ); /* 测试单片机与376之间的通讯接口 */
xWriteCH376Data( 0xaa );
res = xReadCH376Data( );
CH376T_CS_HIGH;
// printf("ss:%02x\r\n",res);
xWriteCH376Cmd( CMD11_CHECK_EXIST ); /*测试单片机与376之间的通讯接口 */
xWriteCH376Data( 0x55 );
res = xReadCH376Data( );
CH376T_CS_HIGH;
// printf("ss:%02x\r\n",res);
xWriteCH376Cmd( CMD11_CHECK_EXIST ); /* 测试单片机与376之间的通讯接口 */
xWriteCH376Data( 0x65 );
CH376T_CS_HIGH;
res = xReadCH376Data( );
xEndCH376Cmd( );
if ( res != 0x9A ) return( ERR_USB_UNKNOWN );
CH376_PORT_INIT( );
xWriteCH376Cmd( CMD11_SET_USB_MODE ); /*设定USB模式 */
xWriteCH376Data( 0x05 );
delay_us( 20 );
res = xReadCH376Data( );
xEndCH376Cmd( );
if ( res == CMD_RET_SUCCESS ) return( USB_INT_SUCCESS );
else
{
return( ERR_USB_UNKNOWN ); /*初始化失败 */
}
}
|
|