This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

WFI internals

Hello :) For the code below, why are we defining the _wfi while in the code we only use _WFI() for addressing the function? Also once we use _asm volatile ("wfi); isnt the syntax still in C ? I presumed that using _asm we need to write the rest of the code in assembly language? I would appreciate peoples response.

 #define __WFI                             __wfi
    
    
    /** \brief  Wait For Interrupt
    
        Wait For Interrupt is a hint instruction that suspends execution
        until one of a number of events occurs.
     */
    __attribute__((always_inline)) __STATIC_INLINE void __WFI(void)
    {
      __ASM volatile ("wfi");
    }
Parents
  • __attribute__((always_inline)) __STATIC_INLINE void __WFI(void)
    

    INLINE does not guarantee that compiler will in all cases inline the function, hence adding __attribute__((always_inline)) forces the compiler to inline this function and the only exception is if the its a recursive inlined function which will exhaust the whole memory. Read this for more information on always_inline attribute

    __ASM volatile ("wfi");
    

    wfi is assembly instruction, but we can use it inside C code by giving __ASM keyword, this __ASM keyword tells the compiler to skip compiler stage and hand over this line directly to the assembler. I am not sure what volatile will do here as this is a single instruction.

    you do not need to write the rest of the code in assembly, as the above code is accepted by C compiler. There are tons of examples you find if you google "inline assembly in C". Below is one good example I found that might be of your interest. www.codeproject.com/.../Using-Inline-Assembly-in-C-C

Reply
  • __attribute__((always_inline)) __STATIC_INLINE void __WFI(void)
    

    INLINE does not guarantee that compiler will in all cases inline the function, hence adding __attribute__((always_inline)) forces the compiler to inline this function and the only exception is if the its a recursive inlined function which will exhaust the whole memory. Read this for more information on always_inline attribute

    __ASM volatile ("wfi");
    

    wfi is assembly instruction, but we can use it inside C code by giving __ASM keyword, this __ASM keyword tells the compiler to skip compiler stage and hand over this line directly to the assembler. I am not sure what volatile will do here as this is a single instruction.

    you do not need to write the rest of the code in assembly, as the above code is accepted by C compiler. There are tons of examples you find if you google "inline assembly in C". Below is one good example I found that might be of your interest. www.codeproject.com/.../Using-Inline-Assembly-in-C-C

Children
Related