myRadio_gpio.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. #include "myRadio_gpio.h"
  2. #include "misc.h"
  3. #include "stm32f10x.h"
  4. #include "stm32f10x_exti.h"
  5. #include "stm32f10x_it.h"
  6. RADIO_GPIO_CALLBACK gpioCallback;
  7. static irqCallback_ts myIrqCallback_extiLine2;
  8. uint8_t sdioMode = 255;
  9. void gpio_delayUs(uint32_t time)
  10. {
  11. delay_us(time);
  12. }
  13. void gpio_delayMs(uint32_t time)
  14. {
  15. delay_ms(time);
  16. }
  17. //---------------------------射频SPI驱动部分---------------------
  18. void BOARD_A5133_SCS_H(void)
  19. {
  20. GPIO_WriteBit(RF_A5133_SCS, BOARD_PIN_H);
  21. }
  22. void BOARD_A5133_SCS_L(void)
  23. {
  24. GPIO_WriteBit(RF_A5133_SCS, BOARD_PIN_L);
  25. }
  26. void BOARD_A5133_SCK_H(void)
  27. {
  28. GPIO_WriteBit(BOARD_GPIO_SPI_CLK, BOARD_PIN_H);
  29. }
  30. void BOARD_A5133_SCK_L(void)
  31. {
  32. GPIO_WriteBit(BOARD_GPIO_SPI_CLK, BOARD_PIN_L);
  33. }
  34. void SET_A5133_SDIO_IN(void)
  35. {
  36. GPIO_InitTypeDef GPIO_InitStructure;
  37. if (sdioMode == 0)
  38. {
  39. return;
  40. }
  41. sdioMode = 0;
  42. GPIO_InitStructure.GPIO_Pin = RF_A5133_SDIO_PIN;
  43. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  44. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  45. GPIO_Init(RF_A5133_SDIO_PORT, &GPIO_InitStructure);
  46. __NOP();
  47. __NOP();
  48. __NOP();
  49. __NOP();
  50. __NOP();
  51. }
  52. void SET_A5133_SDIO_OUT(void)
  53. {
  54. GPIO_InitTypeDef GPIO_InitStructure;
  55. if (sdioMode == 1)
  56. {
  57. return;
  58. }
  59. sdioMode = 1;
  60. GPIO_InitStructure.GPIO_Pin = RF_A5133_SDIO_PIN;
  61. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  62. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  63. GPIO_Init(RF_A5133_SDIO_PORT, &GPIO_InitStructure);
  64. __NOP();
  65. __NOP();
  66. __NOP();
  67. __NOP();
  68. __NOP();
  69. }
  70. void BOARD_A5133_SDIO_H(void)
  71. {
  72. SET_A5133_SDIO_OUT();
  73. GPIO_WriteBit(RF_A5133_SDIO, BOARD_PIN_H);
  74. }
  75. void BOARD_A5133_SDIO_L(void)
  76. {
  77. SET_A5133_SDIO_OUT();
  78. GPIO_WriteBit(RF_A5133_SDIO, BOARD_PIN_L);
  79. }
  80. uint8_t READ_RF_A5133_SDIO(void)
  81. {
  82. SET_A5133_SDIO_IN();
  83. return GPIO_ReadInputDataBit(RF_A5133_SDIO);
  84. }
  85. //---------------------------射频驱动IO部分---------------------
  86. void SET_A5133_GPIO1_IN(void)
  87. {
  88. GPIO_InitTypeDef GPIO_InitStructure;
  89. GPIO_InitStructure.GPIO_Pin = RF_A5133_GPIO1_PIN;
  90. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  91. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  92. GPIO_Init(RF_A5133_GPIO1_PORT, &GPIO_InitStructure);
  93. __NOP();
  94. __NOP();
  95. __NOP();
  96. __NOP();
  97. __NOP();
  98. }
  99. void SET_A5133_GPIO1_OUT(void)
  100. {
  101. GPIO_InitTypeDef GPIO_InitStructure;
  102. GPIO_InitStructure.GPIO_Pin = RF_A5133_GPIO1_PIN;
  103. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  104. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  105. GPIO_Init(RF_A5133_GPIO1_PORT, &GPIO_InitStructure);
  106. __NOP();
  107. __NOP();
  108. __NOP();
  109. __NOP();
  110. __NOP();
  111. }
  112. void RF_A5133_GPIO1_H(void)
  113. {
  114. GPIO_WriteBit(RF_A5133_GPIO1, BOARD_PIN_H);
  115. }
  116. void RF_A5133_GPIO1_L(void)
  117. {
  118. GPIO_WriteBit(RF_A5133_GPIO1, BOARD_PIN_L);
  119. }
  120. void SET_A5133_GPIO2_IN(void)
  121. {
  122. GPIO_InitTypeDef GPIO_InitStructure;
  123. GPIO_InitStructure.GPIO_Pin = RF_A5133_GPIO2_PIN;
  124. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  125. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  126. GPIO_Init(RF_A5133_GPIO2_PORT, &GPIO_InitStructure);
  127. __NOP();
  128. __NOP();
  129. __NOP();
  130. __NOP();
  131. __NOP();
  132. }
  133. void SET_A5133_GPIO2_OUT(void)
  134. {
  135. GPIO_InitTypeDef GPIO_InitStructure;
  136. GPIO_InitStructure.GPIO_Pin = RF_A5133_GPIO2_PIN;
  137. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  138. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  139. GPIO_Init(RF_A5133_GPIO2_PORT, &GPIO_InitStructure);
  140. __NOP();
  141. __NOP();
  142. __NOP();
  143. __NOP();
  144. __NOP();
  145. }
  146. void RF_A5133_GPIO2_H(void)
  147. {
  148. GPIO_WriteBit(RF_A5133_GPIO2, BOARD_PIN_H);
  149. }
  150. void RF_A5133_GPIO2_L(void)
  151. {
  152. GPIO_WriteBit(RF_A5133_GPIO2, BOARD_PIN_L);
  153. }
  154. void RF_A5133_CKO_H(void)
  155. {
  156. GPIO_WriteBit(RF_A5133_CKO, BOARD_PIN_H);
  157. }
  158. void RF_A5133_CKO_L(void)
  159. {
  160. GPIO_WriteBit(RF_A5133_CKO, BOARD_PIN_L);
  161. }
  162. void RF_EXT_PA_RE_H(void)
  163. {
  164. GPIO_WriteBit(RF_EXTPA_RE, BOARD_PIN_H);
  165. }
  166. void RF_EXT_PA_RE_L(void)
  167. {
  168. GPIO_WriteBit(RF_EXTPA_RE, BOARD_PIN_L);
  169. }
  170. void RF_EXT_PA_TE_H(void)
  171. {
  172. GPIO_WriteBit(RF_EXTPA_TE, BOARD_PIN_H);
  173. }
  174. void RF_EXT_PA_TE_L(void)
  175. {
  176. GPIO_WriteBit(RF_EXTPA_TE, BOARD_PIN_L);
  177. }
  178. void RF_EXT_PA_CTR_H(void)
  179. {
  180. GPIO_WriteBit(RF_EXTPA_CTR, BOARD_PIN_H);
  181. }
  182. void RF_EXT_PA_CTR_L(void)
  183. {
  184. GPIO_WriteBit(RF_EXTPA_CTR, BOARD_PIN_L);
  185. }
  186. uint8_t READ_RF_A5133_GPIO1(void)
  187. {
  188. return GPIO_ReadInputDataBit(RF_A5133_GPIO1);
  189. }
  190. /**
  191. * @brief 引脚控制
  192. *
  193. * @param sta =true,设置进入低功耗的引脚状态
  194. * =false,设置退出低功耗的引脚状态
  195. */
  196. void myRadio_gpio_intoLwPwr(bool sta)
  197. {
  198. if (sta == false)
  199. {
  200. }
  201. else
  202. {
  203. }
  204. }
  205. void extiLine2_callback(uint8_t status, uint32_t param)
  206. {
  207. if (gpioCallback)
  208. {
  209. gpioCallback(1);
  210. }
  211. }
  212. void myRadio_gpio_irq_init()
  213. {
  214. NVIC_InitTypeDef NVIC_InitStructure;
  215. EXTI_InitTypeDef EXTI_InitStructure;
  216. GPIO_InitTypeDef GPIO_InitStructure;
  217. myIrqCallback_extiLine2.thisCb = extiLine2_callback;
  218. EXTILINE2_callbackRegiste(&myIrqCallback_extiLine2);
  219. GPIO_InitStructure.GPIO_Pin = RF_A5133_GPIO1_PIN;
  220. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  221. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  222. GPIO_Init(RF_A5133_GPIO1_PORT, &GPIO_InitStructure);
  223. EXTI_ClearITPendingBit(EXTI_Line2);
  224. EXTI_InitStructure.EXTI_Line = EXTI_Line2;
  225. EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  226. EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  227. EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  228. EXTI_Init(&EXTI_InitStructure);
  229. GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource2);
  230. /* Enable and set EXTI1 Interrupt */
  231. NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;
  232. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;
  233. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
  234. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  235. NVIC_Init(&NVIC_InitStructure);
  236. }
  237. void myRadio_gpio_init(RADIO_GPIO_CALLBACK cb)
  238. {
  239. GPIO_InitTypeDef GPIO_InitStructure;
  240. SPI_InitTypeDef SPI_InitStructure;
  241. #if defined(SPI_HARD) && defined(SPI_4LINE)
  242. //----------SPI1时钟使能
  243. RCC_APB2PeriphClockCmd( RCC_APB2Periph_SPI1, ENABLE );
  244. GPIO_InitStructure.GPIO_Pin = BOARD_PIN_SPI_CLK | BOARD_PIN_SPI_MISO | BOARD_PIN_SPI_MOSI;
  245. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
  246. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  247. GPIO_Init(BOARD_PORT_SPI_CLK, &GPIO_InitStructure);
  248. GPIO_SetBits(BOARD_GPIO_SPI_CLK | BOARD_PIN_SPI_MISO | BOARD_PIN_SPI_MOSI);
  249. /*!< SPI configuration */
  250. SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  251. SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  252. SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  253. SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  254. SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
  255. SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  256. SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
  257. SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  258. SPI_InitStructure.SPI_CRCPolynomial = 7;
  259. SPI_Init(SPI1, &SPI_InitStructure);
  260. /*!< Enable the SPI1 */
  261. SPI_Cmd(SPI1, ENABLE);
  262. #else
  263. GPIO_InitStructure.GPIO_Pin = RF_A5133_SDIO_PIN;
  264. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  265. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  266. GPIO_Init(RF_A5133_SDIO_PORT, &GPIO_InitStructure);
  267. GPIO_InitStructure.GPIO_Pin = RF_A5133_SCK_PIN;
  268. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  269. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  270. GPIO_Init(RF_A5133_SCK_PORT, &GPIO_InitStructure);
  271. GPIO_InitStructure.GPIO_Pin = RF_A5133_GPIO2_PIN;
  272. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  273. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  274. GPIO_Init(RF_A5133_GPIO2_PORT, &GPIO_InitStructure);
  275. BOARD_A5133_SDIO_H();
  276. #endif
  277. GPIO_InitStructure.GPIO_Pin = RF_A5133_SCS_PIN;
  278. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  279. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  280. GPIO_Init(RF_A5133_SCS_PORT, &GPIO_InitStructure);
  281. GPIO_InitStructure.GPIO_Pin = RF_A5133_GPIO1_PIN;
  282. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  283. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  284. GPIO_Init(RF_A5133_GPIO1_PORT, &GPIO_InitStructure);
  285. GPIO_InitStructure.GPIO_Pin = RF_A5133_CKO_PIN;
  286. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  287. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  288. GPIO_Init(RF_A5133_CKO_PORT, &GPIO_InitStructure);
  289. GPIO_InitStructure.GPIO_Pin = RF_EXTPA_CTR_PIN;
  290. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  291. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  292. GPIO_Init(RF_EXTPA_CTR_PORT, &GPIO_InitStructure);
  293. GPIO_InitStructure.GPIO_Pin = RF_EXTPA_RE_PIN;
  294. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  295. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  296. GPIO_Init(RF_EXTPA_RE_PORT, &GPIO_InitStructure);
  297. GPIO_InitStructure.GPIO_Pin = RF_EXTPA_TE_PIN;
  298. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  299. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  300. GPIO_Init(RF_EXTPA_TE_PORT, &GPIO_InitStructure);
  301. BOARD_A5133_SCS_H();
  302. BOARD_A5133_SCK_L();
  303. RF_A5133_GPIO1_H();
  304. myRadio_gpio_irq_init();
  305. if (cb)
  306. gpioCallback = cb;
  307. }
  308. uint8_t myRadioSpi_rwByte(uint8_t byteToWrite)
  309. {
  310. #if defined(SPI_HARD)
  311. uint8_t i, temp;
  312. temp = 0;
  313. while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET) //检查指定的SPI标志位设置与否:发送缓存空标志位
  314. {
  315. i++;
  316. if(i > 200)return 0;
  317. }
  318. SPI_I2S_SendData(SPI1, byteToWrite); //通过外设SPIx发送一个数据
  319. i=0;
  320. while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET)//检查指定的SPI标志位设置与否:接受缓存非空标志位
  321. {
  322. i++;
  323. if(i > 200)return 0;
  324. }
  325. /*!< Return the byte read from the SPI bus */
  326. temp = SPI_I2S_ReceiveData(SPI1);
  327. #else
  328. // BOARD_SPI_SCK_L();
  329. // for(i = 0; i < 8; i ++)
  330. // {
  331. // if(byteToWrite & 0x80)
  332. // {
  333. // BOARD_SPI_MOSI_H();
  334. // }
  335. // else
  336. // {
  337. // BOARD_SPI_MOSI_L();
  338. // }
  339. // byteToWrite <<= 1;
  340. // BOARD_SPI_SCK_H();
  341. // temp <<= 1;
  342. // if(READ_BOARD_SPI_MISO())
  343. // {
  344. // temp ++;
  345. // }
  346. // BOARD_SPI_SCK_L();
  347. // }
  348. uint8_t i, temp;
  349. temp = 0;
  350. // BOARD_A5133_SCK_L();
  351. for (i = 0; i < 8; i++)
  352. {
  353. if (byteToWrite & 0x80)
  354. {
  355. BOARD_A5133_SDIO_H();
  356. }
  357. else
  358. {
  359. BOARD_A5133_SDIO_L();
  360. }
  361. byteToWrite <<= 1;
  362. gpio_delayUs(10);
  363. BOARD_A5133_SCK_H();
  364. temp <<= 1;
  365. if (READ_RF_A5133_SDIO())
  366. {
  367. temp++;
  368. }
  369. BOARD_A5133_SCK_L();
  370. }
  371. #endif
  372. return temp;
  373. }
  374. /**
  375. * @brief
  376. * 下降沿后,RF芯片会改变SDIO的状态
  377. * 上升沿后才能读取SDIO状态
  378. * @return uint8_t
  379. */
  380. uint8_t myRadioSpi_rByte(void)
  381. {
  382. uint8_t i, temp;
  383. // temp = 0;
  384. // // BOARD_A5133_SCK_L();
  385. // for (i = 0; i < 8; i++)
  386. // {
  387. // gpio_delayUs(10);
  388. // temp <<= 1;
  389. // if (READ_RF_A5133_SDIO())
  390. // {
  391. // temp++;
  392. // }
  393. // BOARD_A5133_SCK_H();
  394. // gpio_delayUs(10);
  395. // BOARD_A5133_SCK_L();
  396. // }
  397. temp = myRadioSpi_rwByte(0x00);
  398. return temp;
  399. }
  400. /**
  401. * @brief
  402. * 在上升沿之前需设置好SDIO的状态
  403. * @param byteToWrite
  404. */
  405. void myRadioSpi_wByte(uint8_t byteToWrite)
  406. {
  407. // uint8_t i;
  408. // BOARD_A5133_SCK_L();
  409. // for (i = 0; i < 8; i++)
  410. // {
  411. // if (byteToWrite & 0x80)
  412. // {
  413. // BOARD_A5133_SDIO_H();
  414. // }
  415. // else
  416. // {
  417. // BOARD_A5133_SDIO_L();
  418. // }
  419. // byteToWrite <<= 1;
  420. // gpio_delayUs(10);
  421. // BOARD_A5133_SCK_H();
  422. // gpio_delayUs(10);
  423. // BOARD_A5133_SCK_L();
  424. // }
  425. myRadioSpi_rwByte(byteToWrite);
  426. }
  427. void myRadioSpi_wBuffer(uint8_t *pData, uint8_t len)
  428. {
  429. uint8_t i;
  430. for (i = 0; i < len; i++)
  431. {
  432. myRadioSpi_rwByte(*pData);
  433. pData++;
  434. }
  435. }
  436. void myRadioSpi_rBuffer(uint8_t *pData, uint8_t len)
  437. {
  438. uint8_t i;
  439. for (i = 0; i < len; i++)
  440. {
  441. *pData = myRadioSpi_rwByte(0xFF);
  442. pData++;
  443. }
  444. }
  445. void myRadioSpi_rwBuffer(uint8_t *pDataR, uint8_t *pDataW, uint8_t len)
  446. {
  447. uint8_t i;
  448. for (i = 0; i < len; i++)
  449. {
  450. *pDataR = myRadioSpi_rwByte(*pDataW);
  451. pDataR++;
  452. pDataW++;
  453. }
  454. }