Skip to content

Commit

Permalink
Merge pull request #11 from vortigont/IDF5_core
Browse files Browse the repository at this point in the history
Idf5 core
  • Loading branch information
vortigont authored Aug 25, 2024
2 parents 6221285 + 7e8d341 commit 31c3804
Show file tree
Hide file tree
Showing 18 changed files with 382 additions and 74 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
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)
6 changes: 6 additions & 0 deletions ESPIron/CMakeLists.txt
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})
16 changes: 8 additions & 8 deletions ESPIron/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
#define SCREEN_OFFSET 2

// Pins
#define SENSOR_PIN 1 // tip temperature sense 烙铁头温感
#define VIN_PIN 6 // input voltage sense 检测输入电压
#define BUZZER_PIN 3 // buzzer 蜂鸣器
#define BUTTON_ACTION GPIO_NUM_0 // middle push-button
#define BUTTON_INCR GPIO_NUM_2 // incrementer “+” push-button
#define BUTTON_DECR GPIO_NUM_4 // decrementer “-” push-button
#define HEATER_PIN GPIO_NUM_5 // heater MOSFET PWM control 加热器MOSFET PWM控制
#define SH1107_RST_PIN 7 // display reset pin
#define TIP_ADC_SENSOR_PIN 1 // tip temperature sense 烙铁头温感
#define VIN_PIN 6 // input voltage sense 检测输入电压
#define BUZZER_PIN 3 // buzzer 蜂鸣器
#define BUTTON_ACTION GPIO_NUM_0 // middle push-button
#define BUTTON_INCR GPIO_NUM_2 // incrementer “+” push-button
#define BUTTON_DECR GPIO_NUM_4 // decrementer “-” push-button
#define HEATER_PIN GPIO_NUM_5 // heater MOSFET PWM control 加热器MOSFET PWM控制
#define SH1107_RST_PIN 7 // display reset pin

// CH224K USB PD chip pins connection
// https://components101.com/sites/default/files/component_datasheet/WCH_CH224K_ENG.pdf
Expand Down
154 changes: 154 additions & 0 deletions ESPIron/dusputed/adc_idf.cpp_
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);
}

58 changes: 58 additions & 0 deletions ESPIron/dusputed/adc_idf.hpp
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();

};
Loading

0 comments on commit 31c3804

Please sign in to comment.