aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/measure.c
blob: 3141917c98ef41db1b0e4793af42d66cedaee96e (plain)
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
/*
 * Xytronic LF-1600
 * Measurement routines
 *
 * Copyright (c) 2015-2017 Michael Buesch <m@bues.ch>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "measure.h"
#include "scale.h"
#include "timer.h"
#include "debug_uart.h"
#include "ring.h"

#include <string.h>

#include <avr/io.h>
#include <avr/interrupt.h>


/* Trigger source selection */
#define TS_FREERUN		((0 << ADTS2) | (0 << ADTS1) | (0 << ADTS0))
#define TS_ACOMP		((0 << ADTS2) | (0 << ADTS1) | (1 << ADTS0))
#define TS_INT0			((0 << ADTS2) | (1 << ADTS1) | (0 << ADTS0))
#define TS_T0CMA		((0 << ADTS2) | (1 << ADTS1) | (1 << ADTS0))
#define TS_T0OV			((1 << ADTS2) | (0 << ADTS1) | (0 << ADTS0))
#define TS_T1CMB		((1 << ADTS2) | (0 << ADTS1) | (1 << ADTS0))
#define TS_T1OV			((1 << ADTS2) | (1 << ADTS1) | (0 << ADTS0))
#define TS_T1CAP		((1 << ADTS2) | (1 << ADTS1) | (1 << ADTS0))


extern const struct measure_config __flash meastemp_config;
extern const struct measure_config __flash meascurr_config;


struct meas_chan_context {
	const struct measure_config __flash *config;

	int16_t old_report_value;

#if CONF_ADJ
	fixpt_t adjustment;
#endif

	bool plaus_timeout;
	bool plaus_timeout_timer_running;
	struct timer plaus_timeout_timer;
};

struct meas_context {
	struct meas_chan_context channels[NR_MEAS_CHANS];
	uint8_t active_chan;

	uint16_t avg_count;
	uint32_t avg_sum;
	struct timer avg_timer;

	bool result_available;
};

static struct meas_context meas = {
	.channels = {
		/* MEAS_CHAN_CURRENT */
		{
			.config				= &meascurr_config,
			.old_report_value		= 0,
#if CONF_ADJ
			.adjustment			= FLOAT_TO_FIXPT(0),
#endif
			.plaus_timeout			= false,
			.plaus_timeout_timer_running	= false,
			.plaus_timeout_timer		= { },
		},
		/* MEAS_CHAN_TEMP */
		{
			.config				= &meastemp_config,
			.old_report_value		= 0,
#if CONF_ADJ
			.adjustment			= FLOAT_TO_FIXPT(0),
#endif
			.plaus_timeout			= false,
			.plaus_timeout_timer_running	= false,
			.plaus_timeout_timer		= { },

		},
	},
	.active_chan		= 0,
	.avg_count		= 0,
	.avg_sum		= 0,
	.avg_timer		= { },
	.result_available	= false,
};


uint16_t meascurr_filter_handler(uint16_t raw_adc);

static uint16_t measure_user_filter_handler(enum measure_chan chan_id,
					    uint16_t raw_adc)
{
	switch (chan_id) {
	case MEAS_CHAN_TEMP:
	default:
		return raw_adc;
	case MEAS_CHAN_CURRENT:
		return meascurr_filter_handler(raw_adc);
	}
}

void meastemp_result_handler(fixpt_t measured_phys_value,
			     enum measure_plausibility plaus);
void meascurr_result_handler(fixpt_t measured_phys_value,
			     enum measure_plausibility plaus);

static void measure_user_result_handler(enum measure_chan chan_id,
					fixpt_t phys,
					enum measure_plausibility plaus)
{
	switch (chan_id) {
	case MEAS_CHAN_TEMP:
		meastemp_result_handler(phys, plaus);
		break;
	case MEAS_CHAN_CURRENT:
	default:
		meascurr_result_handler(phys, plaus);
		break;
	}
}

static void adc_disable(void)
{
	/* Disable the ADC. */
	ADCSRA = 0;
	/* Clear the interrupt flag. */
	ADCSRA |= 1 << ADIF;
}

static void adc_busywait(void)
{
	do { } while (ADCSRA & (1 << ADSC));
}

static void adc_trigger(uint8_t mux, uint8_t ps, uint8_t ref, uint8_t did,
			bool irq, bool freerunning)
{
	uint8_t trig, ie;

	/* Free-running mode selection. */
	if (freerunning)
		trig = 1 << ADATE;
	else
		trig = 0 << ADATE;

	/* Interrupt-enable selection */
	if (irq)
		ie = 1 << ADIE;
	else
		ie = 0 << ADIE;

	mb();

	/* Disable the digital input */
	DIDR0 |= did;

	/* Set multiplexer and start conversion. */
	ADMUX = ref | (0 << ADLAR) | mux;
	ADCSRB = (0 << ACME) | TS_FREERUN;
	ADCSRA = (1 << ADEN) | (1 << ADSC) | ps | ie | trig;
}

static void adc_trigger_next_chan(void)
{
	const struct measure_config __flash *config;
	uint8_t active_chan;

	/* Switch to the next channel. */
	active_chan = ring_next(meas.active_chan, ARRAY_SIZE(meas.channels) - 1u);
	meas.active_chan = active_chan;
	config = meas.channels[active_chan].config;

	/* Reset the averaging. */
	meas.avg_sum = 0;
	meas.avg_count = 0;
	timer_arm(&meas.avg_timer,
		  config->averaging_timeout_ms);

	/* Start the ADC in freerunning mode. */
	adc_trigger(config->mux, config->ps, config->ref, config->did,
		    true, true);
}

static void measure_handle_result(void)
{
	struct meas_chan_context *active_chan;
	enum measure_chan active_chan_id;
	const struct measure_config __flash *config;
	uint16_t raw_adc;
	fixpt_t phys;
	enum measure_plausibility plaus;

	active_chan_id = (enum measure_chan)meas.active_chan;
	active_chan = &meas.channels[active_chan_id];
	config = active_chan->config;

	/* Calculate the result of the averaging. */
	raw_adc = (uint16_t)(meas.avg_sum / meas.avg_count);

	debug_report_int16(DEBUG_PREFIX1(config->name),
			   &active_chan->old_report_value,
			   (int16_t)raw_adc);

	/* Filter the raw adc value, if we have a filter. */
	raw_adc = measure_user_filter_handler(active_chan_id, raw_adc);

	/* Scale raw to phys. */
	phys = scale((int16_t)raw_adc,
		     (int16_t)config->scale_raw_lo,
		     (int16_t)config->scale_raw_hi,
		     config->scale_phys_lo,
		     config->scale_phys_hi);

	/* Apply the physical value adjustment. */
#if CONF_ADJ
	phys = fixpt_sub(phys, active_chan->adjustment);
#endif

	/* Plausibility check. */
	if (phys < config->plaus_neglim) {
		phys = config->plaus_neglim;
		plaus = MEAS_NOT_PLAUSIBLE;
	} else if (phys > config->plaus_poslim) {
		phys = config->plaus_poslim;
		plaus = MEAS_NOT_PLAUSIBLE;
	} else {
		plaus = MEAS_PLAUSIBLE;

		active_chan->plaus_timeout_timer_running = false;
		active_chan->plaus_timeout = false;
	}

	if (plaus != MEAS_PLAUSIBLE &&
	    !active_chan->plaus_timeout_timer_running) {
		timer_arm(&active_chan->plaus_timeout_timer,
			  config->plaus_timeout_ms);
		active_chan->plaus_timeout_timer_running = true;
	}

	if (active_chan->plaus_timeout_timer_running &&
	    timer_expired(&active_chan->plaus_timeout_timer))
		active_chan->plaus_timeout = true;

	if (active_chan->plaus_timeout)
		plaus = MEAS_PLAUS_TIMEOUT;

	measure_user_result_handler(active_chan_id, phys, plaus);
}

ISR(ADC_vect)
{
	uint16_t raw_adc;

	mb();

	/* Read the raw ADC value. */
	raw_adc = ADC;

	/* Add it to the averaging sum and check if we are done. */
	meas.avg_sum += raw_adc;
	meas.avg_count += 1;
	if (timer_expired(&meas.avg_timer)) {
		meas.result_available = true;
		adc_disable();
	}

	mb();
}

void measure_start(void)
{
	adc_trigger_next_chan();
}

void measure_work(void)
{
	bool result_available;

	irq_disable();
	result_available = meas.result_available;
	meas.result_available = false;
	irq_enable();

	if (result_available) {
		measure_handle_result();
		adc_trigger_next_chan();
	}
}

void measure_adjust_set(enum measure_chan chan,
			fixpt_t adjustment)
{
#if CONF_ADJ
	meas.channels[chan].adjustment = adjustment;
#endif
}

fixpt_t measure_adjust_get(enum measure_chan chan)
{
#if CONF_ADJ
	return meas.channels[chan].adjustment;
#else
	return int_to_fixpt(0);
#endif
}

void measure_init(void)
{
	/* Discard the first measurement. */
	adc_trigger(MEAS_MUX_GND, MEAS_PS_64, MEAS_REF_AREF, MEAS_DID_NONE,
		    true, false);
	adc_busywait();
	adc_disable();
}
bues.ch cgit interface