cancel
Showing results for 
Search instead for 
Did you mean: 

Custom sleep and wakeup system on the lora end node STM32WL

Candra SN.
Associate III

Hi,

I am a bit confused on the lora STM32WL sequence protocol system, I want my device to sleep and wakeup according to the periodic values I want. I have made my own sleep function (stop 2 mode & radio sleep) with wake up triggered using RTC (in seconds) and push button. Where can I put my function (PLB_SleepMode) into the sequencer utility lora end node STM32WL ?

Thank you

Here my code for custom sleep and wake up system:

void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *RtcHandle)
{
  //Clear wake up flag
  __HAL_PWR_CLEAR_FLAG( PWR_FLAG_WU );
}
 
void PLB_RtcWakeUpTimeSetting( RTC_HandleTypeDef *RtcHandle, unsigned int seconds )
{
  //Set wake up time
  /* Disable all used wakeup source */
  HAL_RTCEx_DeactivateWakeUpTimer( RtcHandle );
  
  __HAL_RCC_WAKEUPSTOP_CLK_CONFIG( RCC_STOP_WAKEUPCLOCK_MSI );
  HAL_RTCEx_SetWakeUpTimer_IT( RtcHandle, seconds - 1, RTC_WAKEUPCLOCK_CK_SPRE_16BITS );
  
}
 
RTC_HandleTypeDef *PLB_RtcPowerSavingConfig( void )
{
  //Set RTC Clock, Timer, Calendar. and return RTC_HandleTypeDef to wake up
  static RTC_HandleTypeDef RtcHandle;
  
  RTC_TimeTypeDef sTime;
  RTC_DateTypeDef sDate;
  
  RtcHandle.Instance            = RTC;
  RtcHandle.Init.HourFormat     = RTC_HOURFORMAT_24;
  RtcHandle.Init.AsynchPrediv   = 127;
  RtcHandle.Init.SynchPrediv    = 255;
  RtcHandle.Init.OutPut         = RTC_OUTPUT_DISABLE;
  RtcHandle.Init.OutPutRemap    = RTC_OUTPUT_REMAP_NONE;
  RtcHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  RtcHandle.Init.OutPutType     = RTC_OUTPUT_TYPE_OPENDRAIN;
  HAL_RTC_Init( &RtcHandle );
  
  sTime.Hours          = 0x0;
  sTime.Minutes        = 0x0;
  sTime.Seconds        = 0x0;
  sTime.TimeFormat     = RTC_HOURFORMAT_24;
  sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  sTime.StoreOperation = RTC_STOREOPERATION_RESET;
  HAL_RTC_SetTime( &RtcHandle, &sTime, FORMAT_BIN );
  
  sDate.WeekDay = RTC_WEEKDAY_MONDAY;
  sDate.Month   = RTC_MONTH_JANUARY;
  sDate.Date    = 0x1;
  sDate.Year    = 16;
  HAL_RTC_SetDate( &RtcHandle, &sDate, FORMAT_BIN );
  
  return &RtcHandle;
}
 
void PLB_RtcRccPowerSavingConfig( void )
{
  HAL_StatusTypeDef status = HAL_OK;
  RCC_PeriphCLKInitTypeDef PeriphClkInit;
  RCC_OscInitTypeDef       RCC_OscInitStruct;
  
  /* Disable backup domeain protection */
  HAL_PWR_EnableBkUpAccess();
  
  /* Enable RTC APB clock gating */
  __HAL_RCC_RTCAPB_CLK_ENABLE();
  
  /* Disable the Wake-up Timer */
  __HAL_RTC_WAKEUPTIMER_DISABLE(RtcHandle);
  /* In case of interrupt mode is used, the interrupt source must disabled */ 
  __HAL_RTC_WAKEUPTIMER_DISABLE_IT(RtcHandle,RTC_IT_WUT);
  __HAL_RTC_WAKEUPTIMER_CLEAR_FLAG(RtcHandle,RTC_FLAG_WUTF);
  
  /* Get RTC clock configuration */
  HAL_RCCEx_GetPeriphCLKConfig(&PeriphClkInit);
  
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  RCC_OscInitStruct.LSEState = RCC_LSE_ON;  
  PeriphClkInit.RTCClockSelection    = RCC_RTCCLKSOURCE_LSE;
  
  /* COnfigure oscillator */
  status = HAL_RCC_OscConfig(&RCC_OscInitStruct);
  if(status == HAL_OK)
  {
    /* Configure RTC clock source */
    PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_RTC;
    status = HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
    
    /* Enable RTC Clock */
    if(status == HAL_OK)
    {
      __HAL_RCC_RTC_ENABLE();
    }
  }
  HAL_NVIC_EnableIRQ( RTC_WKUP_IRQn );
  HAL_NVIC_SetPriority( RTC_WKUP_IRQn, 1, 0 );
  
}
 
void PLB_InitSystem(void)
{
  HAL_Init();
  PLB_SystemClock_Config( );
  SystemApp_Init();
  PLB_LED_Init(PLB_LED_MODE_RUN);
  PLB_LED_Init(PLB_LED_LORA_INDICATOR);
  PLB_LORA_Seq_Task();
}
 
void PLB_PowerSavingStopMode(RTC_HandleTypeDef *RtcHandle, unsigned int WakeUpTime, GPIO_TypeDef *WakeUpGPIO)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  
  Radio.Sleep();
  
  /* Enable GPIOs clock */
  __HAL_RCC_GPIOA_CLK_ENABLE();
  __HAL_RCC_GPIOB_CLK_ENABLE();
  __HAL_RCC_GPIOC_CLK_ENABLE();
  __HAL_RCC_GPIOH_CLK_ENABLE();
  
  /* Configure all GPIO port pins in Analog Input mode (floating input trigger OFF) */
  /* Note: Debug using ST-Link is not possible during the execution of this   */
  /*       example because communication between ST-link and the device       */
  /*       under test is done through UART. All GPIO pins are disabled (set   */
  /*       to analog input mode) including  UART I/O pins.           */
  GPIO_InitStructure.Pin = GPIO_PIN_All;
  GPIO_InitStructure.Mode = GPIO_MODE_ANALOG;
  GPIO_InitStructure.Pull = GPIO_NOPULL;
  GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_LOW;
  
  HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
  HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
  HAL_GPIO_Init(GPIOC, &GPIO_InitStructure);
  HAL_GPIO_Init(GPIOH, &GPIO_InitStructure);
  
  //wake up pin setting
  GPIO_InitStructure.Pin   = PLB_WakeupPB_GPIO_Pin; // wakeup by PA.0
  GPIO_InitStructure.Mode  = GPIO_MODE_IT_RISING;
  GPIO_InitStructure.Pull  = GPIO_PULLDOWN;
  GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_HIGH;
  HAL_GPIO_Init( GPIOA, &GPIO_InitStructure );
  
  GPIO_InitStructure.Pin   = GPIO_PIN_9;
  GPIO_InitStructure.Mode  = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStructure.Pull  = GPIO_NOPULL;
  GPIO_InitStructure.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  
  HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
  
  GPIO_InitStructure.Pin = GPIO_PIN_8;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
  
  GPIO_InitStructure.Pin = GPIO_PIN_7;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
 
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET); 
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_9, GPIO_PIN_RESET); 
  HAL_GPIO_WritePin(GPIOA, GPIO_PIN_7, GPIO_PIN_RESET); 
  
  HAL_NVIC_SetPriority(EXTI0_IRQn, 1, 0);
  HAL_NVIC_EnableIRQ(EXTI0_IRQn);
  
  /* Disable GPIOs clock */
  __HAL_RCC_GPIOA_CLK_DISABLE();
  __HAL_RCC_GPIOB_CLK_DISABLE();
  __HAL_RCC_GPIOC_CLK_DISABLE();
  __HAL_RCC_GPIOH_CLK_DISABLE();
  
  //Set power saving duration by RTC
  //  if(WakeUpTime < 5 ) WakeUpTime = WakeUpTime + 5;
  PLB_RtcWakeUpTimeSetting( RtcHandle, WakeUpTime );
  
  // HAL_RTCEx_WakeUpTimerIRQHandler(RtcHandle);
  HAL_PWR_EnableWakeUpPin(PWR_WAKEUP_PIN1);
  
  //Clear wake up flag
  if( __HAL_PWR_GET_FLAG( PWR_FLAG_WU ) != RESET )
  {__HAL_PWR_CLEAR_FLAG( PWR_FLAG_WU ); }
  
  if(__HAL_GPIO_EXTI_GET_FLAG(GPIO_PIN_0) != RESET)
  {__HAL_GPIO_EXTI_CLEAR_FLAG(GPIO_PIN_0); }
  
  /* Enter STOP 2 mode */
  LL_PWR_ClearFlag_C1STOP_C1STB();
  HAL_PWREx_EnterSTOP2Mode(PWR_STOPENTRY_WFI);
  
  PLB_InitSystem();
  Radio.Standby();
  
  /*Disable all used wakeup sources: Pin1(PA.0)*/
  HAL_PWR_DisableWakeUpPin(PWR_WAKEUP_PIN1);
}
 
void PLB_SleepMode(unsigned int wakeUpTime)
{
  RTC_HandleTypeDef *RtcHandle;
  
  APP_PRINTF("\nPowerSaving Mode - Start\n");
  
  //RCC Config for Power Saving
  PLB_RtcRccPowerSavingConfig();
  
  //RTC Config for Power Saving
  RtcHandle = PLB_RtcPowerSavingConfig();
  
  //Enter Stop Mode
  PLB_PowerSavingStopMode( RtcHandle, wakeUpTime, GPIOA );
  APP_PRINTF("\nPowerSaving Mode - End\n");
}

1 ACCEPTED SOLUTION
5 REPLIES 5
SHATE.1
Associate III

Hello

i made a custom board using MIDATRONICS module that integrate the STM32WLE5J8I and i don't find how to make a lorawan end node project !!! when i import the end node project from the nucleo-WL package, it doesn't work (don't make a join and don't send data!!) and because the STM32WLE5J8I HAVE ONLY 64K of flash i'm at 96% of memory !!!

Can you help please ? is anybody make a lorawan end node project directly with cubeMX for the STM32WLE5J8I without any import from the Nucleo project?

Thank you in advance

Bests Regards

YBOUV.1
Senior

Hi @SHATE.1​ ,

please open another question as this does not relate with question above.

thank you

Thank you for your quick reply

i asked a question " How we can made a LORAWAN End Node application with STM32WLE5J8I ????"

can you answer please?

Thank you in advance

YBOUV.1
Senior

Hi,

It is always good to check the .map file to check where to spend the optimization effort,

please find some tips:

  • You should make sure you use optimization level for size in the compiler option
  • Embed 1 region only (in lorawan_conf.h)
  • Use LLs instead of HALs
  • Remove traces. Maybe you do not need to embed traces with verbose level medium or high
  • ultimately, use a device with more memory

back to my comment, the question you asked does not related to "Custom sleep and wakeup system on the lora end node STM32WL"

thank you and best regards