spi.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. };
  105. #define SPI_DEVICE(dev) ((struct rt_spi_device *)(dev))
  106. /* register a SPI bus */
  107. rt_err_t rt_spi_bus_register(struct rt_spi_bus *bus,
  108. const char *name,
  109. const struct rt_spi_ops *ops);
  110. /* attach a device on SPI bus */
  111. rt_err_t rt_spi_bus_attach_device(struct rt_spi_device *device,
  112. const char *name,
  113. const char *bus_name,
  114. void *user_data);
  115. /**
  116. * This function takes SPI bus.
  117. *
  118. * @param device the SPI device attached to SPI bus
  119. *
  120. * @return RT_EOK on taken SPI bus successfully. others on taken SPI bus failed.
  121. */
  122. rt_err_t rt_spi_take_bus(struct rt_spi_device *device);
  123. /**
  124. * This function releases SPI bus.
  125. *
  126. * @param device the SPI device attached to SPI bus
  127. *
  128. * @return RT_EOK on release SPI bus successfully.
  129. */
  130. rt_err_t rt_spi_release_bus(struct rt_spi_device *device);
  131. /**
  132. * This function take SPI device (takes CS of SPI device).
  133. *
  134. * @param device the SPI device attached to SPI bus
  135. *
  136. * @return RT_EOK on release SPI bus successfully. others on taken SPI bus failed.
  137. */
  138. rt_err_t rt_spi_take(struct rt_spi_device *device);
  139. /**
  140. * This function releases SPI device (releases CS of SPI device).
  141. *
  142. * @param device the SPI device attached to SPI bus
  143. *
  144. * @return RT_EOK on release SPI device successfully.
  145. */
  146. rt_err_t rt_spi_release(struct rt_spi_device *device);
  147. /* set configuration on SPI device */
  148. rt_err_t rt_spi_configure(struct rt_spi_device *device,
  149. struct rt_spi_configuration *cfg);
  150. /* send data then receive data from SPI device */
  151. rt_err_t rt_spi_send_then_recv(struct rt_spi_device *device,
  152. const void *send_buf,
  153. rt_size_t send_length,
  154. void *recv_buf,
  155. rt_size_t recv_length);
  156. rt_err_t rt_spi_send_then_send(struct rt_spi_device *device,
  157. const void *send_buf1,
  158. rt_size_t send_length1,
  159. const void *send_buf2,
  160. rt_size_t send_length2);
  161. /**
  162. * This function transmits data to SPI device.
  163. *
  164. * @param device the SPI device attached to SPI bus
  165. * @param send_buf the buffer to be transmitted to SPI device.
  166. * @param recv_buf the buffer to save received data from SPI device.
  167. * @param length the length of transmitted data.
  168. *
  169. * @return the actual length of transmitted.
  170. */
  171. rt_size_t rt_spi_transfer(struct rt_spi_device *device,
  172. const void *send_buf,
  173. void *recv_buf,
  174. rt_size_t length);
  175. /**
  176. * This function transfers a message list to the SPI device.
  177. *
  178. * @param device the SPI device attached to SPI bus
  179. * @param message the message list to be transmitted to SPI device
  180. *
  181. * @return RT_NULL if transmits message list successfully,
  182. * SPI message which be transmitted failed.
  183. */
  184. struct rt_spi_message *rt_spi_transfer_message(struct rt_spi_device *device,
  185. struct rt_spi_message *message);
  186. rt_inline rt_size_t rt_spi_recv(struct rt_spi_device *device,
  187. void *recv_buf,
  188. rt_size_t length)
  189. {
  190. return rt_spi_transfer(device, RT_NULL, recv_buf, length);
  191. }
  192. rt_inline rt_size_t rt_spi_send(struct rt_spi_device *device,
  193. const void *send_buf,
  194. rt_size_t length)
  195. {
  196. return rt_spi_transfer(device, send_buf, RT_NULL, length);
  197. }
  198. rt_inline rt_uint8_t rt_spi_sendrecv8(struct rt_spi_device *device,
  199. rt_uint8_t data)
  200. {
  201. rt_uint8_t value;
  202. rt_spi_send_then_recv(device, &data, 1, &value, 1);
  203. return value;
  204. }
  205. rt_inline rt_uint16_t rt_spi_sendrecv16(struct rt_spi_device *device,
  206. rt_uint16_t data)
  207. {
  208. rt_uint16_t value;
  209. rt_spi_send_then_recv(device, &data, 2, &value, 2);
  210. return value;
  211. }
  212. /**
  213. * This function appends a message to the SPI message list.
  214. *
  215. * @param list the SPI message list header.
  216. * @param message the message pointer to be appended to the message list.
  217. */
  218. rt_inline void rt_spi_message_append(struct rt_spi_message *list,
  219. struct rt_spi_message *message)
  220. {
  221. RT_ASSERT(list != RT_NULL);
  222. if (message == RT_NULL)
  223. return; /* not append */
  224. while (list->next != RT_NULL)
  225. {
  226. list = list->next;
  227. }
  228. list->next = message;
  229. message->next = RT_NULL;
  230. }
  231. #ifdef __cplusplus
  232. }
  233. #endif
  234. #endif