#include "gpio.h" #include "spi.h" #include "board_gpio.h" /** ****************************************************************************** ** \brief 初始化SPI ** ** \return 无 ******************************************************************************/ void board_spi_int(void) { stc_spi_cfg_t SpiInitStruct; stc_gpio_cfg_t GpioInitStruct; DDL_ZERO_STRUCT(GpioInitStruct); Sysctrl_SetPeripheralGate(SysctrlPeripheralSpi0,TRUE); Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio,TRUE); //SPI0引脚配置:主机 GpioInitStruct.enDrv = GpioDrvH; GpioInitStruct.enDir = GpioDirOut; Gpio_Init(RADIO_MOSI_PORT, RADIO_MOSI_PIN,&GpioInitStruct); Gpio_SetAfMode(RADIO_MOSI_PORT, RADIO_MOSI_PIN,GpioAf1); //配置引脚PA7作为SPI0_MOSI Gpio_Init(RADIO_NSS_PORT, RADIO_NSS_PIN,&GpioInitStruct); Gpio_SetAfMode(RADIO_NSS_PORT, RADIO_NSS_PIN,GpioAf1); //配置引脚PA04作为SPI0_CS Gpio_Init(RADIO_SCK_PORT, RADIO_SCK_PIN,&GpioInitStruct); Gpio_SetAfMode(RADIO_SCK_PORT, RADIO_SCK_PIN,GpioAf1); //配置引脚PA05作为SPI0_SCK GpioInitStruct.enDir = GpioDirIn; Gpio_Init(RADIO_MISO_PORT, RADIO_MISO_PIN,&GpioInitStruct); Gpio_SetAfMode(RADIO_MISO_PORT, RADIO_MISO_PIN,GpioAf1); //配置引脚PA06作为SPI0_MISO //SPI0模块配置:主机 SpiInitStruct.enSpiMode = SpiMskMaster; //配置位主机模式 SpiInitStruct.enPclkDiv = SpiClkMskDiv4; //波特率:fsys/4 SpiInitStruct.enCPHA = SpiMskCphafirst;//第一边沿采样 SpiInitStruct.enCPOL = SpiMskcpollow; //极性为低 Spi_Init(RADIO_SPI_CHx, &SpiInitStruct); } uint8_t SpiInOut(uint8_t data_write) { while(Spi_GetStatus(RADIO_SPI_CHx, SpiTxe) == FALSE);//等待发送缓冲器空 Spi_SendData(RADIO_SPI_CHx,data_write); while(Spi_GetStatus(RADIO_SPI_CHx, SpiRxne) == FALSE);//等待接收缓冲器非空 return Spi_ReceiveData(RADIO_SPI_CHx); }