dev_spi_core.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  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_attach_device(struct rt_spi_device *device,
  103. const char *name,
  104. const char *bus_name,
  105. void *user_data)
  106. {
  107. return rt_spi_bus_attach_device_cspin(device, name, bus_name, PIN_NONE, user_data);
  108. }
  109. rt_err_t rt_spi_bus_configure(struct rt_spi_device *device)
  110. {
  111. rt_err_t result = -RT_ERROR;
  112. if (device->bus != RT_NULL)
  113. {
  114. result = rt_mutex_take(&(device->bus->lock), RT_WAITING_FOREVER);
  115. if (result == RT_EOK)
  116. {
  117. if (device->bus->owner == device)
  118. {
  119. /* current device is using, re-configure SPI bus */
  120. result = device->bus->ops->configure(device, &device->config);
  121. if (result != RT_EOK)
  122. {
  123. /* configure SPI bus failed */
  124. LOG_E("SPI device %s configuration failed", device->parent.parent.name);
  125. }
  126. }
  127. else
  128. {
  129. /* RT_EBUSY is not an error condition and
  130. * the configuration will take effect once the device has the bus
  131. */
  132. result = -RT_EBUSY;
  133. }
  134. /* release lock */
  135. rt_mutex_release(&(device->bus->lock));
  136. }
  137. }
  138. else
  139. {
  140. result = RT_EOK;
  141. }
  142. return result;
  143. }
  144. rt_err_t rt_spi_configure(struct rt_spi_device *device,
  145. struct rt_spi_configuration *cfg)
  146. {
  147. RT_ASSERT(device != RT_NULL);
  148. RT_ASSERT(cfg != RT_NULL);
  149. /* reset the CS pin */
  150. if (device->cs_pin != PIN_NONE)
  151. {
  152. rt_err_t result = rt_mutex_take(&(device->bus->lock), RT_WAITING_FOREVER);
  153. if (result == RT_EOK)
  154. {
  155. if (cfg->mode & RT_SPI_CS_HIGH)
  156. {
  157. rt_pin_write(device->cs_pin, PIN_LOW);
  158. }
  159. else
  160. {
  161. rt_pin_write(device->cs_pin, PIN_HIGH);
  162. }
  163. rt_mutex_release(&(device->bus->lock));
  164. }
  165. else
  166. {
  167. return result;
  168. }
  169. }
  170. /* If the configurations are the same, we don't need to set again. */
  171. if (device->config.data_width == cfg->data_width &&
  172. device->config.mode == (cfg->mode & RT_SPI_MODE_MASK) &&
  173. device->config.max_hz == cfg->max_hz)
  174. {
  175. return RT_EOK;
  176. }
  177. /* set configuration */
  178. device->config.data_width = cfg->data_width;
  179. device->config.mode = cfg->mode & RT_SPI_MODE_MASK;
  180. device->config.max_hz = cfg->max_hz;
  181. return rt_spi_bus_configure(device);
  182. }
  183. rt_err_t rt_spi_send_then_send(struct rt_spi_device *device,
  184. const void *send_buf1,
  185. rt_size_t send_length1,
  186. const void *send_buf2,
  187. rt_size_t send_length2)
  188. {
  189. rt_err_t result;
  190. struct rt_spi_message message;
  191. RT_ASSERT(device != RT_NULL);
  192. RT_ASSERT(device->bus != RT_NULL);
  193. result = rt_mutex_take(&(device->bus->lock), RT_WAITING_FOREVER);
  194. if (result == RT_EOK)
  195. {
  196. if (device->bus->owner != device)
  197. {
  198. /* not the same owner as current, re-configure SPI bus */
  199. result = device->bus->ops->configure(device, &device->config);
  200. if (result == RT_EOK)
  201. {
  202. /* set SPI bus owner */
  203. device->bus->owner = device;
  204. }
  205. else
  206. {
  207. /* configure SPI bus failed */
  208. LOG_E("SPI device %s configuration failed", device->parent.parent.name);
  209. goto __exit;
  210. }
  211. }
  212. /* send data1 */
  213. message.send_buf = send_buf1;
  214. message.recv_buf = RT_NULL;
  215. message.length = send_length1;
  216. message.cs_take = 1;
  217. message.cs_release = 0;
  218. message.next = RT_NULL;
  219. result = device->bus->ops->xfer(device, &message);
  220. if (result < 0)
  221. {
  222. LOG_E("SPI device %s transfer failed", device->parent.parent.name);
  223. goto __exit;
  224. }
  225. /* send data2 */
  226. message.send_buf = send_buf2;
  227. message.recv_buf = RT_NULL;
  228. message.length = send_length2;
  229. message.cs_take = 0;
  230. message.cs_release = 1;
  231. message.next = RT_NULL;
  232. result = device->bus->ops->xfer(device, &message);
  233. if (result < 0)
  234. {
  235. LOG_E("SPI device %s transfer failed", device->parent.parent.name);
  236. goto __exit;
  237. }
  238. result = RT_EOK;
  239. }
  240. else
  241. {
  242. return -RT_EIO;
  243. }
  244. __exit:
  245. rt_mutex_release(&(device->bus->lock));
  246. return result;
  247. }
  248. rt_err_t rt_spi_send_then_recv(struct rt_spi_device *device,
  249. const void *send_buf,
  250. rt_size_t send_length,
  251. void *recv_buf,
  252. rt_size_t recv_length)
  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 = rt_mutex_take(&(device->bus->lock), RT_WAITING_FOREVER);
  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 data */
  278. message.send_buf = send_buf;
  279. message.recv_buf = RT_NULL;
  280. message.length = send_length;
  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. /* recv data */
  291. message.send_buf = RT_NULL;
  292. message.recv_buf = recv_buf;
  293. message.length = recv_length;
  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. rt_mutex_release(&(device->bus->lock));
  311. return result;
  312. }
  313. rt_ssize_t rt_spi_transfer(struct rt_spi_device *device,
  314. const void *send_buf,
  315. void *recv_buf,
  316. rt_size_t length)
  317. {
  318. rt_ssize_t result;
  319. struct rt_spi_message message;
  320. RT_ASSERT(device != RT_NULL);
  321. RT_ASSERT(device->bus != RT_NULL);
  322. result = rt_mutex_take(&(device->bus->lock), RT_WAITING_FOREVER);
  323. if (result == RT_EOK)
  324. {
  325. if (device->bus->owner != device)
  326. {
  327. /* not the same owner as current, re-configure SPI bus */
  328. result = device->bus->ops->configure(device, &device->config);
  329. if (result == RT_EOK)
  330. {
  331. /* set SPI bus owner */
  332. device->bus->owner = device;
  333. }
  334. else
  335. {
  336. /* configure SPI bus failed */
  337. LOG_E("SPI device %s configuration failed", device->parent.parent.name);
  338. goto __exit;
  339. }
  340. }
  341. /* initial message */
  342. message.send_buf = send_buf;
  343. message.recv_buf = recv_buf;
  344. message.length = length;
  345. message.cs_take = 1;
  346. message.cs_release = 1;
  347. message.next = RT_NULL;
  348. /* transfer message */
  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. }
  356. else
  357. {
  358. return -RT_EIO;
  359. }
  360. __exit:
  361. rt_mutex_release(&(device->bus->lock));
  362. return result;
  363. }
  364. rt_err_t rt_spi_sendrecv8(struct rt_spi_device *device,
  365. rt_uint8_t senddata,
  366. rt_uint8_t *recvdata)
  367. {
  368. rt_ssize_t len = rt_spi_transfer(device, &senddata, recvdata, 1);
  369. if (len < 0)
  370. {
  371. return (rt_err_t)len;
  372. }
  373. else
  374. {
  375. return RT_EOK;
  376. }
  377. }
  378. rt_err_t rt_spi_sendrecv16(struct rt_spi_device *device,
  379. rt_uint16_t senddata,
  380. rt_uint16_t *recvdata)
  381. {
  382. rt_ssize_t len;
  383. rt_uint16_t tmp;
  384. if (device->config.mode & RT_SPI_MSB)
  385. {
  386. tmp = ((senddata & 0xff00) >> 8) | ((senddata & 0x00ff) << 8);
  387. senddata = tmp;
  388. }
  389. len = rt_spi_transfer(device, &senddata, recvdata, 2);
  390. if (len < 0)
  391. {
  392. return (rt_err_t)len;
  393. }
  394. if (device->config.mode & RT_SPI_MSB)
  395. {
  396. tmp = ((*recvdata & 0xff00) >> 8) | ((*recvdata & 0x00ff) << 8);
  397. *recvdata = tmp;
  398. }
  399. return RT_EOK;
  400. }
  401. struct rt_spi_message *rt_spi_transfer_message(struct rt_spi_device *device,
  402. struct rt_spi_message *message)
  403. {
  404. rt_err_t result;
  405. struct rt_spi_message *index;
  406. RT_ASSERT(device != RT_NULL);
  407. /* get first message */
  408. index = message;
  409. if (index == RT_NULL)
  410. return index;
  411. result = rt_mutex_take(&(device->bus->lock), RT_WAITING_FOREVER);
  412. if (result != RT_EOK)
  413. {
  414. return index;
  415. }
  416. /* configure SPI bus */
  417. if (device->bus->owner != device)
  418. {
  419. /* not the same owner as current, re-configure SPI bus */
  420. result = device->bus->ops->configure(device, &device->config);
  421. if (result == RT_EOK)
  422. {
  423. /* set SPI bus owner */
  424. device->bus->owner = device;
  425. }
  426. else
  427. {
  428. /* configure SPI bus failed */
  429. goto __exit;
  430. }
  431. }
  432. /* transmit each SPI message */
  433. while (index != RT_NULL)
  434. {
  435. /* transmit SPI message */
  436. result = device->bus->ops->xfer(device, index);
  437. if (result < 0)
  438. {
  439. break;
  440. }
  441. index = index->next;
  442. }
  443. __exit:
  444. /* release bus lock */
  445. rt_mutex_release(&(device->bus->lock));
  446. return index;
  447. }
  448. rt_err_t rt_spi_take_bus(struct rt_spi_device *device)
  449. {
  450. rt_err_t result = RT_EOK;
  451. RT_ASSERT(device != RT_NULL);
  452. RT_ASSERT(device->bus != RT_NULL);
  453. result = rt_mutex_take(&(device->bus->lock), RT_WAITING_FOREVER);
  454. if (result != RT_EOK)
  455. {
  456. return -RT_EBUSY;
  457. }
  458. /* configure SPI bus */
  459. if (device->bus->owner != device)
  460. {
  461. /* not the same owner as current, re-configure SPI bus */
  462. result = device->bus->ops->configure(device, &device->config);
  463. if (result == RT_EOK)
  464. {
  465. /* set SPI bus owner */
  466. device->bus->owner = device;
  467. }
  468. else
  469. {
  470. /* configure SPI bus failed */
  471. rt_mutex_release(&(device->bus->lock));
  472. return result;
  473. }
  474. }
  475. return result;
  476. }
  477. rt_err_t rt_spi_release_bus(struct rt_spi_device *device)
  478. {
  479. RT_ASSERT(device != RT_NULL);
  480. RT_ASSERT(device->bus != RT_NULL);
  481. RT_ASSERT(device->bus->owner == device);
  482. /* release lock */
  483. return rt_mutex_release(&(device->bus->lock));
  484. }
  485. rt_err_t rt_spi_take(struct rt_spi_device *device)
  486. {
  487. rt_ssize_t result;
  488. struct rt_spi_message message;
  489. RT_ASSERT(device != RT_NULL);
  490. RT_ASSERT(device->bus != RT_NULL);
  491. rt_memset(&message, 0, sizeof(message));
  492. message.cs_take = 1;
  493. result = device->bus->ops->xfer(device, &message);
  494. if (result < 0)
  495. {
  496. return (rt_err_t)result;
  497. }
  498. return RT_EOK;
  499. }
  500. rt_err_t rt_spi_release(struct rt_spi_device *device)
  501. {
  502. rt_ssize_t result;
  503. struct rt_spi_message message;
  504. RT_ASSERT(device != RT_NULL);
  505. RT_ASSERT(device->bus != RT_NULL);
  506. rt_memset(&message, 0, sizeof(message));
  507. message.cs_release = 1;
  508. result = device->bus->ops->xfer(device, &message);
  509. if (result < 0)
  510. {
  511. return (rt_err_t)result;
  512. }
  513. return RT_EOK;
  514. }