altos: use %lu for APRS altitude in sprintf
[fw/altos] / src / cc1111 / ao_dma.c
1 /*
2  * Copyright © 2009 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
21 #define NUM_DMA 5
22
23 /*
24  * The config address for DMA0 is programmed
25  * separately from that of DMA1-4, but for simplicity,
26  * we make them all contiguous.
27  */
28
29 static __xdata struct cc_dma_channel ao_dma_config[NUM_DMA];
30 static __xdata uint8_t * __xdata ao_dma_done[NUM_DMA];
31 static __data uint8_t ao_next_dma;
32
33 uint8_t
34 ao_dma_alloc(__xdata uint8_t *done)
35 {
36         uint8_t id;
37
38         if (ao_next_dma == NUM_DMA)
39                 ao_panic(AO_PANIC_DMA);
40         id = ao_next_dma++;
41         ao_dma_done[id] = done;
42
43         /* When the first dma object is allocated, set up the DMA
44          * controller
45          */
46         if (id == 0) {
47                 DMAIRQ = 0;
48                 DMAIF = 0;
49                 IEN1 |= IEN1_DMAIE;
50                 DMA0CFGH = ((uint16_t) (&ao_dma_config[0])) >> 8;
51                 DMA0CFGL = ((uint16_t) (&ao_dma_config[0]));
52                 DMA1CFGH = ((uint16_t) (&ao_dma_config[1])) >> 8;
53                 DMA1CFGL = ((uint16_t) (&ao_dma_config[1]));
54         }
55
56         return id;
57 }
58
59 void
60 ao_dma_set_transfer(uint8_t id,
61                     void __xdata *srcaddr,
62                     void __xdata *dstaddr,
63                     uint16_t count,
64                     uint8_t cfg0,
65                     uint8_t cfg1)
66 {
67         if (DMAARM & (1 << id))
68                 ao_panic(AO_PANIC_DMA);
69         ao_dma_config[id].src_high = ((uint16_t) srcaddr) >> 8;
70         ao_dma_config[id].src_low = ((uint16_t) srcaddr);
71         ao_dma_config[id].dst_high = ((uint16_t) dstaddr) >> 8;
72         ao_dma_config[id].dst_low = ((uint16_t) dstaddr);
73         ao_dma_config[id].len_high = count >> 8;
74         ao_dma_config[id].len_low = count;
75         ao_dma_config[id].cfg0 = cfg0;
76         ao_dma_config[id].cfg1 = cfg1 | DMA_CFG1_IRQMASK;
77 }
78
79 #define nop()   __asm nop __endasm;
80
81 void
82 ao_dma_start(uint8_t id)
83 {
84         uint8_t mask = (1 << id);
85         DMAIRQ &= ~mask;
86         if (DMAARM & mask) {
87                 DMAARM = 0x80 | mask;
88                 nop(); nop(); nop(); nop();
89                 nop(); nop(); nop(); nop();
90         }
91         *(ao_dma_done[id]) = 0;
92         DMAARM = mask;
93         nop(); nop(); nop(); nop();
94         nop(); nop(); nop(); nop();
95         nop();
96 }
97
98 void
99 ao_dma_trigger(uint8_t id)
100 {
101         DMAREQ |= (1 << id);
102 }
103
104 void
105 ao_dma_abort(uint8_t id)
106 {
107         uint8_t mask = (1 << id);
108         DMAARM = 0x80 | mask;
109         DMAIRQ &= ~mask;
110 }
111
112 void
113 ao_dma_isr(void) __interrupt 8
114 {
115         uint8_t id, mask;
116
117         /* Find the first DMA channel which is done */
118         mask = 1;
119         for (id = 0; id < ao_next_dma; id++) {
120                 if (DMAIRQ & mask) {
121                         /* Clear CPU interrupt flag */
122                         DMAIF = 0;
123                         /* Clear the completed ID */
124                         DMAIRQ = ~mask;
125                         *(ao_dma_done[id]) = 1;
126                         ao_wakeup(ao_dma_done[id]);
127                         break;
128                 }
129                 mask <<= 1;
130         }
131 }