/ components / hal / adc_hal.c
adc_hal.c
 1  // Copyright 2019 Espressif Systems (Shanghai) PTE LTD
 2  //
 3  // Licensed under the Apache License, Version 2.0 (the "License");
 4  // you may not use this file except in compliance with the License.
 5  // You may obtain a copy of the License at
 6  //
 7  //     http://www.apache.org/licenses/LICENSE-2.0
 8  //
 9  // Unless required by applicable law or agreed to in writing, software
10  // distributed under the License is distributed on an "AS IS" BASIS,
11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  // See the License for the specific language governing permissions and
13  // limitations under the License.
14  
15  #include "hal/adc_hal.h"
16  #include "hal/adc_hal_conf.h"
17  
18  void adc_hal_init(void)
19  {
20      // Set internal FSM wait time, fixed value.
21      adc_ll_digi_set_fsm_time(SOC_ADC_FSM_RSTB_WAIT_DEFAULT, SOC_ADC_FSM_START_WAIT_DEFAULT,
22                               SOC_ADC_FSM_STANDBY_WAIT_DEFAULT);
23      adc_ll_set_sample_cycle(ADC_FSM_SAMPLE_CYCLE_DEFAULT);
24      adc_hal_pwdet_set_cct(SOC_ADC_PWDET_CCT_DEFAULT);
25      adc_ll_digi_output_invert(ADC_NUM_1, SOC_ADC_DIGI_DATA_INVERT_DEFAULT(ADC_NUM_1));
26      adc_ll_digi_output_invert(ADC_NUM_2, SOC_ADC_DIGI_DATA_INVERT_DEFAULT(ADC_NUM_2));
27  }
28  
29  void adc_hal_deinit(void)
30  {
31      adc_ll_set_power_manage(ADC_POWER_SW_OFF);
32  }
33  
34  int adc_hal_convert(adc_ll_num_t adc_n, int channel, int *value)
35  {
36      adc_ll_rtc_enable_channel(adc_n, channel);
37      adc_ll_rtc_start_convert(adc_n, channel);
38      while (adc_ll_rtc_convert_is_done(adc_n) != true);
39      *value = adc_ll_rtc_get_convert_value(adc_n);
40      return (int)adc_ll_rtc_analysis_raw_data(adc_n, (uint16_t)(*value));
41  }