spi.h 11 KB

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