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