AnsweredAssumed Answered

USART communicatoin with DMA

Question asked by kim.seung_man on Nov 15, 2013
Latest reply on Nov 15, 2013 by kim.seung_man
Hello,

I wrote a program to send data through USART6 with DMA.
I tested first USART6 only without DMA.
It worked fine. data in Buffer[] was transferred through USART6 to PC when I push the user button on the board.
However, No data were transferred in BufferDMA[] through USART with DMA. I searched previous post about this issues and spent couple of days. But I still couldn't figure out what is the problem with my code
Please help me if any one knows what is the problem with my codes.
Thank you in advance for the help.  
 
/////////////////////////////////my program below///////////////////////////////////////////////

#include <stm32f4xx.h>
#include <stdio.h>

char Buffer[] = "sending data through USART6>>\r\n" ;
char BufferDMA[] = "sending data through USART6 with DMA>>\r\n" ;
uint32_t DMALISR_flag;

void RCC_Configuration(void)
{
 RCC->AHB1ENR     |=(1UL<<3)|(1UL<<2)|      /*Enable GPIOD, GPIOC clock */
                    (1UL<<0)|             /*Enable GPIOA clock */
                    (1UL<<22);           /* DMA2 clk Enable*/
 
 
 RCC->APB2ENR   |= (1UL<<5);       /*USART6 clk enable */
 
}

void GPIO_init(void)
{

GPIOA->MODER    &= ~((3UL<<0));   /* reset PA0 and set PA0 to input*/
 
GPIOA->OSPEEDR   &= ~((3UL<<0));   /* reset PA0 bit*/
GPIOA->OSPEEDR   |= (2UL<<0);  /* Set PA0 fast speed 50Mhz */ 
 
GPIOA->PUPDR     &= ~((3UL<<0));   /* PA0 no pull-up and pull-down */ 
 
 
GPIOC->MODER    &= ~((3UL<<12)|(3UL<<14));   /* reset bits of PC6,7 */
GPIOC->MODER    |= ((2UL<<12)|(2UL<<14));   /* Set PC6,7 are AF mode */
 
GPIOC->OTYPER    &= ~((1UL<<6)|(1UL<<7));  /* Set Push-Pull output */ 
 
GPIOC->OSPEEDR   &=  ~((3UL<<12)|(3UL<<14));   /*reset bits of PC6,7 */
GPIOC->OSPEEDR   |= ((2UL<<12)|(2UL<<14));  /* Set fast speed 50Mhz for PC6,7*/ 
 
GPIOC->PUPDR     &= ~((3UL<<12)|(3UL<<14));   /*reset bits of PC6,7 */
GPIOC->PUPDR     |= ((1UL<<12)|(1UL<<14));  /* Set Pull-Up resistor for P6,7 */

GPIOC->AFR[0]         =  (8UL<<24)|(8UL<<28);    /*Set PC6 as AF8 --USART6TX, PC7 as AF8--USART6RX */ 
}

void NVIC_Configuration(void)
{
  NVIC_EnableIRQ(DMA2_Stream1_IRQn);
 }

void DMA2_Stream1_IRQHandler(void)
{
 if((DMA2->LISR & (1UL<<11))==1)   /*Test on DMA Stream Transfer Complete interrupt */
 {
  DMA2->LIFCR &= ~(1UL<<11);      /*Clear DMA Stream Transfer Complete interrupt pending bit */
 }
}
void USART_Init(void)
{
 
 USART6->BRR        = 0x02D9;                /*Baud = 115200 with fck = 84Mhz, Over8 = 0 (oversampling by16) */
 
 USART6->CR1       &= ~((1UL<<15)|   /* Oversampling by 16 */
                   (1UL<<12)|       /* Wordlength 8bit */
                   (1UL<<10));   /* Parity control disable */
 
 USART6->CR2      &= ~((3UL<<12));      /* 1 stop bit */
 USART6->CR3      &= ~((1UL<<11));      /* three sample bit method */

 USART6->CR1       |=   (1UL<<2)|       /* Receiver enable */
                                       (1UL<<3);         /* Transmitter enable */
  
 USART6->CR1    |=    (1UL<<13);    /* USART Enable */
 }

void SendData(void)
{
  char i;
 
 for(i = 0; i <sizeof(Buffer)-1; i++)
  {
   while((USART6->SR&0x0080) == 0x00);
   USART6->DR = Buffer[i];
    }
 }

void DMA_Init2(void )       //DMA intial for USART6
{
 DMA2_Stream1->CR    = 0;                   /* Clear CR register */
 DMA2_Stream1->CR    |=    (0x04000000);       /* Channel 2 selected */
 DMA2_Stream1->CR    &= ~(0x01800000);    /*  Memory bust single*/
 DMA2_Stream1->CR    &= ~(0x00600000);    /*Peripheral bust single*/
 
 DMA2->LIFCR &= ~(1UL<<11);               /*Clear DMA Stream Transfer Complete interrupt pending bit */
 
 DMA2_Stream1->CR     |=  (2UL<<16)|       /* Priority level High */
                      (0UL<<13)|       /* Memory data size Byte*/
                      (0UL<<11)|       /*Peripheral data size Byte*/
                      (1UL<<10)|        /*Memory increment  enable */
                      (0UL<<9)|        /*Peripheral increment disable*/
                      (1UL<<4)|        /*Transfer complete interrupt enable(TCIE) */
                      (1UL<<8);        /*Circular mode enable*/
                      
 DMA2_Stream1->CR    |=(1UL<<6);                       /*Direction Memory to peripheral*/ 
 DMA2_Stream1->NDTR  = (uint16_t)sizeof(BufferDMA);    /*Number of data items*/
  DMA2_Stream1->PAR  = (uint32_t) &USART6->DR;       /*Peripheral base address*/
 DMA2_Stream1->M0AR = (uint32_t)BufferDMA;              /*Memory 0 base address*/
 
 DMA2_Stream1->FCR  &= ~(0x00000007);     /*Clear bit[0:2] */ 
  DMA2_Stream1->FCR  |=    (0x00000007);    /*FIFO mode enable and Threshold_Full */
 
 USART6->CR3   |=    (1UL<<7);           /* DMA enable transmitter*/
 USART6->SR              &=~(0x00000040);     /* Reset TC bit                  */
 
 DMA2_Stream1->CR    |= (1UL<<0);        /*DMA2_Stream1 Enable*/                
 
}

uint32_t BTN_Get(void)
{
 return (GPIOA->IDR&(1UL<<0));
 }

int main (void)
{
  uint32_t btns = 0;
  
 SystemCoreClockUpdate();                         /* Get Core Clock Frequency   */
 
 RCC_Configuration();
 GPIO_init();
 NVIC_Configuration();

 USART_Init();
 DMA_Init2( );     //Transmit data through USART   
 
 DMALISR_flag = DMA2->LISR ;
 
 while(1)
 {
  btns = BTN_Get();

        
  /////////////////////////Transmit data through USART6 without DMA//////////////////////////
if(btns == (1UL << 0))
 {       
 //SendData();
 }
  
 }
 
}

Outcomes