94af5cda433ba710c16d13beafc2e884cf5d66e6
[fw/altos] / src / cc1111 / ao_adc.c
1 /*
2  * Copyright © 2009 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include "ao.h"
20
21 volatile __xdata struct ao_data ao_data_ring[AO_DATA_RING];
22 volatile __data uint8_t         ao_data_head;
23 #if (AO_DATA_ALL & ~(AO_DATA_ADC))
24 volatile __data uint8_t         ao_data_present;
25 #endif
26
27 #ifdef TELENANO_V_0_1
28 # define AO_ADC_FIRST_PIN       1
29 #endif
30
31 #if HAS_ACCEL_REF
32 # define AO_ADC_FIRST_PIN       2
33 #endif
34
35 #ifndef AO_ADC_FIRST_PIN
36 # define AO_ADC_FIRST_PIN       0
37 #endif
38
39 void
40 ao_adc_poll(void)
41 {
42         ADCCON3 = ADCCON3_EREF_VDD | ADCCON3_EDIV_512 | AO_ADC_FIRST_PIN;
43 }
44
45 void
46 ao_data_get(__xdata struct ao_data *packet)
47 {
48 #if HAS_FLIGHT
49         uint8_t i = ao_data_ring_prev(ao_sample_data);
50 #else
51         uint8_t i = ao_data_ring_prev(ao_data_head);
52 #endif
53         ao_xmemcpy(packet, (void __xdata *) &ao_data_ring[i], sizeof (struct ao_data));
54 }
55
56 void
57 ao_adc_isr(void) __interrupt 1
58 {
59         uint8_t sequence;
60         uint8_t __xdata *a;
61
62         sequence = (ADCCON2 & ADCCON2_SCH_MASK) >> ADCCON2_SCH_SHIFT;
63 #if TELEMETRUM_V_0_1 || TELEMETRUM_V_0_2 || TELEMETRUM_V_1_0 || TELEMETRUM_V_1_1 || TELEMETRUM_V_1_2 || TELELAUNCH_V_0_1 || TELEBALLOON_V_1_1
64         /* TeleMetrum readings */
65 #if HAS_ACCEL_REF
66         if (sequence == 2) {
67                 a = (uint8_t __xdata *) (&ao_data_ring[ao_data_head].adc.accel_ref);
68                 sequence = 0;
69         } else
70 #endif
71         {
72                 if (sequence == ADCCON3_ECH_TEMP)
73                         sequence = 2;
74                 a = (uint8_t __xdata *) (&ao_data_ring[ao_data_head].adc.accel + sequence);
75                 sequence++;
76         }
77 #define GOT_ADC
78         a[0] = ADCL;
79         a[1] = ADCH;
80         if (sequence < 6) {
81 #if HAS_EXTERNAL_TEMP == 0
82                 /* start next channel conversion */
83                 /* v0.2 replaces external temp sensor with internal one */
84                 if (sequence == 2)
85                         ADCCON3 = ADCCON3_EREF_1_25 | ADCCON3_EDIV_512 | ADCCON3_ECH_TEMP;
86                 else
87 #endif
88                         ADCCON3 = ADCCON3_EREF_VDD | ADCCON3_EDIV_512 | sequence;
89                 return;
90         }
91 #endif
92
93 #if TELEMINI_V_1_0 || TELENANO_V_0_1
94         /* TeleMini readings */
95         a = (uint8_t __xdata *) (&ao_data_ring[ao_data_head].adc.pres);
96 #if TELEMINI_V_1_0
97         switch (sequence) {
98         case 0:
99                 /* pressure */
100                 a += 0;
101                 sequence = ADCCON3_EREF_VDD | ADCCON3_EDIV_512 | 1;
102                 break;
103         case 1:
104                 /* drogue sense */
105                 a += 6;
106                 sequence = ADCCON3_EREF_VDD | ADCCON3_EDIV_512 | 2;
107                 break;
108         case 2:
109                 /* main sense */
110                 a += 8;
111                 sequence = ADCCON3_EREF_VDD | ADCCON3_EDIV_512 | 3;
112                 break;
113         case 3:
114                 /* battery */
115                 a += 4;
116                 sequence = ADCCON3_EREF_1_25 | ADCCON3_EDIV_512 | ADCCON3_ECH_TEMP;
117                 break;
118         case ADCCON3_ECH_TEMP:
119                 a += 2;
120                 sequence = 0;
121                 break;
122         }
123 #define GOT_ADC
124 #endif
125 #ifdef TELENANO_V_0_1
126         switch (sequence) {
127         case 1:
128                 /* pressure */
129                 a += 0;
130                 sequence = ADCCON3_EREF_VDD | ADCCON3_EDIV_512 | 3;
131                 break;
132         case 3:
133                 /* battery */
134                 a += 4;
135                 sequence = ADCCON3_EREF_1_25 | ADCCON3_EDIV_512 | ADCCON3_ECH_TEMP;
136                 break;
137         case ADCCON3_ECH_TEMP:
138                 a += 2;
139                 sequence = 0;
140                 break;
141         }
142 #define GOT_ADC
143 #endif
144         a[0] = ADCL;
145         a[1] = ADCH;
146         if (sequence) {
147                 /* Start next conversion */
148                 ADCCON3 = sequence;
149                 return;
150         }
151 #endif /* telemini || telenano */
152
153 #if defined(TELEFIRE_V_0_1) || defined(TELEFIRE_V_0_2)
154         a = (uint8_t __xdata *) (&ao_data_ring[ao_data_head].adc.sense[0] + sequence - AO_ADC_FIRST_PIN);
155         a[0] = ADCL;
156         a[1] = ADCH;
157         if (sequence < 5) {
158                 ADCCON3 = ADCCON3_EREF_VDD | ADCCON3_EDIV_512 | (sequence + 1);
159                 return;
160         }
161 #define GOT_ADC
162 #endif /* TELEFIRE_V_0_1 */
163
164 #ifdef TELEBT_V_1_0
165         a = (uint8_t __xdata *) (&ao_data_ring[ao_data_head].adc.batt);
166         a[0] = ADCL;
167         a[1] = ADCH;
168 #define GOT_ADC
169 #endif  
170
171 #ifdef FETCH_ADC
172         FETCH_ADC();
173 #define GOT_ADC
174 #endif
175
176 #ifndef GOT_ADC
177 #error No known ADC configuration set
178 #endif
179
180         /* record this conversion series */
181         ao_data_ring[ao_data_head].tick = ao_time();
182         ao_data_head = ao_data_ring_next(ao_data_head);
183         ao_wakeup(DATA_TO_XDATA(&ao_data_head));
184 }
185
186 static void
187 ao_adc_dump(void) __reentrant
188 {
189         static __xdata struct ao_data   packet;
190         ao_data_get(&packet);
191 #ifndef AO_ADC_DUMP
192         printf("tick: %5u accel: %5d pres: %5d temp: %5d batt: %5d drogue: %5d main: %5d\n",
193                packet.tick, packet.adc.accel, packet.adc.pres, packet.adc.temp,
194                packet.adc.v_batt, packet.adc.sense_d, packet.adc.sense_m);
195 #else
196         AO_ADC_DUMP(&packet);
197 #endif
198 }
199
200 __code struct ao_cmds ao_adc_cmds[] = {
201         { ao_adc_dump,  "a\0Current ADC" },
202         { 0, NULL },
203 };
204
205 void
206 ao_adc_init(void)
207 {
208 #ifdef AO_ADC_PINS
209         ADCCFG = AO_ADC_PINS;
210
211 #else
212
213 #if IGNITE_ON_P2
214         /* TeleMetrum configuration */
215         ADCCFG = ((1 << 0) |    /* acceleration */
216                   (1 << 1) |    /* pressure */
217 #if HAS_EXTERNAL_TEMP
218                   (1 << 2) |    /* v0.1 temperature */
219 #endif
220                   (1 << 3) |    /* battery voltage */
221                   (1 << 4) |    /* drogue sense */
222                   (1 << 5));    /* main sense */
223 #endif
224
225 #if IGNITE_ON_P0
226         /* TeleMini configuration */
227         ADCCFG = ((1 << 0) |    /* pressure */
228                   (1 << 1) |    /* drogue sense */
229                   (1 << 2) |    /* main sense */
230                   (1 << 3));    /* battery voltage */
231 #endif
232
233 #endif /* else AO_ADC_PINS */
234
235         /* enable interrupts */
236         ADCIF = 0;
237         IEN0 |= IEN0_ADCIE;
238         ao_cmd_register(&ao_adc_cmds[0]);
239 }