pdm.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * File :_pdm.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 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. * 2017-12-04 Haley the first version
  23. */
  24. #include <rtthread.h>
  25. #include <rtdevice.h>
  26. #include "am_mcu_apollo.h"
  27. #ifdef RT_USING_PDM
  28. /* messagequeue define */
  29. struct rt_messagequeue pdm_mq;
  30. static rt_uint8_t am_pdm_buffer_pool[1024];
  31. #define NWA_FRAME_SAMPLES 160 /* 8k, 16bit, mono audio data */
  32. #define PDM_FIFO_THRESHOLD NWA_FRAME_SAMPLES
  33. #define PDM_GPIO_CLK 22
  34. #define PDM_GPIO_CFG_CLK AM_HAL_PIN_22_PDM_CLK
  35. #define PDM_GPIO_DATA 23
  36. #define PDM_GPIO_CFG_DATA AM_HAL_PIN_23_PDM_DATA
  37. static am_hal_pdm_config_t g_sPDMConfig =
  38. {
  39. AM_HAL_PDM_PCFG_LRSWAP_DISABLE | AM_HAL_PDM_PCFG_RIGHT_PGA_0DB | AM_HAL_PDM_PCFG_LEFT_PGA_0DB
  40. | AM_HAL_PDM_PCFG_MCLKDIV_DIV1 | AM_HAL_PDM_PCFG_SINC_RATE(48) | AM_HAL_PDM_PCFG_ADCHPD_ENABLE
  41. | AM_HAL_PDM_PCFG_HPCUTOFF(0x1) | AM_HAL_PDM_PCFG_CYCLES(0x1) | AM_HAL_PDM_PCFG_SOFTMUTE_DISABLE
  42. | AM_HAL_PDM_PCFG_PDMCORE_ENABLE, /* Set the PDM configuration */
  43. AM_HAL_PDM_IOCLK_750KHZ | AM_HAL_PDM_VCFG_RSTB_NORMAL | AM_HAL_PDM_VCFG_PDMCLK_ENABLE
  44. | AM_HAL_PDM_VCFG_I2SMODE_DISABLE | AM_HAL_PDM_VCFG_BCLKINV_DISABLE | AM_HAL_PDM_VCFG_DMICDEL_DISABLE
  45. | AM_HAL_PDM_VCFG_SELAP_INTERNAL | AM_HAL_PDM_VCFG_PACK_DISABLE | AM_HAL_PDM_VCFG_CHANNEL_LEFT, /* Set the Voice Configuration */
  46. PDM_FIFO_THRESHOLD, /* Select the FIFO PCM sample threshold 0~256 */
  47. };
  48. /**
  49. * @brief Get the pdm data.
  50. *
  51. * @param None.
  52. *
  53. * This function Get the pdm data.
  54. *
  55. * @return None.
  56. */
  57. rt_uint8_t am_pdm_data_get(rt_uint8_t *buff, rt_uint16_t size)
  58. {
  59. rt_uint8_t pdm_rbufftemp[340];
  60. /* wait pdm message forever */
  61. rt_mq_recv(&pdm_mq, pdm_rbufftemp, 340, RT_WAITING_FOREVER);
  62. /* copy the data */
  63. rt_memcpy(buff, (char *)pdm_rbufftemp, size);
  64. return 0;
  65. }
  66. /**
  67. * @brief Start the pdm.
  68. *
  69. * @param None.
  70. *
  71. * This function Start the pdm.
  72. *
  73. * @return None.
  74. */
  75. void am_pdm_start(void)
  76. {
  77. /* Enable PDM */
  78. am_hal_interrupt_enable(AM_HAL_INTERRUPT_PDM);
  79. am_hal_pdm_enable();
  80. }
  81. /**
  82. * @brief Stop the pdm.
  83. *
  84. * @param None.
  85. *
  86. * This function Stop the pdm.
  87. *
  88. * @return None.
  89. */
  90. void am_pdm_stop(void)
  91. {
  92. /* Disable PDM */
  93. am_hal_interrupt_disable(AM_HAL_INTERRUPT_PDM);
  94. am_hal_pdm_disable();
  95. }
  96. /**
  97. * @brief Get the pdm left gain.
  98. *
  99. * @param None.
  100. *
  101. * This function Get the pdm left gain.
  102. *
  103. * @return gain_val.
  104. */
  105. uint8_t am_pdm_left_gain_get(void)
  106. {
  107. /* get the left gain */
  108. return am_hal_pdm_left_gain_get();
  109. }
  110. /**
  111. * @brief Set the pdm left gain.
  112. *
  113. * @param gain_val.
  114. *
  115. * This function Set the pdm left gain.
  116. *
  117. * @return None.
  118. */
  119. void am_pdm_left_gain_set(uint8_t gain_val)
  120. {
  121. /* set the left gain */
  122. am_hal_pdm_left_gain_set(gain_val);
  123. }
  124. /**
  125. * @brief Get the pdm right gain.
  126. *
  127. * @param None.
  128. *
  129. * This function Get the pdm right gain.
  130. *
  131. * @return gain_val.
  132. */
  133. uint8_t am_pdm_right_gain_get(void)
  134. {
  135. /* get the right gain */
  136. return am_hal_pdm_right_gain_get();
  137. }
  138. /**
  139. * @brief Set the pdm right gain.
  140. *
  141. * @param gain_val.
  142. *
  143. * This function Set the pdm right gain.
  144. *
  145. * @return None.
  146. */
  147. void am_pdm_right_gain_set(uint8_t gain_val)
  148. {
  149. /* set the right gain */
  150. am_hal_pdm_right_gain_set(gain_val);
  151. }
  152. /**
  153. * @brief Interrupt handler for the PDM
  154. *
  155. * This function is Interrupt handler for the PDM
  156. *
  157. * @return None.
  158. */
  159. void am_pdm_isr (void)
  160. {
  161. int i;
  162. rt_int16_t pdm_sbufftemp[160];
  163. /* Clear the PDM interrupt */
  164. am_hal_pdm_int_clear(AM_HAL_PDM_INT_UNDFL | AM_HAL_PDM_INT_OVF | AM_HAL_PDM_INT_FIFO);
  165. for (i = 0; i < PDM_FIFO_THRESHOLD; i++) /* adjust as needed */
  166. {
  167. pdm_sbufftemp[i] = (rt_int16_t)am_hal_pdm_fifo_data_read();
  168. }
  169. /* send the message */
  170. rt_mq_send(&pdm_mq, pdm_sbufftemp, PDM_FIFO_THRESHOLD*sizeof(rt_int16_t));
  171. }
  172. /**
  173. * @brief Initialize the PDM
  174. *
  175. * This function initialize the PDM
  176. *
  177. * @return None.
  178. */
  179. int rt_hw_pdm_init(void)
  180. {
  181. /* Enable power to modules used */
  182. am_hal_pwrctrl_periph_enable(AM_HAL_PWRCTRL_PDM);
  183. /* Enable the PDM clock and data */
  184. am_hal_gpio_pin_config(PDM_GPIO_CLK, PDM_GPIO_CFG_CLK | AM_HAL_GPIO_HIGH_DRIVE);
  185. am_hal_gpio_pin_config(PDM_GPIO_DATA, PDM_GPIO_CFG_DATA );
  186. /* PDM setting */
  187. am_hal_pdm_config(&g_sPDMConfig);
  188. /* Enable PDM interrupts */
  189. am_hal_pdm_int_enable(AM_HAL_PDM_INT_FIFO);
  190. /* Clear PDM interrupts */
  191. am_hal_pdm_int_clear(AM_HAL_PDM_INT_UNDFL | AM_HAL_PDM_INT_OVF | AM_HAL_PDM_INT_FIFO);
  192. /* messagequeue init */
  193. rt_mq_init(&pdm_mq, "mq_pdm",
  194. &am_pdm_buffer_pool[0],
  195. 340 - sizeof(void*),
  196. sizeof(am_pdm_buffer_pool),
  197. RT_IPC_FLAG_FIFO);
  198. //rt_kprintf("pdm_init!\n");
  199. return 0;
  200. }
  201. #ifdef RT_USING_COMPONENTS_INIT
  202. INIT_BOARD_EXPORT(rt_hw_pdm_init);
  203. #endif
  204. #endif
  205. /*@}*/