2 * Copyright © 2012 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.
23 struct ao_dma_config {
24 void (*isr)(int index);
27 uint8_t ao_dma_done[NUM_DMA];
29 static struct ao_dma_config ao_dma_config[NUM_DMA];
30 static uint8_t ao_dma_allocated[NUM_DMA];
31 static uint8_t ao_dma_mutex[NUM_DMA];
34 ao_dma_isr(uint8_t index) {
35 /* Get channel interrupt bits */
36 uint32_t isr = stm_dma.isr & (STM_DMA_ISR_MASK <<
41 if (ao_dma_config[index].isr)
42 (*ao_dma_config[index].isr)(index);
44 ao_dma_done[index] = 1;
45 ao_wakeup(&ao_dma_done[index]);
49 void stm_dma1_channel1_isr(void) { ao_dma_isr(STM_DMA_INDEX(1)); }
50 void stm_dma1_channel2_isr(void) { ao_dma_isr(STM_DMA_INDEX(2)); }
51 #ifdef STM_DMA1_3_STOLEN
54 void stm_dma1_channel3_isr(void) { ao_dma_isr(STM_DMA_INDEX(3)); }
56 void stm_dma1_channel4_isr(void) { ao_dma_isr(STM_DMA_INDEX(4)); }
57 #ifdef STM_DMA1_5_STOLEN
60 void stm_dma1_channel5_isr(void) { ao_dma_isr(STM_DMA_INDEX(5)); }
62 void stm_dma1_channel6_isr(void) { ao_dma_isr(STM_DMA_INDEX(6)); }
63 void stm_dma1_channel7_isr(void) { ao_dma_isr(STM_DMA_INDEX(7)); }
66 static uint8_t ao_dma_active;
70 ao_dma_set_transfer(uint8_t index,
71 volatile void *peripheral,
76 if (ao_dma_allocated[index]) {
77 if (ao_dma_mutex[index])
78 ao_panic(AO_PANIC_DMA);
79 ao_dma_mutex[index] = 0xff;
81 ao_mutex_get(&ao_dma_mutex[index]);
84 if (ao_dma_active++ == 0)
85 stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_DMA1EN);
88 stm_dma.channel[index].ccr = ccr | (1 << STM_DMA_CCR_TCIE);
89 stm_dma.channel[index].cndtr = count;
90 stm_dma.channel[index].cpar = peripheral;
91 stm_dma.channel[index].cmar = memory;
92 ao_dma_config[index].isr = NULL;
96 ao_dma_set_isr(uint8_t index, void (*isr)(int))
98 ao_dma_config[index].isr = isr;
102 ao_dma_start(uint8_t index)
104 ao_dma_done[index] = 0;
105 stm_dma.channel[index].ccr |= (1 << STM_DMA_CCR_EN);
109 ao_dma_done_transfer(uint8_t index)
111 stm_dma.channel[index].ccr &= ~(1 << STM_DMA_CCR_EN);
114 if (--ao_dma_active == 0)
115 stm_rcc.ahbenr &= ~(1 << STM_RCC_AHBENR_DMA1EN);
118 if (ao_dma_allocated[index])
119 ao_dma_mutex[index] = 0;
121 ao_mutex_put(&ao_dma_mutex[index]);
125 ao_dma_alloc(uint8_t index)
127 if (ao_dma_allocated[index])
128 ao_panic(AO_PANIC_DMA);
129 ao_dma_allocated[index] = 1;
134 ao_dma_dump_cmd(void)
140 if (ao_dma_active++ == 0)
141 stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_DMA1EN);
144 printf ("isr %08x ifcr%08x\n", stm_dma.isr, stm_dma.ifcr);
145 for (i = 0; i < NUM_DMA; i++)
146 printf("%d: done %d allocated %d mutex %2d ccr %04x cndtr %04x cpar %08x cmar %08x isr %08x\n",
151 stm_dma.channel[i].ccr,
152 stm_dma.channel[i].cndtr,
153 stm_dma.channel[i].cpar,
154 stm_dma.channel[i].cmar,
155 ao_dma_config[i].isr);
158 if (--ao_dma_active == 0)
159 stm_rcc.ahbenr &= ~(1 << STM_RCC_AHBENR_DMA1EN);
164 static const struct ao_cmds ao_dma_cmds[] = {
165 { ao_dma_dump_cmd, "D\0Dump DMA status" },
176 stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_DMA1EN);
178 for (index = 0; index < STM_NUM_DMA; index++) {
179 #if STM_DMA1_5_STOLEN
180 if (index == STM_DMA_INDEX(5)) {
181 ao_dma_allocated[index] = 1;
182 ao_dma_mutex[index] = 0xff;
186 #if STM_DMA1_3_STOLEN
187 if (index == STM_DMA_INDEX(3)) {
188 ao_dma_allocated[index] = 1;
189 ao_dma_mutex[index] = 0xff;
193 stm_nvic_set_enable(STM_ISR_DMA1_CHANNEL1_POS + index);
194 stm_nvic_set_priority(STM_ISR_DMA1_CHANNEL1_POS + index,
195 AO_STM_NVIC_MED_PRIORITY);
196 ao_dma_allocated[index] = 0;
197 ao_dma_mutex[index] = 0;
200 ao_cmd_register(&ao_dma_cmds[0]);