Electrical Engineering Stack Exchange is a question and answer site for electronics and electrical engineering professionals, students, and enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

*Anwser at the end.

I am working with SMT3240G-EVAL, I am working on a project in which I want to be able to send and receive messages using UART. I am starting with embedded development so I don’t have much experience.

I made a question before and they recommend me that I should start with basic and remove functionality so that’s what I am going to do.

For testing I am using a PmodUSBUART and HTerm 0.8.1 beta for sending and receiving information in my computer.

I set up UART and DMA with the following configuration using the HAL Drivers from ST (V1.1.0 / 19-June-2014) . I am using another project as my base.

UART_Handle.Instance = USARTx;
UART_Handle.Init.BaudRate = 9600; 
UART_Handle.Init.WordLength = UART_WORDLENGTH_8B;
UART_Handle.Init.StopBits = UART_STOPBITS_1;
UART_Handle.Init.Parity = UART_PARITY_ODD;
UART_Handle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
UART_Handle.Init.Mode = UART_MODE_TX_RX;
UART_Handle.Init.OverSampling = UART_OVERSAMPLING_16;

From HAL_UART_MspInit:

GPIO_InitTypeDef GPIO_InitStruct;

/* Enable GPIO TX/RX clock */
USARTx_RX_GPIO_CLK_ENABLE();
USARTx_TX_GPIO_CLK_ENABLE();

/* Enable USART1 clock */
USARTx_CLK_ENABLE();

/*Enable DMA clock*/
DMAx_CLK_ENABLE();

GPIO_InitStruct.Pin = USARTx_TX_PIN; // Pin 10 as TX for USART3 same as CN11
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; // pull-push mode
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
GPIO_InitStruct.Alternate = USARTx_TX_AF;

HAL_GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStruct); // Initialize TX pin

GPIO_InitStruct.Pin = USARTx_RX_PIN; // Pin 11 as RX for USART3 same as in CN11
GPIO_InitStruct.Alternate = USARTx_RX_AF;

HAL_GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStruct); //Initialize RX pin

//HAL_NVIC_SetPriority(USARTx_IRQn, 0, 1);
//HAL_NVIC_EnableIRQ(USARTx_IRQn);

/*## Configure the DMA streams ##########################################*/

/* Configure the DMA handler for Transmission process */
HandlerDMA_tx.Instance = USARTx_TX_DMA_STREAM;

HandlerDMA_tx.Init.Channel = USARTx_TX_DMA_CHANNEL;
HandlerDMA_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
HandlerDMA_tx.Init.PeriphInc = DMA_PINC_DISABLE;
HandlerDMA_tx.Init.MemInc = DMA_MINC_ENABLE;
HandlerDMA_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
HandlerDMA_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
HandlerDMA_tx.Init.Mode = DMA_NORMAL;
HandlerDMA_tx.Init.Priority = DMA_PRIORITY_VERY_HIGH;
HandlerDMA_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
HandlerDMA_tx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
HandlerDMA_tx.Init.MemBurst = DMA_MBURST_INC4;
HandlerDMA_tx.Init.PeriphBurst = DMA_PBURST_INC4;


HAL_DMA_Init(&HandlerDMA_tx);

/* Associate the initialized DMA handle to the UART handle */
__HAL_LINKDMA(huart, hdmatx, HandlerDMA_tx);

/* Configure the DMA handler for reception process */
HandlerDMA_rx.Instance = USARTx_RX_DMA_STREAM;
HandlerDMA_rx.Init.Channel = USARTx_RX_DMA_CHANNEL;
HandlerDMA_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
HandlerDMA_rx.Init.PeriphInc = DMA_PINC_DISABLE;
HandlerDMA_rx.Init.MemInc = DMA_MINC_ENABLE;
HandlerDMA_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
HandlerDMA_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
HandlerDMA_rx.Init.Mode = DMA_CIRCULAR;
HandlerDMA_rx.Init.Priority = DMA_PRIORITY_HIGH;
HandlerDMA_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
HandlerDMA_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
HandlerDMA_rx.Init.MemBurst = DMA_MBURST_INC4;
HandlerDMA_rx.Init.PeriphBurst = DMA_PBURST_INC4;

HAL_DMA_Init(&HandlerDMA_rx);

/* Associate the initialized DMA handle to the the UART handle */
__HAL_LINKDMA(huart, hdmarx, HandlerDMA_rx);

/*##-4- Configure the NVIC for DMA #########################################*/
/* NVIC configuration for DMA transfer complete interrupt (USARTx_TX)  */
HAL_NVIC_SetPriority(USARTx_DMA_TX_IRQn, 1, 2);
HAL_NVIC_EnableIRQ(USARTx_DMA_TX_IRQn);

/* NVIC configuration for DMA transfer complete interrupt (USARTx_RX) */
HAL_NVIC_SetPriority(USARTx_DMA_RX_IRQn, 0, 1);
HAL_NVIC_EnableIRQ(USARTx_DMA_RX_IRQn);

/* NVIC configuration for USART TC interrupt */
HAL_NVIC_SetPriority(USARTx_IRQn, 0, 0);
HAL_NVIC_EnableIRQ(USARTx_IRQn);

For this part I wanted to send 10 bytes using the terminal and when completed it will return it back to me. The transmition works fine with no erros from the device but I am getting some weird results and not what I am expecting.

I was expecting to get the same values on the return side. What is happening?

Terminal 1

The sending part of my code is:.

My buffer size is 10

uint8_t rxBuffer[BUFFERSIZE];

in UART_Init() start UART receive and wait for someone to send us something.

__HAL_UART_FLUSH_DRREGISTER(&UART_Handle);
HAL_UART_Receive_DMA(&UART_Handle,(uint8_t*)rxBuffer, BUFFERSIZE);

then on HAL_UART_RxCpltCallback

__HAL_UART_FLUSH_DRREGISTER(huart);
HAL_UART_Transmit_DMA(&UART_Handle, (uint8_t*) rxBuffer, BUFFERSIZE)

EDIT

So I remove the DMA part like they told me and it was still not working, then figure out what was happening ... If you want to seend UART using parity odd or even you need to specify UART_WORDLENGTH_9B otherwise it does not work and you will get trash as you see in the images. Thats the way it is implemented on STM32F4

UART_Handle.Instance = USARTx;
UART_Handle.Init.BaudRate = 1200; // For HART  the BaudRate is 1200
UART_Handle.Init.WordLength = UART_WORDLENGTH_9B;
UART_Handle.Init.StopBits = UART_STOPBITS_1;
UART_Handle.Init.Parity = UART_PARITY_ODD;
UART_Handle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
UART_Handle.Init.Mode = UART_MODE_TX_RX;
UART_Handle.Init.OverSampling = UART_OVERSAMPLING_16;
share|improve this question
1  
You never check the size of received data, or indeed that it is even non-zero. This likely means that you are transmitting a full-length buffer of whatever happened to be in RAM back. But there could be other problems - you have failed to supply the actual code, the data being sent, or even to clearly state what is received, since your screenshot seems to show two conflicting things – Chris Stratton Jun 7 '16 at 16:36
1  
You could simplify your project even more by not trying to use DMA. At a baud rate of 9600, DMA isn't necessary anyway. – brhans Jun 7 '16 at 16:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.