cancel
Showing results for 
Search instead for 
Did you mean: 

UART communication not working properly

Vaclav Chrascina
Associate II
Posted on April 20, 2018 at 12:29

Hello,

I am trying to cummunicate with device throught UART, but communication is not working reliably. I am trying to read data continuosly by transmitting request in infinite loop, but I receive incomplete or currupted message. Could you give me some some advice what am I doing wrong?

Here is my code:

int DataRecieved = 0;

int main(void)

{

  HAL_Init();

  SystemClock_Config();

  MX_GPIO_Init();

  MX_USART3_UART_Init();    

    

    HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_SET);                //RS 485 transmit mode

  while (1)

    {

        if(HAL_UART_Transmit_IT(&huart3, (uint8_t*)aTxBuffer, 2) != HAL_OK)

        {

            while(1);

        }

        while(DataRecieved != 0);

        if(HAL_UART_Receive_IT(&huart3, (uint8_t*)aRxBuffer, 4) != HAL_OK)

        {

            while(1);

        }

        while(DataRecieved == 0);

         //Do something with received data

  }

Here are my callback functions:

void HAL_UART_TxCpltCallback(UART_HandleTypeDef *UartHandle)

{

  /* Set transmission flag: transfer complete */

    HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_RESET);                    //RS 485 receive mode

    DataRecieved = 0;

}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *UartHandle)

{

  /* Set transmission flag: transfer complete */

    HAL_GPIO_WritePin(GPIOB,GPIO_PIN_9,GPIO_PIN_SET);                        //RS 485 transmit mode

    DataRecieved = 1;

}
4 REPLIES 4
Posted on April 20, 2018 at 13:43

Dear Vaclav

According to me, the functions 

HAL_UART_Transmit_IT and 

HAL_UART_Receive_IT have to be placed outside the main 

infinite loop.  Please find attached an example of UART communication tested on a STM32F4 discovery board.

Pins 9 and 10 are connected together to create a loopback on UART1.

Bye

JC

/* Includes ------------------------------------------------------------------*/

#include 'stm32f4xx_hal.h'

/* Private variables ---------------------------------------------------------*/

UART_HandleTypeDef huart1;

/* USER CODE BEGIN 0 */

uint8_t tx_buffer[]={0,1,2,3,4,5,6,7,8,9};

uint8_t rx_buffer[10];

/* USER CODE END 0 */

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_USART1_UART_Init(void);

int main(void)

{

 

/* USER CODE BEGIN 1 */

 

/* USER CODE END 1 */

 

/* MCU Configuration----------------------------------------------------------*/

 

/* Reset of all peripherals, Initializes the Flash interface and the Systick. */

 

HAL_Init();

 

/* Configure the system clock */

 

SystemClock_Config();

 

/* System interrupt init*/

 

/* Sets the priority grouping field */

 

HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_0);

 

HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);

 

/* Initialize all configured peripherals */

 

MX_GPIO_Init();

 

MX_USART1_UART_Init();

 

/* USER CODE BEGIN 2 */

 

HAL_UART_Receive_IT(&huart1,rx_buffer,10);

 

HAL_UART_Transmit_IT(&huart1,tx_buffer,10);

 

/* USER CODE END 2 */

 

/* USER CODE BEGIN 3 */

 

/* Infinite loop */

 

while (1)

 

{

   

    

 

}

 

/* USER CODE END 3 */

}

/** System Clock Configuration

*/

void SystemClock_Config(void)

{

 

RCC_OscInitTypeDef RCC_OscInitStruct;

 

__PWR_CLK_ENABLE();

 

__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);

 

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;

 

RCC_OscInitStruct.HSIState = RCC_HSI_ON;

 

RCC_OscInitStruct.HSICalibrationValue = 6;

 

RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;

 

HAL_RCC_OscConfig(&RCC_OscInitStruct);

}

/* USART1 init function */

void MX_USART1_UART_Init(void)

{

 

huart1.Instance = USART1;

 

huart1.Init.BaudRate = 115200;

 

huart1.Init.WordLength = UART_WORDLENGTH_8B;

 

huart1.Init.StopBits = UART_STOPBITS_1;

 

huart1.Init.Parity = UART_PARITY_NONE;

 

huart1.Init.Mode = UART_MODE_TX_RX;

 

huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;

 

huart1.Init.OverSampling = UART_OVERSAMPLING_16;

 

HAL_UART_Init(&huart1);

}

/** Pinout Configuration

*/

void MX_GPIO_Init(void)

{

 

/* GPIO Ports Clock Enable */

 

__GPIOA_CLK_ENABLE();

}

/* USER CODE BEGIN 4 */

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

{

 

__NOP(); // for debuging only

}

void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)

{

 

__NOP();// for debuging only

}

/* USER CODE END 4 */

Posted on April 20, 2018 at 14:00

I am not sure if I understand completly. If I put functions:

HAL_UART_Receive_IT(&huart1,rx_buffer,10) and HAL_UART_Transmit_IT(&huart1,tx_buffer,10) outside main infinite loop, how do I send another request? I need to send and receive continuously. ShalI I put these functions again in receive callback functions or what?

Posted on April 22, 2018 at 12:28

Dear Vaclav

In your case, when you need to transmit and receive continuously, the use of DMA seems to be the best way.

DMA has to be configured in circular mode.  The following code has been tested on a nucleo L4 board.

PA9 

(USART1_TX) 

and PA10 (USART1_RX) connected together.

In my example, an integer array of size NSIZE is continuously transmitted and received.  

To have more complete explanations, I suggest you to look at the ebook proposed by

Carmine Noveillo Mastering STM32.

JC

/* Includes ------------------------------------------------------------------*/

#include

'main.h'

#include

'stm32l4xx_hal.h'

/* USER CODE BEGIN Includes */

#include

<stdio.h>

#include

<string.h>

/* USER CODE END Includes */

/* Private variables ---------------------------------------------------------*/

UART_HandleTypeDef

huart1;

UART_HandleTypeDef

huart2;

DMA_HandleTypeDef

hdma_usart1_tx;

DMA_HandleTypeDef

hdma_usart1_rx;

/* USER CODE BEGIN PV */

/* Private variables ---------------------------------------------------------*/

#define

NSIZE 20

uint8_t

tx_buffer[NSIZE]={0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};

uint8_t

rx_buffer[NSIZE];

__IO

ITStatus

DataReceived =

RESET

;

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/

void

SystemClock_Config(

void

);

static

void

MX_GPIO_Init(

void

);

static

void

MX_DMA_Init(

void

);

static

void

MX_USART2_UART_Init(

void

);

static

void

MX_USART1_UART_Init(

void

);

int

main(

void

)

{

 

HAL_Init();

 

SystemClock_Config();

 

/* Initialize all configured peripherals */

 

MX_GPIO_Init();

 

MX_DMA_Init();

 

MX_USART2_UART_Init();

 

MX_USART1_UART_Init();

 

/* USER CODE BEGIN 2 */

 

char

msg[80];

 

sprintf

(msg,

'start \r\n'

);

 

HAL_UART_Transmit(&huart2, (

uint8_t

*) msg,

strlen

(msg), HAL_MAX_DELAY);

 

HAL_UART_Transmit_DMA(&huart1, tx_buffer, NSIZE);

 

HAL_UART_Receive_DMA (&huart1, rx_buffer, NSIZE);

 

/* USER CODE END 2 */

 

/* Infinite loop */

 

while

(1)

 

{

 

if

(DataReceived ==

SET

){

      

for

(

int

i=0; i<NSIZE; i++){

           

sprintf

(msg,

'value %d : %d \r\n'

, i, rx_buffer[i]);

           

HAL_UART_Transmit(&huart2, (

uint8_t

*) msg,

strlen

(msg), HAL_MAX_DELAY);

           

}

 

memset

(rx_buffer, 0, NSIZE);

 

HAL_Delay(5000);

 

}

 

}

 

/* USER CODE END 3 */

}

/**

 

* @brief System Clock Configuration

 

* @

retval

None

 

*/

void

SystemClock_Config(

void

)

{

/* ... */

}

/* USART1

init

function */

static

void

MX_USART1_UART_Init(

void

)

{

 

huart1.

Instance

= USART1;

 

huart1.

Init

.

BaudRate

= 115200;

 

huart1.

Init

.

WordLength

= UART_WORDLENGTH_8B;

 

huart1.

Init

.

StopBits

= UART_STOPBITS_1;

 

huart1.

Init

.

Parity

= UART_PARITY_NONE;

 

huart1.

Init

.

Mode

= UART_MODE_TX_RX;

 

huart1.

Init

.

HwFlowCtl

= UART_HWCONTROL_NONE;

 

huart1.

Init

.

OverSampling

= UART_OVERSAMPLING_16;

 

huart1.

Init

.

OneBitSampling

= UART_ONE_BIT_SAMPLE_DISABLE;

 

huart1.

AdvancedInit

.

AdvFeatureInit

= UART_ADVFEATURE_NO_INIT;

 

if

(HAL_UART_Init(&huart1) !=

HAL_OK

)

 

{

   

_Error_Handler(__FILE__, __LINE__);

 

}

}

/* USART2

init

function */

static

void

MX_USART2_UART_Init(

void

)

{

 

huart2.

Instance

= USART2;

 

huart2.

Init

.

BaudRate

= 115200;

 

huart2.

Init

.

WordLength

= UART_WORDLENGTH_8B;

 

huart2.

Init

.

StopBits

= UART_STOPBITS_1;

 

huart2.

Init

.

Parity

= UART_PARITY_NONE;

 

huart2.

Init

.

Mode

= UART_MODE_TX_RX;

 

huart2.

Init

.

HwFlowCtl

= UART_HWCONTROL_NONE;

 

huart2.

Init

.

OverSampling

= UART_OVERSAMPLING_16;

 

huart2.

Init

.

OneBitSampling

= UART_ONE_BIT_SAMPLE_DISABLE;

 

huart2.

AdvancedInit

.

AdvFeatureInit

= UART_ADVFEATURE_NO_INIT;

 

if

(HAL_UART_Init(&huart2) !=

HAL_OK

)

 

{

   

_Error_Handler(__FILE__, __LINE__);

 

}

}

/**

 

* Enable DMA controller clock

 

*/

static

void

MX_DMA_Init(

void

)

{

 

/* DMA controller clock enable */

 

__HAL_RCC_DMA1_CLK_ENABLE();

 

/* DMA interrupt

init

*/

 

/* DMA1_Channel4_IRQn interrupt configuration */

 

HAL_NVIC_SetPriority(

DMA1_Channel4_IRQn

, 0, 0);

 

HAL_NVIC_EnableIRQ(

DMA1_Channel4_IRQn

);

 

/* DMA1_Channel5_IRQn interrupt configuration */

 

HAL_NVIC_SetPriority(

DMA1_Channel5_IRQn

, 0, 0);

 

HAL_NVIC_EnableIRQ(

DMA1_Channel5_IRQn

);

}

/** Configure pins as

       

* Analog

       

* Input

       

* Output

       

* EVENT_OUT

       

* EXTI

*/

static

void

MX_GPIO_Init(

void

)

{

 

GPIO_InitTypeDef

GPIO_InitStruct;

 

/* GPIO Ports Clock Enable */

 

__HAL_RCC_GPIOC_CLK_ENABLE();

 

__HAL_RCC_GPIOH_CLK_ENABLE();

 

__HAL_RCC_GPIOA_CLK_ENABLE();

 

__HAL_RCC_GPIOB_CLK_ENABLE();

 

/*Configure GPIO pin Output Level */

 

HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin,

GPIO_PIN_RESET

);

 

/*Configure GPIO pin : B1_Pin */

 

GPIO_InitStruct.

Pin

= B1_Pin;

 

GPIO_InitStruct.

Mode

= GPIO_MODE_IT_FALLING;

 

GPIO_InitStruct.

Pull

= GPIO_NOPULL;

 

HAL_GPIO_Init(B1_GPIO_Port, &GPIO_InitStruct);

 

/*Configure GPIO pin : LD2_Pin */

 

GPIO_InitStruct.

Pin

= LD2_Pin;

 

GPIO_InitStruct.

Mode

= GPIO_MODE_OUTPUT_PP;

 

GPIO_InitStruct.

Pull

= GPIO_NOPULL;

 

GPIO_InitStruct.

Speed

= GPIO_SPEED_FREQ_LOW;

 

HAL_GPIO_Init(LD2_GPIO_Port, &GPIO_InitStruct);

 

/*Configure GPIO pin : PC5 */

 

GPIO_InitStruct.

Pin

= GPIO_PIN_5;

 

GPIO_InitStruct.

Mode

= GPIO_MODE_AF_PP;

 

GPIO_InitStruct.

Pull

= GPIO_NOPULL;

 

GPIO_InitStruct.

Speed

= GPIO_SPEED_FREQ_VERY_HIGH;

 

GPIO_InitStruct.

Alternate

= GPIO_AF7_USART3;

 

HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

}

/* USER CODE BEGIN 4 */

void

HAL_UART_RxCpltCallback(

UART_HandleTypeDef

*huart)

{

 

__NOP();//for debugging only

 

if

(huart==&huart1){

 

HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin,

GPIO_PIN_SET

);

 

DataReceived =

SET

;

     

}

}

void

HAL_UART_TxCpltCallback(

UART_HandleTypeDef

*huart)

{

 

__NOP();//for debugging only

 

if

(huart==&huart1){

 

HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin,

GPIO_PIN_RESET

);

 

DataReceived =

RESET

;

     

}

}

Posted on April 24, 2018 at 11:00

Thank you very much Jean-Christophe