|
| 1 | +/****************************************************************************** |
| 2 | + * The MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2010 Michael Hope. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person |
| 7 | + * obtaining a copy of this software and associated documentation |
| 8 | + * files (the "Software"), to deal in the Software without |
| 9 | + * restriction, including without limitation the rights to use, copy, |
| 10 | + * modify, merge, publish, distribute, sublicense, and/or sell copies |
| 11 | + * of the Software, and to permit persons to whom the Software is |
| 12 | + * furnished to do so, subject to the following conditions: |
| 13 | + * |
| 14 | + * The above copyright notice and this permission notice shall be |
| 15 | + * included in all copies or substantial portions of the Software. |
| 16 | + * |
| 17 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 | + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 | + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 20 | + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 21 | + * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 22 | + * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 23 | + * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | + * SOFTWARE. |
| 25 | + *****************************************************************************/ |
| 26 | + |
| 27 | +#ifdef STM32F4 |
| 28 | + |
| 29 | +/** |
| 30 | + * @file dmaF4.c |
| 31 | + * @brief Direct Memory Access peripheral support |
| 32 | + */ |
| 33 | + |
| 34 | +#include "dma.h" |
| 35 | +#include "bitband.h" |
| 36 | +#include "util.h" |
| 37 | + |
| 38 | +/* |
| 39 | + * Devices |
| 40 | + */ |
| 41 | + |
| 42 | +static dma_dev dma1 = { |
| 43 | + .regs = DMA1_BASE, |
| 44 | + .clk_id = RCC_DMA1, |
| 45 | + .handlers = {{ .handler = NULL, .irq_line = 11 }, |
| 46 | + { .handler = NULL, .irq_line = 12 }, |
| 47 | + { .handler = NULL, .irq_line = 13 }, |
| 48 | + { .handler = NULL, .irq_line = 14 }, |
| 49 | + { .handler = NULL, .irq_line = 15 }, |
| 50 | + { .handler = NULL, .irq_line = 16 }, |
| 51 | + { .handler = NULL, .irq_line = 17 }, |
| 52 | + { .handler = NULL, .irq_line = 47 }} |
| 53 | +}; |
| 54 | +/** DMA1 device */ |
| 55 | +dma_dev *DMA1 = &dma1; |
| 56 | + |
| 57 | +static dma_dev dma2 = { |
| 58 | + .regs = DMA2_BASE, |
| 59 | + .clk_id = RCC_DMA2, |
| 60 | + .handlers = {{ .handler = NULL, .irq_line = 56 }, |
| 61 | + { .handler = NULL, .irq_line = 57 }, |
| 62 | + { .handler = NULL, .irq_line = 58 }, |
| 63 | + { .handler = NULL, .irq_line = 59 }, |
| 64 | + { .handler = NULL, .irq_line = 60 }, |
| 65 | + { .handler = NULL, .irq_line = 68 }, |
| 66 | + { .handler = NULL, .irq_line = 69 }, |
| 67 | + { .handler = NULL, .irq_line = 70 }} /* !@#$ */ |
| 68 | +}; |
| 69 | +/** DMA2 device */ |
| 70 | +dma_dev *DMA2 = &dma2; |
| 71 | + |
| 72 | + |
| 73 | +/* |
| 74 | + * Convenience routines |
| 75 | + */ |
| 76 | + |
| 77 | +/** |
| 78 | + * @brief Initialize a DMA device. |
| 79 | + * @param dev Device to initialize. |
| 80 | + */ |
| 81 | +void dma_init(dma_dev *dev) { |
| 82 | + rcc_clk_enable(dev->clk_id); |
| 83 | +} |
| 84 | + |
| 85 | +/** |
| 86 | + * @brief Attach an interrupt to a DMA transfer. |
| 87 | + * |
| 88 | + * Interrupts are enabled using appropriate mode flags in |
| 89 | + * dma_setup_transfer(). |
| 90 | + * |
| 91 | + * @param dev DMA device |
| 92 | + * @param stream Stream to attach handler to |
| 93 | + * @param handler Interrupt handler to call when channel interrupt fires. |
| 94 | + * @see dma_setup_transfer() |
| 95 | + * @see dma_detach_interrupt() |
| 96 | + */ |
| 97 | +void dma_attach_interrupt(dma_dev *dev, |
| 98 | + dma_stream stream, |
| 99 | + void (*handler)(void)) { |
| 100 | + dev->handlers[stream].handler = handler; |
| 101 | + nvic_irq_enable(dev->handlers[stream].irq_line); |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + * @brief Detach a DMA transfer interrupt handler. |
| 106 | + * |
| 107 | + * After calling this function, the given channel's interrupts will be |
| 108 | + * disabled. |
| 109 | + * |
| 110 | + * @param dev DMA device |
| 111 | + * @param stream Stream whose handler to detach |
| 112 | + * @sideeffect Clears interrupt enable bits in the channel's CCR register. |
| 113 | + * @see dma_attach_interrupt() |
| 114 | + */ |
| 115 | +void dma_detach_interrupt(dma_dev *dev, dma_stream stream) { |
| 116 | + nvic_irq_disable(dev->handlers[stream].irq_line); |
| 117 | + dev->handlers[stream].handler = NULL; |
| 118 | +} |
| 119 | + |
| 120 | +void dma_clear_isr_bits(dma_dev *dev, dma_stream stream) { |
| 121 | + switch (stream) { |
| 122 | + case 0: |
| 123 | + dev->regs->LIFCR|=0x0000003d; |
| 124 | + break; |
| 125 | + case 1: |
| 126 | + dev->regs->LIFCR|=0x00000f40; |
| 127 | + break; |
| 128 | + case 2: |
| 129 | + dev->regs->LIFCR|=0x003d0000; |
| 130 | + break; |
| 131 | + case 3: |
| 132 | + dev->regs->LIFCR|=0x0f400000; |
| 133 | + break; |
| 134 | + case 4: |
| 135 | + dev->regs->HIFCR|=0x0000003d; |
| 136 | + break; |
| 137 | + case 5: |
| 138 | + dev->regs->HIFCR|=0x00000f40; |
| 139 | + break; |
| 140 | + case 6: |
| 141 | + dev->regs->HIFCR|=0x003d0000; |
| 142 | + break; |
| 143 | + case 7: |
| 144 | + dev->regs->HIFCR|=0x0f400000; |
| 145 | + break; |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +/* |
| 150 | + * IRQ handlers |
| 151 | + */ |
| 152 | + |
| 153 | +static inline void dispatch_handler(dma_dev *dev, dma_stream stream) { |
| 154 | + void (*handler)(void) = dev->handlers[stream].handler; |
| 155 | + if (handler) { |
| 156 | + handler(); |
| 157 | + dma_clear_isr_bits(dev, stream); /* in case handler doesn't */ |
| 158 | + } |
| 159 | +} |
| 160 | + |
| 161 | +//void __irq_dma1_stream0(void) { |
| 162 | +void __irq_dma1_channel1(void) { |
| 163 | + dispatch_handler(DMA1, DMA_STREAM0); |
| 164 | +} |
| 165 | + |
| 166 | +//void __irq_dma1_stream1(void) { |
| 167 | +void __irq_dma1_channel2(void) { |
| 168 | + dispatch_handler(DMA1, DMA_STREAM1); |
| 169 | +} |
| 170 | + |
| 171 | +//void __irq_dma1_stream2(void) { |
| 172 | +void __irq_dma1_channel3(void) { |
| 173 | + dispatch_handler(DMA1, DMA_STREAM2); |
| 174 | +} |
| 175 | + |
| 176 | +//void __irq_dma1_stream3(void) { |
| 177 | +void __irq_dma1_channel4(void) { |
| 178 | + dispatch_handler(DMA1, DMA_STREAM3); |
| 179 | +} |
| 180 | + |
| 181 | +//void __irq_dma1_stream4(void) { |
| 182 | +void __irq_dma1_channel5(void) { |
| 183 | + dispatch_handler(DMA1, DMA_STREAM4); |
| 184 | +} |
| 185 | + |
| 186 | +//void __irq_dma1_stream5(void) { |
| 187 | +void __irq_dma1_channel6(void) { |
| 188 | + dispatch_handler(DMA1, DMA_STREAM5); |
| 189 | +} |
| 190 | + |
| 191 | +//void __irq_dma1_stream6(void) { |
| 192 | +void __irq_dma1_channel7(void) { |
| 193 | + dispatch_handler(DMA1, DMA_STREAM6); |
| 194 | +} |
| 195 | + |
| 196 | +//void __irq_dma1_stream7(void) { |
| 197 | +void __irq_adc3(void) { |
| 198 | + dispatch_handler(DMA1, DMA_STREAM7); |
| 199 | +} |
| 200 | + |
| 201 | +//void __irq_dma2_stream0(void) { |
| 202 | +void __irq_dma2_channel1(void) { |
| 203 | + dispatch_handler(DMA2, DMA_STREAM0); |
| 204 | +} |
| 205 | + |
| 206 | +//void __irq_dma2_stream1(void) { |
| 207 | +void __irq_dma2_channel2(void) { |
| 208 | + dispatch_handler(DMA2, DMA_STREAM1); |
| 209 | +} |
| 210 | + |
| 211 | +//void __irq_dma2_stream2(void) { |
| 212 | +void __irq_dma2_channel3(void) { |
| 213 | + dispatch_handler(DMA2, DMA_STREAM2); |
| 214 | +} |
| 215 | + |
| 216 | +//void __irq_dma2_stream3(void) { |
| 217 | +void __irq_dma2_channel4_5(void) { |
| 218 | + dispatch_handler(DMA2, DMA_STREAM3); |
| 219 | +} |
| 220 | + |
| 221 | +//void __irq_dma2_stream4(void) { |
| 222 | +void __irq_DMA2_Stream4_IRQHandler(void) { |
| 223 | + dispatch_handler(DMA2, DMA_STREAM4); |
| 224 | +} |
| 225 | + |
| 226 | +//void __irq_dma2_stream5(void) { |
| 227 | +void __irq_DMA2_Stream5_IRQHandler(void) { |
| 228 | + dispatch_handler(DMA2, DMA_STREAM5); |
| 229 | +} |
| 230 | + |
| 231 | +//void __irq_dma2_stream6(void) { |
| 232 | +void __irq_DMA2_Stream6_IRQHandler(void) { |
| 233 | + dispatch_handler(DMA2, DMA_STREAM6); |
| 234 | +} |
| 235 | + |
| 236 | +//void __irq_dma2_stream7(void) { |
| 237 | +void __irq_DMA2_Stream7_IRQHandler(void) { |
| 238 | + dispatch_handler(DMA2, DMA_STREAM7); |
| 239 | +} |
| 240 | + |
| 241 | +#endif |
0 commit comments