myRadio_gpio.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. uint32_t i, j;
  12. i = time;
  13. while (i --)
  14. {
  15. for ( j = 0; j < 1; j++)
  16. {
  17. ;
  18. }
  19. }
  20. }
  21. //---------------------------射频SPI驱动部分---------------------
  22. void BOARD_A7169_SCS_H(void)
  23. {
  24. GPIO_WriteBit(RF_A7169_SCS, BOARD_PIN_H);
  25. }
  26. void BOARD_A7169_SCS_L(void)
  27. {
  28. GPIO_WriteBit(RF_A7169_SCS, BOARD_PIN_L);
  29. }
  30. void BOARD_A7169_SCK_H(void)
  31. {
  32. GPIO_WriteBit(BOARD_GPIO_SPI_CLK, BOARD_PIN_H);
  33. }
  34. void BOARD_A7169_SCK_L(void)
  35. {
  36. GPIO_WriteBit(BOARD_GPIO_SPI_CLK, BOARD_PIN_L);
  37. }
  38. void SET_A7169_SDIO_IN(void)
  39. {
  40. GPIO_InitTypeDef GPIO_InitStructure;
  41. if (sdioMode == 0)
  42. {
  43. return;
  44. }
  45. sdioMode = 0;
  46. GPIO_InitStructure.GPIO_Pin = RF_A7169_SDIO_PIN;
  47. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  48. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  49. GPIO_Init(RF_A7169_SDIO_PORT, &GPIO_InitStructure);
  50. __NOP();
  51. __NOP();
  52. __NOP();
  53. __NOP();
  54. __NOP();
  55. }
  56. void SET_A7169_SDIO_OUT(void)
  57. {
  58. GPIO_InitTypeDef GPIO_InitStructure;
  59. if (sdioMode == 1)
  60. {
  61. return;
  62. }
  63. sdioMode = 1;
  64. GPIO_InitStructure.GPIO_Pin = RF_A7169_SDIO_PIN;
  65. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  66. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  67. GPIO_Init(RF_A7169_SDIO_PORT, &GPIO_InitStructure);
  68. __NOP();
  69. __NOP();
  70. __NOP();
  71. __NOP();
  72. __NOP();
  73. }
  74. void BOARD_A7169_SDIO_H(void)
  75. {
  76. SET_A7169_SDIO_OUT();
  77. GPIO_WriteBit(RF_A7169_SDIO, BOARD_PIN_H);
  78. }
  79. void BOARD_A7169_SDIO_L(void)
  80. {
  81. SET_A7169_SDIO_OUT();
  82. GPIO_WriteBit(RF_A7169_SDIO, BOARD_PIN_L);
  83. }
  84. uint8_t READ_RF_A7169_SDIO(void)
  85. {
  86. SET_A7169_SDIO_IN();
  87. return GPIO_ReadInputDataBit(RF_A7169_SDIO);
  88. }
  89. //---------------------------射频驱动IO部分---------------------
  90. void SET_A7169_GPIO1_IN(void)
  91. {
  92. GPIO_InitTypeDef GPIO_InitStructure;
  93. GPIO_InitStructure.GPIO_Pin = RF_A7169_GPIO1_PIN;
  94. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  95. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  96. GPIO_Init(RF_A7169_GPIO1_PORT, &GPIO_InitStructure);
  97. __NOP();
  98. __NOP();
  99. __NOP();
  100. __NOP();
  101. __NOP();
  102. }
  103. void SET_A7169_GPIO1_OUT(void)
  104. {
  105. GPIO_InitTypeDef GPIO_InitStructure;
  106. GPIO_InitStructure.GPIO_Pin = RF_A7169_GPIO1_PIN;
  107. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  108. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  109. GPIO_Init(RF_A7169_GPIO1_PORT, &GPIO_InitStructure);
  110. __NOP();
  111. __NOP();
  112. __NOP();
  113. __NOP();
  114. __NOP();
  115. }
  116. void RF_A7169_GPIO1_H(void)
  117. {
  118. GPIO_WriteBit(RF_A7169_GPIO1, BOARD_PIN_H);
  119. }
  120. void RF_A7169_GPIO1_L(void)
  121. {
  122. GPIO_WriteBit(RF_A7169_GPIO1, BOARD_PIN_L);
  123. }
  124. void RF_EXT_PA_RE_H(void)
  125. {
  126. GPIO_WriteBit(RF_EXTPA_RE, BOARD_PIN_H);
  127. }
  128. void RF_EXT_PA_RE_L(void)
  129. {
  130. GPIO_WriteBit(RF_EXTPA_RE, BOARD_PIN_L);
  131. }
  132. void RF_EXT_PA_TE_H(void)
  133. {
  134. GPIO_WriteBit(RF_EXTPA_TE, BOARD_PIN_H);
  135. }
  136. void RF_EXT_PA_TE_L(void)
  137. {
  138. GPIO_WriteBit(RF_EXTPA_TE, BOARD_PIN_L);
  139. }
  140. uint8_t READ_RF_A7169_GPIO1(void)
  141. {
  142. return GPIO_ReadInputDataBit(RF_A7169_GPIO1);
  143. }
  144. /**
  145. * @brief 引脚控制
  146. *
  147. * @param sta =true,设置进入低功耗的引脚状态
  148. * =false,设置退出低功耗的引脚状态
  149. */
  150. void myRadio_gpio_intoLwPwr(bool sta)
  151. {
  152. if (sta == false)
  153. {
  154. }
  155. else
  156. {
  157. }
  158. }
  159. void extiLine2_callback(uint8_t status, uint32_t param)
  160. {
  161. gpioCallback(1);
  162. }
  163. void myRadio_gpio_irq_init()
  164. {
  165. NVIC_InitTypeDef NVIC_InitStructure;
  166. EXTI_InitTypeDef EXTI_InitStructure;
  167. GPIO_InitTypeDef GPIO_InitStructure;
  168. myIrqCallback_extiLine2.thisCb = extiLine2_callback;
  169. EXTILINE2_callbackRegiste(&myIrqCallback_extiLine2);
  170. GPIO_InitStructure.GPIO_Pin = RF_A7169_GPIO1_PIN;
  171. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  172. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  173. GPIO_Init(RF_A7169_GPIO1_PORT, &GPIO_InitStructure);
  174. EXTI_ClearITPendingBit(EXTI_Line2);
  175. EXTI_InitStructure.EXTI_Line = EXTI_Line2;
  176. EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  177. EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  178. EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  179. EXTI_Init(&EXTI_InitStructure);
  180. GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource2);
  181. /* Enable and set EXTI1 Interrupt */
  182. NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn;
  183. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;
  184. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
  185. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  186. NVIC_Init(&NVIC_InitStructure);
  187. }
  188. void myRadio_gpio_init(RADIO_GPIO_CALLBACK cb)
  189. {
  190. GPIO_InitTypeDef GPIO_InitStructure;
  191. GPIO_InitStructure.GPIO_Pin = RF_A7169_SDIO_PIN;
  192. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  193. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  194. GPIO_Init(RF_A7169_SDIO_PORT, &GPIO_InitStructure);
  195. GPIO_InitStructure.GPIO_Pin = RF_A7169_SCK_PIN;
  196. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  197. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  198. GPIO_Init(RF_A7169_SCK_PORT, &GPIO_InitStructure);
  199. GPIO_InitStructure.GPIO_Pin = RF_A7169_SCS_PIN;
  200. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  201. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  202. GPIO_Init(RF_A7169_SCS_PORT, &GPIO_InitStructure);
  203. GPIO_InitStructure.GPIO_Pin = RF_A7169_GPIO1_PIN;
  204. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  205. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  206. GPIO_Init(RF_A7169_GPIO1_PORT, &GPIO_InitStructure);
  207. // GPIO_InitStructure.GPIO_Pin = RF_EXTPA_RE_PIN;
  208. // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  209. // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  210. // GPIO_Init(RF_EXTPA_RE_PORT, &GPIO_InitStructure);
  211. // GPIO_InitStructure.GPIO_Pin = RF_EXTPA_TE_PIN;
  212. // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  213. // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  214. // GPIO_Init(RF_EXTPA_TE_PORT, &GPIO_InitStructure);
  215. BOARD_A7169_SCS_H();
  216. BOARD_A7169_SCK_L();
  217. BOARD_A7169_SDIO_H();
  218. RF_A7169_GPIO1_H();
  219. myRadio_gpio_irq_init();
  220. if(cb)
  221. gpioCallback = cb;
  222. }
  223. /**
  224. * @brief
  225. * 下降沿后,RF芯片会改变SDIO的状态
  226. * 上升沿后才能读取SDIO状态
  227. * @return uint8_t
  228. */
  229. uint8_t myRadioSpi_rByte(void)
  230. {
  231. uint8_t i, temp;
  232. temp = 0;
  233. BOARD_A7169_SCK_L();
  234. for(i = 0; i < 8; i ++)
  235. {
  236. gpio_delayUs(10);
  237. BOARD_A7169_SCK_H();
  238. temp <<= 1;
  239. if(READ_RF_A7169_SDIO())
  240. {
  241. temp ++;
  242. }
  243. gpio_delayUs(10);
  244. BOARD_A7169_SCK_L();
  245. }
  246. return temp;
  247. }
  248. /**
  249. * @brief
  250. * 在上升沿之前需设置好SDIO的状态
  251. * @param byteToWrite
  252. */
  253. void myRadioSpi_wByte(uint8_t byteToWrite)
  254. {
  255. uint8_t i;
  256. BOARD_A7169_SCK_L();
  257. for(i = 0; i < 8; i ++)
  258. {
  259. if(byteToWrite & 0x80)
  260. {
  261. BOARD_A7169_SDIO_H();
  262. }
  263. else
  264. {
  265. BOARD_A7169_SDIO_L();
  266. }
  267. byteToWrite <<= 1;
  268. gpio_delayUs(10);
  269. BOARD_A7169_SCK_H();
  270. gpio_delayUs(10);
  271. BOARD_A7169_SCK_L();
  272. }
  273. }
  274. uint8_t myRadioSpi_rwByte(uint8_t byteToWrite)
  275. {
  276. uint8_t i, temp;
  277. temp = 0;
  278. // BOARD_A7169_SCK_L();
  279. for(i = 0; i < 8; i ++)
  280. {
  281. if(byteToWrite & 0x80)
  282. {
  283. BOARD_A7169_SDIO_H();
  284. }
  285. else
  286. {
  287. BOARD_A7169_SDIO_L();
  288. }
  289. byteToWrite <<= 1;
  290. gpio_delayUs(10);
  291. BOARD_A7169_SCK_H();
  292. temp <<= 1;
  293. if(READ_RF_A7169_SDIO())
  294. {
  295. temp ++;
  296. }
  297. BOARD_A7169_SCK_L();
  298. }
  299. return temp;
  300. }
  301. void myRadioSpi_wBuffer(uint8_t* pData, uint8_t len)
  302. {
  303. uint8_t i;
  304. for(i = 0; i < len; i++)
  305. {
  306. myRadioSpi_rwByte(*pData);
  307. pData ++;
  308. }
  309. }
  310. void myRadioSpi_rBuffer(uint8_t* pData, uint8_t len)
  311. {
  312. uint8_t i;
  313. for(i = 0; i < len; i++)
  314. {
  315. *pData = myRadioSpi_rwByte(0xFF);
  316. pData ++;
  317. }
  318. }
  319. void myRadioSpi_rwBuffer(uint8_t* pDataR, uint8_t* pDataW, uint8_t len)
  320. {
  321. uint8_t i;
  322. for(i = 0; i < len; i++)
  323. {
  324. *pDataR = myRadioSpi_rwByte(*pDataW);
  325. pDataR ++;
  326. pDataW ++;
  327. }
  328. }