dev_spi_core.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. /*
  2. * Copyright (c) 2006-2025 RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2012-01-08 bernard first version.
  9. * 2012-02-03 bernard add const attribute to the ops.
  10. * 2012-05-15 dzzxzz fixed the return value in attach_device.
  11. * 2012-05-18 bernard Changed SPI message to message list.
  12. * Added take/release SPI device/bus interface.
  13. * 2012-09-28 aozima fixed rt_spi_release_bus assert error.
  14. * 2025-10-30 wdfk-prog enable interrupt-safe operations using spinlocks
  15. */
  16. #include "drivers/dev_spi.h"
  17. #define DBG_TAG "spi.core"
  18. #define DBG_LVL DBG_INFO
  19. #include <rtdbg.h>
  20. #ifdef RT_USING_DM
  21. #include "dev_spi_dm.h"
  22. #endif
  23. extern rt_err_t rt_spi_bus_device_init(struct rt_spi_bus *bus, const char *name);
  24. extern rt_err_t rt_spidev_device_init(struct rt_spi_device *dev, const char *name);
  25. rt_err_t spi_bus_register(struct rt_spi_bus *bus,
  26. const char *name,
  27. const struct rt_spi_ops *ops)
  28. {
  29. rt_err_t result;
  30. result = rt_spi_bus_device_init(bus, name);
  31. if (result != RT_EOK)
  32. return result;
  33. /* initialize mutex lock */
  34. rt_mutex_init(&(bus->lock), name, RT_IPC_FLAG_PRIO);
  35. #ifdef RT_USING_SPI_ISR
  36. rt_spin_lock_init(&bus->_spinlock);
  37. #endif /* RT_USING_SPI_ISR */
  38. /* set ops */
  39. bus->ops = ops;
  40. /* initialize owner */
  41. bus->owner = RT_NULL;
  42. #ifdef RT_USING_DM
  43. if (!bus->slave)
  44. {
  45. int pin_count = rt_pin_get_named_pin_count(&bus->parent, "cs");
  46. if (pin_count > 0)
  47. {
  48. pin_count = rt_max_t(int, pin_count, bus->num_chipselect);
  49. for (int i = 0; i < pin_count; ++i)
  50. {
  51. bus->cs_pins[i] = rt_pin_get_named_pin(&bus->parent, "cs", i,
  52. RT_NULL, &bus->cs_active_vals[i]);
  53. }
  54. }
  55. else if (pin_count < 0)
  56. {
  57. result = pin_count;
  58. LOG_E("CS PIN find error = %s", rt_strerror(result));
  59. rt_device_unregister(&bus->parent);
  60. return result;
  61. }
  62. }
  63. spi_bus_scan_devices(bus);
  64. #endif
  65. return RT_EOK;
  66. }
  67. rt_err_t rt_spi_bus_register(struct rt_spi_bus *bus,
  68. const char *name,
  69. const struct rt_spi_ops *ops)
  70. {
  71. /* set bus mode */
  72. bus->mode = RT_SPI_BUS_MODE_SPI;
  73. return spi_bus_register(bus, name, ops);
  74. }
  75. rt_err_t rt_spi_bus_attach_device_cspin(struct rt_spi_device *device,
  76. const char *name,
  77. const char *bus_name,
  78. rt_base_t cs_pin,
  79. void *user_data)
  80. {
  81. rt_err_t result;
  82. rt_device_t bus;
  83. /* get physical spi bus */
  84. bus = rt_device_find(bus_name);
  85. if (bus != RT_NULL && bus->type == RT_Device_Class_SPIBUS)
  86. {
  87. device->bus = (struct rt_spi_bus *)bus;
  88. if (device->bus->owner == RT_NULL)
  89. device->bus->owner = device;
  90. /* initialize spidev device */
  91. result = rt_spidev_device_init(device, name);
  92. if (result != RT_EOK)
  93. return result;
  94. if (cs_pin != PIN_NONE)
  95. {
  96. rt_pin_mode(cs_pin, PIN_MODE_OUTPUT);
  97. }
  98. rt_memset(&device->config, 0, sizeof(device->config));
  99. device->parent.user_data = user_data;
  100. device->cs_pin = cs_pin;
  101. return RT_EOK;
  102. }
  103. /* not found the host bus */
  104. return -RT_ERROR;
  105. }
  106. rt_err_t rt_spi_bus_detach_device_cspin(struct rt_spi_device *device)
  107. {
  108. rt_err_t result;
  109. RT_ASSERT(device != RT_NULL);
  110. result = rt_device_unregister(&device->parent);
  111. if (result != RT_EOK)
  112. {
  113. LOG_E("Failed to unregister spi device, result: %d", result);
  114. return result;
  115. }
  116. if (device->bus != RT_NULL && device->bus->owner == device)
  117. {
  118. device->bus->owner = RT_NULL;
  119. }
  120. if (device->cs_pin != PIN_NONE)
  121. {
  122. rt_pin_mode(device->cs_pin, PIN_MODE_INPUT);
  123. }
  124. device->bus = RT_NULL;
  125. return RT_EOK;
  126. }
  127. rt_err_t rt_spi_bus_attach_device(struct rt_spi_device *device,
  128. const char *name,
  129. const char *bus_name,
  130. void *user_data)
  131. {
  132. return rt_spi_bus_attach_device_cspin(device, name, bus_name, PIN_NONE, user_data);
  133. }
  134. rt_err_t rt_spi_bus_detach_device(struct rt_spi_device *device)
  135. {
  136. return rt_spi_bus_detach_device_cspin(device);
  137. }
  138. static rt_err_t spi_lock(struct rt_spi_bus *bus)
  139. {
  140. RT_ASSERT(bus);
  141. rt_err_t ret = -RT_ERROR;
  142. /* If the scheduler is started and in thread context */
  143. if (rt_scheduler_is_available())
  144. {
  145. ret = rt_mutex_take(&(bus->lock), RT_WAITING_FOREVER);
  146. }
  147. else
  148. {
  149. #ifdef RT_USING_SPI_ISR
  150. bus->_isr_lvl = rt_spin_lock_irqsave(&bus->_spinlock);
  151. ret = RT_EOK;
  152. #endif /* RT_USING_SPI_ISR */
  153. }
  154. return ret;
  155. }
  156. static rt_err_t spi_unlock(struct rt_spi_bus *bus)
  157. {
  158. RT_ASSERT(bus);
  159. rt_err_t ret = -RT_ERROR;
  160. /* If the scheduler is started and in thread context */
  161. if (rt_scheduler_is_available())
  162. {
  163. ret = rt_mutex_release(&(bus->lock));
  164. }
  165. else
  166. {
  167. #ifdef RT_USING_SPI_ISR
  168. rt_spin_unlock_irqrestore(&bus->_spinlock, bus->_isr_lvl);
  169. ret = RT_EOK;
  170. #endif /* RT_USING_SPI_ISR */
  171. }
  172. return ret;
  173. }
  174. rt_err_t rt_spi_bus_configure(struct rt_spi_device *device)
  175. {
  176. rt_err_t result = -RT_ERROR;
  177. if (device->bus != RT_NULL)
  178. {
  179. result = spi_lock(device->bus);
  180. if (result == RT_EOK)
  181. {
  182. if (device->bus->owner == device)
  183. {
  184. /* current device is using, re-configure SPI bus */
  185. result = device->bus->ops->configure(device, &device->config);
  186. if (result != RT_EOK)
  187. {
  188. /* configure SPI bus failed */
  189. LOG_E("SPI device %s configuration failed", device->parent.parent.name);
  190. }
  191. }
  192. else
  193. {
  194. /* RT_EBUSY is not an error condition and
  195. * the configuration will take effect once the device has the bus
  196. */
  197. result = -RT_EBUSY;
  198. }
  199. /* release lock */
  200. spi_unlock(device->bus);
  201. }
  202. }
  203. else
  204. {
  205. result = RT_EOK;
  206. }
  207. return result;
  208. }
  209. rt_err_t rt_spi_configure(struct rt_spi_device *device,
  210. struct rt_spi_configuration *cfg)
  211. {
  212. RT_ASSERT(device != RT_NULL);
  213. RT_ASSERT(cfg != RT_NULL);
  214. /* reset the CS pin */
  215. if (device->cs_pin != PIN_NONE)
  216. {
  217. rt_err_t result = spi_lock(device->bus);
  218. if (result == RT_EOK)
  219. {
  220. if (cfg->mode & RT_SPI_CS_HIGH)
  221. {
  222. rt_pin_write(device->cs_pin, PIN_LOW);
  223. }
  224. else
  225. {
  226. rt_pin_write(device->cs_pin, PIN_HIGH);
  227. }
  228. spi_unlock(device->bus);
  229. }
  230. else
  231. {
  232. return result;
  233. }
  234. }
  235. /* If the configurations are the same, we don't need to set again. */
  236. if (device->config.data_width == cfg->data_width &&
  237. device->config.mode == (cfg->mode & RT_SPI_MODE_MASK) &&
  238. device->config.max_hz == cfg->max_hz)
  239. {
  240. return RT_EOK;
  241. }
  242. /* set configuration */
  243. device->config.data_width = cfg->data_width;
  244. device->config.mode = cfg->mode & RT_SPI_MODE_MASK;
  245. device->config.max_hz = cfg->max_hz;
  246. return rt_spi_bus_configure(device);
  247. }
  248. rt_err_t rt_spi_send_then_send(struct rt_spi_device *device,
  249. const void *send_buf1,
  250. rt_size_t send_length1,
  251. const void *send_buf2,
  252. rt_size_t send_length2)
  253. {
  254. rt_err_t result;
  255. struct rt_spi_message message;
  256. RT_ASSERT(device != RT_NULL);
  257. RT_ASSERT(device->bus != RT_NULL);
  258. result = spi_lock(device->bus);
  259. if (result == RT_EOK)
  260. {
  261. if (device->bus->owner != device)
  262. {
  263. /* not the same owner as current, re-configure SPI bus */
  264. result = device->bus->ops->configure(device, &device->config);
  265. if (result == RT_EOK)
  266. {
  267. /* set SPI bus owner */
  268. device->bus->owner = device;
  269. }
  270. else
  271. {
  272. /* configure SPI bus failed */
  273. LOG_E("SPI device %s configuration failed", device->parent.parent.name);
  274. goto __exit;
  275. }
  276. }
  277. /* send data1 */
  278. message.send_buf = send_buf1;
  279. message.recv_buf = RT_NULL;
  280. message.length = send_length1;
  281. message.cs_take = 1;
  282. message.cs_release = 0;
  283. message.next = RT_NULL;
  284. result = device->bus->ops->xfer(device, &message);
  285. if (result < 0)
  286. {
  287. LOG_E("SPI device %s transfer failed", device->parent.parent.name);
  288. goto __exit;
  289. }
  290. /* send data2 */
  291. message.send_buf = send_buf2;
  292. message.recv_buf = RT_NULL;
  293. message.length = send_length2;
  294. message.cs_take = 0;
  295. message.cs_release = 1;
  296. message.next = RT_NULL;
  297. result = device->bus->ops->xfer(device, &message);
  298. if (result < 0)
  299. {
  300. LOG_E("SPI device %s transfer failed", device->parent.parent.name);
  301. goto __exit;
  302. }
  303. result = RT_EOK;
  304. }
  305. else
  306. {
  307. return -RT_EIO;
  308. }
  309. __exit:
  310. spi_unlock(device->bus);
  311. return result;
  312. }
  313. rt_err_t rt_spi_send_then_recv(struct rt_spi_device *device,
  314. const void *send_buf,
  315. rt_size_t send_length,
  316. void *recv_buf,
  317. rt_size_t recv_length)
  318. {
  319. rt_err_t result;
  320. struct rt_spi_message message;
  321. RT_ASSERT(device != RT_NULL);
  322. RT_ASSERT(device->bus != RT_NULL);
  323. result = spi_lock(device->bus);
  324. if (result == RT_EOK)
  325. {
  326. if (device->bus->owner != device)
  327. {
  328. /* not the same owner as current, re-configure SPI bus */
  329. result = device->bus->ops->configure(device, &device->config);
  330. if (result == RT_EOK)
  331. {
  332. /* set SPI bus owner */
  333. device->bus->owner = device;
  334. }
  335. else
  336. {
  337. /* configure SPI bus failed */
  338. LOG_E("SPI device %s configuration failed", device->parent.parent.name);
  339. goto __exit;
  340. }
  341. }
  342. /* send data */
  343. message.send_buf = send_buf;
  344. message.recv_buf = RT_NULL;
  345. message.length = send_length;
  346. message.cs_take = 1;
  347. message.cs_release = 0;
  348. message.next = RT_NULL;
  349. result = device->bus->ops->xfer(device, &message);
  350. if (result < 0)
  351. {
  352. LOG_E("SPI device %s transfer failed", device->parent.parent.name);
  353. goto __exit;
  354. }
  355. /* recv data */
  356. message.send_buf = RT_NULL;
  357. message.recv_buf = recv_buf;
  358. message.length = recv_length;
  359. message.cs_take = 0;
  360. message.cs_release = 1;
  361. message.next = RT_NULL;
  362. result = device->bus->ops->xfer(device, &message);
  363. if (result < 0)
  364. {
  365. LOG_E("SPI device %s transfer failed", device->parent.parent.name);
  366. goto __exit;
  367. }
  368. result = RT_EOK;
  369. }
  370. else
  371. {
  372. return -RT_EIO;
  373. }
  374. __exit:
  375. spi_unlock(device->bus);
  376. return result;
  377. }
  378. rt_ssize_t rt_spi_transfer(struct rt_spi_device *device,
  379. const void *send_buf,
  380. void *recv_buf,
  381. rt_size_t length)
  382. {
  383. rt_ssize_t result;
  384. struct rt_spi_message message;
  385. RT_ASSERT(device != RT_NULL);
  386. RT_ASSERT(device->bus != RT_NULL);
  387. result = spi_lock(device->bus);
  388. if (result == RT_EOK)
  389. {
  390. if (device->bus->owner != device)
  391. {
  392. /* not the same owner as current, re-configure SPI bus */
  393. result = device->bus->ops->configure(device, &device->config);
  394. if (result == RT_EOK)
  395. {
  396. /* set SPI bus owner */
  397. device->bus->owner = device;
  398. }
  399. else
  400. {
  401. /* configure SPI bus failed */
  402. LOG_E("SPI device %s configuration failed", device->parent.parent.name);
  403. goto __exit;
  404. }
  405. }
  406. /* initial message */
  407. message.send_buf = send_buf;
  408. message.recv_buf = recv_buf;
  409. message.length = length;
  410. message.cs_take = 1;
  411. message.cs_release = 1;
  412. message.next = RT_NULL;
  413. /* transfer message */
  414. result = device->bus->ops->xfer(device, &message);
  415. if (result < 0)
  416. {
  417. LOG_E("SPI device %s transfer failed", device->parent.parent.name);
  418. goto __exit;
  419. }
  420. }
  421. else
  422. {
  423. return -RT_EIO;
  424. }
  425. __exit:
  426. spi_unlock(device->bus);
  427. return result;
  428. }
  429. rt_err_t rt_spi_sendrecv8(struct rt_spi_device *device,
  430. rt_uint8_t senddata,
  431. rt_uint8_t *recvdata)
  432. {
  433. rt_ssize_t len = rt_spi_transfer(device, &senddata, recvdata, 1);
  434. if (len < 0)
  435. {
  436. return (rt_err_t)len;
  437. }
  438. else
  439. {
  440. return RT_EOK;
  441. }
  442. }
  443. rt_err_t rt_spi_sendrecv16(struct rt_spi_device *device,
  444. rt_uint16_t senddata,
  445. rt_uint16_t *recvdata)
  446. {
  447. rt_ssize_t len;
  448. rt_uint16_t tmp;
  449. if (device->config.mode & RT_SPI_MSB)
  450. {
  451. tmp = ((senddata & 0xff00) >> 8) | ((senddata & 0x00ff) << 8);
  452. senddata = tmp;
  453. }
  454. len = rt_spi_transfer(device, &senddata, recvdata, 2);
  455. if (len < 0)
  456. {
  457. return (rt_err_t)len;
  458. }
  459. if (device->config.mode & RT_SPI_MSB)
  460. {
  461. tmp = ((*recvdata & 0xff00) >> 8) | ((*recvdata & 0x00ff) << 8);
  462. *recvdata = tmp;
  463. }
  464. return RT_EOK;
  465. }
  466. struct rt_spi_message *rt_spi_transfer_message(struct rt_spi_device *device,
  467. struct rt_spi_message *message)
  468. {
  469. rt_err_t result;
  470. struct rt_spi_message *index;
  471. RT_ASSERT(device != RT_NULL);
  472. /* get first message */
  473. index = message;
  474. if (index == RT_NULL)
  475. return index;
  476. result = spi_lock(device->bus);
  477. if (result != RT_EOK)
  478. {
  479. return index;
  480. }
  481. /* configure SPI bus */
  482. if (device->bus->owner != device)
  483. {
  484. /* not the same owner as current, re-configure SPI bus */
  485. result = device->bus->ops->configure(device, &device->config);
  486. if (result == RT_EOK)
  487. {
  488. /* set SPI bus owner */
  489. device->bus->owner = device;
  490. }
  491. else
  492. {
  493. /* configure SPI bus failed */
  494. goto __exit;
  495. }
  496. }
  497. /* transmit each SPI message */
  498. while (index != RT_NULL)
  499. {
  500. /* transmit SPI message */
  501. result = device->bus->ops->xfer(device, index);
  502. if (result < 0)
  503. {
  504. break;
  505. }
  506. index = index->next;
  507. }
  508. __exit:
  509. /* release bus lock */
  510. spi_unlock(device->bus);
  511. return index;
  512. }
  513. rt_err_t rt_spi_take_bus(struct rt_spi_device *device)
  514. {
  515. rt_err_t result = RT_EOK;
  516. RT_ASSERT(device != RT_NULL);
  517. RT_ASSERT(device->bus != RT_NULL);
  518. result = spi_lock(device->bus);
  519. if (result != RT_EOK)
  520. {
  521. return -RT_EBUSY;
  522. }
  523. /* configure SPI bus */
  524. if (device->bus->owner != device)
  525. {
  526. /* not the same owner as current, re-configure SPI bus */
  527. result = device->bus->ops->configure(device, &device->config);
  528. if (result == RT_EOK)
  529. {
  530. /* set SPI bus owner */
  531. device->bus->owner = device;
  532. }
  533. else
  534. {
  535. /* configure SPI bus failed */
  536. spi_unlock(device->bus);
  537. return result;
  538. }
  539. }
  540. return result;
  541. }
  542. rt_err_t rt_spi_release_bus(struct rt_spi_device *device)
  543. {
  544. RT_ASSERT(device != RT_NULL);
  545. RT_ASSERT(device->bus != RT_NULL);
  546. RT_ASSERT(device->bus->owner == device);
  547. /* release lock */
  548. return spi_unlock(device->bus);
  549. }
  550. rt_err_t rt_spi_take(struct rt_spi_device *device)
  551. {
  552. rt_ssize_t result;
  553. struct rt_spi_message message;
  554. RT_ASSERT(device != RT_NULL);
  555. RT_ASSERT(device->bus != RT_NULL);
  556. rt_memset(&message, 0, sizeof(message));
  557. message.cs_take = 1;
  558. result = device->bus->ops->xfer(device, &message);
  559. if (result < 0)
  560. {
  561. return (rt_err_t)result;
  562. }
  563. return RT_EOK;
  564. }
  565. rt_err_t rt_spi_release(struct rt_spi_device *device)
  566. {
  567. rt_ssize_t result;
  568. struct rt_spi_message message;
  569. RT_ASSERT(device != RT_NULL);
  570. RT_ASSERT(device->bus != RT_NULL);
  571. rt_memset(&message, 0, sizeof(message));
  572. message.cs_release = 1;
  573. result = device->bus->ops->xfer(device, &message);
  574. if (result < 0)
  575. {
  576. return (rt_err_t)result;
  577. }
  578. return RT_EOK;
  579. }