bsp_spi.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641
  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. static void spi0CallbackFunc(nrf_drv_spi_evt_t const *pEvent, void *arg);
  13. static void spi1CallbackFunc(nrf_drv_spi_evt_t const *pEvent, void *arg);
  14. static void spi2CallbackFunc(nrf_drv_spi_evt_t const *pEvent, void *arg);
  15. /*********************************************************************
  16. * LOCAL VARIABLES
  17. */
  18. static volatile bool s0_transferOk = true; // SPI0数据传输完成标志
  19. static volatile bool s1_transferOk = true; // SPI1数据传输完成标志
  20. static volatile bool s2_transferOk = true; // SPI2数据传输完成标志
  21. static const nrf_drv_spi_t s0_spiHandle = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE_0); // SPI0 instance
  22. static const nrf_drv_spi_t s1_spiHandle = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE_1); // SPI1 instance
  23. static const nrf_drv_spi_t s2_spiHandle = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE_2); // SPI2 instance
  24. static const uint8_t s0_cs_list[SPI0_CS_NUMBER] = SPI0_CS_LIST; // SPI0 片选列表
  25. static const uint8_t s1_cs_list[SPI1_CS_NUMBER] = SPI1_CS_LIST; // SPI1 片选列表
  26. static const uint8_t s2_cs_list[SPI2_CS_NUMBER] = SPI2_CS_LIST; // SPI2 片选列表
  27. /*********************************************************************
  28. * LOCAL FUNCTIONS
  29. */
  30. /**
  31. @brief SPI中断处理回调函数
  32. @param 无
  33. @return 无
  34. */
  35. static void spi0CallbackFunc(nrf_drv_spi_evt_t const *pEvent, void *arg)
  36. {
  37. //事件类型只有NRF_DRV_SPI_EVENT_DONE
  38. s0_transferOk = true;
  39. }
  40. static void spi1CallbackFunc(nrf_drv_spi_evt_t const *pEvent, void *arg)
  41. {
  42. //事件类型只有NRF_DRV_SPI_EVENT_DONE
  43. s1_transferOk = true;
  44. }
  45. static void spi2CallbackFunc(nrf_drv_spi_evt_t const *pEvent, void *arg)
  46. {
  47. //事件类型只有NRF_DRV_SPI_EVENT_DONE
  48. s2_transferOk = true;
  49. }
  50. /**
  51. @brief SPI片选引脚检查
  52. @param CsPin_number -[in] 片选引脚
  53. @return 错误代码
  54. */
  55. static uint32_t SPI0_CsCheck(uint32_t CsPin_number)
  56. {
  57. uint32_t i;
  58. for(i = 0; i < SPI0_CS_NUMBER; i++)
  59. {
  60. if(s0_cs_list[i] == CsPin_number)
  61. return SPI_OP_SUCCESS;
  62. }
  63. return SPI_ERROR_CS;
  64. }
  65. static uint32_t SPI1_CsCheck(uint32_t CsPin_number)
  66. {
  67. uint32_t i;
  68. for(i = 0; i < SPI1_CS_NUMBER; i++)
  69. {
  70. if(s1_cs_list[i] == CsPin_number)
  71. return SPI_OP_SUCCESS;
  72. }
  73. return SPI_ERROR_CS;
  74. }
  75. static uint32_t SPI2_CsCheck(uint32_t CsPin_number)
  76. {
  77. uint32_t i;
  78. for(i = 0; i < SPI2_CS_NUMBER; i++)
  79. {
  80. if(s2_cs_list[i] == CsPin_number)
  81. return SPI_OP_SUCCESS;
  82. }
  83. return SPI_ERROR_CS;
  84. }
  85. /**
  86. @brief SPI片选信号设置
  87. @param CsPin_number -[in] 片选引脚
  88. @param pinState -[in] 引脚状态
  89. @return 错误代码
  90. */
  91. static uint32_t SPI0_CsSet(uint32_t CsPin_number , uint8_t pinState)
  92. {
  93. uint32_t err_code;
  94. if((err_code = SPI0_CsCheck(CsPin_number)) != SPI_OP_SUCCESS)return err_code;
  95. if(pinState == BOARD_SPI_CS_OFF)
  96. {
  97. nrf_gpio_pin_write(CsPin_number, 1);
  98. }
  99. else if(pinState == BOARD_SPI_CS_ON)
  100. {
  101. nrf_gpio_pin_write(CsPin_number, 0);
  102. }
  103. return SPI_OP_SUCCESS;
  104. }
  105. static uint32_t SPI1_CsSet(uint32_t CsPin_number , uint8_t pinState)
  106. {
  107. uint32_t err_code;
  108. if((err_code = SPI1_CsCheck(CsPin_number)) != SPI_OP_SUCCESS)return err_code;
  109. if(pinState == BOARD_SPI_CS_OFF)
  110. {
  111. nrf_gpio_pin_write(CsPin_number, 1);
  112. }
  113. else if(pinState == BOARD_SPI_CS_ON)
  114. {
  115. nrf_gpio_pin_write(CsPin_number, 0);
  116. }
  117. return SPI_OP_SUCCESS;
  118. }
  119. static uint32_t SPI2_CsSet(uint32_t CsPin_number , uint8_t pinState)
  120. {
  121. uint32_t err_code;
  122. if((err_code = SPI2_CsCheck(CsPin_number)) != SPI_OP_SUCCESS)return err_code;
  123. if(pinState == BOARD_SPI_CS_OFF)
  124. {
  125. nrf_gpio_pin_write(CsPin_number, 1);
  126. }
  127. else if(pinState == BOARD_SPI_CS_ON)
  128. {
  129. nrf_gpio_pin_write(CsPin_number, 0);
  130. }
  131. return SPI_OP_SUCCESS;
  132. }
  133. /**
  134. @brief SPI读出写入数据
  135. @param CsPin_number -[in] 片选引脚
  136. @param pWriteData -[in] 写入数据
  137. @param writeDataLen -[in] 写入数据长度
  138. @param pReadData -[out] 读出数据
  139. @param readDataLen -[in] 读出数据长度
  140. @return 错误代码
  141. */
  142. static uint32_t SPI0_ReadWriteData(uint32_t CsPin_number, uint8_t *pWriteData, uint8_t writeDataLen, uint8_t *pReadData, uint8_t readDataLen)
  143. {
  144. uint32_t err_code;
  145. uint32_t wait_time_out = WAIT_TIME_VALUE;
  146. if((err_code = SPI0_CsCheck(CsPin_number)) != SPI_OP_SUCCESS)return err_code;
  147. SPI0_CsSet(CsPin_number,BOARD_SPI_CS_ON); //片选信号开
  148. s0_transferOk = false;
  149. err_code = nrf_drv_spi_transfer(&s0_spiHandle, pWriteData, writeDataLen, pReadData, readDataLen);
  150. if(err_code != NRF_SUCCESS)return SPI_ERROR_TIMEOUT;
  151. while(!s0_transferOk)
  152. {
  153. if(wait_time_out--){
  154. nrf_pwr_mgmt_run();
  155. }
  156. else{
  157. SPI0_CsSet(CsPin_number,BOARD_SPI_CS_OFF); //片选信号关
  158. return SPI_ERROR_TIMEOUT;
  159. }
  160. } // Error in SPI or transfer already in progress.
  161. SPI0_CsSet(CsPin_number,BOARD_SPI_CS_OFF); //片选信号关
  162. return SPI_OP_SUCCESS;
  163. }
  164. static uint32_t SPI1_ReadWriteData(uint32_t CsPin_number, uint8_t *pWriteData, uint8_t writeDataLen, uint8_t *pReadData, uint8_t readDataLen)
  165. {
  166. uint32_t err_code;
  167. uint32_t wait_time_out = WAIT_TIME_VALUE;
  168. if((err_code = SPI1_CsCheck(CsPin_number)) != SPI_OP_SUCCESS)return err_code;
  169. SPI1_CsSet(CsPin_number,BOARD_SPI_CS_ON); //片选信号开
  170. s1_transferOk = false;
  171. APP_ERROR_CHECK(nrf_drv_spi_transfer(&s1_spiHandle, pWriteData, writeDataLen, pReadData, readDataLen));
  172. while(!s1_transferOk)
  173. {
  174. if(wait_time_out--){
  175. nrf_pwr_mgmt_run();
  176. }
  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. APP_ERROR_CHECK(nrf_drv_spi_transfer(&s2_spiHandle, pWriteData, writeDataLen, pReadData, readDataLen));
  193. while(!s2_transferOk)
  194. {
  195. if(wait_time_out--){
  196. nrf_pwr_mgmt_run();
  197. }
  198. else{
  199. SPI2_CsSet(CsPin_number,BOARD_SPI_CS_OFF); //片选信号关
  200. return SPI_ERROR_TIMEOUT;
  201. }
  202. } // Error in SPI or transfer already in progress.
  203. SPI2_CsSet(CsPin_number,BOARD_SPI_CS_OFF); //片选信号关
  204. return SPI_OP_SUCCESS;
  205. }
  206. /**
  207. @brief SPI读写寄存器数据
  208. @param CsPin_number -[in] 片选引脚
  209. @param reg_addr -[in] 操作的寄存器地址
  210. @param ReadWriteFlag -[in] 读写标志位
  211. @param pData -[in/out] 指向需要操作的数据
  212. @param DataLen -[in] 数据长度
  213. @return 错误代码
  214. */
  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. static uint8_t temp_buff[260];
  218. uint32_t err_code;
  219. uint8_t addr;
  220. if((err_code = SPI0_CsCheck(CsPin_number)) != SPI_OP_SUCCESS)return err_code;
  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. static uint8_t temp_buff[260];
  240. uint32_t err_code;
  241. uint8_t addr;
  242. if((err_code = SPI1_CsCheck(CsPin_number)) != SPI_OP_SUCCESS)return err_code;
  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. static uint8_t temp_buff[260];
  262. uint32_t err_code;
  263. uint8_t addr;
  264. if((err_code = SPI2_CsCheck(CsPin_number)) != SPI_OP_SUCCESS)return err_code;
  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. /*********************************************************************
  282. * PUBLIC FUNCTIONS
  283. */
  284. /****************************************************接口****************************************************/
  285. /**
  286. @brief SPI的初始化函数
  287. @param 无
  288. @return 无
  289. */
  290. void SPI0_Init(void)
  291. {
  292. ret_code_t errCode;
  293. nrf_drv_spi_config_t spiConfig = NRF_DRV_SPI_DEFAULT_CONFIG; // 使用SPI默认配置
  294. // 配置SPI端口,注意CSN不要在这设置,另外用GPIO口控制
  295. spiConfig.miso_pin = BOARD_SPI0_MISO_IO;
  296. spiConfig.mosi_pin = BOARD_SPI0_MOSI_IO;
  297. spiConfig.sck_pin = BOARD_SPI0_CLK_IO;
  298. spiConfig.mode = NRF_DRV_SPI_MODE_0;
  299. spiConfig.frequency = SPI0_FREQUENCY;
  300. spiConfig.irq_priority = SPI0_IRQ_PRIORITY; // 在定时器中使用优先级需小于6
  301. errCode = nrf_drv_spi_init(&s0_spiHandle, &spiConfig, spi0CallbackFunc, NULL);
  302. APP_ERROR_CHECK(errCode);
  303. uint32_t i;
  304. for(i = 0; i < SPI0_CS_NUMBER; i++)
  305. {
  306. nrf_gpio_cfg_output(s0_cs_list[i]);
  307. }
  308. }
  309. void SPI1_Init(void)
  310. {
  311. ret_code_t errCode;
  312. nrf_drv_spi_config_t spiConfig = NRF_DRV_SPI_DEFAULT_CONFIG; // 使用SPI默认配置
  313. // 配置SPI端口,注意CSN不要在这设置,另外用GPIO口控制
  314. spiConfig.miso_pin = BOARD_SPI1_MISO_IO;
  315. spiConfig.mosi_pin = BOARD_SPI1_MOSI_IO;
  316. spiConfig.sck_pin = BOARD_SPI1_CLK_IO;
  317. spiConfig.mode = NRF_DRV_SPI_MODE_0;
  318. spiConfig.frequency = SPI1_FREQUENCY;
  319. spiConfig.irq_priority = SPI1_IRQ_PRIORITY; // 在定时器中使用优先级需小于6
  320. errCode = nrf_drv_spi_init(&s1_spiHandle, &spiConfig, spi1CallbackFunc, NULL);
  321. APP_ERROR_CHECK(errCode);
  322. uint32_t i;
  323. for(i = 0; i < SPI1_CS_NUMBER; i++)
  324. {
  325. nrf_gpio_cfg_output(s1_cs_list[i]);
  326. }
  327. }
  328. void SPI2_Init(void)
  329. {
  330. ret_code_t errCode;
  331. nrf_drv_spi_config_t spiConfig = NRF_DRV_SPI_DEFAULT_CONFIG; // 使用SPI默认配置
  332. // 配置SPI端口,注意CSN不要在这设置,另外用GPIO口控制
  333. spiConfig.miso_pin = BOARD_SPI2_MISO_IO;
  334. spiConfig.mosi_pin = BOARD_SPI2_MOSI_IO;
  335. spiConfig.sck_pin = BOARD_SPI2_CLK_IO;
  336. spiConfig.mode = NRF_DRV_SPI_MODE_0;
  337. spiConfig.frequency = SPI2_FREQUENCY;
  338. spiConfig.irq_priority = SPI2_IRQ_PRIORITY; // 在定时器中使用优先级需小于6
  339. errCode = nrf_drv_spi_init(&s2_spiHandle, &spiConfig, spi2CallbackFunc, NULL);
  340. APP_ERROR_CHECK(errCode);
  341. uint32_t i;
  342. for(i = 0; i < SPI2_CS_NUMBER; i++)
  343. {
  344. nrf_gpio_cfg_output(s2_cs_list[i]);
  345. }
  346. }
  347. /**
  348. @brief SPI只读寄存器数据
  349. @param CsPin_number -[in] 片选引脚
  350. @param reg_addr -[in] 操作的寄存器地址
  351. @param pData -[in/out] 指向需要操作的数据
  352. @param DataLen -[in] 数据长度
  353. @return 错误代码
  354. */
  355. uint32_t SPI0_OnlyReadReg(uint32_t CsPin_number, uint8_t reg_addr, uint8_t *pData, uint8_t DataLen)
  356. {
  357. return SPI0_ReadWriteReg(CsPin_number, reg_addr, SPI_READ, pData, DataLen);
  358. }
  359. uint32_t SPI1_OnlyReadReg(uint32_t CsPin_number, uint8_t reg_addr, uint8_t *pData, uint8_t DataLen)
  360. {
  361. return SPI1_ReadWriteReg(CsPin_number, reg_addr, SPI_READ, pData, DataLen);
  362. }
  363. uint32_t SPI2_OnlyReadReg(uint32_t CsPin_number, uint8_t reg_addr, uint8_t *pData, uint8_t DataLen)
  364. {
  365. return SPI2_ReadWriteReg(CsPin_number, reg_addr, SPI_READ, pData, DataLen);
  366. }
  367. /**
  368. @brief SPI只写寄存器数据
  369. @param CsPin_number -[in] 片选引脚
  370. @param reg_addr -[in] 操作的寄存器地址
  371. @param pData -[in/out] 指向需要操作的数据
  372. @param DataLen -[in] 数据长度
  373. @return 错误代码
  374. */
  375. uint32_t SPI0_OnlyWriteReg(uint32_t CsPin_number, uint8_t reg_addr, uint8_t *pData, uint8_t DataLen)
  376. {
  377. return SPI0_ReadWriteReg(CsPin_number, reg_addr, SPI_WRITE, pData, DataLen);
  378. }
  379. uint32_t SPI1_OnlyWriteReg(uint32_t CsPin_number, uint8_t reg_addr, uint8_t *pData, uint8_t DataLen)
  380. {
  381. return SPI1_ReadWriteReg(CsPin_number, reg_addr, SPI_WRITE, pData, DataLen);
  382. }
  383. uint32_t SPI2_OnlyWriteReg(uint32_t CsPin_number, uint8_t reg_addr, uint8_t *pData, uint8_t DataLen)
  384. {
  385. return SPI2_ReadWriteReg(CsPin_number, reg_addr, SPI_WRITE, pData, DataLen);
  386. }
  387. /**
  388. @brief 开启SPI
  389. @param 无
  390. @return 错误代码
  391. */
  392. uint32_t SPI0_Enable(void)
  393. {
  394. SPI0_Init();
  395. return SPI_OP_SUCCESS;
  396. }
  397. uint32_t SPI1_Enable(void)
  398. {
  399. SPI1_Init();
  400. return SPI_OP_SUCCESS;
  401. }
  402. uint32_t SPI2_Enable(void)
  403. {
  404. SPI2_Init();
  405. return SPI_OP_SUCCESS;
  406. }
  407. /**
  408. @brief 禁用SPI
  409. @param 无
  410. @return 无
  411. */
  412. uint32_t SPI0_Disable(void)
  413. {
  414. uint32_t i;
  415. //所有片选信号关
  416. for(i = 0; i < SPI0_CS_NUMBER; i++)
  417. {
  418. SPI0_CsSet(s0_cs_list[i],BOARD_SPI_CS_OFF);
  419. nrf_gpio_cfg_default(s0_cs_list[i]);
  420. }
  421. nrf_drv_spi_uninit(&s0_spiHandle);
  422. nrf_gpio_cfg_default(BOARD_SPI0_MISO_IO);
  423. nrf_gpio_cfg_default(BOARD_SPI0_MOSI_IO);
  424. nrf_gpio_cfg_default(BOARD_SPI0_CLK_IO);
  425. /*I think you're experiencing this increase from this errata since TWI and SPI shares resources internally,
  426. this errata may also occur when using SPI. Please try this after disabling the SPI:
  427. *(volatile uint32_t *)0x40003FFC = 0;
  428. *(volatile uint32_t *)0x40003FFC;
  429. *(volatile uint32_t *)0x40003FFC = 1;
  430. If SPI1 is used the address is 40004FFC.
  431. */
  432. //下面的一调用,所有SPI都会关闭!!!
  433. // *(volatile uint32_t *)0x40003FFC = 0;
  434. // *(volatile uint32_t *)0x40003FFC;
  435. // *(volatile uint32_t *)0x40003FFC = 1;
  436. return SPI_OP_SUCCESS;
  437. }
  438. uint32_t SPI1_Disable(void)
  439. {
  440. uint32_t i;
  441. //所有片选信号关
  442. for(i = 0; i < SPI1_CS_NUMBER; i++)
  443. {
  444. SPI1_CsSet(s1_cs_list[i],BOARD_SPI_CS_OFF);
  445. nrf_gpio_cfg_default(s1_cs_list[i]);
  446. }
  447. nrf_drv_spi_uninit(&s1_spiHandle);
  448. nrf_gpio_cfg_default(BOARD_SPI1_MISO_IO);
  449. nrf_gpio_cfg_default(BOARD_SPI1_MOSI_IO);
  450. nrf_gpio_cfg_default(BOARD_SPI1_CLK_IO);
  451. /*I think you're experiencing this increase from this errata since TWI and SPI shares resources internally,
  452. this errata may also occur when using SPI. Please try this after disabling the SPI:
  453. *(volatile uint32_t *)0x40003FFC = 0;
  454. *(volatile uint32_t *)0x40003FFC;
  455. *(volatile uint32_t *)0x40003FFC = 1;
  456. If SPI1 is used the address is 40004FFC.
  457. */
  458. //下面的一调用,所有SPI都会关闭!!!
  459. // *(volatile uint32_t *)0x40003FFC = 0;
  460. // *(volatile uint32_t *)0x40003FFC;
  461. // *(volatile uint32_t *)0x40003FFC = 1;
  462. return SPI_OP_SUCCESS;
  463. }
  464. uint32_t SPI2_Disable(void)
  465. {
  466. uint32_t i;
  467. //所有片选信号关
  468. for(i = 0; i < SPI2_CS_NUMBER; i++)
  469. {
  470. SPI2_CsSet(s2_cs_list[i],BOARD_SPI_CS_OFF);
  471. nrf_gpio_cfg_default(s2_cs_list[i]);
  472. }
  473. nrf_drv_spi_uninit(&s2_spiHandle);
  474. nrf_gpio_cfg_default(BOARD_SPI2_MISO_IO);
  475. nrf_gpio_cfg_default(BOARD_SPI2_MOSI_IO);
  476. nrf_gpio_cfg_default(BOARD_SPI2_CLK_IO);
  477. /*I think you're experiencing this increase from this errata since TWI and SPI shares resources internally,
  478. this errata may also occur when using SPI. Please try this after disabling the SPI:
  479. *(volatile uint32_t *)0x40003FFC = 0;
  480. *(volatile uint32_t *)0x40003FFC;
  481. *(volatile uint32_t *)0x40003FFC = 1;
  482. If SPI1 is used the address is 40004FFC.
  483. */
  484. //下面的一调用,所有SPI都会关闭!!!
  485. // *(volatile uint32_t *)0x40003FFC = 0;
  486. // *(volatile uint32_t *)0x40003FFC;
  487. // *(volatile uint32_t *)0x40003FFC = 1;
  488. return SPI_OP_SUCCESS;
  489. }