adc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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 adc.c
  30. * @brief Contains all functions support for adc driver
  31. * @version 0.0
  32. * @date 18. Oct. 2017
  33. * @author qing.han
  34. *
  35. * Copyright(C) 2016, PhyPlus Semiconductor
  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. static bool mAdc_init_flg = FALSE;
  49. static adc_Ctx_t mAdc_Ctx;
  50. static uint8_t adc_cal_read_flag = 0;
  51. static uint16_t adc_cal_postive = 0x0fff;
  52. static uint16_t adc_cal_negtive = 0x0fff;
  53. GPIO_Pin_e s_pinmap[ADC_CH_NUM] = {
  54. GPIO_DUMMY, //ADC_CH0 =0,
  55. GPIO_DUMMY, //ADC_CH1 =1,
  56. P11, //ADC_CH1N =2,
  57. P12, //ADC_CH1P =3, ADC_CH1DIFF = 3,
  58. P13, //ADC_CH2N =4,
  59. P14, //ADC_CH2P =5, ADC_CH2DIFF = 5,
  60. P15, //ADC_CH3N =6,
  61. P20, //ADC_CH3P =7, ADC_CH3DIFF = 7,
  62. GPIO_DUMMY, //ADC_CH_VOICE =8,
  63. };
  64. static void set_sampling_resolution(adc_CH_t channel, bool is_high_resolution,bool is_differential_mode)
  65. {
  66. uint8_t ch1,ch2;
  67. ch1 = (uint8_t)(channel - 2);
  68. ch2 = (ch1%2)?(ch1-1):(ch1+1);
  69. if(is_high_resolution)
  70. {
  71. if(is_differential_mode)
  72. {
  73. BM_SET(REG_IO_CONTROL,BIT(ch1));
  74. BM_CLR(REG_IO_CONTROL,BIT(ch1+8));
  75. BM_SET(REG_IO_CONTROL,BIT(ch2));
  76. BM_CLR(REG_IO_CONTROL,BIT(ch2+8));
  77. }
  78. else
  79. {
  80. BM_SET(REG_IO_CONTROL,BIT(ch1));
  81. BM_CLR(REG_IO_CONTROL,BIT(ch1+8));
  82. }
  83. }
  84. else
  85. {
  86. if(is_differential_mode)
  87. {
  88. BM_CLR(REG_IO_CONTROL,BIT(ch1));
  89. BM_SET(REG_IO_CONTROL,BIT(ch1+8));
  90. BM_CLR(REG_IO_CONTROL,BIT(ch2));
  91. BM_SET(REG_IO_CONTROL,BIT(ch2+8));
  92. }
  93. else
  94. {
  95. BM_CLR(REG_IO_CONTROL,BIT(ch1));
  96. BM_SET(REG_IO_CONTROL,BIT(ch1+8));
  97. }
  98. }
  99. }
  100. static void set_sampling_resolution_auto(uint8_t channel, uint8_t is_high_resolution,uint8_t is_differential_mode)
  101. {
  102. uint8_t i_channel;
  103. adc_CH_t a_channel;
  104. *REG_IO_CONTROL = 0x00;
  105. for(i_channel =2;i_channel<(ADC_CH_NUM-1);i_channel++)
  106. {
  107. if(channel & BIT(i_channel))
  108. {
  109. a_channel = (adc_CH_t)i_channel;
  110. set_sampling_resolution(a_channel,
  111. (is_high_resolution & BIT(i_channel)),
  112. (is_differential_mode & BIT(i_channel)));
  113. }
  114. }
  115. }
  116. static void set_differential_mode(void)
  117. {
  118. subWriteReg(0x4000f048,8,8,0);
  119. subWriteReg(0x4000f048,11,11,0);
  120. }
  121. static void disable_analog_pin(adc_CH_t channel)
  122. {
  123. int index = (int)channel;
  124. GPIO_Pin_e pin = s_pinmap[index];
  125. if(pin == GPIO_DUMMY)
  126. return;
  127. hal_gpio_cfg_analog_io(pin,Bit_DISABLE);
  128. hal_gpio_pin_init(pin,IE); //ie=0,oen=1 set to imput
  129. hal_gpio_pull_set(pin,FLOATING); //set pin pull up/down floating
  130. }
  131. static void clear_adcc_cfg(void)
  132. {
  133. memset(&mAdc_Ctx, 0, sizeof(mAdc_Ctx));
  134. }
  135. /////////////// adc ////////////////////////////
  136. /**************************************************************************************
  137. * @fn hal_ADC_IRQHandler
  138. *
  139. * @brief This function process for adc interrupt
  140. *
  141. * input parameters
  142. *
  143. * @param None.
  144. *
  145. * output parameters
  146. *
  147. * @param None.
  148. *
  149. * @return None.
  150. **************************************************************************************/
  151. void __attribute__((used)) hal_ADC_IRQHandler(void)
  152. {
  153. int ch,status,ch2,n;
  154. uint16_t adc_data[MAX_ADC_SAMPLE_SIZE];
  155. status = GET_IRQ_STATUS;
  156. MASK_ADC_INT;
  157. //LOG("int(0x4005003c):%x\n",*(volatile unsigned int *)0x4005003c);
  158. if(status == mAdc_Ctx.all_channel)//to check this fun
  159. {
  160. for (ch = 2; ch <= ADC_CH7; ch++) {
  161. if (status & BIT(ch)) {
  162. AP_ADCC->intr_mask &= ~BIT(ch);
  163. for (n = 0; n < (MAX_ADC_SAMPLE_SIZE-3); n++) {
  164. adc_data[n] = (uint16_t)(read_reg(ADC_CH_BASE + (ch * 0x80) + ((n+2) * 4))&0xfff);
  165. adc_data[n+1] = (uint16_t)((read_reg(ADC_CH_BASE + (ch * 0x80) + ((n+2) * 4))>>16)&0xfff);
  166. }
  167. AP_ADCC->intr_clear = BIT(ch);
  168. if(mAdc_Ctx.enable == FALSE)
  169. continue;
  170. ch2=(ch%2)?(ch-1):(ch+1);
  171. if (mAdc_Ctx.evt_handler[ch2]){
  172. adc_Evt_t evt;
  173. evt.type = HAL_ADC_EVT_DATA;
  174. evt.ch = (adc_CH_t)ch2;
  175. evt.data = adc_data;
  176. evt.size = MAX_ADC_SAMPLE_SIZE-3;
  177. mAdc_Ctx.evt_handler[ch2](&evt);
  178. }
  179. AP_ADCC->intr_mask |= BIT(ch);
  180. }
  181. }
  182. if(mAdc_Ctx.continue_mode == FALSE){
  183. hal_adc_stop();
  184. }
  185. }
  186. ENABLE_ADC_INT;
  187. }
  188. static void adc_wakeup_hdl(void)
  189. {
  190. NVIC_SetPriority((IRQn_Type)ADCC_IRQ, IRQ_PRIO_HAL);
  191. }
  192. /**************************************************************************************
  193. * @fn hal_adc_init
  194. *
  195. * @brief This function process for adc initial
  196. *
  197. * input parameters
  198. *
  199. * @param ADC_MODE_e mode: adc sample mode select;1:SAM_MANNUAL(mannual mode),0:SAM_AUTO(auto mode)
  200. * ADC_CH_e adc_pin: adc pin select;ADC_CH0~ADC_CH7 and ADC_CH_VOICE
  201. * ADC_SEMODE_e semode: signle-ended mode negative side enable; 1:SINGLE_END(single-ended mode) 0:DIFF(Differentail mode)
  202. * IO_CONTROL_e amplitude: input signal amplitude, 0:BELOW_1V,1:UP_1V
  203. *
  204. * output parameters
  205. *
  206. * @param None.
  207. *
  208. * @return None.
  209. **************************************************************************************/
  210. void hal_adc_init(void) {
  211. mAdc_init_flg = TRUE;
  212. hal_pwrmgr_register(MOD_ADCC,NULL,adc_wakeup_hdl);
  213. clear_adcc_cfg();
  214. }
  215. int hal_adc_clock_config(adc_CLOCK_SEL_t clk){
  216. if(!mAdc_init_flg){
  217. return PPlus_ERR_NOT_REGISTED;
  218. }
  219. subWriteReg(0x4000F000 + 0x7c,2,1,clk);
  220. return PPlus_SUCCESS;
  221. }
  222. int hal_adc_start(void)
  223. {
  224. if(!mAdc_init_flg){
  225. return PPlus_ERR_NOT_REGISTED;
  226. }
  227. mAdc_Ctx.enable = TRUE;
  228. hal_pwrmgr_lock(MOD_ADCC);
  229. //ENABLE_ADC;
  230. AP_PCRM->ANA_CTL |= BIT(3);
  231. //ADC_IRQ_ENABLE;
  232. NVIC_EnableIRQ((IRQn_Type)ADCC_IRQ);
  233. //ENABLE_ADC_INT;
  234. AP_ADCC->intr_mask = 0x1ff;
  235. //disableSleep();
  236. return PPlus_SUCCESS;
  237. }
  238. int hal_adc_config_channel(adc_Cfg_t cfg, adc_Hdl_t evt_handler)
  239. {
  240. uint8_t i;
  241. uint8_t chn_sel,evt_index;
  242. GPIO_Pin_e pin,pin1;
  243. if(!mAdc_init_flg){
  244. return PPlus_ERR_NOT_REGISTED;
  245. }
  246. if(mAdc_Ctx.enable){
  247. return PPlus_ERR_BUSY;
  248. }
  249. if(evt_handler == NULL){
  250. return PPlus_ERR_INVALID_PARAM;
  251. }
  252. if(cfg.channel & BIT(0)/*||channel == ADC_CH1*/ ){
  253. return PPlus_ERR_NOT_SUPPORTED;
  254. }
  255. if((!cfg.channel & BIT(1))&&(cfg.is_differential_mode && (cfg.channel & BIT(1)))){
  256. return PPlus_ERR_INVALID_PARAM;
  257. }
  258. if(cfg.is_differential_mode != 0){
  259. if((cfg.is_differential_mode != 0x80) && (cfg.is_differential_mode != 0x20) && (cfg.is_differential_mode != 0x08)){
  260. return PPlus_ERR_INVALID_PARAM;
  261. }
  262. }
  263. mAdc_Ctx.continue_mode = cfg.is_continue_mode;
  264. mAdc_Ctx.all_channel = cfg.channel & 0x03;
  265. for(i=2;i<8;i++){
  266. if(cfg.channel & BIT(i)){
  267. if(i%2){
  268. mAdc_Ctx.all_channel |= BIT(i-1);
  269. }
  270. else{
  271. mAdc_Ctx.all_channel |= BIT(i+1);
  272. }
  273. }
  274. }
  275. if((AP_PCR->CLKG & BIT(MOD_ADCC)) == 0){
  276. clk_gate_enable(MOD_ADCC);
  277. }
  278. //CLK_1P28M_ENABLE;
  279. AP_PCRM->CLKSEL |= BIT(6);
  280. //ENABLE_XTAL_OUTPUT; //enable xtal 16M output,generate the 32M dll clock
  281. AP_PCRM->CLKHF_CTL0 |= BIT(18);
  282. //ENABLE_DLL; //enable DLL
  283. AP_PCRM->CLKHF_CTL1 |= BIT(7);
  284. //ADC_DBLE_CLOCK_DISABLE; //disable double 32M clock,we are now use 32M clock,should enable bit<13>, diable bit<21>
  285. AP_PCRM->CLKHF_CTL1 &= ~BIT(21);
  286. //ADC_CLOCK_ENABLE; //adc clock enbale,always use clk_32M
  287. AP_PCRM->CLKHF_CTL1 |= BIT(13);
  288. //subWriteReg(0x4000f07c,4,4,1); //set adc mode,1:mannual,0:auto mode
  289. AP_PCRM->ADC_CTL4 |= BIT(4);
  290. AP_PCRM->ADC_CTL4 |= BIT(0);
  291. set_sampling_resolution_auto(cfg.channel, cfg.is_high_resolution,cfg.is_differential_mode);
  292. AP_PCRM->ADC_CTL0 &= ~BIT(20);
  293. AP_PCRM->ADC_CTL0 &= ~BIT(4);
  294. AP_PCRM->ADC_CTL1 &= ~BIT(20);
  295. AP_PCRM->ADC_CTL1 &= ~BIT(4);
  296. AP_PCRM->ADC_CTL2 &= ~BIT(20);
  297. AP_PCRM->ADC_CTL2 &= ~BIT(4);
  298. AP_PCRM->ADC_CTL3 &= ~BIT(20);
  299. AP_PCRM->ADC_CTL3 &= ~BIT(4);
  300. if(cfg.is_differential_mode == 0){
  301. AP_PCRM->ADC_CTL4 &= ~BIT(4); //enable auto mode
  302. for(i=2;i<8;i++){
  303. if(cfg.channel & BIT(i)){
  304. GPIO_Pin_e pin = s_pinmap[i];
  305. pad_ds_control(pin, Bit_ENABLE);
  306. hal_gpio_cfg_analog_io(pin, Bit_ENABLE);
  307. switch (i)
  308. {
  309. case 0:
  310. AP_PCRM->ADC_CTL0 |= BIT(20);
  311. break;
  312. case 1:
  313. AP_PCRM->ADC_CTL0 |= BIT(4);
  314. break;
  315. case 2:
  316. AP_PCRM->ADC_CTL1 |= BIT(20);
  317. break;
  318. case 3:
  319. AP_PCRM->ADC_CTL1 |= BIT(4);
  320. break;
  321. case 4:
  322. AP_PCRM->ADC_CTL2 |= BIT(20);
  323. break;
  324. case 5:
  325. AP_PCRM->ADC_CTL2 |= BIT(4);
  326. break;
  327. case 6:
  328. AP_PCRM->ADC_CTL3 |= BIT(20);
  329. break;
  330. case 7:
  331. AP_PCRM->ADC_CTL3 |= BIT(4);
  332. break;
  333. default:
  334. break;
  335. }
  336. mAdc_Ctx.evt_handler[i] = evt_handler;
  337. }
  338. }
  339. }
  340. else{
  341. switch(cfg.is_differential_mode)
  342. {
  343. case 0x80:
  344. pin = P20;
  345. chn_sel = 0x04;
  346. evt_index = 7;
  347. break;
  348. case 0x20:
  349. pin = P14;
  350. chn_sel = 0x03;
  351. evt_index = 5;
  352. break;
  353. case 0x08:
  354. pin = P12;
  355. chn_sel = 0x02;
  356. evt_index = 3;
  357. break;
  358. default:
  359. break;
  360. }
  361. pad_ds_control(pin, Bit_ENABLE);
  362. subWriteReg(0x4000f048,7,5,chn_sel);
  363. set_differential_mode();
  364. if(pin == P20){
  365. pin1 = P15;
  366. }
  367. else{
  368. pin1 = (GPIO_Pin_e)(pin - GPIO_P01);
  369. }
  370. hal_gpio_cfg_analog_io(pin,Bit_ENABLE);
  371. hal_gpio_cfg_analog_io(pin1,Bit_ENABLE);
  372. mAdc_Ctx.all_channel = (cfg.is_differential_mode >> 1);
  373. mAdc_Ctx.evt_handler[evt_index] = evt_handler;
  374. }
  375. return PPlus_SUCCESS;
  376. }
  377. int hal_adc_stop(void)
  378. {
  379. int i;
  380. if(!mAdc_init_flg){
  381. return PPlus_ERR_NOT_REGISTED;
  382. }
  383. //MASK_ADC_INT;
  384. AP_ADCC->intr_mask = 0x1ff;
  385. NVIC_DisableIRQ((IRQn_Type)ADCC_IRQ);
  386. //DISABLE_ADC;
  387. AP_PCRM->ANA_CTL &= ~BIT(3);
  388. ADC_INIT_TOUT(to);
  389. AP_ADCC->intr_clear = 0x1FF;
  390. while(AP_ADCC->intr_status != 0){
  391. ADC_CHECK_TOUT(to, ADC_OP_TIMEOUT, "hal_adc_clear_int_status timeout\n");
  392. }
  393. //ADC_CLOCK_DISABLE;
  394. AP_PCRM->CLKHF_CTL1 &= ~BIT(13);
  395. for(i =0; i< ADC_CH_NUM; i++){
  396. if(mAdc_Ctx.evt_handler[i]){
  397. disable_analog_pin((adc_CH_t)i);
  398. }
  399. }
  400. clk_gate_disable(MOD_ADCC);//disable I2C clk gated
  401. clear_adcc_cfg();
  402. //enableSleep();
  403. hal_pwrmgr_unlock(MOD_ADCC);
  404. return PPlus_SUCCESS;
  405. }
  406. /**************************************************************************************
  407. * @fn hal_adc_value
  408. *
  409. * @brief This function process for get adc value
  410. *
  411. * input parameters
  412. *
  413. * @param ADC_CH_e adc_pin: adc pin select;ADC_CH0~ADC_CH7 and ADC_CH_VOICE
  414. *
  415. * output parameters
  416. *
  417. * @param None.
  418. *
  419. * @return ADC value
  420. **************************************************************************************/
  421. //float hal_adc_value(uint16_t* buf, uint8_t size, uint8_t high_resol, uint8_t diff_mode)
  422. ////float hal_adc_value(adc_CH_t ch,uint16_t* buf, uint8_t size, uint8_t high_resol, uint8_t diff_mode)
  423. //{
  424. // uint8_t i;
  425. // int adc_sum = 0;
  426. // float result = 0.0;
  427. // for (i = 0; i < size; i++) {
  428. // int value;
  429. // if(diff_mode == TRUE){
  430. // value = (buf[i] & 0x0800) ? 0 - (int)(buf[i]&0x7ff) : (int)(buf[i]&0x7ff);
  431. // }
  432. // else{
  433. // value = (int)(buf[i]&0xfff);
  434. // }
  435. // adc_sum += value;
  436. // }
  437. // result = ((float)adc_sum)/size;
  438. // result = (diff_mode) ? (result / 2048 -1) : (result /4096);
  439. // if(high_resol == FALSE)
  440. // result = result * 4;
  441. // return result;
  442. //}
  443. static void hal_adc_load_calibration_value(void)
  444. {
  445. if(adc_cal_read_flag==FALSE){
  446. adc_cal_read_flag = TRUE;
  447. adc_cal_negtive = read_reg(0x11001000)&0x0fff;
  448. adc_cal_postive = (read_reg(0x11001000)>>16)&0x0fff;
  449. LOG("AD_CAL[%x %x]\n",adc_cal_negtive,adc_cal_postive);
  450. }
  451. }
  452. float hal_adc_value_cal(adc_CH_t ch,uint16_t* buf, uint32_t size, uint8_t high_resol, uint8_t diff_mode)
  453. {
  454. uint32_t i;
  455. int adc_sum = 0;
  456. float result = 0.0;
  457. for (i = 0; i < size; i++) {
  458. adc_sum += (buf[i]&0xfff);
  459. }
  460. hal_adc_load_calibration_value();
  461. result = ((float)adc_sum)/size;
  462. if((adc_cal_postive!=0xfff) && (adc_cal_negtive!=0xfff)){
  463. float delta = ((int)(adc_cal_postive - adc_cal_negtive))/2.0;
  464. if(ch&0x01)
  465. {
  466. result = (diff_mode) ? ((result - 2048 - delta) * 2 / (adc_cal_postive + adc_cal_negtive))
  467. : ((result + delta) / (adc_cal_postive + adc_cal_negtive));
  468. }
  469. else
  470. {
  471. result = (diff_mode) ? ((result-2048-delta)*2/(adc_cal_postive+adc_cal_negtive))
  472. : ((result-delta) /(adc_cal_postive+adc_cal_negtive));
  473. }
  474. }else{
  475. result = (diff_mode) ? (result / 2048 -1) : (result /4096);
  476. }
  477. if(high_resol == FALSE)
  478. result = result * 4;
  479. return result;
  480. }