cancel
Showing results for 
Search instead for 
Did you mean: 

LPTIM does not count pulses from the COMP output

Valen_id
Associate II

I want to count the pulses coming from COMP2's output using LPTIM1. However, when I send it to Tera Term, the displayed LPTIM value is consistently 1, and it doesn't match the COMP2 output.

Has anyone had this problem? How did you resolve it?

I would greatly appreciate it if there is an expert who can assist in fixing this issue.

I am using STM32Cube IDE Version: 1.10.1.

My MCU is STM32L051C8T, on a custom board

This is my clock

Valen_id_0-1691160876385.png

This my LPTIM configuration

Valen_id_1-1691160953620.png

this COMP configuration

Valen_id_2-1691160988272.png

this my code

#include "main.h"
 
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "lc_sensor_metering.c"
#include "stdio.h"
#include "stdint.h"
#include "string.h"
#include "stm32l0xx_hal.h"
/* USER CODE END Includes */
 
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
LcSensor_Sensor_f LcSensor1;
LcSensor_LcStatus_f LcStatus;
/* USER CODE END PTD */
 
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
uint8_t aMsgBuffer[50];
/* USER CODE END PD */
 
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
 
/* USER CODE END PM */
 
/* Private variables ---------------------------------------------------------*/
COMP_HandleTypeDef hcomp2;
 
LPTIM_HandleTypeDef hlptim1;
 
UART_HandleTypeDef hlpuart1;
 
/* USER CODE BEGIN PV */
 
/* USER CODE END PV */
 
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_COMP2_Init(void);
static void MX_LPTIM1_Init(void);
static void MX_LPUART1_UART_Init(void);
/* USER CODE BEGIN PFP */
 
/* USER CODE END PFP */
 
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
 
/* USER CODE END 0 */
 
/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
LcSensor1.CountDetect = 2;
  /* 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_COMP2_Init();
  MX_LPTIM1_Init();
  MX_LPUART1_UART_Init();
  /* USER CODE BEGIN 2 */
 
  HAL_COMP_Start(&hcomp2);
  uint32_t counterValue = HAL_LPTIM_ReadCounter(&hlptim1);
  //sprintf((char*) aMsgBuffer, "Batas deteksi:%d\r\n",counterValue);
  //HAL_UART_Transmit(&hlpuart1, aMsgBuffer, TXBUFFERSIZE(aMsgBuffer),0xFFFF);
  //HAL_UART_Transmit(&hlpuart1, "dua", 10, 1000);
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
HAL_LPTIM_ReadCounter(&hlptim1);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, SET);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, SET);
LC_Init_GPIO();
// sprintf((char*) aMsgBuffer, "Batas deteksi:%d\r\n",LcSensor1.CountDetect);
// HAL_UART_Transmit(&hlpuart1, aMsgBuffer, TXBUFFERSIZE(aMsgBuffer),0xFFFF);
char buffer[50]; // Adjust the buffer size based on your needs
sprintf(buffer, "counter value: %3lu\r\n", counterValue);
HAL_UART_Transmit(&hlpuart1, (uint8_t*) buffer, strlen(buffer), HAL_MAX_DELAY);
  HAL_Delay(1000);
//sprintf(buffer, "counter value: %lu\r\n", counterValue);
//HAL_UART_Transmit(&hlpuart1, (uint8_t*) buffer, strlen(buffer), HAL_MAX_DELAY);
 
    /* USER CODE END WHILE */
 
    /* 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 the main internal regulator output voltage
  */
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
 
  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
 
  /** Initializes the CPU, AHB and APB buses clocks
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV16;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
 
  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_LPUART1|RCC_PERIPHCLK_LPTIM1;
  PeriphClkInit.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_PCLK1;
  PeriphClkInit.LptimClockSelection = RCC_LPTIM1CLKSOURCE_PCLK;
 
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
}
 
/**
  * @brief COMP2 Initialization Function
  * @PAram None
  * @retval None
  */
static void MX_COMP2_Init(void)
{
 
  /* USER CODE BEGIN COMP2_Init 0 */
 
  /* USER CODE END COMP2_Init 0 */
 
  /* USER CODE BEGIN COMP2_Init 1 */
 
  /* USER CODE END COMP2_Init 1 */
  hcomp2.Instance = COMP2;
  hcomp2.Init.InvertingInput = COMP_INPUT_MINUS_IO1;
  hcomp2.Init.NonInvertingInput = COMP_INPUT_PLUS_IO3;
  hcomp2.Init.LPTIMConnection = COMP_LPTIMCONNECTION_IN1_ENABLED;
  hcomp2.Init.OutputPol = COMP_OUTPUTPOL_NONINVERTED;
  hcomp2.Init.Mode = COMP_POWERMODE_MEDIUMSPEED;
  hcomp2.Init.WindowMode = COMP_WINDOWMODE_DISABLE;
  hcomp2.Init.TriggerMode = COMP_TRIGGERMODE_NONE;
  if (HAL_COMP_Init(&hcomp2) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN COMP2_Init 2 */
 
  /* USER CODE END COMP2_Init 2 */
 
}
 
/**
  * @brief LPTIM1 Initialization Function
  * @PAram None
  * @retval None
  */
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_APBCLOCK_LPOSC;
  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;
  if (HAL_LPTIM_Init(&hlptim1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN LPTIM1_Init 2 */
  if (HAL_LPTIM_Counter_Start(&hlptim1, 0xFFFF) != HAL_OK)
    {
      Error_Handler();
    }
  /* USER CODE END LPTIM1_Init 2 */
 
}
 
/**
  * @brief LPUART1 Initialization Function
  * @PAram None
  * @retval None
  */
static void MX_LPUART1_UART_Init(void)
{
 
  /* USER CODE BEGIN LPUART1_Init 0 */
 
  /* USER CODE END LPUART1_Init 0 */
 
  /* USER CODE BEGIN LPUART1_Init 1 */
 
  /* USER CODE END LPUART1_Init 1 */
  hlpuart1.Instance = LPUART1;
  hlpuart1.Init.BaudRate = 9600;
  hlpuart1.Init.WordLength = UART_WORDLENGTH_8B;
  hlpuart1.Init.StopBits = UART_STOPBITS_1;
  hlpuart1.Init.Parity = UART_PARITY_NONE;
  hlpuart1.Init.Mode = UART_MODE_TX_RX;
  hlpuart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  hlpuart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  hlpuart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  if (HAL_UART_Init(&hlpuart1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN LPUART1_Init 2 */
 
  /* USER CODE END LPUART1_Init 2 */
 
}
 
/**
  * @brief GPIO Initialization Function
  * @PAram None
  * @retval None
  */
static void MX_GPIO_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
 
  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOH_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();
 
  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1|GPIO_PIN_3, GPIO_PIN_RESET);
 
  /*Configure GPIO pins : PA1 PA3 */
  GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_3;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
}
 
/* USER CODE BEGIN 4 */
 
/* USER CODE END 4 */
 
/**
  * @brief  This function is executed in case of error occurrence.
  * @retval None
  */
void Error_Handler(void)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  __disable_irq();
  while (1)
  {
  }
  /* USER CODE END Error_Handler_Debug */
}

 

13 REPLIES 13
RhSilicon
Lead

Since you seem to be debugging the code through the serial port (Tera Term), I don't know if it can help in your case, but I found these videos about advanced debugging:

https://youtube.com/playlist?list=PLnMKNibPkDnEDEsV7IBXNvg7oNn3MfRd6

Sarra.S
ST Employee

Hello @Valen_id

maybe as a first step, verify that the LPTIM counter is running; read the value of the counter (since it's always showing value 1) 

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.

Oh yes I forgot to include it, is this what you mean?

Valen_id_0-1691163975492.png

 

If I detect it with an oscilloscope, the output on the COMP pin out is like this

Valen_id_1-1691164694209.jpeg

 

Read out and check/post content of COMP and LPTIM registers.

JW

Thank you for your reply, how do I get it

In debugger, you should have some kind of "peripherals view" window or something similar. I don't use CubeIDE so don't know where.

[EDIT] UM2609 chapter 5 Special Function Registers (SFRs)

JW

Is this what is meant?

Valen_id_2-1691284389579.pngValen_id_3-1691284425328.png

 

 

 

Yes.

And LPTIM_CNT is not 1 as you've said above.

JW