spi.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*****************************************************************************
  2. * Copyright (c) 2019, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/
  27. /**
  28. * @file spi.h
  29. * @author Nations
  30. * @version v1.0.0
  31. *
  32. * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
  33. */
  34. #ifndef __SPI_H__
  35. #define __SPI_H__
  36. #include <stdlib.h>
  37. #include <rtthread.h>
  38. #ifdef __cplusplus
  39. extern "C"{
  40. #endif
  41. #define RT_SPI_CPHA (1<<0) /* bit[0]:CPHA, clock phase */
  42. #define RT_SPI_CPOL (1<<1) /* bit[1]:CPOL, clock polarity */
  43. /**
  44. * At CPOL=0 the base value of the clock is zero
  45. * - For CPHA=0, data are captured on the clock's rising edge (low->high transition)
  46. * and data are propagated on a falling edge (high->low clock transition).
  47. * - For CPHA=1, data are captured on the clock's falling edge and data are
  48. * propagated on a rising edge.
  49. * At CPOL=1 the base value of the clock is one (inversion of CPOL=0)
  50. * - For CPHA=0, data are captured on clock's falling edge and data are propagated
  51. * on a rising edge.
  52. * - For CPHA=1, data are captured on clock's rising edge and data are propagated
  53. * on a falling edge.
  54. */
  55. #define RT_SPI_LSB (0<<2) /* bit[2]: 0-LSB */
  56. #define RT_SPI_MSB (1<<2) /* bit[2]: 1-MSB */
  57. #define RT_SPI_MASTER (0<<3) /* SPI master device */
  58. #define RT_SPI_SLAVE (1<<3) /* SPI slave device */
  59. #define RT_SPI_MODE_0 (0 | 0) /* CPOL = 0, CPHA = 0 */
  60. #define RT_SPI_MODE_1 (0 | RT_SPI_CPHA) /* CPOL = 0, CPHA = 1 */
  61. #define RT_SPI_MODE_2 (RT_SPI_CPOL | 0) /* CPOL = 1, CPHA = 0 */
  62. #define RT_SPI_MODE_3 (RT_SPI_CPOL | RT_SPI_CPHA) /* CPOL = 1, CPHA = 1 */
  63. #define RT_SPI_MODE_MASK (RT_SPI_CPHA | RT_SPI_CPOL | RT_SPI_MSB)
  64. #define RT_SPI_BUS_MODE_SPI (1<<0)
  65. #define RT_SPI_BUS_MODE_QSPI (1<<1)
  66. #define RT_SPI_CS_HIGH (1<<4) /* Chipselect active high */
  67. #define RT_SPI_NO_CS (1<<5) /* No chipselect */
  68. #define RT_SPI_3WIRE (1<<6) /* SI/SO pin shared */
  69. #define RT_SPI_READY (1<<7) /* Slave pulls low to pause */
  70. /**
  71. * SPI message structure
  72. */
  73. struct rt_spi_message
  74. {
  75. const void *send_buf;
  76. void *recv_buf;
  77. rt_size_t length;
  78. struct rt_spi_message *next;
  79. unsigned cs_take : 1;
  80. unsigned cs_release : 1;
  81. };
  82. /**
  83. * SPI configuration structure
  84. */
  85. struct rt_spi_configuration
  86. {
  87. rt_uint8_t mode;
  88. rt_uint8_t data_width;
  89. rt_uint16_t reserved;
  90. rt_uint32_t max_hz;
  91. };
  92. struct rt_spi_ops;
  93. struct rt_spi_bus
  94. {
  95. struct rt_device parent;
  96. rt_uint8_t mode;
  97. const struct rt_spi_ops *ops;
  98. struct rt_mutex lock;
  99. struct rt_spi_device *owner;
  100. };
  101. /**
  102. * SPI operators
  103. */
  104. struct rt_spi_ops
  105. {
  106. rt_err_t (*configure)(struct rt_spi_device *device, struct rt_spi_configuration *configuration);
  107. rt_uint32_t (*xfer)(struct rt_spi_device *device, struct rt_spi_message *message);
  108. };
  109. /**
  110. * SPI Virtual BUS, one device must connected to a virtual BUS
  111. */
  112. struct rt_spi_device
  113. {
  114. struct rt_device parent;
  115. struct rt_spi_bus *bus;
  116. struct rt_spi_configuration config;
  117. void *user_data;
  118. };
  119. struct rt_qspi_message
  120. {
  121. struct rt_spi_message parent;
  122. /* instruction stage */
  123. struct
  124. {
  125. rt_uint8_t content;
  126. rt_uint8_t qspi_lines;
  127. } instruction;
  128. /* address and alternate_bytes stage */
  129. struct
  130. {
  131. rt_uint32_t content;
  132. rt_uint8_t size;
  133. rt_uint8_t qspi_lines;
  134. } address, alternate_bytes;
  135. /* dummy_cycles stage */
  136. rt_uint32_t dummy_cycles;
  137. /* number of lines in qspi data stage, the other configuration items are in parent */
  138. rt_uint8_t qspi_data_lines;
  139. };
  140. struct rt_qspi_configuration
  141. {
  142. struct rt_spi_configuration parent;
  143. /* The size of medium */
  144. rt_uint32_t medium_size;
  145. /* double data rate mode */
  146. rt_uint8_t ddr_mode;
  147. /* the data lines max width which QSPI bus supported, such as 1, 2, 4 */
  148. rt_uint8_t qspi_dl_width ;
  149. };
  150. struct rt_qspi_device
  151. {
  152. struct rt_spi_device parent;
  153. struct rt_qspi_configuration config;
  154. void (*enter_qspi_mode)(struct rt_qspi_device *device);
  155. void (*exit_qspi_mode)(struct rt_qspi_device *device);
  156. };
  157. #define SPI_DEVICE(dev) ((struct rt_spi_device *)(dev))
  158. /* register a SPI bus */
  159. rt_err_t rt_spi_bus_register(struct rt_spi_bus *bus,
  160. const char *name,
  161. const struct rt_spi_ops *ops);
  162. /* attach a device on SPI bus */
  163. rt_err_t rt_spi_bus_attach_device(struct rt_spi_device *device,
  164. const char *name,
  165. const char *bus_name,
  166. void *user_data);
  167. /**
  168. * This function takes SPI bus.
  169. *
  170. * @param device the SPI device attached to SPI bus
  171. *
  172. * @return RT_EOK on taken SPI bus successfully. others on taken SPI bus failed.
  173. */
  174. rt_err_t rt_spi_take_bus(struct rt_spi_device *device);
  175. /**
  176. * This function releases SPI bus.
  177. *
  178. * @param device the SPI device attached to SPI bus
  179. *
  180. * @return RT_EOK on release SPI bus successfully.
  181. */
  182. rt_err_t rt_spi_release_bus(struct rt_spi_device *device);
  183. /**
  184. * This function take SPI device (takes CS of SPI device).
  185. *
  186. * @param device the SPI device attached to SPI bus
  187. *
  188. * @return RT_EOK on release SPI bus successfully. others on taken SPI bus failed.
  189. */
  190. rt_err_t rt_spi_take(struct rt_spi_device *device);
  191. /**
  192. * This function releases SPI device (releases CS of SPI device).
  193. *
  194. * @param device the SPI device attached to SPI bus
  195. *
  196. * @return RT_EOK on release SPI device successfully.
  197. */
  198. rt_err_t rt_spi_release(struct rt_spi_device *device);
  199. /* set configuration on SPI device */
  200. rt_err_t rt_spi_configure(struct rt_spi_device *device,
  201. struct rt_spi_configuration *cfg);
  202. /* send data then receive data from SPI device */
  203. rt_err_t rt_spi_send_then_recv(struct rt_spi_device *device,
  204. const void *send_buf,
  205. rt_size_t send_length,
  206. void *recv_buf,
  207. rt_size_t recv_length);
  208. rt_err_t rt_spi_send_then_send(struct rt_spi_device *device,
  209. const void *send_buf1,
  210. rt_size_t send_length1,
  211. const void *send_buf2,
  212. rt_size_t send_length2);
  213. /**
  214. * This function transmits data to SPI device.
  215. *
  216. * @param device the SPI device attached to SPI bus
  217. * @param send_buf the buffer to be transmitted to SPI device.
  218. * @param recv_buf the buffer to save received data from SPI device.
  219. * @param length the length of transmitted data.
  220. *
  221. * @return the actual length of transmitted.
  222. */
  223. rt_size_t rt_spi_transfer(struct rt_spi_device *device,
  224. const void *send_buf,
  225. void *recv_buf,
  226. rt_size_t length);
  227. /**
  228. * This function transfers a message list to the SPI device.
  229. *
  230. * @param device the SPI device attached to SPI bus
  231. * @param message the message list to be transmitted to SPI device
  232. *
  233. * @return RT_NULL if transmits message list successfully,
  234. * SPI message which be transmitted failed.
  235. */
  236. struct rt_spi_message *rt_spi_transfer_message(struct rt_spi_device *device,
  237. struct rt_spi_message *message);
  238. rt_inline rt_size_t rt_spi_recv(struct rt_spi_device *device,
  239. void *recv_buf,
  240. rt_size_t length)
  241. {
  242. return rt_spi_transfer(device, RT_NULL, recv_buf, length);
  243. }
  244. rt_inline rt_size_t rt_spi_send(struct rt_spi_device *device,
  245. const void *send_buf,
  246. rt_size_t length)
  247. {
  248. return rt_spi_transfer(device, send_buf, RT_NULL, length);
  249. }
  250. rt_inline rt_uint8_t rt_spi_sendrecv8(struct rt_spi_device *device,
  251. rt_uint8_t data)
  252. {
  253. rt_uint8_t value;
  254. rt_spi_send_then_recv(device, &data, 1, &value, 1);
  255. return value;
  256. }
  257. rt_inline rt_uint16_t rt_spi_sendrecv16(struct rt_spi_device *device,
  258. rt_uint16_t data)
  259. {
  260. rt_uint16_t value;
  261. rt_spi_send_then_recv(device, &data, 2, &value, 2);
  262. return value;
  263. }
  264. /**
  265. * This function appends a message to the SPI message list.
  266. *
  267. * @param list the SPI message list header.
  268. * @param message the message pointer to be appended to the message list.
  269. */
  270. rt_inline void rt_spi_message_append(struct rt_spi_message *list,
  271. struct rt_spi_message *message)
  272. {
  273. RT_ASSERT(list != RT_NULL);
  274. if (message == RT_NULL)
  275. return; /* not append */
  276. while (list->next != RT_NULL)
  277. {
  278. list = list->next;
  279. }
  280. list->next = message;
  281. message->next = RT_NULL;
  282. }
  283. /**
  284. * This function can set configuration on QSPI device.
  285. *
  286. * @param device the QSPI device attached to QSPI bus.
  287. * @param cfg the configuration pointer.
  288. *
  289. * @return the actual length of transmitted.
  290. */
  291. rt_err_t rt_qspi_configure(struct rt_qspi_device *device, struct rt_qspi_configuration *cfg);
  292. /**
  293. * This function can register a SPI bus for QSPI mode.
  294. *
  295. * @param bus the SPI bus for QSPI mode.
  296. * @param name The name of the spi bus.
  297. * @param ops the SPI bus instance to be registered.
  298. *
  299. * @return the actual length of transmitted.
  300. */
  301. rt_err_t rt_qspi_bus_register(struct rt_spi_bus *bus, const char *name, const struct rt_spi_ops *ops);
  302. /**
  303. * This function transmits data to QSPI device.
  304. *
  305. * @param device the QSPI device attached to QSPI bus.
  306. * @param message the message pointer.
  307. *
  308. * @return the actual length of transmitted.
  309. */
  310. rt_size_t rt_qspi_transfer_message(struct rt_qspi_device *device, struct rt_qspi_message *message);
  311. /**
  312. * This function can send data then receive data from QSPI device
  313. *
  314. * @param device the QSPI device attached to QSPI bus.
  315. * @param send_buf the buffer to be transmitted to QSPI device.
  316. * @param send_length the number of data to be transmitted.
  317. * @param recv_buf the buffer to be recivied from QSPI device.
  318. * @param recv_length the data to be recivied.
  319. *
  320. * @return the status of transmit.
  321. */
  322. 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);
  323. /**
  324. * This function can send data to QSPI device
  325. *
  326. * @param device the QSPI device attached to QSPI bus.
  327. * @param send_buf the buffer to be transmitted to QSPI device.
  328. * @param send_length the number of data to be transmitted.
  329. *
  330. * @return the status of transmit.
  331. */
  332. rt_err_t rt_qspi_send(struct rt_qspi_device *device, const void *send_buf, rt_size_t length);
  333. #ifdef __cplusplus
  334. }
  335. #endif
  336. #endif