spi.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. * Copyright (c) 2006-2023, 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 __SPI_H__
  13. #define __SPI_H__
  14. #include <stdlib.h>
  15. #include <rtthread.h>
  16. #include <drivers/pin.h>
  17. #ifdef __cplusplus
  18. extern "C"{
  19. #endif
  20. /**
  21. * At CPOL=0 the base value of the clock is zero
  22. * - For CPHA=0, data are captured on the clock's rising edge (low->high transition)
  23. * and data are propagated on a falling edge (high->low clock transition).
  24. * - For CPHA=1, data are captured on the clock's falling edge and data are
  25. * propagated on a rising edge.
  26. * At CPOL=1 the base value of the clock is one (inversion of CPOL=0)
  27. * - For CPHA=0, data are captured on clock's falling edge and data are propagated
  28. * on a rising edge.
  29. * - For CPHA=1, data are captured on clock's rising edge and data are propagated
  30. * on a falling edge.
  31. */
  32. #define RT_SPI_CPHA (1<<0) /* bit[0]:CPHA, clock phase */
  33. #define RT_SPI_CPOL (1<<1) /* bit[1]:CPOL, clock polarity */
  34. #define RT_SPI_LSB (0<<2) /* bit[2]: 0-LSB */
  35. #define RT_SPI_MSB (1<<2) /* bit[2]: 1-MSB */
  36. #define RT_SPI_MASTER (0<<3) /* SPI master device */
  37. #define RT_SPI_SLAVE (1<<3) /* SPI slave device */
  38. #define RT_SPI_CS_HIGH (1<<4) /* Chipselect active high */
  39. #define RT_SPI_NO_CS (1<<5) /* No chipselect */
  40. #define RT_SPI_3WIRE (1<<6) /* SI/SO pin shared */
  41. #define RT_SPI_READY (1<<7) /* Slave pulls low to pause */
  42. #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)
  43. #define RT_SPI_MODE_0 (0 | 0) /* CPOL = 0, CPHA = 0 */
  44. #define RT_SPI_MODE_1 (0 | RT_SPI_CPHA) /* CPOL = 0, CPHA = 1 */
  45. #define RT_SPI_MODE_2 (RT_SPI_CPOL | 0) /* CPOL = 1, CPHA = 0 */
  46. #define RT_SPI_MODE_3 (RT_SPI_CPOL | RT_SPI_CPHA) /* CPOL = 1, CPHA = 1 */
  47. #define RT_SPI_BUS_MODE_SPI (1<<0)
  48. #define RT_SPI_BUS_MODE_QSPI (1<<1)
  49. /**
  50. * SPI message structure
  51. */
  52. struct rt_spi_message
  53. {
  54. const void *send_buf;
  55. void *recv_buf;
  56. rt_size_t length;
  57. struct rt_spi_message *next;
  58. unsigned cs_take : 1;
  59. unsigned cs_release : 1;
  60. };
  61. /**
  62. * SPI configuration structure
  63. */
  64. struct rt_spi_configuration
  65. {
  66. rt_uint8_t mode;
  67. rt_uint8_t data_width;
  68. rt_uint16_t reserved;
  69. rt_uint32_t max_hz;
  70. };
  71. struct rt_spi_ops;
  72. struct rt_spi_bus
  73. {
  74. struct rt_device parent;
  75. rt_uint8_t mode;
  76. const struct rt_spi_ops *ops;
  77. struct rt_mutex lock;
  78. struct rt_spi_device *owner;
  79. };
  80. /**
  81. * SPI operators
  82. */
  83. struct rt_spi_ops
  84. {
  85. rt_err_t (*configure)(struct rt_spi_device *device, struct rt_spi_configuration *configuration);
  86. rt_ssize_t (*xfer)(struct rt_spi_device *device, struct rt_spi_message *message);
  87. };
  88. /**
  89. * SPI Virtual BUS, one device must connected to a virtual BUS
  90. */
  91. struct rt_spi_device
  92. {
  93. struct rt_device parent;
  94. struct rt_spi_bus *bus;
  95. struct rt_spi_configuration config;
  96. rt_base_t cs_pin;
  97. void *user_data;
  98. };
  99. struct rt_qspi_message
  100. {
  101. struct rt_spi_message parent;
  102. /* instruction stage */
  103. struct
  104. {
  105. rt_uint8_t content;
  106. rt_uint8_t qspi_lines;
  107. } instruction;
  108. /* address and alternate_bytes stage */
  109. struct
  110. {
  111. rt_uint32_t content;
  112. rt_uint8_t size;
  113. rt_uint8_t qspi_lines;
  114. } address, alternate_bytes;
  115. /* dummy_cycles stage */
  116. rt_uint32_t dummy_cycles;
  117. /* number of lines in qspi data stage, the other configuration items are in parent */
  118. rt_uint8_t qspi_data_lines;
  119. };
  120. struct rt_qspi_configuration
  121. {
  122. struct rt_spi_configuration parent;
  123. /* The size of medium */
  124. rt_uint32_t medium_size;
  125. /* double data rate mode */
  126. rt_uint8_t ddr_mode;
  127. /* the data lines max width which QSPI bus supported, such as 1, 2, 4 */
  128. rt_uint8_t qspi_dl_width ;
  129. };
  130. struct rt_qspi_device
  131. {
  132. struct rt_spi_device parent;
  133. struct rt_qspi_configuration config;
  134. void (*enter_qspi_mode)(struct rt_qspi_device *device);
  135. void (*exit_qspi_mode)(struct rt_qspi_device *device);
  136. };
  137. #define SPI_DEVICE(dev) ((struct rt_spi_device *)(dev))
  138. /* register a SPI bus */
  139. rt_err_t rt_spi_bus_register(struct rt_spi_bus *bus,
  140. const char *name,
  141. const struct rt_spi_ops *ops);
  142. /* attach a device on SPI bus */
  143. rt_err_t rt_spi_bus_attach_device(struct rt_spi_device *device,
  144. const char *name,
  145. const char *bus_name,
  146. void *user_data);
  147. /* attach a device on SPI bus with CS pin */
  148. rt_err_t rt_spi_bus_attach_device_cspin(struct rt_spi_device *device,
  149. const char *name,
  150. const char *bus_name,
  151. rt_base_t cs_pin,
  152. void *user_data);
  153. /* re-configure SPI bus */
  154. rt_err_t rt_spi_bus_configure(struct rt_spi_device *device);
  155. /**
  156. * This function takes SPI bus.
  157. *
  158. * @param device the SPI device attached to SPI bus
  159. *
  160. * @return RT_EOK on taken SPI bus successfully. others on taken SPI bus failed.
  161. */
  162. rt_err_t rt_spi_take_bus(struct rt_spi_device *device);
  163. /**
  164. * This function releases SPI bus.
  165. *
  166. * @param device the SPI device attached to SPI bus
  167. *
  168. * @return RT_EOK on release SPI bus successfully.
  169. */
  170. rt_err_t rt_spi_release_bus(struct rt_spi_device *device);
  171. /**
  172. * This function take SPI device (takes CS of SPI device).
  173. *
  174. * @param device the SPI device attached to SPI bus
  175. *
  176. * @return RT_EOK on release SPI bus successfully. others on taken SPI bus failed.
  177. */
  178. rt_err_t rt_spi_take(struct rt_spi_device *device);
  179. /**
  180. * This function releases SPI device (releases CS of SPI device).
  181. *
  182. * @param device the SPI device attached to SPI bus
  183. *
  184. * @return RT_EOK on release SPI device successfully.
  185. */
  186. rt_err_t rt_spi_release(struct rt_spi_device *device);
  187. /* set configuration on SPI device */
  188. rt_err_t rt_spi_configure(struct rt_spi_device *device,
  189. struct rt_spi_configuration *cfg);
  190. /* send data then receive data from SPI device */
  191. rt_err_t rt_spi_send_then_recv(struct rt_spi_device *device,
  192. const void *send_buf,
  193. rt_size_t send_length,
  194. void *recv_buf,
  195. rt_size_t recv_length);
  196. rt_err_t rt_spi_send_then_send(struct rt_spi_device *device,
  197. const void *send_buf1,
  198. rt_size_t send_length1,
  199. const void *send_buf2,
  200. rt_size_t send_length2);
  201. /**
  202. * This function transmits data to SPI device.
  203. *
  204. * @param device the SPI device attached to SPI bus
  205. * @param send_buf the buffer to be transmitted to SPI device.
  206. * @param recv_buf the buffer to save received data from SPI device.
  207. * @param length the length of transmitted data.
  208. *
  209. * @return the actual length of transmitted.
  210. */
  211. rt_ssize_t rt_spi_transfer(struct rt_spi_device *device,
  212. const void *send_buf,
  213. void *recv_buf,
  214. rt_size_t length);
  215. rt_err_t rt_spi_sendrecv8(struct rt_spi_device *device,
  216. rt_uint8_t senddata,
  217. rt_uint8_t *recvdata);
  218. rt_err_t rt_spi_sendrecv16(struct rt_spi_device *device,
  219. rt_uint16_t senddata,
  220. rt_uint16_t *recvdata);
  221. /**
  222. * This function transfers a message list to the SPI device.
  223. *
  224. * @param device the SPI device attached to SPI bus
  225. * @param message the message list to be transmitted to SPI device
  226. *
  227. * @return RT_NULL if transmits message list successfully,
  228. * SPI message which be transmitted failed.
  229. */
  230. struct rt_spi_message *rt_spi_transfer_message(struct rt_spi_device *device,
  231. struct rt_spi_message *message);
  232. rt_inline rt_size_t rt_spi_recv(struct rt_spi_device *device,
  233. void *recv_buf,
  234. rt_size_t length)
  235. {
  236. return rt_spi_transfer(device, RT_NULL, recv_buf, length);
  237. }
  238. rt_inline rt_size_t rt_spi_send(struct rt_spi_device *device,
  239. const void *send_buf,
  240. rt_size_t length)
  241. {
  242. return rt_spi_transfer(device, send_buf, RT_NULL, length);
  243. }
  244. /**
  245. * This function appends a message to the SPI message list.
  246. *
  247. * @param list the SPI message list header.
  248. * @param message the message pointer to be appended to the message list.
  249. */
  250. rt_inline void rt_spi_message_append(struct rt_spi_message *list,
  251. struct rt_spi_message *message)
  252. {
  253. RT_ASSERT(list != RT_NULL);
  254. if (message == RT_NULL)
  255. return; /* not append */
  256. while (list->next != RT_NULL)
  257. {
  258. list = list->next;
  259. }
  260. list->next = message;
  261. message->next = RT_NULL;
  262. }
  263. /**
  264. * This function can set configuration on QSPI device.
  265. *
  266. * @param device the QSPI device attached to QSPI bus.
  267. * @param cfg the configuration pointer.
  268. *
  269. * @return the actual length of transmitted.
  270. */
  271. rt_err_t rt_qspi_configure(struct rt_qspi_device *device, struct rt_qspi_configuration *cfg);
  272. /**
  273. * This function can register a SPI bus for QSPI mode.
  274. *
  275. * @param bus the SPI bus for QSPI mode.
  276. * @param name The name of the spi bus.
  277. * @param ops the SPI bus instance to be registered.
  278. *
  279. * @return the actual length of transmitted.
  280. */
  281. rt_err_t rt_qspi_bus_register(struct rt_spi_bus *bus, const char *name, const struct rt_spi_ops *ops);
  282. /**
  283. * This function transmits data to QSPI device.
  284. *
  285. * @param device the QSPI device attached to QSPI bus.
  286. * @param message the message pointer.
  287. *
  288. * @return the actual length of transmitted.
  289. */
  290. rt_size_t rt_qspi_transfer_message(struct rt_qspi_device *device, struct rt_qspi_message *message);
  291. /**
  292. * This function can send data then receive data from QSPI device
  293. *
  294. * @param device the QSPI device attached to QSPI bus.
  295. * @param send_buf the buffer to be transmitted to QSPI device.
  296. * @param send_length the number of data to be transmitted.
  297. * @param recv_buf the buffer to be recivied from QSPI device.
  298. * @param recv_length the data to be recivied.
  299. *
  300. * @return the status of transmit.
  301. */
  302. 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);
  303. /**
  304. * This function can send data to QSPI device
  305. *
  306. * @param device the QSPI device attached to QSPI bus.
  307. * @param send_buf the buffer to be transmitted to QSPI device.
  308. * @param send_length the number of data to be transmitted.
  309. *
  310. * @return the status of transmit.
  311. */
  312. rt_err_t rt_qspi_send(struct rt_qspi_device *device, const void *send_buf, rt_size_t length);
  313. #ifdef __cplusplus
  314. }
  315. #endif
  316. #endif