spi.h 11 KB

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