Skip to content

Commit f0c89f4

Browse files
committed
Add CPPANALOGIO driver interface class
* taken from https://github.com/roughleaf/ESP-IDF-CPP-Components
1 parent 4bbc035 commit f0c89f4

File tree

4 files changed

+317
-0
lines changed

4 files changed

+317
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
set(SOURCES src/CPPANALOG/cppadc.cpp
2+
src/CPPANALOG/cppdac.cpp)
3+
4+
idf_component_register(SRCS ${SOURCES}
5+
INCLUDE_DIRS . include
6+
REQUIRES "esp_adc_cal")
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#pragma once
2+
3+
#include "driver/adc.h"
4+
#include "driver/dac.h"
5+
#include "esp_adc_cal.h"
6+
7+
// ADC Calibration
8+
#if CONFIG_IDF_TARGET_ESP32
9+
constexpr static const esp_adc_cal_value_t ADC_CALI_SCHEME = ESP_ADC_CAL_VAL_EFUSE_VREF;
10+
#elif CONFIG_IDF_TARGET_ESP32S2
11+
constexpr static const esp_adc_cal_value_t ADC_CALI_SCHEME = ESP_ADC_CAL_VAL_EFUSE_TP;
12+
#elif CONFIG_IDF_TARGET_ESP32C3
13+
constexpr static const esp_adc_cal_value_t ADC_CALI_SCHEME = ESP_ADC_CAL_VAL_EFUSE_TP;
14+
#elif CONFIG_IDF_TARGET_ESP32S3
15+
constexpr static const esp_adc_cal_value_t ADC_CALI_SCHEME = ESP_ADC_CAL_VAL_EFUSE_TP_FIT;
16+
#endif
17+
18+
namespace CPPANALOG
19+
{
20+
class CppAdc1
21+
{
22+
private:
23+
adc_bits_width_t _bitWidth = ADC_WIDTH_BIT_12;
24+
adc1_channel_t _adc1Channel;
25+
adc_atten_t _adcAttenuation = ADC_ATTEN_DB_11;
26+
bool _calEnabled = false;
27+
esp_adc_cal_characteristics_t _adc1_characteristics;
28+
bool _checkCalFuse();
29+
30+
public:
31+
CppAdc1(void);
32+
CppAdc1(adc1_channel_t channel);
33+
void SetChannel(adc1_channel_t channel);
34+
esp_err_t SetBitWidth(adc_bits_width_t bitWidth);
35+
esp_err_t SetBitWidth(int bitWidth);
36+
esp_err_t SetAttenuation(adc_atten_t attenuation);
37+
bool CheckCalFuse();
38+
int GetRaw();
39+
int GetVoltage();
40+
}; // CppAdc Class
41+
42+
// DAC channel 1 is GPIO25(ESP32) / GPIO17(ESP32S2)
43+
// DAC channel 2 is GPIO26(ESP32) / GPIO18(ESP32S2)
44+
class CppDac
45+
{
46+
private:
47+
dac_channel_t _channel;
48+
dac_cw_config_t _cosine_wave_config;
49+
esp_err_t _init(dac_channel_t channel);
50+
public:
51+
CppDac(void);
52+
CppDac(dac_channel_t channel);
53+
esp_err_t SetChannel(dac_channel_t channel);
54+
esp_err_t SetVoltageByValue(uint8_t level);
55+
esp_err_t SetVoltage(float voltage);
56+
esp_err_t DacEnable(void);
57+
esp_err_t DacDisable(void);
58+
esp_err_t DacCsEnable(void);
59+
esp_err_t DaCsDisable(void);
60+
esp_err_t SetCsScale(dac_cw_scale_t scale);
61+
esp_err_t SetCsPhase(dac_cw_phase_t phase);
62+
esp_err_t SetCsFrequency(uint32_t freq);
63+
esp_err_t SetCsOffset(int8_t offset);
64+
esp_err_t SetCsConfig(dac_cw_scale_t scale, dac_cw_phase_t phase, uint32_t freq, int8_t offset);
65+
}; // CppDac Class
66+
} // CPPANALOG namespace
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
#include "CPPANALOG/analogio.h"
2+
3+
namespace CPPANALOG
4+
{
5+
bool CppAdc1::_checkCalFuse()
6+
{
7+
esp_err_t status {ESP_OK};
8+
bool cali_enable = false;
9+
10+
status = esp_adc_cal_check_efuse(ADC_CALI_SCHEME);
11+
if (ESP_OK == status)
12+
{
13+
cali_enable = true;
14+
esp_adc_cal_characterize(ADC_UNIT_1, _adcAttenuation, _bitWidth, 0, &_adc1_characteristics);
15+
}
16+
return cali_enable;
17+
}
18+
19+
CppAdc1::CppAdc1(void)
20+
{
21+
}
22+
23+
CppAdc1::CppAdc1(adc1_channel_t channel)
24+
{
25+
SetChannel(channel);
26+
}
27+
28+
void CppAdc1::SetChannel(adc1_channel_t channel)
29+
{
30+
_adc1Channel = channel;
31+
adc1_config_width(_bitWidth);
32+
adc1_config_channel_atten(_adc1Channel, _adcAttenuation);
33+
34+
_calEnabled = _checkCalFuse();
35+
}
36+
37+
bool CppAdc1::CheckCalFuse()
38+
{
39+
return _checkCalFuse();
40+
}
41+
42+
int CppAdc1::GetRaw()
43+
{
44+
return adc1_get_raw(_adc1Channel);
45+
}
46+
47+
int CppAdc1::GetVoltage()
48+
{
49+
if(_calEnabled)
50+
{
51+
return esp_adc_cal_raw_to_voltage(adc1_get_raw(_adc1Channel), &_adc1_characteristics);
52+
}
53+
else
54+
{
55+
return -1;
56+
}
57+
}
58+
59+
esp_err_t CppAdc1::SetBitWidth(adc_bits_width_t bitWidth)
60+
{
61+
_bitWidth = bitWidth;
62+
_calEnabled = _checkCalFuse();
63+
return adc1_config_width(_bitWidth);
64+
}
65+
66+
esp_err_t CppAdc1::SetBitWidth(int bitWidth)
67+
{
68+
switch (bitWidth)
69+
{
70+
case 9:
71+
_bitWidth = ADC_WIDTH_BIT_9;
72+
break;
73+
case 10:
74+
_bitWidth = ADC_WIDTH_BIT_10;
75+
break;
76+
case 11:
77+
_bitWidth = ADC_WIDTH_BIT_11;
78+
break;
79+
case 12:
80+
_bitWidth = ADC_WIDTH_BIT_12;
81+
break;
82+
default:
83+
_bitWidth = ADC_WIDTH_BIT_12;
84+
break;
85+
}
86+
_calEnabled = _checkCalFuse();
87+
return adc1_config_width(_bitWidth);
88+
}
89+
90+
esp_err_t CppAdc1::SetAttenuation(adc_atten_t attenuation)
91+
{
92+
_adcAttenuation = attenuation;
93+
_calEnabled = _checkCalFuse();
94+
return adc1_config_channel_atten(_adc1Channel, _adcAttenuation);
95+
}
96+
97+
} // CPPANALOG namespace
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
#include "CPPANALOG/analogio.h"
2+
3+
namespace CPPANALOG
4+
{
5+
esp_err_t CppDac::_init(dac_channel_t channel)
6+
{
7+
_channel = channel;
8+
9+
// Set default values for cosine wave generator
10+
_cosine_wave_config.en_ch = channel;
11+
_cosine_wave_config.scale = DAC_CW_SCALE_1;
12+
_cosine_wave_config.phase = DAC_CW_PHASE_0;
13+
_cosine_wave_config.freq = 1000;
14+
_cosine_wave_config.offset = 0;
15+
16+
return dac_cw_generator_config(&_cosine_wave_config);
17+
}
18+
19+
CppDac::CppDac(void)
20+
{
21+
}
22+
23+
CppDac::CppDac(dac_channel_t channel)
24+
{
25+
_init(channel);
26+
}
27+
28+
esp_err_t CppDac::SetChannel(dac_channel_t channel)
29+
{
30+
_init(channel);
31+
32+
return ESP_OK;
33+
}
34+
35+
esp_err_t CppDac::SetVoltageByValue(uint8_t level)
36+
{
37+
esp_err_t status = ESP_OK;
38+
39+
status |= DaCsDisable();
40+
status |= DacEnable();
41+
status |= dac_output_voltage(_channel, level);
42+
43+
return status;
44+
}
45+
46+
esp_err_t CppDac::SetVoltage(float voltage)
47+
{
48+
49+
esp_err_t status = ESP_OK;
50+
uint8_t level = 0;
51+
52+
if (voltage < 0)
53+
{
54+
voltage = 0;
55+
}
56+
if (voltage > 3.3)
57+
{
58+
voltage = 3.3;
59+
}
60+
61+
level = voltage * 77.27; // 255/3.3 = 77.27
62+
63+
status |= DaCsDisable();
64+
status |= DacEnable();
65+
status |= dac_output_voltage(_channel, level);
66+
67+
return status;
68+
}
69+
70+
esp_err_t CppDac::DacEnable(void)
71+
{
72+
return dac_output_enable(_channel);
73+
}
74+
75+
esp_err_t CppDac::DacDisable(void)
76+
{
77+
return dac_output_disable(_channel);
78+
}
79+
80+
esp_err_t CppDac::DacCsEnable(void)
81+
{
82+
esp_err_t status = ESP_OK;
83+
// status |= DacDisable();
84+
status |= dac_cw_generator_enable();
85+
86+
return status;
87+
}
88+
89+
esp_err_t CppDac::DaCsDisable(void)
90+
{
91+
return dac_cw_generator_disable();
92+
}
93+
94+
esp_err_t CppDac::SetCsScale(dac_cw_scale_t scale)
95+
{
96+
_cosine_wave_config.scale = scale;
97+
98+
return dac_cw_generator_config(&_cosine_wave_config);
99+
}
100+
101+
esp_err_t CppDac::SetCsPhase(dac_cw_phase_t phase)
102+
{
103+
_cosine_wave_config.phase = phase;
104+
105+
return dac_cw_generator_config(&_cosine_wave_config);
106+
}
107+
108+
esp_err_t CppDac::SetCsFrequency(uint32_t freq)
109+
{
110+
if(freq < 130)
111+
{
112+
freq = 130;
113+
}
114+
if (freq > 55000)
115+
{
116+
freq = 55000;
117+
}
118+
_cosine_wave_config.freq = freq;
119+
120+
return dac_cw_generator_config(&_cosine_wave_config);
121+
}
122+
123+
esp_err_t CppDac::SetCsOffset(int8_t offset)
124+
{
125+
_cosine_wave_config.offset = offset;
126+
127+
return dac_cw_generator_config(&_cosine_wave_config);
128+
}
129+
130+
esp_err_t CppDac::SetCsConfig(dac_cw_scale_t scale, dac_cw_phase_t phase, uint32_t freq, int8_t offset)
131+
{
132+
if(freq < 130)
133+
{
134+
freq = 130;
135+
}
136+
if (freq > 55000)
137+
{
138+
freq = 55000;
139+
}
140+
141+
_cosine_wave_config.scale = scale;
142+
_cosine_wave_config.phase = phase;
143+
_cosine_wave_config.freq = freq;
144+
_cosine_wave_config.offset = offset;
145+
146+
return dac_cw_generator_config(&_cosine_wave_config);
147+
}
148+
} // CPPANALOG namespace

0 commit comments

Comments
 (0)