myRadio_gpio.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. #include "myRadio_gpio.h"
  2. #include "stm32f10x.h"
  3. #include "stm32f10x_exti.h"
  4. #include "stm32f10x_it.h"
  5. RADIO_GPIO_CALLBACK gpioCallback;
  6. static irqCallback_ts myIrqCallback_extiLine1;
  7. //---------------------------射频SPI驱动部分---------------------
  8. void BOARD_SPI_NSS_H(void)
  9. {
  10. GPIO_WriteBit(BOARD_GPIO_SPI_CSN, BOARD_PIN_H);
  11. }
  12. void BOARD_SPI_NSS_L(void)
  13. {
  14. GPIO_WriteBit(BOARD_GPIO_SPI_CSN, BOARD_PIN_L);
  15. }
  16. void BOARD_SPI_SCK_H(void)
  17. {
  18. GPIO_WriteBit(BOARD_GPIO_SPI_CLK, BOARD_PIN_H);
  19. }
  20. void BOARD_SPI_SCK_L(void)
  21. {
  22. GPIO_WriteBit(BOARD_GPIO_SPI_CLK, BOARD_PIN_L);
  23. }
  24. void BOARD_SPI_MISO_H(void)
  25. {
  26. GPIO_WriteBit(BOARD_GPIO_SPI_MISO, BOARD_PIN_H);
  27. }
  28. void BOARD_SPI_MISO_L(void)
  29. {
  30. GPIO_WriteBit(BOARD_GPIO_SPI_MISO, BOARD_PIN_L);
  31. }
  32. void BOARD_SPI_MOSI_H(void)
  33. {
  34. GPIO_WriteBit(BOARD_GPIO_SPI_MOSI, BOARD_PIN_H);
  35. }
  36. void BOARD_SPI_MOSI_L(void)
  37. {
  38. GPIO_WriteBit(BOARD_GPIO_SPI_MOSI, BOARD_PIN_L);
  39. }
  40. uint8_t READ_BOARD_SPI_MISO(void)
  41. {
  42. return GPIO_ReadInputDataBit(BOARD_GPIO_SPI_MISO);
  43. }
  44. //---------------------------射频驱动IO部分---------------------
  45. void RF_SI4438_IRQ_H(void)
  46. {
  47. GPIO_WriteBit(RF_SI4438_IRQ, BOARD_PIN_H);
  48. }
  49. void RF_SI4438_IRQ_L(void)
  50. {
  51. GPIO_WriteBit(RF_SI4438_IRQ, BOARD_PIN_L);
  52. }
  53. void RF_SPI_PDN_H(void)
  54. {
  55. GPIO_WriteBit(RF_SPI_PDN, BOARD_PIN_H);
  56. }
  57. void RF_SPI_PDN_L(void)
  58. {
  59. GPIO_WriteBit(RF_SPI_PDN, BOARD_PIN_L);
  60. }
  61. void RF_SI4438_IO0_H(void)
  62. {
  63. GPIO_WriteBit(RF_SI4438_IO0, BOARD_PIN_H);
  64. }
  65. void RF_SI4438_IO0_L(void)
  66. {
  67. GPIO_WriteBit(RF_SI4438_IO0, BOARD_PIN_L);
  68. }
  69. void RF_SI4438_IO1_H(void)
  70. {
  71. GPIO_WriteBit(RF_SI4438_IO1, BOARD_PIN_H);
  72. }
  73. void RF_SI4438_IO1_L(void)
  74. {
  75. GPIO_WriteBit(RF_SI4438_IO1, BOARD_PIN_L);
  76. }
  77. void RF_SI4438_IO2_H(void)
  78. {
  79. GPIO_WriteBit(RF_SI4438_IO2, BOARD_PIN_H);
  80. }
  81. void RF_SI4438_IO2_L(void)
  82. {
  83. GPIO_WriteBit(RF_SI4438_IO2, BOARD_PIN_L);
  84. }
  85. void RF_SI4438_IO3_H(void)
  86. {
  87. GPIO_WriteBit(RF_SI4438_IO3, BOARD_PIN_H);
  88. }
  89. void RF_SI4438_IO3_L(void)
  90. {
  91. GPIO_WriteBit(RF_SI4438_IO3, BOARD_PIN_L);
  92. }
  93. void RF_SI4438_RX_H(void)
  94. {
  95. GPIO_WriteBit(RF_SI4438_RX, BOARD_PIN_H);
  96. }
  97. void RF_SI4438_RX_L(void)
  98. {
  99. GPIO_WriteBit(RF_SI4438_RX, BOARD_PIN_L);
  100. }
  101. void RF_SI4438_TX_H(void)
  102. {
  103. GPIO_WriteBit(RF_SI4438_TX, BOARD_PIN_H);
  104. }
  105. void RF_SI4438_TX_L(void)
  106. {
  107. GPIO_WriteBit(RF_SI4438_TX, BOARD_PIN_L);
  108. }
  109. uint8_t READ_RF_SI4438_IRQ(void)
  110. {
  111. return GPIO_ReadInputDataBit(RF_SI4438_IRQ);
  112. }
  113. void RF_SI4438_RF_MODE(rf_mode_io_te mode)
  114. {
  115. switch (mode)
  116. {
  117. case RF_MODE_IO_RX:
  118. {
  119. RF_SI4438_RX_H();
  120. RF_SI4438_TX_L();
  121. }
  122. break;
  123. case RF_MODE_IO_TX:
  124. {
  125. RF_SI4438_TX_H();
  126. RF_SI4438_RX_L();
  127. }
  128. break;
  129. default:
  130. break;
  131. }
  132. }
  133. /**
  134. * @brief 引脚控制
  135. *
  136. * @param sta =true,设置进入低功耗的引脚状态
  137. * =false,设置退出低功耗的引脚状态
  138. */
  139. void myRadio_gpio_intoLwPwr(bool sta)
  140. {
  141. if (sta == false)
  142. {
  143. }
  144. else
  145. {
  146. }
  147. }
  148. void extiLine1_callback(uint8_t status, uint32_t param)
  149. {
  150. gpioCallback(1);
  151. }
  152. void myRadio_gpio_irq_init()
  153. {
  154. NVIC_InitTypeDef NVIC_InitStructure;
  155. EXTI_InitTypeDef EXTI_InitStructure;
  156. GPIO_InitTypeDef GPIO_InitStructure;
  157. myIrqCallback_extiLine1.thisCb = extiLine1_callback;
  158. EXTILINE1_callbackRegiste(&myIrqCallback_extiLine1);
  159. GPIO_InitStructure.GPIO_Pin = RF_SI4438_IO0_PIN;
  160. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  161. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  162. GPIO_Init(RF_SI4438_IO0_PORT, &GPIO_InitStructure);
  163. EXTI_ClearITPendingBit(EXTI_Line1);
  164. EXTI_InitStructure.EXTI_Line = EXTI_Line1;
  165. EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  166. EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
  167. EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  168. EXTI_Init(&EXTI_InitStructure);
  169. GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource1);
  170. /* Enable and set EXTI1 Interrupt */
  171. NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
  172. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x01;
  173. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
  174. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  175. NVIC_Init(&NVIC_InitStructure);
  176. }
  177. void myRadio_gpio_init(RADIO_GPIO_CALLBACK cb)
  178. {
  179. GPIO_InitTypeDef GPIO_InitStructure;
  180. SPI_InitTypeDef SPI_InitStructure;
  181. #if defined(SPI_HARD)
  182. //----------SPI1时钟使能
  183. RCC_APB2PeriphClockCmd( RCC_APB2Periph_SPI1, ENABLE );
  184. GPIO_InitStructure.GPIO_Pin = BOARD_PIN_SPI_CLK | BOARD_PIN_SPI_MISO | BOARD_PIN_SPI_MOSI;
  185. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
  186. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  187. GPIO_Init(BOARD_PORT_SPI_CLK, &GPIO_InitStructure);
  188. GPIO_SetBits(BOARD_GPIO_SPI_CLK | BOARD_PIN_SPI_MISO | BOARD_PIN_SPI_MOSI);
  189. /*!< SPI configuration */
  190. SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  191. SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  192. SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  193. SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  194. SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
  195. SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  196. SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
  197. SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  198. SPI_InitStructure.SPI_CRCPolynomial = 7;
  199. SPI_Init(SPI1, &SPI_InitStructure);
  200. /*!< Enable the SPI1 */
  201. SPI_Cmd(SPI1, ENABLE);
  202. #else
  203. GPIO_InitStructure.GPIO_Pin = BOARD_PIN_SPI_MOSI;
  204. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  205. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  206. GPIO_Init(BOARD_PORT_SPI_MOSI, &GPIO_InitStructure);
  207. GPIO_InitStructure.GPIO_Pin = BOARD_PIN_SPI_MISO;
  208. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  209. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  210. GPIO_Init(BOARD_PORT_SPI_MISO, &GPIO_InitStructure);
  211. GPIO_InitStructure.GPIO_Pin = BOARD_PIN_SPI_CLK;
  212. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  213. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  214. GPIO_Init(BOARD_PORT_SPI_CLK, &GPIO_InitStructure);
  215. #endif
  216. GPIO_InitStructure.GPIO_Pin = BOARD_PIN_SPI_CSN;
  217. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  218. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  219. GPIO_Init(BOARD_PORT_SPI_CSN, &GPIO_InitStructure);
  220. GPIO_InitStructure.GPIO_Pin = RF_SI4438_IRQ_PIN;
  221. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  222. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  223. GPIO_Init(RF_SI4438_IRQ_PORT, &GPIO_InitStructure);
  224. GPIO_InitStructure.GPIO_Pin = RF_SPI_PDN_PIN;
  225. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  226. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  227. GPIO_Init(RF_SPI_PDN_PORT, &GPIO_InitStructure);
  228. GPIO_InitStructure.GPIO_Pin = RF_SI4438_IO0_PIN;
  229. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  230. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  231. GPIO_Init(RF_SI4438_IO0_PORT, &GPIO_InitStructure);
  232. GPIO_InitStructure.GPIO_Pin = RF_SI4438_IO1_PIN;
  233. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  234. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  235. GPIO_Init(RF_SI4438_IO1_PORT, &GPIO_InitStructure);
  236. GPIO_InitStructure.GPIO_Pin = RF_SI4438_IO2_PIN;
  237. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  238. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  239. GPIO_Init(RF_SI4438_IO2_PORT, &GPIO_InitStructure);
  240. GPIO_InitStructure.GPIO_Pin = RF_SI4438_IO3_PIN;
  241. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  242. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  243. GPIO_Init(RF_SI4438_IO3_PORT, &GPIO_InitStructure);
  244. GPIO_InitStructure.GPIO_Pin = RF_SI4438_RX_PIN;
  245. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  246. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  247. GPIO_Init(RF_SI4438_RX_PORT, &GPIO_InitStructure);
  248. GPIO_InitStructure.GPIO_Pin = RF_SI4438_TX_PIN;
  249. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  250. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  251. GPIO_Init(RF_SI4438_TX_PORT, &GPIO_InitStructure);
  252. BOARD_SPI_NSS_H();
  253. myRadio_gpio_irq_init();
  254. gpioCallback = cb;
  255. }
  256. uint8_t myRadioSpi_rwByte(uint8_t byteToWrite)
  257. {
  258. uint8_t i, temp;
  259. temp = 0;
  260. #if defined(SPI_HARD)
  261. while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET) //检查指定的SPI标志位设置与否:发送缓存空标志位
  262. {
  263. i++;
  264. if(i > 200)return 0;
  265. }
  266. SPI_I2S_SendData(SPI1, byteToWrite); //通过外设SPIx发送一个数据
  267. i=0;
  268. while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET)//检查指定的SPI标志位设置与否:接受缓存非空标志位
  269. {
  270. i++;
  271. if(i > 200)return 0;
  272. }
  273. /*!< Return the byte read from the SPI bus */
  274. temp = SPI_I2S_ReceiveData(SPI1);
  275. #else
  276. BOARD_SPI_SCK_L();
  277. for(i = 0; i < 8; i ++)
  278. {
  279. if(byteToWrite & 0x80)
  280. {
  281. //SpiSetMosiHigh();
  282. BOARD_SPI_MOSI_H();
  283. }
  284. else
  285. {
  286. //SpiSetMosiLow();
  287. BOARD_SPI_MOSI_L();
  288. }
  289. byteToWrite <<= 1;
  290. //SpiSetSckHigh();
  291. BOARD_SPI_SCK_H();
  292. temp <<= 1;
  293. if(READ_BOARD_SPI_MISO())
  294. {
  295. temp ++;
  296. }
  297. //SpiSetSckLow();
  298. BOARD_SPI_SCK_L();
  299. }
  300. #endif
  301. return temp;
  302. }
  303. void myRadioSpi_wBuffer(uint8_t* pData, uint8_t len)
  304. {
  305. uint8_t i;
  306. for(i = 0; i < len; i++)
  307. {
  308. myRadioSpi_rwByte(*pData);
  309. pData ++;
  310. }
  311. }
  312. void myRadioSpi_rBuffer(uint8_t* pData, uint8_t len)
  313. {
  314. uint8_t i;
  315. for(i = 0; i < len; i++)
  316. {
  317. *pData = myRadioSpi_rwByte(0xFF);
  318. pData ++;
  319. }
  320. }
  321. void myRadioSpi_rwBuffer(uint8_t* pDataR, uint8_t* pDataW, uint8_t len)
  322. {
  323. uint8_t i;
  324. for(i = 0; i < len; i++)
  325. {
  326. *pDataR = myRadioSpi_rwByte(*pDataW);
  327. pDataR ++;
  328. pDataW ++;
  329. }
  330. }