audio_pipe.c 7.7 KB

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