1
0

rtdevice.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. * 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-01-08 bernard first version.
  13. */
  14. #ifndef __RT_DEVICE_H__
  15. #define __RT_DEVICE_H__
  16. #include <rtthread.h>
  17. #define RT_DEVICE(device) ((rt_device_t)device)
  18. /* completion flag */
  19. struct rt_completion
  20. {
  21. rt_uint32_t flag;
  22. /* suspended list */
  23. rt_list_t suspended_list;
  24. };
  25. /* ring buffer */
  26. struct rt_ringbuffer
  27. {
  28. rt_uint8_t *buffer_ptr;
  29. /* use the msb of the {read,write}_index as mirror bit. You can see this as
  30. * if the buffer adds a virtual mirror and the pointers point either to the
  31. * normal or to the mirrored buffer. If the write_index has the same value
  32. * with the read_index, but in differenct mirro, the buffer is full. While
  33. * if the write_index and the read_index are the same and within the same
  34. * mirror, the buffer is empty. The ASCII art of the ringbuffer is:
  35. *
  36. * mirror = 0 mirror = 1
  37. * +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
  38. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 ||| 0 | 1 | 2 | 3 | 4 | 5 | 6 | Full
  39. * +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
  40. * read_idx-^ write_idx-^
  41. *
  42. * +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
  43. * | 0 | 1 | 2 | 3 | 4 | 5 | 6 ||| 0 | 1 | 2 | 3 | 4 | 5 | 6 | Empty
  44. * +---+---+---+---+---+---+---+|+~~~+~~~+~~~+~~~+~~~+~~~+~~~+
  45. * read_idx-^ ^-write_idx
  46. *
  47. * The tradeoff is we could only use 32KiB of buffer for 16 bit of index.
  48. * But it should be enough for most of the cases.
  49. *
  50. * Ref: http://en.wikipedia.org/wiki/Circular_buffer#Mirroring */
  51. rt_uint16_t read_mirror : 1;
  52. rt_uint16_t read_index : 15;
  53. rt_uint16_t write_mirror : 1;
  54. rt_uint16_t write_index : 15;
  55. /* as we use msb of index as mirror bit, the size should be signed and
  56. * could only be positive. */
  57. rt_int16_t buffer_size;
  58. };
  59. /** return the size of data in rb */
  60. rt_inline rt_uint16_t RT_RINGBUFFER_SIZE(struct rt_ringbuffer *rb)
  61. {
  62. if (rb->read_index == rb->write_index)
  63. {
  64. if (rb->read_mirror == rb->write_mirror)
  65. /* we are in the same side, the ringbuffer is empty. */
  66. return 0;
  67. else
  68. return rb->buffer_size;
  69. }
  70. else
  71. {
  72. if (rb->write_index > rb->read_index)
  73. return rb->write_index - rb->read_index;
  74. else
  75. return rb->buffer_size - (rb->read_index - rb->write_index);
  76. }
  77. }
  78. /** return the size of empty space in rb */
  79. #define RT_RINGBUFFER_EMPTY(rb) ((rb)->buffer_size - RT_RINGBUFFER_SIZE(rb))
  80. /* pipe device */
  81. #define PIPE_DEVICE(device) ((struct rt_pipe_device*)(device))
  82. struct rt_pipe_device
  83. {
  84. struct rt_device parent;
  85. /* ring buffer in pipe device */
  86. struct rt_ringbuffer ringbuffer;
  87. /* suspended list */
  88. rt_list_t suspended_read_list;
  89. rt_list_t suspended_write_list;
  90. };
  91. #define RT_DATAQUEUE_EVENT_UNKNOWN 0x00
  92. #define RT_DATAQUEUE_EVENT_POP 0x01
  93. #define RT_DATAQUEUE_EVENT_PUSH 0x02
  94. #define RT_DATAQUEUE_EVENT_LWM 0x03
  95. struct rt_data_item;
  96. #define RT_DATAQUEUE_SIZE(dq) ((dq)->put_index - (dq)->get_index)
  97. #define RT_DATAQUEUE_EMPTY(dq) ((dq)->size - RT_DATAQUEUE_SIZE(dq))
  98. /* data queue implementation */
  99. struct rt_data_queue
  100. {
  101. rt_uint16_t size;
  102. rt_uint16_t lwm;
  103. rt_bool_t waiting_lwm;
  104. rt_uint16_t get_index;
  105. rt_uint16_t put_index;
  106. struct rt_data_item *queue;
  107. rt_list_t suspended_push_list;
  108. rt_list_t suspended_pop_list;
  109. /* event notify */
  110. void (*evt_notify)(struct rt_data_queue *queue, rt_uint32_t event);
  111. };
  112. /**
  113. * Completion
  114. */
  115. void rt_completion_init(struct rt_completion *completion);
  116. rt_err_t rt_completion_wait(struct rt_completion *completion,
  117. rt_int32_t timeout);
  118. void rt_completion_done(struct rt_completion *completion);
  119. /**
  120. * RingBuffer for DeviceDriver
  121. *
  122. * Please note that the ring buffer implementation of RT-Thread
  123. * has no thread wait or resume feature.
  124. */
  125. void rt_ringbuffer_init(struct rt_ringbuffer *rb,
  126. rt_uint8_t *pool,
  127. rt_int16_t size);
  128. rt_size_t rt_ringbuffer_put(struct rt_ringbuffer *rb,
  129. const rt_uint8_t *ptr,
  130. rt_uint16_t length);
  131. rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer *rb,
  132. const rt_uint8_t ch);
  133. rt_size_t rt_ringbuffer_get(struct rt_ringbuffer *rb,
  134. rt_uint8_t *ptr,
  135. rt_uint16_t length);
  136. rt_size_t rt_ringbuffer_getchar(struct rt_ringbuffer *rb, rt_uint8_t *ch);
  137. rt_inline rt_uint16_t rt_ringbuffer_get_size(struct rt_ringbuffer *rb)
  138. {
  139. RT_ASSERT(rb != RT_NULL);
  140. return rb->buffer_size;
  141. }
  142. /**
  143. * Pipe Device
  144. */
  145. rt_err_t rt_pipe_create(const char *name, rt_size_t size);
  146. void rt_pipe_destroy(struct rt_pipe_device *pipe);
  147. /**
  148. * DataQueue for DeviceDriver
  149. */
  150. rt_err_t rt_data_queue_init(struct rt_data_queue *queue,
  151. rt_uint16_t size,
  152. rt_uint16_t lwm,
  153. void (*evt_notify)(struct rt_data_queue *queue, rt_uint32_t event));
  154. rt_err_t rt_data_queue_push(struct rt_data_queue *queue,
  155. const void *data_ptr,
  156. rt_size_t data_size,
  157. rt_int32_t timeout);
  158. rt_err_t rt_data_queue_pop(struct rt_data_queue *queue,
  159. const void **data_ptr,
  160. rt_size_t *size,
  161. rt_int32_t timeout);
  162. rt_err_t rt_data_queue_peak(struct rt_data_queue *queue,
  163. const void **data_ptr,
  164. rt_size_t *size);
  165. void rt_data_queue_reset(struct rt_data_queue *queue);
  166. #ifdef RT_USING_RTC
  167. #include "drivers/rtc.h"
  168. #ifdef RT_USING_ALARM
  169. #include "drivers/alarm.h"
  170. #endif
  171. #endif /* RT_USING_RTC */
  172. #ifdef RT_USING_SPI
  173. #include "drivers/spi.h"
  174. #endif /* RT_USING_SPI */
  175. #ifdef RT_USING_MTD_NOR
  176. #include "drivers/mtd_nor.h"
  177. #endif /* RT_USING_MTD_NOR */
  178. #ifdef RT_USING_MTD_NAND
  179. #include "drivers/mtd_nand.h"
  180. #endif /* RT_USING_MTD_NAND */
  181. #ifdef RT_USING_USB_DEVICE
  182. #include "drivers/usb_device.h"
  183. #endif /* RT_USING_USB_DEVICE */
  184. #ifdef RT_USING_USB_HOST
  185. #include "drivers/usb_host.h"
  186. #endif /* RT_USING_USB_HOST */
  187. #ifdef RT_USING_SERIAL
  188. #include "drivers/serial.h"
  189. #endif /* RT_USING_SERIAL */
  190. #ifdef RT_USING_I2C
  191. #include "drivers/i2c.h"
  192. #include "drivers/i2c_dev.h"
  193. #ifdef RT_USING_I2C_BITOPS
  194. #include "drivers/i2c-bit-ops.h"
  195. #endif /* RT_USING_I2C_BITOPS */
  196. #endif /* RT_USING_I2C */
  197. #ifdef RT_USING_SDIO
  198. #include "drivers/mmcsd_core.h"
  199. #include "drivers/sd.h"
  200. #include "drivers/sdio.h"
  201. #endif
  202. #endif /* __RT_DEVICE_H__ */