board_spi.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include "gpio.h"
  2. #include "spi.h"
  3. #include "board_gpio.h"
  4. /**
  5. ******************************************************************************
  6. ** \brief 初始化SPI
  7. **
  8. ** \return 无
  9. ******************************************************************************/
  10. void board_spi_int(void)
  11. {
  12. stc_spi_cfg_t SpiInitStruct;
  13. stc_gpio_cfg_t GpioInitStruct;
  14. DDL_ZERO_STRUCT(GpioInitStruct);
  15. Sysctrl_SetPeripheralGate(SysctrlPeripheralSpi0,TRUE);
  16. Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio,TRUE);
  17. //SPI0引脚配置:主机
  18. GpioInitStruct.enDrv = GpioDrvH;
  19. GpioInitStruct.enDir = GpioDirOut;
  20. Gpio_Init(RADIO_MOSI_PORT, RADIO_MOSI_PIN,&GpioInitStruct);
  21. Gpio_SetAfMode(RADIO_MOSI_PORT, RADIO_MOSI_PIN,GpioAf1); //配置引脚PA7作为SPI0_MOSI
  22. Gpio_Init(RADIO_NSS_PORT, RADIO_NSS_PIN,&GpioInitStruct);
  23. Gpio_SetAfMode(RADIO_NSS_PORT, RADIO_NSS_PIN,GpioAf1); //配置引脚PA04作为SPI0_CS
  24. Gpio_Init(RADIO_SCK_PORT, RADIO_SCK_PIN,&GpioInitStruct);
  25. Gpio_SetAfMode(RADIO_SCK_PORT, RADIO_SCK_PIN,GpioAf1); //配置引脚PA05作为SPI0_SCK
  26. GpioInitStruct.enDir = GpioDirIn;
  27. Gpio_Init(RADIO_MISO_PORT, RADIO_MISO_PIN,&GpioInitStruct);
  28. Gpio_SetAfMode(RADIO_MISO_PORT, RADIO_MISO_PIN,GpioAf1); //配置引脚PA06作为SPI0_MISO
  29. //SPI0模块配置:主机
  30. SpiInitStruct.enSpiMode = SpiMskMaster; //配置位主机模式
  31. SpiInitStruct.enPclkDiv = SpiClkMskDiv4; //波特率:fsys/4
  32. SpiInitStruct.enCPHA = SpiMskCphafirst;//第一边沿采样
  33. SpiInitStruct.enCPOL = SpiMskcpollow; //极性为低
  34. Spi_Init(RADIO_SPI_CHx, &SpiInitStruct);
  35. }
  36. uint8_t SpiInOut(uint8_t data_write)
  37. {
  38. while(Spi_GetStatus(RADIO_SPI_CHx, SpiTxe) == FALSE);//等待发送缓冲器空
  39. Spi_SendData(RADIO_SPI_CHx,data_write);
  40. while(Spi_GetStatus(RADIO_SPI_CHx, SpiRxne) == FALSE);//等待接收缓冲器非空
  41. return Spi_ReceiveData(RADIO_SPI_CHx);
  42. }