初级会员

- 积分
- 176
- 金钱
- 176
- 注册时间
- 2016-7-19
- 在线时间
- 26 小时
|
1金钱
先贴初始化代码:
void RfSpiInit(void){
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
/* Enable the SPI periph */
RCC_APB2PeriphClockCmd(SPIx_CLK, ENABLE);
/* Enable SCK, MOSI, MISO and NSS GPIO clocks */
RCC_AHBPeriphClockCmd(SPIx_SCK_GPIO_CLK | SPIx_MISO_GPIO_CLK | SPIx_MOSI_GPIO_CLK, ENABLE);
GPIO_PinAFConfig(SPIx_SCK_GPIO_PORT, SPIx_SCK_SOURCE, SPIx_SCK_AF);
GPIO_PinAFConfig(SPIx_MOSI_GPIO_PORT, SPIx_MOSI_SOURCE, SPIx_MOSI_AF);
GPIO_PinAFConfig(SPIx_MISO_GPIO_PORT, SPIx_MISO_SOURCE, SPIx_MISO_AF);
/* SPI SCK/MOSI/MISO pin configuration */
/* SCK */
GPIO_InitStructure.GPIO_Pin = SPIx_SCK_PIN ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
GPIO_Init(SPIx_SCK_GPIO_PORT, &GPIO_InitStructure);
/* MOSI */
GPIO_InitStructure.GPIO_Pin = SPIx_MOSI_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
GPIO_Init(SPIx_SCK_GPIO_PORT, &GPIO_InitStructure);
/* MISO */
GPIO_InitStructure.GPIO_Pin = SPIx_MISO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
GPIO_Init(SPIx_SCK_GPIO_PORT, &GPIO_InitStructure);
/* NSS/CSN/CS */
GPIO_InitStructure.GPIO_Pin = SPIx_NSS_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_Level_3;
GPIO_Init(SPIx_NSS_GPIO_PORT, &GPIO_InitStructure);
/* 注意: 在SPI_NSS_Soft模式下,SSI位决定了NSS引脚上(PA4)的电平,
而SSM=1时释放了NSS引脚,NSS引脚可以用作GPIO口 */
/* SPI configuration -------------------------------------------------------*/
SPI_I2S_DeInit(SPIx);
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPIx, &SPI_InitStructure);
SPI_RxFIFOThresholdConfig(SPIx, SPI_RxFIFOThreshold_QF);
/* Enable the SPI peripheral */
SPI_Cmd(SPIx, ENABLE);
/* 拉低片选 */
RfSpiDeAssertCSN();
}
/* 写 */
static void RfRegWrite(uint8_t addr, uint8_t val)
{
RfSpiAssertCSN();
while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET);
SPI_SendData8(SPIx, addr);
while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET);
SPI_SendData8(SPIx, val);
while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET);
RfSpiDeAssertCSN();
SPI_ReceiveData8(SPIx);
}
/* 读操作 */
static uint8_t RfRegRead(uint8_t addr)
{
uint8_t u8tmp;
RfSpiAssertCSN();
while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET);
SPI_SendData8(SPIx, addr);
/* 读SPI*/
u8tmp=SPI_ReceiveData8(SPIx);
while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_TXE) == RESET);
/* 给时钟信号 */
SPI_SendData8(SPIx, 0x00);
while (SPI_I2S_GetFlagStatus(SPIx, SPI_I2S_FLAG_RXNE) == RESET);
u8tmp = SPI_ReceiveData8(SPIx);
RfSpiDeAssertCSN();
return u8tmp;
}
问题 :对寄存器写一个数,再读出来验证。每次读数据函数都要执行2次才能读到正确的值。
SPI MOSI和MISO波形如下
使用以上读操作,首次执行read都会读到0x0e,第二次执行读操作才会读到0x02。第一次调试32硬件SPI求大神解答一下
|
|