bsp_spi.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. //SPI 1.0V
  2. /*
  3. When you call nrf_drv_spi_transfer(&m_spi_m, tx_buffer, tx_bytes, rx_buffer, rx_bytes), specify value for tx_bytes, it is the number of bytes you want to put on the SPI master-out pin; rx_bytes refers to the number of bytes you want to fill for the rx_buffer, beginning at the start of your transaction.
  4. For example, a register read on a SPI peripheral requires first writing the address of register you want to read, SPI peripheral will return the value of that register as soon as you finish writing the address. In this case tx_bytes is 1, rx_bytes is 2. When the call completes with call back, you will get the read from rx_buffer[1].
  5. In general, if you want to tx x bytes, and receive y bytes after x bytes has been transferred, rx_buffer needs to be the size of (x + y). A valid read back value will start at rx_buffer[x] for the length of y.
  6. 重点:发送X个字节的数据,接受Y个字节的数据,则接收缓冲区至少有X+Y个字节的长度,而接收到的数据从rx_buffer[x]开始,长度为y。
  7. */
  8. /*********************************************************************
  9. * INCLUDES
  10. */
  11. #include "bsp_spi.h"
  12. #include "exception.h"
  13. #include "system.h"
  14. /*Private macro ----------------------------------------------------*/
  15. static void spi0CallbackFunc(nrf_drv_spi_evt_t const *pEvent, void *arg);
  16. static void spi1CallbackFunc(nrf_drv_spi_evt_t const *pEvent, void *arg);
  17. static void spi2CallbackFunc(nrf_drv_spi_evt_t const *pEvent, void *arg);
  18. /*********************************************************************
  19. * LOCAL VARIABLES
  20. */
  21. static volatile bool s0_transferOk = true; // SPI0数据传输完成标志
  22. static volatile bool s1_transferOk = true; // SPI1数据传输完成标志
  23. static volatile bool s2_transferOk = true; // SPI2数据传输完成标志
  24. static const nrf_drv_spi_t s0_spiHandle = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE_0); // SPI0 instance
  25. static const nrf_drv_spi_t s1_spiHandle = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE_1); // SPI1 instance
  26. static const nrf_drv_spi_t s2_spiHandle = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE_2); // SPI2 instance
  27. static const uint8_t s0_cs_list[SPI0_CS_NUMBER] = SPI0_CS_LIST; // SPI0 片选列表
  28. static const uint8_t s1_cs_list[SPI1_CS_NUMBER] = SPI1_CS_LIST; // SPI1 片选列表
  29. static const uint8_t s2_cs_list[SPI2_CS_NUMBER] = SPI2_CS_LIST; // SPI2 片选列表
  30. /*********************************************************************
  31. * LOCAL FUNCTIONS
  32. */
  33. /**
  34. @brief SPI中断处理回调函数
  35. @param 无
  36. @return 无
  37. */
  38. static void spi0CallbackFunc(nrf_drv_spi_evt_t const *pEvent, void *arg)
  39. {
  40. //事件类型只有NRF_DRV_SPI_EVENT_DONE
  41. s0_transferOk = true;
  42. }
  43. static void spi1CallbackFunc(nrf_drv_spi_evt_t const *pEvent, void *arg)
  44. {
  45. //事件类型只有NRF_DRV_SPI_EVENT_DONE
  46. s1_transferOk = true;
  47. }
  48. static void spi2CallbackFunc(nrf_drv_spi_evt_t const *pEvent, void *arg)
  49. {
  50. //事件类型只有NRF_DRV_SPI_EVENT_DONE
  51. s2_transferOk = true;
  52. }
  53. /**
  54. @brief SPI片选引脚检查
  55. @param CsPin_number -[in] 片选引脚
  56. @return 错误代码
  57. */
  58. static uint32_t SPI0_CsCheck(uint32_t CsPin_number)
  59. {
  60. uint32_t i;
  61. for(i = 0; i < SPI0_CS_NUMBER; i++)
  62. {
  63. if(s0_cs_list[i] == CsPin_number)
  64. return SPI_OP_SUCCESS;
  65. }
  66. return SPI_ERROR_CS;
  67. }
  68. static uint32_t SPI1_CsCheck(uint32_t CsPin_number)
  69. {
  70. uint32_t i;
  71. for(i = 0; i < SPI1_CS_NUMBER; i++)
  72. {
  73. if(s1_cs_list[i] == CsPin_number)
  74. return SPI_OP_SUCCESS;
  75. }
  76. return SPI_ERROR_CS;
  77. }
  78. static uint32_t SPI2_CsCheck(uint32_t CsPin_number)
  79. {
  80. uint32_t i;
  81. for(i = 0; i < SPI2_CS_NUMBER; i++)
  82. {
  83. if(s2_cs_list[i] == CsPin_number)
  84. return SPI_OP_SUCCESS;
  85. }
  86. return SPI_ERROR_CS;
  87. }
  88. /**
  89. @brief SPI片选信号设置
  90. @param CsPin_number -[in] 片选引脚
  91. @param pinState -[in] 引脚状态
  92. @return 错误代码
  93. */
  94. static uint32_t SPI0_CsSet(uint32_t CsPin_number , uint8_t pinState)
  95. {
  96. uint32_t err_code;
  97. if((err_code = SPI0_CsCheck(CsPin_number)) != SPI_OP_SUCCESS)return err_code;
  98. if(pinState == BOARD_SPI_CS_OFF)
  99. {
  100. nrf_gpio_pin_write(CsPin_number, 1);
  101. }
  102. else if(pinState == BOARD_SPI_CS_ON)
  103. {
  104. nrf_gpio_pin_write(CsPin_number, 0);
  105. }
  106. return SPI_OP_SUCCESS;
  107. }
  108. static uint32_t SPI1_CsSet(uint32_t CsPin_number , uint8_t pinState)
  109. {
  110. uint32_t err_code;
  111. if((err_code = SPI1_CsCheck(CsPin_number)) != SPI_OP_SUCCESS)return err_code;
  112. if(pinState == BOARD_SPI_CS_OFF)
  113. {
  114. nrf_gpio_pin_write(CsPin_number, 1);
  115. }
  116. else if(pinState == BOARD_SPI_CS_ON)
  117. {
  118. nrf_gpio_pin_write(CsPin_number, 0);
  119. }
  120. return SPI_OP_SUCCESS;
  121. }
  122. static uint32_t SPI2_CsSet(uint32_t CsPin_number , uint8_t pinState)
  123. {
  124. uint32_t err_code;
  125. if((err_code = SPI2_CsCheck(CsPin_number)) != SPI_OP_SUCCESS)return err_code;
  126. if(pinState == BOARD_SPI_CS_OFF)
  127. {
  128. nrf_gpio_pin_write(CsPin_number, 1);
  129. }
  130. else if(pinState == BOARD_SPI_CS_ON)
  131. {
  132. nrf_gpio_pin_write(CsPin_number, 0);
  133. }
  134. return SPI_OP_SUCCESS;
  135. }
  136. /**
  137. @brief SPI读出写入数据
  138. @param CsPin_number -[in] 片选引脚
  139. @param pWriteData -[in] 写入数据
  140. @param writeDataLen -[in] 写入数据长度
  141. @param pReadData -[out] 读出数据
  142. @param readDataLen -[in] 读出数据长度
  143. @return 错误代码
  144. */
  145. static uint32_t SPI0_ReadWriteData(uint32_t CsPin_number, uint8_t *pWriteData, uint8_t writeDataLen, uint8_t *pReadData, uint8_t readDataLen)
  146. {
  147. uint32_t err_code;
  148. uint32_t wait_time_out = WAIT_TIME_VALUE;
  149. if((err_code = SPI0_CsCheck(CsPin_number)) != SPI_OP_SUCCESS)return err_code;
  150. SPI0_CsSet(CsPin_number,BOARD_SPI_CS_ON); //片选信号开
  151. s0_transferOk = false;
  152. err_code = nrf_drv_spi_transfer(&s0_spiHandle, pWriteData, writeDataLen, pReadData, readDataLen);
  153. if(err_code != NRF_SUCCESS)return SPI_ERROR_TIMEOUT;
  154. while(!s0_transferOk)
  155. {
  156. if(wait_time_out--){}
  157. else{
  158. SPI0_CsSet(CsPin_number,BOARD_SPI_CS_OFF); //片选信号关
  159. return SPI_ERROR_TIMEOUT;
  160. }
  161. } // Error in SPI or transfer already in progress.
  162. SPI0_CsSet(CsPin_number,BOARD_SPI_CS_OFF); //片选信号关
  163. return SPI_OP_SUCCESS;
  164. }
  165. static uint32_t SPI1_ReadWriteData(uint32_t CsPin_number, uint8_t *pWriteData, uint8_t writeDataLen, uint8_t *pReadData, uint8_t readDataLen)
  166. {
  167. uint32_t err_code;
  168. uint32_t wait_time_out = WAIT_TIME_VALUE;
  169. if((err_code = SPI1_CsCheck(CsPin_number)) != SPI_OP_SUCCESS)return err_code;
  170. SPI1_CsSet(CsPin_number,BOARD_SPI_CS_ON); //片选信号开
  171. s1_transferOk = false;
  172. err_code = nrf_drv_spi_transfer(&s1_spiHandle, pWriteData, writeDataLen, pReadData, readDataLen);
  173. if(err_code != NRF_SUCCESS)return SPI_ERROR_TIMEOUT;
  174. while(!s1_transferOk)
  175. {
  176. if(wait_time_out--){}
  177. else{
  178. SPI1_CsSet(CsPin_number,BOARD_SPI_CS_OFF); //片选信号关
  179. return SPI_ERROR_TIMEOUT;
  180. }
  181. } // Error in SPI or transfer already in progress.
  182. SPI1_CsSet(CsPin_number,BOARD_SPI_CS_OFF); //片选信号关
  183. return SPI_OP_SUCCESS;
  184. }
  185. static uint32_t SPI2_ReadWriteData(uint32_t CsPin_number, uint8_t *pWriteData, uint8_t writeDataLen, uint8_t *pReadData, uint8_t readDataLen)
  186. {
  187. uint32_t err_code;
  188. uint32_t wait_time_out = WAIT_TIME_VALUE;
  189. if((err_code = SPI2_CsCheck(CsPin_number)) != SPI_OP_SUCCESS)return err_code;
  190. SPI2_CsSet(CsPin_number,BOARD_SPI_CS_ON); //片选信号开
  191. s2_transferOk = false;
  192. err_code = nrf_drv_spi_transfer(&s2_spiHandle, pWriteData, writeDataLen, pReadData, readDataLen);
  193. if(err_code != NRF_SUCCESS)return SPI_ERROR_TIMEOUT;
  194. while(!s2_transferOk)
  195. {
  196. if(wait_time_out--){}
  197. else{
  198. SPI2_CsSet(CsPin_number,BOARD_SPI_CS_OFF); //片选信号关
  199. return SPI_ERROR_TIMEOUT;
  200. }
  201. } // Error in SPI or transfer already in progress.
  202. SPI2_CsSet(CsPin_number,BOARD_SPI_CS_OFF); //片选信号关
  203. return SPI_OP_SUCCESS;
  204. }
  205. /**
  206. @brief SPI读写寄存器数据
  207. @param CsPin_number -[in] 片选引脚
  208. @param reg_addr -[in] 操作的寄存器地址
  209. @param ReadWriteFlag -[in] 读写标志位
  210. @param pData -[in/out] 指向需要操作的数据
  211. @param DataLen -[in] 数据长度
  212. @return 错误代码
  213. */
  214. static uint8_t temp_buff[130];
  215. static uint32_t SPI0_ReadWriteReg(uint32_t CsPin_number, uint8_t reg_addr, uint8_t ReadWriteFlag, uint8_t *pData, uint8_t DataLen)
  216. {
  217. uint32_t err_code;
  218. uint8_t addr;
  219. if((err_code = SPI0_CsCheck(CsPin_number)) != SPI_OP_SUCCESS)return err_code;
  220. if(DataLen > (sizeof(temp_buff)-1))return SPI_ERROR_LENGTH;
  221. if(ReadWriteFlag == SPI_READ)
  222. {
  223. addr = 0x80 | reg_addr;
  224. err_code = SPI0_ReadWriteData(CsPin_number, &addr, 1, temp_buff, DataLen+1); //DataLen+1是因为第一个字节是写地址回应时的垃圾数据
  225. if(err_code != SPI_OP_SUCCESS) return err_code;
  226. memcpy(pData, &temp_buff[1], DataLen);
  227. }
  228. else
  229. {
  230. temp_buff[0] = reg_addr;
  231. memcpy(&temp_buff[1], pData, DataLen);
  232. err_code = SPI0_ReadWriteData(CsPin_number, temp_buff, DataLen+1, NULL, 0);
  233. if(err_code != SPI_OP_SUCCESS) return err_code;
  234. }
  235. return SPI_OP_SUCCESS;
  236. }
  237. static uint32_t SPI1_ReadWriteReg(uint32_t CsPin_number, uint8_t reg_addr, uint8_t ReadWriteFlag, uint8_t *pData, uint8_t DataLen)
  238. {
  239. uint32_t err_code;
  240. uint8_t addr;
  241. if((err_code = SPI1_CsCheck(CsPin_number)) != SPI_OP_SUCCESS)return err_code;
  242. if(DataLen > (sizeof(temp_buff)-1))return SPI_ERROR_LENGTH;
  243. if(ReadWriteFlag == SPI_READ)
  244. {
  245. addr = 0x80 | reg_addr;
  246. err_code = SPI1_ReadWriteData(CsPin_number, &addr, 1, temp_buff, DataLen+1); //DataLen+1是因为第一个字节是写地址回应时的垃圾数据
  247. if(err_code != SPI_OP_SUCCESS) return err_code;
  248. memcpy(pData, &temp_buff[1], DataLen);
  249. }
  250. else
  251. {
  252. temp_buff[0] = reg_addr;
  253. memcpy(&temp_buff[1], pData, DataLen);
  254. err_code = SPI1_ReadWriteData(CsPin_number, temp_buff, DataLen+1, NULL, 0);
  255. if(err_code != SPI_OP_SUCCESS) return err_code;
  256. }
  257. return SPI_OP_SUCCESS;
  258. }
  259. static uint32_t SPI2_ReadWriteReg(uint32_t CsPin_number, uint8_t reg_addr, uint8_t ReadWriteFlag, uint8_t *pData, uint8_t DataLen)
  260. {
  261. uint32_t err_code;
  262. uint8_t addr;
  263. if((err_code = SPI2_CsCheck(CsPin_number)) != SPI_OP_SUCCESS)return err_code;
  264. if(DataLen > (sizeof(temp_buff)-1))return SPI_ERROR_LENGTH;
  265. if(ReadWriteFlag == SPI_READ)
  266. {
  267. addr = 0x80 | reg_addr;
  268. err_code = SPI2_ReadWriteData(CsPin_number, &addr, 1, temp_buff, DataLen+1); //DataLen+1是因为第一个字节是写地址回应时的垃圾数据
  269. if(err_code != SPI_OP_SUCCESS) return err_code;
  270. memcpy(pData, &temp_buff[1], DataLen);
  271. }
  272. else
  273. {
  274. temp_buff[0] = reg_addr;
  275. memcpy(&temp_buff[1], pData, DataLen);
  276. err_code = SPI2_ReadWriteData(CsPin_number, temp_buff, DataLen+1, NULL, 0);
  277. if(err_code != SPI_OP_SUCCESS) return err_code;
  278. }
  279. return SPI_OP_SUCCESS;
  280. }
  281. static void bsp_spi0_init_process(void)
  282. {
  283. if(Except_TxError(EXCEPT_SPI_INIT,"bsp_spi0_init_error\r\n") == 0)
  284. {
  285. Process_Stop(bsp_spi0_init_process);
  286. }
  287. }
  288. static void bsp_spi1_init_process(void)
  289. {
  290. if(Except_TxError(EXCEPT_SPI_INIT,"bsp_spi1_init_error\r\n") == 0)
  291. {
  292. Process_Stop(bsp_spi1_init_process);
  293. }
  294. }
  295. static void bsp_spi2_init_process(void)
  296. {
  297. if(Except_TxError(EXCEPT_SPI_INIT,"bsp_spi2_init_error\r\n") == 0)
  298. {
  299. Process_Stop(bsp_spi2_init_process);
  300. }
  301. }
  302. /*********************************************************************
  303. * PUBLIC FUNCTIONS
  304. */
  305. /****************************************************接口****************************************************/
  306. /**
  307. @brief SPI的初始化函数
  308. @param 无
  309. @return 无
  310. */
  311. void SPI0_Init(void)
  312. {
  313. int ret = 0;
  314. ret_code_t errCode;
  315. nrf_drv_spi_config_t spiConfig = NRF_DRV_SPI_DEFAULT_CONFIG; // 使用SPI默认配置
  316. // 配置SPI端口,注意CSN不要在这设置,另外用GPIO口控制
  317. spiConfig.miso_pin = BOARD_SPI0_MISO_IO;
  318. spiConfig.mosi_pin = BOARD_SPI0_MOSI_IO;
  319. spiConfig.sck_pin = BOARD_SPI0_CLK_IO;
  320. spiConfig.mode = NRF_DRV_SPI_MODE_0;
  321. spiConfig.frequency = SPI0_FREQUENCY;
  322. spiConfig.irq_priority = SPI0_IRQ_PRIORITY; // 在定时器中使用优先级需小于6
  323. errCode = nrf_drv_spi_init(&s0_spiHandle, &spiConfig, spi0CallbackFunc, NULL);
  324. if(errCode != NRF_SUCCESS)ret = -1;
  325. uint32_t i;
  326. for(i = 0; i < SPI0_CS_NUMBER; i++)
  327. {
  328. nrf_gpio_cfg_output(s0_cs_list[i]);
  329. }
  330. if(ret == -1)
  331. {
  332. Process_Start(0,"bsp_spi0_init",bsp_spi0_init_process);
  333. }
  334. }
  335. void SPI1_Init(void)
  336. {
  337. int ret = 0;
  338. ret_code_t errCode;
  339. nrf_drv_spi_config_t spiConfig = NRF_DRV_SPI_DEFAULT_CONFIG; // 使用SPI默认配置
  340. // 配置SPI端口,注意CSN不要在这设置,另外用GPIO口控制
  341. spiConfig.miso_pin = BOARD_SPI1_MISO_IO;
  342. spiConfig.mosi_pin = BOARD_SPI1_MOSI_IO;
  343. spiConfig.sck_pin = BOARD_SPI1_CLK_IO;
  344. spiConfig.mode = NRF_DRV_SPI_MODE_0;
  345. spiConfig.frequency = SPI1_FREQUENCY;
  346. spiConfig.irq_priority = SPI1_IRQ_PRIORITY; // 在定时器中使用优先级需小于6
  347. errCode = nrf_drv_spi_init(&s1_spiHandle, &spiConfig, spi1CallbackFunc, NULL);
  348. if(errCode != NRF_SUCCESS)ret = -1;
  349. uint32_t i;
  350. for(i = 0; i < SPI1_CS_NUMBER; i++)
  351. {
  352. nrf_gpio_cfg_output(s1_cs_list[i]);
  353. }
  354. if(ret == -1)
  355. {
  356. Process_Start(0,"bsp_spi1_init",bsp_spi1_init_process);
  357. }
  358. }
  359. void SPI2_Init(void)
  360. {
  361. int ret = 0;
  362. ret_code_t errCode;
  363. nrf_drv_spi_config_t spiConfig = NRF_DRV_SPI_DEFAULT_CONFIG; // 使用SPI默认配置
  364. // 配置SPI端口,注意CSN不要在这设置,另外用GPIO口控制
  365. spiConfig.miso_pin = BOARD_SPI2_MISO_IO;
  366. spiConfig.mosi_pin = BOARD_SPI2_MOSI_IO;
  367. spiConfig.sck_pin = BOARD_SPI2_CLK_IO;
  368. spiConfig.mode = NRF_DRV_SPI_MODE_0;
  369. spiConfig.frequency = SPI2_FREQUENCY;
  370. spiConfig.irq_priority = SPI2_IRQ_PRIORITY; // 在定时器中使用优先级需小于6
  371. errCode = nrf_drv_spi_init(&s2_spiHandle, &spiConfig, spi2CallbackFunc, NULL);
  372. if(errCode != NRF_SUCCESS)ret = -1;
  373. uint32_t i;
  374. for(i = 0; i < SPI2_CS_NUMBER; i++)
  375. {
  376. nrf_gpio_cfg_output(s2_cs_list[i]);
  377. }
  378. if(ret == -1)
  379. {
  380. Process_Start(0,"bsp_spi2_init",bsp_spi2_init_process);
  381. }
  382. }
  383. /**
  384. @brief SPI只读寄存器数据
  385. @param CsPin_number -[in] 片选引脚
  386. @param reg_addr -[in] 操作的寄存器地址
  387. @param pData -[in/out] 指向需要操作的数据
  388. @param DataLen -[in] 数据长度
  389. @return 错误代码
  390. */
  391. uint32_t SPI0_OnlyReadReg(uint32_t CsPin_number, uint8_t reg_addr, uint8_t *pData, uint8_t DataLen)
  392. {
  393. return SPI0_ReadWriteReg(CsPin_number, reg_addr, SPI_READ, pData, DataLen);
  394. }
  395. uint32_t SPI1_OnlyReadReg(uint32_t CsPin_number, uint8_t reg_addr, uint8_t *pData, uint8_t DataLen)
  396. {
  397. return SPI1_ReadWriteReg(CsPin_number, reg_addr, SPI_READ, pData, DataLen);
  398. }
  399. uint32_t SPI2_OnlyReadReg(uint32_t CsPin_number, uint8_t reg_addr, uint8_t *pData, uint8_t DataLen)
  400. {
  401. return SPI2_ReadWriteReg(CsPin_number, reg_addr, SPI_READ, pData, DataLen);
  402. }
  403. /**
  404. @brief SPI只写寄存器数据
  405. @param CsPin_number -[in] 片选引脚
  406. @param reg_addr -[in] 操作的寄存器地址
  407. @param pData -[in/out] 指向需要操作的数据
  408. @param DataLen -[in] 数据长度
  409. @return 错误代码
  410. */
  411. uint32_t SPI0_OnlyWriteReg(uint32_t CsPin_number, uint8_t reg_addr, uint8_t *pData, uint8_t DataLen)
  412. {
  413. return SPI0_ReadWriteReg(CsPin_number, reg_addr, SPI_WRITE, pData, DataLen);
  414. }
  415. uint32_t SPI1_OnlyWriteReg(uint32_t CsPin_number, uint8_t reg_addr, uint8_t *pData, uint8_t DataLen)
  416. {
  417. return SPI1_ReadWriteReg(CsPin_number, reg_addr, SPI_WRITE, pData, DataLen);
  418. }
  419. uint32_t SPI2_OnlyWriteReg(uint32_t CsPin_number, uint8_t reg_addr, uint8_t *pData, uint8_t DataLen)
  420. {
  421. return SPI2_ReadWriteReg(CsPin_number, reg_addr, SPI_WRITE, pData, DataLen);
  422. }
  423. /**
  424. @brief 开启SPI
  425. @param 无
  426. @return 错误代码
  427. */
  428. uint32_t SPI0_Enable(void)
  429. {
  430. SPI0_Init();
  431. return SPI_OP_SUCCESS;
  432. }
  433. uint32_t SPI1_Enable(void)
  434. {
  435. SPI1_Init();
  436. return SPI_OP_SUCCESS;
  437. }
  438. uint32_t SPI2_Enable(void)
  439. {
  440. SPI2_Init();
  441. return SPI_OP_SUCCESS;
  442. }
  443. /**
  444. @brief 禁用SPI
  445. @param 无
  446. @return 无
  447. */
  448. uint32_t SPI0_Disable(void)
  449. {
  450. uint32_t i;
  451. //所有片选信号关
  452. for(i = 0; i < SPI0_CS_NUMBER; i++)
  453. {
  454. SPI0_CsSet(s0_cs_list[i],BOARD_SPI_CS_OFF);
  455. nrf_gpio_cfg_default(s0_cs_list[i]);
  456. }
  457. nrf_drv_spi_uninit(&s0_spiHandle);
  458. nrf_gpio_cfg_default(BOARD_SPI0_MISO_IO);
  459. nrf_gpio_cfg_default(BOARD_SPI0_MOSI_IO);
  460. nrf_gpio_cfg_default(BOARD_SPI0_CLK_IO);
  461. /*I think you're experiencing this increase from this errata since TWI and SPI shares resources internally,
  462. this errata may also occur when using SPI. Please try this after disabling the SPI:
  463. *(volatile uint32_t *)0x40003FFC = 0;
  464. *(volatile uint32_t *)0x40003FFC;
  465. *(volatile uint32_t *)0x40003FFC = 1;
  466. If SPI1 is used the address is 40004FFC.
  467. */
  468. //下面的一调用,所有SPI都会关闭!!!
  469. // *(volatile uint32_t *)0x40003FFC = 0;
  470. // *(volatile uint32_t *)0x40003FFC;
  471. // *(volatile uint32_t *)0x40003FFC = 1;
  472. return SPI_OP_SUCCESS;
  473. }
  474. uint32_t SPI1_Disable(void)
  475. {
  476. uint32_t i;
  477. //所有片选信号关
  478. for(i = 0; i < SPI1_CS_NUMBER; i++)
  479. {
  480. SPI1_CsSet(s1_cs_list[i],BOARD_SPI_CS_OFF);
  481. nrf_gpio_cfg_default(s1_cs_list[i]);
  482. }
  483. nrf_drv_spi_uninit(&s1_spiHandle);
  484. nrf_gpio_cfg_default(BOARD_SPI1_MISO_IO);
  485. nrf_gpio_cfg_default(BOARD_SPI1_MOSI_IO);
  486. nrf_gpio_cfg_default(BOARD_SPI1_CLK_IO);
  487. /*I think you're experiencing this increase from this errata since TWI and SPI shares resources internally,
  488. this errata may also occur when using SPI. Please try this after disabling the SPI:
  489. *(volatile uint32_t *)0x40003FFC = 0;
  490. *(volatile uint32_t *)0x40003FFC;
  491. *(volatile uint32_t *)0x40003FFC = 1;
  492. If SPI1 is used the address is 40004FFC.
  493. */
  494. //下面的一调用,所有SPI都会关闭!!!
  495. // *(volatile uint32_t *)0x40003FFC = 0;
  496. // *(volatile uint32_t *)0x40003FFC;
  497. // *(volatile uint32_t *)0x40003FFC = 1;
  498. return SPI_OP_SUCCESS;
  499. }
  500. uint32_t SPI2_Disable(void)
  501. {
  502. uint32_t i;
  503. //所有片选信号关
  504. for(i = 0; i < SPI2_CS_NUMBER; i++)
  505. {
  506. SPI2_CsSet(s2_cs_list[i],BOARD_SPI_CS_OFF);
  507. nrf_gpio_cfg_default(s2_cs_list[i]);
  508. }
  509. nrf_drv_spi_uninit(&s2_spiHandle);
  510. nrf_gpio_cfg_default(BOARD_SPI2_MISO_IO);
  511. nrf_gpio_cfg_default(BOARD_SPI2_MOSI_IO);
  512. nrf_gpio_cfg_default(BOARD_SPI2_CLK_IO);
  513. /*I think you're experiencing this increase from this errata since TWI and SPI shares resources internally,
  514. this errata may also occur when using SPI. Please try this after disabling the SPI:
  515. *(volatile uint32_t *)0x40003FFC = 0;
  516. *(volatile uint32_t *)0x40003FFC;
  517. *(volatile uint32_t *)0x40003FFC = 1;
  518. If SPI1 is used the address is 40004FFC.
  519. */
  520. //下面的一调用,所有SPI都会关闭!!!
  521. // *(volatile uint32_t *)0x40003FFC = 0;
  522. // *(volatile uint32_t *)0x40003FFC;
  523. // *(volatile uint32_t *)0x40003FFC = 1;
  524. return SPI_OP_SUCCESS;
  525. }