main/task_adc.c

Sat, 14 Mar 2020 13:07:02 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 14 Mar 2020 13:07:02 +0100
changeset 47
1ab1f4a8c328
parent 44
e52d11b8f252
child 68
121b3fa6b806
permissions
-rw-r--r--

Version 0.2.2 Changed to use a permanent network and WiFi connection. Removed three mainloop stages. Removed MQTT sequence counter that was not used. Update WiFi rssi status during eacht measure cycle. Changed FreeRTOS schedulng to 500 Hz.

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
41
d327e0aff62f Updated doxygen comments. Removed some development debug logs. Increased user inactivity time to 4 minutes. More Dutch translations in the OTA update screens.
Michiel Broek <mbroek@mbse.eu>
parents: 37
diff changeset
3 * @brief The FreeRTOS task to query the pressure sensors connected to ADC inputs.
0
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
44
e52d11b8f252 Removed dead code. Added more doxygen documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 41
diff changeset
15 #define PRESSURE_1 (CONFIG_PRESSURE_1) ///< ADC channel pressure sensor 1
e52d11b8f252 Removed dead code. Added more doxygen documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 41
diff changeset
16 #define PRESSURE_2 (CONFIG_PRESSURE_2) ///< ADC channel pressure sensor 2
e52d11b8f252 Removed dead code. Added more doxygen documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 41
diff changeset
17 #define PRESSURE_3 (CONFIG_PRESSURE_3) ///< ADC channel pressure sensor 3
e52d11b8f252 Removed dead code. Added more doxygen documentation.
Michiel Broek <mbroek@mbse.eu>
parents: 41
diff changeset
18 #define BATT_CHANNEL (CONFIG_BATT_CHANNEL) ///< ADC channel half battery voltage
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
19
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 static const char *TAG = "task_adc";
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
22
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
23 SemaphoreHandle_t xSemaphoreADC = NULL; ///< Semaphire ADC task
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
24 EventGroupHandle_t xEventGroupADC; ///< Events ADC task
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
25 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
26
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
27 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
28 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
29
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 void request_adc(void)
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
33 {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
34 xEventGroupClearBits(xEventGroupADC, TASK_ADC_REQUEST_DONE);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
35 xEventGroupSetBits(xEventGroupADC, TASK_ADC_REQUEST_PRESSURE);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
36 }
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 bool ready_adc(void)
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
41 {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
42 if (xEventGroupGetBits(xEventGroupADC) & TASK_ADC_REQUEST_DONE)
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
43 return true;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
44 return false;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
45 }
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 * 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
51 */
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
52 void task_adc(void *pvParameter)
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
53 {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
54 int i, adc_reading;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
55 adc_atten_t atten = ADC_ATTEN_DB_0;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
56
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
57 ESP_LOGI(TAG, "Starting task ADC sensors");
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
58 adc_state = malloc(sizeof(ADC_State));
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
59 for (i = 0; i < 3; i++) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
60 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
61 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
62 adc_state->Pressure[i].voltage = 0;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
63 adc_state->Pressure[i].error = ADC_ERR_NONE;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
64 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
65 adc_state->Pressure[0].channel = PRESSURE_1;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
66 adc_state->Pressure[1].channel = PRESSURE_2;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
67 adc_state->Pressure[2].channel = PRESSURE_3;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
68 adc_state->Batt_voltage = 0;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
69 adc_state->Batt_error = ADC_ERR_NONE;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
70
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
71 //Characterize ADC
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
72 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
73
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
74 /* event handler and event group for this task */
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
75 xEventGroupADC = xEventGroupCreate();
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
76 EventBits_t uxBits;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
77
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 * Task loop forever.
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
80 */
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
81 while (1) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
82
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
83 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
84
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
85 if (uxBits & TASK_ADC_REQUEST_PRESSURE) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
86
37
358bbd5b608e menuconfig settings now work for I2C display setup. Lot's of code cleanup.
Michiel Broek <mbroek@mbse.eu>
parents: 12
diff changeset
87 ESP_LOGD(TAG, "Requested ADC readings");
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 adc1_config_width(ADC_WIDTH_BIT_12);
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
89
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
90 for (i = 0; i < 3; i++) {
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
91 adc_reading = 0;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
92 atten = ADC_ATTEN_DB_0;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
93
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 * Autoranging the ADC conversion
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
96 */
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
97 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
98 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
99 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
100 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
101 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
102 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
103 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
104 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
105 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
106 atten = ADC_ATTEN_DB_11;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
107 else
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
108 break;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
109 }
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 * 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
113 */
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
114 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
115 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
116 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
117 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
118 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
119 }
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
120 adc_reading += tmp;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
121 }
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
122 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
123 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
124 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
125 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
126 } 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
127 adc_reading /= NO_OF_SAMPLES;
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
128 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
129 adc_state->Pressure[i].error = ADC_ERR_NONE;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
130 }
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
131 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
132 } 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
133 ESP_LOGE(TAG, "Missed lock 1");
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
134 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
135 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
136 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
137 }
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
138
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
139 /*
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 * 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
141 */
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
142 adc_reading = 0;
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
143 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
144 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
145 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
146 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
147 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
148 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
149 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
150 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
151 }
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 adc_reading += tmp;
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
153 }
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
154 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
155 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
156 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
157 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
158 } 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
159 adc_reading /= NO_OF_SAMPLES;
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
160 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
161 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
162 }
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 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
164 } 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
165 ESP_LOGE(TAG, "Missed lock 2");
0
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
166 }
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 xEventGroupClearBits(xEventGroupADC, TASK_ADC_REQUEST_PRESSURE);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
169 xEventGroupSetBits(xEventGroupADC, TASK_ADC_REQUEST_DONE);
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
170 #if 1
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
171 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
172 #endif
88d965579617 Initial import of the CO2 meter application.
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
173 }
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

mercurial