dev_audio_pipe.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /*
  2. * Copyright (c) 2006-2025 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. * 2025-03-04 wumingzi add doxygen comments.
  10. */
  11. #include <rthw.h>
  12. #include <rtdevice.h>
  13. #include "dev_audio_pipe.h"
  14. static void _rt_audio_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_THREAD_LIST_NODE_ENTRY(pipe->suspended_write_list.next);
  22. /* resume the write thread */
  23. rt_thread_resume(thread);
  24. rt_schedule();
  25. }
  26. }
  27. /**
  28. * @brief Read audio pipe
  29. *
  30. * @param[in] dev pointer to audio device will be read
  31. *
  32. * @param[in] pos useless param
  33. *
  34. * @param[in] buffer pointer to ringbuffer of audio pipe to be read
  35. *
  36. * @param[in] size number of bytes will be read
  37. *
  38. * @return number of read bytes
  39. *
  40. * @note This function will execute time-consuming or affecting the
  41. * system operations like memcpy and disable interrupt.
  42. */
  43. static rt_ssize_t rt_audio_pipe_read(rt_device_t dev,
  44. rt_off_t pos,
  45. void *buffer,
  46. rt_size_t size)
  47. {
  48. rt_base_t level;
  49. rt_thread_t thread;
  50. struct rt_audio_pipe *pipe;
  51. rt_size_t read_nbytes;
  52. pipe = (struct rt_audio_pipe *)dev;
  53. RT_ASSERT(pipe != RT_NULL);
  54. if (!(pipe->flag & RT_PIPE_FLAG_BLOCK_RD))
  55. {
  56. level = rt_hw_interrupt_disable();
  57. read_nbytes = rt_ringbuffer_get(&(pipe->ringbuffer), (rt_uint8_t *)buffer, size);
  58. /* if the ringbuffer is empty, there won't be any writer waiting */
  59. if (read_nbytes)
  60. _rt_audio_pipe_resume_writer(pipe);
  61. rt_hw_interrupt_enable(level);
  62. return read_nbytes;
  63. }
  64. thread = rt_thread_self();
  65. /* current context checking */
  66. RT_DEBUG_NOT_IN_INTERRUPT;
  67. do
  68. {
  69. level = rt_hw_interrupt_disable();
  70. read_nbytes = rt_ringbuffer_get(&(pipe->ringbuffer), (rt_uint8_t *)buffer, size);
  71. if (read_nbytes == 0)
  72. {
  73. rt_thread_suspend(thread);
  74. /* waiting on suspended read list */
  75. rt_list_insert_before(&(pipe->suspended_read_list),
  76. &RT_THREAD_LIST_NODE(thread));
  77. rt_hw_interrupt_enable(level);
  78. rt_schedule();
  79. }
  80. else
  81. {
  82. _rt_audio_pipe_resume_writer(pipe);
  83. rt_hw_interrupt_enable(level);
  84. break;
  85. }
  86. }
  87. while (read_nbytes == 0);
  88. return read_nbytes;
  89. }
  90. /**
  91. * @brief Resume audio pipe reader thread
  92. *
  93. * @param[in] pipe pointer to suspended audio pipe thread
  94. */
  95. static void _rt_audio_pipe_resume_reader(struct rt_audio_pipe *pipe)
  96. {
  97. if (pipe->parent.rx_indicate)
  98. pipe->parent.rx_indicate(&pipe->parent,
  99. rt_ringbuffer_data_len(&pipe->ringbuffer));
  100. if (!rt_list_isempty(&pipe->suspended_read_list))
  101. {
  102. rt_thread_t thread;
  103. RT_ASSERT(pipe->flag & RT_PIPE_FLAG_BLOCK_RD);
  104. /* get suspended thread */
  105. thread = RT_THREAD_LIST_NODE_ENTRY(pipe->suspended_read_list.next);
  106. /* resume the read thread */
  107. rt_thread_resume(thread);
  108. rt_schedule();
  109. }
  110. }
  111. /**
  112. * @brief Write data into audio pipe
  113. *
  114. * @param[in] dev pointer to audio pipe that has been configured
  115. *
  116. * @param[in] pos useless param
  117. *
  118. * @param[in] buffer pointer to buffer of ringbuffer
  119. *
  120. * @param[in] size size of data will be written
  121. *
  122. * @return number of written bytes
  123. *
  124. * @note The function will disable interrupt and may suspend current thread
  125. */
  126. static rt_ssize_t rt_audio_pipe_write(rt_device_t dev,
  127. rt_off_t pos,
  128. const void *buffer,
  129. rt_size_t size)
  130. {
  131. rt_base_t level;
  132. rt_thread_t thread;
  133. struct rt_audio_pipe *pipe;
  134. rt_size_t write_nbytes;
  135. pipe = (struct rt_audio_pipe *)dev;
  136. RT_ASSERT(pipe != RT_NULL);
  137. if ((pipe->flag & RT_PIPE_FLAG_FORCE_WR) ||
  138. !(pipe->flag & RT_PIPE_FLAG_BLOCK_WR))
  139. {
  140. level = rt_hw_interrupt_disable();
  141. if (pipe->flag & RT_PIPE_FLAG_FORCE_WR)
  142. write_nbytes = rt_ringbuffer_put_force(&(pipe->ringbuffer),
  143. (const rt_uint8_t *)buffer, size);
  144. else
  145. write_nbytes = rt_ringbuffer_put(&(pipe->ringbuffer),
  146. (const rt_uint8_t *)buffer, size);
  147. _rt_audio_pipe_resume_reader(pipe);
  148. rt_hw_interrupt_enable(level);
  149. return write_nbytes;
  150. }
  151. thread = rt_thread_self();
  152. /* current context checking */
  153. RT_DEBUG_NOT_IN_INTERRUPT;
  154. do
  155. {
  156. level = rt_hw_interrupt_disable();
  157. write_nbytes = rt_ringbuffer_put(&(pipe->ringbuffer), (const rt_uint8_t *)buffer, size);
  158. if (write_nbytes == 0)
  159. {
  160. /* pipe full, waiting on suspended write list */
  161. rt_thread_suspend(thread);
  162. /* waiting on suspended read list */
  163. rt_list_insert_before(&(pipe->suspended_write_list),
  164. &RT_THREAD_LIST_NODE(thread));
  165. rt_hw_interrupt_enable(level);
  166. rt_schedule();
  167. }
  168. else
  169. {
  170. _rt_audio_pipe_resume_reader(pipe);
  171. rt_hw_interrupt_enable(level);
  172. break;
  173. }
  174. }
  175. while (write_nbytes == 0);
  176. return write_nbytes;
  177. }
  178. /**
  179. * @brief Control audio pipe
  180. *
  181. * @param[in] dev pointer to pipe
  182. *
  183. * @param[in] cmd control command
  184. *
  185. * @param[in] args control argument
  186. *
  187. * @return error code, RT_EOK is successful otherwise means failure
  188. */
  189. static rt_err_t rt_audio_pipe_control(rt_device_t dev, int cmd, void *args)
  190. {
  191. struct rt_audio_pipe *pipe;
  192. pipe = (struct rt_audio_pipe *)dev;
  193. if (cmd == PIPE_CTRL_GET_SPACE && args)
  194. *(rt_size_t *)args = rt_ringbuffer_space_len(&pipe->ringbuffer);
  195. return RT_EOK;
  196. }
  197. #ifdef RT_USING_DEVICE_OPS
  198. const static struct rt_device_ops audio_pipe_ops =
  199. {
  200. RT_NULL,
  201. RT_NULL,
  202. RT_NULL,
  203. rt_audio_pipe_read,
  204. rt_audio_pipe_write,
  205. rt_audio_pipe_control
  206. };
  207. #endif
  208. /**
  209. * @brief Init audio pipe
  210. *
  211. * This function will initialize a pipe device and put it under control of
  212. * resource management.
  213. *
  214. * @param pipe the pipe device
  215. *
  216. * @param name the name of pipe device
  217. *
  218. * @param flag the attribute of the pipe device
  219. *
  220. * @param buf the buffer of pipe device
  221. *
  222. * @param size the size of pipe device buffer
  223. *
  224. * @return the operation status, RT_EOK on successful
  225. */
  226. rt_err_t rt_audio_pipe_init(struct rt_audio_pipe *pipe,
  227. const char *name,
  228. rt_int32_t flag,
  229. rt_uint8_t *buf,
  230. rt_size_t size)
  231. {
  232. RT_ASSERT(pipe);
  233. RT_ASSERT(buf);
  234. /* initialize suspended list */
  235. rt_list_init(&pipe->suspended_read_list);
  236. rt_list_init(&pipe->suspended_write_list);
  237. /* initialize ring buffer */
  238. rt_ringbuffer_init(&pipe->ringbuffer, buf, size);
  239. pipe->flag = flag;
  240. /* create pipe */
  241. pipe->parent.type = RT_Device_Class_Pipe;
  242. #ifdef RT_USING_DEVICE_OPS
  243. pipe->parent.ops = &audio_pipe_ops;
  244. #else
  245. pipe->parent.init = RT_NULL;
  246. pipe->parent.open = RT_NULL;
  247. pipe->parent.close = RT_NULL;
  248. pipe->parent.read = rt_audio_pipe_read;
  249. pipe->parent.write = rt_audio_pipe_write;
  250. pipe->parent.control = rt_audio_pipe_control;
  251. #endif
  252. return rt_device_register(&(pipe->parent), name, RT_DEVICE_FLAG_RDWR);
  253. }
  254. /**
  255. * @brief This function will detach a pipe device from resource management
  256. *
  257. * @param pipe the pipe device
  258. *
  259. * @return the operation status, RT_EOK on successful
  260. */
  261. rt_err_t rt_audio_pipe_detach(struct rt_audio_pipe *pipe)
  262. {
  263. return rt_device_unregister(&pipe->parent);
  264. }
  265. /**
  266. * @brief Creat audio pipe
  267. *
  268. * @param[in] name pipe name
  269. *
  270. * @param[in] flag pipe flags, it can be one of enum rt_audio_pipe_flag items
  271. *
  272. * @param[in] size ringbuffer size
  273. *
  274. * @return error code, RT_EOK on initialization successfully
  275. *
  276. * @note depend on RT_USING_HEAP
  277. */
  278. #ifdef RT_USING_HEAP
  279. rt_err_t rt_audio_pipe_create(const char *name, rt_int32_t flag, rt_size_t size)
  280. {
  281. rt_uint8_t *rb_memptr = RT_NULL;
  282. struct rt_audio_pipe *pipe = RT_NULL;
  283. /* get aligned size */
  284. size = RT_ALIGN(size, RT_ALIGN_SIZE);
  285. pipe = (struct rt_audio_pipe *)rt_calloc(1, sizeof(struct rt_audio_pipe));
  286. if (pipe == RT_NULL)
  287. return -RT_ENOMEM;
  288. /* create ring buffer of pipe */
  289. rb_memptr = (rt_uint8_t *)rt_malloc(size);
  290. if (rb_memptr == RT_NULL)
  291. {
  292. rt_free(pipe);
  293. return -RT_ENOMEM;
  294. }
  295. return rt_audio_pipe_init(pipe, name, flag, rb_memptr, size);
  296. }
  297. /**
  298. * @brief Detachaudio pipe and free its ringbuffer
  299. *
  300. * @param[in] pipe pointer to the pipe will be destory
  301. *
  302. * @note depend on RT_USING_HEAP
  303. */
  304. void rt_audio_pipe_destroy(struct rt_audio_pipe *pipe)
  305. {
  306. if (pipe == RT_NULL)
  307. return;
  308. /* un-register pipe device */
  309. rt_audio_pipe_detach(pipe);
  310. /* release memory */
  311. rt_free(pipe->ringbuffer.buffer_ptr);
  312. rt_free(pipe);
  313. return;
  314. }
  315. #endif /* RT_USING_HEAP */