STM32L0 wakes up by serial port interrupt after entering STOP mode

Publisher:李国永Latest update time:2019-02-13 Source: eefocusKeywords:STM32L0 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Function: The MCU samples at a certain sampling frequency (such as 2Khz). For example, every minute (60s), only the first 30s are sampled, and the last 30s are dormant (timed dormancy wake-up). It automatically wakes up again in the next minute, and repeats this cycle for 30 minutes. After 30 minutes, the MCU enters the dormant stop mode. The dormant MCU is awakened by serial port interruption.


Implementation:


1. Timing sleep wake-up code


HAL_Delay(2000); //Sampling time

HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle);

HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, 2, RTC_WAKEUPCLOCK_RTCCLK_DIV16);

HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); //Call library function to enter STOP mode

SystemClockConfig_STOP(); //Wake up after stop mode and restore clock source

count1++;

while(count1==1)//Number of loops

{

Rx_Init();

count1=0;

flag3=0;

__HAL_RTC_ALARM_DISABLE_IT(&RTCHandle, RTC_IT_ALRA);

__HAL_RTC_ALARM_CLEAR_FLAG(&RTCHandle, RTC_IT_ALRA);

HAL_TIM_Base_Stop(&TimHandle); //Timer stop

HAL_RTCEx_DeactivateWakeUpTimer(&RTCHandle);

HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFI); //Call library function to enter STOP mode

}

Calculation method: For example, sleep for 4 seconds

/*  RTC Wakeup Interrupt Generation:

Wakeup Time Base = (RTC_WAKEUPCLOCK_RTCCLK_DIV /(LSE or LSI))

Wakeup Time = Wakeup Time Base * WakeUpCounter

= (RTC_WAKEUPCLOCK_RTCCLK_DIV /(LSE or LSI)) * WakeUpCounter

==> WakeUpCounter = Wakeup Time / Wakeup Time BaseTo configure the wake up timer to 4s the WakeUpCounter is set to 0x1FFF:

RTC_WAKEUPCLOCK_RTCCLK_DIV = RTCCLK_Div16 = 16

Wakeup Time Base = 16 /(~39.000KHz) = ~0,410 ms

Wakeup Time = ~4s = 0,410ms  * WakeUpCounter

==> WakeUpCounter = ~4s/0,410ms = 9750 = 0x2616 */

HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, 0x2616, RTC_WAKEUPCLOCK_RTCCLK_DIV16);


Limitation: Using internal clock LSI, the fixed size leads to the limitation of Wakeup Time Base. Wakeup Time (maximum) = 0.41ms*65535 (0xffff) = 26.869s, which may not meet the sleep time requirement.


Another method: change HAL_RTCEx_SetWakeUpTimer_IT(&RTCHandle, 2, RTC_WAKEUPCLOCK_CK_SPRE_16BITS);


Here RTC_WAKEUPCLOCK_CK_SPRE_16BITS sets the lower three bits in the CR register, the wake-up clock selection, here select 1hz, 2 means entering the wake-up interrupt after working for 2S, this number can be changed at will, and the amount of time it is changed to is the amount of sleep time.



2. Serial port interrupt wakeup


Wake-up mechanism: After the MCU enters the STOP state, it cannot be directly awakened by UART and other peripherals. Before the MCU enters the STOP state, set the RX pin to EXTI mode and enable the corresponding interrupt. After waking up, reinitialize the serial port and configure the clock. (Here are two codes, one is the code of Worms that I referred to, and the other is my own)



by: Worms


void Rx_Init()

{

GPIO_InitTypeDef GPIO_InitStruct;

__HAL_RCC_GPIOA_CLK_ENABLE();

  // Configure the Rx pin of UART1 to EXIT mode

  GPIO_InitStruct.Pin = USARTx_RX_PIN;

  GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING; // Pay special attention to the interrupt mode here

  GPIO_InitStruct.Pull = GPIO_PULLUP;

  HAL_GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStruct);

  HAL_NVIC_SetPriority(EXTI4_15_IRQn, 2, 0);

  HAL_NVIC_EnableIRQ(EXTI4_15_IRQn);

}

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)

{

if(GPIO_Pin==USARTx_RX_PIN)

{

//  HAL_NVIC_EnableIRQ(SysTick_IRQn);

SystemClockConfig_STOP(); //Wake up after stop mode and restore clock source

HAL_Init();

SystemClock_Config();

SystemPower_Config();

HAL_SPI_DeInit(&hspi1);

HAL_GPIO_DeInit(GPIOA,GPIO_PIN_10); //When the serial port is reinitialized, the HAL_UART_Init() function will judge if(huart->State == HAL_UART_STATE_RESET), then HAL_UART_MspInit(huart) will be executed to configure the serial IO port. This sentence must be included!

__HAL_RCC_GPIOA_CLK_ENABLE();

__HAL_RCC_GPIOA_CLK_ENABLE();

__HAL_RCC_GPIOB_CLK_ENABLE();

SPI_GPIO_Init();

UartHandle.Instance        = USARTx;

UartHandle.Init.BaudRate   = uart_baud;

UartHandle.Init.WordLength = UART_WORDLENGTH_8B;

UartHandle.Init.StopBits   = UART_STOPBITS_1;

UartHandle.Init.Parity     = UART_PARITY_NONE;

UartHandle.Init.HwFlowCtl  = UART_HWCONTROL_NONE;

UartHandle.Init.Mode       = UART_MODE_TX_RX;

GPIO_InitTypeDef  GPIO_InitStruct;

/*##-1- Enable peripherals and GPIO Clocks #################################*/

/* Enable GPIO TX/RX clock */

USARTx_TX_GPIO_CLK_ENABLE();

USARTx_RX_GPIO_CLK_ENABLE();

/* Enable USART2 clock */

USARTx_CLK_ENABLE();

/*##-2- Configure peripheral GPIO ##########################################*/

/* UART TX GPIO pin configuration  */

GPIO_InitStruct.Pin = USARTx_TX_PIN;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Pull      = GPIO_NOPULL;

GPIO_InitStruct.Speed     = GPIO_SPEED_FREQ_HIGH  ;

GPIO_InitStruct.Alternate = USARTx_TX_AF;

HAL_GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStruct);

/* UART RX GPIO pin configuration  */

GPIO_InitStruct.Pin = USARTx_RX_PIN;

GPIO_InitStruct.Alternate = USARTx_RX_AF;

HAL_GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStruct);

HAL_NVIC_DisableIRQ(EXTI4_15_IRQn);

__HAL_GPIO_EXTI_CLEAR_IT(USARTx_RX_PIN);

HAL_NVIC_SetPriority(USARTx_IRQn, 4, 0);

HAL_NVIC_EnableIRQ(USARTx_IRQn);

}

}

Finish!

Keywords:STM32L0 Reference address:STM32L0 wakes up by serial port interrupt after entering STOP mode

Previous article:Read and write data between STM32 and Flash AT45DB321D
Next article:Solution to the problem that the stm32 microcontroller cannot download the program after entering the sleep (STOP) mode

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号