altos/stm32f1: Add ao_adc_single support
[fw/altos] / src / stm32f1 / ao_adc_single_stm.c
1 /*
2  * Copyright © 2024 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 #include <ao_adc_single.h>
22
23 static uint8_t                  ao_adc_ready;
24
25 #define AO_ADC_CR2_VAL          ((HAS_ADC_TEMP << STM_ADC_CR2_TSVREF) | \
26                                  (0 << STM_ADC_CR2_SWSTART) |           \
27                                  (0 << STM_ADC_CR2_JWSTART) |           \
28                                  (0 << STM_ADC_CR2_EXTTRIG) |           \
29                                  (0 << STM_ADC_CR2_EXTSEL) |            \
30                                  (0 << STM_ADC_CR2_JEXTTRIG) | \
31                                  (0 << STM_ADC_CR2_JEXTSEL) |           \
32                                  (0 << STM_ADC_CR2_ALIGN) |             \
33                                  (1 << STM_ADC_CR2_DMA) |               \
34                                  (0 << STM_ADC_CR2_CONT) |              \
35                                  (1 << STM_ADC_CR2_ADON))
36
37 /*
38  * Callback from DMA ISR
39  *
40  * Shut down DMA engine, signal anyone waiting
41  */
42 static void ao_adc_done(int index)
43 {
44         (void) index;
45         ao_dma_done_transfer(STM_DMA_INDEX(STM_DMA_CHANNEL_ADC1));
46         ao_adc_ready = 1;
47         ao_wakeup((void *) &ao_adc_ready);
48 }
49
50 /*
51  * Start the ADC sequence using the DMA engine
52  */
53 static void
54 ao_adc_poll(struct ao_adc *packet)
55 {
56         ao_adc_ready = 0;
57         stm_adc.sr = 0;
58         ao_dma_set_transfer(STM_DMA_INDEX(STM_DMA_CHANNEL_ADC1),
59                             &stm_adc.dr,
60                             (void *) packet,
61                             AO_NUM_ADC,
62                             (0 << STM_DMA_CCR_MEM2MEM) |
63                             (STM_DMA_CCR_PL_HIGH << STM_DMA_CCR_PL) |
64                             (STM_DMA_CCR_MSIZE_16 << STM_DMA_CCR_MSIZE) |
65                             (STM_DMA_CCR_PSIZE_16 << STM_DMA_CCR_PSIZE) |
66                             (1 << STM_DMA_CCR_MINC) |
67                             (0 << STM_DMA_CCR_PINC) |
68                             (0 << STM_DMA_CCR_CIRC) |
69                             (STM_DMA_CCR_DIR_PER_TO_MEM << STM_DMA_CCR_DIR));
70         ao_dma_set_isr(STM_DMA_INDEX(STM_DMA_CHANNEL_ADC1), ao_adc_done);
71         ao_dma_start(STM_DMA_INDEX(STM_DMA_CHANNEL_ADC1));
72
73         stm_adc.cr2 = AO_ADC_CR2_VAL | (1 << STM_ADC_CR2_SWSTART);
74 }
75
76 /*
77  * Fetch a copy of the most recent ADC data
78  */
79 void
80 ao_adc_single_get(struct ao_adc *packet)
81 {
82         ao_adc_poll(packet);
83         ao_arch_block_interrupts();
84         while (!ao_adc_ready)
85                 ao_sleep(&ao_adc_ready);
86         ao_arch_release_interrupts();
87 }
88
89 static void
90 ao_adc_dump(void)
91 {
92         struct ao_adc   packet;
93         ao_adc_single_get(&packet);
94         AO_ADC_DUMP(&packet);
95 }
96
97 const struct ao_cmds ao_adc_cmds[] = {
98         { ao_adc_dump,  "a\0Display current ADC values" },
99         { 0, NULL },
100 };
101
102 static inline void
103 adc_pin_set(struct stm_gpio *gpio, int pin)
104 {
105         ao_enable_port(gpio);
106         stm_gpio_conf(gpio, pin,
107                       STM_GPIO_CR_MODE_INPUT,
108                       STM_GPIO_CR_CNF_INPUT_ANALOG);
109 }
110
111 void
112 ao_adc_single_init(void)
113 {
114 #ifdef AO_ADC_PIN0_PORT
115         adc_pin_set(AO_ADC_PIN0_PORT, AO_ADC_PIN0_PIN);
116 #endif
117 #ifdef AO_ADC_PIN1_PORT
118         adc_pin_set(AO_ADC_PIN1_PORT, AO_ADC_PIN1_PIN);
119 #endif
120 #ifdef AO_ADC_PIN2_PORT
121         adc_pin_set(AO_ADC_PIN2_PORT, AO_ADC_PIN2_PIN);
122 #endif
123 #ifdef AO_ADC_PIN3_PORT
124         adc_pin_set(AO_ADC_PIN3_PORT, AO_ADC_PIN3_PIN);
125 #endif
126 #ifdef AO_ADC_PIN4_PORT
127         adc_pin_set(AO_ADC_PIN4_PORT, AO_ADC_PIN4_PIN);
128 #endif
129 #ifdef AO_ADC_PIN5_PORT
130         adc_pin_set(AO_ADC_PIN5_PORT, AO_ADC_PIN5_PIN);
131 #endif
132 #ifdef AO_ADC_PIN6_PORT
133         adc_pin_set(AO_ADC_PIN6_PORT, AO_ADC_PIN6_PIN);
134 #endif
135 #ifdef AO_ADC_PIN7_PORT
136         adc_pin_set(AO_ADC_PIN7_PORT, AO_ADC_PIN7_PIN);
137 #endif
138 #ifdef AO_ADC_PIN8_PORT
139         adc_pin_set(AO_ADC_PIN8_PORT, AO_ADC_PIN8_PIN);
140 #endif
141 #ifdef AO_ADC_PIN9_PORT
142         adc_pin_set(AO_ADC_PIN9_PORT, AO_ADC_PIN9_PIN);
143 #endif
144 #ifdef AO_ADC_PIN10_PORT
145         adc_pin_set(AO_ADC_PIN10_PORT, AO_ADC_PIN10_PIN);
146 #endif
147 #ifdef AO_ADC_PIN11_PORT
148         adc_pin_set(AO_ADC_PIN11_PORT, AO_ADC_PIN11_PIN);
149 #endif
150 #ifdef AO_ADC_PIN12_PORT
151         adc_pin_set(AO_ADC_PIN12_PORT, AO_ADC_PIN12_PIN);
152 #endif
153 #ifdef AO_ADC_PIN13_PORT
154         adc_pin_set(AO_ADC_PIN13_PORT, AO_ADC_PIN13_PIN);
155 #endif
156 #ifdef AO_ADC_PIN14_PORT
157         adc_pin_set(AO_ADC_PIN14_PORT, AO_ADC_PIN14_PIN);
158 #endif
159 #ifdef AO_ADC_PIN15_PORT
160         adc_pin_set(AO_ADC_PIN15_PORT, AO_ADC_PIN15_PIN);
161 #endif
162 #ifdef AO_ADC_PIN16_PORT
163         adc_pin_set(AO_ADC_PIN16_PORT, AO_ADC_PIN16_PIN);
164 #endif
165 #ifdef AO_ADC_PIN17_PORT
166         adc_pin_set(AO_ADC_PIN17_PORT, AO_ADC_PIN17_PIN);
167 #endif
168 #ifdef AO_ADC_PIN18_PORT
169         adc_pin_set(AO_ADC_PIN18_PORT, AO_ADC_PIN18_PIN);
170 #endif
171 #ifdef AO_ADC_PIN19_PORT
172         adc_pin_set(AO_ADC_PIN19_PORT, AO_ADC_PIN19_PIN);
173 #endif
174 #ifdef AO_ADC_PIN20_PORT
175         adc_pin_set(AO_ADC_PIN20_PORT, AO_ADC_PIN20_PIN);
176 #endif
177 #ifdef AO_ADC_PIN21_PORT
178         adc_pin_set(AO_ADC_PIN21_PORT, AO_ADC_PIN21_PIN);
179 #endif
180 #ifdef AO_ADC_PIN22_PORT
181         adc_pin_set(AO_ADC_PIN22_PORT, AO_ADC_PIN22_PIN);
182 #endif
183 #ifdef AO_ADC_PIN23_PORT
184         adc_pin_set(AO_ADC_PIN23_PORT, AO_ADC_PIN23_PIN);
185 #endif
186 #ifdef AO_ADC_PIN24_PORT
187         #error "Too many ADC ports"
188 #endif
189
190         stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_ADC1EN);
191
192         /* Turn off ADC during configuration */
193         stm_adc.cr2 = 0;
194
195         stm_adc.cr1 = ((0 << STM_ADC_CR1_AWDEN ) |
196                        (0 << STM_ADC_CR1_JAWDEN ) |
197                        (STM_ADC_CR1_DUALMOD_INDEPENDENT << STM_ADC_CR1_DUALMOD ) |
198                        (0 << STM_ADC_CR1_DISCNUM ) |
199                        (0 << STM_ADC_CR1_JDISCEN ) |
200                        (0 << STM_ADC_CR1_DISCEN ) |
201                        (0 << STM_ADC_CR1_JAUTO ) |
202                        (0 << STM_ADC_CR1_AWDSGL ) |
203                        (1 << STM_ADC_CR1_SCAN ) |
204                        (0 << STM_ADC_CR1_JEOCIE ) |
205                        (0 << STM_ADC_CR1_AWDIE ) |
206                        (0 << STM_ADC_CR1_EOCIE ) |
207                        (0 << STM_ADC_CR1_AWDCH ));
208
209         /* 384 cycle sample time for everyone */
210         stm_adc.smpr1 = 0x3ffff;
211         stm_adc.smpr2 = 0x3fffffff;
212
213         stm_adc.sqr1 = ((AO_NUM_ADC - 1) << 20);
214 #if AO_NUM_ADC > 0
215         stm_adc.sqr3 |= (AO_ADC_SQ1 << 0);
216 #endif
217 #if AO_NUM_ADC > 1
218         stm_adc.sqr3 |= (AO_ADC_SQ2 << 5);
219 #endif
220 #if AO_NUM_ADC > 2
221         stm_adc.sqr3 |= (AO_ADC_SQ3 << 10);
222 #endif
223 #if AO_NUM_ADC > 3
224         stm_adc.sqr3 |= (AO_ADC_SQ4 << 15);
225 #endif
226 #if AO_NUM_ADC > 4
227         stm_adc.sqr3 |= (AO_ADC_SQ5 << 20);
228 #endif
229 #if AO_NUM_ADC > 5
230         stm_adc.sqr3 |= (AO_ADC_SQ6 << 25);
231 #endif
232 #if AO_NUM_ADC > 6
233         stm_adc.sqr2 |= (AO_ADC_SQ7 << 0);
234 #endif
235 #if AO_NUM_ADC > 7
236         stm_adc.sqr2 |= (AO_ADC_SQ8 << 5);
237 #endif
238 #if AO_NUM_ADC > 8
239         stm_adc.sqr2 |= (AO_ADC_SQ9 << 10);
240 #endif
241 #if AO_NUM_ADC > 9
242         stm_adc.sqr2 |= (AO_ADC_SQ10 << 15);
243 #endif
244 #if AO_NUM_ADC > 10
245         stm_adc.sqr2 |= (AO_ADC_SQ11 << 20);
246 #endif
247 #if AO_NUM_ADC > 11
248         stm_adc.sqr2 |= (AO_ADC_SQ12 << 25);
249 #endif
250 #if AO_NUM_ADC > 12
251         stm_adc.sqr1 |= (AO_ADC_SQ13 << 0);
252 #endif
253 #if AO_NUM_ADC > 13
254         stm_adc.sqr1 |= (AO_ADC_SQ14 << 5);
255 #endif
256 #if AO_NUM_ADC > 14
257         stm_adc.sqr1 |= (AO_ADC_SQ15 << 10);
258 #endif
259 #if AO_NUM_ADC > 15
260         stm_adc.sqr1 |= (AO_ADC_SQ16 << 15);
261 #endif
262 #if AO_NUM_ADC > 15
263 #error "too many ADC channels"
264 #endif
265
266 #ifndef HAS_ADC_TEMP
267 #error Please define HAS_ADC_TEMP
268 #endif
269 #if HAS_ADC_TEMP
270         stm_adc.cr2 |= ((1 << STM_ADC_CR2_TSVREFE));
271 #endif
272
273         /* Clear any stale status bits */
274         stm_adc.sr = 0;
275
276         ao_dma_alloc(STM_DMA_INDEX(STM_DMA_CHANNEL_ADC1));
277
278         ao_cmd_register(&ao_adc_cmds[0]);
279 }