-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdcLoadSensor.cpp
206 lines (160 loc) · 4.43 KB
/
AdcLoadSensor.cpp
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
// Copyright (c) 2021 Mathema GmbH
// SPDX-License-Identifier: BSD-3-Clause
// Author: Günter Woigk (Kio!)
// Copyright (c) 2021 kio@little-bat.de
// BSD 2-clause license
#include <stdio.h>
#include <math.h>
#include "pico/stdlib.h"
#include "hardware/adc.h"
#include "hardware/irq.h"
#include "hardware/regs/intctrl.h" // Scheiß Suchspiel
#include "AdcLoadSensor.h"
#include "utilities.h"
AdcLoadSensor load_sensor;
static_assert (ADC_CORE0_IDLE == 0, "");
static_assert (ADC_CORE1_IDLE == 1, "");
static_assert (ADC_TEMPERATURE == 4, "");
void __not_in_flash_func(AdcLoadSensor::adc_irq_handler) () noexcept
//void adc_irq_handler () noexcept
{
while (!adc_fifo_is_empty())
{
uint value = adc_fifo_get();
bool error = value & 1<<12;
#define L load_sensor
if (error)
{
L.adc_errors++;
}
else switch (L.adc_current_channel)
{
case 0:
L.adc_core0_count++;
L.adc_core0_sum += value;
L.adc_core0_min = min(L.adc_core0_min,value);
L.adc_core0_max = max(L.adc_core0_max,value);
break;
case 1:
L.adc_core1_count++;
L.adc_core1_sum += value;
L.adc_core1_min = min(L.adc_core1_min,value);
L.adc_core1_max = max(L.adc_core1_max,value);
break;
default:
L.adc_temperature_count++;
L.adc_temperature_sum += value;
break;
}
static uint8 nxch[] = {1,4,0,0,0};
L.adc_current_channel = nxch[L.adc_current_channel];
}
}
void AdcLoadSensor::init()
{
printf("ADC sample frequency = %u Hz (all channels)\n", uint(adc_clock+FLOAT(0.5)));
adc_init();
if (0) // if adc can be restarted
{
//adc_core0_min_load = ;
//adc_core0_max_load = ;
//adc_core0_avg_load = ;
//adc_core1_min_load = ;
//adc_core1_max_load = ;
//adc_core1_avg_load = ;
//adc_avg_temperature = ;
adc_current_channel = ADC_TEMPERATURE; // startup with temperature
adc_errors = 0;
}
adc_set_temp_sensor_enabled(true); // apply power
adc_gpio_init(ADC_PIN_CORE0_IDLE);
adc_gpio_init(ADC_PIN_CORE1_IDLE);
adc_select_input(ADC_TEMPERATURE); // start with measurement for temperature before round robbin
adc_set_round_robin((1<<ADC_CORE0_IDLE)+(1<<ADC_CORE1_IDLE)+(1<<ADC_TEMPERATURE));
adc_fifo_setup (true/*enable*/, true/*dreq_en*/, 1/*dreq_thresh*/, true/*err_in_fifo*/, false/*!byte_shift*/);
irq_set_exclusive_handler (ADC_IRQ_FIFO, adc_irq_handler);
adc_irq_set_enabled(true);
irq_set_enabled(ADC_IRQ_FIFO,true);
adc_set_clkdiv(adc_clock_divider); // slowest possible: 832 measurements / sec
adc_run(true);
printf("start ADC\n");
while (adc_temperature_count == 0)
{
if (!adc_fifo_is_empty())
{
printf("ADC interrupt failed to start\n");
//for(;;);
}
}
printf("...ok\n");
}
void AdcLoadSensor::getCore0Load (FLOAT& min, FLOAT& avg, FLOAT& max)
{
uint adc_cnt;
uint adc_sum;
do
{
adc_cnt = L.adc_core0_count;
adc_sum = L.adc_core0_sum;
}
while (adc_cnt != L.adc_core0_count);
do
{
L.adc_core0_count = 0;
L.adc_core0_sum = 0;
}
while(L.adc_core0_count);
uint adc_max = L.adc_core0_max;
uint adc_min = L.adc_core0_min;
L.adc_core0_max = 0;
L.adc_core0_min = 1<<12;
min = map_range(FLOAT(adc_max), 0,1<<12, FLOAT(100), FLOAT(0));
max = map_range(FLOAT(adc_min), 0,1<<12, FLOAT(100), FLOAT(0));
FLOAT adc_core0_avg = FLOAT(adc_sum) / FLOAT(adc_cnt);
avg = map_range(adc_core0_avg, 0,1<<12, FLOAT(100), FLOAT(0));
}
void AdcLoadSensor::getCore1Load (FLOAT& min, FLOAT& avg, FLOAT& max)
{
uint adc_cnt;
uint adc_sum;
do
{
adc_cnt = L.adc_core1_count;
adc_sum = L.adc_core1_sum;
}
while (adc_cnt != L.adc_core1_count);
do
{
L.adc_core1_count = 0;
L.adc_core1_sum = 0;
}
while(L.adc_core1_count);
uint adc_max = L.adc_core1_max;
uint adc_min = L.adc_core1_min;
L.adc_core1_max = 0;
L.adc_core1_min = 1<<12;
min = map_range(FLOAT(adc_max), 0,1<<12, FLOAT(100), FLOAT(0));
max = map_range(FLOAT(adc_min), 0,1<<12, FLOAT(100), FLOAT(0));
FLOAT adc_core0_avg = FLOAT(adc_sum) / FLOAT(adc_cnt);
avg = map_range(adc_core0_avg, 0,1<<12, FLOAT(100), FLOAT(0));
}
FLOAT AdcLoadSensor::getTemperature()
{
uint adc_cnt;
uint adc_sum;
do
{
adc_cnt = L.adc_temperature_count;
adc_sum = L.adc_temperature_sum;
}
while (adc_cnt != L.adc_temperature_count);
do
{
L.adc_temperature_count = 0;
L.adc_temperature_sum = 0;
}
while(L.adc_temperature_count);
FLOAT adc_avg_temperature = FLOAT(adc_sum) / FLOAT(adc_cnt);
FLOAT voltage = map_range(adc_avg_temperature, 0, 1<<12, FLOAT(0), FLOAT(3.3));
return map_range(voltage, 0.706f, 0.706f-0.001721f, FLOAT(27), FLOAT(28));
}