summaryrefslogtreecommitdiffstats
path: root/firmware/adc.c
blob: 320184eb13658cef07a05b50d15d4b7b833d199e (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
/*
 * Simple PWM controller
 * ADC measurement
 *
 * Copyright (c) 2018-2020 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 "compat.h"
#include "adc.h"
#include "util.h"
#include "main.h"
#include "curve.h"
#include "curve_data_adc2sp.h"
#include "pwm.h"
#include "filter.h"
#include "battery.h"


/* ADC configuration. */
#define ADC_HYST		1u		/* ADC hysteresis */
#define ADC_MIN			0u		/* Physical ADC minimum */
#define ADC_MAX			0x3FFu		/* Physical ADC maximum */
#define ADC_VBG_MV		1100u		/* Vbg in millivolts. */

#define ADC_FILTER_SHIFT	9


static struct {
	struct lp_filter filter;
	bool battery_meas;
	uint8_t delay;
	uint8_t prev_pwm_count;
} adc;


/* Configure the ADC hardware.
 * Interrupts must be disabled before calling this function. */
static void adc_configure(bool enable)
{
	/* Disable ADC unit. */
	ADCSRA = 0;
	/* Disable ADC2 digital input */
	DIDR0 = (1 << ADC2D);
	/* Trigger source = free running */
	ADCSRB = (0 << ADTS2) | (0 << ADTS1) | (0 << ADTS0);

	if (enable) {
		if (adc_battery_measurement_running()) {
			/* Ref = Vcc; in = Vbg (1.1V); Right adjust */
			ADMUX = (0 << REFS2) | (0 << REFS1) | (0 << REFS0) |
				(0 << ADLAR) |
				(1 << MUX3) | (1 << MUX2) | (0 << MUX1) | (0 << MUX0);
			/* Enable and start ADC; free running; PS = 64; IRQ enabled */
			ADCSRA = (1 << ADEN) | (1 << ADSC) | (1 << ADATE) |
				 (1 << ADIF) | (1 << ADIE) |
				 (1 << ADPS2) | (1 << ADPS1) | (0 << ADPS0);
			/* Discard the first few results to compensate
			 * for Vbg settling time (1 ms). */
			adc.delay = 10;
		} else if (!battery_voltage_is_critical()) {
			/* Ref = Vcc; in = ADC2/PB4; Right adjust */
			ADMUX = (0 << REFS2) | (0 << REFS1) | (0 << REFS0) |
				(0 << ADLAR) |
				(0 << MUX3) | (0 << MUX2) | (1 << MUX1) | (0 << MUX0);
			/* Enable and start ADC; free running; PS = 64; IRQ enabled */
			ADCSRA = (1 << ADEN) | (1 << ADSC) | (1 << ADATE) |
				 (1 << ADIF) | (1 << ADIE) |
				 (1 << ADPS2) | (1 << ADPS1) | (0 << ADPS0);
		} else {
			/* Battery voltage is critical and no battery measurement
			 * has been requested.
			 * Keep the ADC shut down. */
		}
	}
}

/* Returns true, if a battery measurement conversion is currently running. */
bool adc_battery_measurement_running(void)
{
	return adc.battery_meas && USE_BAT_MONITOR;
}

/* Request a measurement of the battery voltage.
 * Interrupts must be disabled before calling this function. */
void adc_request_battery_measurement(void)
{
	if (USE_BAT_MONITOR) {
		adc.battery_meas = true;
		adc_configure(true);
	}
}

/* ADC conversion complete interrupt service routine */
ISR(ADC_vect)
{
	uint16_t raw_adc;
	uint16_t raw_setpoint;
	uint16_t filt_setpoint;
	uint16_t vcc_mv;
	uint8_t pwm_count;
	bool pwm_collision;

	memory_barrier();

	/* Check if we had a PWM interrupt during our ADC measurement.
	 * This only checks for collisions with the low frequency IRQ mode PWM.
	 * Do this check with interrupts still disabled. */
	pwm_count = pwm_get_irq_count();
	pwm_collision = (pwm_count != adc.prev_pwm_count);
	adc.prev_pwm_count = pwm_count;

	/* Disable the ADC interrupt and
	 * globally enable interrupts.
	 * This allows TIM0_OVF_vect to interrupt us. */
	ADCSRA &= (uint8_t)~(1u << ADIE);
	irq_enable();

	/* Read the analog input */
	raw_adc = ADC;

	if (adc_battery_measurement_running()) {
		/* Battery voltage measurement mode. */

		if (adc.delay == 0u) {
			/* Convert the raw ADC value to millivolts. */
			if (raw_adc > 0u) {
				vcc_mv = lim_u16((((uint32_t)ADC_MAX + 1u) * (uint32_t)ADC_VBG_MV) /
						 (uint32_t)raw_adc);
			} else
				vcc_mv = UINT16_MAX;

			/* Report the measured battery voltage to the
			 * battery voltage logic. */
			evaluate_battery_voltage(vcc_mv);

			/* Disable interrupts for
			 * - PWM shut down
			 * - ADC re-init */
			irq_disable();

			if (battery_voltage_is_critical()) {
				/* Turn the output off immediately.
				 * This also reconfigures the
				 * battery measurement interval. */
				output_setpoint(0u);
			}

			/* We're done.
			 * Turn off battery measurement mode and
			 * return to normal ADC operation mode
			 * (if battery voltage is not critical). */
			adc.battery_meas = false;
			adc_configure(true);
		} else {
			/* VRef/Vbg is not stable, yet.
			 * Continue waiting... */
			adc.delay--;
			irq_disable();
		}
	} else if (!pwm_collision) {
		/* Normal operation mode.
		 * Discard this measurement, if an IRQ controlled
		 * PWM output actuation happened during the measurement. */

		if (ADC_INVERT)
			raw_adc = ADC_MAX - raw_adc;

		/* Transform the value according to the transformation curve. */
		raw_setpoint = curve_interpolate(adc2sp_transformation_curve,
						 ARRAY_SIZE(adc2sp_transformation_curve),
						 raw_adc);

		/* Filter the setpoint value. */
		filt_setpoint = lp_filter_run(&adc.filter,
					      ADC_FILTER_SHIFT,
					      raw_setpoint);

		/* Globally disable interrupts.
		 * TIM0_OVF_vect must not interrupt re-programming of the PWM below. */
		irq_disable();

		/* Change the output signal (PWM). */
		output_setpoint(filt_setpoint);

		if (USE_DEEP_SLEEP) {
			/* If the PWM is disabled, request deep sleep to save power. */
			system_set_standby(raw_setpoint == 0u);
		}
	}

	/* If deep sleep support is disabled, then the watchdog IRQ is also disabled.
	 * Poke the watchdog here. */
	if (!USE_DEEP_SLEEP)
		wdt_reset();

	/* Re-enable the ADC interrupt. */
	ADCSRA |= (1u << ADIE);
	ADCSRA |= (1u << ADIF);

	memory_barrier();
}

/* Initialize the input ADC measurement.
 * Interrupts must be disabled before calling this function. */
void adc_init(bool enable)
{
	lp_filter_reset(&adc.filter);
	adc.prev_pwm_count = pwm_get_irq_count();
	memory_barrier();

	adc_configure(enable);
}
bues.ch cgit interface