2 * Copyright © 2015 Keith Packard <keithp@keithp.com>
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.
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.
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.
19 #ifndef _AO_ADC_FAST_H_
20 #define _AO_ADC_FAST_H_
23 ao_adc_read(uint16_t *dest, int len);
28 /* Total ring size in samples */
29 #define AO_ADC_RING_SIZE 1024
31 extern uint16_t ao_adc_ring[AO_ADC_RING_SIZE] __attribute__((aligned(4)));
33 #define ao_adc_ring_step(pos,inc) (((pos) + (inc)) & (AO_ADC_RING_SIZE - 1))
35 extern uint16_t ao_adc_ring_head, ao_adc_ring_remain;
36 extern uint16_t ao_adc_running;
39 * Place to start fetching values from
41 static inline uint16_t
42 ao_adc_ring_tail(void)
44 return (ao_adc_ring_head - ao_adc_ring_remain) & (AO_ADC_RING_SIZE - 1);
51 * Space available to write ADC values into
53 static inline uint16_t
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;
60 /* no, return just the unused entries beyond head */
61 return AO_ADC_RING_SIZE - ao_adc_ring_remain;
64 static inline uint16_t
65 ao_adc_get(uint16_t n)
67 ao_arch_block_interrupts();
68 while (ao_adc_ring_remain < n) {
71 ao_sleep(&ao_adc_ring_head);
73 ao_arch_release_interrupts();
74 return ao_adc_ring_tail();
78 ao_adc_ack(uint16_t n)
80 ao_arch_block_interrupts();
81 ao_adc_ring_remain -= n;
84 ao_arch_release_interrupts();
87 #endif /* _AO_ADC_FAST_H_ */