summaryrefslogtreecommitdiff
path: root/src/ao_adc.c
blob: cd8550fdfaa61bc4aec471783d4c2fce5cb1832e (plain) (blame)
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
/*
 * Copyright © 2009 Keith Packard <keithp@keithp.com>
 *
 * 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; version 2 of the License.
 *
 * 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.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

#include "ao.h"
#include "ao_pins.h"

volatile __xdata struct ao_adc	ao_adc_ring[AO_ADC_RING];
volatile __data uint8_t		ao_adc_head;

void
ao_adc_poll(void)
{
	ADCCON3 = ADCCON3_EREF_VDD | ADCCON3_EDIV_512 | 0;
}

void
ao_adc_sleep(void)
{
	ao_sleep(&ao_adc_ring);
}

void
ao_adc_get(__xdata struct ao_adc *packet)
{
	uint8_t	i = ao_adc_ring_prev(ao_adc_head);
	memcpy(packet, &ao_adc_ring[i], sizeof (struct ao_adc));
}

#ifdef HAS_ADC_5V
static void
ao_fix_accel(void)
{
	uint32_t	total;
	uint8_t		adc_l, adc_h;
	uint8_t		ref_l, ref_h;
	uint8_t __xdata	*p = (uint8_t __xdata *) &ao_adc_ring[ao_adc_head].accel;

	ref_l = ADCL;
	adc_l = p[0];
	total = (uint32_t) (uint16_t) (adc_l * ref_l);

	adc_h = p[1];
	total += (uint32_t) (uint16_t) (adc_h * ref_l) << 8;

	ref_h = ADCH;
	total += (uint32_t) (uint16_t) (adc_l * ref_h) << 8;
	total += (uint32_t) (uint16_t) (adc_h * ref_h) << 16;
	*(uint16_t __xdata *) p = total >> 16;
}
#endif

void
ao_adc_isr(void) __interrupt 1
{
	uint8_t sequence;
	uint8_t	__xdata *a;

	sequence = (ADCCON2 & ADCCON2_SCH_MASK) >> ADCCON2_SCH_SHIFT;
#if HAS_ADC_5V
	if (sequence == 2) {
		ao_fix_accel();
		/* and launch the temp sensor next */
		ADCCON3 = ADCCON3_EREF_1_25 | ADCCON3_EDIV_512 | ADCCON3_ECH_TEMP;
		return;
	}
#endif
#if HAS_EXTERNAL_TEMP
	if (sequence == ADCCON3_ECH_TEMP)
		sequence = 2;
#endif
	a = (uint8_t __xdata *) (&ao_adc_ring[ao_adc_head].accel + sequence);
	a[0] = ADCL;
	a[1] = ADCH;
	if (sequence < 5) {
#if !HAS_EXTERNAL_TEMP && !HAS_ADC_5V
		/* start next channel conversion */
		/* v0.2 replaces external temp sensor with internal one */
		if (sequence == 1)
			ADCCON3 = ADCCON3_EREF_1_25 | ADCCON3_EDIV_512 | ADCCON3_ECH_TEMP;
		else
#endif
			ADCCON3 = ADCCON3_EREF_VDD | ADCCON3_EDIV_512 | (sequence + 1);
	} else {
		/* record this conversion series */
		ao_adc_ring[ao_adc_head].tick = ao_time();
		ao_adc_head = ao_adc_ring_next(ao_adc_head);
		ao_wakeup(ao_adc_ring);
	}
}

static void
ao_adc_dump(void) __reentrant
{
	static __xdata struct ao_adc	packet;
	ao_adc_get(&packet);
	printf("tick: %5u accel: %5d pres: %5d temp: %5d batt: %5d drogue: %5d main: %5d\n",
	       packet.tick, packet.accel, packet.pres, packet.temp,
	       packet.v_batt, packet.sense_d, packet.sense_m);
}

__code struct ao_cmds ao_adc_cmds[] = {
	{ 'a',	ao_adc_dump,	"a                                  Display current ADC values" },
	{ 0,	ao_adc_dump, NULL },
};

void
ao_adc_init(void)
{
	ADCCFG = ((1 << 0) |	/* acceleration */
		  (1 << 1) |	/* pressure */
#if HAS_EXTERNAL_TEMP
		  (1 << 2) |	/* v0.1 temperature */
#endif
		  (1 << 3) |	/* battery voltage */
		  (1 << 4) |	/* drogue sense */
		  (1 << 5));	/* main sense */

	/* enable interrupts */
	ADCIF = 0;
	IEN0 |= IEN0_ADCIE;
	ao_cmd_register(&ao_adc_cmds[0]);
}