-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource.c
360 lines (315 loc) · 7.87 KB
/
source.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
/*
* Copyright 2020 Codethink Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "source.h"
#include "opamp.h"
#include "adc.h"
#include "../mq.h"
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/adc.h>
typedef struct
{
uint32_t gpio_port;
uint8_t gpio_pin;
bl_acq_opamp_t **opamp;
bool opamp_used;
bl_acq_adc_t **adc;
uint8_t adc_channel;
uint32_t adc_ccr_flag;
uint32_t enable;
uint8_t channel_count;
uint8_t channel[BL_ACQ_CHANNEL_COUNT];
uint8_t channel_current;
bl_acq_source_config_t config;
} bl_acq_source_t;
static bl_acq_source_t bl_acq_source[] =
{
#if (BL_REVISION == 1)
[BL_ACQ_PD1] = {
.gpio_port = GPIOA,
.gpio_pin = 4,
.opamp = &bl_acq_opamp4,
.opamp_used = false,
.adc = &bl_acq_adc2,
.adc_channel = 1,
.adc_ccr_flag = 0,
.enable = 0,
},
[BL_ACQ_PD2] = {
.gpio_port = GPIOA,
.gpio_pin = 3,
.opamp = NULL,
.opamp_used = false,
.adc = &bl_acq_adc1,
.adc_channel = 4,
.adc_ccr_flag = 0,
.enable = 0,
},
[BL_ACQ_PD3] = {
.gpio_port = GPIOA,
.gpio_pin = 7,
.opamp = &bl_acq_opamp2,
.opamp_used = false,
.adc = &bl_acq_adc2,
.adc_channel = 4,
.adc_ccr_flag = 0,
.enable = 0,
},
[BL_ACQ_PD4] = {
.gpio_port = GPIOA,
.gpio_pin = 5,
.opamp = NULL,
.opamp_used = false,
.adc = &bl_acq_adc2,
.adc_channel = 2,
.adc_ccr_flag = 0,
.enable = 0,
},
[BL_ACQ_3V3] = {
.gpio_port = GPIOA,
.gpio_pin = 1,
.opamp = NULL,
.opamp_used = false,
.adc = &bl_acq_adc1,
.adc_channel = 2,
.adc_ccr_flag = 0,
.enable = 0,
},
[BL_ACQ_5V0] = {
.gpio_port = GPIOA,
.gpio_pin = 2,
.opamp = NULL,
.opamp_used = false,
.adc = &bl_acq_adc1,
.adc_channel = 3,
.adc_ccr_flag = 0,
.enable = 0,
},
[BL_ACQ_TMP] = {
.gpio_port = 0,
.opamp = NULL,
.opamp_used = false,
.adc = &bl_acq_adc1,
.adc_channel = ADC_CHANNEL_TEMP,
.adc_ccr_flag = ADC_CCR_TSEN,
.enable = 0,
},
#else
[BL_ACQ_PD1] = {
.gpio_port = GPIOB,
.gpio_pin = 2,
.opamp = &bl_acq_opamp3,
.opamp_used = true,
.adc = NULL,
.enable = 0,
},
[BL_ACQ_PD2] = {
.gpio_port = GPIOB,
.gpio_pin = 10,
.opamp = &bl_acq_opamp4,
.opamp_used = true,
.adc = NULL,
.enable = 0,
},
[BL_ACQ_PD3] = {
.gpio_port = GPIOB,
.gpio_pin = 1,
.opamp = NULL,
.opamp_used = false,
.adc = &bl_acq_adc3,
.adc_channel = 1,
.enable = 0,
},
[BL_ACQ_PD4] = {
.gpio_port = GPIOA,
.gpio_pin = 3,
.opamp = &bl_acq_opamp1,
.opamp_used = true,
.adc = NULL,
.enable = 0,
},
[BL_ACQ_3V3] = {
.gpio_port = 0,
.opamp = NULL,
.opamp_used = false,
.adc = &bl_acq_adc1,
.adc_channel = ADC_CHANNEL_VBAT,
.adc_ccr_flag = ADC_CCR_VBATEN,
.enable = 0,
},
[BL_ACQ_5V0] = {
.gpio_port = GPIOA,
.gpio_pin = 7,
.opamp = &bl_acq_opamp2,
.opamp_used = false,
.adc = &bl_acq_adc2,
.adc_channel = 4,
.adc_ccr_flag = 0,
.enable = 0,
},
[BL_ACQ_TMP] = {
.gpio_port = 0,
.opamp = NULL,
.opamp_used = false,
.adc = &bl_acq_adc1,
.adc_channel = ADC_CHANNEL_TEMP,
.adc_ccr_flag = ADC_CCR_TSEN,
.enable = 0,
},
[BL_ACQ_EXT] = {
.gpio_port = GPIOB,
.gpio_pin = 15,
.opamp = &bl_acq_opamp5,
.opamp_used = true,
.adc = NULL,
.enable = 0,
},
#endif
};
void bl_acq_source_calibrate(enum bl_acq_source source)
{
bl_acq_source_t *src = &bl_acq_source[source];
if (src->opamp) {
bl_acq_opamp_calibrate(*(src->opamp));
}
if (src->adc) {
bl_acq_adc_calibrate(*(src->adc));
}
}
enum bl_error bl_acq_source_configure(enum bl_acq_source source)
{
bl_acq_source_t *src = &bl_acq_source[source];
bl_acq_source_config_t *config = &src->config;
bool opamp_needed = (config->opamp_gain > 1) ||
(config->opamp_offset != OPAMP_OFFSET_ZERO);
if (opamp_needed && (src->opamp == NULL)) {
return BL_ERROR_HARDWARE_CONFLICT;
}
src->opamp_used = opamp_needed || (src->adc == NULL);
return BL_ERROR_NONE;
}
void bl_acq_source_assign_channel(enum bl_acq_source source, unsigned channel)
{
bl_acq_source_t *src = &bl_acq_source[source];
for (unsigned i = 0; i < src->channel_count; i++) {
if (src->channel[i] == channel) {
return;
}
}
src->channel[src->channel_count++] = channel;
}
void bl_acq_source_enable(enum bl_acq_source source)
{
bl_acq_source_t *src = &bl_acq_source[source];
src->enable++;
if (src->enable > 1) {
/* Already enabled. */
return;
}
if (src->gpio_port != 0) {
gpio_mode_setup(src->gpio_port, GPIO_MODE_ANALOG,
GPIO_PUPD_NONE, (1U << src->gpio_pin));
}
const bl_acq_source_config_t *config = &src->config;
if (src->opamp_used && (src->opamp != NULL)) {
bl_acq_opamp_enable(*(src->opamp));
} else if (src->adc != NULL) {
bl_acq_adc_enable(*(src->adc), src->adc_ccr_flag);
}
}
void bl_acq_source_disable(enum bl_acq_source source)
{
bl_acq_source_t *src = &bl_acq_source[source];
if (src->enable == 0) {
return;
}
src->enable--;
if (src->enable > 0) {
return;
}
if ((src->adc != NULL) && (src->config.opamp_gain <= 1)) {
bl_acq_adc_disable(*(src->adc), src->adc_ccr_flag);
} else if (src->opamp != NULL) {
bl_acq_opamp_disable(*(src->opamp));
}
/* Clear configuration so we're ready for re-configuring. */
src->channel_count = 0;
src->channel_current = 0;
/* Don't need to do anything here to disable GPIO (it is floating). */
}
bool bl_acq_source_is_enabled(enum bl_acq_source source)
{
bl_acq_source_t *src = &bl_acq_source[source];
return src->enable;
}
bl_acq_source_config_t *bl_acq_source_get_config(enum bl_acq_source source)
{
bl_acq_source_t *src = &bl_acq_source[source];
return &src->config;
}
bl_acq_timer_t *bl_acq_source_get_timer(enum bl_acq_source source)
{
bl_acq_source_t *src = &bl_acq_source[source];
if (src->opamp_used) {
bl_acq_adc_t *adc = bl_acq_opamp_get_adc(
*(src->opamp), NULL);
return bl_acq_adc_get_timer(adc);
}
return bl_acq_adc_get_timer(*(src->adc));
}
bl_acq_opamp_t *bl_acq_source_get_opamp(enum bl_acq_source source)
{
bl_acq_source_t *src = &bl_acq_source[source];
if (!src->opamp_used) {
return NULL;
}
return *(src->opamp);
}
#if (BL_REVISION >= 2)
bl_acq_dac_t *bl_acq_source_get_dac(enum bl_acq_source source,
uint8_t *dac_channel)
{
bl_acq_opamp_t *opamp = bl_acq_source_get_opamp(source);
if (opamp == NULL) {
return NULL;
}
return bl_acq_opamp_get_dac(opamp, dac_channel);
}
#endif
bl_acq_adc_t *bl_acq_source_get_adc(enum bl_acq_source source,
uint8_t *adc_channel)
{
bl_acq_source_t *src = &bl_acq_source[source];
if (src->opamp_used) {
return bl_acq_opamp_get_adc(*(src->opamp), adc_channel);
}
if (adc_channel != NULL) {
*adc_channel = src->adc_channel;
}
return *(src->adc);
}
static inline uint8_t bl_acq_source__current_idx(bl_acq_source_t *src)
{
uint8_t current = src->channel_current++;
if (src->channel_current == src->channel_count) {
src->channel_current = 0;
}
return current;
}
uint8_t bl_acq_source_get_channel(enum bl_acq_source source)
{
bl_acq_source_t *src = &bl_acq_source[source];
return src->channel[bl_acq_source__current_idx(src)];
}