Switch from GPLv2 to GPLv2+
[fw/altos] / src / stmf0 / ao_adc_fast.c
1 /*
2  * Copyright © 2015 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_adc_fast.h>
21
22 uint16_t ao_adc_ring[AO_ADC_RING_SIZE] __attribute__((aligned(4)));
23
24 /* Maximum number of samples fetched per _ao_adc_start call */
25 #define AO_ADC_RING_CHUNK       (AO_ADC_RING_SIZE >> 1)
26
27 uint16_t ao_adc_ring_head, ao_adc_ring_remain;
28 uint16_t ao_adc_running;
29
30 /*
31  * Callback from DMA ISR
32  *
33  * Wakeup any waiting processes, mark the DMA as done, start the ADC
34  * if there's still lots of space in the ring
35  */
36 static void ao_adc_dma_done(int index)
37 {
38         (void) index;
39         ao_adc_ring_head += ao_adc_running;
40         ao_adc_ring_remain += ao_adc_running;
41         if (ao_adc_ring_head == AO_ADC_RING_SIZE)
42                 ao_adc_ring_head = 0;
43         ao_adc_running = 0;
44         ao_wakeup(&ao_adc_ring_head);
45         ao_dma_done_transfer(STM_DMA_INDEX(STM_DMA_CHANNEL_ADC_1));
46         _ao_adc_start();
47 }
48
49 void
50 _ao_adc_start(void)
51 {
52         uint16_t        *buf;
53         uint16_t        count;
54
55         if (ao_adc_running)
56                 return;
57         count = _ao_adc_space();
58         if (count == 0)
59                 return;
60         if (count > AO_ADC_RING_CHUNK)
61                 count = AO_ADC_RING_CHUNK;
62         ao_adc_running = count;
63         buf = ao_adc_ring + ao_adc_ring_head;
64         stm_adc.isr = 0;
65         ao_dma_set_transfer(STM_DMA_INDEX(STM_DMA_CHANNEL_ADC_1),
66                             &stm_adc.dr,
67                             buf,
68                             count,
69                             (0 << STM_DMA_CCR_MEM2MEM) |
70                             (STM_DMA_CCR_PL_HIGH << STM_DMA_CCR_PL) |
71                             (STM_DMA_CCR_MSIZE_16 << STM_DMA_CCR_MSIZE) |
72                             (STM_DMA_CCR_PSIZE_16 << STM_DMA_CCR_PSIZE) |
73                             (1 << STM_DMA_CCR_MINC) |
74                             (0 << STM_DMA_CCR_PINC) |
75                             (0 << STM_DMA_CCR_CIRC) |
76                             (STM_DMA_CCR_DIR_PER_TO_MEM << STM_DMA_CCR_DIR) |
77                             (1 << STM_DMA_CCR_TCIE));
78
79         ao_dma_set_isr(STM_DMA_INDEX(STM_DMA_CHANNEL_ADC_1), ao_adc_dma_done);
80         ao_dma_start(STM_DMA_INDEX(STM_DMA_CHANNEL_ADC_1));
81
82         stm_adc.cr |= (1 << STM_ADC_CR_ADSTART);
83 }
84
85 void
86 ao_adc_init(void)
87 {
88         uint32_t        chselr;
89
90         /* Reset ADC */
91         stm_rcc.apb2rstr |= (1 << STM_RCC_APB2RSTR_ADCRST);
92         stm_rcc.apb2rstr &= ~(1 << STM_RCC_APB2RSTR_ADCRST);
93
94         /* Turn on ADC pins */
95         stm_rcc.ahbenr |= AO_ADC_RCC_AHBENR;
96
97 #ifdef AO_ADC_PIN0_PORT
98         stm_moder_set(AO_ADC_PIN0_PORT, AO_ADC_PIN0_PIN, STM_MODER_ANALOG);
99 #endif
100 #ifdef AO_ADC_PIN1_PORT
101         stm_moder_set(AO_ADC_PIN1_PORT, AO_ADC_PIN1_PIN, STM_MODER_ANALOG);
102 #endif
103 #ifdef AO_ADC_PIN2_PORT
104         stm_moder_set(AO_ADC_PIN2_PORT, AO_ADC_PIN2_PIN, STM_MODER_ANALOG);
105 #endif
106 #ifdef AO_ADC_PIN3_PORT
107         stm_moder_set(AO_ADC_PIN3_PORT, AO_ADC_PIN3_PIN, STM_MODER_ANALOG);
108 #endif
109 #ifdef AO_ADC_PIN4_PORT
110         stm_moder_set(AO_ADC_PIN4_PORT, AO_ADC_PIN4_PIN, STM_MODER_ANALOG);
111 #endif
112 #ifdef AO_ADC_PIN5_PORT
113         stm_moder_set(AO_ADC_PIN5_PORT, AO_ADC_PIN5_PIN, STM_MODER_ANALOG);
114 #endif
115 #ifdef AO_ADC_PIN6_PORT
116         stm_moder_set(AO_ADC_PIN6_PORT, AO_ADC_PIN6_PIN, STM_MODER_ANALOG);
117 #endif
118 #ifdef AO_ADC_PIN7_PORT
119         stm_moder_set(AO_ADC_PIN7_PORT, AO_ADC_PIN7_PIN, STM_MODER_ANALOG);
120 #endif
121 #ifdef AO_ADC_PIN24_PORT
122         #error "Too many ADC ports"
123 #endif
124
125         stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_ADCEN);
126
127         chselr = 0;
128 #if AO_NUM_ADC > 0
129         chselr |= (1 << AO_ADC_PIN0_CH);
130 #endif
131 #if AO_NUM_ADC > 1
132         chselr |= (1 << AO_ADC_PIN1_CH);
133 #endif
134 #if AO_NUM_ADC > 2
135         chselr |= (1 << AO_ADC_PIN2_CH);
136 #endif
137 #if AO_NUM_ADC > 3
138         chselr |= (1 << AO_ADC_PIN3_CH);
139 #endif
140 #if AO_NUM_ADC > 4
141         chselr |= (1 << AO_ADC_PIN4_CH);
142 #endif
143 #if AO_NUM_ADC > 5
144         chselr |= (1 << AO_ADC_PIN5_CH);
145 #endif
146 #if AO_NUM_ADC > 6
147         chselr |= (1 << AO_ADC_PIN6_CH);
148 #endif
149 #if AO_NUM_ADC > 7
150         chselr |= (1 << AO_ADC_PIN7_CH);
151 #endif
152 #if AO_NUM_ADC > 8
153 #error Need more ADC defines
154 #endif
155
156         /* Set the clock */
157         stm_adc.cfgr2 = STM_ADC_CFGR2_CKMODE_PCLK_2 << STM_ADC_CFGR2_CKMODE;
158
159         /* Shortest sample time */
160         stm_adc.smpr = STM_ADC_SMPR_SMP_1_5 << STM_ADC_SMPR_SMP;
161
162         /* Turn off enable and start */
163         stm_adc.cr &= ~((1 << STM_ADC_CR_ADEN) | (1 << STM_ADC_CR_ADSTART));
164
165         /* Calibrate */
166         stm_adc.cr |= (1 << STM_ADC_CR_ADCAL);
167         while ((stm_adc.cr & (1 << STM_ADC_CR_ADCAL)) != 0)
168                 ;
169
170         /* Enable */
171         stm_adc.cr |= (1 << STM_ADC_CR_ADEN);
172         while ((stm_adc.isr & (1 << STM_ADC_ISR_ADRDY)) == 0)
173                 ;
174
175         stm_adc.chselr = chselr;
176
177         stm_adc.cfgr1 = ((0 << STM_ADC_CFGR1_AWDCH) |
178                          (0 << STM_ADC_CFGR1_AWDEN) |
179                          (0 << STM_ADC_CFGR1_AWDSGL) |
180                          (0 << STM_ADC_CFGR1_DISCEN) |
181                          (0 << STM_ADC_CFGR1_AUTOOFF) |
182                          (0 << STM_ADC_CFGR1_WAIT) |
183                          (1 << STM_ADC_CFGR1_CONT) |
184                          (1 << STM_ADC_CFGR1_OVRMOD) |
185                          (STM_ADC_CFGR1_EXTEN_DISABLE << STM_ADC_CFGR1_EXTEN) |
186                          (0 << STM_ADC_CFGR1_ALIGN) |
187                          (STM_ADC_CFGR1_RES_12 << STM_ADC_CFGR1_RES) |
188                          (STM_ADC_CFGR1_SCANDIR_UP << STM_ADC_CFGR1_SCANDIR) |
189                          (STM_ADC_CFGR1_DMACFG_ONESHOT << STM_ADC_CFGR1_DMACFG) |
190                          (1 << STM_ADC_CFGR1_DMAEN));
191         stm_adc.ccr = 0;
192
193         /* Clear any stale status bits */
194         stm_adc.isr = 0;
195
196         /* Turn on syscfg */
197         stm_rcc.apb2enr |= (1 << STM_RCC_APB2ENR_SYSCFGCOMPEN);
198
199         /* Set ADC to use DMA channel 1 (option 1) */
200         stm_syscfg.cfgr1 &= ~(1 << STM_SYSCFG_CFGR1_ADC_DMA_RMP);
201
202         ao_dma_alloc(STM_DMA_INDEX(STM_DMA_CHANNEL_ADC_1));
203 }