@@ -55,63 +55,66 @@ static rg_battery_t battery_state = {0};
5555static inline bool _adc_setup_channel (adc_unit_t unit , adc_channel_t channel , adc_atten_t atten , adc_bitwidth_t width , bool calibrate )
5656{
5757 RG_ASSERT (unit == ADC_UNIT_1 || unit == ADC_UNIT_2 , "Invalid ADC unit" );
58+ esp_err_t err = ESP_FAIL ;
5859#ifdef USE_ADC_DRIVER_NG
5960 if (!adc_handles [unit ])
6061 {
61- const adc_oneshot_unit_init_cfg_t config = {
62- .unit_id = unit ,
63- .clk_src = 0 ,
64- .ulp_mode = ADC_ULP_MODE_DISABLE ,
65- };
66- if (adc_oneshot_new_unit (& config , & adc_handles [unit ]) != ESP_OK )
67- {
68- RG_LOGE ("Failed to initialize ADC unit:%d" , (int )unit );
69- return false;
70- }
62+ adc_oneshot_unit_init_cfg_t config = {.unit_id = unit , .clk_src = 0 , .ulp_mode = ADC_ULP_MODE_DISABLE };
63+ err = adc_oneshot_new_unit (& config , & adc_handles [unit ]);
7164 }
7265 const adc_oneshot_chan_cfg_t config = {.atten = atten , .bitwidth = ADC_BITWIDTH_DEFAULT };
73- if (adc_oneshot_config_channel (adc_handles [unit ], channel , & config ) != ESP_OK )
66+ err = adc_oneshot_config_channel (adc_handles [unit ], channel , & config );
67+ #else
68+ if (RG_BATTERY_ADC_UNIT == ADC_UNIT_1 )
69+ {
70+ adc1_config_width (ADC_WIDTH_MAX - 1 );
71+ err = adc1_config_channel_atten (channel , atten );
72+ }
73+ else if (RG_BATTERY_ADC_UNIT == ADC_UNIT_2 )
74+ {
75+ err = adc2_config_channel_atten (channel , atten );
76+ }
77+ #endif
78+ if (err != ESP_OK )
7479 {
75- RG_LOGE ("Failed to configure ADC unit: %d channel:%d atten:%d width:%d" ,
76- (int )unit , (int )channel , (int )atten , (int )width );
80+ RG_LOGE ("Failed to configure ADC_UNIT_ %d channel:%d atten:%d width:%d error:0x%02X " ,
81+ (int )unit , (int )channel , (int )atten , (int )width , ( int ) err );
7782 return false;
7883 }
84+
7985 if (calibrate )
8086 {
87+ #ifdef USE_ADC_DRIVER_NG
8188 #if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED
82- adc_cali_curve_fitting_config_t config = {
89+ const adc_cali_curve_fitting_config_t config = {
8390 .unit_id = unit ,
8491 .atten = atten ,
8592 .bitwidth = width ,
8693 };
87- adc_cali_create_scheme_curve_fitting (& config , & adc_cali_handles [unit ]);
94+ err = adc_cali_create_scheme_curve_fitting (& config , & adc_cali_handles [unit ]);
8895 #elif ADC_CALI_SCHEME_LINE_FITTING_SUPPORTED
89- adc_cali_line_fitting_config_t config = {
96+ const adc_cali_line_fitting_config_t config = {
9097 .unit_id = unit ,
9198 .atten = atten ,
9299 .bitwidth = width ,
93100 #if CONFIG_IDF_TARGET_ESP32
94101 .default_vref = 1100 ,
95102 #endif
96103 };
97- adc_cali_create_scheme_line_fitting (& config , & adc_cali_handles [unit ]);
104+ err = adc_cali_create_scheme_line_fitting (& config , & adc_cali_handles [unit ]);
98105 #else
99106 RG_LOGW ("Calibration not supported!" );
107+ err = ESP_ERR_NOT_SUPPORTED ;
100108 #endif
101- }
102109#else
103- if (RG_BATTERY_ADC_UNIT == ADC_UNIT_1 )
104- {
105- adc1_config_width (ADC_WIDTH_MAX - 1 );
106- adc1_config_channel_atten (channel , atten );
107- }
108- else if (RG_BATTERY_ADC_UNIT == ADC_UNIT_2 )
109- {
110- adc2_config_channel_atten (channel , atten );
111- }
112- if (calibrate )
113- esp_adc_cal_characterize (unit , atten , ADC_WIDTH_MAX - 1 , 1100 , & adc_chars );
110+ err = esp_adc_cal_characterize (unit , atten , ADC_WIDTH_MAX - 1 , 1100 , & adc_chars );
114111#endif
112+ if (err != ESP_OK )
113+ {
114+ RG_LOGW ("Failed to calibrate ADC_UNIT_%d atten:%d width:%d error:0x%02X" ,
115+ (int )unit , (int )atten , (int )width , (int )err );
116+ }
117+ }
115118 return true;
116119}
117120
0 commit comments