tc_audio_drv_player.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. * 2025-05-02 wumingzi First version
  9. */
  10. #include "tc_audio_common.h"
  11. #define THREAD_PRIORITY 9
  12. #define THREAD_TIMESLICE 5
  13. #define thread_simulate_intr_create_stacksize 1024
  14. static rt_thread_t thread_simulate_intr_handle;
  15. static struct sound_device snd_dev;
  16. static void thread_simulate_intr(void *parameter)
  17. {
  18. rt_flag_t exec_once = 0;
  19. while(1)
  20. {
  21. if(audio_fsm_step == 1 && exec_once == 0)
  22. {
  23. /* Move the data(0xAA) from kernel to DMA buffer */
  24. rt_audio_tx_complete(&snd_dev.audio);
  25. audio_fsm_step = 2;
  26. exec_once = 1;
  27. rt_thread_mdelay(10);
  28. }
  29. else if(audio_fsm_step == 2 && exec_once == 1)
  30. {
  31. /* Move the data(0x55) from kernel to DMA buffer */
  32. rt_audio_tx_complete(&snd_dev.audio);
  33. audio_fsm_step = 3;
  34. rt_thread_mdelay(10);
  35. }
  36. else if(audio_fsm_step == 4)
  37. {
  38. /* rt_device_close will call rt_completion_wait(FOREVER), so we need delay to
  39. * let system run the point */
  40. rt_thread_mdelay(10);
  41. rt_audio_tx_complete(&snd_dev.audio);
  42. break;
  43. }
  44. rt_thread_mdelay(10);
  45. }
  46. while (1)
  47. {
  48. rt_thread_mdelay(10);
  49. }
  50. }
  51. static void thread_simulate_intr_create(void)
  52. {
  53. thread_simulate_intr_handle = rt_thread_create(
  54. "thread_simulate_intr",
  55. thread_simulate_intr,
  56. RT_NULL,
  57. thread_simulate_intr_create_stacksize,
  58. THREAD_PRIORITY - 1, THREAD_TIMESLICE);
  59. rt_thread_startup(thread_simulate_intr_handle);
  60. }
  61. static rt_err_t player_device_init(struct rt_audio_device *audio)
  62. {
  63. return RT_EOK;
  64. }
  65. /* Simulate DMA interrupt */
  66. static rt_err_t player_device_start(struct rt_audio_device *audio, int stream)
  67. {
  68. thread_simulate_intr_create();
  69. return RT_EOK;
  70. }
  71. static rt_err_t player_device_stop(struct rt_audio_device *audio, int stream)
  72. {
  73. rt_thread_delete(thread_simulate_intr_handle);
  74. return RT_EOK;
  75. }
  76. static rt_err_t player_device_getcaps(struct rt_audio_device *audio, struct rt_audio_caps *caps)
  77. {
  78. return RT_EOK;
  79. }
  80. static rt_err_t player_device_configure(struct rt_audio_device *audio, struct rt_audio_caps *caps)
  81. {
  82. return RT_EOK;
  83. }
  84. static rt_ssize_t player_device_transmit(struct rt_audio_device *audio, const void *writeBuf, void *readBuf, rt_size_t size)
  85. {
  86. return size;
  87. }
  88. static void player_device_buffer_info(struct rt_audio_device *audio, struct rt_audio_buf_info *info)
  89. {
  90. RT_ASSERT(audio != RT_NULL);
  91. /**
  92. * TX_FIFO
  93. * +----------------+----------------+
  94. * | block1 | block2 |
  95. * +----------------+----------------+
  96. * \ block_size /
  97. */
  98. info->buffer = snd_dev.tx_fifo;
  99. info->total_size = TX_DMA_FIFO_SIZE;
  100. info->block_size = TX_DMA_BLOCK_SIZE;
  101. info->block_count = RT_AUDIO_REPLAY_MP_BLOCK_COUNT;
  102. }
  103. static struct rt_audio_ops audio_ops =
  104. {
  105. .getcaps = player_device_getcaps,
  106. .configure = player_device_configure,
  107. .init = player_device_init,
  108. .start = player_device_start,
  109. .stop = player_device_stop,
  110. .transmit = player_device_transmit,
  111. .buffer_info = player_device_buffer_info,
  112. };
  113. static int rt_hw_sound_init(void)
  114. {
  115. rt_uint8_t *tx_fifo = RT_NULL;
  116. tx_fifo = rt_malloc(TX_DMA_FIFO_SIZE);
  117. if (tx_fifo == NULL)
  118. {
  119. return -RT_ENOMEM;
  120. }
  121. snd_dev.tx_fifo = tx_fifo;
  122. /* Init default configuration */
  123. {
  124. snd_dev.config.samplerate = PLAYER_SAMPLERATE;
  125. snd_dev.config.channels = PLAYER_CHANNEL;
  126. snd_dev.config.samplebits = PLAYER_SAMPLEBITS;
  127. snd_dev.volume = PLAYER_VOLUME;
  128. }
  129. snd_dev.audio.ops = &audio_ops;
  130. rt_audio_register(&snd_dev.audio, SOUND_PLAYER_DEVICE_NAME, RT_DEVICE_FLAG_WRONLY, &snd_dev);
  131. return RT_EOK;
  132. }
  133. INIT_DEVICE_EXPORT(rt_hw_sound_init);