.h文件部分代码
#define AFE4490_FLAG_TIMEOUT ((uint32_t)0x1000) //用于延时
/* AFE4490 SPI Interface pins */ //AFE4490连接SPI2,对应PB12~PB15
#define AFE4490_SPI SPI2
#define AFE4490_SPI_CLK RCC_APB1Periph_SPI2
#define AFE4490_SPI_SCLK_PIN GPIO_Pin_13 //PB13
#define AFE4490_SPI_SCLK_GPIO_PORT GPIOB
#define AFE4490_SPI_SCLK_GPIO_CLK RCC_AHB1Periph_GPIOB
#define AFE4490_SPI_SCLK_SOURCE GPIO_PinSource13
#define AFE4490_SPI_SCLK_AF GPIO_AF_SPI2
#define AFE4490_SPI_SOMI_PIN GPIO_Pin_14 //PB14
#define AFE4490_SPI_SOMI_GPIO_PORT GPIOB
#define AFE4490_SPI_SOMI_GPIO_CLK RCC_AHB1Periph_GPIOB
#define AFE4490_SPI_SOMI_SOURCE GPIO_PinSource14
#define AFE4490_SPI_SOMI_AF GPIO_AF_SPI2
#define AFE4490_SPI_SIMO_PIN GPIO_Pin_15 //PB15
#define AFE4490_SPI_SIMO_GPIO_PORT GPIOB
#define AFE4490_SPI_SIMO_GPIO_CLK RCC_AHB1Periph_GPIOB
#define AFE4490_SPI_SIMO_SOURCE GPIO_PinSource15
#define AFE4490_SPI_SIMO_AF GPIO_AF_SPI2
#define AFE4490_SPI_STE_PIN GPIO_Pin_12 //PB12
#define AFE4490_SPI_STE_GPIO_PORT GPIOB
#define AFE4490_SPI_STE_GPIO_CLK RCC_AHB1Periph_GPIOB
.c文件,SPI设置
void AFE4490_Init(void) //SPI设置
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
/* Enable the SPI periph */
RCC_APB1PeriphClockCmd(AFE4490_SPI_CLK,ENABLE);
/* Enable SCLK, SOMI, SIMO ,and STE GPIO clocks */
RCC_AHB1PeriphClockCmd(AFE4490_SPI_SCLK_GPIO_CLK| AFE4490_SPI_SOMI_GPIO_CLK | AFE4490_SPI_SIMO_GPIO_CLK, ENABLE);
RCC_AHB1PeriphClockCmd(AFE4490_SPI_STE_GPIO_CLK, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
/* SPI SCLK pin configuration */
GPIO_InitStructure.GPIO_Pin = AFE4490_SPI_SCLK_PIN;
GPIO_Init(AFE4490_SPI_SCLK_GPIO_PORT, &GPIO_InitStructure);
/* SPI SOMI pin configuration */
GPIO_InitStructure.GPIO_Pin = AFE4490_SPI_SOMI_PIN;
GPIO_Init(AFE4490_SPI_SOMI_GPIO_PORT, &GPIO_InitStructure);
/* SPI SIMO pin configuration */
GPIO_InitStructure.GPIO_Pin = AFE4490_SPI_SIMO_PIN;
GPIO_Init(AFE4490_SPI_SIMO_GPIO_PORT, &GPIO_InitStructure);
GPIO_PinAFConfig(AFE4490_SPI_SCLK_GPIO_PORT, AFE4490_SPI_SCLK_SOURCE, AFE4490_SPI_SCLK_AF);
GPIO_PinAFConfig(AFE4490_SPI_SOMI_GPIO_PORT, AFE4490_SPI_SOMI_SOURCE, AFE4490_SPI_SOMI_AF);
GPIO_PinAFConfig(AFE4490_SPI_SIMO_GPIO_PORT, AFE4490_SPI_SIMO_SOURCE, AFE4490_SPI_SIMO_AF);
/* SPI configuration -------------------------------------------------------*/
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_256;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_Init(AFE4490_SPI, &SPI_InitStructure);
/* Enable SPI2 */
SPI_Cmd(AFE4490_SPI, ENABLE);
GPIO_InitStructure.GPIO_Pin =AFE4490_SPI_STE_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(AFE4490_SPI_STE_GPIO_PORT, &GPIO_InitStructure);
GPIO_SetBits(AFE4490_SPI_STE_GPIO_PORT, AFE4490_SPI_STE_PIN);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB, GPIO_Pin_11);
GPIO_SetBits(GPIOB, GPIO_Pin_11);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_SetBits(GPIOC, GPIO_Pin_5);
/* CPIO clksel reset */
delay(1000);
GPIO_ResetBits(GPIOC, GPIO_Pin_5);
delay(1000);
GPIO_SetBits(GPIOC, GPIO_Pin_5);
}
uint8_t AFE4490_SendByte(uint8_t byte)
{
AFE4490Timeout = AFE4490_FLAG_TIMEOUT;
//等待发送信号寄存器为非空,然后发送一个字节到spi总线上
while (SPI_I2S_GetFlagStatus(AFE4490_SPI, SPI_I2S_FLAG_TXE) == RESET)
{
if((AFE4490Timeout--) == 0) return AFE4490_TIMEOUT_UserCallback();
}
//只管发送即可
SPI_I2S_SendData(AFE4490_SPI, byte);
AFE4490Timeout = AFE4490_FLAG_TIMEOUT;
//等待接收信号寄存器为非空,然后从spi总线上接收一个字节
while (SPI_I2S_GetFlagStatus(AFE4490_SPI, SPI_I2S_FLAG_RXNE) == RESET)
{
if((AFE4490Timeout--) == 0) return AFE4490_TIMEOUT_UserCallback();
}
//需要返回一个字节
return (uint8_t)SPI_I2S_ReceiveData(AFE4490_SPI);
}
|