]> git.gag.com Git - fw/altos/blob - src/stm/ao_adc_stm.c
doc: Add a bunch of Altos Droid material to the docs
[fw/altos] / src / stm / ao_adc_stm.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; 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 #include <ao_data.h>
20
21 static uint8_t                  ao_adc_ready;
22
23 #define AO_ADC_CR2_VAL          ((0 << STM_ADC_CR2_SWSTART) |           \
24                                  (STM_ADC_CR2_EXTEN_DISABLE << STM_ADC_CR2_EXTEN) | \
25                                  (0 << STM_ADC_CR2_EXTSEL) |            \
26                                  (0 << STM_ADC_CR2_JWSTART) |           \
27                                  (STM_ADC_CR2_JEXTEN_DISABLE << STM_ADC_CR2_JEXTEN) | \
28                                  (0 << STM_ADC_CR2_JEXTSEL) |           \
29                                  (0 << STM_ADC_CR2_ALIGN) |             \
30                                  (0 << STM_ADC_CR2_EOCS) |              \
31                                  (1 << STM_ADC_CR2_DDS) |               \
32                                  (1 << STM_ADC_CR2_DMA) |               \
33                                  (STM_ADC_CR2_DELS_UNTIL_READ << STM_ADC_CR2_DELS) | \
34                                  (0 << STM_ADC_CR2_CONT) |              \
35                                  (1 << STM_ADC_CR2_ADON))
36
37 /*
38  * Callback from DMA ISR
39  *
40  * Mark time in ring, shut down DMA engine
41  */
42 static void ao_adc_done(int index)
43 {
44         AO_DATA_PRESENT(AO_DATA_ADC);
45         ao_dma_done_transfer(STM_DMA_INDEX(STM_DMA_CHANNEL_ADC1));
46         if (ao_data_present == AO_DATA_ALL) {
47 #if HAS_MS5607
48                 ao_data_ring[ao_data_head].ms5607_raw = ao_ms5607_current;
49 #endif
50 #if HAS_MMA655X
51                 ao_data_ring[ao_data_head].mma655x = ao_mma655x_current;
52 #endif
53 #if HAS_HMC5883
54                 ao_data_ring[ao_data_head].hmc5883 = ao_hmc5883_current;
55 #endif
56 #if HAS_MPU6000
57                 ao_data_ring[ao_data_head].mpu6000 = ao_mpu6000_current;
58 #endif
59                 ao_data_ring[ao_data_head].tick = ao_tick_count;
60                 ao_data_head = ao_data_ring_next(ao_data_head);
61                 ao_wakeup((void *) &ao_data_head);
62         }
63         ao_adc_ready = 1;
64 }
65
66 /*
67  * Start the ADC sequence using the DMA engine
68  */
69 void
70 ao_adc_poll(void)
71 {
72         if (!ao_adc_ready)
73                 return;
74         ao_adc_ready = 0;
75         stm_adc.sr = 0;
76         ao_dma_set_transfer(STM_DMA_INDEX(STM_DMA_CHANNEL_ADC1),
77                             &stm_adc.dr,
78                             (void *) (&ao_data_ring[ao_data_head].adc),
79                             AO_NUM_ADC,
80                             (0 << STM_DMA_CCR_MEM2MEM) |
81                             (STM_DMA_CCR_PL_HIGH << STM_DMA_CCR_PL) |
82                             (STM_DMA_CCR_MSIZE_16 << STM_DMA_CCR_MSIZE) |
83                             (STM_DMA_CCR_PSIZE_16 << STM_DMA_CCR_PSIZE) |
84                             (1 << STM_DMA_CCR_MINC) |
85                             (0 << STM_DMA_CCR_PINC) |
86                             (0 << STM_DMA_CCR_CIRC) |
87                             (STM_DMA_CCR_DIR_PER_TO_MEM << STM_DMA_CCR_DIR));
88         ao_dma_set_isr(STM_DMA_INDEX(STM_DMA_CHANNEL_ADC1), ao_adc_done);
89         ao_dma_start(STM_DMA_INDEX(STM_DMA_CHANNEL_ADC1));
90
91         stm_adc.cr2 = AO_ADC_CR2_VAL | (1 << STM_ADC_CR2_SWSTART);
92 }
93
94 /*
95  * Fetch a copy of the most recent ADC data
96  */
97 void
98 ao_adc_get(__xdata struct ao_adc *packet)
99 {
100 #if HAS_FLIGHT
101         uint8_t i = ao_data_ring_prev(ao_sample_data);
102 #else
103         uint8_t i = ao_data_ring_prev(ao_data_head);
104 #endif
105         memcpy(packet, (void *) &ao_data_ring[i].adc, sizeof (struct ao_adc));
106 }
107
108 static void
109 ao_adc_dump(void) __reentrant
110 {
111         struct ao_data  packet;
112         int16_t *d;
113         uint8_t i;
114
115         ao_data_get(&packet);
116         printf("tick: %5u",  packet.tick);
117         d = (int16_t *) (&packet.adc);
118         for (i = 0; i < AO_NUM_ADC; i++)
119                 printf (" %2d: %5d", i, d[i]);
120         printf("\n");
121 }
122
123 __code struct ao_cmds ao_adc_cmds[] = {
124         { ao_adc_dump,  "a\0Display current ADC values" },
125         { 0, NULL },
126 };
127
128 void
129 ao_adc_init(void)
130 {
131 #ifdef AO_ADC_PIN0_PORT
132         stm_rcc.ahbenr |= AO_ADC_RCC_AHBENR;
133 #endif
134
135 #ifdef AO_ADC_PIN0_PORT
136         stm_moder_set(AO_ADC_PIN0_PORT, AO_ADC_PIN0_PIN, STM_MODER_ANALOG);
137 #endif
138 #ifdef AO_ADC_PIN1_PORT
139         stm_moder_set(AO_ADC_PIN1_PORT, AO_ADC_PIN1_PIN, STM_MODER_ANALOG);
140 #endif
141 #ifdef AO_ADC_PIN2_PORT
142         stm_moder_set(AO_ADC_PIN2_PORT, AO_ADC_PIN2_PIN, STM_MODER_ANALOG);
143 #endif
144 #ifdef AO_ADC_PIN3_PORT
145         stm_moder_set(AO_ADC_PIN3_PORT, AO_ADC_PIN3_PIN, STM_MODER_ANALOG);
146 #endif
147 #ifdef AO_ADC_PIN4_PORT
148         stm_moder_set(AO_ADC_PIN4_PORT, AO_ADC_PIN4_PIN, STM_MODER_ANALOG);
149 #endif
150 #ifdef AO_ADC_PIN5_PORT
151         stm_moder_set(AO_ADC_PIN5_PORT, AO_ADC_PIN5_PIN, STM_MODER_ANALOG);
152 #endif
153 #ifdef AO_ADC_PIN6_PORT
154         stm_moder_set(AO_ADC_PIN6_PORT, AO_ADC_PIN6_PIN, STM_MODER_ANALOG);
155 #endif
156 #ifdef AO_ADC_PIN7_PORT
157         stm_moder_set(AO_ADC_PIN7_PORT, AO_ADC_PIN7_PIN, STM_MODER_ANALOG);
158 #endif
159 #ifdef AO_ADC_PIN8_PORT
160         stm_moder_set(AO_ADC_PIN8_PORT, AO_ADC_PIN8_PIN, STM_MODER_ANALOG);
161 #endif
162 #ifdef AO_ADC_PIN9_PORT
163         stm_moder_set(AO_ADC_PIN9_PORT, AO_ADC_PIN9_PIN, STM_MODER_ANALOG);
164 #endif
165 #ifdef AO_ADC_PIN10_PORT
166         stm_moder_set(AO_ADC_PIN10_PORT, AO_ADC_PIN10_PIN, STM_MODER_ANALOG);
167 #endif
168 #ifdef AO_ADC_PIN11_PORT
169         stm_moder_set(AO_ADC_PIN11_PORT, AO_ADC_PIN11_PIN, STM_MODER_ANALOG);
170 #endif
171 #ifdef AO_ADC_PIN12_PORT
172         stm_moder_set(AO_ADC_PIN12_PORT, AO_ADC_PIN12_PIN, STM_MODER_ANALOG);
173 #endif
174
175         stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_ADC1EN);
176
177         /* Turn off ADC during configuration */
178         stm_adc.cr2 = 0;
179
180         stm_adc.cr1 = ((0 << STM_ADC_CR1_OVRIE ) |
181                        (STM_ADC_CR1_RES_12 << STM_ADC_CR1_RES ) |
182                        (0 << STM_ADC_CR1_AWDEN ) |
183                        (0 << STM_ADC_CR1_JAWDEN ) |
184                        (0 << STM_ADC_CR1_PDI ) |
185                        (0 << STM_ADC_CR1_PDD ) |
186                        (0 << STM_ADC_CR1_DISCNUM ) |
187                        (0 << STM_ADC_CR1_JDISCEN ) |
188                        (0 << STM_ADC_CR1_DISCEN ) |
189                        (0 << STM_ADC_CR1_JAUTO ) |
190                        (0 << STM_ADC_CR1_AWDSGL ) |
191                        (1 << STM_ADC_CR1_SCAN ) |
192                        (0 << STM_ADC_CR1_JEOCIE ) |
193                        (0 << STM_ADC_CR1_AWDIE ) |
194                        (0 << STM_ADC_CR1_EOCIE ) |
195                        (0 << STM_ADC_CR1_AWDCH ));
196
197         /* 384 cycle sample time for everyone */
198         stm_adc.smpr1 = 0x3ffff;
199         stm_adc.smpr2 = 0x3fffffff;
200         stm_adc.smpr3 = 0x3fffffff;
201
202         stm_adc.sqr1 = ((AO_NUM_ADC - 1) << 20);
203         stm_adc.sqr2 = 0;
204         stm_adc.sqr3 = 0;
205         stm_adc.sqr4 = 0;
206         stm_adc.sqr5 = 0;
207 #if AO_NUM_ADC > 0
208         stm_adc.sqr5 |= (AO_ADC_SQ1 << 0);
209 #endif
210 #if AO_NUM_ADC > 1
211         stm_adc.sqr5 |= (AO_ADC_SQ2 << 5);
212 #endif
213 #if AO_NUM_ADC > 2
214         stm_adc.sqr5 |= (AO_ADC_SQ3 << 10);
215 #endif
216 #if AO_NUM_ADC > 3
217         stm_adc.sqr5 |= (AO_ADC_SQ4 << 15);
218 #endif
219 #if AO_NUM_ADC > 4
220         stm_adc.sqr5 |= (AO_ADC_SQ5 << 20);
221 #endif
222 #if AO_NUM_ADC > 5
223         stm_adc.sqr5 |= (AO_ADC_SQ6 << 25);
224 #endif
225 #if AO_NUM_ADC > 6
226         stm_adc.sqr4 |= (AO_ADC_SQ7 << 0);
227 #endif
228 #if AO_NUM_ADC > 7
229         stm_adc.sqr4 |= (AO_ADC_SQ8 << 5);
230 #endif
231 #if AO_NUM_ADC > 8
232         stm_adc.sqr4 |= (AO_ADC_SQ9 << 10);
233 #endif
234 #if AO_NUM_ADC > 9
235         stm_adc.sqr4 |= (AO_ADC_SQ10 << 15);
236 #endif
237 #if AO_NUM_ADC > 10
238         stm_adc.sqr4 |= (AO_ADC_SQ11 << 20);
239 #endif
240 #if AO_NUM_ADC > 11
241         stm_adc.sqr4 |= (AO_ADC_SQ12 << 25);
242 #endif
243 #if AO_NUM_ADC > 12
244 #error "need to finish stm_adc.sqr settings"
245 #endif
246         
247         /* Turn ADC on */
248         stm_adc.cr2 = AO_ADC_CR2_VAL;
249
250         /* Wait for ADC to be ready */
251         while (!(stm_adc.sr & (1 << STM_ADC_SR_ADONS)))
252                 ;
253
254 #if HAS_ADC_TEMP
255         stm_adc.ccr = ((1 << STM_ADC_CCR_TSVREFE));
256 #else
257         stm_adc.ccr = 0;
258 #endif
259         /* Clear any stale status bits */
260         stm_adc.sr = 0;
261
262         ao_dma_alloc(STM_DMA_INDEX(STM_DMA_CHANNEL_ADC1));
263
264         ao_cmd_register(&ao_adc_cmds[0]);
265
266         ao_adc_ready = 1;
267 }