title: How can i Use DMA to copy a source array to destination array Using CMSIS CORE?
body: <p>I was trying DMA in stm32f103rc. I followed this tutorial <a href="https://letanphuc.net/2014/06/how-to-use-stm32-dma/" rel="nofollow noreferrer">https://letanphuc.net/2014/06/how-to-use-stm32-dma/</a> and wrote my own code using CMSIS CORE. Here i m taking two arrays one 'sourceArr' where i am storing a random value and copying that array to 'destArr' with the help of DMA.</p>
  
  <p><strong>DMA RELATED FUNCTIONS DEFINES AND VARIABLES</strong></p>
  
  <pre><code>volatile uint32_t status = 0;
  uint32_t sourceArr[ARRAYSIZE];
  uint32_t destArr[ARRAYSIZE];
  
  #define DMA1_CLOCK_EN()     (RCC-&gt;AHBENR = RCC_AHBENR_DMA1EN) 
  #define DMA1_CHANNEL1_EN()  (DMA1_Channel1-&gt;CCR |= DMA_CCR1_EN)     //((uint16_t)0x0001)
  
  void DMA1_Channel1_IRQHandler(void)
  {
      if (DMA1-&gt;ISR &amp; DMA_ISR_TCIF1)
      {
          status = 1;
          Blink_Led(5000);
          DMA1-&gt;IFCR |= DMA_IFCR_CTCIF1;
      }
  }
  
  void NVIC_Init()
  {
       NVIC_EnableIRQ(DMA1_Channel1_IRQn);
       NVIC_SetPriority(DMA1_Channel1_IRQn, 0);
  }
  
  void DMA_Init(void)
  {
      DMA1_Channel1-&gt;CCR |= DMA_CCR1_MEM2MEM;     /*Memory to memory transfer enable  ((uint16_t)0x4000)*/
      DMA1_Channel1-&gt;CCR &amp;= ~DMA_CCR1_CIRC;               /* circular mode* ((uint16_t)0x0020) */
      /* Priroty set as medium PL[1:0] = 01 */
      DMA1_Channel1-&gt;CCR |= DMA_CCR1_PL_0;   //((uint16_t)0x1000)
      /* source and destination data size set as 32bit */
      DMA1_Channel1-&gt;CCR |= DMA_CCR1_MSIZE_1;  //((uint16_t)0x0800)
      DMA1_Channel1-&gt;CCR |= DMA_CCR1_PSIZE_1;  //((uint16_t)0x0200) 
      /* Auto increment of memory enabled for source and destination */
      DMA1_Channel1-&gt;CCR |= DMA_CCR1_PINC;     //((uint16_t)0x0040)
      DMA1_Channel1-&gt;CCR |= DMA_CCR1_MINC;     //((uint16_t)0x0080)
      /* Data transfer direction set as read from peripheral*/
      DMA1_Channel1-&gt;CCR &amp;= ~DMA_CCR1_DIR;     //((uint16_t)0x0010)
      /* source and destination start addresses */
      DMA1_Channel1-&gt;CPAR = (uint32_t)&amp;sourceArr;
      DMA1_Channel1-&gt;CMAR = (uint32_t)&amp;destArr;
      /* Enable DMA1 Channel Transfer Complete interrupt */
    DMA1_Channel1-&gt;CCR |= DMA_CCR1_TCIE;     //((uint16_t)0x0002)
  
  }
  
  void DMA_DeInit(void)
  {
      DMA1_Channel1-&gt;CCR &amp;= (uint16_t)~DMA_CCR1_EN;   /* Disable the selected DMAy Channelx */
      DMA1_Channel1-&gt;CCR = 0;                                             /* Reset DMAy Channelx control register */
      DMA1_Channel1-&gt;CNDTR = 0;                                           /* Reset DMAy Channelx remaining bytes register */
      DMA1_Channel1-&gt;CPAR = 0;                                            /* Reset DMAy Channelx peripheral address register */
      DMA1_Channel1-&gt;CMAR = 0;                                            /* Reset DMAy Channelx memory address register */
      DMA1-&gt;IFCR  |= DMA_IFCR_CGIF1 | DMA_IFCR_CTCIF1 | DMA_IFCR_CHTIF1 | DMA_IFCR_CTEIF1;        /* Reset interrupt pending bits for DMA1 Channel1 */
  
  }
  </code></pre>
  
  <p><strong>MAIN</strong></p>
  
  <pre><code>int main(void)
  {
      int i;
      for (i=0;i&lt;ARRAYSIZE;i++)
          sourceArr[i] = i;
      Led_Init();
      Blink_Led(1000);
      DMA1_CLOCK_EN();        //enable clock for DMA
      DMA_DeInit();
      DMA_Init();
      NVIC_Init();
      Blink_Led(1000);
      status = 0;
      __enable_irq();
      DMA1_CHANNEL1_EN();  //Enable DMA1 Channel 1 transfer
      while(status==0);
      Blink_Led(1000);
      for (i=0; i&lt;ARRAYSIZE;i++)
    {
       destArr[i]=sourceArr[i];
    }
      Blink_Led(1000);
  
      while (1)
      {
  
      }
  }
  </code></pre>
  
  <p>if everything works fine then there will be 5 Blinks. 4 in the main function and 1 in IRQ.</p>
  
  <p>But i am only getting two blinks.</p>
  
  <p>Any suggestions will be really helpful.</p>
  
  <p>Thanks in Advance</p>