188 lines
5.7 KiB
C
188 lines
5.7 KiB
C
/*
|
|
* adc_ifc.c
|
|
*
|
|
* Created on: Jan 17, 2023
|
|
* Author: Partha
|
|
*/
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <stdlib.h>
|
|
#include <inttypes.h>
|
|
#include "esp_log.h"
|
|
//#include "driver/adc.h"
|
|
#include "esp_adc/adc_oneshot.h"
|
|
#include "esp_adc/adc_cali.h"
|
|
#include "esp_adc/adc_cali_scheme.h"
|
|
//#include "esp_adc_cal.h"
|
|
#include "data_processing.h"
|
|
#include "hmi.h"
|
|
#include "ulp_main.h"
|
|
|
|
static const char* TAG = "ADC_IF";
|
|
|
|
#define LOG_LOCAL_LEVEL ESP_LOG_INFO
|
|
|
|
//static esp_adc_cal_characteristics_t adc1_chars;
|
|
RTC_DATA_ATTR uint32_t light_data3;
|
|
RTC_DATA_ATTR uint32_t prev_light_data3;
|
|
|
|
adc_oneshot_unit_handle_t adc1_handle;
|
|
adc_cali_handle_t adc1_cali_handle = NULL;
|
|
static int adc_raw[2];
|
|
static int voltage[2];
|
|
|
|
static bool adc_calibration_init(adc_unit_t unit, adc_atten_t atten, adc_cali_handle_t *out_handle)
|
|
{
|
|
adc_cali_handle_t handle = NULL;
|
|
esp_err_t ret = ESP_FAIL;
|
|
bool calibrated = false;
|
|
|
|
#if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED
|
|
if (!calibrated) {
|
|
ESP_LOGI(TAG, "calibration scheme version is %s", "Curve Fitting");
|
|
adc_cali_curve_fitting_config_t cali_config = {
|
|
.unit_id = unit,
|
|
.atten = atten,
|
|
.bitwidth = ADC_BITWIDTH_DEFAULT,
|
|
};
|
|
ret = adc_cali_create_scheme_curve_fitting(&cali_config, &handle);
|
|
if (ret == ESP_OK) {
|
|
calibrated = true;
|
|
}
|
|
}
|
|
#endif
|
|
|
|
#if ADC_CALI_SCHEME_LINE_FITTING_SUPPORTED
|
|
if (!calibrated) {
|
|
ESP_LOGI(TAG, "calibration scheme version is %s", "Line Fitting");
|
|
adc_cali_line_fitting_config_t cali_config = {
|
|
.unit_id = unit,
|
|
.atten = atten,
|
|
.bitwidth = ADC_BITWIDTH_DEFAULT,
|
|
};
|
|
ret = adc_cali_create_scheme_line_fitting(&cali_config, &handle);
|
|
if (ret == ESP_OK) {
|
|
calibrated = true;
|
|
}
|
|
}
|
|
#endif
|
|
|
|
*out_handle = handle;
|
|
if (ret == ESP_OK) {
|
|
ESP_LOGI(TAG, "Calibration Success");
|
|
} else if (ret == ESP_ERR_NOT_SUPPORTED || !calibrated) {
|
|
ESP_LOGW(TAG, "eFuse not burnt, skip software calibration");
|
|
} else {
|
|
ESP_LOGE(TAG, "Invalid arg or no memory");
|
|
}
|
|
|
|
return calibrated;
|
|
}
|
|
|
|
void adc_ifx_init(void)
|
|
{
|
|
/*esp_adc_cal_value_t val_type = esp_adc_cal_characterize(ADC_UNIT_1, ADC_ATTEN_DB_11, ADC_WIDTH_BIT_DEFAULT, 0, &adc1_chars);
|
|
|
|
//Check type of calibration value used to characterize ADC
|
|
if (val_type == ESP_ADC_CAL_VAL_EFUSE_VREF)
|
|
{
|
|
printf("ADC: eFuse Vref");
|
|
}
|
|
else if (val_type == ESP_ADC_CAL_VAL_EFUSE_TP)
|
|
{
|
|
printf("ADC: Two Point");
|
|
}
|
|
else
|
|
{
|
|
printf("ADC: Default");
|
|
}
|
|
adc1_ulp_enable();
|
|
ESP_ERROR_CHECK(adc1_config_width(ADC_WIDTH_BIT_DEFAULT));*/
|
|
|
|
//-------------ADC1 Init---------------//
|
|
adc_oneshot_unit_init_cfg_t init_config1 = {
|
|
.unit_id = ADC_UNIT_1,
|
|
.ulp_mode = ADC_ULP_MODE_DISABLE
|
|
};
|
|
ESP_ERROR_CHECK(adc_oneshot_new_unit(&init_config1, &adc1_handle));
|
|
|
|
//-------------ADC1 Config---------------//
|
|
adc_oneshot_chan_cfg_t config = {
|
|
.bitwidth = ADC_BITWIDTH_13,
|
|
.atten = ADC_ATTEN_DB_11,
|
|
};
|
|
ESP_ERROR_CHECK(adc_oneshot_config_channel(adc1_handle, ADC_CHANNEL_0, &config));
|
|
//ESP_ERROR_CHECK(adc_oneshot_config_channel(adc1_handle, ADC_CHANNEL_1, &config));
|
|
|
|
//-------------ADC1 Calibration Init---------------//
|
|
if(false == adc_calibration_init(ADC_UNIT_1, ADC_ATTEN_DB_11, &adc1_cali_handle))
|
|
{
|
|
ESP_LOGI(TAG, "ADC Calibration NOT Supported");
|
|
}
|
|
}
|
|
|
|
static void adc_cal_deinit(adc_cali_handle_t handle)
|
|
{
|
|
#if ADC_CALI_SCHEME_CURVE_FITTING_SUPPORTED
|
|
ESP_LOGI(TAG, "deregister %s calibration scheme", "Curve Fitting");
|
|
ESP_ERROR_CHECK(adc_cali_delete_scheme_curve_fitting(handle));
|
|
|
|
#elif ADC_CALI_SCHEME_LINE_FITTING_SUPPORTED
|
|
ESP_LOGI(TAG, "deregister %s calibration scheme", "Line Fitting");
|
|
ESP_ERROR_CHECK(adc_cali_delete_scheme_line_fitting(handle));
|
|
#endif
|
|
}
|
|
|
|
void adx_ifx_deinit(void)
|
|
{
|
|
//adc_cal_deinit(adc1_cali_handle);
|
|
adc_oneshot_del_unit(adc1_handle);
|
|
}
|
|
|
|
uint32_t adc_if_get_batt_mV(void)
|
|
{
|
|
//uint32_t val = 4*esp_adc_cal_raw_to_voltage(adc1_get_raw(ADC1_CHANNEL_0), &adc1_chars);
|
|
ESP_ERROR_CHECK(adc_oneshot_read(adc1_handle, ADC_CHANNEL_0, &adc_raw[0]));
|
|
ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc1_cali_handle, adc_raw[0], &voltage[0]));
|
|
return 4*voltage[0];
|
|
}
|
|
|
|
#if 0
|
|
uint32_t adc_if_get_light_mV(void)
|
|
{
|
|
uint32_t test_val = 0;
|
|
//uint32_t val = esp_adc_cal_raw_to_voltage(adc1_get_raw(ADC1_CHANNEL_1), &adc1_chars);
|
|
ESP_ERROR_CHECK(adc_oneshot_read(adc1_handle, ADC_CHANNEL_1, &adc_raw[1]));
|
|
ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc1_cali_handle, adc_raw[1], &voltage[1]));
|
|
|
|
uint32_t val = voltage[1];
|
|
|
|
ESP_LOGI(TAG, "Light mV: %ld", val);
|
|
test_val = (val*255)/2504;
|
|
//if(data_get_light_sensor_gp() && data_get_light_sensor_gn())
|
|
{
|
|
/*if the new light_sensor value is greater than GB_threshold or lower than GN_threshold*/
|
|
//if((data_get_light_sensor_gn() > test_val) || (data_get_light_sensor_gp() < test_val))
|
|
{
|
|
/* set the prev_light_data to the previous reading */
|
|
//ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc1_cali_handle, ulp_raw_light_data, (int *)&prev_light_data));
|
|
//prev_light_data = ulp_light_data;
|
|
//ulp_prev_light_data=ulp_light_data;//light_data;
|
|
//prev_light_data = (255*prev_light_data)/2504;
|
|
//ulp_light_data = (255*val)/2504;
|
|
//ESP_LOGI(TAG,"LIGHT_GN = %ld\nLIGHT_GP = %ld",data_get_light_sensor_gn(),data_get_light_sensor_gp());
|
|
ESP_LOGI(TAG,"Updating Prev_light_data");
|
|
prev_light_data3 = light_data3;
|
|
light_data3 = test_val;
|
|
//ulp_light_data = val;
|
|
}
|
|
}
|
|
return val;
|
|
}
|
|
|
|
uint32_t adc_if_get_prev_light_mV(void)
|
|
{
|
|
return prev_light_data3;
|
|
}
|
|
#endif
|