1
0

pdm.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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. #include "board.h"
  28. #ifdef RT_USING_PDM
  29. /* sem define */
  30. rt_sem_t pdmsem = RT_NULL;
  31. #define NWA_FRAME_MS 10
  32. #define NWA_FRAME_SAMPLES (16*NWA_FRAME_MS) /* 16k, 16bit, mono audio data */
  33. #define PDM_FIFO_THRESHOLD NWA_FRAME_SAMPLES
  34. #define PDM_GPIO_CLK 22
  35. #define PDM_GPIO_CFG_CLK AM_HAL_PIN_22_PDM_CLK
  36. #define PDM_GPIO_DATA 23
  37. #define PDM_GPIO_CFG_DATA AM_HAL_PIN_23_PDM_DATA
  38. #define PING_PONG_BUF_SIZE 8000*NWA_FRAME_MS
  39. static rt_uint16_t am_pdm_buffer_pool[PING_PONG_BUF_SIZE];
  40. static rt_uint8_t pdm_flag = 0;
  41. static rt_uint16_t pdm_cnt = 0;
  42. static am_hal_pdm_config_t g_sPDMConfig =
  43. {
  44. AM_HAL_PDM_PCFG_LRSWAP_DISABLE | AM_HAL_PDM_PCFG_RIGHT_PGA_P105DB | AM_HAL_PDM_PCFG_LEFT_PGA_P105DB
  45. | AM_HAL_PDM_PCFG_MCLKDIV_DIV1 | AM_HAL_PDM_PCFG_SINC_RATE(48) | AM_HAL_PDM_PCFG_ADCHPD_ENABLE
  46. | AM_HAL_PDM_PCFG_HPCUTOFF(0xB) | AM_HAL_PDM_PCFG_CYCLES(0x1) | AM_HAL_PDM_PCFG_SOFTMUTE_DISABLE
  47. | AM_HAL_PDM_PCFG_PDMCORE_ENABLE, /* Set the PDM configuration */
  48. AM_REG_PDM_VCFG_IOCLKEN_EN | AM_HAL_PDM_VCFG_RSTB_NORMAL | AM_REG_PDM_VCFG_PDMCLKSEL_750KHz
  49. | AM_HAL_PDM_VCFG_PDMCLK_ENABLE | AM_HAL_PDM_VCFG_I2SMODE_DISABLE | AM_HAL_PDM_VCFG_BCLKINV_DISABLE
  50. | AM_HAL_PDM_VCFG_DMICDEL_DISABLE | AM_HAL_PDM_VCFG_SELAP_INTERNAL | AM_HAL_PDM_VCFG_PACK_DISABLE
  51. | AM_HAL_PDM_VCFG_CHANNEL_RIGHT, /* Set the Voice Configuration */
  52. PDM_FIFO_THRESHOLD, /* Select the FIFO PCM sample threshold */
  53. };
  54. /**
  55. * @brief Get the pdm data.
  56. *
  57. * @param None.
  58. *
  59. * This function Get the pdm data.
  60. *
  61. * @return None.
  62. */
  63. rt_uint8_t am_pdm_data_get(rt_uint8_t *buff, rt_uint16_t size)
  64. {
  65. uint8_t temp;
  66. int i;
  67. /* wait adc interrupt release sem forever */
  68. rt_sem_take(pdmsem, RT_WAITING_FOREVER);
  69. for(i = 0; i < PING_PONG_BUF_SIZE; i++)
  70. {
  71. temp = (uint8_t)(am_pdm_buffer_pool[i] & 0xFF);
  72. /* lower byte */
  73. while ( AM_BFRn(UART, 0, FR, TXFF) );
  74. AM_REGn(UART, 0, DR) = temp;
  75. temp = (uint8_t)((am_pdm_buffer_pool[i] & 0xFF00)>>8);
  76. /* higher byte */
  77. while ( AM_BFRn(UART, 0, FR, TXFF) );
  78. AM_REGn(UART, 0, DR) = temp;
  79. }
  80. /* copy the data */
  81. //rt_memcpy(buff, am_pdm_buffer_pool, size*sizeof(rt_uint16_t));
  82. pdm_flag = 0;
  83. return 0;
  84. }
  85. /**
  86. * @brief Start the pdm.
  87. *
  88. * @param None.
  89. *
  90. * This function Start the pdm.
  91. *
  92. * @return None.
  93. */
  94. void am_pdm_start(void)
  95. {
  96. /* adcsem create */
  97. pdmsem = rt_sem_create("pdmsem", 0, RT_IPC_FLAG_FIFO);
  98. /* Enable PDM */
  99. am_hal_pdm_enable();
  100. am_hal_interrupt_enable(AM_HAL_INTERRUPT_PDM);
  101. }
  102. /**
  103. * @brief Stop the pdm.
  104. *
  105. * @param None.
  106. *
  107. * This function Stop the pdm.
  108. *
  109. * @return None.
  110. */
  111. void am_pdm_stop(void)
  112. {
  113. /* Disable PDM */
  114. am_hal_pdm_disable();
  115. am_hal_interrupt_disable(AM_HAL_INTERRUPT_PDM);
  116. /* adcsem delete */
  117. rt_sem_delete(pdmsem);
  118. }
  119. /**
  120. * @brief Get the pdm left gain.
  121. *
  122. * @param None.
  123. *
  124. * This function Get the pdm left gain.
  125. *
  126. * @return gain_val.
  127. */
  128. uint8_t am_pdm_left_gain_get(void)
  129. {
  130. /* get the left gain */
  131. return am_hal_pdm_left_gain_get();
  132. }
  133. /**
  134. * @brief Set the pdm left gain.
  135. *
  136. * @param None.
  137. *
  138. * This function Set the pdm left gain.
  139. *
  140. * @return None.
  141. */
  142. void am_pdm_left_gain_set(uint8_t gain_val)
  143. {
  144. /* set the left gain */
  145. am_hal_pdm_left_gain_set(gain_val);
  146. }
  147. /**
  148. * @brief Get the pdm right gain.
  149. *
  150. * @param None.
  151. *
  152. * This function Get the pdm right gain.
  153. *
  154. * @return gain_val.
  155. */
  156. uint8_t am_pdm_right_gain_get(void)
  157. {
  158. /* get the right gain */
  159. return am_hal_pdm_right_gain_get();
  160. }
  161. /**
  162. * @brief Set the pdm right gain.
  163. *
  164. * @param None.
  165. *
  166. * This function Set the pdm right gain.
  167. *
  168. * @return None.
  169. */
  170. void am_pdm_right_gain_set(uint8_t gain_val)
  171. {
  172. /* set the right gain */
  173. am_hal_pdm_right_gain_set(gain_val);
  174. }
  175. /**
  176. * @brief Interrupt handler for the PDM
  177. *
  178. * This function is Interrupt handler for the PDM
  179. *
  180. * @return None.
  181. */
  182. void am_pdm_isr (void)
  183. {
  184. uint32_t ui32FifoData;
  185. int i;
  186. /* Clear the PDM interrupt */
  187. am_hal_pdm_int_clear(AM_HAL_PDM_INT_UNDFL | AM_HAL_PDM_INT_OVF | AM_HAL_PDM_INT_FIFO);
  188. for (i = 0; i < PDM_FIFO_THRESHOLD; i++) /* adjust as needed */
  189. {
  190. ui32FifoData = am_hal_pdm_fifo_data_read();
  191. if(pdm_flag == 0)
  192. {
  193. am_pdm_buffer_pool[pdm_cnt * PDM_FIFO_THRESHOLD + i] = (uint16_t)ui32FifoData;
  194. }
  195. }
  196. if(pdm_flag == 0)
  197. pdm_cnt ++;
  198. if(pdm_cnt == PING_PONG_BUF_SIZE/PDM_FIFO_THRESHOLD) /* 500 means 10 second logging under 8k sampling rate */
  199. {
  200. pdm_cnt = 0;
  201. pdm_flag = 1;
  202. /* release pdmsem */
  203. rt_sem_release(pdmsem);
  204. }
  205. }
  206. /**
  207. * @brief Initialize the PDM
  208. *
  209. * This function initialize the PDM
  210. *
  211. * @return None.
  212. */
  213. int rt_hw_pdm_init(void)
  214. {
  215. /* Enable power to modules used */
  216. am_hal_pwrctrl_periph_enable(AM_HAL_PWRCTRL_PDM);
  217. /* Enable the PDM clock and data */
  218. am_hal_gpio_pin_config(PDM_GPIO_CLK, PDM_GPIO_CFG_CLK | AM_HAL_GPIO_HIGH_DRIVE);
  219. am_hal_gpio_pin_config(PDM_GPIO_DATA, PDM_GPIO_CFG_DATA );
  220. /* PDM setting */
  221. am_hal_pdm_config(&g_sPDMConfig);
  222. /* Enable PDM interrupts */
  223. am_hal_pdm_int_enable(AM_HAL_PDM_INT_FIFO);
  224. /* Clear PDM interrupts */
  225. am_hal_pdm_int_clear(AM_HAL_PDM_INT_UNDFL | AM_HAL_PDM_INT_OVF | AM_HAL_PDM_INT_FIFO);
  226. rt_kprintf("pdm_init!\n");
  227. return 0;
  228. }
  229. #ifdef RT_USING_COMPONENTS_INIT
  230. INIT_BOARD_EXPORT(rt_hw_pdm_init);
  231. #endif
  232. #endif
  233. /*@}*/