-
Notifications
You must be signed in to change notification settings - Fork 0
/
adc.cpp
177 lines (159 loc) · 4.49 KB
/
adc.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "adc.h"
//#include "esp_adc_cal.h"
#include "system.h"
using namespace libesp;
const char *ADC::LOGTAG = "ADC";
ADC::ADC() : ADCChannel1(ADC1_CHANNEL_MAX), ADCChannel2(ADC2_CHANNEL_MAX), ADCWidth(ADC_WIDTH_MAX), ADCAtten(ADC_ATTEN_MAX)
, Samples(1), VRef(1100), ADCCalCharacteristics(), VRefPin(NOPIN) {
}
adc1_channel_t ADC::convertGPIOToADC1(gpio_num_t pin) {
switch(pin) {
case 36:
return ADC1_CHANNEL_0;
case 37:
return ADC1_CHANNEL_1;
case 38:
return ADC1_CHANNEL_2;
case 39:
return ADC1_CHANNEL_3;
case 32:
return ADC1_CHANNEL_4;
case 33:
return ADC1_CHANNEL_5;
case 34:
return ADC1_CHANNEL_6;
case 35:
return ADC1_CHANNEL_7;
default:
return ADC1_CHANNEL_MAX;
}
}
adc2_channel_t ADC::convertGPIOToADC2(gpio_num_t pin) {
switch(pin) {
case 4:
return ADC2_CHANNEL_0;
case 0:
return ADC2_CHANNEL_1;
case 2:
return ADC2_CHANNEL_2;
case 15:
return ADC2_CHANNEL_3;
case 13:
return ADC2_CHANNEL_4;
case 12:
return ADC2_CHANNEL_5;
case 14:
return ADC2_CHANNEL_6;
case 27:
return ADC2_CHANNEL_7;
case 25:
return ADC2_CHANNEL_8;
case 26:
return ADC2_CHANNEL_9;
default:
return ADC2_CHANNEL_MAX;
}
}
ErrorType ADC::init(const adc1_channel_t &adc1_chan, const adc2_channel_t &adc2_chan, const adc_bits_width_t &adc_width, const adc_atten_t &atten,uint16_t samples) {
return init(adc1_chan, adc2_chan, adc_width, atten, samples, NOPIN);
}
adc_unit_t ADC::getUnit() {
if(ADCChannel1 != ADC1_CHANNEL_MAX)
return ADC_UNIT_1;
return ADC_UNIT_2;
}
ErrorType ADC::init(const adc1_channel_t &adc1_chan, const adc2_channel_t &adc2_chan, const adc_bits_width_t &adc_width, const adc_atten_t &atten , uint16_t samples, gpio_num_t refPin) {
ErrorType et;
ADCChannel1 = adc1_chan;
ADCChannel2 = adc2_chan;
ADCWidth = adc_width;
ADCAtten = atten;
Samples = samples;
VRefPin = refPin;
//only 1
if(ADCChannel1 == ADC1_CHANNEL_MAX || ADCChannel2 == ADC2_CHANNEL_MAX) {
if(VRefPin!=NOPIN) {
et = adc_vref_to_gpio(ADC_UNIT_2,VRefPin);
if(et.ok()) {
uint32_t reading = 0;
adc2_channel_t vrefADC = convertGPIOToADC2(VRefPin);
for(int i=0;i<Samples;++i) {
int raw;
adc2_get_raw(vrefADC, ADCWidth, &raw);
reading+=raw;
}
VRef = reading/Samples;
}
} //else check to see if value is in the fuse and update VRef
//TODO
if(et.ok()) {
printEFuseData();
printCharacteristics();
}
} else {
et = ErrorType::INVALID_CONFIG;
}
return et;
}
uint32_t ADC::getRawSamples() {
uint32_t adc_reading = 0;
for (int i = 0; i < Samples; i++) {
if (getUnit() == ADC_UNIT_1) {
adc_reading += adc1_get_raw(ADCChannel1);
} else {
int raw;
adc2_get_raw(ADCChannel2, ADCWidth, &raw);
adc_reading += raw;
}
}
return adc_reading;
}
void ADC::printEFuseData() {
if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_TP) == ESP_OK) {
printf("eFuse Two Point: Supported\n");
} else {
printf("eFuse Two Point: NOT supported\n");
}
//Check Vref is burned into eFuse
if (esp_adc_cal_check_efuse(ESP_ADC_CAL_VAL_EFUSE_VREF) == ESP_OK) {
printf("eFuse Vref: Supported\n");
} else {
printf("eFuse Vref: NOT supported\n");
}
}
void ADC::printCharacteristics() {
esp_adc_cal_value_t val_type = esp_adc_cal_characterize(getUnit(), ADCAtten, ADCWidth, VRef, &ADCCalCharacteristics);
if (val_type == ESP_ADC_CAL_VAL_EFUSE_TP) {
ESP_LOGI(LOGTAG, "Characterized using Two Point Value\n");
} else if (val_type == ESP_ADC_CAL_VAL_EFUSE_VREF) {
ESP_LOGI(LOGTAG, "Characterized using eFuse Vref\n");
} else {
ESP_LOGI(LOGTAG,"Characterized using Default Vref\n");
}
}
ErrorType ADC::acquireData(Result &v) {
ErrorType et;
uint32_t adc_reading = 0;
//Multisampling
for (int i = 0; i < Samples; i++) {
if (getUnit() == ADC_UNIT_1) {
adc_reading += adc1_get_raw(ADCChannel1);
} else {
int raw;
adc2_get_raw(ADCChannel2, ADCWidth, &raw);
adc_reading += raw;
}
}
adc_reading /= Samples;
//Convert adc_reading to voltage in mV
uint32_t voltage = esp_adc_cal_raw_to_voltage(adc_reading, &ADCCalCharacteristics);
printf("Raw: %d\tVoltage: %dmV\n", adc_reading, voltage);
v.RawAvg = adc_reading;
v.CalculatedVoltage = voltage;
return et;
}
void ADC::shutdown() {
}
ADC::~ADC() {
shutdown();
}