dev_audio_pipe.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2012-09-30 Bernard first version.
  9. */
  10. #include <rthw.h>
  11. #include <rtdevice.h>
  12. #include "dev_audio_pipe.h"
  13. static void _rt_audio_pipe_resume_writer(struct rt_audio_pipe *pipe)
  14. {
  15. if (!rt_list_isempty(&pipe->suspended_write_list))
  16. {
  17. rt_thread_t thread;
  18. RT_ASSERT(pipe->flag & RT_PIPE_FLAG_BLOCK_WR);
  19. /* get suspended thread */
  20. thread = RT_THREAD_LIST_NODE_ENTRY(pipe->suspended_write_list.next);
  21. /* resume the write thread */
  22. rt_thread_resume(thread);
  23. rt_schedule();
  24. }
  25. }
  26. static rt_ssize_t rt_audio_pipe_read(rt_device_t dev,
  27. rt_off_t pos,
  28. void *buffer,
  29. rt_size_t size)
  30. {
  31. rt_base_t level;
  32. rt_thread_t thread;
  33. struct rt_audio_pipe *pipe;
  34. rt_size_t read_nbytes;
  35. pipe = (struct rt_audio_pipe *)dev;
  36. RT_ASSERT(pipe != RT_NULL);
  37. if (!(pipe->flag & RT_PIPE_FLAG_BLOCK_RD))
  38. {
  39. level = rt_hw_interrupt_disable();
  40. read_nbytes = rt_ringbuffer_get(&(pipe->ringbuffer), (rt_uint8_t *)buffer, size);
  41. /* if the ringbuffer is empty, there won't be any writer waiting */
  42. if (read_nbytes)
  43. _rt_audio_pipe_resume_writer(pipe);
  44. rt_hw_interrupt_enable(level);
  45. return read_nbytes;
  46. }
  47. thread = rt_thread_self();
  48. /* current context checking */
  49. RT_DEBUG_NOT_IN_INTERRUPT;
  50. do
  51. {
  52. level = rt_hw_interrupt_disable();
  53. read_nbytes = rt_ringbuffer_get(&(pipe->ringbuffer), (rt_uint8_t *)buffer, size);
  54. if (read_nbytes == 0)
  55. {
  56. rt_thread_suspend(thread);
  57. /* waiting on suspended read list */
  58. rt_list_insert_before(&(pipe->suspended_read_list),
  59. &RT_THREAD_LIST_NODE(thread));
  60. rt_hw_interrupt_enable(level);
  61. rt_schedule();
  62. }
  63. else
  64. {
  65. _rt_audio_pipe_resume_writer(pipe);
  66. rt_hw_interrupt_enable(level);
  67. break;
  68. }
  69. }
  70. while (read_nbytes == 0);
  71. return read_nbytes;
  72. }
  73. static void _rt_audio_pipe_resume_reader(struct rt_audio_pipe *pipe)
  74. {
  75. if (pipe->parent.rx_indicate)
  76. pipe->parent.rx_indicate(&pipe->parent,
  77. rt_ringbuffer_data_len(&pipe->ringbuffer));
  78. if (!rt_list_isempty(&pipe->suspended_read_list))
  79. {
  80. rt_thread_t thread;
  81. RT_ASSERT(pipe->flag & RT_PIPE_FLAG_BLOCK_RD);
  82. /* get suspended thread */
  83. thread = RT_THREAD_LIST_NODE_ENTRY(pipe->suspended_read_list.next);
  84. /* resume the read thread */
  85. rt_thread_resume(thread);
  86. rt_schedule();
  87. }
  88. }
  89. static rt_ssize_t rt_audio_pipe_write(rt_device_t dev,
  90. rt_off_t pos,
  91. const void *buffer,
  92. rt_size_t size)
  93. {
  94. rt_base_t level;
  95. rt_thread_t thread;
  96. struct rt_audio_pipe *pipe;
  97. rt_size_t write_nbytes;
  98. pipe = (struct rt_audio_pipe *)dev;
  99. RT_ASSERT(pipe != RT_NULL);
  100. if ((pipe->flag & RT_PIPE_FLAG_FORCE_WR) ||
  101. !(pipe->flag & RT_PIPE_FLAG_BLOCK_WR))
  102. {
  103. level = rt_hw_interrupt_disable();
  104. if (pipe->flag & RT_PIPE_FLAG_FORCE_WR)
  105. write_nbytes = rt_ringbuffer_put_force(&(pipe->ringbuffer),
  106. (const rt_uint8_t *)buffer, size);
  107. else
  108. write_nbytes = rt_ringbuffer_put(&(pipe->ringbuffer),
  109. (const rt_uint8_t *)buffer, size);
  110. _rt_audio_pipe_resume_reader(pipe);
  111. rt_hw_interrupt_enable(level);
  112. return write_nbytes;
  113. }
  114. thread = rt_thread_self();
  115. /* current context checking */
  116. RT_DEBUG_NOT_IN_INTERRUPT;
  117. do
  118. {
  119. level = rt_hw_interrupt_disable();
  120. write_nbytes = rt_ringbuffer_put(&(pipe->ringbuffer), (const rt_uint8_t *)buffer, size);
  121. if (write_nbytes == 0)
  122. {
  123. /* pipe full, waiting on suspended write list */
  124. rt_thread_suspend(thread);
  125. /* waiting on suspended read list */
  126. rt_list_insert_before(&(pipe->suspended_write_list),
  127. &RT_THREAD_LIST_NODE(thread));
  128. rt_hw_interrupt_enable(level);
  129. rt_schedule();
  130. }
  131. else
  132. {
  133. _rt_audio_pipe_resume_reader(pipe);
  134. rt_hw_interrupt_enable(level);
  135. break;
  136. }
  137. }
  138. while (write_nbytes == 0);
  139. return write_nbytes;
  140. }
  141. static rt_err_t rt_audio_pipe_control(rt_device_t dev, int cmd, void *args)
  142. {
  143. struct rt_audio_pipe *pipe;
  144. pipe = (struct rt_audio_pipe *)dev;
  145. if (cmd == PIPE_CTRL_GET_SPACE && args)
  146. *(rt_size_t *)args = rt_ringbuffer_space_len(&pipe->ringbuffer);
  147. return RT_EOK;
  148. }
  149. #ifdef RT_USING_DEVICE_OPS
  150. const static struct rt_device_ops audio_pipe_ops =
  151. {
  152. RT_NULL,
  153. RT_NULL,
  154. RT_NULL,
  155. rt_audio_pipe_read,
  156. rt_audio_pipe_write,
  157. rt_audio_pipe_control
  158. };
  159. #endif
  160. /**
  161. * This function will initialize a pipe device and put it under control of
  162. * resource management.
  163. *
  164. * @param pipe the pipe device
  165. * @param name the name of pipe device
  166. * @param flag the attribute of the pipe device
  167. * @param buf the buffer of pipe device
  168. * @param size the size of pipe device buffer
  169. *
  170. * @return the operation status, RT_EOK on successful
  171. */
  172. rt_err_t rt_audio_pipe_init(struct rt_audio_pipe *pipe,
  173. const char *name,
  174. rt_int32_t flag,
  175. rt_uint8_t *buf,
  176. rt_size_t size)
  177. {
  178. RT_ASSERT(pipe);
  179. RT_ASSERT(buf);
  180. /* initialize suspended list */
  181. rt_list_init(&pipe->suspended_read_list);
  182. rt_list_init(&pipe->suspended_write_list);
  183. /* initialize ring buffer */
  184. rt_ringbuffer_init(&pipe->ringbuffer, buf, size);
  185. pipe->flag = flag;
  186. /* create pipe */
  187. pipe->parent.type = RT_Device_Class_Pipe;
  188. #ifdef RT_USING_DEVICE_OPS
  189. pipe->parent.ops = &audio_pipe_ops;
  190. #else
  191. pipe->parent.init = RT_NULL;
  192. pipe->parent.open = RT_NULL;
  193. pipe->parent.close = RT_NULL;
  194. pipe->parent.read = rt_audio_pipe_read;
  195. pipe->parent.write = rt_audio_pipe_write;
  196. pipe->parent.control = rt_audio_pipe_control;
  197. #endif
  198. return rt_device_register(&(pipe->parent), name, RT_DEVICE_FLAG_RDWR);
  199. }
  200. /**
  201. * This function will detach a pipe device from resource management
  202. *
  203. * @param pipe the pipe device
  204. *
  205. * @return the operation status, RT_EOK on successful
  206. */
  207. rt_err_t rt_audio_pipe_detach(struct rt_audio_pipe *pipe)
  208. {
  209. return rt_device_unregister(&pipe->parent);
  210. }
  211. #ifdef RT_USING_HEAP
  212. rt_err_t rt_audio_pipe_create(const char *name, rt_int32_t flag, rt_size_t size)
  213. {
  214. rt_uint8_t *rb_memptr = RT_NULL;
  215. struct rt_audio_pipe *pipe = RT_NULL;
  216. /* get aligned size */
  217. size = RT_ALIGN(size, RT_ALIGN_SIZE);
  218. pipe = (struct rt_audio_pipe *)rt_calloc(1, sizeof(struct rt_audio_pipe));
  219. if (pipe == RT_NULL)
  220. return -RT_ENOMEM;
  221. /* create ring buffer of pipe */
  222. rb_memptr = (rt_uint8_t *)rt_malloc(size);
  223. if (rb_memptr == RT_NULL)
  224. {
  225. rt_free(pipe);
  226. return -RT_ENOMEM;
  227. }
  228. return rt_audio_pipe_init(pipe, name, flag, rb_memptr, size);
  229. }
  230. void rt_audio_pipe_destroy(struct rt_audio_pipe *pipe)
  231. {
  232. if (pipe == RT_NULL)
  233. return;
  234. /* un-register pipe device */
  235. rt_audio_pipe_detach(pipe);
  236. /* release memory */
  237. rt_free(pipe->ringbuffer.buffer_ptr);
  238. rt_free(pipe);
  239. return;
  240. }
  241. #endif /* RT_USING_HEAP */