audio_pipe.c 7.7 KB

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