HAL RCC Use Cases

User application sets the system at maximum frequency

@startuml
participant Application
participant HAL_RCC
participant HAL_PWR
participant HAL_FLASH
participant LL_RCC
participant RCC_REG
participant HAL

note over Application : Enable HSI oscillator
Application -> HAL_RCC : HAL_RCC_HSI_Enable(HAL_RCC_HSI_DIV1)
    HAL_RCC -> LL_RCC  : enable HSI oscillator
    LL_RCC --> RCC_REG
    LL_RCC <-- RCC_REG
    HAL_RCC <-- LL_RCC
alt timeout (HSI Ready flag not set)
    HAL_RCC --> Application : <font color=red> HAL_ERROR
else
    HAL_RCC --> Application : <font color=green> HAL_OK
end

note over Application : Configure PLL1 to output 520MHz on PLL1P output\n  hal_rcc_pll_config_t config_pll1;\n  config_pll1.pll_source = HAL_RCC_PLL_SRC_HSI;\n  config_pll1.pll_m = 8;\n  config_pll1.pll_n = 65;\n  config_pll1.pll_p = 1;\n  config_pll1.pll_q = 2;\n  config_pll1.pll_r = 2;\n config_pll1.pll_s = 2;\n config_pll1.pll_t = 2;\n config_pll1.pll_fracn = 0;\n  config_pll1.pll_input_range = HAL_RCC_PLL_INPUT_RANGE_4_8;\n  config_pll1.pll_vco_range = HAL_RCC_PLL_VCO_RANGE_384_1672;
Application -> HAL_RCC : HAL_RCC_PLL1_SetConfig(&config_pll1)
alt invalid param (Parameters are not valid)
    HAL_RCC --> Application : <font color=orange> HAL_INVALID_PARAM
else
    HAL_RCC -> RCC_REG : configure PLL1 with HSI as source, PLLP is 520Mhz
    RCC_REG --> HAL_RCC

    alt already active (PLL already active)
        HAL_RCC --> Application : <font color=red> HAL_ERROR
    else
        HAL_RCC --> Application : <font color=green> HAL_OK
    end
end

note over Application : Enable PLL1
Application -> HAL_RCC : HAL_RCC_PLL1_Enable()
    HAL_RCC -> RCC_REG : enable PLL1
    RCC_REG --> HAL_RCC
alt timeout (PLL1 Ready flag not set)
    HAL_RCC --> Application : <font color=red> HAL_ERROR
else
    HAL_RCC --> Application : <font color=green> HAL_OK
end

note over Application : Enable PLL1P output
Application -> HAL_RCC : HAL_RCC_PLL1_EnableOutput\n(HAL_RCC_PLL1_OUTPUT_P)
HAL_RCC -> LL_RCC : LL_RCC_PLL1_EnableOutput\n(LL_RCC_PLL1_OUTPUT_P)
LL_RCC -> RCC_REG : Enable PLL1P output

note over Application: Adjusting VOS level\nto increase performances
Application -> HAL_PWR : HAL_PWR_SetMainRegulatorVoltageScalingRange\n(HAL_PWR_VOLT_SCALE_0)

note over Application: Increasing the number\nof wait states\nbecause of higher\nSystem frequency
Application -> HAL_FLASH : HAL_FLASH_ITF_SetLatency\n(HAL_FLASH_ITF_LATENCY_7)

note over Application: Increasing the programming delay\nbecause of higher\nSystem frequency
Application -> HAL_FLASH : HAL_FLASH_ITF_SetProgrammingDelay\n(HAL_FLASH_ITF_PROGRAM_DELAY_3)

note over Application: Set AHBs and APBs prescalers
Application -> HAL_RCC : HAL_RCC_SetBusClockConfig(&config_bus)
HAL_RCC --> LL_RCC : LL_RCC_ConfigBusClock()
HAL_RCC --> Application : HAL_OK

note over Application : Set PLL as source\nclock of system clock
Application -> HAL_RCC : HAL_RCC_SetSYSCLKSource\n(HAL_RCC_SYSCLK_SRC_PLLCLK)
HAL_RCC -> LL_RCC : LL_RCC_SetSYSCLKSource\n(LL_RCC_SYS_CLKSOURCE_PLL1)
LL_RCC -> RCC_REG : set system clock source
HAL_RCC -> HAL_RCC : HAL_RCC_GetSYSCLKSource()
alt timeout (PLL not selected as clock source)
    HAL_RCC --> Application : <font color=red> HAL_ERROR
else
    HAL_RCC --> Application : <font color=green> HAL_OK
end

note over Application : Update SystemCoreClock
Application -> HAL : HAL_UpdateCoreClock
Application <-- HAL

@enduml

Called functions:

Reset the RCC clock configuration to the default system clock (HSI)

@startuml
participant Application
participant HAL_RCC
participant LL_RCC
participant HAL
== HAL_RCC_ResetSystemClock function ==
Application -> HAL_RCC : HAL_RCC_ResetSystemClock
note over HAL_RCC : Enable HSI
HAL_RCC -> LL_RCC : LL_RCC_HSI_SetDivider(LL_RCC_HSI_DIV_1)
HAL_RCC -> LL_RCC : LL_RCC_HSI_Enable()
LL_RCC --> HAL_RCC
alt timeout (HSI divider not ready)
    HAL_RCC --> Application : <font color=red> HAL_ERROR
else HSI divider ready
    alt timeout (HSI not ready)
        HAL_RCC --> Application : <font color=red> HAL_ERROR
    else HSI ready
        note over HAL_RCC : Select HSI\nas system clock source
        HAL_RCC -> LL_RCC : LL_RCC_SetSysClkSource\n(LL_RCC_SYS_CLKSOURCE_HSI)
        LL_RCC --> HAL_RCC
        HAL_RCC -> LL_RCC : LL_RCC_GetSysClkSource
        LL_RCC --> HAL_RCC
        alt timeout (HSI not selected as clock source)
            HAL_RCC --> Application : <font color=red> HAL_ERROR
        else HSI selected as clock source
            HAL_RCC -> LL_RCC : LL_RCC_SetBMPrescaler\n(LL_RCC_BMCLK_PRESCALER_1)
            note over HAL_RCC : Set SystemCoreClock\nto HSI_VALUE
            HAL_RCC -> HAL: HAL_InitTick
            HAL --> HAL_RCC: HAL_OK
            HAL_RCC --> Application: <font color=green> HAL_OK
        end
    end
end
@enduml

Called functions:

Enable HSE with unitary function example_1

@startuml
== Call HAL_RCC_HSE_Enable function (enable HSE) ==
participant Application
participant HAL_RCC
participant LL_RCC
participant RCC_REG

Application -> HAL_RCC : HAL_RCC_HSE_Enable(HAL_RCC_HSE_ON)
    HAL_RCC -> LL_RCC : LL_RCC_HSE_Enable()
    LL_RCC --> RCC_REG : enable HSE oscillator
    RCC_REG --> LL_RCC
    LL_RCC --> HAL_RCC : LL_RCC_HSE_IsReady()
alt successful case (HSE Ready flag set)
    HAL_RCC --> Application : <font color=green> HAL_OK
else timeout case (HSE Ready flag not set)
    HAL_RCC --> Application : <font color=red> HAL_ERROR
end
@enduml

Called functions:

  • HAL_RCC_HSE_Enable()

Enable HSE with unitary function example_2

@startuml
== HSE enable in Bypass mode ==
participant Application
participant HAL_RCC
participant LL_RCC
participant RCC_REG

Application -> HAL_RCC : HAL_RCC_HSE_Enable(HAL_RCC_HSE_BYPASS)
HAL_RCC -> LL_RCC : LL_RCC_HSE_EnableBypass()
LL_RCC -> RCC_REG
HAL_RCC -> LL_RCC : LL_RCC_HSE_IsReady()
LL_RCC -> RCC_REG
LL_RCC <-- RCC_REG
HAL_RCC <-- LL_RCC
alt successful case (HSE Ready flag set)
    HAL_RCC --> Application : HAL_OK
else timeout case (HSE Ready flag not set)
    HAL_RCC --> Application : HAL_ERROR
end
@enduml

Called functions:

  • HAL_RCC_HSE_Enable()

  • HAL_RCC_HSE_Disable()

Bus prescalers configuration

@startuml
== Call the global HAL_RCC_SetBusClockConfig ==
alt Increasing the number of wait states because of higher CPU frequency
Application -> HAL_FLASH : HAL_FLASH_ITF_GetLatency
HAL_FLASH --> Application : return Current_latency
note over Application : if HAL_FLASH_ITF_LATENCY_X > Current_latency
Application -> HAL_FLASH : HAL_FLASH_ITF_SetLatency\n(HAL_FLASH_ITF_LATENCY_X)
HAL_FLASH --> Application
end

note over Application : Fill hal_rcc_bus_clk_config_t structure\n(config_bus)
alt Increasing the BUS frequency divider
note over Application : if ({bus}_clk_divider > current_{bus}_clock)
Application -> HAL_RCC : HAL_RCC_SetBusClockConfig\n(&config_bus)
HAL_RCC -> RCC_REG : update {bus} dividers in RCC registers
RCC_REG --> HAL_RCC
HAL_RCC --> Application : <font color=green> HAL_OK
end
loop HSI is selected as System Clock Source
end
Application -> HAL_RCC : HAL_RCC_SetSYSCLKSource\n(HAL_RCC_SYSCLK_SRC_HSI)
HAL_RCC -> LL_RCC : set system\n clock source
LL_RCC -> RCC_REG
LL_RCC <-- RCC_REG
HAL_RCC <-- LL_RCC
Application <-- HAL_RCC
alt Decreasing the number of wait states because of lower CPU frequency
Application -> HAL_FLASH : HAL_FLASH_ITF_GetLatency
HAL_FLASH --> Application : return Current_latency
note over Application : if HAL_FLASH_ITF_LATENCY_X < Current_latency
Application -> HAL_FLASH : HAL_FLASH_ITF_SetLatency\n(HAL_FLASH_ITF_LATENCY_X)
HAL_FLASH --> Application
end
alt Decreasing the BUS frequency divider
note over Application : if ({bus}_clk_divider < current_{bus}_clock)
Application -> HAL_RCC : HAL_RCC_SetBusClockConfig\n(&config_bus)
HAL_RCC -> RCC_REG : update {bus} dividers in RCC registers
RCC_REG --> HAL_RCC
HAL_RCC --> Application : <font color=green> HAL_OK
end
@enduml

Called functions:

Usage of HAL_RCC_SetConfigMCO

@startuml
participant Application
participant HAL_RCC
participant HAL_GPIO
participant LL_RCC
== GPIO initialization outside HAL_RCC_SetConfigMCO ==
note over Application : Initialize MCO pin on PA8\n  hal_gpio_init_t init_struct;\n  init_struct.Mode = HAL_GPIO_MODE_AF_PP;\n  init_struct.Speed = HAL_GPIO_SPEED_FREQ_HIGH;\n  init_struct.Pull = HAL_GPIO_NOPULL;\n  init_struct.Alternate = HAL_GPIO_AF0_MCO;
Application -> HAL_RCC : HAL_RCC_GPIOA_EnableClock
HAL_RCC -> LL_BUS : LL_AHB4_GRP1_EnableClock\n(LL_AHB4_GRP1_PERIPH_GPIOA)
LL_BUS -> RCC_REG
Application -> HAL_GPIO : HAL_GPIO_Init(HAL_GPIOA, &init_struct, HAL_GPIO_PIN_8)
Application <-- HAL_GPIO
== Call HAL_RCC_SetConfigMCO function ==
Application -> HAL_RCC : HAL_RCC_SetConfigMCO(HAL_RCC_MCO1_SRC_SYSCLK, \nHAL_RCC_MCO1_PRESCALER4)
HAL_RCC -> LL_RCC : LL_RCC_ConfigMCO(LL_RCC_MCO1_SRC_SYSCLK, \nLL_RCC_MCO1_PRESCALER_4)
LL_RCC -> RCC_REG
RCC_REG --> LL_RCC
HAL_RCC <-- LL_RCC
Application <-- HAL_RCC
@enduml

Called functions: