myRadio_gpio.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #include "myRadio_gpio.h"
  2. #include "stm32f10x.h"
  3. #include "stm32f10x_exti.h"
  4. RADIO_GPIO_CALLBACK gpioCallback;
  5. int spiMosiMode = 0;
  6. //---------------------------射频SPI驱动部分---------------------
  7. void BOARD_SPI_NSS_H(void)
  8. {
  9. GPIO_WriteBit(BOARD_GPIO_SPI_CSN, BOARD_PIN_H);
  10. }
  11. void BOARD_SPI_NSS_L(void)
  12. {
  13. GPIO_WriteBit(BOARD_GPIO_SPI_CSN, BOARD_PIN_L);
  14. }
  15. void BOARD_SPI_SCK_H(void)
  16. {
  17. GPIO_WriteBit(BOARD_GPIO_SPI_CLK, BOARD_PIN_H);
  18. }
  19. void BOARD_SPI_SCK_L(void)
  20. {
  21. GPIO_WriteBit(BOARD_GPIO_SPI_CLK, BOARD_PIN_L);
  22. }
  23. void BOARD_SPI_MISO_H(void)
  24. {
  25. GPIO_WriteBit(BOARD_GPIO_SPI_MISO, BOARD_PIN_H);
  26. }
  27. void BOARD_SPI_MISO_L(void)
  28. {
  29. GPIO_WriteBit(BOARD_GPIO_SPI_MISO, BOARD_PIN_L);
  30. }
  31. void BOARD_SPI_MOSI_H(void)
  32. {
  33. #ifdef SPI_SOFT_3LINE
  34. int ret;
  35. if (spiMosiMode == 0)
  36. {
  37. GPIO_InitTypeDef GPIO_InitStructure;
  38. GPIO_InitStructure.GPIO_Pin = BOARD_PIN_SPI_MOSI;
  39. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  40. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  41. GPIO_Init(BOARD_PORT_SPI_MOSI, &GPIO_InitStructure);
  42. }
  43. spiMosiMode = 1;
  44. #endif
  45. GPIO_WriteBit(BOARD_GPIO_SPI_MOSI, BOARD_PIN_H);
  46. }
  47. void BOARD_SPI_MOSI_L(void)
  48. {
  49. #ifdef SPI_SOFT_3LINE
  50. int ret;
  51. if (spiMosiMode == 0)
  52. {
  53. GPIO_InitTypeDef GPIO_InitStructure;
  54. GPIO_InitStructure.GPIO_Pin = BOARD_PIN_SPI_MOSI;
  55. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  56. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  57. GPIO_Init(BOARD_PORT_SPI_MOSI, &GPIO_InitStructure);
  58. }
  59. spiMosiMode = 1;
  60. #endif
  61. GPIO_WriteBit(BOARD_GPIO_SPI_MOSI, BOARD_PIN_L);
  62. }
  63. uint8_t READ_BOARD_SPI_MISO(void)
  64. {
  65. #ifndef SPI_SOFT_3LINE
  66. return GPIO_ReadInputDataBit(BOARD_GPIO_SPI_MISO);
  67. #else
  68. int ret;
  69. if (spiMosiMode == 1)
  70. {
  71. GPIO_InitTypeDef GPIO_InitStructure;
  72. GPIO_InitStructure.GPIO_Pin = BOARD_PIN_SPI_MOSI;
  73. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  74. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  75. GPIO_Init(BOARD_PORT_SPI_MOSI, &GPIO_InitStructure);
  76. }
  77. spiMosiMode = 0;
  78. ret = GPIO_ReadInputDataBit(BOARD_GPIO_SPI_MOSI);
  79. return ret;
  80. #endif
  81. }
  82. //---------------------------射频驱动IO部分---------------------
  83. void RF_PAN312x_IRQ_H(void)
  84. {
  85. GPIO_WriteBit(RF_PAN312x_IRQ, BOARD_PIN_H);
  86. }
  87. void RF_PAN312x_IRQ_L(void)
  88. {
  89. GPIO_WriteBit(RF_PAN312x_IRQ, BOARD_PIN_L);
  90. }
  91. void RF_PAN312x_RST_H(void)
  92. {
  93. GPIO_WriteBit(RF_PAN312x_RST, BOARD_PIN_H);
  94. }
  95. void RF_PAN312x_RST_L(void)
  96. {
  97. GPIO_WriteBit(RF_PAN312x_RST, BOARD_PIN_L);
  98. }
  99. void RF_PAN312x_IO0_H(void)
  100. {
  101. GPIO_WriteBit(RF_PAN312x_IO0, BOARD_PIN_H);
  102. }
  103. void RF_PAN312x_IO0_L(void)
  104. {
  105. GPIO_WriteBit(RF_PAN312x_IO0, BOARD_PIN_L);
  106. }
  107. uint8_t READ_RF_PAN312x_IRQ(void)
  108. {
  109. return GPIO_ReadInputDataBit(RF_PAN312x_IRQ);
  110. }
  111. uint8_t READ_RF_PAN312x_IO0(void)
  112. {
  113. return GPIO_ReadInputDataBit(RF_PAN312x_IO0);
  114. }
  115. void EXTI0_IRQHandler(void)
  116. {
  117. if(EXTI_GetITStatus(EXTI_Line0) != RESET)
  118. {
  119. /* Clear the EXTI line 1 pending bit */
  120. EXTI_ClearITPendingBit(EXTI_Line0);
  121. if (READ_RF_PAN312x_IRQ())
  122. {
  123. gpioCallback(1);
  124. }
  125. }
  126. }
  127. // BOARD_GPIOB
  128. void myRadio_gpio_irq_init()
  129. {
  130. NVIC_InitTypeDef NVIC_InitStructure;
  131. EXTI_InitTypeDef EXTI_InitStructure;
  132. GPIO_InitTypeDef GPIO_InitStructure;
  133. GPIO_InitStructure.GPIO_Pin = RF_PAN312x_IRQ_PIN;
  134. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  135. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  136. GPIO_Init(RF_PAN312x_IRQ_PORT, &GPIO_InitStructure);
  137. EXTI_ClearITPendingBit(EXTI_Line0);
  138. EXTI_InitStructure.EXTI_Line = EXTI_Line0;
  139. EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  140. EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
  141. EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  142. EXTI_Init(&EXTI_InitStructure);
  143. GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource0);
  144. /* Enable and set EXTI1 Interrupt */
  145. NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
  146. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;
  147. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
  148. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  149. NVIC_Init(&NVIC_InitStructure);
  150. }
  151. void myRadio_gpio_init(RADIO_GPIO_CALLBACK cb)
  152. {
  153. GPIO_InitTypeDef GPIO_InitStructure;
  154. SPI_InitTypeDef SPI_InitStructure;
  155. #if defined(SPI_HARD)
  156. //----------SPI1时钟使能
  157. RCC_APB2PeriphClockCmd( RCC_APB2Periph_SPI1, ENABLE );
  158. GPIO_InitStructure.GPIO_Pin = BOARD_PIN_SPI_CLK | BOARD_PIN_SPI_MISO | BOARD_PIN_SPI_MOSI;
  159. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
  160. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  161. GPIO_Init(BOARD_PORT_SPI_CLK, &GPIO_InitStructure);
  162. GPIO_SetBits(BOARD_GPIO_SPI_CLK | BOARD_PIN_SPI_MISO | BOARD_PIN_SPI_MOSI);
  163. /*!< SPI configuration */
  164. SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  165. SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  166. SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  167. SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  168. SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
  169. SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  170. SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
  171. SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  172. SPI_InitStructure.SPI_CRCPolynomial = 7;
  173. SPI_Init(SPI1, &SPI_InitStructure);
  174. /*!< Enable the SPI1 */
  175. SPI_Cmd(SPI1, ENABLE);
  176. #else
  177. GPIO_InitStructure.GPIO_Pin = BOARD_PIN_SPI_MOSI;
  178. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  179. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  180. GPIO_Init(BOARD_PORT_SPI_MOSI, &GPIO_InitStructure);
  181. #ifndef SPI_SOFT_3LINE
  182. GPIO_InitStructure.GPIO_Pin = BOARD_PIN_SPI_MISO;
  183. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  184. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  185. GPIO_Init(BOARD_PORT_SPI_MISO, &GPIO_InitStructure);
  186. #endif
  187. GPIO_InitStructure.GPIO_Pin = BOARD_PIN_SPI_CLK;
  188. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  189. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  190. GPIO_Init(BOARD_PORT_SPI_CLK, &GPIO_InitStructure);
  191. BOARD_SPI_SCK_L();
  192. #endif
  193. GPIO_InitStructure.GPIO_Pin = BOARD_PIN_SPI_CSN;
  194. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  195. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  196. GPIO_Init(BOARD_PORT_SPI_CSN, &GPIO_InitStructure);
  197. GPIO_InitStructure.GPIO_Pin = RF_PAN312x_IRQ_PIN;
  198. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  199. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  200. GPIO_Init(RF_PAN312x_IRQ_PORT, &GPIO_InitStructure);
  201. GPIO_InitStructure.GPIO_Pin = RF_PAN312x_RST_PIN;
  202. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  203. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  204. GPIO_Init(RF_PAN312x_RST_PORT, &GPIO_InitStructure);
  205. GPIO_InitStructure.GPIO_Pin = RF_PAN312x_IO0_PIN;
  206. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  207. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  208. GPIO_Init(RF_PAN312x_IO0_PORT, &GPIO_InitStructure);
  209. GPIO_InitStructure.GPIO_Pin = RF_PAN312x_IO1_PIN;
  210. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  211. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  212. GPIO_Init(RF_PAN312x_IO1_PORT, &GPIO_InitStructure);
  213. GPIO_InitStructure.GPIO_Pin = RF_PAN312x_IO2_PIN;
  214. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  215. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  216. GPIO_Init(RF_PAN312x_IO2_PORT, &GPIO_InitStructure);
  217. GPIO_InitStructure.GPIO_Pin = RF_PAN312x_IO9_PIN;
  218. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  219. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  220. GPIO_Init(RF_PAN312x_IO9_PORT, &GPIO_InitStructure);
  221. GPIO_InitStructure.GPIO_Pin = RF_PAN312x_IO10_PIN;
  222. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  223. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  224. GPIO_Init(RF_PAN312x_IO10_PORT, &GPIO_InitStructure);
  225. BOARD_SPI_NSS_H();
  226. RF_PAN312x_RST_H();
  227. myRadio_gpio_irq_init();
  228. gpioCallback = cb;
  229. }
  230. uint8_t myRadioSpi_rwByte(uint8_t byteToWrite)
  231. {
  232. uint16_t i = 0;
  233. uint8_t temp;
  234. temp = 0;
  235. #if defined(SPI_HARD)
  236. while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET) //检查指定的SPI标志位设置与否:发送缓存空标志位
  237. {
  238. i++;
  239. if(i > 2000)return 0;
  240. }
  241. SPI_I2S_SendData(SPI1, byteToWrite); //通过外设SPIx发送一个数据
  242. i=0;
  243. while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET)//检查指定的SPI标志位设置与否:接受缓存非空标志位
  244. {
  245. i++;
  246. if(i > 2000)return 0;
  247. }
  248. /*!< Return the byte read from the SPI bus */
  249. temp = SPI_I2S_ReceiveData(SPI1);
  250. #else
  251. BOARD_SPI_SCK_L();
  252. for(i = 0; i < 8; i ++)
  253. {
  254. if(byteToWrite & 0x80)
  255. {
  256. BOARD_SPI_MOSI_H();
  257. }
  258. else
  259. {
  260. BOARD_SPI_MOSI_L();
  261. }
  262. byteToWrite <<= 1;
  263. BOARD_SPI_SCK_H();
  264. temp <<= 1;
  265. if(READ_BOARD_SPI_MISO())
  266. {
  267. temp ++;
  268. }
  269. BOARD_SPI_SCK_L();
  270. }
  271. #endif
  272. return temp;
  273. }
  274. void myRadioSpi_wBuffer(uint8_t* pData, uint8_t len)
  275. {
  276. uint8_t i;
  277. for(i = 0; i < len; i++)
  278. {
  279. myRadioSpi_rwByte(*pData);
  280. pData ++;
  281. }
  282. }
  283. void myRadioSpi_rBuffer(uint8_t* pData, uint8_t len)
  284. {
  285. uint8_t i;
  286. for(i = 0; i < len; i++)
  287. {
  288. *pData = myRadioSpi_rwByte(0xFF);
  289. pData ++;
  290. }
  291. }