drv_spi.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-05-22 Sherman first version
  9. * 2020-11-02 xckhmf fixed bug
  10. * 2023-01-28 Andrew add Nrf5340 support
  11. */
  12. #include <stdint.h>
  13. #include <string.h>
  14. #include "board.h"
  15. #include "drv_spi.h"
  16. #define DBG_LEVEL DBG_LOG
  17. #include <rtdbg.h>
  18. #define LOG_TAG "drv.spi"
  19. #ifdef BSP_USING_SPI
  20. #if defined(SOC_NRF5340)
  21. #if defined(BSP_USING_SPI0) || defined(BSP_USING_SPI1) || defined(BSP_USING_SPI2) || defined(BSP_USING_SPI3) || defined(BSP_USING_SPI4)
  22. static struct nrfx_drv_spi_config spi_config[] =
  23. {
  24. #ifdef BSP_USING_SPI0
  25. NRFX_SPI0_CONFIG,
  26. #endif
  27. #ifdef BSP_USING_SPI1
  28. NRFX_SPI1_CONFIG,
  29. #endif
  30. #ifdef BSP_USING_SPI2
  31. NRFX_SPI2_CONFIG,
  32. #endif
  33. #ifdef BSP_USING_SPI3
  34. NRFX_SPI3_CONFIG,
  35. #endif
  36. #ifdef BSP_USING_SPI4
  37. NRFX_SPI4_CONFIG,
  38. #endif
  39. };
  40. static struct nrfx_drv_spi spi_bus_obj[sizeof(spi_config) / sizeof(spi_config[0])];
  41. //Configure SPI bus pins using the menuconfig
  42. static struct nrfx_drv_spi_pin_config bsp_spi_pin[] =
  43. {
  44. #ifdef BSP_USING_SPI0
  45. {
  46. .sck_pin = BSP_SPI0_SCK_PIN,
  47. .mosi_pin = BSP_SPI0_MOSI_PIN,
  48. .miso_pin = BSP_SPI0_MISO_PIN,
  49. .ss_pin = BSP_SPI0_SS_PIN
  50. },
  51. #endif
  52. #ifdef BSP_USING_SPI1
  53. {
  54. .sck_pin = BSP_SPI1_SCK_PIN,
  55. .mosi_pin = BSP_SPI1_MOSI_PIN,
  56. .miso_pin = BSP_SPI1_MISO_PIN,
  57. .ss_pin = BSP_SPI1_SS_PIN
  58. },
  59. #endif
  60. #ifdef BSP_USING_SPI2
  61. {
  62. .sck_pin = BSP_SPI2_SCK_PIN,
  63. .mosi_pin = BSP_SPI2_MOSI_PIN,
  64. .miso_pin = BSP_SPI2_MISO_PIN,
  65. .ss_pin = BSP_SPI2_SS_PIN
  66. },
  67. #endif
  68. #ifdef BSP_USING_SPI3
  69. .sck_pin = BSP_SPI3_SCK_PIN,
  70. .mosi_pin = BSP_SPI3_MOSI_PIN,
  71. .miso_pin = BSP_SPI3_MISO_PIN,
  72. .ss_pin = BSP_SPI3_SS_PIN
  73. },
  74. #endif
  75. #ifdef BSP_USING_SPI4
  76. {
  77. .sck_pin = BSP_SPI4_SCK_PIN,
  78. .mosi_pin = BSP_SPI4_MOSI_PIN,
  79. .miso_pin = BSP_SPI4_MISO_PIN,
  80. .ss_pin = BSP_SPI4_SS_PIN
  81. },
  82. #endif
  83. };
  84. static rt_uint8_t spi_index_find(struct rt_spi_bus *spi_bus)
  85. {
  86. for (int i = 0; i < sizeof(spi_config) / sizeof(spi_config[0]); i++)
  87. {
  88. if(spi_bus == &spi_bus_obj[i].spi_bus)
  89. return i;
  90. }
  91. return 0xFF;
  92. }
  93. /**
  94. * spi event handler function
  95. */
  96. static void spi0_handler(const nrfx_spim_evt_t *p_event, void *p_context)
  97. {
  98. LOG_I("\nspi0_handler");
  99. }
  100. static void spi1_handler(const nrfx_spim_evt_t *p_event, void *p_context)
  101. {
  102. LOG_I("\nspi1_handler");
  103. }
  104. static void spi2_handler(const nrfx_spim_evt_t *p_event, void *p_context)
  105. {
  106. LOG_I("\nspi2_handler");
  107. }
  108. static void spi3_handler(const nrfx_spim_evt_t *p_event, void *p_context)
  109. {
  110. LOG_I("\nspi3_handler");
  111. }
  112. static void spi4_handler(const nrfx_spim_evt_t *p_event, void *p_context)
  113. {
  114. LOG_I("\nspi4_handler");
  115. }
  116. nrfx_spim_evt_handler_t spi_handler[] = {spi0_handler, spi1_handler, spi2_handler ,spi3_handler,spi4_handler};
  117. /**
  118. * @brief This function config spi bus
  119. * @param device
  120. * @param configuration
  121. * @retval RT_EOK / -RT_ERROR
  122. */
  123. static rt_err_t spi_configure(struct rt_spi_device *device,
  124. struct rt_spi_configuration *configuration)
  125. {
  126. RT_ASSERT(device != RT_NULL);
  127. RT_ASSERT(device->bus != RT_NULL);
  128. RT_ASSERT(device->bus->parent.user_data != RT_NULL);
  129. RT_ASSERT(configuration != RT_NULL);
  130. rt_uint8_t index = spi_index_find(device->bus);
  131. RT_ASSERT(index != 0xFF);
  132. nrfx_spim_t spi = spi_bus_obj[index].spi;
  133. nrfx_spim_config_t config = NRFX_SPIM_DEFAULT_CONFIG(bsp_spi_pin[index].sck_pin,
  134. bsp_spi_pin[index].mosi_pin, bsp_spi_pin[index].miso_pin, NRFX_SPIM_PIN_NOT_USED);
  135. /* spi config ss pin */
  136. if(device->parent.user_data != RT_NULL)
  137. {
  138. nrf_gpio_cfg_output((uint32_t)device->parent.user_data);
  139. }
  140. /* spi config bit order */
  141. if(configuration->mode & RT_SPI_MSB)
  142. {
  143. config.bit_order = NRF_SPIM_BIT_ORDER_MSB_FIRST;
  144. }
  145. else
  146. {
  147. config.bit_order = NRF_SPIM_BIT_ORDER_LSB_FIRST;
  148. }
  149. /* spi mode config */
  150. switch (configuration->mode & RT_SPI_MODE_3 )
  151. {
  152. case RT_SPI_MODE_0/* RT_SPI_CPOL:0 , RT_SPI_CPHA:0 */:
  153. config.mode = NRF_SPIM_MODE_0;
  154. break;
  155. case RT_SPI_MODE_1/* RT_SPI_CPOL:0 , RT_SPI_CPHA:1 */:
  156. config.mode = NRF_SPIM_MODE_1;
  157. break;
  158. case RT_SPI_MODE_2/* RT_SPI_CPOL:1 , RT_SPI_CPHA:0 */:
  159. config.mode = NRF_SPIM_MODE_2;
  160. break;
  161. case RT_SPI_MODE_3/* RT_SPI_CPOL:1 , RT_SPI_CPHA:1 */:
  162. config.mode = NRF_SPIM_MODE_3;
  163. break;
  164. default:
  165. LOG_E("spi_configure mode error %x\n",configuration->mode);
  166. return -RT_ERROR;
  167. }
  168. /* spi frequency config */
  169. switch (configuration->max_hz / 1000)
  170. {
  171. case 125:
  172. config.frequency = NRF_SPIM_FREQ_125K;
  173. break;
  174. case 250:
  175. config.frequency = NRF_SPIM_FREQ_250K;
  176. break;
  177. case 500:
  178. config.frequency = NRF_SPIM_FREQ_500K;
  179. break;
  180. case 1000:
  181. config.frequency = NRF_SPIM_FREQ_1M;
  182. break;
  183. case 2000:
  184. config.frequency = NRF_SPIM_FREQ_2M;
  185. break;
  186. case 4000:
  187. config.frequency = NRF_SPIM_FREQ_4M;
  188. break;
  189. case 8000:
  190. config.frequency = NRF_SPIM_FREQ_8M;
  191. break;
  192. default:
  193. LOG_E("spi_configure rate error %d\n",configuration->max_hz);
  194. break;
  195. }
  196. rt_memcpy((void*)&spi_bus_obj[index].spi_config, (void*)&config, sizeof(nrfx_spim_config_t));
  197. nrfx_spim_evt_handler_t handler = RT_NULL; //spi send callback handler ,default NULL
  198. void * context = RT_NULL;
  199. nrfx_err_t nrf_ret = nrfx_spim_init(&spi, &config, handler, context);
  200. if(NRFX_SUCCESS == nrf_ret)
  201. return RT_EOK;
  202. return -RT_ERROR;
  203. }
  204. static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
  205. {
  206. RT_ASSERT(device != RT_NULL);
  207. RT_ASSERT(device->bus != RT_NULL);
  208. RT_ASSERT(device->bus->parent.user_data != RT_NULL);
  209. rt_uint8_t index = spi_index_find(device->bus);
  210. nrfx_err_t nrf_ret;
  211. RT_ASSERT(index != 0xFF);
  212. nrfx_spim_t * p_instance = &spi_bus_obj[index].spi;
  213. nrfx_spim_xfer_desc_t p_xfer_desc;
  214. if(message->cs_take == 1)
  215. {
  216. nrf_gpio_pin_clear((uint32_t)device->parent.user_data);
  217. }
  218. p_xfer_desc.p_rx_buffer = message->recv_buf;
  219. p_xfer_desc.rx_length = message->length;
  220. p_xfer_desc.p_tx_buffer = message->send_buf;
  221. p_xfer_desc.tx_length = message->length ;
  222. if(message->send_buf == RT_NULL)
  223. {
  224. p_xfer_desc.tx_length = 0;
  225. }
  226. if(message->recv_buf == RT_NULL)
  227. {
  228. p_xfer_desc.rx_length = 0;
  229. }
  230. nrf_ret = nrfx_spim_xfer(p_instance, &p_xfer_desc, 0);
  231. if(message->cs_release == 1)
  232. {
  233. nrf_gpio_pin_set((uint32_t)device->parent.user_data);
  234. }
  235. if( NRFX_SUCCESS != nrf_ret)
  236. {
  237. return 0;
  238. }
  239. else
  240. {
  241. return message->length;
  242. }
  243. }
  244. /* spi bus callback function */
  245. static const struct rt_spi_ops nrfx_spi_ops =
  246. {
  247. .configure = spi_configure,
  248. .xfer = spixfer,
  249. };
  250. /*spi bus init*/
  251. static int rt_hw_spi_bus_init(void)
  252. {
  253. rt_err_t result = -RT_ERROR;
  254. for (int i = 0; i < sizeof(spi_config) / sizeof(spi_config[0]); i++)
  255. {
  256. spi_bus_obj[i].spi = spi_config[i].spi;
  257. spi_bus_obj[i].spi_bus.parent.user_data = &spi_config[i]; //SPI INSTANCE
  258. result = rt_spi_bus_register(&spi_bus_obj[i].spi_bus, spi_config[i].bus_name, &nrfx_spi_ops);
  259. RT_ASSERT(result == RT_EOK);
  260. }
  261. return result;
  262. }
  263. int rt_hw_spi_init(void)
  264. {
  265. return rt_hw_spi_bus_init();
  266. }
  267. INIT_BOARD_EXPORT(rt_hw_spi_init);
  268. /**
  269. * Attach the spi device to SPI bus, this function must be used after initialization.
  270. */
  271. rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_uint32_t ss_pin)
  272. {
  273. RT_ASSERT(bus_name != RT_NULL);
  274. RT_ASSERT(device_name != RT_NULL);
  275. RT_ASSERT(ss_pin != RT_NULL);
  276. rt_err_t result;
  277. struct rt_spi_device *spi_device;
  278. /* attach the device to spi bus*/
  279. spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
  280. RT_ASSERT(spi_device != RT_NULL);
  281. /* initialize the cs pin */
  282. result = rt_spi_bus_attach_device(spi_device, device_name, bus_name, (void*)ss_pin);
  283. if (result != RT_EOK)
  284. {
  285. LOG_E("%s attach to %s faild, %d", device_name, bus_name, result);
  286. result = -RT_ERROR;
  287. }
  288. RT_ASSERT(result == RT_EOK);
  289. return result;
  290. }
  291. #define SPI_DEVICE_NAME "spi4x"
  292. #define TEST_STRING "liguan"
  293. static rt_uint8_t m_tx_buf[] = TEST_STRING; /**< TX buffer. */
  294. static int spi_sample(int argc, char *argv[])
  295. {
  296. struct rt_spi_device *spi_dev;
  297. char name[RT_NAME_MAX];
  298. rt_uint8_t w25x_read_id = 0x65;
  299. rt_uint8_t id[5] = {0};
  300. rt_hw_spi_device_attach("spi4", SPI_DEVICE_NAME, 11);
  301. struct rt_spi_configuration cfg;
  302. cfg.data_width = 8;
  303. cfg.mode = RT_SPI_MASTER | RT_SPI_MODE_0 | RT_SPI_MSB;
  304. cfg.max_hz = 20 * 1000 *1000; /* 20M */
  305. /* 查找 spi 设备获取设备句柄 */
  306. spi_dev = (struct rt_spi_device *)rt_device_find(SPI_DEVICE_NAME);
  307. if (!spi_dev)
  308. {
  309. rt_kprintf("spi sample run failed! can't find %s device!\n", name);
  310. }
  311. else
  312. {
  313. rt_spi_configure(spi_dev, &cfg);
  314. /* 方式1:使用 rt_spi_send_then_recv()发送命令读取ID */
  315. while(1)
  316. { rt_spi_send(spi_dev, m_tx_buf, 6);rt_thread_mdelay(500);}
  317. //rt_kprintf("use rt_spi_send_then_recv() read w25q ID is:%x%x\n", id[3], id[4]);
  318. }
  319. return RT_EOK;
  320. }
  321. MSH_CMD_EXPORT(spi_sample, spi sample);
  322. #endif /* BSP_USING_SPI0 || BSP_USING_SPI1 || BSP_USING_SPI2 */
  323. #else
  324. #if defined(BSP_USING_SPI0) || defined(BSP_USING_SPI1) || defined(BSP_USING_SPI2)
  325. static struct nrfx_drv_spi_config spi_config[] =
  326. {
  327. #ifdef BSP_USING_SPI0
  328. NRFX_SPI0_CONFIG,
  329. #endif
  330. #ifdef BSP_USING_SPI1
  331. NRFX_SPI1_CONFIG,
  332. #endif
  333. #ifdef BSP_USING_SPI2
  334. NRFX_SPI2_CONFIG,
  335. #endif
  336. };
  337. static struct nrfx_drv_spi spi_bus_obj[sizeof(spi_config) / sizeof(spi_config[0])];
  338. //Configure SPI bus pins using the menuconfig
  339. static struct nrfx_drv_spi_pin_config bsp_spi_pin[] =
  340. {
  341. #ifdef BSP_USING_SPI0
  342. {
  343. .sck_pin = BSP_SPI0_SCK_PIN,
  344. .mosi_pin = BSP_SPI0_MOSI_PIN,
  345. .miso_pin = BSP_SPI0_MISO_PIN,
  346. .ss_pin = BSP_SPI0_SS_PIN
  347. },
  348. #endif
  349. #ifdef BSP_USING_SPI1
  350. {
  351. .sck_pin = BSP_SPI1_SCK_PIN,
  352. .mosi_pin = BSP_SPI1_MOSI_PIN,
  353. .miso_pin = BSP_SPI1_MISO_PIN,
  354. .ss_pin = BSP_SPI1_SS_PIN
  355. },
  356. #endif
  357. #ifdef BSP_USING_SPI2
  358. {
  359. .sck_pin = BSP_SPI2_SCK_PIN,
  360. .mosi_pin = BSP_SPI2_MOSI_PIN,
  361. .miso_pin = BSP_SPI2_MISO_PIN,
  362. .ss_pin = BSP_SPI2_SS_PIN
  363. },
  364. #endif
  365. };
  366. static rt_uint8_t spi_index_find(struct rt_spi_bus *spi_bus)
  367. {
  368. for (int i = 0; i < sizeof(spi_config) / sizeof(spi_config[0]); i++)
  369. {
  370. if(spi_bus == &spi_bus_obj[i].spi_bus)
  371. return i;
  372. }
  373. return 0xFF;
  374. }
  375. /**
  376. * spi event handler function
  377. */
  378. static void spi0_handler(const nrfx_spi_evt_t *p_event, void *p_context)
  379. {
  380. LOG_I("\nspi0_handler");
  381. }
  382. static void spi1_handler(const nrfx_spi_evt_t *p_event, void *p_context)
  383. {
  384. LOG_I("\nspi1_handler");
  385. }
  386. static void spi2_handler(const nrfx_spi_evt_t *p_event, void *p_context)
  387. {
  388. LOG_I("\nspi2_handler");
  389. }
  390. nrfx_spi_evt_handler_t spi_handler[] = {spi0_handler, spi1_handler, spi2_handler};
  391. /**
  392. * @brief This function config spi bus
  393. * @param device
  394. * @param configuration
  395. * @retval RT_EOK / -RT_ERROR
  396. */
  397. static rt_err_t spi_configure(struct rt_spi_device *device,
  398. struct rt_spi_configuration *configuration)
  399. {
  400. RT_ASSERT(device != RT_NULL);
  401. RT_ASSERT(device->bus != RT_NULL);
  402. RT_ASSERT(device->bus->parent.user_data != RT_NULL);
  403. RT_ASSERT(configuration != RT_NULL);
  404. rt_uint8_t index = spi_index_find(device->bus);
  405. RT_ASSERT(index != 0xFF);
  406. nrfx_spi_t spi = spi_bus_obj[index].spi;
  407. nrfx_spi_config_t config = NRFX_SPI_DEFAULT_CONFIG(bsp_spi_pin[index].sck_pin,
  408. bsp_spi_pin[index].mosi_pin, bsp_spi_pin[index].miso_pin, NRFX_SPI_PIN_NOT_USED);
  409. /* spi config ss pin */
  410. if(device->parent.user_data != RT_NULL)
  411. {
  412. nrf_gpio_cfg_output((uint32_t)device->parent.user_data);
  413. }
  414. /* spi config bit order */
  415. if(configuration->mode & RT_SPI_MSB)
  416. {
  417. config.bit_order = NRF_SPI_BIT_ORDER_MSB_FIRST;
  418. }
  419. else
  420. {
  421. config.bit_order = NRF_SPI_BIT_ORDER_LSB_FIRST;
  422. }
  423. /* spi mode config */
  424. switch (configuration->mode & RT_SPI_MODE_3 )
  425. {
  426. case RT_SPI_MODE_0/* RT_SPI_CPOL:0 , RT_SPI_CPHA:0 */:
  427. config.mode = NRF_SPI_MODE_0;
  428. break;
  429. case RT_SPI_MODE_1/* RT_SPI_CPOL:0 , RT_SPI_CPHA:1 */:
  430. config.mode = NRF_SPI_MODE_1;
  431. break;
  432. case RT_SPI_MODE_2/* RT_SPI_CPOL:1 , RT_SPI_CPHA:0 */:
  433. config.mode = NRF_SPI_MODE_2;
  434. break;
  435. case RT_SPI_MODE_3/* RT_SPI_CPOL:1 , RT_SPI_CPHA:1 */:
  436. config.mode = NRF_SPI_MODE_3;
  437. break;
  438. default:
  439. LOG_E("spi_configure mode error %x\n",configuration->mode);
  440. return -RT_ERROR;
  441. }
  442. /* spi frequency config */
  443. switch (configuration->max_hz / 1000)
  444. {
  445. case 125:
  446. config.frequency = NRF_SPI_FREQ_125K;
  447. break;
  448. case 250:
  449. config.frequency = NRF_SPI_FREQ_250K;
  450. break;
  451. case 500:
  452. config.frequency = NRF_SPI_FREQ_500K;
  453. break;
  454. case 1000:
  455. config.frequency = NRF_SPI_FREQ_1M;
  456. break;
  457. case 2000:
  458. config.frequency = NRF_SPI_FREQ_2M;
  459. break;
  460. case 4000:
  461. config.frequency = NRF_SPI_FREQ_4M;
  462. break;
  463. case 8000:
  464. config.frequency = NRF_SPI_FREQ_8M;
  465. break;
  466. default:
  467. LOG_E("spi_configure rate error %d\n",configuration->max_hz);
  468. break;
  469. }
  470. rt_memcpy((void*)&spi_bus_obj[index].spi_config, (void*)&config, sizeof(nrfx_spi_config_t));
  471. nrfx_spi_evt_handler_t handler = RT_NULL; //spi send callback handler ,default NULL
  472. void * context = RT_NULL;
  473. nrfx_err_t nrf_ret = nrfx_spi_init(&spi, &config, handler, context);
  474. if(NRFX_SUCCESS == nrf_ret)
  475. return RT_EOK;
  476. return -RT_ERROR;
  477. }
  478. static rt_uint32_t spixfer(struct rt_spi_device *device, struct rt_spi_message *message)
  479. {
  480. RT_ASSERT(device != RT_NULL);
  481. RT_ASSERT(device->bus != RT_NULL);
  482. RT_ASSERT(device->bus->parent.user_data != RT_NULL);
  483. rt_uint8_t index = spi_index_find(device->bus);
  484. nrfx_err_t nrf_ret;
  485. RT_ASSERT(index != 0xFF);
  486. nrfx_spi_t * p_instance = &spi_bus_obj[index].spi;
  487. nrfx_spi_xfer_desc_t p_xfer_desc;
  488. if(message->cs_take == 1)
  489. {
  490. nrf_gpio_pin_clear((uint32_t)device->parent.user_data);
  491. }
  492. p_xfer_desc.p_rx_buffer = message->recv_buf;
  493. p_xfer_desc.rx_length = message->length;
  494. p_xfer_desc.p_tx_buffer = message->send_buf;
  495. p_xfer_desc.tx_length = message->length ;
  496. if(message->send_buf == RT_NULL)
  497. {
  498. p_xfer_desc.tx_length = 0;
  499. }
  500. if(message->recv_buf == RT_NULL)
  501. {
  502. p_xfer_desc.rx_length = 0;
  503. }
  504. nrf_ret = nrfx_spi_xfer(p_instance, &p_xfer_desc, 0);
  505. if(message->cs_release == 1)
  506. {
  507. nrf_gpio_pin_set((uint32_t)device->parent.user_data);
  508. }
  509. if( NRFX_SUCCESS != nrf_ret)
  510. {
  511. return 0;
  512. }
  513. else
  514. {
  515. return message->length;
  516. }
  517. }
  518. /* spi bus callback function */
  519. static const struct rt_spi_ops nrfx_spi_ops =
  520. {
  521. .configure = spi_configure,
  522. .xfer = spixfer,
  523. };
  524. /*spi bus init*/
  525. static int rt_hw_spi_bus_init(void)
  526. {
  527. rt_err_t result = -RT_ERROR;
  528. for (int i = 0; i < sizeof(spi_config) / sizeof(spi_config[0]); i++)
  529. {
  530. spi_bus_obj[i].spi = spi_config[i].spi;
  531. spi_bus_obj[i].spi_bus.parent.user_data = &spi_config[i]; //SPI INSTANCE
  532. result = rt_spi_bus_register(&spi_bus_obj[i].spi_bus, spi_config[i].bus_name, &nrfx_spi_ops);
  533. RT_ASSERT(result == RT_EOK);
  534. }
  535. return result;
  536. }
  537. int rt_hw_spi_init(void)
  538. {
  539. return rt_hw_spi_bus_init();
  540. }
  541. INIT_BOARD_EXPORT(rt_hw_spi_init);
  542. /**
  543. * Attach the spi device to SPI bus, this function must be used after initialization.
  544. */
  545. rt_err_t rt_hw_spi_device_attach(const char *bus_name, const char *device_name, rt_uint32_t ss_pin)
  546. {
  547. RT_ASSERT(bus_name != RT_NULL);
  548. RT_ASSERT(device_name != RT_NULL);
  549. RT_ASSERT(ss_pin != RT_NULL);
  550. rt_err_t result;
  551. struct rt_spi_device *spi_device;
  552. /* attach the device to spi bus*/
  553. spi_device = (struct rt_spi_device *)rt_malloc(sizeof(struct rt_spi_device));
  554. RT_ASSERT(spi_device != RT_NULL);
  555. /* initialize the cs pin */
  556. result = rt_spi_bus_attach_device(spi_device, device_name, bus_name, (void*)ss_pin);
  557. if (result != RT_EOK)
  558. {
  559. LOG_E("%s attach to %s faild, %d", device_name, bus_name, result);
  560. result = -RT_ERROR;
  561. }
  562. RT_ASSERT(result == RT_EOK);
  563. return result;
  564. }
  565. #endif /* BSP_USING_SPI0 || BSP_USING_SPI1 || BSP_USING_SPI2 */
  566. #endif
  567. #endif /*BSP_USING_SPI*/