cancel
Showing results for 
Search instead for 
Did you mean: 

UART loop in STM32H753

Naresh_
Associate III

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 */

}
1 ACCEPTED SOLUTION

Accepted Solutions
Andrew Neil
Evangelist III

@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 ...

View solution in original post

7 REPLIES 7
SofLit
ST Employee

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 on "Accept as Solution" on the reply which solved your issue or answered your question.
PPopo.1
Senior

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_
Associate III

Hi 

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

 

Thank you 

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 on "Accept as Solution" on the reply which solved your issue or answered your question.

Hi SofLit,

thank you quick response, 

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

  

 

What do you mean by "ACK"?

Again check your HW and the differences between the boards on UART4_TX and UART4_RX pins

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.
Andrew Neil
Evangelist III

@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 ...