学前班
最后登录1970-1-1
在线时间 小时
注册时间2017-9-6
|
本帖最后由 lzzy.ha@qq.com 于 2017-9-6 18:00 编辑
我用STM32F407VGT6 SPI接口连接 CC1101芯片,程序卡在了send data, while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)); 无限循环
- #define SPI1_NSS GPIO_Pin_4
- #define SPI1_SCK GPIO_Pin_5
- #define SPI1_MISO GPIO_Pin_6
- #define SPI1_MOSI GPIO_Pin_7
- #define GPIO_SPI1 GPIOA
- #define RCC_GPIO_SPI1 RCC_AHB1Periph_GPIOA
- void SPI1_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- SPI_InitTypeDef SPI_InitStruct;
- // NVIC_InitTypeDef NVIC_InitStruct;
- {// GPIO SPI
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
- GPIO_InitStruct.GPIO_Pin = SPI1_SCK | SPI1_MISO | SPI1_MOSI;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStruct.GPIO_Speed = GPIO_High_Speed;
- GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_Init(GPIO_SPI1, &GPIO_InitStruct);
- // connect SPI1 pins to SPI alternate function
- GPIO_PinAFConfig(GPIO_SPI1, GPIO_PinSource5, GPIO_AF_SPI1);
- GPIO_PinAFConfig(GPIO_SPI1, GPIO_PinSource6, GPIO_AF_SPI1);
- GPIO_PinAFConfig(GPIO_SPI1, GPIO_PinSource7, GPIO_AF_SPI1);
- // CS
- GPIO_InitStruct.GPIO_Pin = SPI1_NSS;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStruct.GPIO_Speed = GPIO_High_Speed;
- GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_Init(GPIO_SPI1, &GPIO_InitStruct);
- SPI1_Unselect();
- }
- {// SPI
- RCC_AHB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
- SPI_InitStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex; // set to full duplex mode, seperate MOSI and MISO lines
- SPI_InitStruct.SPI_Mode = SPI_Mode_Master; // transmit in master mode, NSS pin has to be always high
- SPI_InitStruct.SPI_DataSize = SPI_DataSize_8b; // one packet of data is 8 bits wide
- SPI_InitStruct.SPI_CPOL = SPI_CPOL_Low; // clock is low when idle
- SPI_InitStruct.SPI_CPHA = SPI_CPHA_1Edge; // data sampled at first edge
- SPI_InitStruct.SPI_NSS = SPI_NSS_Soft; // set the NSS management to internal and pull internal NSS high
- SPI_InitStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128; // SPI frequency is APB2 frequency / 4
- SPI_InitStruct.SPI_FirstBit = SPI_FirstBit_MSB;// data is transmitted MSB first
- SPI_InitStruct.SPI_CRCPolynomial = 7;
- SPI_Init(SPI1, &SPI_InitStruct);
- SPI_Cmd(SPI1, ENABLE); // enable SPI1
- }
- }
- void SPI1_Select()
- {
- GPIO_ResetBits(GPIO_SPI1, SPI1_NSS);
- }
- void SPI1_Unselect()
- {
- GPIO_SetBits(GPIO_SPI1, SPI1_NSS);
- }
- uint8_t spi1_send_byte(uint8_t byte)
- {
- while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE)); //卡死
- SPI_I2S_SendData(SPI1, byte);
- while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE));
- return SPI_I2S_ReceiveData(SPI1);
- }
复制代码 测试代码:
- int main(void)
- {
- SystemInit();
- SPI1_Init();
- uint8_t value;
- SPI1_Select();
- spi1_send_byte( CC1101_VERSION| READ_SINGLE );
- value = spi1_send_byte(0xff);
- SPI1_Unselect();
- }
复制代码
大家帮忙看看吧,谢谢!!!
|
|