cancel
Showing results for 
Search instead for 
Did you mean: 

Issue with HSI not always configured after STOP mode in STM32Cube_FW_F0_V1.9.0

Klasson.Johannes
Associate II

I have an STM32F051R8 on a custom board and use the HAL library, and much of the code is based on the generated code from CubeMX. The problem I see is that about 1 out of every 10 times when I go to STOP mode the HSI is not configured as the current clock source after a wakeup from GPIO interrupt. From the datasheet: "When exiting Stop mode by issuing an interrupt or a wakeup event, the HSI oscillator is selected as system clock.".

I configure the clocks as follows:

        RCC_OscInitTypeDef RCC_OscInitStruct;
        RCC_ClkInitTypeDef RCC_ClkInitStruct;
 
        /**Initializes the CPU, AHB and APB busses clocks */
        RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
        RCC_OscInitStruct.HSIState = RCC_HSI_ON;
        RCC_OscInitStruct.HSICalibrationValue = 16;
        RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
        RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
        RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
        RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV1;
        if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
        {
            Error_Set(ERROR_MODULE_PWR, ERROR_PWR_OSC_CONFIG);
        }
 
        /**Initializes the CPU, AHB and APB busses clocks */
        RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1;
        RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
        RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
        RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
 
        if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
        {
            Error_Set(ERROR_MODULE_PWR, ERROR_PWR_CLK_CONFIG);
        }
 
        /**Configure the Systick */
        HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

As I use the PLL I need to reconfigure the clocks after each time I enter STOP mode, which is where I see the error. The function HAL_RCC_OscConfig() returns HAL_ERROR after the failed check if(__HAL_RCC_GET_SYSCLK_SOURCE() != RCC_SYSCLKSOURCE_STATUS_PLLCLK), which indicated that the PLL is already used as the current clock source.

So I started looking at the RCC->CFGR register directly after entering STOP mode. And when the error occurs the register is 0x328000a, i.e both SWS and SW are 10: PLL used as system clock. I have made sure that I actually call EnterSTOPMode (by calling HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_SLEEPENTRY_WFI)) before trying to reconfigure the clocks after a wakeup.

So I'm wondering if there is something wrong with my code/setup, or if this is an error with the STM32 hardware/the HAL library?

1 REPLY 1
Klasson.Johannes
Associate II

The issue has been found. To make sure that I don't miss a wakeup interrupt I call __disable_irq() before preparing to enter STOP mode, and directly after the WFI instruction I call __enable_irq() again.

So the issue occured if an interrupt occured after calling __disable_irq() so that an pending interrupt directly woke up the STM after going to STOP mode. In this case it does not seems like the clock was reconfigured to HSI, and therefor I got an error when I tried to reconfigure it.