spi.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2012-11-23 Bernard Add extern "C"
  23. */
  24. #ifndef __SPI_H__
  25. #define __SPI_H__
  26. #include <stdlib.h>
  27. #include <rtthread.h>
  28. #ifdef __cplusplus
  29. extern "C"{
  30. #endif
  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. /**
  34. * At CPOL=0 the base value of the clock is zero
  35. * - For CPHA=0, data are captured on the clock's rising edge (low->high transition)
  36. * and data are propagated on a falling edge (high->low clock transition).
  37. * - For CPHA=1, data are captured on the clock's falling edge and data are
  38. * propagated on a rising edge.
  39. * At CPOL=1 the base value of the clock is one (inversion of CPOL=0)
  40. * - For CPHA=0, data are captured on clock's falling edge and data are propagated
  41. * on a rising edge.
  42. * - For CPHA=1, data are captured on clock's rising edge and data are propagated
  43. * on a falling edge.
  44. */
  45. #define RT_SPI_LSB (0<<2) /* bit[2]: 0-LSB */
  46. #define RT_SPI_MSB (1<<2) /* bit[2]: 1-MSB */
  47. #define RT_SPI_MASTER (0<<3) /* SPI master device */
  48. #define RT_SPI_SLAVE (1<<3) /* SPI slave device */
  49. #define RT_SPI_MODE_0 (0 | 0) /* CPOL = 0, CPHA = 0 */
  50. #define RT_SPI_MODE_1 (0 | RT_SPI_CPHA) /* CPOL = 0, CPHA = 1 */
  51. #define RT_SPI_MODE_2 (RT_SPI_CPOL | 0) /* CPOL = 1, CPHA = 0 */
  52. #define RT_SPI_MODE_3 (RT_SPI_CPOL | RT_SPI_CPHA) /* CPOL = 1, CPHA = 1 */
  53. #define RT_SPI_MODE_MASK (RT_SPI_CPHA | RT_SPI_CPOL | RT_SPI_MSB)
  54. #define RT_SPI_CS_HIGH (1<<4) /* Chipselect active high */
  55. #define RT_SPI_NO_CS (1<<5) /* No chipselect */
  56. #define RT_SPI_3WIRE (1<<6) /* SI/SO pin shared */
  57. #define RT_SPI_READY (1<<7) /* Slave pulls low to pause */
  58. /**
  59. * SPI message structure
  60. */
  61. struct rt_spi_message
  62. {
  63. const void *send_buf;
  64. void *recv_buf;
  65. rt_size_t length;
  66. struct rt_spi_message *next;
  67. unsigned cs_take : 1;
  68. unsigned cs_release : 1;
  69. };
  70. /**
  71. * SPI configuration structure
  72. */
  73. struct rt_spi_configuration
  74. {
  75. rt_uint8_t mode;
  76. rt_uint8_t data_width;
  77. rt_uint16_t reserved;
  78. rt_uint32_t max_hz;
  79. };
  80. struct rt_spi_ops;
  81. struct rt_spi_bus
  82. {
  83. struct rt_device parent;
  84. const struct rt_spi_ops *ops;
  85. struct rt_mutex lock;
  86. struct rt_spi_device *owner;
  87. };
  88. /**
  89. * SPI operators
  90. */
  91. struct rt_spi_ops
  92. {
  93. rt_err_t (*configure)(struct rt_spi_device *device, struct rt_spi_configuration *configuration);
  94. rt_uint32_t (*xfer)(struct rt_spi_device *device, struct rt_spi_message *message);
  95. };
  96. /**
  97. * SPI Virtual BUS, one device must connected to a virtual BUS
  98. */
  99. struct rt_spi_device
  100. {
  101. struct rt_device parent;
  102. struct rt_spi_bus *bus;
  103. struct rt_spi_configuration config;
  104. void *user_data;
  105. };
  106. #define SPI_DEVICE(dev) ((struct rt_spi_device *)(dev))
  107. /* register a SPI bus */
  108. rt_err_t rt_spi_bus_register(struct rt_spi_bus *bus,
  109. const char *name,
  110. const struct rt_spi_ops *ops);
  111. /* attach a device on SPI bus */
  112. rt_err_t rt_spi_bus_attach_device(struct rt_spi_device *device,
  113. const char *name,
  114. const char *bus_name,
  115. void *user_data);
  116. /**
  117. * This function takes SPI bus.
  118. *
  119. * @param device the SPI device attached to SPI bus
  120. *
  121. * @return RT_EOK on taken SPI bus successfully. others on taken SPI bus failed.
  122. */
  123. rt_err_t rt_spi_take_bus(struct rt_spi_device *device);
  124. /**
  125. * This function releases SPI bus.
  126. *
  127. * @param device the SPI device attached to SPI bus
  128. *
  129. * @return RT_EOK on release SPI bus successfully.
  130. */
  131. rt_err_t rt_spi_release_bus(struct rt_spi_device *device);
  132. /**
  133. * This function take SPI device (takes CS of SPI device).
  134. *
  135. * @param device the SPI device attached to SPI bus
  136. *
  137. * @return RT_EOK on release SPI bus successfully. others on taken SPI bus failed.
  138. */
  139. rt_err_t rt_spi_take(struct rt_spi_device *device);
  140. /**
  141. * This function releases SPI device (releases CS of SPI device).
  142. *
  143. * @param device the SPI device attached to SPI bus
  144. *
  145. * @return RT_EOK on release SPI device successfully.
  146. */
  147. rt_err_t rt_spi_release(struct rt_spi_device *device);
  148. /* set configuration on SPI device */
  149. rt_err_t rt_spi_configure(struct rt_spi_device *device,
  150. struct rt_spi_configuration *cfg);
  151. /* send data then receive data from SPI device */
  152. rt_err_t rt_spi_send_then_recv(struct rt_spi_device *device,
  153. const void *send_buf,
  154. rt_size_t send_length,
  155. void *recv_buf,
  156. rt_size_t recv_length);
  157. rt_err_t rt_spi_send_then_send(struct rt_spi_device *device,
  158. const void *send_buf1,
  159. rt_size_t send_length1,
  160. const void *send_buf2,
  161. rt_size_t send_length2);
  162. /**
  163. * This function transmits data to SPI device.
  164. *
  165. * @param device the SPI device attached to SPI bus
  166. * @param send_buf the buffer to be transmitted to SPI device.
  167. * @param recv_buf the buffer to save received data from SPI device.
  168. * @param length the length of transmitted data.
  169. *
  170. * @return the actual length of transmitted.
  171. */
  172. rt_size_t rt_spi_transfer(struct rt_spi_device *device,
  173. const void *send_buf,
  174. void *recv_buf,
  175. rt_size_t length);
  176. /**
  177. * This function transfers a message list to the SPI device.
  178. *
  179. * @param device the SPI device attached to SPI bus
  180. * @param message the message list to be transmitted to SPI device
  181. *
  182. * @return RT_NULL if transmits message list successfully,
  183. * SPI message which be transmitted failed.
  184. */
  185. struct rt_spi_message *rt_spi_transfer_message(struct rt_spi_device *device,
  186. struct rt_spi_message *message);
  187. rt_inline rt_size_t rt_spi_recv(struct rt_spi_device *device,
  188. void *recv_buf,
  189. rt_size_t length)
  190. {
  191. return rt_spi_transfer(device, RT_NULL, recv_buf, length);
  192. }
  193. rt_inline rt_size_t rt_spi_send(struct rt_spi_device *device,
  194. const void *send_buf,
  195. rt_size_t length)
  196. {
  197. return rt_spi_transfer(device, send_buf, RT_NULL, length);
  198. }
  199. rt_inline rt_uint8_t rt_spi_sendrecv8(struct rt_spi_device *device,
  200. rt_uint8_t data)
  201. {
  202. rt_uint8_t value;
  203. rt_spi_send_then_recv(device, &data, 1, &value, 1);
  204. return value;
  205. }
  206. rt_inline rt_uint16_t rt_spi_sendrecv16(struct rt_spi_device *device,
  207. rt_uint16_t data)
  208. {
  209. rt_uint16_t value;
  210. rt_spi_send_then_recv(device, &data, 2, &value, 2);
  211. return value;
  212. }
  213. /**
  214. * This function appends a message to the SPI message list.
  215. *
  216. * @param list the SPI message list header.
  217. * @param message the message pointer to be appended to the message list.
  218. */
  219. rt_inline void rt_spi_message_append(struct rt_spi_message *list,
  220. struct rt_spi_message *message)
  221. {
  222. RT_ASSERT(list != RT_NULL);
  223. if (message == RT_NULL)
  224. return; /* not append */
  225. while (list->next != RT_NULL)
  226. {
  227. list = list->next;
  228. }
  229. list->next = message;
  230. message->next = RT_NULL;
  231. }
  232. #ifdef __cplusplus
  233. }
  234. #endif
  235. #endif