1
0

rtdevice.h 11 KB

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