AnsweredAssumed Answered

Problem with DMA on USART1 on STM32F0xx

Question asked by b.bulek on Oct 19, 2013
Latest reply on Oct 20, 2013 by Clive One
Hi,

I cannot get DMA receiver on USART1 to working state. I'm attaching code and would be thankful for any help or advice. Are there anywhere some working examples with DMA?
I must be missing something obvious....
void HW_USART_Init(uint32_t BaudRate) {
 
  USART_InitTypeDef USART_InitStructure;
  GPIO_InitTypeDef GPIO_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;
 
   
//---------------------------- Clocks Init ----------------------------
  /* Enable DMA clock */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
 
//---------------------------- GPIO Init ----------------------------
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1 );
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1 );
#ifdef HW_FLOW_CONTROL
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_1 );
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource12, GPIO_AF_1 );
#endif
 
  /* Configure USART1 pins:  Rx and Tx ----------------------------*/
#ifdef HW_FLOW_CONTROL
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10  | GPIO_Pin_11  | GPIO_Pin_12;
#else
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
#endif
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
 
 
   
 
   
   
//---------------------------- USART Init ----------------------------
  USART_InitStructure.USART_BaudRate = BaudRate;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No;
#ifdef HW_FLOW_CONTROL
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS_CTS;
#else
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
#endif
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  USART_Init(USART1, &USART_InitStructure);
 
//---------------------------- NVIC Init ----------------------------
//Enable interrupts only if DMA is not active
  /* Enable the USART1 RX DMA Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_5_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPriority = 0x00;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
   
//---------------------------- DMA Init ----------------------------
  DMA_InitTypeDef DMA_InitStructure;
  
  /* USART1_RX DMA1 Channel 5 (See RM0008) */
  
  DMA_DeInit(DMA1_Channel5);
  
  DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART1->RDR;
  DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&(UartBuf.Chars);
  DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
  DMA_InitStructure.DMA_BufferSize = sizeof(UartBuf.Chars);
  DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
  DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
  DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
  DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
  DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
  DMA_InitStructure.DMA_Priority = DMA_Priority_Low;
  DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
  
  DMA_Init(DMA1_Channel5, &DMA_InitStructure);
  
  USART_DMACmd(USART1, USART_DMAReq_Rx, ENABLE);
 
  USART_Cmd(USART1, ENABLE);
 
  DMA_Cmd(DMA1_Channel5, ENABLE);
 
 }

Thanks in advance,
regards,
Bulek.

Outcomes