cancel
Showing results for 
Search instead for 
Did you mean: 

Low power mode with ThreadX

jampino
Associate II

Hello all,

I am testing low power mode 2 using ThreadX but having a bit of a problem when waking up. I have generated a simple test, there is one task running blinking a led every second for 10 secs and then sending the MCU to low power mode 2, until this point all works okay.

I have got a button that will wake up the MCU by an interrupt when it is pressed and the unit should go and start blinking the LED every 1 sec for another 10 secs.

The MCU comes out of mode 2 when the the button is pressed but I am having a problem with the clock configuration when waking up, the led flashes at around 6 secs after waking up instead of every 1 sec.

Main routine:

VOID LED1_thread_entry(ULONG initial_input)
{
   uint8_t counter = 0;
   while(1)
   {
      HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin);
      tx_thread_sleep(1000);
      counter++;
      if(counter == 10)
      {
         counter = 0;
         Enter_LowPower_Mode();
       }
   }
}

This is my routine that put the MCU into low power mode 2:

 

 

void Enter_LowPower_Mode(void)
{
   HAL_GPIO_WritePin(LED1_GPIO_Port, LED1_Pin, GPIO_PIN_RESET);
   __HAL_RCC_WAKEUPSTOP_CLK_CONFIG(RCC_STOP_WAKEUPCLOCK_HSI);
   HAL_PWREx_EnterSTOP2Mode(PWR_STOPENTRY_WFI);
}

 

 


This is the routine exiting the lo power mode2:

 

 

void HAL_GPIO_EXTI_Rising_Callback(uint16_t GPIO_Pin)
{
   if(GPIO_Pin == Button_Pin)
   {
      Exit_LowPower_Mode();
   }
}

 

 

 

 

 

void Exit_LowPower_Mode(void)
{
   SystemClock_Config();
}

 

 

 

The clock configuration that I am using is:

 

 

void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE2) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB busses clocks
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMBOOST = RCC_PLLMBOOST_DIV1;
  RCC_OscInitStruct.PLL.PLLM = 1;
  RCC_OscInitStruct.PLL.PLLN = 13;
  RCC_OscInitStruct.PLL.PLLP = 2;
  RCC_OscInitStruct.PLL.PLLQ = 2;
  RCC_OscInitStruct.PLL.PLLR = 2;
  RCC_OscInitStruct.PLL.PLLRGE = RCC_PLLVCIRANGE_1;
  RCC_OscInitStruct.PLL.PLLFRACN = 4096;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }

  /** Initializes the CPU, AHB and APB busses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
                              |RCC_CLOCKTYPE_PCLK3;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB3CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK)
  {
    Error_Handler();
  }
}

 

 

 

Any idea what the problem could be?

Thank you 

4 REPLIES 4
Sarra.S
ST Employee

Hello @jampino

This is relatively known, as in some of our examples we already handle this by re-enabling the PLL and setting the correct clock source after waking up from low-power mode.

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Hi @Sarra.S,

Is this what you are referring to: https://github.com/STMicroelectronics/STM32CubeU5/blob/main/Projects/NUCLEO-U575ZI-Q/Applications/ThreadX/Tx_LowPower/Src/app_threadx.c#L118
if not, could you point me to one of your examples please.

Thank you

Yes! 

Exactly here, restoring the initial SystemClock configuration after exiting a low-power mode: 

void Exit_LowPower_Mode(void)
{
  /* Enable AHB APB Peripheral Clock */
  __HAL_RCC_AHB1_CLK_ENABLE();
  __HAL_RCC_AHB2_1_CLK_ENABLE();
  __HAL_RCC_AHB2_2_CLK_ENABLE();

  __HAL_RCC_APB1_CLK_ENABLE();
  __HAL_RCC_APB2_CLK_ENABLE();

  /* Reconfigure the system clock*/
  SystemClock_Restore();

 

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Thanks, tried before but still having the same results