audio_pipe.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * File : pipe.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2012-09-30 Bernard first version.
  23. */
  24. #include <rthw.h>
  25. #include <rtthread.h>
  26. #include <rtdevice.h>
  27. #include "audio_pipe.h"
  28. static void _rt_pipe_resume_writer(struct rt_audio_pipe *pipe)
  29. {
  30. if (!rt_list_isempty(&pipe->suspended_write_list))
  31. {
  32. rt_thread_t thread;
  33. RT_ASSERT(pipe->flag & RT_PIPE_FLAG_BLOCK_WR);
  34. /* get suspended thread */
  35. thread = rt_list_entry(pipe->suspended_write_list.next,
  36. struct rt_thread,
  37. tlist);
  38. /* resume the write thread */
  39. rt_thread_resume(thread);
  40. rt_schedule();
  41. }
  42. }
  43. static rt_size_t rt_pipe_read(rt_device_t dev,
  44. rt_off_t pos,
  45. void *buffer,
  46. rt_size_t size)
  47. {
  48. rt_uint32_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), buffer, size);
  58. /* if the ringbuffer is empty, there won't be any writer waiting */
  59. if (read_nbytes)
  60. _rt_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. level = rt_hw_interrupt_disable();
  69. read_nbytes = rt_ringbuffer_get(&(pipe->ringbuffer), 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. &(thread->tlist));
  76. rt_hw_interrupt_enable(level);
  77. rt_schedule();
  78. }
  79. else
  80. {
  81. _rt_pipe_resume_writer(pipe);
  82. rt_hw_interrupt_enable(level);
  83. break;
  84. }
  85. } while (read_nbytes == 0);
  86. return read_nbytes;
  87. }
  88. static void _rt_pipe_resume_reader(struct rt_audio_pipe *pipe)
  89. {
  90. if (pipe->parent.rx_indicate)
  91. pipe->parent.rx_indicate(&pipe->parent,
  92. rt_ringbuffer_data_len(&pipe->ringbuffer));
  93. if (!rt_list_isempty(&pipe->suspended_read_list))
  94. {
  95. rt_thread_t thread;
  96. RT_ASSERT(pipe->flag & RT_PIPE_FLAG_BLOCK_RD);
  97. /* get suspended thread */
  98. thread = rt_list_entry(pipe->suspended_read_list.next,
  99. struct rt_thread,
  100. tlist);
  101. /* resume the read thread */
  102. rt_thread_resume(thread);
  103. rt_schedule();
  104. }
  105. }
  106. static rt_size_t rt_pipe_write(rt_device_t dev,
  107. rt_off_t pos,
  108. const void *buffer,
  109. rt_size_t size)
  110. {
  111. rt_uint32_t level;
  112. rt_thread_t thread;
  113. struct rt_audio_pipe *pipe;
  114. rt_size_t write_nbytes;
  115. pipe = (struct rt_audio_pipe *)dev;
  116. RT_ASSERT(pipe != RT_NULL);
  117. if ((pipe->flag & RT_PIPE_FLAG_FORCE_WR) ||
  118. !(pipe->flag & RT_PIPE_FLAG_BLOCK_WR))
  119. {
  120. level = rt_hw_interrupt_disable();
  121. if (pipe->flag & RT_PIPE_FLAG_FORCE_WR)
  122. write_nbytes = rt_ringbuffer_put_force(&(pipe->ringbuffer),
  123. buffer, size);
  124. else
  125. write_nbytes = rt_ringbuffer_put(&(pipe->ringbuffer),
  126. buffer, size);
  127. _rt_pipe_resume_reader(pipe);
  128. rt_hw_interrupt_enable(level);
  129. return write_nbytes;
  130. }
  131. thread = rt_thread_self();
  132. /* current context checking */
  133. RT_DEBUG_NOT_IN_INTERRUPT;
  134. do {
  135. level = rt_hw_interrupt_disable();
  136. write_nbytes = rt_ringbuffer_put(&(pipe->ringbuffer), buffer, size);
  137. if (write_nbytes == 0)
  138. {
  139. /* pipe full, waiting on suspended write list */
  140. rt_thread_suspend(thread);
  141. /* waiting on suspended read list */
  142. rt_list_insert_before(&(pipe->suspended_write_list),
  143. &(thread->tlist));
  144. rt_hw_interrupt_enable(level);
  145. rt_schedule();
  146. }
  147. else
  148. {
  149. _rt_pipe_resume_reader(pipe);
  150. rt_hw_interrupt_enable(level);
  151. break;
  152. }
  153. } while (write_nbytes == 0);
  154. return write_nbytes;
  155. }
  156. static rt_err_t rt_pipe_control(rt_device_t dev, int cmd, void *args)
  157. {
  158. struct rt_audio_pipe *pipe;
  159. pipe = (struct rt_audio_pipe *)dev;
  160. if (cmd == PIPE_CTRL_GET_SPACE && args)
  161. *(rt_size_t*)args = rt_ringbuffer_space_len(&pipe->ringbuffer);
  162. return RT_EOK;
  163. }
  164. #ifdef RT_USING_DEVICE_OPS
  165. const static struct rt_device_ops audio_pipe_ops
  166. {
  167. RT_NULL,
  168. RT_NULL,
  169. RT_NULL,
  170. rt_pipe_read,
  171. rt_pipe_write,
  172. rt_pipe_control
  173. };
  174. #endif
  175. /**
  176. * This function will initialize a pipe device and put it under control of
  177. * resource management.
  178. *
  179. * @param pipe the pipe device
  180. * @param name the name of pipe device
  181. * @param flag the attribute of the pipe device
  182. * @param buf the buffer of pipe device
  183. * @param size the size of pipe device buffer
  184. *
  185. * @return the operation status, RT_EOK on successful
  186. */
  187. rt_err_t rt_audio_pipe_init(struct rt_audio_pipe *pipe,
  188. const char *name,
  189. enum rt_audio_pipe_flag flag,
  190. rt_uint8_t *buf,
  191. rt_size_t size)
  192. {
  193. RT_ASSERT(pipe);
  194. RT_ASSERT(buf);
  195. /* initialize suspended list */
  196. rt_list_init(&pipe->suspended_read_list);
  197. rt_list_init(&pipe->suspended_write_list);
  198. /* initialize ring buffer */
  199. rt_ringbuffer_init(&pipe->ringbuffer, buf, size);
  200. pipe->flag = flag;
  201. /* create pipe */
  202. pipe->parent.type = RT_Device_Class_Pipe;
  203. #ifdef RT_USING_DEVICE_OPS
  204. pipe->parent.ops = &audio_pipe_ops;
  205. #else
  206. pipe->parent.init = RT_NULL;
  207. pipe->parent.open = RT_NULL;
  208. pipe->parent.close = RT_NULL;
  209. pipe->parent.read = rt_pipe_read;
  210. pipe->parent.write = rt_pipe_write;
  211. pipe->parent.control = rt_pipe_control;
  212. #endif
  213. return rt_device_register(&(pipe->parent), name, RT_DEVICE_FLAG_RDWR);
  214. }
  215. /**
  216. * This function will detach a pipe device from resource management
  217. *
  218. * @param pipe the pipe device
  219. *
  220. * @return the operation status, RT_EOK on successful
  221. */
  222. rt_err_t rt_audio_pipe_detach(struct rt_audio_pipe *pipe)
  223. {
  224. return rt_device_unregister(&pipe->parent);
  225. }
  226. #ifdef RT_USING_HEAP
  227. rt_err_t rt_audio_pipe_create(const char *name, enum rt_audio_pipe_flag flag, rt_size_t size)
  228. {
  229. rt_uint8_t *rb_memptr = RT_NULL;
  230. struct rt_audio_pipe *pipe = RT_NULL;
  231. /* get aligned size */
  232. size = RT_ALIGN(size, RT_ALIGN_SIZE);
  233. pipe = (struct rt_audio_pipe *)rt_calloc(1, sizeof(struct rt_audio_pipe));
  234. if (pipe == RT_NULL)
  235. return -RT_ENOMEM;
  236. /* create ring buffer of pipe */
  237. rb_memptr = rt_malloc(size);
  238. if (rb_memptr == RT_NULL)
  239. {
  240. rt_free(pipe);
  241. return -RT_ENOMEM;
  242. }
  243. return rt_audio_pipe_init(pipe, name, flag, rb_memptr, size);
  244. }
  245. void rt_audio_pipe_destroy(struct rt_audio_pipe *pipe)
  246. {
  247. if (pipe == RT_NULL)
  248. return;
  249. /* un-register pipe device */
  250. rt_audio_pipe_detach(pipe);
  251. /* release memory */
  252. rt_free(pipe->ringbuffer.buffer_ptr);
  253. rt_free(pipe);
  254. return;
  255. }
  256. #endif /* RT_USING_HEAP */