tc_audio_drv_mic.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 mic_device mic_dev;
  16. /* Simulate callback function */
  17. static void thread_simulate_intr(void *parameter)
  18. {
  19. /* Send the data(0xAA) from DMA buffer to kernel */
  20. rt_memset((void*)&mic_dev.rx_fifo[0], 0xAA, RX_DMA_BLOCK_SIZE);
  21. rt_audio_rx_done((struct rt_audio_device *)&(mic_dev.audio), mic_dev.rx_fifo, RX_DMA_BLOCK_SIZE);
  22. audio_fsm_step = 1;
  23. while (1)
  24. {
  25. if(audio_fsm_step == 2)
  26. {
  27. /* Send the the data(0x55) from DMA buffer to kernel */
  28. rt_memset((void*)&mic_dev.rx_fifo[RX_DMA_BLOCK_SIZE], 0x55, RX_DMA_BLOCK_SIZE);
  29. rt_audio_rx_done(&mic_dev.audio, &mic_dev.rx_fifo[RX_DMA_BLOCK_SIZE], RX_DMA_BLOCK_SIZE);
  30. audio_fsm_step = 3;
  31. break;
  32. }
  33. if(audio_fsm_step == 4)
  34. {
  35. rt_thread_mdelay(10);
  36. rt_audio_rx_done(&mic_dev.audio, &mic_dev.rx_fifo[RX_DMA_BLOCK_SIZE], RX_DMA_BLOCK_SIZE);
  37. break;
  38. }
  39. rt_thread_mdelay(10);
  40. }
  41. while(1)
  42. {
  43. rt_thread_mdelay(10);
  44. }
  45. }
  46. static void thread_simulate_intr_create(void)
  47. {
  48. thread_simulate_intr_handle = rt_thread_create(
  49. "thread_simulate_intr",
  50. thread_simulate_intr,
  51. RT_NULL,
  52. thread_simulate_intr_create_stacksize,
  53. THREAD_PRIORITY - 1, THREAD_TIMESLICE);
  54. if (thread_simulate_intr_handle == RT_NULL)
  55. {
  56. rt_kprintf("Error: Failed to create thread!\n");
  57. return;
  58. }
  59. if (rt_thread_startup(thread_simulate_intr_handle) != RT_EOK)
  60. {
  61. rt_kprintf("Error: Failed to start thread!\n");
  62. thread_simulate_intr_handle = RT_NULL;
  63. }
  64. }
  65. static rt_err_t mic_device_init(struct rt_audio_device *audio)
  66. {
  67. return RT_EOK;
  68. }
  69. /* Simulate DMA interrupt */
  70. static rt_err_t mic_device_start(struct rt_audio_device *audio, int stream)
  71. {
  72. thread_simulate_intr_create();
  73. return RT_EOK;
  74. }
  75. static rt_err_t mic_device_stop(struct rt_audio_device *audio, int stream)
  76. {
  77. if (thread_simulate_intr_handle != RT_NULL)
  78. {
  79. rt_thread_delete(thread_simulate_intr_handle);
  80. thread_simulate_intr_handle = RT_NULL;
  81. }
  82. return RT_EOK;
  83. }
  84. static rt_err_t mic_device_getcaps(struct rt_audio_device *audio, struct rt_audio_caps *caps)
  85. {
  86. return RT_EOK;
  87. }
  88. static rt_err_t mic_device_configure(struct rt_audio_device *audio, struct rt_audio_caps *caps)
  89. {
  90. return RT_EOK;
  91. }
  92. static struct rt_audio_ops _mic_audio_ops =
  93. {
  94. .getcaps = mic_device_getcaps,
  95. .configure = mic_device_configure,
  96. .init = mic_device_init,
  97. .start = mic_device_start,
  98. .stop = mic_device_stop,
  99. .transmit = RT_NULL,
  100. .buffer_info = RT_NULL,
  101. };
  102. static int rt_hw_mic_init(void)
  103. {
  104. struct rt_audio_device *audio = &mic_dev.audio;
  105. /* mic default */
  106. mic_dev.rx_fifo = rt_malloc(RX_DMA_FIFO_SIZE);
  107. if (mic_dev.rx_fifo == RT_NULL)
  108. {
  109. return -RT_ENOMEM;
  110. }
  111. mic_dev.config.channels = MIC_CHANNEL;
  112. mic_dev.config.samplerate = MIC_SAMPLERATE;
  113. mic_dev.config.samplebits = MIC_SAMPLEBITS;
  114. /* register mic device */
  115. audio->ops = &_mic_audio_ops;
  116. rt_audio_register(audio, SOUND_MIC_DEVICE_NAME, RT_DEVICE_FLAG_RDONLY, (void *)&mic_dev);
  117. return RT_EOK;
  118. }
  119. INIT_DEVICE_EXPORT(rt_hw_mic_init);