drv_audio.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * File : drv_audio.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2017, 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. * 2018-05-26 RT-Thread the first version
  23. */
  24. #include <rtthread.h>
  25. #include <rthw.h>
  26. #include <rtdevice.h>
  27. #include <string.h>
  28. #include "drv_pl041.h"
  29. #include "drv_ac97.h"
  30. #include "drv_audio.h"
  31. #define DATA_NODE_MAX (10)
  32. #define CODEC_TX_FIFO_SIZE (256)
  33. #define AUDIO_DEVICE_DECODE_MP_SIZE (4096)
  34. #define AUDIO_DEVICE_DECODE_MP_CONUT (4)
  35. struct codec_data_node
  36. {
  37. char *data_ptr;
  38. rt_size_t data_size;
  39. };
  40. struct audio_buff_des
  41. {
  42. struct codec_data_node *data_list;
  43. void (*free_fun)(void *);
  44. rt_uint32_t read_offset;
  45. rt_uint16_t node_num;
  46. rt_uint16_t read_index, put_index;
  47. };
  48. struct audio_device
  49. {
  50. /* inherit from rt_device */
  51. struct rt_device parent;
  52. };
  53. static struct audio_device audio_device_drive;
  54. static struct audio_buff_des *audio_buff;
  55. static int irq_flag = 0;
  56. static void _audio_buff_cb(void *buff)
  57. {
  58. if (audio_device_drive.parent.tx_complete != RT_NULL)
  59. {
  60. audio_device_drive.parent.tx_complete(&audio_device_drive.parent, buff);
  61. }
  62. }
  63. static rt_size_t _audio_buff_push(struct audio_buff_des *hdle, void *buff, int size)
  64. {
  65. struct codec_data_node *node;
  66. rt_uint16_t next_index;
  67. rt_uint32_t level;
  68. if ((buff == RT_NULL) || (size == 0))
  69. {
  70. return 0;
  71. }
  72. next_index = hdle->put_index + 1;
  73. if (next_index >= hdle->node_num)
  74. next_index = 0;
  75. /* check data_list full */
  76. if (next_index == hdle->read_index)
  77. {
  78. rt_kprintf("data_list full\n");
  79. rt_set_errno(-RT_EFULL);
  80. return 0;
  81. }
  82. level = rt_hw_interrupt_disable();
  83. node = &hdle->data_list[hdle->put_index];
  84. hdle->put_index = next_index;
  85. /* set node attribute */
  86. node->data_ptr = (char *) buff;
  87. node->data_size = size;
  88. rt_hw_interrupt_enable(level);
  89. return size;
  90. }
  91. static rt_size_t _audio_buff_pop(struct audio_buff_des *hdle, void *buff, int size)
  92. {
  93. struct codec_data_node *node;
  94. rt_uint32_t next_index, count = 0, cp_size = 0, offset = 0;
  95. node = &hdle->data_list[hdle->read_index];
  96. if ((hdle->read_index == hdle->put_index) && (node->data_ptr == RT_NULL))
  97. {
  98. memset(buff, 0xff, size);
  99. return 0;
  100. }
  101. while (count < size)
  102. {
  103. node = &hdle->data_list[hdle->read_index];
  104. offset = hdle->read_offset;
  105. cp_size = (node->data_size - offset) > (size - count) ? (size - count) : (node->data_size - offset);
  106. if (node->data_ptr == RT_NULL)
  107. {
  108. memset(buff, 0, size - count);
  109. return count;
  110. }
  111. memcpy((rt_uint8_t *)buff + count, (rt_uint8_t *)(node->data_ptr) + offset, cp_size);
  112. hdle->read_offset += cp_size;
  113. count += cp_size;
  114. if (hdle->read_offset >= node->data_size)
  115. {
  116. /* notify transmitted complete. */
  117. if (hdle->free_fun != RT_NULL)
  118. {
  119. hdle->free_fun(node->data_ptr);
  120. }
  121. /* clear current node */
  122. memset(node, 0, sizeof(struct codec_data_node));
  123. next_index = hdle->read_index + 1;
  124. if (next_index >= hdle->node_num)
  125. {
  126. next_index = 0;
  127. }
  128. hdle->read_offset = 0;
  129. hdle->read_index = next_index;
  130. }
  131. }
  132. return count;
  133. }
  134. static void transit_wav_data(rt_uint32_t status)
  135. {
  136. rt_uint16_t sample[CODEC_TX_FIFO_SIZE];
  137. int i = 0, size;
  138. size = _audio_buff_pop(audio_buff, sample, CODEC_TX_FIFO_SIZE * sizeof(rt_uint16_t));
  139. if ((size == 0) && (irq_flag == 1))
  140. {
  141. aaci_pl041_irq_disable(0, AACI_IE_UR | AACI_IE_TX | AACI_IE_TXC);
  142. irq_flag = 0;
  143. }
  144. for (i = 0; i < (size >> 1); i++)
  145. {
  146. aaci_pl041_channle_write(0, &sample[i], 1);
  147. }
  148. }
  149. static void rt_hw_aaci_isr(rt_uint32_t status, void *user_data)
  150. {
  151. if (status & AACI_SR_TXHE)
  152. {
  153. transit_wav_data(status);
  154. }
  155. }
  156. static rt_err_t codec_init(rt_device_t dev)
  157. {
  158. struct pl041_cfg _cfg;
  159. _cfg.itype = PL041_CHANNLE_LEFT_ADC | PL041_CHANNLE_RIGHT_ADC;
  160. _cfg.otype = PL041_CHANNLE_LEFT_DAC | PL041_CHANNLE_RIGHT_DAC;
  161. _cfg.vol = 50;
  162. _cfg.rate = 8000;
  163. ac97_reset();
  164. aaci_pl041_channle_cfg(0, &_cfg);
  165. aaci_pl041_irq_register(0, rt_hw_aaci_isr, RT_NULL);
  166. return RT_EOK;
  167. }
  168. static rt_err_t codec_open(rt_device_t dev, rt_uint16_t oflag)
  169. {
  170. return RT_EOK;
  171. }
  172. static rt_err_t codec_close(rt_device_t dev)
  173. {
  174. rt_uint16_t temp = 0, i = 1024 * 10;
  175. while (PL041->sr1 & AACI_SR_TXB);
  176. while (i)
  177. {
  178. if (aaci_pl041_channle_write(0, &temp, 1) != 0)
  179. {
  180. i--;
  181. }
  182. }
  183. return RT_EOK;
  184. }
  185. static rt_size_t codec_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
  186. {
  187. return 0;
  188. }
  189. static rt_size_t codec_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
  190. {
  191. _audio_buff_push(audio_buff, (void *)buffer, size);
  192. if (irq_flag == 0)
  193. {
  194. //open irq
  195. irq_flag = 1;
  196. aaci_pl041_channle_enable(0);
  197. aaci_pl041_irq_enable(0, AACI_IE_UR | AACI_IE_TX | AACI_IE_TXC);
  198. }
  199. return 0;
  200. }
  201. static rt_err_t codec_control(rt_device_t dev, int cmd, void *args)
  202. {
  203. rt_err_t result = RT_EOK;
  204. switch (cmd)
  205. {
  206. case CODEC_CMD_RESET:
  207. {
  208. break;
  209. }
  210. case CODEC_CMD_SET_VOLUME:
  211. {
  212. uint32_t v;
  213. v = *(rt_uint32_t *)args;
  214. result = ac97_set_vol(v);
  215. break;
  216. }
  217. case CODEC_CMD_GET_VOLUME:
  218. {
  219. int *v = args;
  220. *v = ac97_get_vol();
  221. break;
  222. }
  223. case CODEC_CMD_SAMPLERATE:
  224. {
  225. int v;
  226. v = *(rt_uint32_t *)args;
  227. ac97_set_rate(v);
  228. break;
  229. }
  230. default:
  231. result = RT_ERROR;
  232. }
  233. return result;
  234. }
  235. int audio_hw_init(void)
  236. {
  237. struct audio_device *codec = &audio_device_drive;
  238. codec->parent.type = RT_Device_Class_Sound;
  239. codec->parent.rx_indicate = RT_NULL;
  240. codec->parent.tx_complete = RT_NULL;
  241. codec->parent.init = codec_init;
  242. codec->parent.open = codec_open;
  243. codec->parent.close = codec_close;
  244. codec->parent.read = codec_read;
  245. codec->parent.write = codec_write;
  246. codec->parent.control = codec_control;
  247. codec->parent.user_data = RT_NULL;
  248. audio_buff = rt_malloc(sizeof(struct audio_buff_des) + sizeof(struct codec_data_node) * DATA_NODE_MAX);
  249. if (audio_buff == RT_NULL)
  250. {
  251. rt_kprintf("audio buff malloc fail\n");
  252. return -1;
  253. }
  254. rt_memset(audio_buff, 0, sizeof(struct audio_buff_des) + sizeof(struct codec_data_node) * DATA_NODE_MAX);
  255. audio_buff->data_list = (struct codec_data_node *)((rt_uint8_t *)audio_buff + sizeof(struct audio_buff_des));
  256. audio_buff->free_fun = _audio_buff_cb;
  257. audio_buff->node_num = DATA_NODE_MAX;
  258. /* register the device */
  259. rt_device_register(&codec->parent, "sound", RT_DEVICE_FLAG_WRONLY | RT_DEVICE_FLAG_DMA_TX);
  260. aaci_pl041_init();
  261. rt_device_init(&codec->parent);
  262. return 0;
  263. }
  264. INIT_DEVICE_EXPORT(audio_hw_init);