2023-07-09 05:13 AM
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.
Solved! Go to Solution.
2023-07-10 06:17 AM
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.
2023-07-10 06:17 AM
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.
2023-08-02 01:53 PM
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!
2023-08-02 02:26 PM
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é
2023-08-03 09:17 AM
Hello @GwenoleB ,
Yes, I need no additional settings so only WFI will do the trick. Thank you!
Best regards,
Uros M.
2023-08-04 07:56 AM - last edited on 2023-08-08 12:37 AM by Amelie ACKERMANN
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
2023-08-04 08:19 AM
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.
2023-08-07 08:44 AM
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: