-
Notifications
You must be signed in to change notification settings - Fork 78
/
adc_global.c
369 lines (310 loc) · 11 KB
/
adc_global.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
* adc_global.c
*
* Created on: Jan 12, 2021
* Author: David Original work by Jose Barros (PTDreamer), 2017
*/
#include "adc_global.h"
#include "buzzer.h"
#include "iron.h"
#include "tempsensors.h"
#include "voltagesensors.h"
#include "board.h"
#ifdef __BASE_FILE__
#undef __BASE_FILE__
#endif
#define __BASE_FILE__ "adc_global.c"
#define SELECTIVE_FILTERING
volatile adc_measures_t ADC_measures[ADC_BFSIZ];
volatile ADC_Status_t ADC_Status;
volatile ADCDataTypeDef_t TIP = {
.adc_buffer = &ADC_measures[0].TIP,
};
#ifdef USE_VIN
volatile ADCDataTypeDef_t VIN = {
.adc_buffer = &ADC_measures[0].VIN,
.filter.coefficient = 95,
.filter.threshold = 20,
.filter.reset_threshold = 60,
.filter.count_limit = 0,
.filter.min = 50,
.filter.step = 5,
};
#endif
#ifdef USE_NTC
volatile ADCDataTypeDef_t NTC = {
.adc_buffer = &ADC_measures[0].NTC,
.filter.coefficient = 95,
.filter.threshold = 20,
.filter.reset_threshold = 60,
.filter.count_limit = 0,
.filter.min = 50,
.filter.step = 5,
};
#endif
#ifdef USE_VREF
volatile ADCDataTypeDef_t VREF = {
.adc_buffer = &ADC_measures[0].VREF,
.filter.coefficient = 95,
.filter.threshold = 20,
.filter.reset_threshold = 60,
.filter.count_limit = 0,
.filter.min = 50,
.filter.step = 5,
};
#endif
#ifdef ENABLE_INT_TEMP
volatile ADCDataTypeDef_t INT_TMP = {
.adc_buffer = &ADC_measures[0].INT_TMP,
.filter.coefficient = 95,
.filter.threshold = 10,
.filter.reset_threshold = 30,
.filter.count_limit = 0,
.filter.min = 50,
.filter.step = 5,
};
#endif
static ADC_HandleTypeDef *adc_device;
volatile uint8_t reset_measures;
uint8_t ADC_Cal(void){
return HAL_ADCEx_Calibration_Start(adc_device);
}
void ADC_Init(ADC_HandleTypeDef *adc){
adc_device=adc;
ADC_ChannelConfTypeDef sConfig = {0};
#ifdef STM32F072xB
adc_device->Instance->CHSELR &= ~(0x7FFFF); // Disable all regular channels
sConfig.Rank = ADC_RANK_CHANNEL_NUMBER;
#endif
#if defined STM32F101xB || defined STM32F102xB || defined STM32F103xB
adc_device->Init.NbrOfConversion = ADC_Num;
#endif
adc_device->Init.ExternalTrigConv = ADC_SOFTWARE_START; // Set software trigger
if (HAL_ADC_Init(adc_device) != HAL_OK) { Error_Handler(); }
sConfig.SamplingTime = ADC_SAMPLETIME_28CYCLES_5; // More sampling time to compensate high input impedances
#ifdef ADC_CH_1ST
#if defined STM32F101xB || defined STM32F102xB || defined STM32F103xB
sConfig.Rank = ADC_REGULAR_RANK_1;
#endif
sConfig.Channel = ADC_CH_1ST;
if (HAL_ADC_ConfigChannel(adc_device, &sConfig) != HAL_OK){Error_Handler();}
#endif
#ifdef ADC_CH_2ND
#if defined STM32F101xB || defined STM32F102xB || defined STM32F103xB
sConfig.Rank = ADC_REGULAR_RANK_2;
#endif
sConfig.Channel = ADC_CH_2ND;
if (HAL_ADC_ConfigChannel(adc_device, &sConfig) != HAL_OK){Error_Handler();}
#endif
#ifdef ADC_CH_3RD
#if defined STM32F101xB || defined STM32F102xB || defined STM32F103xB
sConfig.Rank = ADC_REGULAR_RANK_3;
#endif
sConfig.Channel = ADC_CH_3RD;
#if defined ENABLE_INT_TEMP && !defined ADC_CH_4TH
sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5; // Last channel is internal temperature, requires min. 10uS sampling time
#endif
if (HAL_ADC_ConfigChannel(adc_device, &sConfig) != HAL_OK){Error_Handler();}
#endif
#ifdef ADC_CH_4TH
#if defined STM32F101xB || defined STM32F102xB || defined STM32F103xB
sConfig.Rank = ADC_REGULAR_RANK_4;
#endif
sConfig.Channel = ADC_CH_4TH;
#if defined ENABLE_INT_TEMP && !defined ADC_CH_5TH
sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
#endif
if (HAL_ADC_ConfigChannel(adc_device, &sConfig) != HAL_OK){Error_Handler();}
#endif
#ifdef ADC_CH_5TH
#if defined STM32F101xB || defined STM32F102xB || defined STM32F103xB
sConfig.Rank = ADC_REGULAR_RANK_5;
#endif
sConfig.Channel = ADC_CH_5TH;
#if defined ENABLE_INT_TEMP && !defined ADC_CH_6TH
sConfig.SamplingTime = ADC_SAMPLETIME_239CYCLES_5;
#endif
if (HAL_ADC_ConfigChannel(adc_device, &sConfig) != HAL_OK){Error_Handler();}
#endif
if(ADC_Cal() != HAL_OK ){
Error_Handler();
}
ADC_Status = ADC_Idle;
buzzer_beep(SHORT_BEEP);
}
void ADC_Start_DMA(){
if(ADC_Status!=ADC_Waiting){
Error_Handler();
}
if(getSettings()->isSaving){ // If saving, skip ADC conversion (PWM pin disabled)
ADC_Status=ADC_Idle;
HAL_IWDG_Refresh(&hiwdg);
return;
}
#ifdef DEBUG_PWM
PWM_DBG_GPIO_Port->BSRR=PWM_DBG_Pin; // Set TEST to 1
#endif
ADC_Status=ADC_Sampling;
if(HAL_ADC_Start_DMA(adc_device, (uint32_t*)ADC_measures, sizeof(ADC_measures)/(sizeof(uint16_t)) )!=HAL_OK){ // Start ADC conversion now
Error_Handler();
}
}
void ADC_Stop_DMA(void){
HAL_ADC_Stop_DMA(adc_device);
}
void ADC_Reset_measures(void){
reset_measures=1; // Set the ADC flag to reset all averages
while(reset_measures); // Cleared after new ADC conversion
}
/*
* Some credits: https://kiritchatterjee.wordpress.com/2014/11/10/a-simple-digital-low-pass-filter-in-c/
*/
void DoAverage(volatile ADCDataTypeDef_t* InputData){
volatile uint16_t *inputBuffer=InputData->adc_buffer;
int32_t adc_sum,avg_data;
uint16_t max=0, min=0xffff;
volatile filter_t *f = &InputData->filter;
int8_t factor = f->coefficient;
float k;
#ifdef DEBUG_PWM
InputData->prev_avg=InputData->last_avg;
InputData->prev_raw=InputData->last_raw;
#endif
// Make the average of the ADC buffer
adc_sum = 0;
for(uint16_t x = 0; x < ADC_BFSIZ; x++) {
adc_sum += *inputBuffer;
if(*inputBuffer > max){
max = *inputBuffer;
}
if(*inputBuffer < min){
min = *inputBuffer;
}
inputBuffer += ADC_Num;
}
//Remove highest and lowest values
adc_sum -= (min + max);
// Calculate average
avg_data = adc_sum / (ADC_BFSIZ-2);
InputData->last_raw = avg_data;
// Reset measures active?
if(reset_measures){
InputData->last_avg = avg_data;
InputData->EMA_of_Input = avg_data;
return;
}
#ifdef SELECTIVE_FILTERING
#if defined DEBUG_PWM && defined SWO_PRINT
extern bool dbg_newData;
#endif
if(factor>0 && factor<100){
int32_t diff = avg_data - (int32_t)InputData->last_avg;
int32_t abs_diff=abs(diff);
if(abs_diff > f->reset_threshold){ // If diff greater reset_threshold, reset the filter
factor = 0;
f->counter = 0;
}
else if(abs_diff > f->threshold){ // diff greater than threshold
if(f->counter < f->count_limit){ // If counter below limit, just increase it, use normal filter coefficient
f->counter++;
}
else{ // Else,
factor = factor + ((f->counter - f->count_limit)*f->step); // compute new coefficient (Note adding because step is negative)
if(factor > f->min){ // if factor greater than limit
f->counter++; // Keep decreasing
}
else{
factor = f->min; // Else, use min
}
}
}
else{ // Below threshold limit, use normal coefficient
f->counter = 0;
}
}
if(factor==0 || factor==100){ // Factor 100 would use 100% of old value, never updating, so we force 0% if that happens (shouldn't)
InputData->EMA_of_Input = avg_data; // Use last average (No filtering)
}
else{
k=((float)factor/100);
InputData->EMA_of_Input = (InputData->EMA_of_Input*k) + ((float)avg_data*(1.0-k));
}
#endif
InputData->last_avg = InputData->EMA_of_Input+0.5f;
}
uint16_t ADC_to_mV (uint16_t adc){
/*
* Instead running ( ADC*(3300/4095) ),
* We previously multiply (3300/4095)*2^20 = 845006
* Then we can use the fast hardware multiplier and
* divide just with bit rotation.
*
* So it becomes Vadc = (ADC * 845006) >>20
* Max possible input = 20 bit number, more will cause overflow to the 32 bit variable
* Calculated to use 12 bit max input from ADC (4095)
* Much, much faster than floats!
*/
return( ((uint32_t)845006*adc)>>20 );
}
// Don't call this function, only the ADC ISR should use it.
void handle_ADC_Data(void){
DoAverage(&TIP);
#ifdef USE_VREF
DoAverage(&VREF);
#endif
#ifdef USE_NTC
DoAverage(&NTC);
#endif
#ifdef USE_VIN
DoAverage(&VIN);
#endif
#ifdef ENABLE_INT_TEMP
DoAverage(&INT_TMP);
#endif
reset_measures = 0;
}
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* _hadc){
#if defined DEBUG_PWM && defined SWO_PRINT
extern bool dbg_newData;
extern uint16_t dbg_prev_TIP_Raw, dbg_prev_TIP, dbg_prev_VIN, dbg_prev_PWR;
extern int16_t dbg_prev_NTC;
bool dbg_t=dbg_newData;
#endif
if(_hadc == adc_device){
if(ADC_Status!=ADC_Sampling){
Error_Handler();
}
ADC_Stop_DMA();
ADC_Status = ADC_Idle;
#ifdef DEBUG_PWM
PWM_DBG_GPIO_Port->BSRR=PWM_DBG_Pin<<16; // Set TEST to 0
#endif
if(getProfileSettings()->WakeInputMode==mode_stand){
readWake();
}
__HAL_TIM_SET_COUNTER(getIronPwmTimer(),0); // Synchronize PWM
#ifndef DEBUG
if((!getIronErrorFlags().safeMode) && (getCurrentMode() != mode_sleep) && getBootCompleteFlag()){
configurePWMpin(output_PWM);
}
#endif
handle_ADC_Data();
#if defined DEBUG_PWM && defined SWO_PRINT
if(dbg_t!=dbg_newData){ // Save values before handleIron() updates them
dbg_prev_TIP_Raw=last_TIP_Raw; // If filter was resetted, print values
dbg_prev_TIP=last_TIP;
dbg_prev_VIN=last_VIN;
dbg_prev_NTC=last_NTC_C;
dbg_prev_PWR=Iron.CurrentIronPower;
}
#endif
handleIron();
runAwayCheck();
#ifdef DEBUG // In debug mode, enable the tip power at the end
if((!getIronErrorFlags().safeMode) && (getCurrentMode() != mode_sleep) && getBootCompleteFlag()){ // Otherwise the tip will stay on and burn if stopping at the iron function
configurePWMpin(output_PWM);
}
#endif
HAL_IWDG_Refresh(&hiwdg);
}
}