-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from vortigont/IDF5_core
Idf5 core
- Loading branch information
Showing
18 changed files
with
382 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
cmake_minimum_required(VERSION 3.16.0) | ||
include($ENV{IDF_PATH}/tools/cmake/project.cmake) | ||
project(ESPIron-PTS200) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# This file was automatically generated for projects | ||
# without default 'CMakeLists.txt' file. | ||
|
||
FILE(GLOB_RECURSE app_sources ${CMAKE_SOURCE_DIR}/ESPIron/*.*) | ||
|
||
idf_component_register(SRCS ${app_sources}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
// *** ADCSensor_OneShot *** | ||
|
||
ADCSensor_OneShot::~ADCSensor_OneShot(){ | ||
if (_cal_handle){ | ||
_adc_calibration_deinit(); | ||
_cal_handle = nullptr; | ||
} | ||
|
||
if (_adc_handle){ | ||
adc_oneshot_del_unit(_adc_handle); | ||
_adc_handle = nullptr; | ||
} | ||
}; | ||
|
||
esp_err_t ADCSensor_OneShot::init(int gpio, bool cal){ | ||
//SOC_GPIO_PIN_COUNT | ||
|
||
// get AD chan and unit | ||
esp_err_t err = adc_oneshot_io_to_channel(gpio, &_unit, &_chan); | ||
if (err != ESP_OK) { | ||
log_e("gpio %u is not ADC pin!", gpio); | ||
return err; | ||
} | ||
|
||
adc_oneshot_unit_init_cfg_t cfg; | ||
cfg.unit_id = _unit; | ||
adc_unit_t a; | ||
err = adc_oneshot_new_unit(&cfg, &_adc_handle); | ||
if (err != ESP_OK) | ||
return err; | ||
|
||
// use defaults | ||
// https://github.com/espressif/arduino-esp32/blob/master/docs/en/api/adc.rst | ||
adc_oneshot_chan_cfg_t config = { ADC_ATTEN_DB_12, ADC_BITWIDTH_DEFAULT }; | ||
err = adc_oneshot_config_channel(_adc_handle, _chan, &config); | ||
if (err != ESP_OK) | ||
return err; | ||
|
||
if (cal){ | ||
// set calibration | ||
err = _adc_calibration_init(); | ||
if (err == ESP_OK){ | ||
_cal_enabled = true; | ||
//ADC_LOGD(T_ADC, println, "ADC Init success"); | ||
} else { | ||
_cal_enabled = false; | ||
//ADC_LOGD(T_ADC, println, "ADC Init was not complete"); | ||
} | ||
} | ||
|
||
return err; | ||
} | ||
|
||
|
||
esp_err_t ADCSensor_OneShot::_adc_calibration_init(){ | ||
esp_err_t ret = ESP_FAIL; | ||
bool _cal_enabled = false; | ||
|
||
#if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED | ||
if (!_cal_handle) { | ||
//ESP_LOGI(TAG, "calibration scheme version is %s", "Curve Fitting"); | ||
adc_cali_curve_fitting_config_t cali_config = { | ||
.unit_id = _unit, | ||
.chan = _chan, | ||
.atten = ADC_ATTEN_DB_12, | ||
.bitwidth = ADC_BITWIDTH_DEFAULT, | ||
}; | ||
ret = adc_cali_create_scheme_curve_fitting(&cali_config, &_cal_handle); | ||
if (ret == ESP_OK) { | ||
_cal_enabled = true; | ||
} | ||
} else | ||
ret = ESP_OK; | ||
#endif | ||
|
||
#if ADC_CALI_SCHEME_LINE_FITTING_SUPPORTED | ||
if (!_cal_handle) { | ||
//ESP_LOGI(TAG, "calibration scheme version is %s", "Line Fitting"); | ||
adc_cali_line_fitting_config_t cali_config = { | ||
.unit_id = _unit, | ||
.atten = ADC_ATTEN_DB_12, | ||
.bitwidth = ADC_BITWIDTH_DEFAULT, | ||
}; | ||
ret = adc_cali_create_scheme_line_fitting(&cali_config, &_cal_handle); | ||
if (ret == ESP_OK) { | ||
_cal_enabled = true; | ||
} | ||
} else | ||
ret = ESP_OK; | ||
#endif | ||
|
||
if (ret == ESP_OK) { | ||
ADC_LOGD(T_ADC, println, "ADC Calibration enabled"); | ||
} else if (ret == ESP_ERR_NOT_SUPPORTED || !_cal_enabled) { | ||
ADC_LOGD(T_ADC, println, "eFuse not burnt, skip software calibration"); | ||
} else { | ||
ADC_LOGD(T_ADC, println, "Invalid arg or no memory"); | ||
} | ||
return ret; | ||
} | ||
|
||
void ADCSensor_OneShot::_adc_calibration_deinit(){ | ||
#if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED | ||
//ESP_LOGI(T_Sensor, "deregister %s calibration scheme", "Curve Fitting"); | ||
adc_cali_delete_scheme_curve_fitting(_cal_handle); | ||
|
||
#elif ADC_CALI_SCHEME_LINE_FITTING_SUPPORTED | ||
//ESP_LOGI(T_Sensor, "deregister %s calibration scheme", "Line Fitting"); | ||
adc_cali_delete_scheme_line_fitting(_cal_handle); | ||
#endif | ||
} | ||
|
||
int32_t ADCSensor_OneShot::read(){ | ||
// https://docs.espressif.com/projects/esp-idf/en/v5.2.2/esp32s2/api-reference/peripherals/adc_calibration.html | ||
// https://docs.espressif.com/projects/esp-idf/en/v5.2.2/esp32s2/api-reference/peripherals/adc_oneshot.html | ||
esp_err_t ret; | ||
ret = adc_oneshot_read(_adc_handle, _chan, &adc_raw); | ||
if (ret != ESP_OK) | ||
return -1; | ||
|
||
if(_cal_enabled){ | ||
ret = adc_cali_raw_to_voltage(_cal_handle, adc_raw, &adc_voltage); | ||
//ADC_LOGV(T_ADC, printf, "ADC%d Channel[%d] raw:%d Cali Voltage: %d mV, cv:%d\n", _unit, _chan, adc_raw, adc_voltage, _convertmv()); | ||
if (ret == ESP_OK) | ||
return adc_voltage; | ||
} | ||
|
||
// otherwise return uncalibrated value | ||
return _convertmv(); | ||
} | ||
|
||
int32_t ADCSensor_OneShot::readraw(){ | ||
esp_err_t ret; | ||
ret = adc_oneshot_read(_adc_handle, _chan, &adc_raw); | ||
if (ret != ESP_OK) | ||
return -1; | ||
|
||
return adc_raw; | ||
}; | ||
|
||
int32_t ADCSensor_OneShot::readmv(){ | ||
esp_err_t ret; | ||
ret = adc_oneshot_read(_adc_handle, _chan, &adc_raw); | ||
if (ret != ESP_OK) | ||
return -1; | ||
return _convertmv(); | ||
} | ||
|
||
int32_t ADCSensor_OneShot::_convertmv(){ | ||
// for ADC_ATTEN_DB_12 Vref is 2500 mV on ESP32-S2 | ||
// https://github.com/espressif/arduino-esp32/blob/master/docs/en/api/adc.rst | ||
return adc_raw * 2500 / (1 << ADC_BITWIDTH_12); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* @brief Class provides an instance of ESP32 oneshot ADC sampler with calibration | ||
* | ||
*/ | ||
class ADCSensor_OneShot { | ||
// do calibration | ||
bool _cal_enabled; | ||
|
||
//static adc_unit_t _adc_units[SOC_ADC_PERIPH_NUM]; | ||
// ADC Unit | ||
adc_unit_t _unit; | ||
// ADC channel to sample | ||
adc_channel_t _chan; | ||
static adc_oneshot_unit_handle_t _adc_handle[SOC_ADC_PERIPH_NUM]; | ||
adc_cali_handle_t _cal_handle{nullptr}; | ||
|
||
esp_err_t _adc_calibration_init(); | ||
void _adc_calibration_deinit(); | ||
|
||
int32_t _convertmv(); | ||
|
||
protected: | ||
int adc_raw, adc_voltage; | ||
|
||
public: | ||
~ADCSensor_OneShot(); | ||
|
||
// initialize ADC | ||
esp_err_t init(int gpio, bool cal = true); | ||
|
||
bool calibarationEnabled() const { return _cal_enabled; } | ||
|
||
/** | ||
* @brief returns calibrated mV reading | ||
* on read error returns -1 | ||
* on calibration error returns readmv() | ||
* | ||
* @return int32_t | ||
*/ | ||
int32_t read(); | ||
|
||
/** | ||
* @brief returns raw adc reading | ||
* on error returns -1 | ||
* | ||
* @return int32_t | ||
*/ | ||
int32_t readraw(); | ||
|
||
/** | ||
* @brief returns uncalibrated mV | ||
* on error returns -1 | ||
* | ||
* @return int32_t | ||
*/ | ||
int32_t readmv(); | ||
|
||
}; |
Oops, something went wrong.