cancel
Showing results for 
Search instead for 
Did you mean: 

Problem with DMA on USART1 on STM32F0xx

prijazendom
Associate II
Posted on October 19, 2013 at 19:42

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. #stm32 #discovery #stm32-usart-dma
5 REPLIES 5
Posted on October 19, 2013 at 21:36

RM0008 is NOT the controlling document for the STM32F0xx parts, that would be RM0091

For DMA1 Channel5 you'd need to change the DMA mapping in SYSCFG, normally I believe it would be DMA1 Channel3
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
prijazendom
Associate II
Posted on October 20, 2013 at 11:28

Hi,

thanks for response. Actually that comment was left in - my code is based on your examples... According to reference manual for f0xx, chanels 4 and 5 can also be used for USART1.

I've read carefully document in library regarding proper initialization, done it in exact way, but doesn't work.

I'm also confused about libraries/examples to base my work on. 

My example is based on STM32F0-Discovery_FW_V1.0.0, but I've somewhere also downloaded STM32F0xx_StdPeriph_Lib_V1.1.0, that has example with DMA between two boards.. Is the latter one newet and preferable over the first one ?

I'm using discovery F0 board, and also try things on module with stm32f050K6...

Thanks in advance,

regards,

Bulek...

Posted on October 20, 2013 at 14:38

According to reference manual for f0xx, chanels 4 and 5 can also be used for USART1.

Yes, but repeating : For DMA1 Channel5 you'd need to change the DMA mapping in SYSCFG, normally I believe it would be DMA1 Channel3

/* Enable SYSCFG clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
/* @arg SYSCFG_DMARemap_USART1Rx: Remap USART1 Rx DMA requests from channel3 to channel5 */
SYSCFG_DMAChannelRemapConfig(SYSCFG_DMARemap_USART1Rx, ENABLE); 

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on October 20, 2013 at 14:43

I'm also confused about libraries/examples to base my work on.  My example is based on STM32F0-Discovery_FW_V1.0.0, but I've somewhere also downloaded STM32F0xx_StdPeriph_Lib_V1.1.0, that has example with DMA between two boards.. Is the latter one newet and preferable over the first one ?

The StdPeriph_Lib has a primary target of the STM320518-EVAL board, not the STM32F0-Discovery, examples would need to be ported to the appropriate pins, and LCD and other extended functions removed.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on October 20, 2013 at 15:06

0690X00000605QiQAI.png 0690X00000605W6QAI.png
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..