e384a28d941056b8a646dac97e60604fcf2cdad3
[fw/altos] / src-avr / ao_adc_avr.c
1 /*
2  * Copyright © 2011 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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include "ao.h"
19
20 volatile __xdata struct ao_adc  ao_adc_ring[AO_ADC_RING];
21 volatile __data uint8_t         ao_adc_head;
22
23 #define ADC0
24 const uint8_t   adc_channels[NUM_ADC] = {
25         0x00,
26         0x01,
27         0x04,
28         0x05,
29         0x06,
30         0x07,
31         0x20,
32         0x21,
33         0x22,
34         0x23,
35         0x24,
36         0x25,
37 };
38
39 static uint8_t  ao_adc_channel;
40
41
42 static uint16_t ao_adc_int_count;
43 static uint16_t ao_adc_int_error;
44
45 #define ADC_CHANNEL_LOW(c)      (((c) & 0x1f) << MUX0)
46 #define ADC_CHANNEL_HIGH(c)     ((((c) & 0x20) >> 5) << MUX5)
47
48 #define ADCSRA_INIT     ((1 << ADEN) |          /* Enable ADC */                \
49                          (0 << ADATE) |         /* No auto ADC trigger */       \
50                          (1 << ADIF) |          /* Clear interrupt */           \
51                          (0 << ADIE) |          /* Enable interrupt */          \
52                          (6 << ADPS0))          /* Prescale clock by 64 */
53
54 #define ADCSRB_INIT     ((0 << ADHSM) |         /* No high-speed mode */ \
55                          (0 << ACME) |          /* Some comparitor thing */ \
56                          (2 << ADTS0))          /* Free running mode (don't care) */
57
58 static void
59 ao_adc_start(void)
60 {
61         uint8_t channel = adc_channels[ao_adc_channel];
62         ADMUX = ((0 << REFS1) |                         /* AVcc reference */
63                  (1 << REFS0) |                         /* AVcc reference */
64                  (1 << ADLAR) |                         /* Left-shift results */
65                  (ADC_CHANNEL_LOW(channel)));           /* Select channel */
66
67         ADCSRB = (ADCSRB_INIT |
68                   ADC_CHANNEL_HIGH(channel));           /* High channel bit */
69
70         ADCSRA = (ADCSRA_INIT |
71                   (1 << ADSC) |
72                   (1 << ADIE));                         /* Start conversion */
73 }
74
75 ISR(ADC_vect)
76 {
77         uint16_t        value;
78         uint8_t channel;
79         ++ao_adc_int_count;
80         channel = (ADMUX & 0x1f) | (ADCSRB & 0x20);
81         if (adc_channels[ao_adc_channel] != channel)
82                 ++ao_adc_int_error;
83         value = ADCL;
84         value |= (ADCH << 8);
85         ao_adc_ring[ao_adc_head].adc[ao_adc_channel] = value;
86         if (++ao_adc_channel < NUM_ADC)
87                 ao_adc_start();
88         else {
89                 ADCSRA = ADCSRA_INIT;
90                 ao_adc_ring[ao_adc_head].tick = ao_time();
91                 ao_adc_head = ao_adc_ring_next(ao_adc_head);
92                 ao_wakeup((void *) &ao_adc_head);
93                 ao_cpu_sleep_disable = 0;
94         }
95 }
96
97 void
98 ao_adc_poll(void)
99 {
100         ao_cpu_sleep_disable = 1;
101         ao_adc_channel = 0;
102         ao_adc_start();
103 }
104
105 void
106 ao_adc_get(__xdata struct ao_adc *packet)
107 {
108         uint8_t i = ao_adc_ring_prev(ao_adc_head);
109         memcpy(packet, (void *) &ao_adc_ring[i], sizeof (struct ao_adc));
110 }
111
112 static void
113 ao_adc_dump(void) __reentrant
114 {
115         static __xdata struct ao_adc    packet;
116         uint8_t i;
117         printf ("interrupts: %u\n", ao_adc_int_count);
118         printf ("errors: %u\n", ao_adc_int_error);
119         ao_adc_get(&packet);
120         printf ("ADMUX %02x ADCSRA %02x ADCSRB %02x\n",
121                 ADMUX, ADCSRA, ADCSRB);
122         printf("tick: %5u",  packet.tick);
123         for (i = 0; i < NUM_ADC; i++)
124                 printf (" %5u", packet.adc[i]);
125         printf ("\n");
126 }
127
128 __code struct ao_cmds ao_adc_cmds[] = {
129         { ao_adc_dump,  "a\0Display current ADC values" },
130         { 0, NULL },
131 };
132
133 void
134 ao_adc_init(void)
135 {
136         DIDR0 = 0xf3;
137         DIDR2 = 0x3f;
138         ADCSRB = ADCSRB_INIT;
139         ADCSRA = ADCSRA_INIT;
140         ao_cmd_register(&ao_adc_cmds[0]);
141 }