altos/stmf0: Re-implement fast ADC code for stmf0
[fw/altos] / src / stmf0 / ao_adc_fast.h
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; 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 #ifndef _AO_ADC_FAST_H_
19 #define _AO_ADC_FAST_H_
20
21 void
22 ao_adc_read(uint16_t *dest, int len);
23
24 void
25 ao_adc_init(void);
26
27 /* Total ring size in samples */
28 #define AO_ADC_RING_SIZE        256
29 /* Number of samples fetched per ao_adc_start call */
30 #define AO_ADC_RING_CHUNK       (AO_ADC_RING_SIZE >> 1)
31
32 extern uint16_t ao_adc_ring[AO_ADC_RING_SIZE];
33
34 #define ao_adc_ring_step(pos,inc)       (((pos) + (inc)) & (AO_ADC_RING_SIZE - 1))
35
36 extern uint16_t ao_adc_ring_head, ao_adc_ring_tail;
37 extern uint8_t  ao_adc_running;
38
39 void
40 _ao_adc_start(void);
41
42 static inline uint16_t
43 _ao_adc_remain(void)
44 {
45         if (ao_adc_ring_tail > ao_adc_ring_head)
46                 return AO_ADC_RING_SIZE - ao_adc_ring_tail;
47         return ao_adc_ring_head - ao_adc_ring_tail;
48 }
49
50 static inline uint16_t
51 _ao_adc_space(void)
52 {
53         if (ao_adc_ring_head == ao_adc_ring_tail)
54                 return AO_ADC_RING_SIZE;
55         if (ao_adc_ring_head > ao_adc_ring_tail)
56                 return AO_ADC_RING_SIZE - ao_adc_ring_head;
57         return ao_adc_ring_tail - ao_adc_ring_head;
58 }
59
60 static inline uint16_t *
61 ao_adc_get(uint16_t n)
62 {
63         if (ao_adc_ring_tail + n > AO_ADC_RING_SIZE)
64                 ao_panic(AO_PANIC_ADC);
65         ao_arch_block_interrupts();
66         while (_ao_adc_remain() < n) {
67                 if (!ao_adc_running)
68                         _ao_adc_start();
69                 ao_sleep(&ao_adc_ring_head);
70         }
71         ao_arch_release_interrupts();
72         return &ao_adc_ring[ao_adc_ring_tail];
73 }
74
75 static inline void
76 ao_adc_ack(uint16_t n)
77 {
78         if (ao_adc_ring_tail + n > AO_ADC_RING_SIZE)
79                 ao_panic(AO_PANIC_ADC);
80         ao_arch_block_interrupts();
81         ao_adc_ring_tail += n;
82         if (ao_adc_ring_tail == AO_ADC_RING_SIZE)
83                 ao_adc_ring_tail = 0;
84         if (!ao_adc_running && _ao_adc_space() >= AO_ADC_RING_CHUNK)
85                 _ao_adc_start();
86         ao_arch_release_interrupts();
87 }
88
89 #endif /* _AO_ADC_FAST_H_ */