1

I'm trying to change the clock to HSE and half the clock speed in APB1 and 2 buses.Before calling HAL_RCC_ClockConfig() I am able to print the clock values through UART. After calling of HAL_RCC_ClockConfig() I can't able to print the clock values through UART. I've checked by trying printing values before the function call and after the function call. Can I know what's happened.

I've attached main.c here

    /*
 * main.c
 *
 *  Created on: 29-Jul-2022
 *      Author: ajith
 */

#include<string.h>
#include "stm32f4xx_hal.h"
#include "main.h"

#define TRUE 1
#define FALSE 0


void UART2_Init(void);
void Error_handler(void);


UART_HandleTypeDef huart2;

char  *user_data = "programme is running\r\n" ;


int main(void)
{
    RCC_OscInitTypeDef osc_init;
    RCC_ClkInitTypeDef clk_init;
    char msg[100];

    HAL_Init();

    UART2_Init();
    uint16_t data_len = strlen(user_data);
        if( HAL_UART_Transmit(&huart2, (uint8_t*) user_data,data_len,HAL_MAX_DELAY) != HAL_OK)
        {
            Error_handler();
        }

    //before clock changing

        memset(msg,0,sizeof(msg));
        sprintf(msg,"SYSCLK : %ldHz\r\n",HAL_RCC_GetSysClockFreq());
        HAL_UART_Transmit(&huart2,(uint8_t*)msg,strlen(msg),HAL_MAX_DELAY);

        memset(msg,0,sizeof(msg));
        sprintf(msg,"HCLK   : %ldHz\r\n",HAL_RCC_GetHCLKFreq());
        HAL_UART_Transmit(&huart2,(uint8_t*)msg,strlen(msg),HAL_MAX_DELAY);

        memset(msg,0,sizeof(msg));
        sprintf(msg,"PCLK1  : %ldHz\r\n",HAL_RCC_GetPCLK1Freq());
        HAL_UART_Transmit(&huart2,(uint8_t*)msg,strlen(msg),HAL_MAX_DELAY);

        memset(msg,0,sizeof(msg));
        sprintf(msg,"PCLK2  : %ldHz\r\n",HAL_RCC_GetPCLK2Freq());
        HAL_UART_Transmit(&huart2,(uint8_t*)msg,strlen(msg),HAL_MAX_DELAY);





    memset(&osc_init,0,sizeof(osc_init));
    osc_init.OscillatorType = RCC_OSCILLATORTYPE_HSE;
    osc_init.HSEState = RCC_HSE_BYPASS;
    if ( HAL_RCC_OscConfig(&osc_init) != HAL_OK)
    {
        Error_handler();
    }

    clk_init.ClockType = RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | \
                        RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
    clk_init.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
    clk_init.AHBCLKDivider = RCC_SYSCLK_DIV2;
    clk_init.APB1CLKDivider = RCC_HCLK_DIV2;
    clk_init.APB2CLKDivider = RCC_HCLK_DIV2;

   if( HAL_RCC_ClockConfig(&clk_init, FLASH_ACR_LATENCY_0WS) != HAL_OK)
    {
        Error_handler();
    }




 /*---------------------------- AFTER THIS LINE SYSCLK is SOURCED BY HSE------------------*/

    __HAL_RCC_HSI_DISABLE(); //Saves some current

    /* LETS REDO THE SYSTICK CONFIGURATION */

     HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

     HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

     /*printing values after initialization */

     UART2_Init();

    memset(msg,0,sizeof(msg));
    sprintf(msg,"SYSCLK : %ldHz\r\n",HAL_RCC_GetSysClockFreq());
    HAL_UART_Transmit(&huart2,(uint8_t*)msg,strlen(msg),HAL_MAX_DELAY);

    memset(msg,0,sizeof(msg));
    sprintf(msg,"HCLK   : %ldHz\r\n",HAL_RCC_GetHCLKFreq());
    HAL_UART_Transmit(&huart2,(uint8_t*)msg,strlen(msg),HAL_MAX_DELAY);

    memset(msg,0,sizeof(msg));
    sprintf(msg,"PCLK1  : %ldHz\r\n",HAL_RCC_GetPCLK1Freq());
    HAL_UART_Transmit(&huart2,(uint8_t*)msg,strlen(msg),HAL_MAX_DELAY);

    memset(msg,0,sizeof(msg));
    sprintf(msg,"PCLK2  : %ldHz\r\n",HAL_RCC_GetPCLK2Freq());
    HAL_UART_Transmit(&huart2,(uint8_t*)msg,strlen(msg),HAL_MAX_DELAY);

    while(1);


    return 0;
}



void UART2_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.HwFlowCtl = UART_HWCONTROL_NONE;
    huart2.Init.Mode = UART_MODE_TX_RX;
    if ( HAL_UART_Init(&huart2) != HAL_OK )
    {
        //There is a problem
        Error_handler();
    }


}


void Error_handler(void)
{
    while(1);
}

output on putty is:- putty output`

Can I know why UART not working after HAL_RCC_ClockConfig() call..?

  • Happened that you also changed timing of all the peripherals connected to APB1 and APB2 buses. So now the baude rate is something undefined. Reinitialize all clock sensitive peripherals, UART, SPI, etc, to the new base clock. before trying to retransmite anything on the serial line. – Frankie_C Jul 29 '22 at 07:55
  • I agree. I think you changed the clock of the MCU, but you also changed UART baud rate, which is derived from it. So it's working OK, it's just sending stuff at another baud rate. You need to change UART clock setting to set correct baud rate again, as far as I can tell. Can you test this theory, op? – Ilya Jul 29 '22 at 08:31
  • oh by the way I don't quite get how you're not getting a hardfault. You set 0 wait states for flash while using HSE clock. Weird. Should be more. Something like 5. Specifics are in the datasheet reference manual. Wait states vs clock speed. – Ilya Jul 29 '22 at 10:13
  • @liya I already reinitialized the UART again before printing the values in the code. The function UART2_Init() will do that for me. Still, I am getting the same output. – Ajith Pinninti Jul 29 '22 at 14:56
  • Compare UART registers after initialization before and after switching the clock (and initializing UART). With a breakpoint. Baudrate prescaler specifically, or whatever it's called. The divider value that makes your UART clock from system clock. Make sure they don't get set up with identical divider value, because there is possibility that UART2_Init() put a value thinking it's 16MHz system clock, while it's already not. – Ilya Jul 29 '22 at 19:15
  • As an experiment: on your terminal where you see messages from UART, increase baudrate by the same factor as you increased system clock by switching from HSI to HSE. Or half of that. If you see something in the terminal after clock switching, then it's exactly that - UART was initialized with prescaler value fixed for 16MHz and not for HSE. – Ilya Jul 29 '22 at 19:17
  • What is the external crystal oscillator frequency you have on your board? What do you think your SYSCLK is after your configuration? – hcheung Jul 30 '22 at 12:33
  • @hcheung I have no idea. I don't have this board. But internal is always 16MHz RC, while external can be anything (typically 16MHz or 25MHz), external is also usually fed to PLL to multiply its frequency and produce >50MHz. So if your system clock tripled, but UART init set up UART with the same parameters, your baud rate also tripled. Your terminal looks like there is communication, just wrong baud rate. This is a point to my version of what happened. – Ilya Aug 02 '22 at 08:31
  • @Ilya, I'm not the OP. I'm asking OP for the question. – hcheung Aug 03 '22 at 05:33

0 Answers0