drv_spi.c 17 KB

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