rtdevice.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * File : rtdevice.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-01-08 bernard first version.
  23. */
  24. #ifndef __RT_DEVICE_H__
  25. #define __RT_DEVICE_H__
  26. #include <rtthread.h>
  27. #define RT_DEVICE(device) ((rt_device_t)device)
  28. /* completion flag */
  29. struct rt_completion
  30. {
  31. rt_uint32_t flag;
  32. /* suspended list */
  33. rt_list_t suspended_list;
  34. };
  35. /* ring buffer */
  36. struct rt_ringbuffer
  37. {
  38. rt_uint8_t *buffer_ptr;
  39. /* use the msb of the {read,write}_index as mirror bit. You can see this as
  40. * if the buffer adds a virtual mirror and the pointers point either to the
  41. * normal or to the mirrored buffer. If the write_index has the same value
  42. * with the read_index, but in a different mirror, the buffer is full.
  43. * While if the write_index and the read_index are the same and within the
  44. * same mirror, the buffer is empty. The ASCII art of the ringbuffer is:
  45. *
  46. * mirror = 0 mirror = 1
  47. * +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
  48. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 ||| 0 | 1 | 2 | 3 | 4 | 5 | 6 | Full
  49. * +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
  50. * read_idx-^ write_idx-^
  51. *
  52. * +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
  53. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 ||| 0 | 1 | 2 | 3 | 4 | 5 | 6 | Empty
  54. * +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
  55. * read_idx-^ ^-write_idx
  56. *
  57. * The tradeoff is we could only use 32KiB of buffer for 16 bit of index.
  58. * But it should be enough for most of the cases.
  59. *
  60. * Ref: http://en.wikipedia.org/wiki/Circular_buffer#Mirroring */
  61. rt_uint16_t read_mirror : 1;
  62. rt_uint16_t read_index : 15;
  63. rt_uint16_t write_mirror : 1;
  64. rt_uint16_t write_index : 15;
  65. /* as we use msb of index as mirror bit, the size should be signed and
  66. * could only be positive. */
  67. rt_int16_t buffer_size;
  68. };
  69. /* pipe device */
  70. #define PIPE_DEVICE(device) ((struct rt_pipe_device*)(device))
  71. enum rt_pipe_flag
  72. {
  73. /* both read and write won't block */
  74. RT_PIPE_FLAG_NONBLOCK_RDWR = 0x00,
  75. /* read would block */
  76. RT_PIPE_FLAG_BLOCK_RD = 0x01,
  77. /* write would block */
  78. RT_PIPE_FLAG_BLOCK_WR = 0x02,
  79. /* write to this pipe will discard some data when the pipe is full.
  80. * When this flag is set, RT_PIPE_FLAG_BLOCK_WR will be ignored since write
  81. * operation will always be success. */
  82. RT_PIPE_FLAG_FORCE_WR = 0x04,
  83. };
  84. struct rt_pipe_device
  85. {
  86. struct rt_device parent;
  87. /* ring buffer in pipe device */
  88. struct rt_ringbuffer ringbuffer;
  89. enum rt_pipe_flag flag;
  90. /* suspended list */
  91. rt_list_t suspended_read_list;
  92. rt_list_t suspended_write_list;
  93. };
  94. #define RT_DATAQUEUE_EVENT_UNKNOWN 0x00
  95. #define RT_DATAQUEUE_EVENT_POP 0x01
  96. #define RT_DATAQUEUE_EVENT_PUSH 0x02
  97. #define RT_DATAQUEUE_EVENT_LWM 0x03
  98. struct rt_data_item;
  99. #define RT_DATAQUEUE_SIZE(dq) ((dq)->put_index - (dq)->get_index)
  100. #define RT_DATAQUEUE_EMPTY(dq) ((dq)->size - RT_DATAQUEUE_SIZE(dq))
  101. /* data queue implementation */
  102. struct rt_data_queue
  103. {
  104. rt_uint16_t size;
  105. rt_uint16_t lwm;
  106. rt_bool_t waiting_lwm;
  107. rt_uint16_t get_index;
  108. rt_uint16_t put_index;
  109. struct rt_data_item *queue;
  110. rt_list_t suspended_push_list;
  111. rt_list_t suspended_pop_list;
  112. /* event notify */
  113. void (*evt_notify)(struct rt_data_queue *queue, rt_uint32_t event);
  114. };
  115. /**
  116. * Completion
  117. */
  118. void rt_completion_init(struct rt_completion *completion);
  119. rt_err_t rt_completion_wait(struct rt_completion *completion,
  120. rt_int32_t timeout);
  121. void rt_completion_done(struct rt_completion *completion);
  122. /**
  123. * RingBuffer for DeviceDriver
  124. *
  125. * Please note that the ring buffer implementation of RT-Thread
  126. * has no thread wait or resume feature.
  127. */
  128. void rt_ringbuffer_init(struct rt_ringbuffer *rb,
  129. rt_uint8_t *pool,
  130. rt_int16_t size);
  131. rt_size_t rt_ringbuffer_put(struct rt_ringbuffer *rb,
  132. const rt_uint8_t *ptr,
  133. rt_uint16_t length);
  134. rt_size_t rt_ringbuffer_put_force(struct rt_ringbuffer *rb,
  135. const rt_uint8_t *ptr,
  136. rt_uint16_t length);
  137. rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer *rb,
  138. const rt_uint8_t ch);
  139. rt_size_t rt_ringbuffer_putchar_force(struct rt_ringbuffer *rb,
  140. const rt_uint8_t ch);
  141. rt_size_t rt_ringbuffer_get(struct rt_ringbuffer *rb,
  142. rt_uint8_t *ptr,
  143. rt_uint16_t length);
  144. rt_size_t rt_ringbuffer_getchar(struct rt_ringbuffer *rb, rt_uint8_t *ch);
  145. enum rt_ringbuffer_state
  146. {
  147. RT_RINGBUFFER_EMPTY,
  148. RT_RINGBUFFER_FULL,
  149. /* half full is neither full nor empty */
  150. RT_RINGBUFFER_HALFFULL,
  151. };
  152. rt_inline rt_uint16_t rt_ringbuffer_get_size(struct rt_ringbuffer *rb)
  153. {
  154. RT_ASSERT(rb != RT_NULL);
  155. return rb->buffer_size;
  156. }
  157. rt_inline enum rt_ringbuffer_state
  158. rt_ringbuffer_status(struct rt_ringbuffer *rb)
  159. {
  160. if (rb->read_index == rb->write_index)
  161. {
  162. if (rb->read_mirror == rb->write_mirror)
  163. return RT_RINGBUFFER_EMPTY;
  164. else
  165. return RT_RINGBUFFER_FULL;
  166. }
  167. return RT_RINGBUFFER_HALFFULL;
  168. }
  169. /** return the size of data in rb */
  170. rt_inline rt_uint16_t rt_ringbuffer_data_len(struct rt_ringbuffer *rb)
  171. {
  172. switch (rt_ringbuffer_status(rb))
  173. {
  174. case RT_RINGBUFFER_EMPTY:
  175. return 0;
  176. case RT_RINGBUFFER_FULL:
  177. return rb->buffer_size;
  178. case RT_RINGBUFFER_HALFFULL:
  179. default:
  180. if (rb->write_index > rb->read_index)
  181. return rb->write_index - rb->read_index;
  182. else
  183. return rb->buffer_size - (rb->read_index - rb->write_index);
  184. };
  185. }
  186. /** return the size of empty space in rb */
  187. #define rt_ringbuffer_space_len(rb) ((rb)->buffer_size - rt_ringbuffer_data_len(rb))
  188. /**
  189. * Pipe Device
  190. */
  191. rt_err_t rt_pipe_init(struct rt_pipe_device *pipe,
  192. const char *name,
  193. enum rt_pipe_flag flag,
  194. rt_uint8_t *buf,
  195. rt_size_t size);
  196. rt_err_t rt_pipe_detach(struct rt_pipe_device *pipe);
  197. #ifdef RT_USING_HEAP
  198. rt_err_t rt_pipe_create(const char *name, enum rt_pipe_flag flag, rt_size_t size);
  199. void rt_pipe_destroy(struct rt_pipe_device *pipe);
  200. #endif
  201. /**
  202. * DataQueue for DeviceDriver
  203. */
  204. rt_err_t rt_data_queue_init(struct rt_data_queue *queue,
  205. rt_uint16_t size,
  206. rt_uint16_t lwm,
  207. void (*evt_notify)(struct rt_data_queue *queue, rt_uint32_t event));
  208. rt_err_t rt_data_queue_push(struct rt_data_queue *queue,
  209. const void *data_ptr,
  210. rt_size_t data_size,
  211. rt_int32_t timeout);
  212. rt_err_t rt_data_queue_pop(struct rt_data_queue *queue,
  213. const void **data_ptr,
  214. rt_size_t *size,
  215. rt_int32_t timeout);
  216. rt_err_t rt_data_queue_peak(struct rt_data_queue *queue,
  217. const void **data_ptr,
  218. rt_size_t *size);
  219. void rt_data_queue_reset(struct rt_data_queue *queue);
  220. #ifdef RT_USING_RTC
  221. #include "drivers/rtc.h"
  222. #ifdef RT_USING_ALARM
  223. #include "drivers/alarm.h"
  224. #endif
  225. #endif /* RT_USING_RTC */
  226. #ifdef RT_USING_SPI
  227. #include "drivers/spi.h"
  228. #endif /* RT_USING_SPI */
  229. #ifdef RT_USING_MTD_NOR
  230. #include "drivers/mtd_nor.h"
  231. #endif /* RT_USING_MTD_NOR */
  232. #ifdef RT_USING_MTD_NAND
  233. #include "drivers/mtd_nand.h"
  234. #endif /* RT_USING_MTD_NAND */
  235. #ifdef RT_USING_USB_DEVICE
  236. #include "drivers/usb_device.h"
  237. #endif /* RT_USING_USB_DEVICE */
  238. #ifdef RT_USING_USB_HOST
  239. #include "drivers/usb_host.h"
  240. #endif /* RT_USING_USB_HOST */
  241. #ifdef RT_USING_SERIAL
  242. #include "drivers/serial.h"
  243. #endif /* RT_USING_SERIAL */
  244. #ifdef RT_USING_I2C
  245. #include "drivers/i2c.h"
  246. #include "drivers/i2c_dev.h"
  247. #ifdef RT_USING_I2C_BITOPS
  248. #include "drivers/i2c-bit-ops.h"
  249. #endif /* RT_USING_I2C_BITOPS */
  250. #endif /* RT_USING_I2C */
  251. #ifdef RT_USING_SDIO
  252. #include "drivers/mmcsd_core.h"
  253. #include "drivers/sd.h"
  254. #include "drivers/sdio.h"
  255. #endif
  256. #endif /* __RT_DEVICE_H__ */