cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L4R9ZI, adding rtc wakeup standby mode to pre existing examples doesn't work

reokyouma
Associate III

When I created a new project using the stm32cubeide, and set the new project to be able to go to standby mode and then wakeup using RTC timer, it works.

But when I try to add the standby mode with RTC Wakeup timer to a pre-existing example project, I can't seem to be able to wakeup from sleep but I am using the same configurations that I use in the new/blank project.

For Context, I am using the HAL Libraries to produce the standby mode and RTC Wakeup timer and adding it to the fp-ind-predmnt1 example project of ST, is there anything that I missed doing to configure the example project? also this example project doesn't have ioc so I don't know to configure anything that needs stm32cubemx ioc files

4 REPLIES 4

Hello @reokyouma​ 

have you added the code for enabling the WakeUp ?

Regards,

Nesrine

am i right to assume this one ?

HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN1);

I tried it but It didn't work out

uint8_t predrun =0;
int main(void)
{
  HAL_Init();
  
    HAL_PWREx_EnableVddIO2();
  __HAL_RCC_PWR_CLK_ENABLE();
  HAL_PWREx_EnableVddUSB();
 
  /* Configure the System clock */
  SystemClock_Config();
 
 
  RTC_init();
 
  if(__HAL_PWR_GET_FLAG(PWR_FLAG_SB) != RESET){
	  __HAL_PWR_CLEAR_FLAG(PWR_FLAG_SB);
	  __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
	  __HAL_RTC_ALARM_CLEAR_FLAG(&hrtc,RTC_FLAG_ALRAF);
  }
  predrun =0;
  InitTargetPlatform(TARGET_STWIN);
  
  t_stwin = HAL_GetTick();
  
  /* For enabling CRC clock for using motion libraries (for checking if STM32 microprocessor is used)*/
  MX_CRC_Init();
  
  /* Check the MetaDataManager */
  InitMetaDataManager((void *)&known_MetaData,MDM_DATA_TYPE_GMD,NULL); 
  
  PREDMNT1_PRINTF("\n\t(HAL %ld.%ld.%ld_%ld)\r\n"
        "\tCompiled %s %s"
 
#if defined (__IAR_SYSTEMS_ICC__)
        " (IAR)\r\n"
#elif defined (__CC_ARM)
        " (KEIL)\r\n"
#elif defined (__GNUC__)
        " (STM32CubeIDE)\r\n"
#endif
         "\tSend Every %4dmS Temperature/Humidity/Pressure\r\n"
         "\tSend Every %4dmS Acc/Gyro/Magneto\r\n"
         "\tSend Every %4dmS dB noise\r\n\n",
           HAL_GetHalVersion() >>24,
          (HAL_GetHalVersion() >>16)&0xFF,
          (HAL_GetHalVersion() >> 8)&0xFF,
           HAL_GetHalVersion()      &0xFF,
         __DATE__,__TIME__,
         PERIOD_ENVIRONMENTAL,
         PERIOD_ACC_GYRO_MAG,
         MICS_DB_UPDATE_MUL_10MS * 10);
 
#ifdef PREDMNT1_DEBUG_CONNECTION
  PREDMNT1_PRINTF("Debug Connection         Enabled\r\n");
#endif /* PREDMNT1_DEBUG_CONNECTION */
 
#ifdef PREDMNT1_DEBUG_NOTIFY_TRAMISSION
  PREDMNT1_PRINTF("Debug Notify Trasmission Enabled\r\n\n");
#endif /* PREDMNT1_DEBUG_NOTIFY_TRAMISSION */
 
  /* Set Node Name */
  ReCallNodeNameFromMemory();
  
  HCI_TL_SPI_Reset();
 
  /* Initialize the BlueNRG */
  Init_BlueNRG_Stack();
 
  /* Initialize the BlueNRG Custom services */
  Init_BlueNRG_Custom_Services();  
  
  /* Check the BootLoader Compliance */
  PREDMNT1_PRINTF("\r\n");
  if(CheckBootLoaderCompliance()) {
    PREDMNT1_PRINTF("BootLoader Compliant with FOTA procedure\r\n\n");
  } else {
    PREDMNT1_PRINTF("ERROR: BootLoader NOT Compliant with FOTA procedure\r\n\n");
  }
 
  /* Set Accelerometer Full Scale to 2G */
  Set2GAccelerometerFullScale();
 
  /* initialize timers */
  InitTimers();
  
  /* Predictive Maintenance Initialization */
  InitPredictiveMaintenance();
  
  /* Infinite loop */
  while (1)
  {
    /* Led Blinking when there is not a client connected */
    if(!connected)
    {
      if(!TargetBoardFeatures.LedStatus) {
        if(!(HAL_GetTick()&0x3FF)) {
          LedOnTargetPlatform();
        }
      } else {
        if(!(HAL_GetTick()&0x3F)) {
          LedOffTargetPlatform();
        }
      }
    }
 
    if(set_connectable){     
      if(NecessityToSaveMetaDataManager) {
        uint32_t Success = EraseMetaDataManager();
        if(Success) {
          SaveMetaDataManager();
        }
      }
 
      /* Now update the BLE advertize data and make the Board connectable */
      setConnectable();
      set_connectable = FALSE;
    }
 
    /* Handle user button */
    if(ButtonPressed) {
      ButtonCallback();
      ButtonPressed=0;       
    }
    
    if(PredictiveMaintenance){
      if (IsFirstTime)
      {
        IsFirstTime = 0;
 
	/* Initializes the MotionSP Vibration parameters */
        MotionSP_VibrationInit();
		
        if(FFT_Alarm)
        {
          /* Initialization of Alarm Status on Axes, Alarm Values Reported
             and Thresholds to detect WARNING and ALARM conditions */
          MotionSP_TimeDomainAlarmInit(&sTdAlarm, &sTimeDomainVal, &sTdRmsThresholds, &sTdPkThresholds);
          
          /* Frequency domain initialization of Alarm Status */
          MotionSP_FreqDomainAlarmInit(&FDWarnThresh, &FDAlarmThresh, &THR_Fft_Alarms, MotionSP_Parameters.subrange_num);
        }
        
        /* Configure the FIFO settings in according with others parammeters changed */
        MotionSP_ConfigFifo();
        
        enable_FIFO();
        
        /* Start of the Time Window */
        Start_Tick = HAL_GetTick();
      }
	  
      MotionSP_VibrationAnalysis();
      predrun=1;
    }
 
    /* handle BLE event */
    if(HCI_ProcessEvent) {
      HCI_ProcessEvent=0;
      hci_user_evt_proc();
    }
 
    /* Environmental Data */
    if(SendEnv) {
      SendEnv=0;
      SendEnvironmentalData();
    }
    
    /* Mic Data */
    if (SendAudioLevel) {
      SendAudioLevel = 0;
      SendAudioLevelData();
    }
 
    /* Motion Data */
    if(SendAccGyroMag) {
      SendAccGyroMag=0;
      SendMotionData();
    }
    
    /* Battery Info Data */
    if(SendBatteryInfo){
      SendBatteryInfo=0;
      SendBatteryInfoData();
    }
    if(!connected && predrun == 1){
            	predrun=0;
            	RTC_CalendarConfig();
            	HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN1);
            	__HAL_RTC_ALARM_CLEAR_FLAG(&hrtc,RTC_FLAG_ALRAF);
            		  __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
 
            		  RTC_AlarmConfig();
//            	HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 0xFFFF, RTC_WAKEUPCLOCK_RTCCLK_DIV16);
            	HAL_PWR_EnterSTANDBYMode();
        }
    /* Wait for Event */
    //__WFI();
  }
}
 
 
void RTC_init(void){
	hrtc.Instance = RTC;
	hrtc.Init.HourFormat = RTC_HOURFORMAT_12;
	hrtc.Init.AsynchPrediv = 0x7F;
	hrtc.Init.SynchPrediv = 0xFF;
	hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
	hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
	hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
 
	if(HAL_RTC_Init(&hrtc) != HAL_OK){
		Error_Handler();
	}
 
}
void RTC_CalendarConfig(void){
	//this function does the rtc calendar config
 
	RTC_TimeTypeDef RTC_TimeInit;
	RTC_TimeInit.Hours = 12;
	RTC_TimeInit.Minutes = 11;
	RTC_TimeInit.Seconds = 0;
	RTC_TimeInit.TimeFormat = RTC_HOURFORMAT12_AM;
	HAL_RTC_SetTime(&hrtc, &RTC_TimeInit, RTC_FORMAT_BIN);
 
	RTC_DateTypeDef RTC_DateInit;
	RTC_DateInit.Date = 12;
	RTC_DateInit.Month = RTC_MONTH_JUNE;
	RTC_DateInit.Year = 18;
	RTC_DateInit.WeekDay = RTC_WEEKDAY_TUESDAY;
	HAL_RTC_SetDate(&hrtc, &RTC_DateInit, RTC_FORMAT_BIN);
 
 
 
}
 
void HAL_RTC_AlarmAEventCallback(RTC_HandleTypeDef *hrtc)
{
 
  /* Prevent unused argument(s) compilation warning */
//  UNUSED(hrtc);
 
  /* NOTE : This function should not be modified, when the callback is needed,
            the HAL_RTC_AlarmAEventCallback could be implemented in the user file
   */
}
 
void RTC_AlarmConfig(void){
 
	RTC_AlarmTypeDef RTC_AlarmInit;
 
	memset(&RTC_AlarmInit,0,sizeof(RTC_AlarmInit));
 
	HAL_RTC_DeactivateAlarm(&hrtc, RTC_ALARM_A);
 
	RTC_AlarmInit.Alarm = RTC_ALARM_A;
	RTC_AlarmInit.AlarmTime.Hours = 12;
	RTC_AlarmInit.AlarmTime.Minutes = 11;
	RTC_AlarmInit.AlarmTime.Seconds=10;
 
	RTC_AlarmInit.AlarmTime.TimeFormat = RTC_HOURFORMAT12_AM;
	RTC_AlarmInit.AlarmMask = RTC_ALARMMASK_DATEWEEKDAY;
	RTC_AlarmInit.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_NONE;
	if(HAL_RTC_SetAlarm(&hrtc, &RTC_AlarmInit, RTC_FORMAT_BIN) != HAL_OK){
		Error_Handler();
	}
}

should be the whole code

try this code,

/** Enable the WakeUp

*/

if (HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 0, RTC_WAKEUPCLOCK_RTCCLK_DIV16) != HAL_OK)

{

Error_Handler();

}

unfortunately it did't work

Correct me if I am wrong but aside from the code above, I need put this into the msp.c right?

void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc)
{
  //1. turn on LSE
  RCC_OscInitTypeDef RCC_OscInitStruct;
 
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
  RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK){
	  Error_Handler();
  }
 
  //2 select LSE as RTC CLK
  RCC_PeriphCLKInitTypeDef RCC_RTCPeriphCLKInit;
  RCC_RTCPeriphCLKInit.PeriphClockSelection = RCC_PERIPHCLK_RTC;
  RCC_RTCPeriphCLKInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
  if(HAL_RCCEx_PeriphCLKConfig(&RCC_RTCPeriphCLKInit) != HAL_OK){
	  Error_Handler();
  }
 
  // 3 Enable the RTC Clock
  __HAL_RCC_RTC_ENABLE();
 
  //4 enable interrupt
  HAL_NVIC_SetPriority(RTC_Alarm_IRQn,15,0);
  HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
 
}
 
/**
* @brief RTC MSP De-Initialization
* This function freeze the hardware resources used in this example
* @param hrtc: RTC handle pointer
* @retval None
*/
void HAL_RTC_MspDeInit(RTC_HandleTypeDef* hrtc)
{
  if(hrtc->Instance==RTC)
  {
  /* USER CODE BEGIN RTC_MspDeInit 0 */
 
  /* USER CODE END RTC_MspDeInit 0 */
    /* Peripheral clock disable */
    __HAL_RCC_RTC_DISABLE();
  /* USER CODE BEGIN RTC_MspDeInit 1 */
 
  /* USER CODE END RTC_MspDeInit 1 */
  }
 
}

and this one to the it.c

void RTC_Alarm_IRQHandler(void){
	HAL_RTC_AlarmIRQHandler(&hrtc);
}