spi.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include "spi.h"
  2. #include "stm32f4xx_gpio.h"
  3. #include "stm32f4xx_spi.h"
  4. #include "stm32f4xx.h"
  5. #if !defined(SPI_HARD)
  6. void RF_SPI_SCK_H(void)
  7. {
  8. GPIO_WriteBit(RF_SPI_SCK_Port, RF_SPI_SCK_IO, Bit_SET);
  9. }
  10. void RF_SPI_SCK_L(void)
  11. {
  12. GPIO_WriteBit(RF_SPI_SCK_Port, RF_SPI_SCK_IO, Bit_RESET);
  13. }
  14. void RF_SPI_MISO_H(void)
  15. {
  16. GPIO_WriteBit(RF_SPI_MISO_Port, RF_SPI_MISO_IO, Bit_SET);
  17. }
  18. void RF_SPI_MISO_L(void)
  19. {
  20. GPIO_WriteBit(RF_SPI_MISO_Port, RF_SPI_MISO_IO, Bit_RESET);
  21. }
  22. void RF_SPI_MISO_SETMODE(GPIOMode_TypeDef mode)
  23. {
  24. GPIO_InitTypeDef GPIO_InitStructure;
  25. GPIO_InitStructure.GPIO_Pin = RF_SPI_MISO_IO;
  26. GPIO_InitStructure.GPIO_Mode = mode;
  27. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  28. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  29. GPIO_Init(RF_SPI_MISO_Port, &GPIO_InitStructure);
  30. }
  31. uint8_t READ_RF_SPI_MISO(void)
  32. {
  33. return GPIO_ReadInputDataBit(RF_SPI_MISO_Port, RF_SPI_MISO_IO);
  34. }
  35. #endif
  36. void RF_SPI_NSS_H(void)
  37. {
  38. GPIO_WriteBit(RF_SPI_NSS_Port, RF_SPI_NSS_IO, Bit_SET);
  39. }
  40. void RF_SPI_NSS_L(void)
  41. {
  42. GPIO_WriteBit(RF_SPI_NSS_Port, RF_SPI_NSS_IO, Bit_RESET);
  43. }
  44. void spi_init(void)
  45. {
  46. GPIO_InitTypeDef GPIO_InitStructure;
  47. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE); //使能GPIOA时钟
  48. GPIO_InitStructure.GPIO_Pin = RF_SPI_MISO_IO;
  49. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  50. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  51. GPIO_Init(RF_SPI_MISO_Port, &GPIO_InitStructure);
  52. GPIO_InitStructure.GPIO_Pin = RF_SPI_SCK_IO;
  53. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  54. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  55. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  56. GPIO_Init(RF_SPI_SCK_Port, &GPIO_InitStructure);
  57. GPIO_InitStructure.GPIO_Pin = RF_SPI_NSS_IO;
  58. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  59. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  60. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  61. GPIO_Init(RF_SPI_NSS_Port, &GPIO_InitStructure);
  62. //GPIOF9,F10初始化设置
  63. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  64. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//普通输出模式
  65. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
  66. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
  67. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //无上下拉
  68. GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化
  69. GPIO_ResetBits(GPIOB,GPIO_Pin_9);
  70. RF_SPI_NSS_H();
  71. }
  72. void SpiWrite(uint8_t byteToWrite)
  73. {
  74. uint8_t i;
  75. __set_PRIMASK(1); //全局中断关闭
  76. RF_SPI_MISO_SETMODE(GPIO_Mode_OUT);
  77. for(i=0; i<8; i++)
  78. {
  79. RF_SPI_SCK_L();
  80. if(byteToWrite & 0x80)
  81. {
  82. RF_SPI_MISO_H();
  83. }
  84. else
  85. {
  86. RF_SPI_MISO_L();
  87. }
  88. RF_SPI_SCK_H();
  89. byteToWrite <<= 1;
  90. }
  91. RF_SPI_MISO_SETMODE(GPIO_Mode_IN);
  92. RF_SPI_SCK_L();
  93. __set_PRIMASK(0); //全局中断打开
  94. }
  95. uint8_t SpiRead(void)
  96. {
  97. uint8_t i, temp;
  98. temp = 0;
  99. __set_PRIMASK(1); //全局中断关闭
  100. for(i=0; i<8; i++)
  101. {
  102. RF_SPI_SCK_L();
  103. temp <<= 1;
  104. RF_SPI_SCK_H();
  105. if(READ_RF_SPI_MISO())
  106. {
  107. temp ++;
  108. }
  109. }
  110. RF_SPI_SCK_L();
  111. __set_PRIMASK(0); //全局中断打开
  112. return temp;
  113. }