2013-10-19 10:42 AM
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
2013-10-19 12:36 PM
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 Channel32013-10-20 02:28 AM
2013-10-20 05:38 AM
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);
2013-10-20 05:43 AM
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.2013-10-20 06:06 AM