audio.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843
  1. /*
  2. * audio.c
  3. *
  4. * Created on: 2016Äê10ÔÂ19ÈÕ
  5. * Author: Urey
  6. */
  7. #include <stdio.h>
  8. #include <rthw.h>
  9. #include <rtthread.h>
  10. #include <rtdevice.h>
  11. #include <drivers/audio.h>
  12. #define AUDIO_DEBUG 0
  13. #if AUDIO_DEBUG
  14. #define AUDIO_DBG(...) printf("[AUDIO]:"),printf(__VA_ARGS__)
  15. #else
  16. #define AUDIO_DBG(...)
  17. #endif
  18. rt_err_t _audio_queue_init(struct rt_audio_queue *queue, rt_uint16_t size, rt_uint16_t lwm)
  19. {
  20. RT_ASSERT(queue != RT_NULL);
  21. queue->count = 0;
  22. queue->size = size;
  23. queue->lwm = lwm;
  24. queue->waiting_lwm = RT_FALSE;
  25. queue->get_index = 0;
  26. queue->put_index = 0;
  27. rt_list_init(&(queue->suspended_push_list));
  28. rt_list_init(&(queue->suspended_pop_list));
  29. queue->queue = (struct rt_audio_frame *)rt_malloc(sizeof(struct rt_audio_frame) * size);
  30. if (queue->queue == RT_NULL)
  31. {
  32. return -RT_ENOMEM;
  33. }
  34. return RT_EOK;
  35. }
  36. rt_err_t _audio_queue_push(struct rt_audio_queue *queue, struct rt_audio_frame *frame, rt_int32_t timeout)
  37. {
  38. rt_ubase_t level;
  39. rt_thread_t thread;
  40. rt_err_t result;
  41. RT_ASSERT(queue != RT_NULL);
  42. result = RT_EOK;
  43. thread = rt_thread_self();
  44. AUDIO_DBG("%s count = %d\n",__func__,queue->count);
  45. level = rt_hw_interrupt_disable();
  46. while(queue->count == queue->size)
  47. {// audio queue is full
  48. queue->waiting_lwm = RT_TRUE;
  49. /* queue is full */
  50. if (timeout == 0)
  51. {
  52. result = -RT_ETIMEOUT;
  53. goto __exit;
  54. }
  55. /* current context checking */
  56. RT_DEBUG_NOT_IN_INTERRUPT;
  57. /* reset thread error number */
  58. thread->error = RT_EOK;
  59. /* suspend thread on the push list */
  60. rt_thread_suspend(thread);
  61. rt_list_insert_before(&(queue->suspended_push_list), &(thread->tlist));
  62. /* start timer */
  63. if (timeout > 0)
  64. {
  65. /* reset the timeout of thread timer and start it */
  66. rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &timeout);
  67. rt_timer_start(&(thread->thread_timer));
  68. }
  69. /* enable interrupt */
  70. rt_hw_interrupt_enable(level);
  71. /* do schedule */
  72. rt_schedule();
  73. /* thread is waked up */
  74. result = thread->error;
  75. level = rt_hw_interrupt_disable();
  76. if (result != RT_EOK) goto __exit;
  77. }
  78. queue->queue[queue->put_index].data_ptr = frame->data_ptr;
  79. queue->queue[queue->put_index].data_size = frame->data_size;
  80. queue->queue[queue->put_index].data_ofs = frame->data_ofs;
  81. queue->put_index = (queue->put_index + 1) % queue->size;
  82. queue->count ++;
  83. if (!rt_list_isempty(&(queue->suspended_pop_list)))
  84. {
  85. /* there is at least one thread in suspended list */
  86. /* get thread entry */
  87. thread = rt_list_entry(queue->suspended_pop_list.next,
  88. struct rt_thread,
  89. tlist);
  90. /* resume it */
  91. rt_thread_resume(thread);
  92. rt_hw_interrupt_enable(level);
  93. /* perform a schedule */
  94. rt_schedule();
  95. return result;
  96. }
  97. __exit:
  98. rt_hw_interrupt_enable(level);
  99. return result;
  100. }
  101. rt_err_t _audio_queue_pop(struct rt_audio_queue *queue, struct rt_audio_frame *frame, rt_int32_t timeout)
  102. {
  103. rt_ubase_t level;
  104. rt_thread_t thread;
  105. rt_err_t result;
  106. RT_ASSERT(queue != RT_NULL);
  107. RT_ASSERT(frame != RT_NULL);
  108. result = RT_EOK;
  109. thread = rt_thread_self();
  110. AUDIO_DBG("%s count = %d\n",__func__,queue->count);
  111. level = rt_hw_interrupt_disable();
  112. while (queue->count == 0)
  113. {
  114. /* queue is empty */
  115. if (timeout == 0)
  116. {
  117. result = -RT_ETIMEOUT;
  118. goto __exit;
  119. }
  120. /* current context checking */
  121. RT_DEBUG_NOT_IN_INTERRUPT;
  122. /* reset thread error number */
  123. thread->error = RT_EOK;
  124. /* suspend thread on the pop list */
  125. rt_thread_suspend(thread);
  126. rt_list_insert_before(&(queue->suspended_pop_list), &(thread->tlist));
  127. /* start timer */
  128. if (timeout > 0)
  129. {
  130. /* reset the timeout of thread timer and start it */
  131. rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &timeout);
  132. rt_timer_start(&(thread->thread_timer));
  133. }
  134. /* enable interrupt */
  135. rt_hw_interrupt_enable(level);
  136. /* do schedule */
  137. rt_schedule();
  138. /* thread is waked up */
  139. result = thread->error;
  140. level = rt_hw_interrupt_disable();
  141. if (result != RT_EOK)
  142. goto __exit;
  143. }
  144. frame->data_ptr = queue->queue[queue->get_index].data_ptr;
  145. frame->data_size = queue->queue[queue->get_index].data_size;
  146. frame->data_ofs = queue->queue[queue->get_index].data_ofs;
  147. queue->get_index = (queue->get_index + 1) % queue->size;
  148. queue->count --;
  149. if ((queue->waiting_lwm == RT_TRUE) &&
  150. (queue->put_index - queue->get_index) <= queue->lwm)
  151. {
  152. queue->waiting_lwm = RT_FALSE;
  153. /*
  154. * there is at least one thread in suspended list
  155. * and less than low water mark
  156. */
  157. if (!rt_list_isempty(&(queue->suspended_push_list)))
  158. {
  159. /* get thread entry */
  160. thread = rt_list_entry(queue->suspended_push_list.next,
  161. struct rt_thread,
  162. tlist);
  163. /* resume it */
  164. rt_thread_resume(thread);
  165. rt_hw_interrupt_enable(level);
  166. /* perform a schedule */
  167. rt_schedule();
  168. }
  169. return result;
  170. }
  171. __exit:
  172. rt_hw_interrupt_enable(level);
  173. return result;
  174. }
  175. rt_err_t _audio_queue_peak(struct rt_audio_queue *queue, struct rt_audio_frame *frame)
  176. {
  177. rt_ubase_t level;
  178. RT_ASSERT(queue != RT_NULL);
  179. AUDIO_DBG("%s count = %d\n",__func__,queue->count);
  180. level = rt_hw_interrupt_disable();
  181. if (queue->count == 0)
  182. {
  183. rt_hw_interrupt_enable(level);
  184. return -RT_EEMPTY;
  185. }
  186. frame->data_ptr = queue->queue[queue->get_index].data_ptr;
  187. frame->data_size = queue->queue[queue->get_index].data_size;
  188. frame->data_ofs = queue->queue[queue->get_index].data_ofs;
  189. rt_hw_interrupt_enable(level);
  190. return RT_EOK;
  191. }
  192. rt_err_t _audio_queue_unpeak(struct rt_audio_queue *queue, struct rt_audio_frame *frame)
  193. {
  194. rt_ubase_t level;
  195. RT_ASSERT(queue != RT_NULL);
  196. level = rt_hw_interrupt_disable();
  197. if (queue->count == 0)
  198. {
  199. rt_hw_interrupt_enable(level);
  200. return -RT_EEMPTY;
  201. }
  202. queue->queue[queue->get_index].data_ptr = frame->data_ptr;
  203. queue->queue[queue->get_index].data_size = frame->data_size;
  204. queue->queue[queue->get_index].data_ofs = frame->data_ofs;
  205. rt_hw_interrupt_enable(level);
  206. return RT_EOK;
  207. }
  208. rt_err_t _audio_queue_reset(struct rt_audio_queue *queue)
  209. {
  210. struct rt_thread *thread;
  211. register rt_ubase_t temp;
  212. rt_enter_critical();
  213. /* wakeup all suspend threads */
  214. /* resume on pop list */
  215. while (!rt_list_isempty(&(queue->suspended_pop_list)))
  216. {
  217. /* disable interrupt */
  218. temp = rt_hw_interrupt_disable();
  219. /* get next suspend thread */
  220. thread = rt_list_entry(queue->suspended_pop_list.next,
  221. struct rt_thread,
  222. tlist);
  223. /* set error code to RT_ERROR */
  224. thread->error = -RT_ERROR;
  225. /*
  226. * resume thread
  227. * In rt_thread_resume function, it will remove current thread from
  228. * suspend list
  229. */
  230. rt_thread_resume(thread);
  231. /* enable interrupt */
  232. rt_hw_interrupt_enable(temp);
  233. }
  234. /* resume on push list */
  235. while (!rt_list_isempty(&(queue->suspended_push_list)))
  236. {
  237. /* disable interrupt */
  238. temp = rt_hw_interrupt_disable();
  239. /* get next suspend thread */
  240. thread = rt_list_entry(queue->suspended_push_list.next,
  241. struct rt_thread,
  242. tlist);
  243. /* set error code to RT_ERROR */
  244. thread->error = -RT_ERROR;
  245. /*
  246. * resume thread
  247. * In rt_thread_resume function, it will remove current thread from
  248. * suspend list
  249. */
  250. rt_thread_resume(thread);
  251. /* enable interrupt */
  252. rt_hw_interrupt_enable(temp);
  253. }
  254. rt_exit_critical();
  255. rt_schedule();
  256. }
  257. static rt_err_t _audio_send_replay_frame(struct rt_audio_device *audio)
  258. {
  259. rt_err_t result = RT_EOK;
  260. rt_base_t level;
  261. struct rt_audio_frame frame;
  262. RT_ASSERT(audio != RT_NULL);
  263. //check repaly queue is empty
  264. if(_audio_queue_peak(&audio->replay->queue,&frame) != RT_EOK)
  265. {
  266. AUDIO_DBG("TX queue is empty\n");
  267. result = -RT_EEMPTY;
  268. level = rt_hw_interrupt_disable();
  269. audio->replay->activated = RT_FALSE;
  270. rt_hw_interrupt_enable(level);
  271. goto _exit;
  272. }
  273. if(audio->ops->transmit != RT_NULL)
  274. {
  275. AUDIO_DBG("audio transmit...\n");
  276. if(audio->ops->transmit(audio,frame.data_ptr,RT_NULL, frame.data_size) != frame.data_size)
  277. {
  278. result = -RT_EBUSY;
  279. goto _exit;
  280. }
  281. }
  282. //pop the head frame...
  283. _audio_queue_pop(&audio->replay->queue,&frame,RT_WAITING_NO);
  284. _exit:
  285. return result;
  286. }
  287. static rt_err_t _audio_flush_replay_frame(struct rt_audio_device *audio)
  288. {
  289. struct rt_audio_frame frame;
  290. if(audio->replay == RT_NULL)
  291. return -RT_EIO;
  292. while(_audio_queue_peak(&audio->replay->queue,&frame) == RT_EOK)
  293. {
  294. //pop the head frame...
  295. _audio_queue_pop(&audio->replay->queue,&frame,RT_WAITING_NO);
  296. /* notify transmitted complete. */
  297. if(audio->parent.tx_complete != RT_NULL)
  298. audio->parent.tx_complete(&audio->parent,(void *)frame.data_ptr);
  299. }
  300. return RT_EOK;
  301. }
  302. static rt_err_t _audio_dev_init(struct rt_device *dev)
  303. {
  304. rt_err_t result = RT_EOK;
  305. struct rt_audio_device *audio;
  306. RT_ASSERT(dev != RT_NULL);
  307. audio = (struct rt_audio_device *)dev;
  308. /* initialize replay & record */
  309. audio->replay = RT_NULL;
  310. audio->record = RT_NULL;
  311. /* apply configuration */
  312. if (audio->ops->init)
  313. result = audio->ops->init(audio);
  314. return result;
  315. }
  316. static rt_err_t _audio_dev_open(struct rt_device *dev, rt_uint16_t oflag)
  317. {
  318. rt_err_t result = RT_EOK;
  319. rt_base_t level;
  320. struct rt_audio_device *audio;
  321. RT_ASSERT(dev != RT_NULL);
  322. audio = (struct rt_audio_device *)dev;
  323. /* check device flag with the open flag */
  324. if ((oflag & RT_DEVICE_OFLAG_RDONLY) && !(dev->flag & RT_DEVICE_FLAG_RDONLY))
  325. return -RT_EIO;
  326. if ((oflag & RT_DEVICE_OFLAG_WRONLY) && !(dev->flag & RT_DEVICE_FLAG_WRONLY))
  327. return -RT_EIO;
  328. /* get open flags */
  329. dev->open_flag = oflag & 0xff;
  330. /* initialize the Rx/Tx structure according to open flag */
  331. if (oflag & RT_DEVICE_OFLAG_WRONLY)
  332. {
  333. AUDIO_DBG("open audio device ,oflag = %x\n",oflag);
  334. if(audio->replay == RT_NULL)
  335. {
  336. struct rt_audio_replay *replay = (struct rt_audio_replay *)rt_malloc(sizeof(struct rt_audio_replay));
  337. if(replay == RT_NULL)
  338. {
  339. AUDIO_DBG("request memory for replay error\n");
  340. return -RT_ENOMEM;
  341. }
  342. //init queue for audio replay
  343. _audio_queue_init(&replay->queue,CFG_AUDIO_REPLAY_QUEUE_COUNT,CFG_AUDIO_REPLAY_QUEUE_COUNT / 2);
  344. replay->activated = RT_FALSE;
  345. audio->replay = replay;
  346. }
  347. dev->open_flag |= RT_DEVICE_OFLAG_WRONLY;
  348. }
  349. if(oflag & RT_DEVICE_OFLAG_RDONLY)
  350. {
  351. if(audio->record == RT_NULL)
  352. {
  353. struct rt_audio_record *record = (struct rt_audio_record *)rt_malloc(sizeof(struct rt_audio_record));
  354. if(record == RT_NULL)
  355. {
  356. AUDIO_DBG("request memory for record error\n");
  357. return -RT_ENOMEM;
  358. }
  359. //init pipe for record
  360. {
  361. rt_size_t size = CFG_AUDIO_RECORD_PIPE_SIZE;
  362. rt_uint8_t *buf = rt_malloc(CFG_AUDIO_RECORD_PIPE_SIZE);
  363. if(buf == RT_NULL)
  364. {
  365. rt_free(record);
  366. AUDIO_DBG("request pipe memory error\n");
  367. return -RT_ENOMEM;
  368. }
  369. rt_pipe_init(&record->pipe,"recpipe",RT_PIPE_FLAG_FORCE_WR | RT_PIPE_FLAG_BLOCK_RD,buf,CFG_AUDIO_RECORD_PIPE_SIZE);
  370. }
  371. record->activated = RT_FALSE;
  372. audio->record = record;
  373. }
  374. //open record pipe
  375. if(audio->record != RT_NULL)
  376. {
  377. rt_device_open(RT_DEVICE(&audio->record->pipe),RT_DEVICE_OFLAG_RDONLY);
  378. }
  379. dev->open_flag |= RT_DEVICE_OFLAG_RDONLY;
  380. }
  381. return RT_EOK;
  382. }
  383. static rt_err_t _audio_dev_close(struct rt_device *dev)
  384. {
  385. struct rt_audio_device *audio;
  386. RT_ASSERT(dev != RT_NULL);
  387. audio = (struct rt_audio_device *)dev;
  388. //shutdown the lower device
  389. if(audio->ops->shutdown != RT_NULL)
  390. audio->ops->shutdown(audio);
  391. if(dev->open_flag & RT_DEVICE_OFLAG_WRONLY)
  392. {
  393. struct rt_audio_frame frame;
  394. //stop replay stream
  395. audio->ops->stop(audio,AUDIO_STREAM_REPLAY);
  396. //flush all frame
  397. while(_audio_queue_peak(&audio->replay->queue,&frame) == RT_EOK)
  398. {
  399. _audio_queue_pop(&audio->replay->queue,&frame,RT_WAITING_NO);
  400. //indicate this frame complete(maybe upper device need free data)
  401. if(dev->tx_complete != RT_NULL)
  402. dev->tx_complete(dev,(void *)frame.data_ptr);
  403. }
  404. dev->open_flag &= ~RT_DEVICE_OFLAG_WRONLY;
  405. }
  406. if(dev->open_flag & RT_DEVICE_OFLAG_RDONLY)
  407. {
  408. //stop record stream
  409. audio->ops->stop(audio,AUDIO_STREAM_RECORD);
  410. //close record pipe
  411. if(audio->record != RT_NULL)
  412. rt_device_close(RT_DEVICE(&audio->record->pipe));
  413. dev->open_flag &= ~RT_DEVICE_OFLAG_RDONLY;
  414. }
  415. return RT_EOK;
  416. }
  417. static rt_size_t _audio_dev_read(struct rt_device *dev, rt_off_t pos, void *buffer, rt_size_t size)
  418. {
  419. struct rt_audio_device *audio;
  420. RT_ASSERT(dev != RT_NULL);
  421. audio = (struct rt_audio_device *)dev;
  422. if(!(dev->open_flag & RT_DEVICE_OFLAG_RDONLY) || (audio->record == RT_NULL))
  423. return 0;
  424. return rt_device_read(RT_DEVICE(&audio->record->pipe), pos, buffer, size);
  425. }
  426. static rt_size_t _audio_dev_write(struct rt_device *dev, rt_off_t pos, const void *buffer, rt_size_t size)
  427. {
  428. rt_err_t result = RT_EOK;
  429. rt_base_t level;
  430. struct rt_audio_device *audio;
  431. RT_ASSERT(dev != RT_NULL);
  432. audio = (struct rt_audio_device *)dev;
  433. if(!(dev->open_flag & RT_DEVICE_OFLAG_WRONLY) || (audio->replay == RT_NULL))
  434. return 0;
  435. AUDIO_DBG("audio write : pos = %d,buffer = %x,size = %d\n",pos,(rt_uint32_t)buffer,size);
  436. //push a new frame to tx queue
  437. {
  438. struct rt_audio_frame frame;
  439. frame.data_ptr = buffer;
  440. frame.data_size = size;
  441. frame.data_ofs = 0;
  442. result = _audio_queue_push(&audio->replay->queue,&frame,RT_WAITING_FOREVER);
  443. if(result != RT_EOK)
  444. {
  445. AUDIO_DBG("TX frame queue push error\n");
  446. rt_set_errno(-RT_EFULL);
  447. return 0;
  448. }
  449. }
  450. //check tx state...
  451. level = rt_hw_interrupt_disable();
  452. if(audio->replay->activated != RT_TRUE)
  453. {
  454. audio->replay->activated = RT_TRUE;
  455. rt_hw_interrupt_enable(level);
  456. _audio_send_replay_frame(audio);
  457. }
  458. return size;
  459. }
  460. static rt_err_t _audio_dev_control(struct rt_device *dev, rt_uint8_t cmd, void *args)
  461. {
  462. rt_err_t result = RT_EOK;
  463. struct rt_audio_device *audio;
  464. RT_ASSERT(dev != RT_NULL);
  465. audio = (struct rt_audio_device *)dev;
  466. //dev stat...
  467. switch (cmd)
  468. {
  469. case AUDIO_CTL_GETCAPS:
  470. {
  471. struct rt_audio_caps *caps = (struct rt_audio_caps *) args;
  472. AUDIO_DBG("AUDIO_CTL_GETCAPS: main_type = %d,sub_type = %d\n",caps->main_type,caps->sub_type);
  473. if (audio->ops->getcaps != RT_NULL)
  474. {
  475. result = audio->ops->getcaps(audio, caps);
  476. }
  477. }
  478. break;
  479. case AUDIO_CTL_CONFIGURE:
  480. {
  481. struct rt_audio_caps *caps = (struct rt_audio_caps *) args;
  482. AUDIO_DBG("AUDIO_CTL_CONFIGURE: main_type = %d,sub_type = %d\n",caps->main_type,caps->sub_type);
  483. if (audio->ops->configure != RT_NULL)
  484. {
  485. result = audio->ops->configure(audio, caps);
  486. }
  487. }
  488. break;
  489. case AUDIO_CTL_SHUTDOWN:
  490. {
  491. AUDIO_DBG("AUDIO_CTL_SHUTDOWN\n");
  492. if (audio->ops->shutdown != RT_NULL)
  493. result = audio->ops->shutdown(audio);
  494. //flush replay frame...
  495. _audio_flush_replay_frame(audio);
  496. }
  497. break;
  498. case AUDIO_CTL_START:
  499. {
  500. int stream = *(int *) args;
  501. AUDIO_DBG("AUDIO_CTL_START: stream = %d\n",stream);
  502. if (audio->ops->start != RT_NULL)
  503. result = audio->ops->start(audio, stream);
  504. }
  505. break;
  506. case AUDIO_CTL_STOP:
  507. {
  508. int stream = *(int *) args;
  509. AUDIO_DBG("AUDIO_CTL_STOP: stream = %d\n",stream);
  510. if (audio->ops->start != RT_NULL)
  511. result = audio->ops->stop(audio, stream);
  512. if(stream == AUDIO_STREAM_REPLAY)
  513. {
  514. _audio_flush_replay_frame(audio);
  515. }
  516. }
  517. break;
  518. case AUDIO_CTL_PAUSE:
  519. {
  520. int stream = *(int *) args;
  521. AUDIO_DBG("AUDIO_CTL_PAUSE: stream = %d\n",stream);
  522. if (audio->ops->start != RT_NULL)
  523. result = audio->ops->suspend(audio, stream);
  524. }
  525. break;
  526. case AUDIO_CTL_RESUME:
  527. {
  528. int stream = *(int *) args;
  529. AUDIO_DBG("AUDIO_CTL_RESUME: stream = %d\n",stream);
  530. if (audio->ops->start != RT_NULL)
  531. result = audio->ops->resume(audio, stream);
  532. //resume tx frame...
  533. if(stream == AUDIO_STREAM_REPLAY)
  534. _audio_send_replay_frame(audio);
  535. }
  536. break;
  537. #ifdef AUDIO_DEVICE_USE_PRIVATE_BUFFER
  538. case AUDIO_CTL_ALLOCBUFFER:
  539. {
  540. struct rt_audio_buf_desc *desc = (struct rt_audio_buf_desc *)args;
  541. if((audio->ops->buffer_alloc != RT_NULL) && (desc != RT_NULL))
  542. {
  543. result = audio->ops->buffer_alloc(audio,&desc->data_ptr,&desc->data_size);
  544. break;
  545. }
  546. result = -RT_EIO;
  547. }
  548. break;
  549. case AUDIO_CTL_FREEBUFFER:
  550. {
  551. rt_uint8_t *data_ptr = (rt_uint8_t *)args;
  552. if((audio->ops->buffer_free != RT_NULL) && (data_ptr != RT_NULL))
  553. {
  554. audio->ops->buffer_free(audio,data_ptr);
  555. break;
  556. }
  557. }
  558. break;
  559. #endif
  560. default:
  561. result = audio->ops->control(audio, cmd, args);
  562. break;
  563. }
  564. return result;
  565. }
  566. rt_err_t rt_audio_register(struct rt_audio_device *audio, const char *name, rt_uint32_t flag, void *data)
  567. {
  568. struct rt_device *device;
  569. RT_ASSERT(audio != RT_NULL);
  570. device = &(audio->parent);
  571. device->type = RT_Device_Class_Sound;
  572. device->rx_indicate = RT_NULL;
  573. device->tx_complete = RT_NULL;
  574. device->init = _audio_dev_init;
  575. device->open = _audio_dev_open;
  576. device->close = _audio_dev_close;
  577. device->read = _audio_dev_read;
  578. device->write = _audio_dev_write;
  579. device->control = _audio_dev_control;
  580. device->user_data = data;
  581. /* register a character device */
  582. return rt_device_register(device, name, flag | RT_DEVICE_FLAG_REMOVABLE);
  583. }
  584. rt_size_t rt_audio_get_buffer_size(struct rt_audio_device *audio)
  585. {
  586. // return (audio->config.period_count * audio->config.period_size);
  587. return 0;
  588. }
  589. int rt_audio_samplerate_to_speed(rt_uint32_t bitValue)
  590. {
  591. int speed = 0;
  592. switch (bitValue)
  593. {
  594. case AUDIO_SAMP_RATE_8K:
  595. speed = 8000;
  596. break;
  597. case AUDIO_SAMP_RATE_11K:
  598. speed = 11052;
  599. break;
  600. case AUDIO_SAMP_RATE_16K:
  601. speed = 16000;
  602. break;
  603. case AUDIO_SAMP_RATE_22K:
  604. speed = 22050;
  605. break;
  606. case AUDIO_SAMP_RATE_32K:
  607. speed = 32000;
  608. break;
  609. case AUDIO_SAMP_RATE_44K:
  610. speed = 44100;
  611. break;
  612. case AUDIO_SAMP_RATE_48K:
  613. speed = 48000;
  614. break;
  615. case AUDIO_SAMP_RATE_96K:
  616. speed = 96000;
  617. break;
  618. case AUDIO_SAMP_RATE_128K:
  619. speed = 128000;
  620. break;
  621. case AUDIO_SAMP_RATE_160K:
  622. speed = 160000;
  623. break;
  624. case AUDIO_SAMP_RATE_172K:
  625. speed = 176400;
  626. break;
  627. case AUDIO_SAMP_RATE_192K:
  628. speed = 192000;
  629. break;
  630. default:
  631. break;
  632. }
  633. return speed;
  634. }
  635. rt_uint32_t rt_audio_format_to_bits(rt_uint32_t format)
  636. {
  637. switch (format)
  638. {
  639. case AUDIO_FMT_PCM_U8:
  640. case AUDIO_FMT_PCM_S8:
  641. return 8;
  642. case AUDIO_FMT_PCM_S16_LE:
  643. case AUDIO_FMT_PCM_S16_BE:
  644. case AUDIO_FMT_PCM_U16_LE:
  645. case AUDIO_FMT_PCM_U16_BE:
  646. return 16;
  647. default:
  648. return 32;
  649. };
  650. }
  651. void rt_audio_tx_complete(struct rt_audio_device *audio,rt_uint8_t *pbuf)
  652. {
  653. rt_err_t result;
  654. AUDIO_DBG("audio tx complete ptr=%x...\n",(rt_uint32_t)pbuf);
  655. //try to send all frame
  656. do
  657. {
  658. result = _audio_send_replay_frame(audio);
  659. }while(result == RT_EOK);
  660. /* notify transmitted complete. */
  661. if(audio->parent.tx_complete != RT_NULL)
  662. audio->parent.tx_complete(&audio->parent,(void *)pbuf);
  663. }
  664. void rt_audio_rx_done(struct rt_audio_device *audio,rt_uint8_t *pbuf,rt_size_t len)
  665. {
  666. rt_err_t result = RT_EOK;
  667. //save data to record pipe
  668. rt_device_write(RT_DEVICE(RT_DEVICE(&audio->record->pipe)),0,pbuf,len);
  669. /* invoke callback */
  670. if(audio->parent.rx_indicate != RT_NULL)
  671. audio->parent.rx_indicate(&audio->parent,len);
  672. }