cancel
Showing results for 
Search instead for 
Did you mean: 

Issue with LPTIM1 in Low Power Mode for Pulse Counting and Interrupt Triggering on STM32WL55JC16

Embedded_Engi_2
Associate

Hello STM32 community,

I am currently working on an embedded application using the LPTIM1 (Low Power Timer Mode) on an STM32WL55JC16 microcontroller. My goal is to count pulses from an external signal connected to the BP5 pin (assuming this is the correct pin for pulse input), and after counting 500 pulses, I want to trigger an interrupt.
However, I am facing some issues in achieving this functionality. Here is the general setup:


Pulse Source: (Flow Sensor) External pulse signal connected to BP5 pin.
Timer Mode: LPTIM1 in low-power mode.
Desired Behavior: Count up to 500 pulses, then trigger an interrupt.

Issue:

  • After setting up the LPTIM1 and starting the timer, the program counter reaches HAL_Init and continues initialization, then enters the LPTIM1_StartWithCompare(compare_value) function.
  • The timer starts but gets stuck in Error_Handler() after this function is called.

I have shared the code below, and I am uncertain about whether this issue is related to clock configuration or the timer mode.

 
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/

#include "main.h"



/* Private includes ----------------------------------------------------------*/

/* USER CODE BEGIN Includes */

#include<stdio.h>

#include<string.h>



uint16_t compare_value = 500;

HAL_StatusTypeDef timer_error;

/* Private variables ---------------------------------------------------------*/

LPTIM_HandleTypeDef hlptim1;



UART_HandleTypeDef huart1;

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_LPTIM1_Init(void);

static void MX_USART1_UART_Init(void);

/* USER CODE BEGIN PFP */

void LPTIM1_StartWithCompare(uint16_t);

void Enter_LowPowerMode(void);

/* USER CODE END PFP */

int main(void)

{

HAL_Init();

SystemClock_Config();

/* Initialize all configured peripherals */

MX_GPIO_Init();

MX_LPTIM1_Init();

MX_USART1_UART_Init();

/* USER CODE BEGIN 2 */



LPTIM1_StartWithCompare(compare_value);



/* USER CODE END 2 */

HAL_PWREx_ReleaseCore(PWR_CORE_CPU2);



/* Infinite loop */

/* USER CODE BEGIN WHILE */

// HAL_LPTIM_CompareMatchCallback(&hlptim1);

while (1)

{

/* USER CODE END WHILE */



/* USER CODE BEGIN 3 */

}

/* USER CODE END 3 */

}



This is my clock configuration:

void SystemClock_Config(void)

{

RCC_OscInitTypeDef RCC_OscInitStruct = {0};

RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};



/** Configure LSE Drive Capability

*/

HAL_PWR_EnableBkUpAccess();

__HAL_RCC_LSEDRIVE_CONFIG(RCC_LSEDRIVE_LOW);



/** Configure the main internal regulator output voltage

*/

__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);



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

*/

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE|RCC_OSCILLATORTYPE_MSI;

RCC_OscInitStruct.LSEState = RCC_LSE_ON;

RCC_OscInitStruct.MSIState = RCC_MSI_ON;

RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;

RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_6;

RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;

if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

{

Error_Handler();

}



/** Configure the SYSCLKSource, HCLK, PCLK1 and PCLK2 clocks dividers

*/

RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK3|RCC_CLOCKTYPE_HCLK2

|RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK

|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;

RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;

RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

RCC_ClkInitStruct.AHBCLK2Divider = RCC_SYSCLK_DIV1;

RCC_ClkInitStruct.AHBCLK3Divider = RCC_SYSCLK_DIV1;



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

{

Error_Handler();

}

}

This is LPTIM1 Initialization

static void MX_LPTIM1_Init(void)

{



/* USER CODE BEGIN LPTIM1_Init 0 */



/* USER CODE END LPTIM1_Init 0 */



/* USER CODE BEGIN LPTIM1_Init 1 */



/* USER CODE END LPTIM1_Init 1 */

hlptim1.Instance = LPTIM1;

hlptim1.Init.Clock.Source = LPTIM_CLOCKSOURCE_ULPTIM;

hlptim1.Init.Clock.Prescaler = LPTIM_PRESCALER_DIV1;

hlptim1.Init.UltraLowPowerClock.Polarity = LPTIM_CLOCKPOLARITY_RISING;

hlptim1.Init.UltraLowPowerClock.SampleTime = LPTIM_CLOCKSAMPLETIME_DIRECTTRANSITION;

hlptim1.Init.Trigger.Source = LPTIM_TRIGSOURCE_SOFTWARE;

hlptim1.Init.OutputPolarity = LPTIM_OUTPUTPOLARITY_HIGH;

hlptim1.Init.UpdateMode = LPTIM_UPDATE_IMMEDIATE;

hlptim1.Init.CounterSource = LPTIM_COUNTERSOURCE_EXTERNAL;

hlptim1.Init.Input1Source = LPTIM_INPUT1SOURCE_GPIO;

hlptim1.Init.Input2Source = LPTIM_INPUT2SOURCE_GPIO;

if (HAL_LPTIM_Init(&hlptim1) != HAL_OK)

{

Error_Handler();

}

/* USER CODE BEGIN LPTIM1_Init 2 */

__HAL_LPTIM_ENABLE_IT(&hlptim1,LPTIM_IT_CMPM);

HAL_NVIC_SetPriority(LPTIM1_IRQn,0,0);

HAL_NVIC_EnableIRQ(LPTIM1_IRQn);



/* USER CODE END LPTIM1_Init 2 */



}

And this is my functions

/* USER CODE BEGIN 4 */

void LPTIM1_StartWithCompare( uint16_t compare_value)

{

__HAL_LPTIM_COMPARE_SET(&hlptim1, compare_value);

if (HAL_LPTIM_Counter_Start_IT(&hlptim1, 0xFFFF) != HAL_OK)

{

Error_Handler();

}

}



void Enter_LowPowerMode(void)

{

HAL_PWREx_EnableLowPowerRunMode();

HAL_PWR_EnterSTOPMode(PWR_MAINREGULATOR_ON, PWR_STOPENTRY_WFI);

}



void HAL_LPTIM_CompareMatchCallback(LPTIM_HandleTypeDef *hlptim)

{

const char str[] = "500 pulse Limit Reached";

if (hlptim->Instance == LPTIM1)

{

HAL_UART_Transmit(&huart1, (const uint8_t *)str, strlen(str), HAL_MAX_DELAY);

}

}


and this is the body of my function and after program counter enters into the function it getting stuck into Error_Handler So my question is that why this is happening ?
is there any clock configuration required or I did anything wrong with LPTIM1 mode.

And also I have attached the zip file of my project.

1 REPLY 1
Sarra.S
ST Employee

Hello @Embedded_Engi_2

>>assuming this is the correct pin for pulse input

yes it is. 

SarraS_0-1732724387836.png

there is a similar example in the provided CubeFW named LPTIM_PulseCounter: STM32CubeWL/Projects/NUCLEO-WL55JC/Examples/LPTIM/LPTIM_PulseCounter at main · STMicroelectronics/STM32CubeWL · GitHub 

 

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.