macros.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #ifndef MACROS_H
  2. #define MACROS_H
  3. #include <avr/interrupt.h> //for cli() and sei()
  4. #define FORCE_INLINE __attribute__((always_inline)) inline
  5. #define _UNUSED __attribute__((unused))
  6. #ifndef CRITICAL_SECTION_START
  7. #define CRITICAL_SECTION_START unsigned char _sreg = SREG; cli();
  8. #define CRITICAL_SECTION_END SREG = _sreg;
  9. #endif //CRITICAL_SECTION_START
  10. #define _REGNAME(registerbase,number,suffix) registerbase##number##suffix
  11. #define _REGNAME_SHORT(registerbase,suffix) registerbase##suffix
  12. // Macros to make a string from a macro
  13. #define STRINGIFY_(M) #M
  14. #define STRINGIFY(M) STRINGIFY_(M)
  15. // Macros for bit masks
  16. #undef _BV
  17. #define _BV(n) (1<<(n))
  18. #define TEST(n,b) (!!((n)&_BV(b)))
  19. #define SET_BIT_TO(N,B,TF) do{ if (TF) SBI(N,B); else CBI(N,B); }while(0)
  20. #ifndef SBI
  21. #define SBI(A,B) (A |= (1 << (B)))
  22. #endif
  23. #ifndef CBI
  24. #define CBI(A,B) (A &= ~(1 << (B)))
  25. #endif
  26. #define TBI(N,B) (N ^= _BV(B))
  27. // Macros to chain up to 12 conditions
  28. #define _DO_1(W,C,A) (_##W##_1(A))
  29. #define _DO_2(W,C,A,B) (_##W##_1(A) C _##W##_1(B))
  30. #define _DO_3(W,C,A,V...) (_##W##_1(A) C _DO_2(W,C,V))
  31. #define _DO_4(W,C,A,V...) (_##W##_1(A) C _DO_3(W,C,V))
  32. #define _DO_5(W,C,A,V...) (_##W##_1(A) C _DO_4(W,C,V))
  33. #define _DO_6(W,C,A,V...) (_##W##_1(A) C _DO_5(W,C,V))
  34. #define _DO_7(W,C,A,V...) (_##W##_1(A) C _DO_6(W,C,V))
  35. #define _DO_8(W,C,A,V...) (_##W##_1(A) C _DO_7(W,C,V))
  36. #define _DO_9(W,C,A,V...) (_##W##_1(A) C _DO_8(W,C,V))
  37. #define _DO_10(W,C,A,V...) (_##W##_1(A) C _DO_9(W,C,V))
  38. #define _DO_11(W,C,A,V...) (_##W##_1(A) C _DO_10(W,C,V))
  39. #define _DO_12(W,C,A,V...) (_##W##_1(A) C _DO_11(W,C,V))
  40. #define __DO_N(W,C,N,V...) _DO_##N(W,C,V)
  41. #define _DO_N(W,C,N,V...) __DO_N(W,C,N,V)
  42. #define DO(W,C,V...) _DO_N(W,C,NUM_ARGS(V),V)
  43. // Macros to support option testing
  44. #define _CAT(a,V...) a##V
  45. #define CAT(a,V...) _CAT(a,V)
  46. #define _ISENA_ ~,1
  47. #define _ISENA_1 ~,1
  48. #define _ISENA_0x1 ~,1
  49. #define _ISENA_true ~,1
  50. #define _ISENA(V...) IS_PROBE(V)
  51. #define _ENA_1(O) _ISENA(CAT(_IS,CAT(ENA_, O)))
  52. #define _DIS_1(O) NOT(_ENA_1(O))
  53. #define ENABLED(V...) DO(ENA,&&,V)
  54. #define DISABLED(V...) DO(DIS,&&,V)
  55. #define TERN(O,A,B) _TERN(_ENA_1(O),B,A) // OPTION converted to '0' or '1'
  56. #define TERN0(O,A) _TERN(_ENA_1(O),0,A) // OPTION converted to A or '0'
  57. #define TERN1(O,A) _TERN(_ENA_1(O),1,A) // OPTION converted to A or '1'
  58. #define TERN_(O,A) _TERN(_ENA_1(O),,A) // OPTION converted to A or '<nul>'
  59. #define _TERN(E,V...) __TERN(_CAT(T_,E),V) // Prepend 'T_' to get 'T_0' or 'T_1'
  60. #define __TERN(T,V...) ___TERN(_CAT(_NO,T),V) // Prepend '_NO' to get '_NOT_0' or '_NOT_1'
  61. #define ___TERN(P,V...) THIRD(P,V) // If first argument has a comma, A. Else B.
  62. // Use NUM_ARGS(__VA_ARGS__) to get the number of variadic arguments
  63. #define _NUM_ARGS(_,Z,Y,X,W,V,U,T,S,R,Q,P,O,N,M,L,K,J,I,H,G,F,E,D,C,B,A,OUT,...) OUT
  64. #define NUM_ARGS(V...) _NUM_ARGS(0,V,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0)
  65. //
  66. // Primitives supporting precompiler REPEAT
  67. //
  68. #define FIRST(a,...) a
  69. #define SECOND(a,b,...) b
  70. #define THIRD(a,b,c,...) c
  71. #define IS_PROBE(V...) SECOND(V, 0) // Get the second item passed, or 0
  72. #define NOT(x) IS_PROBE(_CAT(_NOT_, x)) // NOT('0') gets '1'. Anything else gets '0'.
  73. #endif //MACROS_H