cancel
Showing results for 
Search instead for 
Did you mean: 

WFE examples required for stm32 family?

bivin mato
Associate III

Hello ,

I would like to get some examples for WFE based sleep . In stm32cube examples, i see that most of them are like WFI based examples. Request someone to help me with this information. Thanks in advance.

Regards,

Vinoth

1 ACCEPTED SOLUTION

Accepted Solutions
bivin mato
Associate III

After searching for while i attempted myself with the understanding from the datasheet.

It works. Hope it helps someone in need. Thanks to @TDK​ .

Code snippet for SEVONPEND = 1 as below

int main(void)
{
	/* Add your application code here */
	__IO uint32_t tmpreg;
 
	/* Enable clock for toggle LED */
	SET_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOIEN);
	tmpreg = READ_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOIEN);
 
	/* Enable clock for switch input (EXTI) */
	SET_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOCEN);
	tmpreg = READ_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOCEN);
 
	/* Enable clock for SYSCFG that connects switch to EXTI */
	SET_BIT(RCC->APB2ENR, RCC_APB2ENR_SYSCFGEN);
	tmpreg = READ_BIT(RCC->APB2ENR, RCC_APB2ENR_SYSCFGEN);
 
	/* Configure toggle LED */
	GPIOI->MODER |= GPIO_MODER_MODER15_0;
	GPIOI->PUPDR |= GPIO_PUPDR_PUPDR15_0;
	GPIOI->OSPEEDR |= GPIO_OSPEEDR_OSPEEDR15;
	GPIOI->BSRR= GPIO_BSRR_BS15; // Reset
 
	/* Configure switch input */
	GPIOC->PUPDR = 0;
	GPIOC->MODER = 0;
	SYSCFG->EXTICR[3] = 0x20;
	EXTI->IMR = 0x2000;
	EXTI->EMR = 0;
	EXTI->RTSR = 0x2000;
 
	SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SEVONPEND_Msk));
 
	/* Infinite loop */
	while (1)
	{
		/* Set SLEEPDEEP bit of Cortex System Control Register */
		SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
 
		/* Ensure that all instructions done before entering SLEEP mode */
		__DSB();
		__ISB();
 
	    /* Request Wait For Event */
	    __SEV();
	    __WFE();
	    __WFE();
 
		/* Reset SLEEPDEEP bit of Cortex System Control Register */
		SCB->SCR &= (uint32_t)~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
 
	    /* Clear the flag responding to the event */
	    EXTI->PR = 0x2000;
		while (EXTI->PR != 0);
 
		/* Clear the pending flag responding to the event in NVIC */
		NVIC_ClearPendingIRQ(EXTI15_10_IRQn);
 
		ToggleLed();
	}
 
}

Code snippet for SEVONPEND = 0 as below

int main(void)
{
	/* Add your application code here */
	__IO uint32_t tmpreg;
 
	/* Enable clock for toggle LED */
	SET_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOIEN);
	tmpreg = READ_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOIEN);
 
	/* Enable clock for switch input (EXTI) */
	SET_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOCEN);
	tmpreg = READ_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOCEN);
 
	/* Enable clock for SYSCFG that connects switch to EXTI */
	SET_BIT(RCC->APB2ENR, RCC_APB2ENR_SYSCFGEN);
	tmpreg = READ_BIT(RCC->APB2ENR, RCC_APB2ENR_SYSCFGEN);
 
	/* Configure toggle LED */
	GPIOI->MODER |= GPIO_MODER_MODER15_0;
	GPIOI->PUPDR |= GPIO_PUPDR_PUPDR15_0;
	GPIOI->OSPEEDR |= GPIO_OSPEEDR_OSPEEDR15;
	GPIOI->BSRR= GPIO_BSRR_BS15; // Reset
 
	/* Configure switch input */
	GPIOC->PUPDR = 0;
	GPIOC->MODER = 0;
	SYSCFG->EXTICR[3] = 0x20;
	EXTI->IMR = 0;
	EXTI->EMR = 0x2000;
	EXTI->RTSR = 0x2000;
 
	/* Infinite loop */
	while (1)
	{
		/* Set SLEEPDEEP bit of Cortex System Control Register */
		SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
 
		/* Ensure that all instructions done before entering SLEEP mode */
		__DSB();
		__ISB();
 
	    /* Request Wait For Event */
	    __SEV();
	    __WFE();
	    __WFE();
 
	    /* Reset SLEEPDEEP bit of Cortex System Control Register */
		SCB->SCR &= (uint32_t)~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
 
		ToggleLed();
	}
 
}

View solution in original post

2 REPLIES 2
TDK
Guru

> WFE examples required for stm32 family?

Might want to narrow down STM32 family a bit. There are a lot of products out there.

Doesn't seem to be many CubeMX examples that use WFE. But here is one:

https://github.com/STMicroelectronics/STM32CubeH7/blob/e261d820b398846a94f22f4aeb32d86c29546efb/Projects/STM32H747I-EVAL/Applications/OpenAMP/OpenAMP_PingPong/CM4/Src/main_cm4.c

When I want examples, sometimes I use the raw GitHub search to find where things are used. A lot of hits are from the definition file, but some are in project usage:

https://github.com/search?q=PWR_SLEEPENTRY_WFE+filename%3A*.c+language%3AC+language%3AC&type=Code

If you feel a post has answered your question, please click "Accept as Solution".
bivin mato
Associate III

After searching for while i attempted myself with the understanding from the datasheet.

It works. Hope it helps someone in need. Thanks to @TDK​ .

Code snippet for SEVONPEND = 1 as below

int main(void)
{
	/* Add your application code here */
	__IO uint32_t tmpreg;
 
	/* Enable clock for toggle LED */
	SET_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOIEN);
	tmpreg = READ_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOIEN);
 
	/* Enable clock for switch input (EXTI) */
	SET_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOCEN);
	tmpreg = READ_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOCEN);
 
	/* Enable clock for SYSCFG that connects switch to EXTI */
	SET_BIT(RCC->APB2ENR, RCC_APB2ENR_SYSCFGEN);
	tmpreg = READ_BIT(RCC->APB2ENR, RCC_APB2ENR_SYSCFGEN);
 
	/* Configure toggle LED */
	GPIOI->MODER |= GPIO_MODER_MODER15_0;
	GPIOI->PUPDR |= GPIO_PUPDR_PUPDR15_0;
	GPIOI->OSPEEDR |= GPIO_OSPEEDR_OSPEEDR15;
	GPIOI->BSRR= GPIO_BSRR_BS15; // Reset
 
	/* Configure switch input */
	GPIOC->PUPDR = 0;
	GPIOC->MODER = 0;
	SYSCFG->EXTICR[3] = 0x20;
	EXTI->IMR = 0x2000;
	EXTI->EMR = 0;
	EXTI->RTSR = 0x2000;
 
	SET_BIT(SCB->SCR, ((uint32_t)SCB_SCR_SEVONPEND_Msk));
 
	/* Infinite loop */
	while (1)
	{
		/* Set SLEEPDEEP bit of Cortex System Control Register */
		SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
 
		/* Ensure that all instructions done before entering SLEEP mode */
		__DSB();
		__ISB();
 
	    /* Request Wait For Event */
	    __SEV();
	    __WFE();
	    __WFE();
 
		/* Reset SLEEPDEEP bit of Cortex System Control Register */
		SCB->SCR &= (uint32_t)~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
 
	    /* Clear the flag responding to the event */
	    EXTI->PR = 0x2000;
		while (EXTI->PR != 0);
 
		/* Clear the pending flag responding to the event in NVIC */
		NVIC_ClearPendingIRQ(EXTI15_10_IRQn);
 
		ToggleLed();
	}
 
}

Code snippet for SEVONPEND = 0 as below

int main(void)
{
	/* Add your application code here */
	__IO uint32_t tmpreg;
 
	/* Enable clock for toggle LED */
	SET_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOIEN);
	tmpreg = READ_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOIEN);
 
	/* Enable clock for switch input (EXTI) */
	SET_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOCEN);
	tmpreg = READ_BIT(RCC->AHB1ENR, RCC_AHB1ENR_GPIOCEN);
 
	/* Enable clock for SYSCFG that connects switch to EXTI */
	SET_BIT(RCC->APB2ENR, RCC_APB2ENR_SYSCFGEN);
	tmpreg = READ_BIT(RCC->APB2ENR, RCC_APB2ENR_SYSCFGEN);
 
	/* Configure toggle LED */
	GPIOI->MODER |= GPIO_MODER_MODER15_0;
	GPIOI->PUPDR |= GPIO_PUPDR_PUPDR15_0;
	GPIOI->OSPEEDR |= GPIO_OSPEEDR_OSPEEDR15;
	GPIOI->BSRR= GPIO_BSRR_BS15; // Reset
 
	/* Configure switch input */
	GPIOC->PUPDR = 0;
	GPIOC->MODER = 0;
	SYSCFG->EXTICR[3] = 0x20;
	EXTI->IMR = 0;
	EXTI->EMR = 0x2000;
	EXTI->RTSR = 0x2000;
 
	/* Infinite loop */
	while (1)
	{
		/* Set SLEEPDEEP bit of Cortex System Control Register */
		SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk;
 
		/* Ensure that all instructions done before entering SLEEP mode */
		__DSB();
		__ISB();
 
	    /* Request Wait For Event */
	    __SEV();
	    __WFE();
	    __WFE();
 
	    /* Reset SLEEPDEEP bit of Cortex System Control Register */
		SCB->SCR &= (uint32_t)~((uint32_t)SCB_SCR_SLEEPDEEP_Msk);
 
		ToggleLed();
	}
 
}