gismilyk

Forum Replies Created

Viewing 1 post (of 1 total)
  • Author
    Posts
  • gismilyk
    Participant

    Hi, all!

    for a long time I’ve been having the same problem – a nice working program jumpes to HAL_Error callback every time I press F5. As it was shown in Artem’s posts, something wrong was (allegedly) in openocd config files.
    I looked at the clock settings when my program was halted in the callback – they were settings from stm32F4x.cfg, not from CubeMX settings.
    Finally, it seems I found the reason of strange clock settings with a debugger attached.

    The stm32F4x.cfg contains following lines

        mmw 0x40023800 0x01000000 0 ;# RCC_CR |= PLLON

        sleep 10                    ;# Wait for PLL to lock

        mmw 0x40023808 0x00001000 0 ;# RCC_CFGR |= RCC_CFGR_PPRE1_DIV2

        mmw 0x40023808 0x00000002 0 ;# RCC_CFGR |= RCC_CFGR_SW_PLL

    which turns PLL on and make it a System Clock Source (the last one).
    If a program (as it is in my case) uses Cube generated SystemClock_Config() to set up clocks, it calls large HAL function HAL_RCC_OscConfig from stm32f4xx_hal_rcc.c, where one may find the following lines:

    /* Check if the PLL is used as system clock or not */

    if(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_CFGR_SWS_PLL)

    That is if System Clock Source is already PLL we finally get a HAL_Error. If not, PLL will be set up according to CubeMX settings.

    You may consider this as HAL bug or feature at your taste 😊
    Here is simple cure for this ‘feature’:
    In my main.cpp I’ve added the following:

    /* Configure the system clock */

    #ifdef DEBUG

    if (__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_CFGR_SWS_PLL)

    HAL_RCC_DeInit();

    #endif

    SystemClock_Config();

    /* USER CODE BEGIN SysInit */

    DeInit does the job – now when SystemClock_Config() is called it runs the right way.

    Note, that I’ve added  this snippet outside the USER CODE comments, so it will be deleted if you re-generate the code from STM32CubeIDE.

    Hope this may be helpful.

Viewing 1 post (of 1 total)