spi.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * File : spi.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2012, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2012-11-23 Bernard Add extern "C"
  13. */
  14. #ifndef __SPI_H__
  15. #define __SPI_H__
  16. #include <stdlib.h>
  17. #include <rtthread.h>
  18. #ifdef __cplusplus
  19. extern "C"{
  20. #endif
  21. #define RT_SPI_CPHA (1<<0) /* bit[0]:CPHA, clock phase */
  22. #define RT_SPI_CPOL (1<<1) /* bit[1]:CPOL, clock polarity */
  23. /**
  24. * At CPOL=0 the base value of the clock is zero
  25. * - For CPHA=0, data are captured on the clock's rising edge (low¡úhigh transition)
  26. * and data are propagated on a falling edge (high¡úlow clock transition).
  27. * - For CPHA=1, data are captured on the clock's falling edge and data are
  28. * propagated on a rising edge.
  29. * At CPOL=1 the base value of the clock is one (inversion of CPOL=0)
  30. * - For CPHA=0, data are captured on clock's falling edge and data are propagated
  31. * on a rising edge.
  32. * - For CPHA=1, data are captured on clock's rising edge and data are propagated
  33. * on a falling edge.
  34. */
  35. #define RT_SPI_LSB (0<<2) /* bit[2]: 0-LSB */
  36. #define RT_SPI_MSB (1<<2) /* bit[2]: 1-MSB */
  37. #define RT_SPI_MASTER (0<<3) /* SPI master device */
  38. #define RT_SPI_SLAVE (1<<3) /* SPI slave device */
  39. #define RT_SPI_MODE_0 (0 | 0) /* CPOL = 0, CPHA = 0 */
  40. #define RT_SPI_MODE_1 (0 | RT_SPI_CPHA) /* CPOL = 0, CPHA = 1 */
  41. #define RT_SPI_MODE_2 (RT_SPI_CPOL | 0) /* CPOL = 1, CPHA = 0 */
  42. #define RT_SPI_MODE_3 (RT_SPI_CPOL | RT_SPI_CPHA) /* CPOL = 1, CPHA = 1 */
  43. #define RT_SPI_MODE_MASK (RT_SPI_CPHA | RT_SPI_CPOL | RT_SPI_MSB)
  44. /**
  45. * SPI message structure
  46. */
  47. struct rt_spi_message
  48. {
  49. const void *send_buf;
  50. void *recv_buf;
  51. rt_size_t length;
  52. struct rt_spi_message *next;
  53. unsigned cs_take : 1;
  54. unsigned cs_release : 1;
  55. };
  56. /**
  57. * SPI configuration structure
  58. */
  59. struct rt_spi_configuration
  60. {
  61. rt_uint8_t mode;
  62. rt_uint8_t data_width;
  63. rt_uint16_t reserved;
  64. rt_uint32_t max_hz;
  65. };
  66. struct rt_spi_ops;
  67. struct rt_spi_bus
  68. {
  69. struct rt_device parent;
  70. const struct rt_spi_ops *ops;
  71. struct rt_mutex lock;
  72. struct rt_spi_device *owner;
  73. };
  74. /**
  75. * SPI operators
  76. */
  77. struct rt_spi_ops
  78. {
  79. rt_err_t (*configure)(struct rt_spi_device *device, struct rt_spi_configuration *configuration);
  80. rt_uint32_t (*xfer)(struct rt_spi_device *device, struct rt_spi_message *message);
  81. };
  82. /**
  83. * SPI Virtual BUS, one device must connected to a virtual BUS
  84. */
  85. struct rt_spi_device
  86. {
  87. struct rt_device parent;
  88. struct rt_spi_bus *bus;
  89. struct rt_spi_configuration config;
  90. };
  91. #define SPI_DEVICE(dev) ((struct rt_spi_device *)(dev))
  92. /* register a SPI bus */
  93. rt_err_t rt_spi_bus_register(struct rt_spi_bus *bus,
  94. const char *name,
  95. const struct rt_spi_ops *ops);
  96. /* attach a device on SPI bus */
  97. rt_err_t rt_spi_bus_attach_device(struct rt_spi_device *device,
  98. const char *name,
  99. const char *bus_name,
  100. void *user_data);
  101. /**
  102. * This function takes SPI bus.
  103. *
  104. * @param device the SPI device attached to SPI bus
  105. *
  106. * @return RT_EOK on taken SPI bus successfully. others on taken SPI bus failed.
  107. */
  108. rt_err_t rt_spi_take_bus(struct rt_spi_device *device);
  109. /**
  110. * This function releases SPI bus.
  111. *
  112. * @param device the SPI device attached to SPI bus
  113. *
  114. * @return RT_EOK on release SPI bus successfully.
  115. */
  116. rt_err_t rt_spi_release_bus(struct rt_spi_device *device);
  117. /**
  118. * This function take SPI device (takes CS of SPI device).
  119. *
  120. * @param device the SPI device attached to SPI bus
  121. *
  122. * @return RT_EOK on release SPI bus successfully. others on taken SPI bus failed.
  123. */
  124. rt_err_t rt_spi_take(struct rt_spi_device *device);
  125. /**
  126. * This function releases SPI device (releases CS of SPI device).
  127. *
  128. * @param device the SPI device attached to SPI bus
  129. *
  130. * @return RT_EOK on release SPI device successfully.
  131. */
  132. rt_err_t rt_spi_release(struct rt_spi_device *device);
  133. /* set configuration on SPI device */
  134. rt_err_t rt_spi_configure(struct rt_spi_device *device,
  135. struct rt_spi_configuration *cfg);
  136. /* send data then receive data from SPI device */
  137. rt_err_t rt_spi_send_then_recv(struct rt_spi_device *device,
  138. const void *send_buf,
  139. rt_size_t send_length,
  140. void *recv_buf,
  141. rt_size_t recv_length);
  142. rt_err_t rt_spi_send_then_send(struct rt_spi_device *device,
  143. const void *send_buf1,
  144. rt_size_t send_length1,
  145. const void *send_buf2,
  146. rt_size_t send_length2);
  147. /**
  148. * This function transmits data to SPI device.
  149. *
  150. * @param device the SPI device attached to SPI bus
  151. * @param send_buf the buffer to be transmitted to SPI device.
  152. * @param recv_buf the buffer to save received data from SPI device.
  153. * @param length the length of transmitted data.
  154. *
  155. * @return the actual length of transmitted.
  156. */
  157. rt_size_t rt_spi_transfer(struct rt_spi_device *device,
  158. const void *send_buf,
  159. void *recv_buf,
  160. rt_size_t length);
  161. /**
  162. * This function transfers a message list to the SPI device.
  163. *
  164. * @param device the SPI device attached to SPI bus
  165. * @param message the message list to be transmitted to SPI device
  166. *
  167. * @return RT_NULL if transmits message list successfully,
  168. * SPI message which be transmitted failed.
  169. */
  170. struct rt_spi_message *rt_spi_transfer_message(struct rt_spi_device *device,
  171. struct rt_spi_message *message);
  172. rt_inline rt_size_t rt_spi_recv(struct rt_spi_device *device,
  173. void *recv_buf,
  174. rt_size_t length)
  175. {
  176. return rt_spi_transfer(device, RT_NULL, recv_buf, length);
  177. }
  178. rt_inline rt_size_t rt_spi_send(struct rt_spi_device *device,
  179. const void *send_buf,
  180. rt_size_t length)
  181. {
  182. return rt_spi_transfer(device, send_buf, RT_NULL, length);
  183. }
  184. rt_inline rt_uint8_t rt_spi_sendrecv8(struct rt_spi_device *device,
  185. rt_uint8_t data)
  186. {
  187. rt_uint8_t value;
  188. rt_spi_send_then_recv(device, &data, 1, &value, 1);
  189. return value;
  190. }
  191. rt_inline rt_uint16_t rt_spi_sendrecv16(struct rt_spi_device *device,
  192. rt_uint16_t data)
  193. {
  194. rt_uint16_t value;
  195. rt_spi_send_then_recv(device, &data, 2, &value, 2);
  196. return value;
  197. }
  198. /**
  199. * This function appends a message to the SPI message list.
  200. *
  201. * @param list the SPI message list header.
  202. * @param message the message pointer to be appended to the message list.
  203. */
  204. rt_inline void rt_spi_message_append(struct rt_spi_message *list,
  205. struct rt_spi_message *message)
  206. {
  207. RT_ASSERT(list != RT_NULL);
  208. if (message == RT_NULL)
  209. return; /* not append */
  210. while (list->next != RT_NULL)
  211. {
  212. list = list->next;
  213. }
  214. list->next = message;
  215. message->next = RT_NULL;
  216. }
  217. #ifdef __cplusplus
  218. }
  219. #endif
  220. #endif