altos: Disable DMA unit when idle
authorKeith Packard <keithp@keithp.com>
Sat, 7 Apr 2012 05:39:45 +0000 (22:39 -0700)
committerKeith Packard <keithp@keithp.com>
Sat, 7 Apr 2012 05:39:45 +0000 (22:39 -0700)
Should save a bit of power

Signed-off-by: Keith Packard <keithp@keithp.com>
src/stm/ao_dma_stm.c

index 70b9e48a4aac460e001c8120d074fa197ad80d16..f996e7cd603ca7666fa273bd39594c2e9267277d 100644 (file)
@@ -27,6 +27,7 @@ uint8_t ao_dma_done[NUM_DMA];
 
 static struct ao_dma_config ao_dma_config[NUM_DMA];
 static uint8_t ao_dma_mutex[NUM_DMA];
+static uint8_t ao_dma_active;
 
 static void
 ao_dma_isr(uint8_t index) {
@@ -58,10 +59,12 @@ ao_dma_set_transfer(uint8_t                 index,
                    uint32_t            ccr)
 {
        ao_mutex_get(&ao_dma_mutex[index]);
+       if (ao_dma_active++ == 0)
+               stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_DMA1EN);
        stm_dma.channel[index].ccr = ccr | (1 << STM_DMA_CCR_TCIE);
        stm_dma.channel[index].cndtr = count;
-       stm_dma.channel[index].cpar = (uint32_t) peripheral;
-       stm_dma.channel[index].cmar = (uint32_t) memory;
+       stm_dma.channel[index].cpar = peripheral;
+       stm_dma.channel[index].cmar = memory;
 }
 
 void
@@ -75,6 +78,8 @@ void
 ao_dma_done_transfer(uint8_t index)
 {
        stm_dma.channel[index].ccr &= ~(1 << STM_DMA_CCR_EN);
+       if (--ao_dma_active == 0)
+               stm_rcc.ahbenr &= ~(1 << STM_RCC_AHBENR_DMA1EN);
        ao_mutex_put(&ao_dma_mutex[index]);
 }
 
@@ -89,8 +94,6 @@ ao_dma_init(void)
 {
        int     index;
 
-       stm_rcc.ahbenr |= (1 << STM_RCC_AHBENR_DMA1EN);
-
        for (index = 0; index < STM_NUM_DMA; index++) {
                stm_nvic_set_enable(STM_ISR_DMA1_CHANNEL1_POS + index);
                stm_nvic_set_priority(STM_ISR_DMA1_CHANNEL1_POS + index, 4);