Question
Questions regarding implementing a UART_DMA Receiver.
Posted on March 03, 2015 at 20:35
Hi ST Forum,
I want to implement a DMA RS232 device (with a MAX232 transceiver), where if the master sends a certain message (e.g. 0x01), then the STM32 will send back a certain message (e.g. 0x02). I got the DMA Transmit part working, but I'm wondering how to:1. Implement the DMA_Rx section2. Control the timing of DMA_Tx based on what is received DMA_RxIs this even possible?My DMA_Tx (working) is follows, any suggestion how to progress is strongly appreciated./* * PC6 USART6_TX * PC7 UART6_RX * Rate = 115.2k * Tested working 03/03/15 */#include ''stm32f4xx_dma.h''#include ''stm32f4xx_gpio.h''#include ''stm32f4xx_usart.h''#include ''stm32f4xx_rcc.h''#include ''misc.h''#include ''semihosting.h''#define UART_SPEED 115200 //defines 9600//char Buffer[] = ''The quick brown fox jumps over the lazy dog\r\n'';char Buffer[] = {0x01,0x02,0x03,0x04};void RCC_Configuration(void){ /* UART4 clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE); /* GPIOC clock enable */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); /* DMA1 clock enable */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);}void GPIO_Configuration(void){ GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; // PC.10 UART4_TX, potential clash SCLK CS43L22 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); /* Connect USART pins to AF */ GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6); GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6);}void UART4_Configuration(void){ USART_InitTypeDef USART_InitStructure; /* USARTx configured as follow: - BaudRate = 9600 baud, can be up to 115200 - Word Length = 8 Bits - One Stop Bit - No parity - Hardware flow control disabled (RTS and CTS signals) - Receive and transmit enabled*/ USART_InitStructure.USART_BaudRate = UART_SPEED; //9600 baud USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART6, &USART_InitStructure); USART_Cmd(USART6, ENABLE);}void DMA_Configuration(void){ DMA_InitTypeDef DMA_InitStructure; DMA_DeInit(DMA2_Stream6); DMA_InitStructure.DMA_Channel = DMA_Channel_5; DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; // Transmit DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)Buffer; DMA_InitStructure.DMA_BufferSize = (uint16_t)sizeof(Buffer); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&USART6->DR; 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_High; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA2_Stream6, &DMA_InitStructure); USART_DMACmd(USART6, USART_DMAReq_Tx, ENABLE); /* Enable the USART Tx DMA request */ DMA_ITConfig(DMA2_Stream6, DMA_IT_TC, ENABLE); /* Enable DMA Stream Transfer Complete interrupt */ DMA_Cmd(DMA2_Stream6, ENABLE); /* Enable the DMA Tx Stream */}void DMA2_Stream6_IRQHandler(void){ /* Test on DMA Stream Transfer Complete interrupt */ if (DMA_GetITStatus(DMA2_Stream6, DMA_IT_TCIF4)) { /* Clear DMA Stream Transfer Complete interrupt pending bit */ DMA_ClearITPendingBit(DMA2_Stream6, DMA_IT_TCIF4); }}void NVIC_Configuration(void){ NVIC_InitTypeDef NVIC_InitStructure; /* Configure the Priority Group to 2 bits */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); /* Enable the UART4 RX DMA Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream6_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);}int main(void){ RCC_Configuration(); NVIC_Configuration(); GPIO_Configuration(); UART4_Configuration(); DMA_Configuration(); while(1) { }}