dev_spi_core.c 17 KB

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