初级会员
- 积分
- 68
- 金钱
- 68
- 注册时间
- 2019-1-19
- 在线时间
- 13 小时
|
大家好!
最近本人在开发Si4463,开始用STM32f103开发Si4463实现了无线通信,公司为了节省成本,缩小元器件的空间,换STM32f042芯片,但死活不成功。硬件外围电路是一样的,
就主控芯片不一样。下面是我SPI配置代码,请问有错吗?麻烦大家帮看看。
实现收发通信的STM32f103 SPI配置:
#define SPI_CLK_GPIO_PORT GPIOA
#define SPI_CLK_GPIO_CLK RCC_APB2Periph_GPIOA
#define SPI_CLK_GPIO_PIN GPIO_Pin_5
#define SPI_MISO_GPIO_PORT GPIOA
#define SPI_MISO_GPIO_CLK RCC_APB2Periph_GPIOA
#define SPI_MISO_GPIO_PIN GPIO_Pin_6
#define SPI_MOSI_GPIO_PORT GPIOA
#define SPI_MOSI_GPIO_CLK RCC_APB2Periph_GPIOA
#define SPI_MOSI_GPIO_PIN GPIO_Pin_7
#define SPI_NSS_GPIO_PORT GPIOA
#define SPI_NSS_GPIO_CLK RCC_APB2Periph_GPIOA
#define SPI_NSS_GPIO_PIN GPIO_Pin_4
#define spi_set_nss_high( ) SPI_NSS_GPIO_PORT->ODR |= SPI_NSS_GPIO_PIN
#define spi_set_nss_low( ) SPI_NSS_GPIO_PORT->ODR &= (uint32_t)( ~((uint32_t)SPI_NSS_GPIO_PIN ))
#define SPI_PORT SPI1
#define SPI_PORT_CLK RCC_APB2Periph_SPI1
void drv_spi_init( void );
unsigned char drv_spi_read_write_byte( unsigned char TxByte );
#define SPI_WAIT_TIMEOUT ((unsigned short int)0xFFFF)
void drv_spi_init( void )
{
GPIO_InitTypeDef SpiGpioInitStructer;
SPI_InitTypeDef SpiInitStructer;
RCC_APB2PeriphClockCmd( SPI_CLK_GPIO_CLK | SPI_MISO_GPIO_CLK | SPI_MOSI_GPIO_CLK | SPI_NSS_GPIO_CLK, ENABLE );
SpiGpioInitStructer.GPIO_Speed = GPIO_Speed_10MHz;
SpiGpioInitStructer.GPIO_Mode = GPIO_Mode_AF_PP;
SpiGpioInitStructer.GPIO_Pin = SPI_CLK_GPIO_PIN;
GPIO_Init( SPI_CLK_GPIO_PORT, &SpiGpioInitStructer );
SpiGpioInitStructer.GPIO_Pin = SPI_MOSI_GPIO_PIN;
GPIO_Init( SPI_MOSI_GPIO_PORT, &SpiGpioInitStructer );
GPIO_SetBits( SPI_MOSI_GPIO_PORT, SPI_MOSI_GPIO_PIN );
SpiGpioInitStructer.GPIO_Pin = SPI_MISO_GPIO_PIN;
GPIO_Init( SPI_MISO_GPIO_PORT, &SpiGpioInitStructer );
GPIO_SetBits( SPI_MISO_GPIO_PORT, SPI_MISO_GPIO_PIN );
SpiGpioInitStructer.GPIO_Mode = GPIO_Mode_Out_PP;
SpiGpioInitStructer.GPIO_Pin = SPI_NSS_GPIO_PIN;
GPIO_Init( SPI_NSS_GPIO_PORT, &SpiGpioInitStructer );
GPIO_SetBits( SPI_NSS_GPIO_PORT, SPI_NSS_GPIO_PIN );
SPI_I2S_DeInit( SPI_PORT );
if( SPI1 == SPI_PORT )
{
RCC_APB2PeriphClockCmd( SPI_PORT_CLK, ENABLE );
}
else
{
RCC_APB1PeriphClockCmd( SPI_PORT_CLK, ENABLE );
}
SPI_Cmd( SPI_PORT, DISABLE );
SpiInitStructer.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SpiInitStructer.SPI_Mode = SPI_Mode_Master;
SpiInitStructer.SPI_CPOL = SPI_CPOL_Low;
SpiInitStructer.SPI_CPHA = SPI_CPHA_1Edge;
SpiInitStructer.SPI_DataSize = SPI_DataSize_8b;
SpiInitStructer.SPI_NSS = SPI_NSS_Soft;
SpiInitStructer.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32;
SpiInitStructer.SPI_FirstBit = SPI_FirstBit_MSB;
SpiInitStructer.SPI_CRCPolynomial = 7;
SPI_Init( SPI_PORT, &SpiInitStructer );
SPI_Cmd( SPI_PORT, ENABLE );
}
unsigned char drv_spi_read_write_byte( unsigned char TxByte )
{
uint8_t l_Data = 0;
uint16_t l_WaitTime = 0;
while( RESET == SPI_I2S_GetFlagStatus( SPI_PORT, SPI_I2S_FLAG_TXE ) )
{
if( SPI_WAIT_TIMEOUT == ++l_WaitTime )
{
break;
}
}
l_WaitTime = SPI_WAIT_TIMEOUT / 4;
SPI_PORT->DR = TxByte;
while( RESET == SPI_I2S_GetFlagStatus( SPI_PORT, SPI_I2S_FLAG_RXNE ) )
{
if( SPI_WAIT_TIMEOUT == ++l_WaitTime )
{
break;
}
}
l_Data = (uint8_t)SPI_PORT->DR;
return l_Data;
}
void SI446x_Gpio_Init( void )
{
GPIO_InitTypeDef GpioInitStructer;
//打开引脚端口时钟
RCC_APB2PeriphClockCmd( SI4463_SDN_CLK | SI4463_IRQ_CLK | SI4463_GPIO0_CLK | SI4463_GPIO1_CLK | SI4463_GPIO2_CLK | SI4463_GPIO3_CLK, ENABLE );
//SDN 引脚配置为推挽输出
GpioInitStructer.GPIO_Mode = GPIO_Mode_Out_PP;
GpioInitStructer.GPIO_Speed = GPIO_Speed_10MHz;
GpioInitStructer.GPIO_Pin = SI4463_SDN_PIN;
GPIO_Init( SI4463_SDN_PORT, &GpioInitStructer );
GPIO_ResetBits( SI4463_SDN_PORT, SI4463_SDN_PIN ); //SDN 置低 使能设备
//IRQ GPIO0~GPIO3输入 可做外部信号中断输入 Demo程序采用查询方式 未配置成中断
GpioInitStructer.GPIO_Mode = GPIO_Mode_IPU;
GpioInitStructer.GPIO_Pin = SI4463_IRQ_PIN;
GPIO_Init( SI4463_IRQ_PORT, &GpioInitStructer ); //IRQ
}
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
通信s死活不成功的的STM32f042 SPI配置:
#define SPI_CLK_GPIO_PORT GPIOA
#define SPI_CLK_GPIO_CLK RCC_AHBPeriph_GPIOA
#define SPI_CLK_GPIO_PIN GPIO_Pin_5
#define SPI_MISO_GPIO_PORT GPIOA
#define SPI_MISO_GPIO_CLK RCC_AHBPeriph_GPIOA
#define SPI_MISO_GPIO_PIN GPIO_Pin_6
#define SPI_MOSI_GPIO_PORT GPIOA
#define SPI_MOSI_GPIO_CLK RCC_AHBPeriph_GPIOA
#define SPI_MOSI_GPIO_PIN GPIO_Pin_7
#define SPI_NSS_GPIO_PORT GPIOA
#define SPI_NSS_GPIO_CLK RCC_AHBPeriph_GPIOA
#define SPI_NSS_GPIO_PIN GPIO_Pin_4
//#define spi_set_nss_high() SPI_NSS_GPIO_PORT->ODR |= SPI_NSS_GPIO_PIN
//#define spi_set_nss_low() SPI_NSS_GPIO_PORT->ODR &= (uint32_t)( ~((uint32_t)SPI_NSS_GPIO_PIN ))
#define spi_set_nss_high() (SPI_NSS_GPIO_PORT->BSRR = SPI_NSS_GPIO_PIN)
#define spi_set_nss_low() (SPI_NSS_GPIO_PORT->BRR = SPI_NSS_GPIO_PIN)
#define SPI_PORT SPI1
#define SPI_PORT_CLK RCC_APB2Periph_SPI1
void drv_spi_init( void );
unsigned char drv_spi_read_write_byte( unsigned char TxByte );
void SPI_SetSpeed(u8 SPI_BaudRatePrescaler);
#define SPI_WAIT_TIMEOUT ((unsigned short int)0xFFFF)
void drv_spi_init( void )
{
GPIO_InitTypeDef SpiGpioInitStructer;
SPI_InitTypeDef SpiInitStructer;
RCC_AHBPeriphClockCmd( SPI_CLK_GPIO_CLK | SPI_MISO_GPIO_CLK | SPI_MOSI_GPIO_CLK | SPI_NSS_GPIO_CLK, ENABLE );
GPIO_PinAFConfig(GPIOA,GPIO_PinSource5, GPIO_AF_0);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource6, GPIO_AF_0);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource7, GPIO_AF_0);
SPI_StructInit(&SpiInitStructer);
SpiGpioInitStructer.GPIO_Speed = GPIO_Speed_10MHz;
SpiGpioInitStructer.GPIO_Mode = GPIO_Mode_AF;
SpiGpioInitStructer.GPIO_OType = GPIO_OType_PP;
SpiGpioInitStructer.GPIO_PuPd = GPIO_PuPd_UP ;
SpiGpioInitStructer.GPIO_Pin = SPI_CLK_GPIO_PIN;
GPIO_Init(SPI_CLK_GPIO_PORT , &SpiGpioInitStructer);
SpiGpioInitStructer.GPIO_Pin = SPI_MOSI_GPIO_PIN;
GPIO_Init( SPI_MOSI_GPIO_PORT, &SpiGpioInitStructer );
GPIO_SetBits( SPI_MOSI_GPIO_PORT, SPI_MOSI_GPIO_PIN );
SpiGpioInitStructer.GPIO_Pin = SPI_MISO_GPIO_PIN;
GPIO_Init( SPI_MISO_GPIO_PORT, &SpiGpioInitStructer );
GPIO_SetBits( SPI_MISO_GPIO_PORT, SPI_MISO_GPIO_PIN );
SpiGpioInitStructer.GPIO_Pin = SPI_NSS_GPIO_PIN;
SpiGpioInitStructer.GPIO_Mode = GPIO_Mode_OUT;
SpiGpioInitStructer.GPIO_OType= GPIO_OType_OD;
GPIO_Init( SPI_NSS_GPIO_PORT, &SpiGpioInitStructer );
GPIO_SetBits( SPI_NSS_GPIO_PORT, SPI_NSS_GPIO_PIN );
SPI_I2S_DeInit( SPI_PORT );
if( SPI1 == SPI_PORT )
{
RCC_APB2PeriphClockCmd( SPI_PORT_CLK, ENABLE );
}
else
{
RCC_APB1PeriphClockCmd( SPI_PORT_CLK, ENABLE );
}
SPI_Cmd( SPI_PORT, DISABLE );
SpiInitStructer.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SpiInitStructer.SPI_Mode = SPI_Mode_Master;
SpiInitStructer.SPI_CPOL = SPI_CPOL_Low;
SpiInitStructer.SPI_CPHA = SPI_CPHA_1Edge;
SpiInitStructer.SPI_DataSize = SPI_DataSize_8b;
SpiInitStructer.SPI_NSS = SPI_NSS_Soft;
SpiInitStructer.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
SpiInitStructer.SPI_FirstBit = SPI_FirstBit_MSB;
SpiInitStructer.SPI_CRCPolynomial = 7;
SPI_Init( SPI_PORT, &SpiInitStructer );
SPI_RxFIFOThresholdConfig(SPI_PORT, SPI_RxFIFOThreshold_QF);
SPI_Cmd( SPI_PORT, ENABLE );
drv_spi_read_write_byte(0xff);//Æô¶ˉ′«êä
SPI_SetSpeed(SPI_BaudRatePrescaler_16);//éèÖÃÎa18Mê±Öó,¸ßËùÄ£ê½
}
void SI446x_Gpio_Init( void )
{
GPIO_InitTypeDef GpioInitStructer;
//′ò¿aòy½Å¶Ë¿úê±Öó
RCC_AHBPeriphClockCmd( SI4463_SDN_CLK | SI4463_IRQ_CLK , ENABLE );
//SDN òy½ÅÅäÖÃÎaíÆíìêä3ö
GpioInitStructer.GPIO_OType = GPIO_OType_PP;
GpioInitStructer.GPIO_Mode = GPIO_Mode_OUT;
GpioInitStructer.GPIO_Speed = GPIO_Speed_50MHz;
GpioInitStructer.GPIO_Pin = SI4463_SDN_PIN;
GPIO_Init( SI4463_SDN_PORT, &GpioInitStructer );
GPIO_ResetBits( SI4463_SDN_PORT, SI4463_SDN_PIN ); //SDN ÖÆμí ê1Äüé豸
//IRQ BAT êäèë ¿é×öía2¿DÅoÅêäèë Demo3ìDò2éóÃ2éÑˉ·½ê½ Î′ÅäÖÃ3éÖD¶Ï
GpioInitStructer.GPIO_Mode = GPIO_Mode_IN;
GpioInitStructer.GPIO_Pin = SI4463_IRQ_PIN;
GpioInitStructer.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init( SI4463_IRQ_PORT, &GpioInitStructer ); //IRQ
}
//-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
共同部分
unsigned char drv_spi_read_write_byte( unsigned char TxByte )
{
uint8_t l_Data = 0;
uint16_t l_WaitTime = 0;
while(SPI_I2S_GetFlagStatus( SPI_PORT, SPI_I2S_FLAG_TXE )==RESET )
{
if( SPI_WAIT_TIMEOUT == ++l_WaitTime )
{
break;
}
}
l_WaitTime = SPI_WAIT_TIMEOUT / 4;
SPI_PORT->DR = TxByte;
while( RESET == SPI_I2S_GetFlagStatus( SPI_PORT, SPI_I2S_FLAG_RXNE ) )
{
if( SPI_WAIT_TIMEOUT == ++l_WaitTime )
{
break;
}
}
l_Data = (uint8_t)SPI_PORT->DR;
return l_Data;
}
void SI446x_Init( void )
{
SI446x_Gpio_Init( ); //SI4463òy½Å
SI446x_Reset( ); //SI4463¸′λ
SI446x_Power_Up( 30000000 );//reset oóDèòaPower upé豸 ¾§Õñ30MHz
SI446x_Config_Init( ); //SI4463Ä£¿é3õê¼»ˉ
SI446x_Set_Power( 0x7F ); //1|ÂêéèÖÃ
//SI446x_Change_Status( 6 ); //ÇD»»μ½RX×′ì¬
//while( 6 != SI446x_Get_Device_Status( ));
SI446x_Start_Rx( 0, 0, PACKET_LENGTH,0,0,3 );
SI446x_Get_Part_Informatoin();
}
求助!各位大神!谢谢帮忙看看。拉线都是的,就是换了IO口和SPI初始化配置。
|
|