Skip to main content
Graduate
December 14, 2023
Solved

HSE UART issue

  • December 14, 2023
  • 3 replies
  • 4628 views

Hello STM32 Team.

I using uart1,3,6 setting for baud rate 115200,

Uart3 setting is it.

 huart3.Instance = USART3;
 huart3.Init.BaudRate = 115200;
 huart3.Init.WordLength = UART_WORDLENGTH_8B;
 huart3.Init.StopBits = UART_STOPBITS_1;
 huart3.Init.Parity = UART_PARITY_NONE;
 huart3.Init.Mode = UART_MODE_TX_RX;
 huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
 huart3.Init.OverSampling = UART_OVERSAMPLING_16;
 if (HAL_UART_Init(&huart3) != HAL_OK)
 {
 Error_Handler();
 }
 HAL_UART_Receive_IT(&huart3,&rxd3,1);

 

HSI using Uart is it ok,

But, HSE 8Mhz crystal, broken uart message.

Only difference function "void SystemClock_Config(void)"

How can i fix it?

 

[Setting HSI]

Terminal Message [ok]

 

KOPRO_2-1702538010113.png

 

void SystemClock_Config(void)
{
 RCC_OscInitTypeDef RCC_OscInitStruct = {0};
 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

 // Configure the main internal regulator output voltage

 __HAL_RCC_PWR_CLK_ENABLE();
 __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

 // Initializes the RCC Oscillators according to the specified parameters
 // in the RCC_OscInitTypeDef structure.

 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSE;
 RCC_OscInitStruct.LSEState = RCC_LSE_ON;
 RCC_OscInitStruct.HSIState = RCC_HSI_ON;
 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_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
 {
 Error_Handler();
 }
}

[Setting HSE]

Terminal message [broken]

KOPRO_0-1702537746382.png

 

project.ioc

KOPRO_1-1702537868066.png

 

this is my SystemClock_Config source

void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

// Configure the main internal regulator output voltage

__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

// Initializes the RCC Oscillators according to the specified parameters
// in the RCC_OscInitTypeDef structure.

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE|RCC_OSCILLATORTYPE_LSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.LSEState = RCC_LSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 216;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV6;
RCC_OscInitStruct.PLL.PLLQ = 2;
RCC_OscInitStruct.PLL.PLLR = 2;
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_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}

 

 

    This topic has been closed for replies.
    Best answer by KOPRO

    This issue has been resolved.
    Create a project without FreeRTOS and add only UART
    When tested, it worked normally.
    Afterwards, the .ioc file was stored for testing in FreeRTOS.
    I found that the Heap Size was set to 2048 for many task.
    This seems to have exceeded the Heap Size and caused problems in the UART. When the Heap Size was set to 1024, the UART was output normally.

     

    Thaks you,

     

    Best regards,

    3 replies

    Graduate II
    December 14, 2023

    Sorry, but what STM32 part are you talking about and on what board?

    Check HSE_VALUE is reflective of the actual HSE Frequency. 

    KOPROAuthor
    Graduate
    December 14, 2023

    Hello.

    I also checked that HSE_VALUE is correct.

    #define HSE_VALUE ((uint32_t)8000000) /*!< Default value of the External oscillator in Hz */

     

    Part name is "STM32F412RGT6"

    Include board schematic. 

    We are modifying and testing the circuit for the CTS RTS test of UART1.

    UART6 is used as CTS RTS, and UART3 is used as USB output debugger.

     

    KOPRO_0-1702555809607.png

     

    system_stm32f4xx.c

     

    #include "stm32f4xx.h"

    #if !defined (HSE_VALUE)
    #define HSE_VALUE ((uint32_t)8000000) /*!< Default value of the External oscillator in Hz */
    #endif /* HSE_VALUE */

    #if !defined (HSI_VALUE)
    #define HSI_VALUE ((uint32_t)16000000) /*!< Value of the Internal oscillator in Hz*/
    #endif /* HSI_VALUE */

    Technical Moderator
    December 14, 2023

    Hello,

    First try to output your system clock on MCO pin and check if you get your 36MHz. It could be something not good from your external crystal/resonator.

    KOPROAuthor
    Graduate
    December 15, 2023

    Like you said
    I measured the following using an oscilloscope.
    When in HSE mode, the frequency is abnormal as shown below.

    [When in HSE mode = Abnormal (Fail)]
    PA8(HSE), PC9 (SystemClock)
    CH1 = System clock = PC9(System Clock)
    CH2 = Crystal Clock = PA8(HSE) is 8Mhz

    [When in HSI mode = Normal (Pass)]
    CH1 = System clock = PC9(System Clock) is 15.96Mhz
    CH2 = Crystal Clock = PA8(HSI) is 15.96Mhz

    Below are photos and measured videos.

     

    ch1_20231215_132418134.jpgch1_20231215_132418134_01.jpgch1_20231215_132418134_02.jpgch1_20231215_131716421_01.jpg

     

     

     

    void SystemClock_Config(void)
    {
    	RCC_OscInitTypeDef RCC_OscInitStruct = {0};
    	RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
    	// Configure the main internal regulator output voltage
    	__HAL_RCC_PWR_CLK_ENABLE();
    	__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
    	// Initializes the RCC Oscillators according to the specified parameters
    	// in the RCC_OscInitTypeDef structure.
    	//	HSE Cystal = 8Mhz
    	//	SYSCLK = 36MHZ
    	RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE|RCC_OSCILLATORTYPE_LSE;
    	RCC_OscInitStruct.HSEState = RCC_HSE_ON;
    	RCC_OscInitStruct.LSEState = RCC_LSE_ON;
    	RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
    	RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
    	RCC_OscInitStruct.PLL.PLLM = 8;
    	RCC_OscInitStruct.PLL.PLLN = 216;
    	RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV6;
    	RCC_OscInitStruct.PLL.PLLQ = 2;
    	RCC_OscInitStruct.PLL.PLLR = 2;
    	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_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
    	RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    	RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
    	RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
    	if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
    	{
    		Error_Handler();
    	}
    	HAL_RCC_MCOConfig(RCC_MCO1, RCC_MCO1SOURCE_HSE, RCC_MCODIV_1); // PA8
    	HAL_RCC_MCOConfig(RCC_MCO2, RCC_MCO2SOURCE_SYSCLK, RCC_MCODIV_1); // PC9
    }

     

     

    KOPROAuthorAnswer
    Graduate
    December 15, 2023

    This issue has been resolved.
    Create a project without FreeRTOS and add only UART
    When tested, it worked normally.
    Afterwards, the .ioc file was stored for testing in FreeRTOS.
    I found that the Heap Size was set to 2048 for many task.
    This seems to have exceeded the Heap Size and caused problems in the UART. When the Heap Size was set to 1024, the UART was output normally.

     

    Thaks you,

     

    Best regards,

    Technical Moderator
    December 15, 2023

    But it doesn't explain the fact that it works with HSI and not with HSE :thinking_face:!

    But okay.. lucky you!