STM32L4 Sleep mode using FreeRTOS

S

A useful functionality in low-powered applications is to set the microcontroller in sleep mode for a specific time. This will reduce the overall consumption of the system, and depending on the application it can dramatically improve the battery life. However, using FreeRTOS, the activation of this mode is a bit tricky due to the fact that you have to take into account several other parameters. In this example, we are using an STM32 microcontroller (STM32L4) to perform sleep modes using the RTC.

To archive our results we need to make some minor modifications. First, we need to make sure that the configuration of FreeRTOS includes the IDLE Hook and it is not in tickless mode. Afterwards we need to set the RTC with the highest priority (0). And by using the following code snippet we are able to put our microcontroller in sleep mode just by changing the sleep_time variable.

__weak void vApplicationIdleHook( void )
{
    if(sleep_time !=0)
    {
	portENTER_CRITICAL();
	HAL_PWREx_EnableInternalWakeUpLine();
	if (HAL_RTCEx_SetWakeUpTimer(&hrtc, sleep_time, RTC_WAKEUPCLOCK_CK_SPRE_16BITS)== HAL_OK)
	    HAL_PWREx_EnterSTOP2Mode(PWR_STOPENTRY_WFI);
	sleep_time=0;
	portEXIT_CRITICAL();
    }
}

Disclaimer: The present content may not be used for training artificial intelligence or machine learning algorithms. All other uses, including search, entertainment, and commercial use, are permitted.

Categories

Tags