cancel
Showing results for 
Search instead for 
Did you mean: 

What to do if an LPTIM takes too long to wake up STM32L476 from Stop 2 mode for the first time after every reset when LPTIM1 is used as a Timeout wakeup unit?

HShah.14
Associate

I am using STM32L476.

I want to enter the Stop 2 mode and use LPTIM1 as a Timeout wakeup unit.

The code is written such that the LPTIM1 wakes up the MCU from Stop 2 mode after every second.

The LPTIM1 takes too long to wakeup the MCU for the first time and once it wakes up the system for the first time, the code works fine and wakes up the MCU after very second until another reset.

What might be the problem and how can I fix it?

2 REPLIES 2

LPTIM_CFGR.PRELOAD set ?

Write a simple minimal program only running LPTIM1, outputting/toggling an output/LED, and observe its behaviour. Does it the same thing, i.e. first period is longer?

Read out and check the LPTIM1 registers before you start it running.

JW

The LPTIM1 code for toggling an LED works fine, It goes into the ISR every one second.

The code I am using for entering STOP 2 mode is here

void Config()

{

 /* USER CODE BEGIN Init */

 /* USER CODE END Init */

 /* Configure the system clock */

 SystemClock_Config();

 /* USER CODE BEGIN SysInit */

 /* USER CODE END SysInit */

 /* Initialize all configured peripherals */

 MX_GPIO_Init();

 MX_LPTIM1_Init();

 MX_UART4_Init();

 MX_USART2_UART_Init();

}

/* USER CODE END 0 */

/**

 * @brief The application entry point.

 * @retval int

 */

int main(void)

{

 /* USER CODE BEGIN 1 */

 /* USER CODE END 1 */

 /* MCU Configuration--------------------------------------------------------*/

 /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

// HAL_Init();

//

//

// /* USER CODE BEGIN Init */

//

// /* USER CODE END Init */

//

// /* Configure the system clock */

// SystemClock_Config();

//

// /* USER CODE BEGIN SysInit */

//

// /* USER CODE END SysInit */

//

// /* Initialize all configured peripherals */

// MX_GPIO_Init();

// MX_LPTIM1_Init();

// MX_UART4_Init();

// MX_USART2_UART_Init();

 /* USER CODE BEGIN 2 */

  // HAL_Delay(3000);

 //UART_Welcome();

// HAL_Delay(3000);

HAL_Init();

 Config();

 /* Start the Timeout function in interrupt mode

  * Period = 65535

  * Pulse = 32767

  * According to this configuration (LPTIMER clocked by LSE = 32.768 KHz and Compare = 32767)

   * The Timeout period = (Compare + 1) / LSE_Frequency = 1s */

 //UART_In();

   HAL_LPTIM_TimeOut_Start_IT(&hlptim1, PERIOD, PULSE);

//   __HAL_RCC_DBGMCU_CLK_ENABLE() ;

 // HAL_DBGMCU_EnableDBGStopMode();

   /* Enter in STOP2 mode */

   // UART_In();

 //HAL_LPTIM_TimeOut_Start_IT(&hlptim1, PERIOD, PULSE);

 /* USER CODE END 2 *4/

 /* Infinite loop */

 /* USER CODE BEGIN WHILE */

 while (1)

 {

  /* USER CODE END WHILE */

 UART_In_while();

 HAL_PWREx_EnterSTOP2Mode(PWR_STOPENTRY_WFI);

 SYSCLKConfig_STOP();

 //SYSCLKConfig_STOP();

 UART_out_while();

//UART_In();

  /* USER CODE BEGIN 3 */

 }

 /* USER CODE END 3 */

}

/**

 * @brief System Clock Configuration

 * @retval None

 */

void SystemClock_Config(void)

{

 RCC_OscInitTypeDef RCC_OscInitStruct = {0};

 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

 RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

 /** Configure LSE Drive Capability

 */

 HAL_PWR_EnableBkUpAccess();

 __HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);

 /** Initializes the CPU, AHB and APB busses clocks

 */

 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSE;

 RCC_OscInitStruct.LSEState = RCC_LSE_ON;

 RCC_OscInitStruct.HSIState = RCC_HSI_ON;

 RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;

 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;

 RCC_OscInitStruct.PLL.PLLM = 1;

 RCC_OscInitStruct.PLL.PLLN = 10;

 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;

 RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;

 RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;

 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_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)

 {

  Error_Handler();

 }

 PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2|RCC_PERIPHCLK_UART4

               |RCC_PERIPHCLK_LPTIM1;

 PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;

 PeriphClkInit.Uart4ClockSelection = RCC_UART4CLKSOURCE_PCLK1;

 PeriphClkInit.Lptim1ClockSelection = RCC_LPTIM1CLKSOURCE_LSE;

 if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)

 {

  Error_Handler();

 }

 /** Configure the main internal regulator output voltage

 */

 if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)

 {

  Error_Handler();

 }

}

void HAL_LPTIM_CompareMatchCallback(LPTIM_HandleTypeDef *hlptim)

{

 /* Timeout was reached, turn on LED1 */

Config();

  UART_In_Timeout();

}

void UART_In_while()

{

  char message5[] = "Enter in while\r\n";

  HAL_UART_Transmit(&huart4, message5, strlen(message5), 1000);

}

void UART_out_while()

{

  char message6[] = "End while\r\n";

  HAL_UART_Transmit(&huart4, message6, strlen(message6), 1000);

}

void UART_Welcome()

{

  char message[] = "We are in the Endgame now\r\n";

  HAL_UART_Transmit(&huart4, message, strlen(message), 1000);

}

void UART_Entry()

{

  char message1[] = "Enter Stop2 mode \r\n";

  HAL_UART_Transmit(&huart4, message1, strlen(message1), 1000);

}

void UART_In()

{

  char message2[] = "In Timeout \r\n";

  HAL_UART_Transmit(&huart4, message2, strlen(message2), 1000);

}

void UART_In_Timeout()

{

  char message3[] = "Timeout Reached \r\n";

  HAL_UART_Transmit(&huart4, message3, strlen(message3), 1000);

}

The LPTIM_CFGR_PRELOAD bit is given by this

 /* Clear CKSEL, PRESC, TRIGEN, TRGFLT, WAVPOL, PRELOAD & COUNTMODE bits */

 tmpcfgr &= (uint32_t)(~(LPTIM_CFGR_CKSEL | LPTIM_CFGR_TRIGEN | LPTIM_CFGR_PRELOAD |

             LPTIM_CFGR_WAVPOL | LPTIM_CFGR_PRESC | LPTIM_CFGR_COUNTMODE ));

Is there some problem with the System Clock