main/task_adc.c

Sun, 13 Oct 2019 19:27:12 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Sun, 13 Oct 2019 19:27:12 +0200
changeset 12
7dc9003f86a8
parent 0
88d965579617
child 37
358bbd5b608e
permissions
-rw-r--r--

ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.

0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
1 /**
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
2 * @file task_adc.c
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
3 * @brief The FreeRTOS task to query the pressure sensors on ADC inputs.
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
4 * The task will update the ADC_State structure.
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
5 */
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
6
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
7
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
8 #include "config.h"
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
9
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
10
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
11 #define DEFAULT_VREF 1093 ///< Use adc2_vref_to_gpio() to obtain a better estimate
12
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
12 #define NO_OF_SAMPLES 128 ///< Multisampling
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
13
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
14
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
15 #define PRESSURE_1 (CONFIG_PRESSURE_1)
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
16 #define PRESSURE_2 (CONFIG_PRESSURE_2)
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
17 #define PRESSURE_3 (CONFIG_PRESSURE_3)
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
18 #define BATT_CHANNEL (CONFIG_BATT_CHANNEL)
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
19 #define BATT_ACTOR (CONFIG_BATT_ACTOR)
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
20
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
21
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
22 static const char *TAG = "task_adc";
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
23
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
24 SemaphoreHandle_t xSemaphoreADC = NULL; ///< Semaphire ADC task
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
25 EventGroupHandle_t xEventGroupADC; ///< Events ADC task
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
26 ADC_State *adc_state; ///< Public state for other tasks
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
27
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
28 const int TASK_ADC_REQUEST_PRESSURE = BIT0; ///< Request temperature measurements
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
29 const int TASK_ADC_REQUEST_DONE = BIT1; ///< Request is completed
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
30
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
31
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
32
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
33 void request_adc(void)
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
34 {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
35 xEventGroupClearBits(xEventGroupADC, TASK_ADC_REQUEST_DONE);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
36 xEventGroupSetBits(xEventGroupADC, TASK_ADC_REQUEST_PRESSURE);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
37 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
38
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
39
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
40
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
41 bool ready_adc(void)
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
42 {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
43 if (xEventGroupGetBits(xEventGroupADC) & TASK_ADC_REQUEST_DONE)
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
44 return true;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
45 return false;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
46 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
47
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
48
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
49
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
50 /*
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
51 * Task to read pressure sensors and battery voltage on request.
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
52 */
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
53 void task_adc(void *pvParameter)
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
54 {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
55 int i, adc_reading;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
56 adc_atten_t atten = ADC_ATTEN_DB_0;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
57
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
58 ESP_LOGI(TAG, "Starting task ADC sensors");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
59 adc_state = malloc(sizeof(ADC_State));
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
60 for (i = 0; i < 3; i++) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
61 adc_state->Pressure[i].valid = false;
12
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
62 adc_state->Pressure[i].atten = ADC_ATTEN_DB_0;
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
63 adc_state->Pressure[i].voltage = 0;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
64 adc_state->Pressure[i].error = ADC_ERR_NONE;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
65 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
66 adc_state->Pressure[0].channel = PRESSURE_1;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
67 adc_state->Pressure[1].channel = PRESSURE_2;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
68 adc_state->Pressure[2].channel = PRESSURE_3;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
69 adc_state->Batt_voltage = 0;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
70 adc_state->Batt_error = ADC_ERR_NONE;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
71
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
72 //Characterize ADC
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
73 esp_adc_cal_characteristics_t *adc_chars = calloc(1, sizeof(esp_adc_cal_characteristics_t));
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
74
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
75 /* event handler and event group for this task */
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
76 xEventGroupADC = xEventGroupCreate();
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
77 EventBits_t uxBits;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
78
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
79 /*
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
80 * Task loop forever.
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
81 */
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
82 while (1) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
83
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
84 uxBits = xEventGroupWaitBits(xEventGroupADC, TASK_ADC_REQUEST_PRESSURE, pdFALSE, pdFALSE, portMAX_DELAY );
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
85
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
86 if (uxBits & TASK_ADC_REQUEST_PRESSURE) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
87
12
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
88 ESP_LOGI(TAG, "Requested ADC readings");
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
89 adc1_config_width(ADC_WIDTH_BIT_12);
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
90
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
91 for (i = 0; i < 3; i++) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
92 adc_reading = 0;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
93 atten = ADC_ATTEN_DB_0;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
94
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
95 /*
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
96 * Autoranging the ADC conversion
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
97 */
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
98 while (1) {
12
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
99 esp_adc_cal_characterize(ADC_UNIT_1, atten, ADC_WIDTH_BIT_12, DEFAULT_VREF, adc_chars);
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
100 adc1_config_channel_atten((adc1_channel_t)adc_state->Pressure[i].channel, atten);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
101 int raw = adc1_get_raw((adc1_channel_t)adc_state->Pressure[i].channel);
12
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
102 if (atten == ADC_ATTEN_DB_0 && raw > 3700)
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
103 atten = ADC_ATTEN_DB_2_5;
12
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
104 else if (atten == ADC_ATTEN_DB_2_5 && raw > 3700)
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
105 atten = ADC_ATTEN_DB_6;
12
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
106 else if (atten == ADC_ATTEN_DB_6 && raw > 3700)
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
107 atten = ADC_ATTEN_DB_11;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
108 else
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
109 break;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
110 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
111
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
112 /*
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
113 * Now that he have the best attenuation, multisample the real value.
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
114 */
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
115 for (int j = 0; j < NO_OF_SAMPLES; j++) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
116 int tmp = adc1_get_raw((adc1_channel_t)adc_state->Pressure[i].channel);
12
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
117 if (tmp < 0) {
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
118 adc_reading = -1;
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
119 break;
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
120 }
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
121 adc_reading += tmp;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
122 }
12
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
123 if (xSemaphoreTake(xSemaphoreADC, 10) == pdTRUE) {
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
124 if (adc_reading < 0) {
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
125 adc_state->Pressure[i].error = ADC_ERR_READ;
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
126 adc_state->Pressure[i].voltage = 0;
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
127 } else {
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
128 adc_reading /= NO_OF_SAMPLES;
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
129 adc_state->Pressure[i].voltage = esp_adc_cal_raw_to_voltage(adc_reading, adc_chars); // voltage in mV
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
130 adc_state->Pressure[i].error = ADC_ERR_NONE;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
131 }
12
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
132 xSemaphoreGive(xSemaphoreADC);
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
133 } else {
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
134 ESP_LOGE(TAG, "Missed lock 1");
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
135 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
136 ESP_LOGI(TAG, "Pressure %d raw: %4d, atten: %d, %.3f volt, error: %d",
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
137 i, adc_reading, atten, adc_state->Pressure[i].voltage / 1000.0, adc_state->Pressure[i].error);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
138 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
139
12
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
140 /*
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
141 * Read VDD by reading 1/2 VDD from a precision ladder.
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
142 */
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
143 adc_reading = 0;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
144 atten = ADC_ATTEN_DB_6; // Don't use DB_11, it has a bad linearity.
12
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
145 esp_adc_cal_characterize(ADC_UNIT_1, atten, ADC_WIDTH_BIT_12, DEFAULT_VREF, adc_chars);
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
146 adc1_config_channel_atten((adc1_channel_t)BATT_CHANNEL, atten);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
147 for (int j = 0; j < NO_OF_SAMPLES; j++) {
12
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
148 int tmp = adc1_get_raw((adc1_channel_t)BATT_CHANNEL);
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
149 if (tmp < 0) {
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
150 adc_reading = -1;
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
151 break;
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
152 }
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
153 adc_reading += tmp;
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
154 }
12
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
155 if (xSemaphoreTake(xSemaphoreADC, 10) == pdTRUE) {
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
156 if (adc_reading < 0) {
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
157 adc_state->Batt_voltage = 3.3;
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
158 adc_state->Batt_error = ADC_ERR_READ;
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
159 } else {
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
160 adc_reading /= NO_OF_SAMPLES;
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
161 adc_state->Batt_voltage = esp_adc_cal_raw_to_voltage(adc_reading, adc_chars) * 2; // Chip supply voltage in mV
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
162 adc_state->Batt_error = ADC_ERR_NONE;
12
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
163 }
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
164 xSemaphoreGive(xSemaphoreADC);
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
165 } else {
7dc9003f86a8 ADC1 source code cleanup. Switched to 12 bit resolution again. Multisample set to 128. Improved adc-state structure locking. Improved ADC read error detection. DS18B20 extra error logging. MQTT better publish counter locking.
Michiel Broek <mbroek@mbse.eu>
parents: 0
diff changeset
166 ESP_LOGE(TAG, "Missed lock 2");
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
167 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
168
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
169 xEventGroupClearBits(xEventGroupADC, TASK_ADC_REQUEST_PRESSURE);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
170 xEventGroupSetBits(xEventGroupADC, TASK_ADC_REQUEST_DONE);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
171 #if 1
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
172 ESP_LOGI(TAG, "Battery raw: %4d, atten: %d %.3f volt, error: %d", adc_reading, atten, adc_state->Batt_voltage / 1000.0, adc_state->Batt_error);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
173 #endif
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
174 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
175 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
176 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
177

mercurial