audio_pipe.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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), 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. level = rt_hw_interrupt_disable();
  55. read_nbytes = rt_ringbuffer_get(&(pipe->ringbuffer), 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. } 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. buffer, size);
  110. else
  111. write_nbytes = rt_ringbuffer_put(&(pipe->ringbuffer),
  112. 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. level = rt_hw_interrupt_disable();
  122. write_nbytes = rt_ringbuffer_put(&(pipe->ringbuffer), buffer, size);
  123. if (write_nbytes == 0)
  124. {
  125. /* pipe full, waiting on suspended write list */
  126. rt_thread_suspend(thread);
  127. /* waiting on suspended read list */
  128. rt_list_insert_before(&(pipe->suspended_write_list),
  129. &(thread->tlist));
  130. rt_hw_interrupt_enable(level);
  131. rt_schedule();
  132. }
  133. else
  134. {
  135. _rt_pipe_resume_reader(pipe);
  136. rt_hw_interrupt_enable(level);
  137. break;
  138. }
  139. } while (write_nbytes == 0);
  140. return write_nbytes;
  141. }
  142. static rt_err_t rt_pipe_control(rt_device_t dev, int cmd, void *args)
  143. {
  144. struct rt_audio_pipe *pipe;
  145. pipe = (struct rt_audio_pipe *)dev;
  146. if (cmd == PIPE_CTRL_GET_SPACE && args)
  147. *(rt_size_t*)args = rt_ringbuffer_space_len(&pipe->ringbuffer);
  148. return RT_EOK;
  149. }
  150. #ifdef RT_USING_DEVICE_OPS
  151. const static struct rt_device_ops audio_pipe_ops
  152. {
  153. RT_NULL,
  154. RT_NULL,
  155. RT_NULL,
  156. rt_pipe_read,
  157. rt_pipe_write,
  158. rt_pipe_control
  159. };
  160. #endif
  161. /**
  162. * This function will initialize a pipe device and put it under control of
  163. * resource management.
  164. *
  165. * @param pipe the pipe device
  166. * @param name the name of pipe device
  167. * @param flag the attribute of the pipe device
  168. * @param buf the buffer of pipe device
  169. * @param size the size of pipe device buffer
  170. *
  171. * @return the operation status, RT_EOK on successful
  172. */
  173. rt_err_t rt_audio_pipe_init(struct rt_audio_pipe *pipe,
  174. const char *name,
  175. enum rt_audio_pipe_flag flag,
  176. rt_uint8_t *buf,
  177. rt_size_t size)
  178. {
  179. RT_ASSERT(pipe);
  180. RT_ASSERT(buf);
  181. /* initialize suspended list */
  182. rt_list_init(&pipe->suspended_read_list);
  183. rt_list_init(&pipe->suspended_write_list);
  184. /* initialize ring buffer */
  185. rt_ringbuffer_init(&pipe->ringbuffer, buf, size);
  186. pipe->flag = flag;
  187. /* create pipe */
  188. pipe->parent.type = RT_Device_Class_Pipe;
  189. #ifdef RT_USING_DEVICE_OPS
  190. pipe->parent.ops = &audio_pipe_ops;
  191. #else
  192. pipe->parent.init = RT_NULL;
  193. pipe->parent.open = RT_NULL;
  194. pipe->parent.close = RT_NULL;
  195. pipe->parent.read = rt_pipe_read;
  196. pipe->parent.write = rt_pipe_write;
  197. pipe->parent.control = rt_pipe_control;
  198. #endif
  199. return rt_device_register(&(pipe->parent), name, RT_DEVICE_FLAG_RDWR);
  200. }
  201. /**
  202. * This function will detach a pipe device from resource management
  203. *
  204. * @param pipe the pipe device
  205. *
  206. * @return the operation status, RT_EOK on successful
  207. */
  208. rt_err_t rt_audio_pipe_detach(struct rt_audio_pipe *pipe)
  209. {
  210. return rt_device_unregister(&pipe->parent);
  211. }
  212. #ifdef RT_USING_HEAP
  213. rt_err_t rt_audio_pipe_create(const char *name, enum rt_audio_pipe_flag flag, rt_size_t size)
  214. {
  215. rt_uint8_t *rb_memptr = RT_NULL;
  216. struct rt_audio_pipe *pipe = RT_NULL;
  217. /* get aligned size */
  218. size = RT_ALIGN(size, RT_ALIGN_SIZE);
  219. pipe = (struct rt_audio_pipe *)rt_calloc(1, sizeof(struct rt_audio_pipe));
  220. if (pipe == RT_NULL)
  221. return -RT_ENOMEM;
  222. /* create ring buffer of pipe */
  223. rb_memptr = rt_malloc(size);
  224. if (rb_memptr == RT_NULL)
  225. {
  226. rt_free(pipe);
  227. return -RT_ENOMEM;
  228. }
  229. return rt_audio_pipe_init(pipe, name, flag, rb_memptr, size);
  230. }
  231. void rt_audio_pipe_destroy(struct rt_audio_pipe *pipe)
  232. {
  233. if (pipe == RT_NULL)
  234. return;
  235. /* un-register pipe device */
  236. rt_audio_pipe_detach(pipe);
  237. /* release memory */
  238. rt_free(pipe->ringbuffer.buffer_ptr);
  239. rt_free(pipe);
  240. return;
  241. }
  242. #endif /* RT_USING_HEAP */