cancel
Showing results for 
Search instead for 
Did you mean: 

STM32G070 RTC wakeup from STOP1 mode not working

rickard2
Associate III

The processor never wakes up from STOP1 mode and I do not know why. I know RTC clock is running

#define RTC_ASYNCH_PREDIV    0x7F
#define RTC_SYNCH_PREDIV     0xF9  /* 32Khz/128 - 1 */
void Sleep_Init(void)
{
	HW_RTC_Init();
	__HAL_RCC_PWR_CLK_ENABLE();
}
 
 
void HW_RTC_Init(void)
{
	//LL_RTC_InitTypeDef RTC_InitStructure;
	LL_RTC_TimeTypeDef time;
	LL_RTC_DateTypeDef date;
    HAL_PWR_EnableBkUpAccess();
    __HAL_RCC_RTC_ENABLE();
    __HAL_RCC_RTCAPB_CLK_ENABLE();
 
	LL_RCC_LSI_Enable();
 
	while(LL_RCC_LSI_IsReady() == 0)
	{
 
	}
	LL_RCC_SetRTCClockSource(LL_RCC_RTC_CLKSOURCE_LSI);
	__HAL_RCC_RTC_ENABLE();
	LL_RTC_WaitForSynchro(RTC);
 
	//Set the default date to 2000-01-01 00:00:00:000
 
	date.Year = 0;
	date.Day = 1;
	date.Month = 1;
	date.WeekDay = 6;
	time.Hours = 0;
	time.Minutes = 0;
	time.Seconds = 0;
 
	//LL_RTC_Init(RTC, &RTC_InitStructure);
 
 
	/* Configure RTC */
	RTCHandle.Instance = RTC;
	/* Set the RTC time base to 1s */
	/* Configure RTC prescaler and RTC data registers as follow:
	- Hour Format = Format 24
	- Asynch Prediv = Value according to source clock
	- Synch Prediv = Value according to source clock
	- OutPut = Output Disable
	- OutPutPolarity = High Polarity
	- OutPutType = Open Drain */
	RTCHandle.Init.HourFormat = RTC_HOURFORMAT_24;
	RTCHandle.Init.AsynchPrediv = 127;
	RTCHandle.Init.SynchPrediv = 255;
	RTCHandle.Init.OutPut = RTC_OUTPUT_DISABLE;
	RTCHandle.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
	RTCHandle.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
 
	HAL_RTC_Init(&RTCHandle);
 
	LL_RTC_TIME_SetFormat(RTC, LL_RTC_HOURFORMAT_24HOUR);
	LL_RTC_TIME_Init(RTC, LL_RTC_FORMAT_BIN, &time);
	LL_RTC_DATE_Init(RTC, LL_RTC_FORMAT_BIN, &date);
 
	rtcSet = false;
 
}
 
 
void EnterSTOP1Mode(void)
{
  LL_GPIO_InitTypeDef gpio_initstruct = {LL_GPIO_PIN_ALL, LL_GPIO_MODE_ANALOG,
		  	  	  	  	  	  	  	  	  LL_GPIO_SPEED_FREQ_LOW, LL_GPIO_OUTPUT_PUSHPULL,
                                         LL_GPIO_PULL_NO, LL_GPIO_AF_0};
  /* Enable Power Clock */
   LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_PWR);
  /* Set all GPIO in analog state to reduce power consumption,                */
  /* 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.                    */
  LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOA |
                           LL_IOP_GRP1_PERIPH_GPIOB |
                           LL_IOP_GRP1_PERIPH_GPIOC |
                           LL_IOP_GRP1_PERIPH_GPIOD |
                           LL_IOP_GRP1_PERIPH_GPIOF);
 
    LL_GPIO_Init(GPIOA, &gpio_initstruct);
  LL_GPIO_Init(GPIOB, &gpio_initstruct);
  LL_GPIO_Init(GPIOC, &gpio_initstruct);
  LL_GPIO_Init(GPIOD, &gpio_initstruct);
  LL_GPIO_Init(GPIOF, &gpio_initstruct);
 
  LL_IOP_GRP1_DisableClock(LL_IOP_GRP1_PERIPH_GPIOA |
                           LL_IOP_GRP1_PERIPH_GPIOB |
                           LL_IOP_GRP1_PERIPH_GPIOC |
                           LL_IOP_GRP1_PERIPH_GPIOD |
                           LL_IOP_GRP1_PERIPH_GPIOF);
 
  /** Request to enter STOP 1 mode
    * Following procedure describe in STM32G0xx Reference Manual
    * See PWR part, section Low-power modes, STOP 1 mode
    */
  /* Set STO1 0 mode when CPU enters deepsleep */
  LL_PWR_SetPowerMode(LL_PWR_MODE_STOP1);
 
  /* Set SLEEPDEEP bit of Cortex System Control Register */
  LL_LPM_EnableDeepSleep();
 
  /* Request Wait For Interrupt */
  //__WFI();
  __WFE();
}
 
 
void Sleep(unsigned int ms)
{
	if(ms > 0x7FFF)
	{
		return;
	}
 
	ms = ms *2;	// 1ms = 16/(32e3)*2
	ms++;
 
	/* Disable all used wakeup source */
	HAL_StatusTypeDef err = HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle);
	if(err != HAL_OK)
	{
		printf("fel %d\n\r", err);
		return;
	}
 
	/* Re-enable wakeup source */
	/* ## Setting the Wake up time ############################################*/
	/* RTC Wakeup Interrupt Generation:
	the wake-up counter is set to its maximum value to yield the longuest
	stop time to let the current reach its lowest operating point.
	The maximum value is 0xFFFF, corresponding to about 33 sec. when
	RTC_WAKEUPCLOCK_RTCCLK_DIV = RTCCLK_Div16 = 16
 
	Wakeup Time Base = (RTC_WAKEUPCLOCK_RTCCLK_DIV /(LSI))
	Wakeup Time = Wakeup Time Base * WakeUpCounter
	= (RTC_WAKEUPCLOCK_RTCCLK_DIV /(LSI)) * WakeUpCounter
	==> WakeUpCounter = Wakeup Time / Wakeup Time Base
 
	To configure the wake up timer to 60s the WakeUpCounter is set to 0xFFFF:
	Wakeup Time Base = 16 /(~32.000KHz) = ~0.5 ms
	Wakeup Time = 0.5 ms  * WakeUpCounter
	Therefore, with wake-up counter =  0xFFFF  = 65,535
	 Wakeup Time =  0,5 ms *  65,535 = 32,7675 s ~ 33 sec. */
	err = HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, ms, RTC_WAKEUPCLOCK_RTCCLK_DIV16);
	if(err != HAL_OK)
	{
		printf("fel %d\n\r", err);
		return;
	}
 
	printf("Stop1\n\r");
	EnterSTOP1Mode();
}

0 REPLIES 0