dev_audio_pipe.c 9.2 KB

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