Skip to main content
UMilo.1
Associate III
July 9, 2023
Solved

Putting STM32F103 in sleep mode

  • July 9, 2023
  • 3 replies
  • 4322 views

Hello,

I am trying to put my STM32F103C8T8 in sleep mode where all clocks are running etc. and wake it up using external UART interrupt.

I am using MikroC as an environment and I don't think they have Wait for interrupt function. Is there any other solution to solve this? Is it too much of the work to set up registers, in that case do anybody knows which registers should I tackle?

Thanks in advance!

Best regards,

Uros M.

This topic has been closed for replies.
Best answer by RomainR.

Hello @UMilo.1 

wfi() or wfe() are assembly instruction for ARM Cortex-M, they are not related to C language syntax (see PM0056 https://www.st.com/resource/en/programming_manual/pm0056-stm32f10xxx20xxx21xxxl1xxxx-cortexm3-programming-manual-stmicroelectronics.pdf)

I don't know about MikroC, but as any other embedded C language compiler port, you should be able to call assembly instructions in C/C++ program.

Let's try this documentation.   

http://download.mikroe.com/documents/compilers/mikroc/arm/help/asm_declaration.htm

BR

Romain,

3 replies

RomainR.
RomainR.Best answer
ST Employee
July 10, 2023

Hello @UMilo.1 

wfi() or wfe() are assembly instruction for ARM Cortex-M, they are not related to C language syntax (see PM0056 https://www.st.com/resource/en/programming_manual/pm0056-stm32f10xxx20xxx21xxxl1xxxx-cortexm3-programming-manual-stmicroelectronics.pdf)

I don't know about MikroC, but as any other embedded C language compiler port, you should be able to call assembly instructions in C/C++ program.

Let's try this documentation.   

http://download.mikroe.com/documents/compilers/mikroc/arm/help/asm_declaration.htm

BR

Romain,

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
UMilo.1
UMilo.1Author
Associate III
August 2, 2023

Hello @RomainR.,

Thanks for reply!
Now I understand, thank you!

I am sending WFI command using asm like this

asm {
WFI // Wait For Interrupt
}

Is that enough or I need to change bits in System Control Register as well?

Thank you so much!

ST Employee
August 2, 2023

Hello @UMilo.1,

I think you are talking about the SLEEPONEXIT bit.
By default, this bit is reset. Consequently, the MCU enters Sleep mode as soon as WFI or WFE instruction is executed.
By setting this bit, the MCU enters Sleep mode as soon as it exits the lowest priority ISR.
So it's up to you to configure according to your needs.

Best Regards,
Gwénolé

UMilo.1
UMilo.1Author
Associate III
August 3, 2023

Hello @GwenoleB ,

Yes, I need no additional settings so only WFI will do the trick. Thank you!

 

Best regards,

Uros M.

Piranha
Principal III
August 4, 2023

Before the WFI instruction, the DSB instruction is required. Read the "ARM Cortex-M Programming Guide to Memory Barrier Instructions, Application Note 321" section "4.14 Entering sleep modes".

Also take a note that, for example, GCC requires also a compiler barrier with inline assembler code. You can see it, for example, in __WFI() macro.

@GwenoleB , nobody asked for "sleep on exit" feature here...

Edited to adhere to community guidelines

ST Employee
August 4, 2023

Or add a read operation before WFI()?

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
Piranha
Principal III
August 7, 2023

A read of what and why? A read-back is necessary after a write to PWR, RCC or some other peripheral, which is located outside of the CPU core, and the write has to be fully complete before the WFI is executed. The DSB before the WFI just drains the CPU write buffer (to any address), but doesn't guarantee the completion, and it forbids the CPU to do speculative reads and executing other instructions beyond the DSB.

Therefore the "read-back after a write to PWR" and the "DSB before the WFI" are to separate concepts, both of which must be respected. You can see a complete example with a detailed explanation there:

https://community.st.com/t5/stm32-mcu-products/how-to-enter-standby-or-shutdown-mode-on-stm32/m-p/145849