This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

what is st() macro used for in z stack

#define st(x)      do { x } while (__LINE__ == -1)

I saw most of them used for set register value. is that required or we can directly set register value?

 

also" while (__LINE__ == -1)", what the "__LINE__ == -1" indicates for?

 

finally, how many CPU instruction cycles are used to execute one st()

 

thx

  • Hi Rui,

    You can find all the answers in hal_defs.h where st() is defined. There is some explanation just above the definition:

    /*
     *  This macro is for use by other macros to form a fully valid C statement.
     *  Without this, the if/else conditionals could show unexpected behavior.
     *
     *  For example, use...
     *    #define SET_REGS()  st( ioreg1 = 0; ioreg2 = 0; )
     *  instead of ...
     *    #define SET_REGS()  { ioreg1 = 0; ioreg2 = 0; }
     *  or
     *    #define  SET_REGS()    ioreg1 = 0; ioreg2 = 0;
     *  The last macro would not behave as expected in the if/else construct.
     *  The second to last macro will cause a compiler error in certain uses
     *  of if/else construct
     *
     *  It is not necessary, or recommended, to use this macro where there is
     *  already a valid C statement.  For example, the following is redundant...
     *    #define CALL_FUNC()   st(  func();  )
     *  This should simply be...
     *    #define CALL_FUNC()   func()
     *
     * (The while condition below evaluates false without generating a
     *  constant-controlling-loop type of warning on most compilers.)
     */
    #define st(x)      do { x } while (__LINE__ == -1)

    __LINE__==-1 is always 0, but doesn't bring a warning on most compilers unlike when you put a constant "0" in that place.
    And this kind of do-while doesn't take any instruction cycle because it will be removed when the compiler optimizes the code.
    - Cetri

  • Thx, But could you explain a little bit about" this kind of do-while doesn't take any instruction cycle because it will be removed when the compiler optimizes the code".

     

    for example:

     

    #define HAL_ENTER_CRITICAL_SECTION(x)   st( x = EA;  HAL_DISABLE_INTERRUPTS(); )

     

    it dose not take any CPU any instruction cycle?

     

    Or let me give out a extreme example

     UINT32 a;

    st(a=0xFFFFFFFF);

     

    I believe this definitely takes some time to execute. Am I wrong?

    Also whether st() will be INT by ISR?

    thx

  • Hi Rui,

    I meant "do" and "while" themselves didn't took any cycle.

    In your example, a=0xFFFFFFFF and st(a=0xFFFFFFFF) will take the same cycles. I hope it's clear now.

    - Cetri