|
1 |
| -#include "esp_adc/adc_oneshot.h" |
2 | 1 | #include "esp_adc/adc_cali.h"
|
3 | 2 | #include "esp_adc/adc_cali_scheme.h"
|
| 3 | +#include "esp_adc/adc_oneshot.h" |
4 | 4 | #include "esp_log.h"
|
5 | 5 |
|
6 | 6 | #define BATT_VOLTAGE_ADC_CHANNEL ADC_CHANNEL_3
|
|
9 | 9 | #define ADC_UNIT ADC_UNIT_1
|
10 | 10 | #define ADC_VREF 1100
|
11 | 11 |
|
12 |
| -// Calibrate the battery voltage measurement based on the resistor voltage divider |
| 12 | +// Calibrate the battery voltage measurement |
| 13 | +// based on the resistor voltage divider |
13 | 14 | #define VDIV_RATIO 5.02
|
14 | 15 |
|
15 | 16 | static const char *TAG = "Battery";
|
16 | 17 |
|
17 | 18 | adc_oneshot_unit_handle_t batt_voltage_adc_handle;
|
18 | 19 | adc_cali_handle_t batt_voltage_adc_cali_handle = NULL;
|
19 | 20 |
|
20 |
| -static bool adc_calibration_init(adc_unit_t unit, adc_channel_t channel, adc_atten_t atten, adc_bitwidth_t bitwidth, adc_cali_handle_t *out_handle) |
| 21 | +static bool adc_calibration_init(adc_unit_t unit, adc_channel_t channel, adc_atten_t atten, adc_bitwidth_t bitwidth, |
| 22 | + adc_cali_handle_t *out_handle) |
21 | 23 | {
|
22 |
| - adc_cali_handle_t handle = NULL; |
23 |
| - esp_err_t ret = ESP_FAIL; |
24 |
| - bool calibrated = false; |
25 |
| - |
26 |
| - if (!calibrated) { |
27 |
| - #if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED |
28 |
| - adc_cali_curve_fitting_config_t cali_config = { |
29 |
| - .unit_id = unit, |
30 |
| - .chan = channel, |
31 |
| - .atten = atten, |
32 |
| - .bitwidth = bitwidth, |
33 |
| - }; |
34 |
| - ret = adc_cali_create_scheme_curve_fitting(&cali_config, &handle); |
35 |
| - |
36 |
| - |
37 |
| - #elif ADC_CALI_SCHEME_LINE_FITTING_SUPPORTED |
38 |
| - adc_cali_line_fitting_config_t cali_config = { |
39 |
| - .unit_id = unit, |
40 |
| - .atten = atten, |
41 |
| - .bitwidth = bitwidth, |
42 |
| - }; |
43 |
| - ret = adc_cali_create_scheme_line_fitting(&cali_config, &handle); |
44 |
| - #endif |
| 24 | + adc_cali_handle_t handle = NULL; |
| 25 | + esp_err_t ret = ESP_FAIL; |
| 26 | + bool calibrated = false; |
| 27 | + |
| 28 | + if (!calibrated) { |
| 29 | +#if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED |
| 30 | + adc_cali_curve_fitting_config_t cali_config = { |
| 31 | + .unit_id = unit, |
| 32 | + .chan = channel, |
| 33 | + .atten = atten, |
| 34 | + .bitwidth = bitwidth, |
| 35 | + }; |
| 36 | + ret = adc_cali_create_scheme_curve_fitting(&cali_config, &handle); |
| 37 | + |
| 38 | +#elif ADC_CALI_SCHEME_LINE_FITTING_SUPPORTED |
| 39 | + adc_cali_line_fitting_config_t cali_config = { |
| 40 | + .unit_id = unit, |
| 41 | + .atten = atten, |
| 42 | + .bitwidth = bitwidth, |
| 43 | + }; |
| 44 | + ret = adc_cali_create_scheme_line_fitting(&cali_config, &handle); |
| 45 | +#endif |
| 46 | + |
| 47 | + if (ret == ESP_OK) { |
| 48 | + calibrated = true; |
| 49 | + } |
| 50 | + } |
45 | 51 |
|
| 52 | + *out_handle = handle; |
46 | 53 | if (ret == ESP_OK) {
|
47 |
| - calibrated = true; |
| 54 | + ESP_LOGI(TAG, "Calibration Success"); |
| 55 | + } else if (ret == ESP_ERR_NOT_SUPPORTED || !calibrated) { |
| 56 | + ESP_LOGW(TAG, "eFuse not burnt, skip software calibration"); |
| 57 | + } else { |
| 58 | + ESP_LOGE(TAG, "Invalid arg or no memory"); |
48 | 59 | }
|
49 |
| - } |
50 |
| - |
51 |
| - *out_handle = handle; |
52 |
| - if (ret == ESP_OK) { |
53 |
| - ESP_LOGI(TAG, "Calibration Success"); |
54 |
| - } else if (ret == ESP_ERR_NOT_SUPPORTED || !calibrated) { |
55 |
| - ESP_LOGW(TAG, "eFuse not burnt, skip software calibration"); |
56 |
| - } else { |
57 |
| - ESP_LOGE(TAG, "Invalid arg or no memory"); |
58 |
| - } |
59 |
| - |
60 |
| - return calibrated; |
| 60 | + |
| 61 | + return calibrated; |
61 | 62 | }
|
62 | 63 |
|
63 |
| -// Configures the battery ADC |
| 64 | +// Configures the battery ADC |
64 | 65 | static void config_batt_adc()
|
65 | 66 | {
|
66 |
| - ESP_LOGI(TAG, "Configuring ADC characteristics"); |
67 |
| - |
68 |
| - // ADC1 Init |
69 |
| - adc_oneshot_unit_init_cfg_t init_config1 = { |
70 |
| - .unit_id = ADC_UNIT, |
71 |
| - .ulp_mode = ADC_ULP_MODE_DISABLE, |
72 |
| - }; |
73 |
| - ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config1, &batt_voltage_adc_handle)); |
74 |
| - |
75 |
| - // ADC1 Config |
76 |
| - adc_oneshot_chan_cfg_t config = { |
77 |
| - .bitwidth = BATT_VOLTAGE_ADC_BITWIDTH, |
78 |
| - .atten = BATT_VOLTAGE_ADC_ATTEN, |
79 |
| - }; |
80 |
| - ESP_ERROR_CHECK(adc_oneshot_config_channel(batt_voltage_adc_handle, BATT_VOLTAGE_ADC_CHANNEL, &config)); |
| 67 | + ESP_LOGI(TAG, "Configuring ADC characteristics"); |
| 68 | + |
| 69 | + // ADC1 Init |
| 70 | + adc_oneshot_unit_init_cfg_t init_config1 = { |
| 71 | + .unit_id = ADC_UNIT, |
| 72 | + .ulp_mode = ADC_ULP_MODE_DISABLE, |
| 73 | + }; |
| 74 | + ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config1, &batt_voltage_adc_handle)); |
| 75 | + |
| 76 | + // ADC1 Config |
| 77 | + adc_oneshot_chan_cfg_t config = { |
| 78 | + .bitwidth = BATT_VOLTAGE_ADC_BITWIDTH, |
| 79 | + .atten = BATT_VOLTAGE_ADC_ATTEN, |
| 80 | + }; |
| 81 | + ESP_ERROR_CHECK(adc_oneshot_config_channel(batt_voltage_adc_handle, BATT_VOLTAGE_ADC_CHANNEL, &config)); |
81 | 82 | }
|
82 | 83 |
|
83 | 84 | // Deinitialize calibration of an ADC handle
|
84 | 85 | static void adc_calibration_deinit(adc_cali_handle_t handle)
|
85 | 86 | {
|
86 |
| - #if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED |
87 |
| - ESP_ERROR_CHECK(adc_cali_delete_scheme_curve_fitting(handle)); |
| 87 | +#if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED |
| 88 | + ESP_ERROR_CHECK(adc_cali_delete_scheme_curve_fitting(handle)); |
88 | 89 |
|
89 |
| - #elif ADC_CALI_SCHEME_LINE_FITTING_SUPPORTED |
90 |
| - ESP_ERROR_CHECK(adc_cali_delete_scheme_line_fitting(handle)); |
91 |
| - #endif |
| 90 | +#elif ADC_CALI_SCHEME_LINE_FITTING_SUPPORTED |
| 91 | + ESP_ERROR_CHECK(adc_cali_delete_scheme_line_fitting(handle)); |
| 92 | +#endif |
92 | 93 | }
|
93 | 94 |
|
94 |
| -// Read ADC voltage from battery and return battery voltage calculated from the ADC and voltage divider |
| 95 | +// Read ADC voltage from battery and return battery |
| 96 | +// voltage calculated from the ADC and voltage divider |
95 | 97 | float get_battery_voltage()
|
96 | 98 | {
|
97 |
| - int adc_raw[1][10]; |
98 |
| - int voltage[1][10]; |
99 |
| - |
100 |
| - config_batt_adc(); |
101 |
| - |
102 |
| - // ADC1 Read |
103 |
| - ESP_ERROR_CHECK(adc_oneshot_read(batt_voltage_adc_handle, BATT_VOLTAGE_ADC_CHANNEL, &adc_raw[0][0])); |
104 |
| - ESP_LOGI(TAG, "ADC%d Channel[%d] Raw Data: %d", ADC_UNIT + 1, BATT_VOLTAGE_ADC_CHANNEL, adc_raw[0][0]); |
105 |
| - |
106 |
| - // ADC1 Calibration |
107 |
| - bool do_calibration_batt_voltage = adc_calibration_init(ADC_UNIT, BATT_VOLTAGE_ADC_CHANNEL, BATT_VOLTAGE_ADC_ATTEN, BATT_VOLTAGE_ADC_BITWIDTH, &batt_voltage_adc_cali_handle); |
108 |
| - |
109 |
| - if (do_calibration_batt_voltage) { |
110 |
| - ESP_ERROR_CHECK(adc_cali_raw_to_voltage(batt_voltage_adc_cali_handle, adc_raw[0][0], &voltage[0][0])); |
111 |
| - ESP_LOGI(TAG, "ADC%d Channel[%d] Cali Voltage: %d mV", ADC_UNIT + 1, BATT_VOLTAGE_ADC_CHANNEL, voltage[0][0]); |
112 |
| - } |
113 |
| - |
114 |
| - // ADC1 Teardown |
115 |
| - ESP_ERROR_CHECK(adc_oneshot_del_unit(batt_voltage_adc_handle)); |
116 |
| - if(do_calibration_batt_voltage) |
117 |
| - adc_calibration_deinit(batt_voltage_adc_cali_handle); |
118 |
| - |
119 |
| - return (voltage[0][0] / 1000.f) * VDIV_RATIO; |
| 99 | + int adc_raw[1][10]; |
| 100 | + int voltage[1][10]; |
| 101 | + |
| 102 | + config_batt_adc(); |
| 103 | + |
| 104 | + // ADC1 Read |
| 105 | + ESP_ERROR_CHECK(adc_oneshot_read(batt_voltage_adc_handle, BATT_VOLTAGE_ADC_CHANNEL, &adc_raw[0][0])); |
| 106 | + ESP_LOGI(TAG, "ADC%d Channel[%d] Raw Data: %d", ADC_UNIT + 1, BATT_VOLTAGE_ADC_CHANNEL, adc_raw[0][0]); |
| 107 | + |
| 108 | + // ADC1 Calibration |
| 109 | + bool do_calibration_batt_voltage = |
| 110 | + adc_calibration_init(ADC_UNIT, BATT_VOLTAGE_ADC_CHANNEL, BATT_VOLTAGE_ADC_ATTEN, BATT_VOLTAGE_ADC_BITWIDTH, |
| 111 | + &batt_voltage_adc_cali_handle); |
| 112 | + |
| 113 | + if (do_calibration_batt_voltage) { |
| 114 | + ESP_ERROR_CHECK(adc_cali_raw_to_voltage(batt_voltage_adc_cali_handle, adc_raw[0][0], &voltage[0][0])); |
| 115 | + ESP_LOGI(TAG, "ADC%d Channel[%d] Cali Voltage: %d mV", ADC_UNIT + 1, BATT_VOLTAGE_ADC_CHANNEL, |
| 116 | + voltage[0][0]); |
| 117 | + } |
| 118 | + |
| 119 | + // ADC1 Teardown |
| 120 | + ESP_ERROR_CHECK(adc_oneshot_del_unit(batt_voltage_adc_handle)); |
| 121 | + if (do_calibration_batt_voltage) |
| 122 | + adc_calibration_deinit(batt_voltage_adc_cali_handle); |
| 123 | + |
| 124 | + return (voltage[0][0] / 1000.f) * VDIV_RATIO; |
120 | 125 | }
|
0 commit comments