audio_pipe.c 7.8 KB

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