AnsweredAssumed Answered

usart dma - receive

Question asked by kaka on Jul 6, 2015
Latest reply on Jul 7, 2015 by kaka
hej, 
can anyone tell me why dma is  not working?...but the usart works...



volatile uint8_t Buffer[32];
uint8_t receiveword;


/**************************************************************************************/
void gpio_Usart()
{
          //enable the usart
          RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);


          RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE);


          //enabling the port for usart6
          GPIO_InitTypeDef GPIO_InitStruct;
          GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 |GPIO_Pin_7;
          GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
          GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
          GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
          GPIO_InitStruct.GPIO_PuPd =GPIO_PuPd_UP;
          GPIO_Init(GPIOC, &GPIO_InitStruct);




          GPIO_PinAFConfig(GPIOC, GPIO_PinSource6 , GPIO_AF_USART6);
          GPIO_PinAFConfig(GPIOC, GPIO_PinSource7 , GPIO_AF_USART6);


          //enable usart configuration
          USART_InitTypeDef USART_InitStruct;
          USART_InitStruct.USART_BaudRate = 9600;
          USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None ;
          USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;;
          USART_InitStruct.USART_Parity = USART_Parity_No;
          USART_InitStruct.USART_StopBits = USART_StopBits_1;
          USART_InitStruct.USART_WordLength = USART_WordLength_8b;
          USART_Init(USART6, &USART_InitStruct);


          //enable usart
          USART_Cmd( USART6, ENABLE);


          //clock for leds
          RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);




                    //GPIO intialization for leds
                    GPIO_InitTypeDef GPIO_IntStruct;
                    GPIO_IntStruct.GPIO_Mode = GPIO_Mode_OUT;
                    GPIO_IntStruct.GPIO_OType = GPIO_OType_PP ;
                    GPIO_IntStruct.GPIO_Pin = GPIO_Pin_12 |GPIO_Pin_13 |GPIO_Pin_14 |GPIO_Pin_15 ;
                    GPIO_IntStruct.GPIO_PuPd = GPIO_PuPd_UP ;
                    GPIO_IntStruct.GPIO_Speed = GPIO_Speed_50MHz;
                    GPIO_Init(GPIOD, &GPIO_IntStruct);




}
/**************************************************************************************/






void DMA_Nvic_Configuration(void)
{
               //clock for dma
          RCC_AHB1PeriphResetCmd(RCC_AHB1Periph_DMA2, ENABLE);
          DMA_DeInit(DMA2_Stream1);

          DMA_InitTypeDef DMA_InitStruct;
          DMA_InitStruct.DMA_Channel = DMA_Channel_5;
          DMA_InitStruct.DMA_DIR = DMA_DIR_PeripheralToMemory; // Receive
          DMA_InitStruct.DMA_Memory0BaseAddr = (uint32_t)Buffer;
          DMA_InitStruct.DMA_BufferSize = (uint16_t)sizeof(Buffer);
          DMA_InitStruct.DMA_PeripheralBaseAddr = (uint32_t)&USART6->DR;
          DMA_InitStruct.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
          DMA_InitStruct.DMA_MemoryInc = DMA_MemoryInc_Enable;
          DMA_InitStruct.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
          DMA_InitStruct.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
          DMA_InitStruct.DMA_Mode = DMA_Mode_Circular;
          DMA_InitStruct.DMA_Priority = DMA_Priority_High;
          DMA_InitStruct.DMA_FIFOMode = DMA_FIFOMode_Enable;
          DMA_InitStruct.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
          DMA_InitStruct.DMA_MemoryBurst = DMA_MemoryBurst_Single;
          DMA_InitStruct.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
          DMA_Init(DMA2_Stream1, &DMA_InitStruct);


          //enable dma2
          DMA_Cmd(DMA2_Stream1, ENABLE);


          //usart request
          USART_DMACmd(USART6, USART_DMAReq_Rx, ENABLE);


          //enable the dma interrupt
          DMA_ITConfig(DMA2_Stream1, DMA_IT_TC | DMA_IT_HT, ENABLE);

          //enable interrupt for dma
          NVIC_InitTypeDef NVIC_InitStruct;
          NVIC_InitStruct.NVIC_IRQChannel                   = DMA2_Stream1_IRQn; 
          NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
          NVIC_InitStruct.NVIC_IRQChannelSubPriority        = 1;
          NVIC_InitStruct.NVIC_IRQChannelCmd                = ENABLE;
          NVIC_Init(&NVIC_InitStruct);
}


void DMA2_Stream1_IRQHandler(void)
{
  /* Test on DMA Stream Transfer Complete interrupt */
  if (DMA_GetITStatus(DMA2_Stream1, DMA_IT_TCIF1))
  {
    /* Clear DMA Stream Transfer Complete interrupt pending bit */
    DMA_ClearITPendingBit(DMA2_Stream1, DMA_IT_TCIF1);
    GPIO_SetBits(GPIOD, GPIO_Pin_12);
  }


  /* Test on DMA Stream Half Transfer interrupt */
  if (DMA_GetITStatus(DMA2_Stream1, DMA_IT_HTIF1))
  {
    /* Clear DMA Stream Half Transfer interrupt pending bit */
    DMA_ClearITPendingBit(DMA2_Stream1, DMA_IT_HTIF1);
    GPIO_SetBits(GPIOD, GPIO_Pin_13);
  }
}


void receiveUsart()
{
     while(USART_GetFlagStatus(USART6, USART_FLAG_RXNE) != SET);
     receiveword = USART6->DR;
}



int main(void)
{


     int decremt = 32;
     printf("decremt %d\n", decremt);
     gpio_Usart();
     DMA_Nvic_Configuration();


     while(decremt)
     {
          receiveUsart();
          printf("receiveword %c\n", receiveword);
          decremt--;
     }


     return 0;
}

Outcomes