altos: Remove 8051 address space specifiers
[fw/altos] / src / lpc / ao_adc_lpc.c
1 /*
2  * Copyright © 2012 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 #include <ao_data.h>
21
22 #ifndef AO_ADC_0
23 #define AO_ADC_0        0
24 #endif
25
26 #ifndef AO_ADC_1
27 #define AO_ADC_1        0
28 #endif
29
30 #ifndef AO_ADC_2
31 #define AO_ADC_2        0
32 #endif
33
34 #ifndef AO_ADC_3
35 #define AO_ADC_3        0
36 #endif
37
38 #ifndef AO_ADC_4
39 #define AO_ADC_4        0
40 #endif
41
42 #ifndef AO_ADC_5
43 #define AO_ADC_5        0
44 #endif
45
46 #ifndef AO_ADC_6
47 #define AO_ADC_6        0
48 #endif
49
50 #ifndef AO_ADC_7
51 #define AO_ADC_7        0
52 #endif
53
54 #define AO_ADC_NUM      (AO_ADC_0 + AO_ADC_1 + AO_ADC_2 + AO_ADC_3 + \
55                          AO_ADC_4 + AO_ADC_5 + AO_ADC_6 + AO_ADC_7)
56
57 /* ADC clock is divided by this value + 1, which ensures that
58  * the ADC clock will be strictly less than 4.5MHz as required
59  */
60 #define AO_ADC_CLKDIV   (AO_LPC_SYSCLK / 450000)
61
62 static uint8_t          ao_adc_ready;
63 static uint8_t          ao_adc_sequence;
64
65 static const uint8_t    ao_adc_mask_seq[AO_ADC_NUM] = {
66 #if AO_ADC_0
67         1 << 0,
68 #endif
69 #if AO_ADC_1
70         1 << 1,
71 #endif
72 #if AO_ADC_2
73         1 << 2,
74 #endif
75 #if AO_ADC_3
76         1 << 3,
77 #endif
78 #if AO_ADC_4
79         1 << 4,
80 #endif
81 #if AO_ADC_5
82         1 << 6,
83 #endif
84 #if AO_ADC_6
85         1 << 6,
86 #endif
87 #if AO_ADC_7
88         1 << 7,
89 #endif
90 };
91
92 #define sample(id)      (*out++ = (uint16_t) lpc_adc.dr[id] >> 1)
93
94 static inline void lpc_adc_start(void) {
95         lpc_adc.cr = ((ao_adc_mask_seq[ao_adc_sequence] << LPC_ADC_CR_SEL) |
96                       (AO_ADC_CLKDIV << LPC_ADC_CR_CLKDIV) |
97                       (0 << LPC_ADC_CR_BURST) |
98                       (LPC_ADC_CR_CLKS_11 << LPC_ADC_CR_CLKS) |
99                       (LPC_ADC_CR_START_NOW << LPC_ADC_CR_START));
100 }
101
102 void  lpc_adc_isr(void)
103 {
104         uint16_t        *out;
105
106         /* Store converted value in packet */
107         out = (uint16_t *) &ao_data_ring[ao_data_head].adc;
108         out[ao_adc_sequence] = (uint16_t) lpc_adc.gdr >> 1;
109         if (++ao_adc_sequence < AO_ADC_NUM) {
110                 lpc_adc_start();
111                 return;
112         }
113
114         AO_DATA_PRESENT(AO_DATA_ADC);
115         if (ao_data_present == AO_DATA_ALL) {
116 #if HAS_MS5607
117                 ao_data_ring[ao_data_head].ms5607_raw = ao_ms5607_current;
118 #endif
119 #if HAS_MMA655X
120                 ao_data_ring[ao_data_head].mma655x = ao_mma655x_current;
121 #endif
122 #if HAS_HMC5883
123                 ao_data_ring[ao_data_head].hmc5883 = ao_hmc5883_current;
124 #endif
125 #if HAS_MPU6000
126                 ao_data_ring[ao_data_head].mpu6000 = ao_mpu6000_current;
127 #endif
128                 ao_data_ring[ao_data_head].tick = ao_tick_count;
129                 ao_data_head = ao_data_ring_next(ao_data_head);
130                 ao_wakeup((void *) &ao_data_head);
131         }
132         ao_adc_ready = 1;
133 }
134
135
136 /*
137  * Start the ADC sequence using burst mode
138  */
139 void
140 ao_adc_poll(void)
141 {
142         if (!ao_adc_ready)
143                 return;
144         ao_adc_ready = 0;
145         ao_adc_sequence = 0;
146         lpc_adc_start();
147 }
148
149 static void
150 ao_adc_dump(void) 
151 {
152         struct ao_data  packet;
153 #ifndef AO_ADC_DUMP
154         int16_t *d;
155         uint8_t i;
156 #endif
157
158         ao_data_get(&packet);
159 #ifdef AO_ADC_DUMP
160         AO_ADC_DUMP(&packet);
161 #else
162         printf("tick: %5u",  packet.tick);
163         d = (int16_t *) (&packet.adc);
164         for (i = 0; i < AO_ADC_NUM; i++)
165                 printf (" %2d: %5d", i, d[i]);
166         printf("\n");
167 #endif
168 }
169
170 const struct ao_cmds ao_adc_cmds[] = {
171         { ao_adc_dump,  "a\0Display current ADC values" },
172         { 0, NULL },
173 };
174
175 void
176 ao_adc_init(void)
177 {
178         lpc_scb.sysahbclkctrl |= (1 << LPC_SCB_SYSAHBCLKCTRL_ADC);
179         lpc_scb.pdruncfg &= ~(1 << LPC_SCB_PDRUNCFG_ADC_PD);
180
181         /* Enable interrupt when channel is complete */
182         lpc_adc.inten = (1 << LPC_ADC_INTEN_ADGINTEN);
183
184         lpc_nvic_set_enable(LPC_ISR_ADC_POS);
185         lpc_nvic_set_priority(LPC_ISR_ADC_POS, AO_LPC_NVIC_CLOCK_PRIORITY);
186 #if AO_ADC_0
187         ao_enable_analog(0, 11, 0);
188 #endif
189 #if AO_ADC_1
190         ao_enable_analog(0, 12, 1);
191 #endif
192 #if AO_ADC_2
193         ao_enable_analog(0, 13, 2);
194 #endif
195 #if AO_ADC_3
196         ao_enable_analog(0, 14, 3);
197 #endif
198 #if AO_ADC_4
199         ao_enable_analog(0, 15, 4);
200 #endif
201 #if AO_ADC_5
202         ao_enable_analog(0, 16, 5);
203 #endif
204 #if AO_ADC_6
205         ao_enable_analog(0, 22, 6);
206 #endif
207 #if AO_ADC_7
208         ao_enable_analog(0, 23, 7);
209 #endif
210
211         lpc_adc.cr = ((0 << LPC_ADC_CR_SEL) |
212                       (AO_ADC_CLKDIV << LPC_ADC_CR_CLKDIV) |
213                       (0 << LPC_ADC_CR_BURST) |
214                       (LPC_ADC_CR_CLKS_11 << LPC_ADC_CR_CLKS));
215
216         ao_cmd_register(&ao_adc_cmds[0]);
217
218         ao_adc_ready = 1;
219 }