rtdevice.h 11 KB

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