Switch from GPLv2 to GPLv2+
[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; 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 #ifndef _AO_ADC_FAST_H_
20 #define _AO_ADC_FAST_H_
21
22 void
23 ao_adc_read(uint16_t *dest, int len);
24
25 void
26 ao_adc_init(void);
27
28 /* Total ring size in samples */
29 #define AO_ADC_RING_SIZE        256
30
31 extern uint16_t ao_adc_ring[AO_ADC_RING_SIZE];
32
33 #define ao_adc_ring_step(pos,inc)       (((pos) + (inc)) & (AO_ADC_RING_SIZE - 1))
34
35 extern uint16_t ao_adc_ring_head, ao_adc_ring_remain;
36 extern uint16_t ao_adc_running;
37
38 /*
39  * Place to start fetching values from
40  */
41 static inline uint16_t
42 ao_adc_ring_tail(void)
43 {
44         return (ao_adc_ring_head - ao_adc_ring_remain) & (AO_ADC_RING_SIZE - 1);
45 }
46
47 void
48 _ao_adc_start(void);
49
50 /*
51  * Space available to write ADC values into
52  */
53 static inline uint16_t
54 _ao_adc_space(void)
55 {
56         /* Free to end of buffer? */
57         if (ao_adc_ring_remain <= ao_adc_ring_head)
58                 return AO_ADC_RING_SIZE - ao_adc_ring_head;
59
60         /* no, return just the unused entries beyond head */
61         return AO_ADC_RING_SIZE - ao_adc_ring_remain;
62 }
63
64 static inline uint16_t
65 ao_adc_get(uint16_t n)
66 {
67         ao_arch_block_interrupts();
68         while (ao_adc_ring_remain < n) {
69                 if (!ao_adc_running)
70                         _ao_adc_start();
71                 ao_sleep(&ao_adc_ring_head);
72         }
73         ao_arch_release_interrupts();
74         return ao_adc_ring_tail();
75 }
76
77 static inline void
78 ao_adc_ack(uint16_t n)
79 {
80         ao_arch_block_interrupts();
81         ao_adc_ring_remain -= n;
82         if (!ao_adc_running)
83                 _ao_adc_start();
84         ao_arch_release_interrupts();
85 }
86
87 #endif /* _AO_ADC_FAST_H_ */