dev_spi.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. /*
  2. * Copyright (c) 2006-2024 RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2012-11-23 Bernard Add extern "C"
  9. * 2020-06-13 armink fix the 3 wires issue
  10. * 2022-09-01 liYony fix api rt_spi_sendrecv16 about MSB and LSB bug
  11. */
  12. #ifndef __DEV_SPI_H__
  13. #define __DEV_SPI_H__
  14. #include <stdlib.h>
  15. #include <rtthread.h>
  16. #include <drivers/dev_pin.h>
  17. #include <drivers/core/driver.h>
  18. /**
  19. * @addtogroup Drivers RTTHREAD Driver
  20. * @defgroup SPI SPI
  21. *
  22. * @brief SPI driver api
  23. *
  24. * <b>Example</b>
  25. * @code {.c}
  26. * #include <rtthread.h>
  27. * #include <rtdevice.h>
  28. *
  29. * #define W25Q_SPI_DEVICE_NAME "qspi10"
  30. *
  31. * static void spi_w25q_sample(int argc, char *argv[])
  32. * {
  33. * struct rt_spi_device *spi_dev_w25q;
  34. * char name[RT_NAME_MAX];
  35. * rt_uint8_t w25x_read_id = 0x90;
  36. * rt_uint8_t id[5] = {0};
  37. *
  38. * if (argc == 2)
  39. * {
  40. * rt_strncpy(name, argv[1], RT_NAME_MAX);
  41. * }
  42. * else
  43. * {
  44. * rt_strncpy(name, W25Q_SPI_DEVICE_NAME, RT_NAME_MAX);
  45. * }
  46. *
  47. * // 查找 spi 设备获取设备句柄
  48. * spi_dev_w25q = (struct rt_spi_device *)rt_device_find(name);
  49. * if (!spi_dev_w25q)
  50. * {
  51. * rt_kprintf("spi sample run failed! can't find %s device!\n", name);
  52. * }
  53. * else
  54. * {
  55. * // 方式1:使用 rt_spi_send_then_recv()发送命令读取ID
  56. * rt_spi_send_then_recv(spi_dev_w25q, &w25x_read_id, 1, id, 5);
  57. * rt_kprintf("use rt_spi_send_then_recv() read w25q ID is:%x%x\n", id[3], id[4]);
  58. *
  59. * // 方式2:使用 rt_spi_transfer_message()发送命令读取ID
  60. * struct rt_spi_message msg1, msg2;
  61. *
  62. * msg1.send_buf = &w25x_read_id;
  63. * msg1.recv_buf = RT_NULL;
  64. * msg1.length = 1;
  65. * msg1.cs_take = 1;
  66. * msg1.cs_release = 0;
  67. * msg1.next = &msg2;
  68. *
  69. * msg2.send_buf = RT_NULL;
  70. * msg2.recv_buf = id;
  71. * msg2.length = 5;
  72. * msg2.cs_take = 0;
  73. * msg2.cs_release = 1;
  74. * msg2.next = RT_NULL;
  75. *
  76. * rt_spi_transfer_message(spi_dev_w25q, &msg1);
  77. * rt_kprintf("use rt_spi_transfer_message() read w25q ID is:%x%x\n", id[3], id[4]);
  78. *
  79. * }
  80. * }
  81. * // 导出到 msh 命令列表中
  82. * MSH_CMD_EXPORT(spi_w25q_sample, spi w25q sample);
  83. * @endcode
  84. *
  85. * @ingroup Drivers
  86. */
  87. /*!
  88. * @addtogroup SPI
  89. * @{
  90. */
  91. #ifdef __cplusplus
  92. extern "C"{
  93. #endif
  94. /**
  95. * At CPOL=0 the base value of the clock is zero
  96. * - For CPHA=0, data are captured on the clock's rising edge (low->high transition)
  97. * and data are propagated on a falling edge (high->low clock transition).
  98. * - For CPHA=1, data are captured on the clock's falling edge and data are
  99. * propagated on a rising edge.
  100. * At CPOL=1 the base value of the clock is one (inversion of CPOL=0)
  101. * - For CPHA=0, data are captured on clock's falling edge and data are propagated
  102. * on a rising edge.
  103. * - For CPHA=1, data are captured on clock's rising edge and data are propagated
  104. * on a falling edge.
  105. */
  106. #define RT_SPI_CPHA (1<<0) /*!< bit[0]:CPHA, clock phase */
  107. #define RT_SPI_CPOL (1<<1) /*!< bit[1]:CPOL, clock polarity */
  108. #define RT_SPI_LSB (0<<2) /*!< bit[2]: 0-LSB */
  109. #define RT_SPI_MSB (1<<2) /*!< bit[2]: 1-MSB */
  110. #define RT_SPI_MASTER (0<<3) /*!< SPI master device */
  111. #define RT_SPI_SLAVE (1<<3) /*!< SPI slave device */
  112. #define RT_SPI_CS_HIGH (1<<4) /*!< Chipselect active high */
  113. #define RT_SPI_NO_CS (1<<5) /*!< No chipselect */
  114. #define RT_SPI_3WIRE (1<<6) /*!< SI/SO pin shared */
  115. #define RT_SPI_READY (1<<7) /*!< Slave pulls low to pause */
  116. #define RT_SPI_MODE_MASK (RT_SPI_CPHA | RT_SPI_CPOL | RT_SPI_MSB | RT_SPI_SLAVE | RT_SPI_CS_HIGH | RT_SPI_NO_CS | RT_SPI_3WIRE | RT_SPI_READY)
  117. #define RT_SPI_MODE_0 (0 | 0) /*!< CPOL = 0, CPHA = 0 */
  118. #define RT_SPI_MODE_1 (0 | RT_SPI_CPHA) /*!< CPOL = 0, CPHA = 1 */
  119. #define RT_SPI_MODE_2 (RT_SPI_CPOL | 0) /*!< CPOL = 1, CPHA = 0 */
  120. #define RT_SPI_MODE_3 (RT_SPI_CPOL | RT_SPI_CPHA) /*!< CPOL = 1, CPHA = 1 */
  121. #define RT_SPI_BUS_MODE_SPI (1<<0)
  122. #define RT_SPI_BUS_MODE_QSPI (1<<1)
  123. /**
  124. * @brief SPI message structure
  125. */
  126. struct rt_spi_message
  127. {
  128. const void *send_buf;
  129. void *recv_buf;
  130. rt_size_t length;
  131. struct rt_spi_message *next;
  132. unsigned cs_take : 1;
  133. unsigned cs_release : 1;
  134. };
  135. /**
  136. * @brief SPI configuration structure
  137. */
  138. struct rt_spi_configuration
  139. {
  140. rt_uint8_t mode;
  141. rt_uint8_t data_width;
  142. #ifdef RT_USING_DM
  143. rt_uint8_t data_width_tx;
  144. rt_uint8_t data_width_rx;
  145. #else
  146. rt_uint16_t reserved;
  147. #endif
  148. rt_uint32_t max_hz;
  149. };
  150. struct rt_spi_ops;
  151. /**
  152. * @brief SPI bus structure
  153. */
  154. struct rt_spi_bus
  155. {
  156. struct rt_device parent;
  157. rt_uint8_t mode;
  158. const struct rt_spi_ops *ops;
  159. #ifdef RT_USING_DM
  160. rt_base_t *pins;
  161. rt_bool_t slave;
  162. int num_chipselect;
  163. #endif /* RT_USING_DM */
  164. struct rt_mutex lock;
  165. struct rt_spi_device *owner;
  166. };
  167. /**
  168. * @brief SPI operators
  169. */
  170. struct rt_spi_ops
  171. {
  172. rt_err_t (*configure)(struct rt_spi_device *device, struct rt_spi_configuration *configuration);
  173. rt_ssize_t (*xfer)(struct rt_spi_device *device, struct rt_spi_message *message);
  174. };
  175. #ifdef RT_USING_DM
  176. /**
  177. * @brief SPI delay info
  178. */
  179. struct rt_spi_delay
  180. {
  181. #define RT_SPI_DELAY_UNIT_USECS 0
  182. #define RT_SPI_DELAY_UNIT_NSECS 1
  183. #define RT_SPI_DELAY_UNIT_SCK 2
  184. rt_uint16_t value;
  185. rt_uint8_t unit;
  186. };
  187. #endif /* RT_USING_DM */
  188. /**
  189. * @brief SPI Virtual BUS, one device must connected to a virtual BUS
  190. */
  191. struct rt_spi_device
  192. {
  193. struct rt_device parent;
  194. struct rt_spi_bus *bus;
  195. #ifdef RT_USING_DM
  196. const char *name;
  197. const struct rt_spi_device_id *id;
  198. const struct rt_ofw_node_id *ofw_id;
  199. rt_uint8_t chip_select;
  200. struct rt_spi_delay cs_setup;
  201. struct rt_spi_delay cs_hold;
  202. struct rt_spi_delay cs_inactive;
  203. #endif
  204. struct rt_spi_configuration config;
  205. rt_base_t cs_pin;
  206. void *user_data;
  207. };
  208. /**
  209. * @brief QSPI message structure
  210. */
  211. struct rt_qspi_message
  212. {
  213. struct rt_spi_message parent;
  214. /* instruction stage */
  215. struct
  216. {
  217. rt_uint8_t content;
  218. rt_uint8_t qspi_lines;
  219. } instruction;
  220. /* address and alternate_bytes stage */
  221. struct
  222. {
  223. rt_uint32_t content;
  224. rt_uint8_t size;
  225. rt_uint8_t qspi_lines;
  226. } address, alternate_bytes;
  227. /* dummy_cycles stage */
  228. rt_uint32_t dummy_cycles;
  229. /* number of lines in qspi data stage, the other configuration items are in parent */
  230. rt_uint8_t qspi_data_lines;
  231. };
  232. /**
  233. * @brief QSPI configuration structure
  234. */
  235. struct rt_qspi_configuration
  236. {
  237. struct rt_spi_configuration parent;
  238. /* The size of medium */
  239. rt_uint32_t medium_size;
  240. /* double data rate mode */
  241. rt_uint8_t ddr_mode;
  242. /* the data lines max width which QSPI bus supported, such as 1, 2, 4 */
  243. rt_uint8_t qspi_dl_width ;
  244. };
  245. /**
  246. * @brief QSPI operators
  247. */
  248. struct rt_qspi_device
  249. {
  250. struct rt_spi_device parent;
  251. struct rt_qspi_configuration config;
  252. void (*enter_qspi_mode)(struct rt_qspi_device *device);
  253. void (*exit_qspi_mode)(struct rt_qspi_device *device);
  254. };
  255. #define SPI_DEVICE(dev) ((struct rt_spi_device *)(dev))
  256. #ifdef RT_USING_DM
  257. struct rt_spi_device_id
  258. {
  259. char name[20];
  260. void *data;
  261. };
  262. struct rt_spi_driver
  263. {
  264. struct rt_driver parent;
  265. const struct rt_spi_device_id *ids;
  266. const struct rt_ofw_node_id *ofw_ids;
  267. rt_err_t (*probe)(struct rt_spi_device *device);
  268. rt_err_t (*remove)(struct rt_spi_device *device);
  269. rt_err_t (*shutdown)(struct rt_spi_device *device);
  270. };
  271. rt_err_t rt_spi_driver_register(struct rt_spi_driver *driver);
  272. rt_err_t rt_spi_device_register(struct rt_spi_device *device);
  273. #define RT_SPI_DRIVER_EXPORT(driver) RT_DRIVER_EXPORT(driver, spi, BUILIN)
  274. #endif /* RT_USING_DM */
  275. /**
  276. * @brief register a SPI bus
  277. *
  278. * @param bus the SPI bus
  279. * @param name the name of SPI bus
  280. * @param ops the operations of SPI bus
  281. *
  282. * @return rt_err_t error code
  283. */
  284. rt_err_t rt_spi_bus_register(struct rt_spi_bus *bus,
  285. const char *name,
  286. const struct rt_spi_ops *ops);
  287. /**
  288. * @brief attach a device on SPI bus
  289. *
  290. * @param device the SPI device
  291. * @param name the name of SPI device
  292. * @param bus_name the name of SPI bus
  293. * @param user_data the user data of SPI device
  294. *
  295. * @return rt_err_t error code
  296. */
  297. rt_err_t rt_spi_bus_attach_device(struct rt_spi_device *device,
  298. const char *name,
  299. const char *bus_name,
  300. void *user_data);
  301. /**
  302. * @brief attach a device on SPI bus with CS pin
  303. *
  304. * @param device the SPI device
  305. * @param name the name of SPI device
  306. * @param bus_name the name of SPI bus
  307. * @param cs_pin the CS pin of SPI device
  308. * @param user_data the user data of SPI device
  309. *
  310. * @return rt_err_t error code
  311. */
  312. rt_err_t rt_spi_bus_attach_device_cspin(struct rt_spi_device *device,
  313. const char *name,
  314. const char *bus_name,
  315. rt_base_t cs_pin,
  316. void *user_data);
  317. /**
  318. * @brief Reconfigure the SPI bus for the specified device.
  319. *
  320. * @param device: Pointer to the SPI device attached to the SPI bus.
  321. * @retval RT_EOK if the SPI device was successfully released and the bus was configured.
  322. * RT_EBUSY if the SPI bus is currently in use; the new configuration will take effect once the device releases the bus.
  323. * Other return values indicate failure to configure the SPI bus due to various reasons.
  324. * @note If the configuration of the SPI device has been updated and requires bus re-initialization,
  325. * call this function directly. This function will reconfigure the SPI bus for the specified device.
  326. * If this is the first time to initialize the SPI device, please call rt_spi_configure or rt_qspi_configure.
  327. * This function is used to reconfigure the SPI bus when the SPI device is already in use.
  328. * For further details, refer to:
  329. * https://github.com/RT-Thread/rt-thread/pull/8528
  330. */
  331. rt_err_t rt_spi_bus_configure(struct rt_spi_device *device);
  332. /**
  333. * @brief This function takes SPI bus.
  334. *
  335. * @param device the SPI device attached to SPI bus
  336. *
  337. * @return RT_EOK on taken SPI bus successfully. others on taken SPI bus failed.
  338. */
  339. rt_err_t rt_spi_take_bus(struct rt_spi_device *device);
  340. /**
  341. * @brief This function releases SPI bus.
  342. *
  343. * @param device the SPI device attached to SPI bus
  344. *
  345. * @return RT_EOK on release SPI bus successfully.
  346. */
  347. rt_err_t rt_spi_release_bus(struct rt_spi_device *device);
  348. /**
  349. * @brief This function take SPI device (takes CS of SPI device).
  350. *
  351. * @param device the SPI device attached to SPI bus
  352. *
  353. * @return RT_EOK on release SPI bus successfully. others on taken SPI bus failed.
  354. */
  355. rt_err_t rt_spi_take(struct rt_spi_device *device);
  356. /**
  357. * @brief This function releases SPI device (releases CS of SPI device).
  358. *
  359. * @param device the SPI device attached to SPI bus
  360. *
  361. * @return RT_EOK on release SPI device successfully.
  362. */
  363. rt_err_t rt_spi_release(struct rt_spi_device *device);
  364. /**
  365. * @brief This function can set configuration on SPI device.
  366. *
  367. * @param device: the SPI device attached to SPI bus
  368. * @param cfg: the configuration pointer.
  369. *
  370. * @retval RT_EOK on release SPI device successfully.
  371. * RT_EBUSY is not an error condition and the configuration will take effect once the device has the bus
  372. * others on taken SPI bus failed.
  373. */
  374. rt_err_t rt_spi_configure(struct rt_spi_device *device,
  375. struct rt_spi_configuration *cfg);
  376. /**
  377. * @brief This function can send data then receive data from SPI device.
  378. *
  379. * @param device the SPI device attached to SPI bus
  380. * @param send_buf the buffer to be transmitted to SPI device.
  381. * @param send_length the number of data to be transmitted.
  382. * @param recv_buf the buffer to be recivied from SPI device.
  383. * @param recv_length the data to be recivied.
  384. *
  385. * @return rt_err_t error code
  386. */
  387. rt_err_t rt_spi_send_then_recv(struct rt_spi_device *device,
  388. const void *send_buf,
  389. rt_size_t send_length,
  390. void *recv_buf,
  391. rt_size_t recv_length);
  392. /**
  393. * @brief This function can send data then send data from SPI device.
  394. *
  395. * @param device the SPI device attached to SPI bus
  396. * @param send_buf1 the buffer to be transmitted to SPI device.
  397. * @param send_length1 the number of data to be transmitted.
  398. * @param send_buf2 the buffer to be transmitted to SPI device.
  399. * @param send_length2 the number of data to be transmitted.
  400. *
  401. * @return the status of transmit.
  402. */
  403. rt_err_t rt_spi_send_then_send(struct rt_spi_device *device,
  404. const void *send_buf1,
  405. rt_size_t send_length1,
  406. const void *send_buf2,
  407. rt_size_t send_length2);
  408. /**
  409. * @brief This function transmits data to SPI device.
  410. *
  411. * @param device the SPI device attached to SPI bus
  412. * @param send_buf the buffer to be transmitted to SPI device.
  413. * @param recv_buf the buffer to save received data from SPI device.
  414. * @param length the length of transmitted data.
  415. *
  416. * @return the actual length of transmitted.
  417. */
  418. rt_ssize_t rt_spi_transfer(struct rt_spi_device *device,
  419. const void *send_buf,
  420. void *recv_buf,
  421. rt_size_t length);
  422. /**
  423. * @brief The SPI device transmits 8 bytes of data
  424. *
  425. * @param device the SPI device attached to SPI bus
  426. * @param senddata send data buffer
  427. * @param recvdata receive data buffer
  428. *
  429. * @return rt_err_t error code
  430. */
  431. rt_err_t rt_spi_sendrecv8(struct rt_spi_device *device,
  432. rt_uint8_t senddata,
  433. rt_uint8_t *recvdata);
  434. /**
  435. * @brief The SPI device transmits 16 bytes of data
  436. *
  437. * @param device the SPI device attached to SPI bus
  438. * @param senddata send data buffer
  439. * @param recvdata receive data buffer
  440. *
  441. * @return rt_err_t error code
  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. * @brief This function transfers a message list to the SPI device.
  448. *
  449. * @param device the SPI device attached to SPI bus
  450. * @param message the message list to be transmitted to SPI device
  451. *
  452. * @return RT_NULL if transmits message list successfully,
  453. * SPI message which be transmitted failed.
  454. */
  455. struct rt_spi_message *rt_spi_transfer_message(struct rt_spi_device *device,
  456. struct rt_spi_message *message);
  457. /**
  458. * @brief This function receives data from SPI device.
  459. *
  460. * @param device the SPI device attached to SPI bus
  461. * @param recv_buf the buffer to be recivied from SPI device.
  462. * @param length the data to be recivied.
  463. *
  464. * @return the actual length of received.
  465. */
  466. rt_inline rt_size_t rt_spi_recv(struct rt_spi_device *device,
  467. void *recv_buf,
  468. rt_size_t length)
  469. {
  470. return rt_spi_transfer(device, RT_NULL, recv_buf, length);
  471. }
  472. /**
  473. * @brief This function sends data to SPI device.
  474. *
  475. * @param device the SPI device attached to SPI bus
  476. * @param send_buf the buffer to be transmitted to SPI device.
  477. * @param length the number of data to be transmitted.
  478. *
  479. * @return the actual length of send.
  480. */
  481. rt_inline rt_size_t rt_spi_send(struct rt_spi_device *device,
  482. const void *send_buf,
  483. rt_size_t length)
  484. {
  485. return rt_spi_transfer(device, send_buf, RT_NULL, length);
  486. }
  487. /**
  488. * @brief This function appends a message to the SPI message list.
  489. *
  490. * @param list the SPI message list header.
  491. * @param message the message pointer to be appended to the message list.
  492. */
  493. rt_inline void rt_spi_message_append(struct rt_spi_message *list,
  494. struct rt_spi_message *message)
  495. {
  496. RT_ASSERT(list != RT_NULL);
  497. if (message == RT_NULL)
  498. return; /* not append */
  499. while (list->next != RT_NULL)
  500. {
  501. list = list->next;
  502. }
  503. list->next = message;
  504. message->next = RT_NULL;
  505. }
  506. /**
  507. * @brief This function can set configuration on QSPI device.
  508. *
  509. * @param device the QSPI device attached to QSPI bus.
  510. * @param cfg the configuration pointer.
  511. *
  512. * @return the actual length of transmitted.
  513. */
  514. rt_err_t rt_qspi_configure(struct rt_qspi_device *device, struct rt_qspi_configuration *cfg);
  515. /**
  516. * @brief This function can register a SPI bus for QSPI mode.
  517. *
  518. * @param bus the SPI bus for QSPI mode.
  519. * @param name The name of the spi bus.
  520. * @param ops the SPI bus instance to be registered.
  521. *
  522. * @return the actual length of transmitted.
  523. */
  524. rt_err_t rt_qspi_bus_register(struct rt_spi_bus *bus, const char *name, const struct rt_spi_ops *ops);
  525. /**
  526. * @brief This function transmits data to QSPI device.
  527. *
  528. * @param device the QSPI device attached to QSPI bus.
  529. * @param message the message pointer.
  530. *
  531. * @return the actual length of transmitted.
  532. */
  533. rt_size_t rt_qspi_transfer_message(struct rt_qspi_device *device, struct rt_qspi_message *message);
  534. /**
  535. * @brief This function can send data then receive data from QSPI device
  536. *
  537. * @param device the QSPI device attached to QSPI bus.
  538. * @param send_buf the buffer to be transmitted to QSPI device.
  539. * @param send_length the number of data to be transmitted.
  540. * @param recv_buf the buffer to be recivied from QSPI device.
  541. * @param recv_length the data to be recivied.
  542. *
  543. * @return the status of transmit.
  544. */
  545. rt_err_t rt_qspi_send_then_recv(struct rt_qspi_device *device, const void *send_buf, rt_size_t send_length,void *recv_buf, rt_size_t recv_length);
  546. /**
  547. * @brief This function can send data to QSPI device
  548. *
  549. * @param device the QSPI device attached to QSPI bus.
  550. * @param send_buf the buffer to be transmitted to QSPI device.
  551. * @param length the number of data to be transmitted.
  552. *
  553. * @return the status of transmit.
  554. */
  555. rt_err_t rt_qspi_send(struct rt_qspi_device *device, const void *send_buf, rt_size_t length);
  556. #ifdef __cplusplus
  557. }
  558. #endif
  559. /*! @}*/
  560. #endif