sx1276_7_8_spi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. // ========================================================
  2. /// @file SPI.c
  3. /// @brief Software simulation SPI performance function
  4. /// @version V1.0
  5. /// @date 2014/05/04
  6. /// @company HR_WT.
  7. /// @website http://www.HR_WT.com
  8. /// @author ISA
  9. /// @mobile
  10. /// @tel
  11. // ========================================================
  12. #include "sx1276_7_8_spi.h"
  13. #include "stm32f0xx.h"
  14. #include "mapp_gpio.h"
  15. #include "main.h"
  16. /**********************************************
  17. 函数功能;CS
  18. 入口参数:
  19. 出口参数:
  20. 作者 :凡达电子,www.finedar.com
  21. QQ :75531552
  22. **********************************************/
  23. void SX1278_SPI_nSEL_H(void)
  24. {
  25. GPIO_SetBits(GPIOA , GPIO_Pin_6);
  26. }
  27. /**********************************************
  28. 函数功能;SX1278 IO驱动
  29. 入口参数:
  30. 出口参数:
  31. 作者 :凡达电子,www.finedar.com
  32. QQ :75531552
  33. **********************************************/
  34. void SX1278_SPI_nSEL_L(void)
  35. {
  36. GPIO_ResetBits(GPIOA , GPIO_Pin_6);
  37. }
  38. /**********************************************
  39. 函数功能;SX1278 IO驱动
  40. 入口参数:
  41. 出口参数:
  42. 作者 :凡达电子,www.finedar.com
  43. QQ :75531552
  44. **********************************************/
  45. void SX1278_SPI_SCK_H(void)
  46. {
  47. GPIO_SetBits(GPIOA , GPIO_Pin_5);
  48. }
  49. /**********************************************
  50. 函数功能;SX1278 IO驱动
  51. 入口参数:
  52. 出口参数:
  53. 作者 :凡达电子,www.finedar.com
  54. QQ :75531552
  55. **********************************************/
  56. void SX1278_SPI_SCK_L(void)
  57. {
  58. GPIO_ResetBits(GPIOA , GPIO_Pin_5);
  59. }
  60. /**********************************************
  61. 函数功能;SX1278 IO驱动
  62. 入口参数:
  63. 出口参数:
  64. 作者 :凡达电子,www.finedar.com
  65. QQ :75531552
  66. **********************************************/
  67. void SX1278_SPI_MOSI_H(void)
  68. {
  69. #ifdef MAIN_433
  70. GPIO_SetBits(GPIOA , GPIO_Pin_0);
  71. #else
  72. GPIO_SetBits(GPIOA , GPIO_Pin_2);
  73. #endif
  74. }
  75. /**********************************************
  76. 函数功能;SX1278 IO驱动
  77. 入口参数:
  78. 出口参数:
  79. 作者 :凡达电子,www.finedar.com
  80. QQ :75531552
  81. **********************************************/
  82. void SX1278_SPI_MOSI_L(void)
  83. {
  84. #ifdef MAIN_433
  85. GPIO_ResetBits(GPIOA , GPIO_Pin_0);
  86. #else
  87. GPIO_ResetBits(GPIOA , GPIO_Pin_2);
  88. #endif
  89. }
  90. /**********************************************
  91. 函数功能;SX1278 IO驱动
  92. 入口参数:
  93. 出口参数:
  94. 作者 :凡达电子,www.finedar.com
  95. QQ :75531552
  96. **********************************************/
  97. unsigned char GetSX1278_SPI_MISO(void)
  98. {
  99. #ifdef MAIN_433
  100. return GPIO_ReadInputDataBit(GPIOA , GPIO_Pin_1);
  101. #else
  102. return GPIO_ReadInputDataBit(GPIOA , GPIO_Pin_3);
  103. #endif
  104. }
  105. /**********************************************
  106. 函数功能;SX1278 IO驱动
  107. 入口参数:
  108. 出口参数:
  109. 作者 :凡达电子,www.finedar.com
  110. QQ :75531552
  111. **********************************************/
  112. unsigned char Get_SX1278_NIRQ(void)
  113. {
  114. return GPIO_ReadInputDataBit(GPIOA , GPIO_Pin_4);
  115. }
  116. /**********************************************
  117. 函数功能;SX1278 IO初始化
  118. 入口参数:
  119. 出口参数:
  120. 作者 :凡达电子,www.finedar.com
  121. QQ :75531552
  122. **********************************************/
  123. #ifdef MAIN_433
  124. void SX1278_Main_IO_Init(void)
  125. {
  126. GPIO_InitTypeDef GPIO_InitStructure;
  127. /* Enable GPIO clocks */
  128. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  129. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
  130. /************PA******************
  131. PA0------------------1276_M0SI
  132. PA1----------------- 1276_MIS0
  133. PA2------------------
  134. PA3------------------
  135. PA4------------------1276_IRQ
  136. PA5------------------1276_CLK
  137. PA6------------------1276_CSN
  138. PA7------------------
  139. PA9------------------
  140. PA10----------------
  141. PA13----------------
  142. PA14----------------
  143. ****************************************/
  144. // 复用推挽输出
  145. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0| GPIO_Pin_5| GPIO_Pin_6;
  146. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  147. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  148. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  149. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  150. GPIO_Init(GPIOA, &GPIO_InitStructure);
  151. //GpioSpiSclCfg(SET);
  152. //GpioMOSICfg(SET);
  153. // PA3->MISO;PA4 PENINQ
  154. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  155. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  156. GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
  157. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  158. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  159. GPIO_Init(GPIOA, &GPIO_InitStructure);
  160. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  161. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  162. GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
  163. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  164. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  165. GPIO_Init(GPIOA, &GPIO_InitStructure);
  166. // PB1->CE
  167. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  168. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  169. GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
  170. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  171. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  172. GPIO_Init(GPIOB, &GPIO_InitStructure);
  173. }
  174. #else
  175. void SX1278_IO_Init(void)
  176. {
  177. uint32_t u32prm = 0;
  178. GPIO_InitTypeDef GPIO_InitStructure;
  179. /* Enable GPIO clocks */
  180. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  181. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
  182. /************PA******************
  183. PA0------------------RTP_SDO
  184. PA1----------------- CTP_RST
  185. PA2------------------1276_M0SI
  186. PA3------------------ 1276_MIS0
  187. PA4------------------1276_IRQ
  188. PA5------------------1276_CLK
  189. PA6------------------1276_CSN
  190. PA7------------------MCU_SCL
  191. PA9------------------MCU_SDA
  192. PA10---------------- TCP_INT 检测触摸按键按下
  193. PA13----------------
  194. PA14----------------
  195. ****************************************/
  196. // 复用推挽输出
  197. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2| GPIO_Pin_5| GPIO_Pin_6;
  198. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  199. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  200. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  201. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  202. GPIO_Init(GPIOA, &GPIO_InitStructure);
  203. GpioSpiSclCfg(SET);
  204. GpioMOSICfg(SET);
  205. // PA3->MISO;PA4 PENINQ
  206. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  207. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  208. GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
  209. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  210. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  211. GPIO_Init(GPIOA, &GPIO_InitStructure);
  212. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  213. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
  214. GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
  215. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  216. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  217. GPIO_Init(GPIOA, &GPIO_InitStructure);
  218. // PB1->CE
  219. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  220. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  221. GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
  222. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  223. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  224. GPIO_Init(GPIOB, &GPIO_InitStructure);
  225. GPIO_SetBits(GPIOB, GPIO_Pin_1);
  226. u32prm = ( (YKQADDER[0] <<24) | (YKQADDER[1] <<16) | (YKQADDER[2] <<8) | YKQADDER[4] ) ;
  227. gtArm.ykq_add = HexToInt(u32prm);
  228. }
  229. #endif
  230. /**********************************************************
  231. **Name: SPICmd8bit
  232. **Function: SPI Write one byte
  233. **Input: WrPara
  234. **Output: none
  235. **note: use for burst mode
  236. **********************************************************/
  237. void SPICmd8bit(unsigned char WrPara)
  238. {
  239. unsigned char bitcnt;
  240. SX1278_SPI_nSEL_L();
  241. SX1278_SPI_SCK_L();
  242. for(bitcnt=8; bitcnt!=0; bitcnt--)
  243. {
  244. SX1278_SPI_SCK_L();
  245. if(WrPara&0x80)
  246. SX1278_SPI_MOSI_H();
  247. else
  248. SX1278_SPI_MOSI_L();
  249. SX1278_SPI_SCK_H();
  250. WrPara <<= 1;
  251. }
  252. SX1278_SPI_SCK_L();
  253. SX1278_SPI_MOSI_H();
  254. }
  255. /**********************************************************
  256. **Name: SPIRead8bit
  257. **Function: SPI Read one byte
  258. **Input: None
  259. **Output: result byte
  260. **Note: use for burst mode
  261. **********************************************************/
  262. unsigned char SPIRead8bit(void)
  263. {
  264. unsigned char RdPara = 0;
  265. unsigned char bitcnt;
  266. SX1278_SPI_nSEL_L();
  267. SX1278_SPI_MOSI_H(); //Read one byte data from FIFO, MOSI hold to High
  268. for(bitcnt=8; bitcnt!=0; bitcnt--)
  269. {
  270. SX1278_SPI_SCK_L();
  271. RdPara <<= 1;
  272. SX1278_SPI_SCK_H();
  273. if(GetSX1278_SPI_MISO())
  274. RdPara |= 0x01;
  275. else
  276. RdPara |= 0x00;
  277. }
  278. SX1278_SPI_SCK_L();
  279. return(RdPara);
  280. }
  281. /**********************************************************
  282. **Name: SPIRead
  283. **Function: SPI Read CMD
  284. **Input: adr -> address for read
  285. **Output: None
  286. **********************************************************/
  287. unsigned char SPIRead(unsigned char adr)
  288. {
  289. unsigned char tmp;
  290. SPICmd8bit(adr); //Send address first
  291. tmp = SPIRead8bit();
  292. SX1278_SPI_nSEL_H();
  293. return(tmp);
  294. }
  295. /**********************************************************
  296. **Name: SPIWrite
  297. **Function: SPI Write CMD
  298. **Input: unsigned char address & unsigned char data
  299. **Output: None
  300. **********************************************************/
  301. void SPIWrite(unsigned char adr, unsigned char WrPara)
  302. {
  303. SX1278_SPI_nSEL_L();
  304. SPICmd8bit(adr|0x80); //写入地址
  305. SPICmd8bit(WrPara);//写入数据
  306. SX1278_SPI_SCK_L();
  307. SX1278_SPI_MOSI_H();
  308. SX1278_SPI_nSEL_H();
  309. }
  310. /**********************************************************
  311. **Name: SPIBurstRead
  312. **Function: SPI burst read mode
  313. **Input: adr-----address for read
  314. ** ptr-----data buffer point for read
  315. ** length--how many bytes for read
  316. **Output: None
  317. **********************************************************/
  318. void SPIBurstRead(unsigned char adr, unsigned char *ptr, unsigned char length)
  319. {
  320. unsigned char i;
  321. if(length<=1) //length must more than one
  322. return;
  323. else
  324. {
  325. SX1278_SPI_SCK_L();
  326. SX1278_SPI_nSEL_L();
  327. SPICmd8bit(adr);
  328. for(i=0;i<length;i++)
  329. ptr[i] = SPIRead8bit();
  330. SX1278_SPI_nSEL_H();
  331. }
  332. }
  333. /**********************************************************
  334. **Name: SPIBurstWrite
  335. **Function: SPI burst write mode
  336. **Input: adr-----address for write
  337. ** ptr-----data buffer point for write
  338. ** length--how many bytes for write
  339. **Output: none
  340. **********************************************************/
  341. void BurstWrite(unsigned char adr, unsigned char *ptr, unsigned char length)
  342. {
  343. unsigned char i;
  344. if(length<=1) //length must more than one
  345. return;
  346. else
  347. {
  348. SX1278_SPI_SCK_L();
  349. SX1278_SPI_nSEL_L();
  350. SPICmd8bit(adr|0x80);
  351. for(i=0;i<length;i++)
  352. SPICmd8bit(ptr[i]);
  353. SX1278_SPI_nSEL_H();
  354. }
  355. }