cancel
Showing results for 
Search instead for 
Did you mean: 

stm32g0 lptim1 low power sleep

jlthompson
Associate II

I want to use LPTIM1 of the STM32G0x1 to wake up the MCU, if it's not already awake, and start a DMA multichannel single conversion. I think I know how to do the DMA/ADC part after getting the ADC_MultiChannelSingleConversion example running; I'll the code from that project to this one later.. But the most part is waking up. Here's the relevant part of what I have:

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();
  /* USER CODE BEGIN 2 */
  /* Enable Power Clock */
  __HAL_RCC_PWR_CLK_ENABLE();
  
  RCC->IOPSMENR  = 0x0000u;
  RCC->AHBSMENR  = 0x00300u;

  RCC->APBSMENR1 = 0x9000u;
  RCC->APBSMENR2 = 0x0000u;
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
	if (HAL_LPTIM_TimeOut_Start_IT(&hlptim1, PERIOD, TIMEOUT) != HAL_OK)
	{
	 Error_Handler();
	}

    /* Reduce the System clock to below 2 MHz */
    SystemClock_Decrease();

    /* Enter Sleep Mode, wake up is done once User push-button is pressed */
    HAL_PWR_EnterSLEEPMode(PWR_LOWPOWERREGULATOR_ON, PWR_SLEEPENTRY_WFI);

    /* ... Low-power SLEEP mode ... */
    __WFI();
    /* System is Low Power Run mode when exiting Low Power Sleep mode,
       disable low power run mode and reset the clock to initialization configuration */ 
    HAL_PWREx_DisableLowPowerRunMode();

    /* Configure the system clock for the RUN mode */
    SystemClock_Config();

    /* Re-init LED4 to toggle during Run mode */
    if (LED_FLASH_PERIOD == ++led_flash_count)
    {
    	led_flash_count = 0;
    	HAL_GPIO_TogglePin(LED_4_GPIO_Port, LED_4_Pin);
    }
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Configure the main internal regulator output voltage
  */
  HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE2);

  /** Initializes the RCC Oscillators according to the specified parameters
  * in the RCC_OscInitTypeDef structure.
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSIDiv = RCC_HSI_DIV1;
  RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  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_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

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

 I can tell the MCU goes to sleep because the STMLInk connection is lost, but it never wakes up. What am I missing?

When the MCU wakes up, I want the LPTim1 interrupt handler to call HAL_ADC_Start_DMA. While awake, I'll see if I've collected all the data I need. That will take much longer than the LPTIM1 Timeout period, but I need the periodic conversions to continue to collect ADC at the same time. Is this possible, and how?

Thanks!

2 REPLIES 2
jlthompson
Associate II

Also, I think I want LPTIM1 to operate as a simple internal clock counter, but I can't figure out how to configure it for that using the IDE Pinout & Configuration tool. How is that done?

I selected 'Counts internal clock events', but the function definition is disabled, so I can't call it.