zero
This commit is contained in:
parent
b6682ac288
commit
383600cbdf
@ -2,15 +2,21 @@
|
||||
#include "esp_adc/adc_continuous.h"
|
||||
#include "esp_adc/adc_cali_scheme.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_log.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/portmacro.h"
|
||||
#include "freertos/task.h"
|
||||
#include "math.h"
|
||||
|
||||
#include "hal/adc_types.h"
|
||||
#include "measure.h"
|
||||
#include "soc/soc_caps.h"
|
||||
|
||||
|
||||
adc_continuous_handle_t init_measurement_inputs(measurement_input inputs[], int inputs_num){
|
||||
adc_continuous_handle_cfg_t adc_h_conf = {
|
||||
.conv_frame_size = SOC_ADC_DIGI_DATA_BYTES_PER_CONV, // One sample per interrupt for now
|
||||
.max_store_buf_size = SOC_ADC_DIGI_DATA_BYTES_PER_CONV * inputs_num * 100, // TODO: averaging nb
|
||||
.max_store_buf_size = SOC_ADC_DIGI_DATA_BYTES_PER_CONV * inputs_num * ADC_STORE_BUFFER_SIZE,
|
||||
};
|
||||
|
||||
adc_continuous_handle_t handle;
|
||||
@ -34,7 +40,7 @@ adc_continuous_handle_t init_measurement_inputs(measurement_input inputs[], int
|
||||
.pattern_num = inputs_num,
|
||||
.conv_mode = ADC_CONV_SINGLE_UNIT_1,
|
||||
.format = ADC_DIGI_OUTPUT_FORMAT_TYPE2,
|
||||
.sample_freq_hz = SOC_ADC_SAMPLE_FREQ_THRES_LOW,
|
||||
.sample_freq_hz = SOC_ADC_SAMPLE_FREQ_THRES_HIGH,
|
||||
};
|
||||
|
||||
ESP_ERROR_CHECK(adc_continuous_config(handle, &adc_conf));
|
||||
@ -51,4 +57,54 @@ void init_conv_driver(measurement_input inputs[], int inputs_num, adc_cali_handl
|
||||
};
|
||||
ESP_ERROR_CHECK(adc_cali_create_scheme_curve_fitting(&cali_config, &out[i]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void cali_get_one_chunk_avg(adc_continuous_handle_t handle, unsigned nb_samples, measurement_input inputs[], int nb_inputs, uint32_t meas[], uint32_t meas_nb[]){
|
||||
|
||||
uint32_t samples_size = nb_samples * SOC_ADC_DIGI_DATA_BYTES_PER_CONV;
|
||||
|
||||
uint8_t res_r[samples_size];
|
||||
uint32_t ret_num;
|
||||
|
||||
ESP_ERROR_CHECK(adc_continuous_read(handle, res_r, samples_size, &ret_num, 100));
|
||||
if(ret_num != samples_size)
|
||||
ESP_LOGW("ZERO calibration", "Zero calibration : number of samples received not equal to requested %lu / %lu", ret_num, samples_size);
|
||||
|
||||
uint32_t ret_nb = ret_num / SOC_ADC_DIGI_DATA_BYTES_PER_CONV;
|
||||
|
||||
for(int i = 0; i < ret_nb; i++){
|
||||
adc_digi_output_data_t* res = (adc_digi_output_data_t*)(&res_r[i*sizeof(adc_digi_output_data_t)]);
|
||||
int index = 0;
|
||||
|
||||
for(int j = 0; j < nb_inputs; j++){ // meh
|
||||
if(inputs[j].channel == res->type2.channel){
|
||||
index = j;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
meas[index] += res->type2.data;
|
||||
meas_nb[index]++;
|
||||
}
|
||||
}
|
||||
|
||||
void start_zero_cali(adc_continuous_handle_t handle, unsigned int nb_samples, measurement_input inputs[], int nb_inputs, uint32_t offsets[]){
|
||||
uint32_t meas[nb_inputs];
|
||||
uint32_t meas_nb[nb_inputs];
|
||||
for(int i = 0; i < nb_inputs; i++){
|
||||
meas[i] = 0;
|
||||
meas_nb[i] = 0;
|
||||
}
|
||||
|
||||
for(int i = 0; i < nb_samples / ADC_STORE_BUFFER_SIZE; i++){
|
||||
unsigned int nsamp = fmin(nb_samples - (i * ADC_STORE_BUFFER_SIZE), ADC_STORE_BUFFER_SIZE);
|
||||
cali_get_one_chunk_avg(handle, nsamp, inputs, nb_inputs, meas, meas_nb);
|
||||
vTaskDelay(10 / portTICK_PERIOD_MS);
|
||||
}
|
||||
|
||||
ESP_LOGI("ZERO cali", "%lu %lu %lu", meas[0]/meas_nb[0], meas[1]/meas_nb[1], meas[2]/meas_nb[2]);
|
||||
|
||||
for(int i = 0; i < nb_inputs; i++){
|
||||
offsets[i] = meas[i] / meas_nb[i];
|
||||
}
|
||||
}
|
||||
|
@ -3,5 +3,8 @@ typedef struct {
|
||||
unsigned int gain; // x1000
|
||||
} measurement_input;
|
||||
|
||||
#define ADC_STORE_BUFFER_SIZE 100 // in number of samples / nb of inputs
|
||||
|
||||
adc_continuous_handle_t init_measurement_inputs(measurement_input inputs[], int inputs_num);
|
||||
void init_conv_driver(measurement_input inputs[], int inputs_num, adc_cali_handle_t* out);
|
||||
void init_conv_driver(measurement_input inputs[], int inputs_num, adc_cali_handle_t* out);
|
||||
void start_zero_cali(adc_continuous_handle_t handle, unsigned int nb_samples, measurement_input inputs[], int nb_inputs, uint32_t offsets[]);
|
||||
|
@ -58,11 +58,10 @@ measurement_input inputs[] = {
|
||||
};
|
||||
|
||||
static spinlock_t adc_res_mutex;
|
||||
volatile unsigned long conv_done_cpt = 0;
|
||||
volatile int meas_res[INPUTS_NUM];
|
||||
volatile int meas_nb[INPUTS_NUM];
|
||||
volatile uint32_t meas_res[INPUTS_NUM];
|
||||
volatile uint32_t meas_nb[INPUTS_NUM];
|
||||
|
||||
char buff[64];
|
||||
uint32_t offsets[INPUTS_NUM];
|
||||
|
||||
unsigned int r_ind = 0;
|
||||
|
||||
@ -80,8 +79,6 @@ int get_input_index_from_channel(adc_channel_t channel){
|
||||
}
|
||||
|
||||
static bool IRAM_ATTR on_conv_done(adc_continuous_handle_t handle, const adc_continuous_evt_data_t *edata, void *user_data){
|
||||
conv_done_cpt = edata->size;
|
||||
|
||||
adc_digi_output_data_t* res = (adc_digi_output_data_t*)edata->conv_frame_buffer;
|
||||
|
||||
int index = get_input_index_from_channel(res->type2.channel);
|
||||
@ -126,7 +123,7 @@ void app_main(void){
|
||||
.on_conv_done = &on_conv_done,
|
||||
.on_pool_ovf = NULL,
|
||||
};
|
||||
adc_continuous_register_event_callbacks(adc_handle, &adc_event, NULL);
|
||||
ESP_ERROR_CHECK(adc_continuous_register_event_callbacks(adc_handle, &adc_event, NULL));
|
||||
ESP_ERROR_CHECK(adc_continuous_start(adc_handle));
|
||||
|
||||
|
||||
@ -148,7 +145,9 @@ void app_main(void){
|
||||
|
||||
for(int i = 0; i < INPUTS_NUM; i++){
|
||||
int mv;
|
||||
ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc_conv_h[i], meas_res_buff[i]/meas_nb_buff[i], &mv));
|
||||
uint32_t val = meas_res_buff[i]/meas_nb_buff[i];
|
||||
val = (val >= offsets[i])?(val-offsets[i]):0;
|
||||
ESP_ERROR_CHECK(adc_cali_raw_to_voltage(adc_conv_h[i], val, &mv));
|
||||
ESP_LOGI(TAG, "IN %d : %d mV (%d / %d)", i, mv, meas_res_buff[i], meas_nb_buff[i]);
|
||||
}
|
||||
|
||||
@ -157,5 +156,7 @@ void app_main(void){
|
||||
|
||||
vTaskDelay(500 / portTICK_PERIOD_MS);
|
||||
r_ind = (r_ind + 1) % R_NUM;
|
||||
|
||||
if(r_ind == 2) start_zero_cali(adc_handle, 1000, inputs, INPUTS_NUM, offsets);
|
||||
}
|
||||
}
|
||||
|
12
sdkconfig
12
sdkconfig
@ -318,14 +318,14 @@ CONFIG_ESPTOOLPY_FLASHFREQ_80M=y
|
||||
# CONFIG_ESPTOOLPY_FLASHFREQ_20M is not set
|
||||
CONFIG_ESPTOOLPY_FLASHFREQ="80m"
|
||||
# CONFIG_ESPTOOLPY_FLASHSIZE_1MB is not set
|
||||
CONFIG_ESPTOOLPY_FLASHSIZE_2MB=y
|
||||
# CONFIG_ESPTOOLPY_FLASHSIZE_4MB is not set
|
||||
# CONFIG_ESPTOOLPY_FLASHSIZE_2MB is not set
|
||||
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
|
||||
# CONFIG_ESPTOOLPY_FLASHSIZE_8MB is not set
|
||||
# CONFIG_ESPTOOLPY_FLASHSIZE_16MB is not set
|
||||
# CONFIG_ESPTOOLPY_FLASHSIZE_32MB is not set
|
||||
# CONFIG_ESPTOOLPY_FLASHSIZE_64MB is not set
|
||||
# CONFIG_ESPTOOLPY_FLASHSIZE_128MB is not set
|
||||
CONFIG_ESPTOOLPY_FLASHSIZE="2MB"
|
||||
CONFIG_ESPTOOLPY_FLASHSIZE="4MB"
|
||||
# CONFIG_ESPTOOLPY_HEADER_FLASHSIZE_UPDATE is not set
|
||||
CONFIG_ESPTOOLPY_BEFORE_RESET=y
|
||||
# CONFIG_ESPTOOLPY_BEFORE_NORESET is not set
|
||||
@ -724,8 +724,7 @@ CONFIG_ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP=y
|
||||
#
|
||||
# Memory protection
|
||||
#
|
||||
CONFIG_ESP_SYSTEM_MEMPROT_FEATURE=y
|
||||
CONFIG_ESP_SYSTEM_MEMPROT_FEATURE_LOCK=y
|
||||
# CONFIG_ESP_SYSTEM_MEMPROT_FEATURE is not set
|
||||
# end of Memory protection
|
||||
|
||||
CONFIG_ESP_SYSTEM_EVENT_QUEUE_SIZE=32
|
||||
@ -1536,8 +1535,7 @@ CONFIG_ESP_SYSTEM_PM_POWER_DOWN_CPU=y
|
||||
# CONFIG_ESP32C3_DEFAULT_CPU_FREQ_80 is not set
|
||||
CONFIG_ESP32C3_DEFAULT_CPU_FREQ_160=y
|
||||
CONFIG_ESP32C3_DEFAULT_CPU_FREQ_MHZ=160
|
||||
CONFIG_ESP32C3_MEMPROT_FEATURE=y
|
||||
CONFIG_ESP32C3_MEMPROT_FEATURE_LOCK=y
|
||||
# CONFIG_ESP32C3_MEMPROT_FEATURE is not set
|
||||
CONFIG_SYSTEM_EVENT_QUEUE_SIZE=32
|
||||
CONFIG_SYSTEM_EVENT_TASK_STACK_SIZE=2304
|
||||
CONFIG_MAIN_TASK_STACK_SIZE=3584
|
||||
|
Loading…
x
Reference in New Issue
Block a user