altos/test: Adjust CRC error rate after FEC fix
[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 #ifndef AO_LPC_ADC_CLOCK
61 #define AO_LPC_ADC_CLOCK        4500000
62 #endif
63 #define AO_ADC_CLKDIV   (AO_LPC_SYSCLK / AO_LPC_ADC_CLOCK)
64
65 static uint8_t          ao_adc_ready;
66 static uint8_t          ao_adc_sequence;
67
68 static const uint8_t    ao_adc_mask_seq[AO_ADC_NUM] = {
69 #if AO_ADC_0
70         1 << 0,
71 #endif
72 #if AO_ADC_1
73         1 << 1,
74 #endif
75 #if AO_ADC_2
76         1 << 2,
77 #endif
78 #if AO_ADC_3
79         1 << 3,
80 #endif
81 #if AO_ADC_4
82         1 << 4,
83 #endif
84 #if AO_ADC_5
85         1 << 5,
86 #endif
87 #if AO_ADC_6
88         1 << 6,
89 #endif
90 #if AO_ADC_7
91         1 << 7,
92 #endif
93 };
94
95 #define sample(id)      (*out++ = (uint16_t) lpc_adc.dr[id] >> 1)
96
97 static inline void lpc_adc_start(void) {
98         lpc_adc.cr = (((uint32_t) ao_adc_mask_seq[ao_adc_sequence] << LPC_ADC_CR_SEL) |
99                       (AO_ADC_CLKDIV << LPC_ADC_CR_CLKDIV) |
100                       (0 << LPC_ADC_CR_BURST) |
101                       (LPC_ADC_CR_CLKS_11 << LPC_ADC_CR_CLKS) |
102                       (LPC_ADC_CR_START_NOW << LPC_ADC_CR_START));
103 }
104
105 void  lpc_adc_isr(void)
106 {
107         uint16_t        *out;
108
109         /* Store converted value in packet */
110         out = (uint16_t *) &ao_data_ring[ao_data_head].adc;
111         out[ao_adc_sequence] = (uint16_t) lpc_adc.gdr >> 1;
112         if (++ao_adc_sequence < AO_ADC_NUM) {
113                 lpc_adc_start();
114                 return;
115         }
116
117         AO_DATA_PRESENT(AO_DATA_ADC);
118         ao_data_fill(ao_data_head);
119         ao_adc_ready = 1;
120 }
121
122
123 /*
124  * Start the ADC sequence using burst mode
125  */
126 void
127 ao_adc_poll(void)
128 {
129         if (!ao_adc_ready)
130                 return;
131         ao_adc_ready = 0;
132         ao_adc_sequence = 0;
133         lpc_adc_start();
134 }
135
136 static void
137 ao_adc_dump(void) 
138 {
139         struct ao_data  packet;
140 #ifndef AO_ADC_DUMP
141         int16_t *d;
142         uint8_t i;
143 #endif
144
145         ao_data_get(&packet);
146 #ifdef AO_ADC_DUMP
147         AO_ADC_DUMP(&packet);
148 #else
149         printf("tick: %5u",  packet.tick);
150         d = (int16_t *) (&packet.adc);
151         for (i = 0; i < AO_ADC_NUM; i++)
152                 printf (" %2d: %5d", i, d[i]);
153         printf("\n");
154 #endif
155 }
156
157 const struct ao_cmds ao_adc_cmds[] = {
158         { ao_adc_dump,  "a\0Display current ADC values" },
159         { 0, NULL },
160 };
161
162 void
163 ao_adc_init(void)
164 {
165         lpc_scb.sysahbclkctrl |= (1 << LPC_SCB_SYSAHBCLKCTRL_ADC);
166         lpc_scb.pdruncfg &= ~(1UL << LPC_SCB_PDRUNCFG_ADC_PD);
167
168         /* Enable interrupt when channel is complete */
169         lpc_adc.inten = (1 << LPC_ADC_INTEN_ADGINTEN);
170
171         lpc_nvic_set_enable(LPC_ISR_ADC_POS);
172         lpc_nvic_set_priority(LPC_ISR_ADC_POS, AO_LPC_NVIC_CLOCK_PRIORITY);
173 #if AO_ADC_0
174         ao_enable_analog(0, 11, 0);
175 #endif
176 #if AO_ADC_1
177         ao_enable_analog(0, 12, 1);
178 #endif
179 #if AO_ADC_2
180         ao_enable_analog(0, 13, 2);
181 #endif
182 #if AO_ADC_3
183         ao_enable_analog(0, 14, 3);
184 #endif
185 #if AO_ADC_4
186         ao_enable_analog(0, 15, 4);
187 #endif
188 #if AO_ADC_5
189         ao_enable_analog(0, 16, 5);
190 #endif
191 #if AO_ADC_6
192         ao_enable_analog(0, 22, 6);
193 #endif
194 #if AO_ADC_7
195         ao_enable_analog(0, 23, 7);
196 #endif
197
198         lpc_adc.cr = ((0 << LPC_ADC_CR_SEL) |
199                       (AO_ADC_CLKDIV << LPC_ADC_CR_CLKDIV) |
200                       (0 << LPC_ADC_CR_BURST) |
201                       (LPC_ADC_CR_CLKS_11 << LPC_ADC_CR_CLKS));
202
203         ao_cmd_register(&ao_adc_cmds[0]);
204
205         ao_adc_ready = 1;
206 }