dev_spi.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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 group_Drivers RTTHREAD Driver
  20. * @defgroup group_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 group_Drivers
  86. */
  87. /*!
  88. * @addtogroup group_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. #define RT_SPI_CS_CNT_MAX 16
  124. /**
  125. * @brief SPI message structure
  126. */
  127. struct rt_spi_message
  128. {
  129. const void *send_buf;
  130. void *recv_buf;
  131. rt_size_t length;
  132. struct rt_spi_message *next;
  133. unsigned cs_take : 1;
  134. unsigned cs_release : 1;
  135. };
  136. /**
  137. * @brief SPI configuration structure
  138. */
  139. struct rt_spi_configuration
  140. {
  141. rt_uint8_t mode;
  142. rt_uint8_t data_width;
  143. #ifdef RT_USING_DM
  144. rt_uint8_t data_width_tx;
  145. rt_uint8_t data_width_rx;
  146. #else
  147. rt_uint16_t reserved;
  148. #endif
  149. rt_uint32_t max_hz;
  150. };
  151. struct rt_spi_ops;
  152. /**
  153. * @brief SPI bus structure
  154. */
  155. struct rt_spi_bus
  156. {
  157. struct rt_device parent;
  158. rt_uint8_t mode;
  159. const struct rt_spi_ops *ops;
  160. #ifdef RT_USING_DM
  161. rt_base_t cs_pins[RT_SPI_CS_CNT_MAX];
  162. rt_uint8_t cs_active_vals[RT_SPI_CS_CNT_MAX];
  163. rt_bool_t slave;
  164. int num_chipselect;
  165. #endif /* RT_USING_DM */
  166. struct rt_mutex lock;
  167. struct rt_spi_device *owner;
  168. };
  169. /**
  170. * @brief SPI operators
  171. */
  172. struct rt_spi_ops
  173. {
  174. rt_err_t (*configure)(struct rt_spi_device *device, struct rt_spi_configuration *configuration);
  175. rt_ssize_t (*xfer)(struct rt_spi_device *device, struct rt_spi_message *message);
  176. };
  177. #ifdef RT_USING_DM
  178. /**
  179. * @brief SPI delay info
  180. */
  181. struct rt_spi_delay
  182. {
  183. #define RT_SPI_DELAY_UNIT_USECS 0
  184. #define RT_SPI_DELAY_UNIT_NSECS 1
  185. #define RT_SPI_DELAY_UNIT_SCK 2
  186. rt_uint16_t value;
  187. rt_uint8_t unit;
  188. };
  189. #endif /* RT_USING_DM */
  190. /**
  191. * @brief SPI Virtual BUS, one device must connected to a virtual BUS
  192. */
  193. struct rt_spi_device
  194. {
  195. struct rt_device parent;
  196. struct rt_spi_bus *bus;
  197. #ifdef RT_USING_DM
  198. const char *name;
  199. const struct rt_spi_device_id *id;
  200. const struct rt_ofw_node_id *ofw_id;
  201. rt_uint8_t chip_select[RT_SPI_CS_CNT_MAX];
  202. struct rt_spi_delay cs_setup;
  203. struct rt_spi_delay cs_hold;
  204. struct rt_spi_delay cs_inactive;
  205. #endif
  206. struct rt_spi_configuration config;
  207. rt_base_t cs_pin;
  208. void *user_data;
  209. };
  210. /**
  211. * @brief QSPI message structure
  212. */
  213. struct rt_qspi_message
  214. {
  215. struct rt_spi_message parent;
  216. /* instruction stage */
  217. struct
  218. {
  219. rt_uint8_t content;
  220. rt_uint8_t qspi_lines;
  221. } instruction;
  222. /* address and alternate_bytes stage */
  223. struct
  224. {
  225. rt_uint32_t content;
  226. rt_uint8_t size;
  227. rt_uint8_t qspi_lines;
  228. } address, alternate_bytes;
  229. /* dummy_cycles stage */
  230. rt_uint32_t dummy_cycles;
  231. /* number of lines in qspi data stage, the other configuration items are in parent */
  232. rt_uint8_t qspi_data_lines;
  233. };
  234. /**
  235. * @brief QSPI configuration structure
  236. */
  237. struct rt_qspi_configuration
  238. {
  239. struct rt_spi_configuration parent;
  240. /* The size of medium */
  241. rt_uint32_t medium_size;
  242. /* double data rate mode */
  243. rt_uint8_t ddr_mode;
  244. /* the data lines max width which QSPI bus supported, such as 1, 2, 4 */
  245. rt_uint8_t qspi_dl_width ;
  246. };
  247. /**
  248. * @brief QSPI operators
  249. */
  250. struct rt_qspi_device
  251. {
  252. struct rt_spi_device parent;
  253. struct rt_qspi_configuration config;
  254. void (*enter_qspi_mode)(struct rt_qspi_device *device);
  255. void (*exit_qspi_mode)(struct rt_qspi_device *device);
  256. };
  257. #define SPI_DEVICE(dev) ((struct rt_spi_device *)(dev))
  258. #ifdef RT_USING_DM
  259. struct rt_spi_device_id
  260. {
  261. char name[20];
  262. void *data;
  263. };
  264. struct rt_spi_driver
  265. {
  266. struct rt_driver parent;
  267. const struct rt_spi_device_id *ids;
  268. const struct rt_ofw_node_id *ofw_ids;
  269. rt_err_t (*probe)(struct rt_spi_device *device);
  270. rt_err_t (*remove)(struct rt_spi_device *device);
  271. rt_err_t (*shutdown)(struct rt_spi_device *device);
  272. };
  273. rt_err_t rt_spi_driver_register(struct rt_spi_driver *driver);
  274. rt_err_t rt_spi_device_register(struct rt_spi_device *device);
  275. #define RT_SPI_DRIVER_EXPORT(driver) RT_DRIVER_EXPORT(driver, spi, BUILIN)
  276. #endif /* RT_USING_DM */
  277. /**
  278. * @brief register a SPI bus
  279. *
  280. * @param bus the SPI bus
  281. * @param name the name of SPI bus
  282. * @param ops the operations of SPI bus
  283. *
  284. * @return rt_err_t error code
  285. */
  286. rt_err_t rt_spi_bus_register(struct rt_spi_bus *bus,
  287. const char *name,
  288. const struct rt_spi_ops *ops);
  289. /**
  290. * @brief attach a device on SPI bus
  291. *
  292. * @param device the SPI device
  293. * @param name the name of SPI device
  294. * @param bus_name the name of SPI bus
  295. * @param user_data the user data of SPI device
  296. *
  297. * @return rt_err_t error code
  298. */
  299. rt_err_t rt_spi_bus_attach_device(struct rt_spi_device *device,
  300. const char *name,
  301. const char *bus_name,
  302. void *user_data);
  303. /**
  304. * @brief attach a device on SPI bus with CS pin
  305. *
  306. * @param device the SPI device
  307. * @param name the name of SPI device
  308. * @param bus_name the name of SPI bus
  309. * @param cs_pin the CS pin of SPI device
  310. * @param user_data the user data of SPI device
  311. *
  312. * @return rt_err_t error code
  313. */
  314. rt_err_t rt_spi_bus_attach_device_cspin(struct rt_spi_device *device,
  315. const char *name,
  316. const char *bus_name,
  317. rt_base_t cs_pin,
  318. void *user_data);
  319. /**
  320. * @brief Reconfigure the SPI bus for the specified device.
  321. *
  322. * @param device: Pointer to the SPI device attached to the SPI bus.
  323. * @retval RT_EOK if the SPI device was successfully released and the bus was configured.
  324. * RT_EBUSY if the SPI bus is currently in use; the new configuration will take effect once the device releases the bus.
  325. * Other return values indicate failure to configure the SPI bus due to various reasons.
  326. * @note If the configuration of the SPI device has been updated and requires bus re-initialization,
  327. * call this function directly. This function will reconfigure the SPI bus for the specified device.
  328. * If this is the first time to initialize the SPI device, please call rt_spi_configure or rt_qspi_configure.
  329. * This function is used to reconfigure the SPI bus when the SPI device is already in use.
  330. * For further details, refer to:
  331. * https://github.com/RT-Thread/rt-thread/pull/8528
  332. */
  333. rt_err_t rt_spi_bus_configure(struct rt_spi_device *device);
  334. /**
  335. * @brief This function takes SPI bus.
  336. *
  337. * @param device the SPI device attached to SPI bus
  338. *
  339. * @return RT_EOK on taken SPI bus successfully. others on taken SPI bus failed.
  340. */
  341. rt_err_t rt_spi_take_bus(struct rt_spi_device *device);
  342. /**
  343. * @brief This function releases SPI bus.
  344. *
  345. * @param device the SPI device attached to SPI bus
  346. *
  347. * @return RT_EOK on release SPI bus successfully.
  348. */
  349. rt_err_t rt_spi_release_bus(struct rt_spi_device *device);
  350. /**
  351. * @brief This function take SPI device (takes CS of SPI device).
  352. *
  353. * @param device the SPI device attached to SPI bus
  354. *
  355. * @return RT_EOK on release SPI bus successfully. others on taken SPI bus failed.
  356. */
  357. rt_err_t rt_spi_take(struct rt_spi_device *device);
  358. /**
  359. * @brief This function releases SPI device (releases CS of SPI device).
  360. *
  361. * @param device the SPI device attached to SPI bus
  362. *
  363. * @return RT_EOK on release SPI device successfully.
  364. */
  365. rt_err_t rt_spi_release(struct rt_spi_device *device);
  366. /**
  367. * @brief This function can set configuration on SPI device.
  368. *
  369. * @param device: the SPI device attached to SPI bus
  370. * @param cfg: the configuration pointer.
  371. *
  372. * @retval RT_EOK on release SPI device successfully.
  373. * RT_EBUSY is not an error condition and the configuration will take effect once the device has the bus
  374. * others on taken SPI bus failed.
  375. */
  376. rt_err_t rt_spi_configure(struct rt_spi_device *device,
  377. struct rt_spi_configuration *cfg);
  378. /**
  379. * @brief This function can send data then receive data from SPI device.
  380. *
  381. * @param device the SPI device attached to SPI bus
  382. * @param send_buf the buffer to be transmitted to SPI device.
  383. * @param send_length the number of data to be transmitted.
  384. * @param recv_buf the buffer to be recivied from SPI device.
  385. * @param recv_length the data to be recivied.
  386. *
  387. * @return rt_err_t error code
  388. */
  389. rt_err_t rt_spi_send_then_recv(struct rt_spi_device *device,
  390. const void *send_buf,
  391. rt_size_t send_length,
  392. void *recv_buf,
  393. rt_size_t recv_length);
  394. /**
  395. * @brief This function can send data then send data from SPI device.
  396. *
  397. * @param device the SPI device attached to SPI bus
  398. * @param send_buf1 the buffer to be transmitted to SPI device.
  399. * @param send_length1 the number of data to be transmitted.
  400. * @param send_buf2 the buffer to be transmitted to SPI device.
  401. * @param send_length2 the number of data to be transmitted.
  402. *
  403. * @return the status of transmit.
  404. */
  405. rt_err_t rt_spi_send_then_send(struct rt_spi_device *device,
  406. const void *send_buf1,
  407. rt_size_t send_length1,
  408. const void *send_buf2,
  409. rt_size_t send_length2);
  410. /**
  411. * @brief This function transmits data to SPI device.
  412. *
  413. * @param device the SPI device attached to SPI bus
  414. * @param send_buf the buffer to be transmitted to SPI device.
  415. * @param recv_buf the buffer to save received data from SPI device.
  416. * @param length the length of transmitted data.
  417. *
  418. * @return the actual length of transmitted.
  419. */
  420. rt_ssize_t rt_spi_transfer(struct rt_spi_device *device,
  421. const void *send_buf,
  422. void *recv_buf,
  423. rt_size_t length);
  424. /**
  425. * @brief The SPI device transmits 8 bytes of data
  426. *
  427. * @param device the SPI device attached to SPI bus
  428. * @param senddata send data buffer
  429. * @param recvdata receive data buffer
  430. *
  431. * @return rt_err_t error code
  432. */
  433. rt_err_t rt_spi_sendrecv8(struct rt_spi_device *device,
  434. rt_uint8_t senddata,
  435. rt_uint8_t *recvdata);
  436. /**
  437. * @brief The SPI device transmits 16 bytes of data
  438. *
  439. * @param device the SPI device attached to SPI bus
  440. * @param senddata send data buffer
  441. * @param recvdata receive data buffer
  442. *
  443. * @return rt_err_t error code
  444. */
  445. rt_err_t rt_spi_sendrecv16(struct rt_spi_device *device,
  446. rt_uint16_t senddata,
  447. rt_uint16_t *recvdata);
  448. /**
  449. * @brief This function transfers a message list to the SPI device.
  450. *
  451. * @param device the SPI device attached to SPI bus
  452. * @param message the message list to be transmitted to SPI device
  453. *
  454. * @return RT_NULL if transmits message list successfully,
  455. * SPI message which be transmitted failed.
  456. */
  457. struct rt_spi_message *rt_spi_transfer_message(struct rt_spi_device *device,
  458. struct rt_spi_message *message);
  459. /**
  460. * @brief This function receives data from SPI device.
  461. *
  462. * @param device the SPI device attached to SPI bus
  463. * @param recv_buf the buffer to be recivied from SPI device.
  464. * @param length the data to be recivied.
  465. *
  466. * @return the actual length of received.
  467. */
  468. rt_inline rt_size_t rt_spi_recv(struct rt_spi_device *device,
  469. void *recv_buf,
  470. rt_size_t length)
  471. {
  472. return rt_spi_transfer(device, RT_NULL, recv_buf, length);
  473. }
  474. /**
  475. * @brief This function sends data to SPI device.
  476. *
  477. * @param device the SPI device attached to SPI bus
  478. * @param send_buf the buffer to be transmitted to SPI device.
  479. * @param length the number of data to be transmitted.
  480. *
  481. * @return the actual length of send.
  482. */
  483. rt_inline rt_size_t rt_spi_send(struct rt_spi_device *device,
  484. const void *send_buf,
  485. rt_size_t length)
  486. {
  487. return rt_spi_transfer(device, send_buf, RT_NULL, length);
  488. }
  489. /**
  490. * @brief This function appends a message to the SPI message list.
  491. *
  492. * @param list the SPI message list header.
  493. * @param message the message pointer to be appended to the message list.
  494. */
  495. rt_inline void rt_spi_message_append(struct rt_spi_message *list,
  496. struct rt_spi_message *message)
  497. {
  498. RT_ASSERT(list != RT_NULL);
  499. if (message == RT_NULL)
  500. return; /* not append */
  501. while (list->next != RT_NULL)
  502. {
  503. list = list->next;
  504. }
  505. list->next = message;
  506. message->next = RT_NULL;
  507. }
  508. /**
  509. * @brief This function can set configuration on QSPI device.
  510. *
  511. * @param device the QSPI device attached to QSPI bus.
  512. * @param cfg the configuration pointer.
  513. *
  514. * @return the actual length of transmitted.
  515. */
  516. rt_err_t rt_qspi_configure(struct rt_qspi_device *device, struct rt_qspi_configuration *cfg);
  517. /**
  518. * @brief This function can register a SPI bus for QSPI mode.
  519. *
  520. * @param bus the SPI bus for QSPI mode.
  521. * @param name The name of the spi bus.
  522. * @param ops the SPI bus instance to be registered.
  523. *
  524. * @return the actual length of transmitted.
  525. */
  526. rt_err_t rt_qspi_bus_register(struct rt_spi_bus *bus, const char *name, const struct rt_spi_ops *ops);
  527. /**
  528. * @brief This function transmits data to QSPI device.
  529. *
  530. * @param device the QSPI device attached to QSPI bus.
  531. * @param message the message pointer.
  532. *
  533. * @return the actual length of transmitted.
  534. */
  535. rt_size_t rt_qspi_transfer_message(struct rt_qspi_device *device, struct rt_qspi_message *message);
  536. /**
  537. * @brief This function can send data then receive data from QSPI device
  538. *
  539. * @param device the QSPI device attached to QSPI bus.
  540. * @param send_buf the buffer to be transmitted to QSPI device.
  541. * @param send_length the number of data to be transmitted.
  542. * @param recv_buf the buffer to be recivied from QSPI device.
  543. * @param recv_length the data to be recivied.
  544. *
  545. * @return the status of transmit.
  546. */
  547. 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);
  548. /**
  549. * @brief This function can send data to QSPI device
  550. *
  551. * @param device the QSPI device attached to QSPI bus.
  552. * @param send_buf the buffer to be transmitted to QSPI device.
  553. * @param length the number of data to be transmitted.
  554. *
  555. * @return the status of transmit.
  556. */
  557. rt_err_t rt_qspi_send(struct rt_qspi_device *device, const void *send_buf, rt_size_t length);
  558. #ifdef __cplusplus
  559. }
  560. #endif
  561. /*! @}*/
  562. #endif