Skip to main content
Associate III
June 18, 2024
Solved

UART loop in STM32H753

Hi 

  i am receive data using UART 4 and transfer same data back like loopback but data is mismatch.
  same process in stm32f207 is working, 

 

#include "main.h"

UART_HandleTypeDef huart4;

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_UART4_Init(void);

int main(void)

{

char rx_buffer[10]={0};

HAL_Init();

SystemClock_Config();

MX_GPIO_Init();

MX_UART4_Init();

while (1)

{

HAL_UART_Receive(&huart4, rx_buffer, 10, 1000);

HAL_Delay(100);

HAL_UART_Transmit(&huart4, rx_buffer, 10, 100);

// HAL_Delay(1000);

}

/* USER CODE END 3 */

}



void SystemClock_Config(void)

{

RCC_OscInitTypeDef RCC_OscInitStruct = {0};

RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};



/** Supply configuration update enable

*/

HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY);



/** Configure the main internal regulator output voltage

*/

__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);



while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}



/** Initializes the RCC Oscillators according to the specified parameters

* in the RCC_OscInitTypeDef structure.

*/

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;

RCC_OscInitStruct.HSIState = RCC_HSI_DIV1;

RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;

RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;

if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

{

Error_Handler();

}



/** Initializes the CPU, AHB and APB buses clocks

*/

RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK

|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2

|RCC_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;

RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;

RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;

RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;

RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV1;

RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV1;

RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV1;

RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV1;



if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)

{

Error_Handler();

}

}



static void MX_UART4_Init(void)

{

/* USER CODE END UART4_Init 1 */

huart4.Instance = UART4;

huart4.Init.BaudRate = 115200;

huart4.Init.WordLength = UART_WORDLENGTH_8B;

huart4.Init.StopBits = UART_STOPBITS_1;

huart4.Init.Parity = UART_PARITY_NONE;

huart4.Init.Mode = UART_MODE_TX_RX;

huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;

huart4.Init.OverSampling = UART_OVERSAMPLING_16;

huart4.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;

huart4.Init.ClockPrescaler = UART_PRESCALER_DIV1;

huart4.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;

if (HAL_UART_Init(&huart4) != HAL_OK)

{

Error_Handler();

}

if (HAL_UARTEx_SetTxFifoThreshold(&huart4, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)

{

Error_Handler();

}

if (HAL_UARTEx_SetRxFifoThreshold(&huart4, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)

{

Error_Handler();

}

if (HAL_UARTEx_DisableFifoMode(&huart4) != HAL_OK)

{

Error_Handler();

}

}



static void MX_GPIO_Init(void)

{



/* GPIO Ports Clock Enable */

__HAL_RCC_GPIOC_CLK_ENABLE();

__HAL_RCC_GPIOH_CLK_ENABLE();

__HAL_RCC_GPIOA_CLK_ENABLE();



/* USER CODE BEGIN MX_GPIO_Init_2 */

/* USER CODE END MX_GPIO_Init_2 */

}

void Error_Handler(void)

{

/* USER CODE BEGIN Error_Handler_Debug */

/* User can add his own implementation to report the HAL error return state */

__disable_irq();

while (1)

{

}

/* USER CODE END Error_Handler_Debug */

}
This topic has been closed for replies.
Andrew Neil
Andrew NeilBest answer
Super User
July 24, 2024

@Naresh_ wrote:

I am receive data using UART 4 and transfer same data back like loopback but data is mismatch.

The commonest reason for receiving "junk" characters is wrong baud rate:

https://learn.sparkfun.com/tutorials/serial-communication/all#:~:text=If%20all%20the%20receiving%20device%20sees%20on%20its%20receive%20line%20is%20garbage%2C%20check%20to%20make%20sure%20the%20baud%20rates%20match%20up.

Baud rate depends on your clock setup - so check that.

Use the debugger to check if the corruption happens on reception or transmission - or both!

 


@Naresh_ wrote:

 same process in stm32f207 is working, 

So look carefully at the differences between those two systems.

Again, use the debugger to see what is happening in the stm32f207, and compare/contrast what is happening in the STM32H753 ...

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.

7 replies

mƎALLEm
ST Technical Moderator
June 18, 2024

Hello,

Please use the button </> to insert your code.

Please read these recommendations on how to post a thread: https://community.st.com/t5/community-guidelines/how-to-write-your-question-to-maximize-your-chances-to-find-a/ta-p/575228

Thank you for your understanding

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
PPopo.1
Senior
June 18, 2024

try resetting the buffer after transfer, also why different delays?

If full data is not received than it wont be sent as expected. Use a debugger to check if you receive what you are expecting.

Naresh_Author
Associate III
June 26, 2024

Hi 

      This is working in custom board but it's not working EVK board, what is issue?

 

Thank you 

mƎALLEm
ST Technical Moderator
July 24, 2024

Hello,


@Naresh_ wrote:

      This is working in custom board but it's not working EVK board, what is issue?


Need to check your hardware differences on both boards: what is connected externally on UART4 pins apart of your looping-back wire?

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
Naresh_Author
Associate III
July 24, 2024

Hi SofLit,

thank you quick response, 

yes loop back is also requirement, but in receive side some data+ ACK is also loop back.

  

 

Andrew Neil
Andrew NeilBest answer
Super User
July 24, 2024

@Naresh_ wrote:

I am receive data using UART 4 and transfer same data back like loopback but data is mismatch.

The commonest reason for receiving "junk" characters is wrong baud rate:

https://learn.sparkfun.com/tutorials/serial-communication/all#:~:text=If%20all%20the%20receiving%20device%20sees%20on%20its%20receive%20line%20is%20garbage%2C%20check%20to%20make%20sure%20the%20baud%20rates%20match%20up.

Baud rate depends on your clock setup - so check that.

Use the debugger to check if the corruption happens on reception or transmission - or both!

 


@Naresh_ wrote:

 same process in stm32f207 is working, 

So look carefully at the differences between those two systems.

Again, use the debugger to see what is happening in the stm32f207, and compare/contrast what is happening in the STM32H753 ...

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.