Skip to content

HSE Bypass setup attempt causes problems to the XTAL setup #4753

@LordGuilly

Description

@LordGuilly

Description

  • Type: Bug
  • Priority: Minor

Bug

Target
BLUEPILL_F103C8
although similar F1 platforms can be affected

Toolchain:
GCC_ARM

Toolchain version:

mbed-cli version:

mbed-os sha:
mbed_lib_145

DAPLink version:

Expected behavior
the SetSysClock function attempts to initialise the the bypass oscillator config first, then the Xtal oscillator, and finally the internal one. For this, it sets the HSEState variable of an RCC_OscInitTypeDef struct.
For the BLUEPILL, the system should fail the bypass and succeed with the Xtal, generating a SystemCoreClock of 72MHz

Actual behavior
The __HAL_RCC_HSE_CONFIG (of stm32f1xx_hal_rcc.c) sets the bits of the RCC_CR registry according to that state, but if it's HSE_ON, it does not clear the BYPASS flag. Because the BYPASS bit is set when trying to start in bypass mode, the attempt to use the xtal is not done correctly
The system then always falls to the HSI scenario, and ends up working at 64MHz

Steps to reproduce
start the system, and at main.c, print the system clock


Suggested enhancement
a proper fix would be to change the __HAL_RCC_HSE_CONFIG macro, with the follwoing code:

#define __HAL_RCC_HSE_CONFIG(__STATE__)                                     \
                    do{                                                     \
                      if ((__STATE__) == RCC_HSE_ON)                        \
                      {                                                     \
                        CLEAR_BIT(RCC->CR, RCC_CR_HSEBYP);                  \
                        SET_BIT(RCC->CR, RCC_CR_HSEON);                     \
                      }                                                     \
                      else if ((__STATE__) == RCC_HSE_OFF)                  \
                      {                                                     \
                        CLEAR_BIT(RCC->CR, RCC_CR_HSEON);                   \
                        CLEAR_BIT(RCC->CR, RCC_CR_HSEBYP);                  \
                      }                                                     \
                      else if ((__STATE__) == RCC_HSE_BYPASS)               \
                      {                                                     \
                        SET_BIT(RCC->CR, RCC_CR_HSEBYP);                    \
                        SET_BIT(RCC->CR, RCC_CR_HSEON);                     \
                      }                                                     \
                      else                                                  \
                      {                                                     \
                        CLEAR_BIT(RCC->CR, RCC_CR_HSEON);                   \
                        CLEAR_BIT(RCC->CR, RCC_CR_HSEBYP);                  \
                      }                                                     \
                    }while(0U)

another alternative would be to change the follwoing define at the BLUEPILL startup code (system_stm32f1xx.c)

#define USE_PLL_HSE_EXTC (0) /* Use external clock */

both solutions are not exclusive, and can be applied simultaneosly

Pros

Cons


Question

happy to implement the patch and generate a pull request, not sure if there are tests for this?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions