cancel
Showing results for 
Search instead for 
Did you mean: 

HO to put STM32 IoT node into deep sleep and wake up every hour from alarm clock.

ambinabhi
Associate II

HI

I am using the STM32L475 IoT discovery kit, I want put my board into deep sleep and wake up every one hour based on event, I was considering setting up alarm clock. I am using x-cube-cld-gen library'r MQTT example.

For now i have scene other examples and set up the alarm sample code. But i don't get the HAL_RTC_AlarmAEventCallback trigger. The clock just keeps on running.

/* Configure the system clock */
  SystemClock_Config();
 
  Periph_Config();
 
  BSP_LED_Init(LED_GREEN);
 // BSP_PB_Init(BUTTON_USER, BUTTON_MODE_EXTI);
 
  /* RNG init function */
  hrng.Instance = RNG;
  if (HAL_RNG_Init(&hrng) != HAL_OK)
  {
    Error_Handler();
  }
 
  /* UART console init */
 Console_UART_Init();
 
  hrtc.Instance = RTC;
  /* RTC init */
  RTC_Init();
  RTC_AlarmConfig();
 
#ifdef FIREWALL_MBEDLIB
  firewall_init();
#endif
  
  while(1){
	  RTC_TimeShow(aShowTime);
	  if(wake_up_time){
		  printf("I woke up on time....\r\n");
	  }
	 HAL_Delay(1000);
  }
void Periph_Config(void)
 {
  RCC_PeriphCLKInitTypeDef PeriphClkInit;
  PeriphClkInit.PeriphClockSelection  = RCC_PERIPHCLK_RTC|RCC_PERIPHCLK_USART1
                                       |RCC_PERIPHCLK_USART3|RCC_PERIPHCLK_I2C2
                                       |RCC_PERIPHCLK_RNG;
  PeriphClkInit.Usart1ClockSelection  = RCC_USART1CLKSOURCE_PCLK2;
  PeriphClkInit.Usart3ClockSelection  = RCC_USART3CLKSOURCE_PCLK1;
  PeriphClkInit.I2c2ClockSelection    = RCC_I2C2CLKSOURCE_PCLK1;
  PeriphClkInit.RngClockSelection     = RCC_RNGCLKSOURCE_MSI;
 
  PeriphClkInit.RTCClockSelection     = RCC_RTCCLKSOURCE_LSE;
 
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
 
   __HAL_RCC_PWR_CLK_ENABLE();
 
  if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  {
    Error_Handler();
  }
}
static void RTC_Init(void)
{
    /**Initialize RTC Only */
  hrtc.Instance = RTC;
  hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
  hrtc.Init.AsynchPrediv = 0x7F;
  hrtc.Init.SynchPrediv = 0xF9;
  hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
#ifdef RTC_OUTPUT_REMAP_NONE
  hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
#endif
  hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  if (HAL_RTC_Init(&hrtc) != HAL_OK)
  {
    Error_Handler();
  }
}
static void RTC_AlarmConfig(void)
{
  printf("Setting alarm...\r\n");
  RTC_DateTypeDef  sdatestructure;
    RTC_TimeTypeDef  stimestructure;
    RTC_AlarmTypeDef salarmstructure;
 
    /*##-1- Configure the Date #################################################*/
      /* Set Date: Tuesday February 18th 2014 */
      sdatestructure.Year = 0x14;
      sdatestructure.Month = RTC_MONTH_FEBRUARY;
      sdatestructure.Date = 0x18;
      sdatestructure.WeekDay = RTC_WEEKDAY_TUESDAY;
 
      if(HAL_RTC_SetDate(&hrtc,&sdatestructure,RTC_FORMAT_BCD) != HAL_OK)
      {
        /* Initialization Error */
        Error_Handler();
      }
 
      /*##-2- Configure the Time #################################################*/
      /* Set Time: 02:20:00 */
      stimestructure.Hours = 0x02;
      stimestructure.Minutes = 0x20;
      stimestructure.Seconds = 0x00;
      stimestructure.TimeFormat = RTC_HOURFORMAT12_AM;
      stimestructure.DayLightSaving = RTC_DAYLIGHTSAVING_NONE ;
      stimestructure.StoreOperation = RTC_STOREOPERATION_RESET;
 
      if(HAL_RTC_SetTime(&hrtc,&stimestructure,RTC_FORMAT_BCD) != HAL_OK)
      {
        /* Initialization Error */
        Error_Handler();
      }
 
      /*##-3- Configure the RTC Alarm peripheral #################################*/
      /* Set Alarm to 02:20:30
         RTC Alarm Generation: Alarm on Hours, Minutes and Seconds */
      salarmstructure.Alarm = RTC_ALARM_A;
      salarmstructure.AlarmDateWeekDay = RTC_WEEKDAY_MONDAY;
      salarmstructure.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_WEEKDAY;
      salarmstructure.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY;
      salarmstructure.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_NONE;
      salarmstructure.AlarmTime.TimeFormat = RTC_HOURFORMAT12_AM;
      salarmstructure.AlarmTime.Hours = 0x02;
      salarmstructure.AlarmTime.Minutes = 0x20;
      salarmstructure.AlarmTime.Seconds = 0x30;
      salarmstructure.AlarmTime.SubSeconds = 0x56;
 
      if(HAL_RTC_SetAlarm_IT(&hrtc,&salarmstructure,RTC_FORMAT_BCD) != HAL_OK)
      {
        /* Initialization Error */
        Error_Handler();
      }
}
void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
{
  /* Turn LED2 on: Alarm generation */
  wake_up_time = true;
  BSP_LED_On(LED2);
}

I was using the STM32L476G-EVAL's RTC_Time project as reference from the stm32cubel4 library samples.

0 REPLIES 0