spi.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. /**
  55. * SPI message structure
  56. */
  57. struct rt_spi_message
  58. {
  59. const void *send_buf;
  60. void *recv_buf;
  61. rt_size_t length;
  62. struct rt_spi_message *next;
  63. unsigned cs_take : 1;
  64. unsigned cs_release : 1;
  65. };
  66. /**
  67. * SPI configuration structure
  68. */
  69. struct rt_spi_configuration
  70. {
  71. rt_uint8_t mode;
  72. rt_uint8_t data_width;
  73. rt_uint16_t reserved;
  74. rt_uint32_t max_hz;
  75. };
  76. struct rt_spi_ops;
  77. struct rt_spi_bus
  78. {
  79. struct rt_device parent;
  80. const struct rt_spi_ops *ops;
  81. struct rt_mutex lock;
  82. struct rt_spi_device *owner;
  83. };
  84. /**
  85. * SPI operators
  86. */
  87. struct rt_spi_ops
  88. {
  89. rt_err_t (*configure)(struct rt_spi_device *device, struct rt_spi_configuration *configuration);
  90. rt_uint32_t (*xfer)(struct rt_spi_device *device, struct rt_spi_message *message);
  91. };
  92. /**
  93. * SPI Virtual BUS, one device must connected to a virtual BUS
  94. */
  95. struct rt_spi_device
  96. {
  97. struct rt_device parent;
  98. struct rt_spi_bus *bus;
  99. struct rt_spi_configuration config;
  100. };
  101. #define SPI_DEVICE(dev) ((struct rt_spi_device *)(dev))
  102. /* register a SPI bus */
  103. rt_err_t rt_spi_bus_register(struct rt_spi_bus *bus,
  104. const char *name,
  105. const struct rt_spi_ops *ops);
  106. /* attach a device on SPI bus */
  107. rt_err_t rt_spi_bus_attach_device(struct rt_spi_device *device,
  108. const char *name,
  109. const char *bus_name,
  110. void *user_data);
  111. /**
  112. * This function takes SPI bus.
  113. *
  114. * @param device the SPI device attached to SPI bus
  115. *
  116. * @return RT_EOK on taken SPI bus successfully. others on taken SPI bus failed.
  117. */
  118. rt_err_t rt_spi_take_bus(struct rt_spi_device *device);
  119. /**
  120. * This function releases SPI bus.
  121. *
  122. * @param device the SPI device attached to SPI bus
  123. *
  124. * @return RT_EOK on release SPI bus successfully.
  125. */
  126. rt_err_t rt_spi_release_bus(struct rt_spi_device *device);
  127. /**
  128. * This function take SPI device (takes CS of SPI device).
  129. *
  130. * @param device the SPI device attached to SPI bus
  131. *
  132. * @return RT_EOK on release SPI bus successfully. others on taken SPI bus failed.
  133. */
  134. rt_err_t rt_spi_take(struct rt_spi_device *device);
  135. /**
  136. * This function releases SPI device (releases CS of SPI device).
  137. *
  138. * @param device the SPI device attached to SPI bus
  139. *
  140. * @return RT_EOK on release SPI device successfully.
  141. */
  142. rt_err_t rt_spi_release(struct rt_spi_device *device);
  143. /* set configuration on SPI device */
  144. rt_err_t rt_spi_configure(struct rt_spi_device *device,
  145. struct rt_spi_configuration *cfg);
  146. /* send data then receive data from SPI device */
  147. rt_err_t rt_spi_send_then_recv(struct rt_spi_device *device,
  148. const void *send_buf,
  149. rt_size_t send_length,
  150. void *recv_buf,
  151. rt_size_t recv_length);
  152. rt_err_t rt_spi_send_then_send(struct rt_spi_device *device,
  153. const void *send_buf1,
  154. rt_size_t send_length1,
  155. const void *send_buf2,
  156. rt_size_t send_length2);
  157. /**
  158. * This function transmits data to SPI device.
  159. *
  160. * @param device the SPI device attached to SPI bus
  161. * @param send_buf the buffer to be transmitted to SPI device.
  162. * @param recv_buf the buffer to save received data from SPI device.
  163. * @param length the length of transmitted data.
  164. *
  165. * @return the actual length of transmitted.
  166. */
  167. rt_size_t rt_spi_transfer(struct rt_spi_device *device,
  168. const void *send_buf,
  169. void *recv_buf,
  170. rt_size_t length);
  171. /**
  172. * This function transfers a message list to the SPI device.
  173. *
  174. * @param device the SPI device attached to SPI bus
  175. * @param message the message list to be transmitted to SPI device
  176. *
  177. * @return RT_NULL if transmits message list successfully,
  178. * SPI message which be transmitted failed.
  179. */
  180. struct rt_spi_message *rt_spi_transfer_message(struct rt_spi_device *device,
  181. struct rt_spi_message *message);
  182. rt_inline rt_size_t rt_spi_recv(struct rt_spi_device *device,
  183. void *recv_buf,
  184. rt_size_t length)
  185. {
  186. return rt_spi_transfer(device, RT_NULL, recv_buf, length);
  187. }
  188. rt_inline rt_size_t rt_spi_send(struct rt_spi_device *device,
  189. const void *send_buf,
  190. rt_size_t length)
  191. {
  192. return rt_spi_transfer(device, send_buf, RT_NULL, length);
  193. }
  194. rt_inline rt_uint8_t rt_spi_sendrecv8(struct rt_spi_device *device,
  195. rt_uint8_t data)
  196. {
  197. rt_uint8_t value;
  198. rt_spi_send_then_recv(device, &data, 1, &value, 1);
  199. return value;
  200. }
  201. rt_inline rt_uint16_t rt_spi_sendrecv16(struct rt_spi_device *device,
  202. rt_uint16_t data)
  203. {
  204. rt_uint16_t value;
  205. rt_spi_send_then_recv(device, &data, 2, &value, 2);
  206. return value;
  207. }
  208. /**
  209. * This function appends a message to the SPI message list.
  210. *
  211. * @param list the SPI message list header.
  212. * @param message the message pointer to be appended to the message list.
  213. */
  214. rt_inline void rt_spi_message_append(struct rt_spi_message *list,
  215. struct rt_spi_message *message)
  216. {
  217. RT_ASSERT(list != RT_NULL);
  218. if (message == RT_NULL)
  219. return; /* not append */
  220. while (list->next != RT_NULL)
  221. {
  222. list = list->next;
  223. }
  224. list->next = message;
  225. message->next = RT_NULL;
  226. }
  227. #ifdef __cplusplus
  228. }
  229. #endif
  230. #endif