cancel
Showing results for 
Search instead for 
Did you mean: 

Can't enter sleep mode in STM32L052C8T6 (part of RHF76-052C LoRaWAN Module)

Mazaq1
Associate II

I have Lora module with STM32L052C8T6 mcu. I like to send short massages every minute or two. I sent data with success alredy, but my device will be battery powered so I need very power efficient approach.

I figured out that stop mode will be best suitable for me.

I created new project on CubeMX.

Selected 2 pins as output for led.

In RTC section: Selected: Activated Clock Source

Selected: Activate Calendar.

Alarm A: Disable, Alarm B: Disable,

Wake Up: Internal Wake Up

To generated code I added in main loop:

	while (1) {
 
		HAL_GPIO_TogglePin(GPIOB, LED1_Pin);
		HAL_Delay(1000);
		HAL_GPIO_TogglePin(GPIOB, LED1_Pin);
 
		uint32_t _time = (((uint32_t) 500) * 2314) / 1000;
		HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, _time, RTC_WAKEUPCLOCK_RTCCLK_DIV16);
 
		__HAL_RCC_PWR_CLK_ENABLE(); // Enable Power Control clock
		HAL_PWREx_EnableUltraLowPower(); // Ultra low power mode
		HAL_PWREx_EnableFastWakeUp(); // Fast wake-up for ultra low power mode
 
 
		// Switch to STOPMode
		HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
 
		HAL_RTCEx_DeactivateWakeUpTimer(&hrtc);
 
		for (int n = 0; n < 10; n++) {
			HAL_GPIO_TogglePin(GPIOB, LED1_Pin);
			HAL_Delay(100);
		}
	}

And mcu is blinking all the time. It never enters stop mode I think.

I tried everything. For example when I add before while loop line:

HAL_PWREx_EnableLowPowerRunMode();

Program stops when it should, but never wakes up...

I don't know what is going on. Every sample on the Internet looks simple, but I can't reach stop mode. Maybe this mcu do this on other way?

I tried to use sample for nucleo board PWR_STOP_RTC but result is identical.

Please help me ;).

6 REPLIES 6

Most likely the SysTick remains enabled and it wakes up the processor after a millisecond of sleep.

Try to disable the SysTick interrupt before entering into STOP mode and enable it after coming back:

HAL_SuspendTick();
 
// Switch to STOPMode
HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
 
HAL_ResumeTick();

Mazaq1
Associate II

Thank you for quick answer, I done some tests, but with no success :/.

Now mcu stops on "HAL_PWR_EnterSTOPMode" line and does not wake up.

I made wake up counter very low but still program is on hold forever:

HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 10, RTC_WAKEUPCLOCK_RTCCLK_DIV16);

I even created whole new project to be sure that I did not broke something else.

I also changed STOP with SLEEP - result is the same.

Here is whole while code. There are any other changes in code generated by CubeMX. Maybe configuration in CubeMX is wrong? System clock is MSI by default.

  while (1)
  {
		HAL_GPIO_TogglePin(GPIOB, LED1_Pin);
		HAL_Delay(1000);
		HAL_GPIO_TogglePin(GPIOB, LED1_Pin);
		HAL_Delay(200);
 
		//sleep for a very short time
 
		//uint32_t _time = (((uint32_t) 500) * 2314) / 1000;
		//HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, _time, RTC_WAKEUPCLOCK_RTCCLK_DIV16);
		HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 10, RTC_WAKEUPCLOCK_RTCCLK_DIV16);
 
 
		__HAL_RCC_PWR_CLK_ENABLE();
		HAL_PWREx_EnableUltraLowPower();
		HAL_PWREx_EnableFastWakeUp();
 
	    HAL_SuspendTick();
 
	    //second led
	    HAL_GPIO_TogglePin(GPIOA, LED2_Pin);
 
	    // Switch to STOPMode
	    HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
 
	    HAL_ResumeTick();
 
		HAL_RTCEx_DeactivateWakeUpTimer(&hrtc);
 
		for (int n = 0; n < 100; n++) {
			HAL_GPIO_TogglePin(GPIOB, LED1_Pin);
			HAL_Delay(50);
		}
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
  }

> HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 10, RTC_WAKEUPCLOCK_RTCCLK_DIV16);

Where does the value of 10 come from? Are you sure it's long enough to last until you go to sleep? I mean, maybe the wakeup interrupt fires before even the MCU goes into STOP mode.

Also, I hope you didn't forget to define the HAL's callback function for wakeup, something like this:

void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
{
    __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
}

And also calling HAL's interrupt handler from from your stm32lxxx_it.c:

void RTC_IRQHandler(void)
{
    HAL_RTCEx_WakeUpTimerIRQHandler(&hrtc);
}

And don't forget to reinitialize the clocks after coming back from STOP mode.

Yes, you right. I changed value to 10 for having very short waiting time, but I switched it back to original look:

uint32_t _time = (((uint32_t) 500) * 2314) / 1000;
HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, _time, RTC_WAKEUPCLOCK_RTCCLK_DIV16);

But still no success :(. It stuck at entering stop mode.

As I said this project was "fresh" so I didn't include callbacks and handlers. I believe yesterderay I've experimented with this, but check it again now. Let me show what I changed now:

new lines in stm32l0xx_it.c :

extern RTC_HandleTypeDef hrtc;
...
...
...
void RTC_IRQHandler(void)
{
    HAL_RTCEx_WakeUpTimerIRQHandler(&hrtc);
}

And I added this before main function:

void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
{
    __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
}

I've tried to blink leds in this wakeUpTimerCallback to check if it is invoked, but no.

These are new things to me, so maybe I forgot of something else?

Mazaq1
Associate II

Let me refresh this topic, because I can not believe that such simple exercise like enter and exit stop mode gives me so hard time.

So let's do little summary and please treat mi like a complete rookie, maybe I missed something basic and important...

Project is generated in CubeMX. For this testing purpouse I created new project. I activated PA3 and PB3 ports as outputs for LEDs. In timers section I activated RTC with settings:

checked "Activate Clock Source", checked "Activate Calendar", "Alarm A": disable, "Alarm B": disable, "WakeUp": Internal WakeUp (Wake up clock: RTCCLK / 16, Wake Up Counter: 0). Other options disabled.

Clock configuration: sustem clock mux: MSI, 2097kHz.

And absolutley no other changes was made. Let's go to source:

main.c:

just before main I added:

void HAL_RTCEx_WakeUpTimerEventCallback(RTC_HandleTypeDef *hrtc)
{
    __HAL_PWR_CLEAR_FLAG(PWR_FLAG_WU);
}

(I also put breakpoint here, but program never stops here0

In main while loop I put this ans some led blinking lines:

uint32_t _time = (((uint32_t) 500) * 2314) / 1000;
HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, _time, RTC_WAKEUPCLOCK_RTCCLK_DIV16);
 
__HAL_RCC_PWR_CLK_ENABLE();
HAL_PWREx_EnableUltraLowPower();
HAL_PWREx_EnableFastWakeUp();
 
HAL_SuspendTick();
 
// Switch to STOPMode
HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI);
 
HAL_ResumeTick();
 
HAL_RTCEx_DeactivateWakeUpTimer(&hrtc);

(debuger stops on enter stop mode. When I click suspend and resume in debug mode program goes on, but in normal conditions it waits here forever. Never wakes up.)

At the end of stm32l0xx_it.c:

void RTC_IRQHandler(void)
{
    HAL_RTCEx_WakeUpTimerIRQHandler(&hrtc);
}

and at the begining, after includes:

extern RTC_HandleTypeDef hrtc;

(name of variable hrtc is the same as in main.c)

In header file stm32l0xx_it.h after other prototypes I added:

void RTC_IRQHandler(void);

And that was only changes in new project. My only lines are for blinking LEDs... I have no idea what I made wrong.

Please help me :).

I think I chosen correct device in CubeMX, because I made working LoRa radio succesfully and OLED display... And I stuck on this stop mode since week...

Uwe1
Associate II

Hi @Mazaq1​ ,

I am contacting you because I also want to use the RHF76-052 module as a standalone LoRa platform. I have read your project plans in a Polish electronics forum. I have exactly the same plans.

Currently I'm not so far to enter the sleep mode. Maybe we could solve the issue together. But I need some information about the connection to the HF switches. Is it necessary to control these lines (PA1 and PA2) to get HF part working?

Btw, I have a prototype software running on a B-L072Z-LRWAN1 discovery kit by ST. This runs perfectly with a sensor (BME280) and it goes into deep sleep mode as it should. The discovery board utilizes a Murata LoRa module. But this module also contains a STM32L0 controller and a SX1276. So, my idea is to port the B-L072Z-LRWAN1 demo firmware onto the RHF76 module, which shouldn't be a big problem.

Hope we get in contact with this and will solve the deep sleep issue.

Thank you in advance!

Best Regards.