voice.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. /**************************************************************************************************
  2. Phyplus Microelectronics Limited confidential and proprietary.
  3. All rights reserved.
  4. IMPORTANT: All rights of this software belong to Phyplus Microelectronics
  5. Limited ("Phyplus"). Your use of this Software is limited to those
  6. specific rights granted under the terms of the business contract, the
  7. confidential agreement, the non-disclosure agreement and any other forms
  8. of agreements as a customer or a partner of Phyplus. You may not use this
  9. Software unless you agree to abide by the terms of these agreements.
  10. You acknowledge that the Software may not be modified, copied,
  11. distributed or disclosed unless embedded on a Phyplus Bluetooth Low Energy
  12. (BLE) integrated circuit, either as a product or is integrated into your
  13. products. Other than for the aforementioned purposes, you may not use,
  14. reproduce, copy, prepare derivative works of, modify, distribute, perform,
  15. display or sell this Software and/or its documentation for any purposes.
  16. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  17. PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  18. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  19. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  20. PHYPLUS OR ITS SUBSIDIARIES BE LIABLE OR OBLIGATED UNDER CONTRACT,
  21. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  22. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  23. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  24. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  25. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  26. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  27. **************************************************************************************************/
  28. /*******************************************************************************
  29. * @file voice.c
  30. * @brief Contains all functions support for adc driver
  31. * @version 0.0
  32. * @date 16. Jun. 2018
  33. * @author qing.han
  34. *
  35. * Copyright(C) 2018, PhyPlus Microelectronics
  36. * All rights reserved.
  37. *
  38. *******************************************************************************/
  39. #include "error.h"
  40. #include "ap_cp.h"
  41. #include "common.h"
  42. #include "gpio.h"
  43. #include "pwrmgr.h"
  44. #include "clock.h"
  45. #include "adc.h"
  46. #include <string.h>
  47. #include "log.h"
  48. #include "voice.h"
  49. static voice_Ctx_t mVoiceCtx;
  50. static uint32_t voice_data[HALF_VOICE_WORD_SIZE];
  51. // Enable voice core
  52. void hal_voice_enable(void)
  53. {
  54. clk_gate_enable(MOD_ADCC);
  55. subWriteReg(0x40050000,0,0,1);
  56. }
  57. // Disable voice core
  58. void hal_voice_disable(void)
  59. {
  60. clk_gate_disable(MOD_ADCC);
  61. subWriteReg(0x40050000,0,0,0);
  62. }
  63. // Select DMIC
  64. void hal_voice_dmic_mode(void)
  65. {
  66. subWriteReg(0x4005000c,0,0,1);
  67. }
  68. // Select AMIC
  69. void hal_voice_amic_mode(void)
  70. {
  71. subWriteReg(0x4005000c,0,0,0);
  72. subWriteReg(0x4000f048,7,5,0); //Connect ADC to PGA
  73. subWriteReg(0x4000f07c,4,4,1);
  74. subWriteReg(0x4000f07c,0,0,1);
  75. subWriteReg(0x4000F000 + 0x7c,2,1,HAL_ADC_CLOCK_320K);
  76. }
  77. // Open a GPIO pin for DMIC
  78. void hal_voice_dmic_open(GPIO_Pin_e dmicDataPin, GPIO_Pin_e dmicClkPin)
  79. {
  80. hal_gpio_fmux_set(dmicDataPin, (Fmux_Type_e)ADCC);
  81. hal_gpio_fmux_set(dmicClkPin, (Fmux_Type_e)CLK1P28M);
  82. }
  83. // Set PGA gain for AMIC
  84. void hal_voice_amic_gain(uint8_t amicGain)
  85. {
  86. uint8_t pgaGain1;
  87. uint8_t pgaGain2;
  88. if (amicGain > 14)
  89. amicGain = 14;
  90. if (amicGain > 8) {
  91. pgaGain1 = 2;
  92. pgaGain2 = amicGain - 8;
  93. }
  94. else if (amicGain > 4) {
  95. pgaGain1 = 1;
  96. pgaGain2 = amicGain - 4;
  97. }
  98. else {
  99. pgaGain1 = 0;
  100. pgaGain2 = amicGain;
  101. }
  102. subWriteReg(0x4000f048,18,17,(uint32_t)pgaGain1);
  103. subWriteReg(0x4000f048,21,19,(uint32_t)pgaGain2);
  104. }
  105. // Set voice process gain
  106. void hal_voice_gain(uint8_t voiceGain)
  107. {
  108. subWriteReg(0x4005000c,22,16,(uint32_t)voiceGain);
  109. }
  110. // Set voice encoding mode
  111. void hal_voice_encode(VOICE_ENCODE_t voiceEncodeMode)
  112. {
  113. subWriteReg(0x4005000c,13,12,voiceEncodeMode);
  114. }
  115. // Set voice data rate
  116. void hal_voice_rate(VOICE_RATE_t voiceRate)
  117. {
  118. subWriteReg(0x4005000c,9,8,voiceRate);
  119. }
  120. // INTERNAL: Set voice notch filter config
  121. static void set_voice_notch(VOICE_NOTCH_t voiceNotch)
  122. {
  123. subWriteReg(0x4005000c,3,2,voiceNotch);
  124. }
  125. // INTERNAL: Set voice data polarity
  126. static void set_voice_polarity(VOICE_POLARITY_t voicePolarity)
  127. {
  128. subWriteReg(0x4005000c,1,1,voicePolarity);
  129. }
  130. // Enable voice auto-mute
  131. void hal_voice_amute_on(void)
  132. {
  133. subWriteReg(0x40050014,0,0,0);
  134. }
  135. // Disable voice auto-mute
  136. void hal_voice_amute_off(void)
  137. {
  138. subWriteReg(0x40050014,0,0,1);
  139. }
  140. // INTERNAL: Set voice auto-mute configurations
  141. static void set_voice_amute_cfg(
  142. uint16_t amutGainMax,
  143. uint8_t amutGainBwMax,
  144. uint8_t amutGdut,
  145. uint8_t amutGst2,
  146. uint8_t amutGst1,
  147. uint16_t amutLvl2,
  148. uint16_t amutLvl1,
  149. uint8_t amutAlvl,
  150. uint8_t amutBeta,
  151. uint8_t amutWinl)
  152. {
  153. subWriteReg(0x40050010,30,20,(uint32_t)amutGainMax);
  154. subWriteReg(0x40050010,19,16,(uint32_t)amutGainBwMax);
  155. subWriteReg(0x40050010,13,8,(uint32_t)amutGdut);
  156. subWriteReg(0x40050010,7,4,(uint32_t)amutGst2);
  157. subWriteReg(0x40050010,3,0,(uint32_t)amutGst1);
  158. subWriteReg(0x40050014,30,20,(uint32_t)amutLvl2);
  159. subWriteReg(0x40050014,18,8,(uint32_t)amutLvl1);
  160. subWriteReg(0x40050018,15,8,(uint32_t)amutAlvl);
  161. subWriteReg(0x40050018,6,4,(uint32_t)amutBeta);
  162. subWriteReg(0x40050018,3,0,(uint32_t)amutWinl);
  163. }
  164. /**************************************************************************************
  165. * @fn hal_VOICE_IRQHandler
  166. *
  167. * @brief This function process for adc interrupt
  168. *
  169. * input parameters
  170. *
  171. * @param None.
  172. *
  173. * output parameters
  174. *
  175. * @param None.
  176. *
  177. * @return None.
  178. **************************************************************************************/
  179. void __attribute__((used)) hal_ADC_IRQHandler(void)
  180. {
  181. // uint32_t voice_data[HALF_VOICE_SAMPLE_SIZE];
  182. // LOG("Voice interrupt processing\n");
  183. MASK_VOICE_INT;
  184. if (GET_IRQ_STATUS & BIT(8)) {
  185. int n;
  186. for (n = 0; n < HALF_VOICE_WORD_SIZE; n++) {
  187. voice_data[n] = (uint32_t)(read_reg(VOICE_BASE + n * 4));
  188. }
  189. CLEAR_VOICE_HALF_INT;
  190. while (IS_CLAER_VOICE_HALF_INT) {}
  191. // if(mVoiceCtx.enable == FALSE)
  192. // continue;
  193. if (mVoiceCtx.evt_handler) {
  194. voice_Evt_t evt;
  195. evt.type = HAL_VOICE_EVT_DATA;
  196. evt.data = voice_data;
  197. evt.size = HALF_VOICE_WORD_SIZE;
  198. mVoiceCtx.evt_handler(&evt);
  199. // LOG("Voice memory half full interrupt processing completed\n");
  200. }
  201. }
  202. else if (GET_IRQ_STATUS & BIT(9)) {
  203. int n;
  204. for (n = 0; n < HALF_VOICE_WORD_SIZE; n++) {
  205. voice_data[n] = (uint32_t)(read_reg(VOICE_MID_BASE + n * 4));
  206. }
  207. CLEAR_VOICE_FULL_INT;
  208. while (IS_CLAER_VOICE_FULL_INT) {}
  209. // if(mVoiceCtx.enable == FALSE)
  210. // continue;
  211. if (mVoiceCtx.evt_handler) {
  212. voice_Evt_t evt;
  213. evt.type = HAL_VOICE_EVT_DATA;
  214. evt.data = voice_data;
  215. evt.size = HALF_VOICE_WORD_SIZE;
  216. mVoiceCtx.evt_handler(&evt);
  217. // LOG("Voice memory full interrupt processing completed\n");
  218. }
  219. }
  220. ENABLE_VOICE_INT;
  221. }
  222. /**************************************************************************************
  223. * @fn hal_voice_init
  224. *
  225. * @brief This function process for adc initial
  226. *
  227. * input parameters
  228. *
  229. * @param ADC_MODE_e mode: adc sample mode select;1:SAM_MANNUAL(mannual mode),0:SAM_AUTO(auto mode)
  230. * ADC_CH_e adc_pin: adc pin select;ADC_CH0~ADC_CH7 and ADC_CH_VOICE
  231. * ADC_SEMODE_e semode: signle-ended mode negative side enable; 1:SINGLE_END(single-ended mode) 0:DIFF(Differentail mode)
  232. * IO_CONTROL_e amplitude: input signal amplitude, 0:BELOW_1V,1:UP_1V
  233. *
  234. * output parameters
  235. *
  236. * @param None.
  237. *
  238. * @return None.
  239. **************************************************************************************/
  240. void hal_voice_init(void) {
  241. hal_pwrmgr_register(MOD_ADCC,NULL,NULL);
  242. hal_pwrmgr_register(MOD_VOC,NULL,NULL);
  243. memset(&mVoiceCtx, 0, sizeof(mVoiceCtx));;
  244. }
  245. int hal_voice_config(voice_Cfg_t cfg, voice_Hdl_t evt_handler)
  246. {
  247. if(mVoiceCtx.enable)
  248. return PPlus_ERR_BUSY;
  249. if(evt_handler == NULL)
  250. return PPlus_ERR_INVALID_PARAM;
  251. clk_gate_enable(MOD_ADCC);//enable I2C clk gated
  252. mVoiceCtx.evt_handler = evt_handler; //evt_handler;
  253. // mVoiceCtx.evt_handler = evtHandler;
  254. if(cfg.voiceSelAmicDmic) {
  255. hal_voice_dmic_mode();
  256. hal_voice_dmic_open(cfg.dmicDataPin, cfg.dmicClkPin);
  257. }
  258. else {
  259. hal_voice_amic_mode();
  260. hal_voice_amic_gain(cfg.amicGain);
  261. }
  262. hal_voice_gain(cfg.voiceGain);
  263. hal_voice_encode(cfg.voiceEncodeMode);
  264. hal_voice_rate(cfg.voiceRate);
  265. set_voice_notch(VOICE_NOTCH_1);
  266. set_voice_polarity(VOICE_POLARITY_POS);
  267. if(cfg.voiceAutoMuteOnOff) {
  268. hal_voice_amute_off();
  269. }
  270. else {
  271. hal_voice_amute_on();
  272. }
  273. set_voice_amute_cfg(64, 6, 9, 0, 1, 55, 10, 48, 3, 10);
  274. mVoiceCtx.cfg = cfg;
  275. // mVoiceCtx.evt_handler = evt_handler;
  276. //CLK_1P28M_ENABLE;
  277. AP_PCRM->CLKSEL |= BIT(6);
  278. //ENABLE_XTAL_OUTPUT; //enable xtal 16M output,generate the 32M dll clock
  279. AP_PCRM->CLKHF_CTL0 |= BIT(18);
  280. //ENABLE_DLL; //enable DLL
  281. AP_PCRM->CLKHF_CTL1 |= BIT(7);
  282. //ADC_DBLE_CLOCK_DISABLE; //disable double 32M clock,we are now use 32M clock,should enable bit<13>, diable bit<21>
  283. AP_PCRM->CLKHF_CTL1 &= ~BIT(21);
  284. //ADC_CLOCK_ENABLE; //adc clock enbale,always use clk_32M
  285. AP_PCRM->CLKHF_CTL1 |= BIT(13);
  286. //subWriteReg(0x4000f07c,4,4,1); //set adc mode,1:mannual,0:auto mode
  287. AP_PCRM->ADC_CTL4 |= BIT(4);
  288. //*(volatile unsigned int *) 0x4000f040=0x5014B820;
  289. //*(volatile unsigned int *) 0x4000f044=0x019028b0;
  290. //*(volatile unsigned int *) 0x4000f048=0x0000014b;
  291. // hal_pwrmgr_register(MOD_ADCC,NULL,NULL);
  292. // hal_pwrmgr_register(MOD_VOC,NULL,NULL);
  293. return PPlus_SUCCESS;
  294. }
  295. int hal_voice_start(void)
  296. {
  297. clk_gate_enable(MOD_ADCC);
  298. mVoiceCtx.enable = TRUE;
  299. hal_pwrmgr_lock(MOD_ADCC);
  300. hal_pwrmgr_lock(MOD_VOC);
  301. if (mVoiceCtx.cfg.voiceSelAmicDmic) {
  302. }
  303. else {
  304. AP_PCRM->ANA_CTL |= BIT(16); //Power on PGA
  305. AP_PCRM->ANA_CTL |= BIT(3); //Power on ADC
  306. }
  307. NVIC_SetPriority((IRQn_Type)ADCC_IRQ, IRQ_PRIO_HAL);//teddy add 20190121
  308. //ADCC_IRQ_ENABLE;
  309. NVIC_EnableIRQ((IRQn_Type)ADCC_IRQ);
  310. //Enable voice core
  311. hal_voice_enable();
  312. //Enable VOICE IRQ
  313. ENABLE_VOICE_INT;
  314. return PPlus_SUCCESS;
  315. }
  316. int hal_voice_stop(void)
  317. {
  318. MASK_VOICE_INT;
  319. //Disable voice core
  320. hal_voice_disable();
  321. if (mVoiceCtx.cfg.voiceSelAmicDmic) {
  322. }
  323. else {
  324. AP_PCRM->ANA_CTL &= ~BIT(16); //Power off PGA
  325. }
  326. //Enable sleep
  327. hal_pwrmgr_unlock(MOD_VOC);
  328. hal_pwrmgr_unlock(MOD_ADCC);
  329. mVoiceCtx.enable = FALSE;
  330. return 0;
  331. }
  332. int hal_voice_clear(void)
  333. {
  334. //MASK_VOICE_INT;
  335. MASK_VOICE_INT;
  336. NVIC_DisableIRQ((IRQn_Type)ADCC_IRQ);
  337. if (mVoiceCtx.cfg.voiceSelAmicDmic) {
  338. hal_gpioin_disable(mVoiceCtx.cfg.dmicDataPin);
  339. hal_gpioin_disable(mVoiceCtx.cfg.dmicClkPin);
  340. }
  341. else {
  342. }
  343. //clk_gate_disable(MOD_ADCC);//disable I2C clk gated
  344. memset(&mVoiceCtx, 0, sizeof(mVoiceCtx));
  345. //enableSleep();
  346. hal_pwrmgr_unlock(MOD_VOC);
  347. return 0;
  348. }