Guest User

Untitled

a guest
Jan 23rd, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. #ifndef COMMON_H__
  2. #define COMMON_H__
  3.  
  4.  
  5. #include <inttypes.h>
  6. #include <stdarg.h>
  7.  
  8. #ifdef __cplusplus
  9. extern "C" {
  10. #endif
  11.  
  12.  
  13. /**
  14. * \addtogroup helpers_bitmanipulation Bit-manipulation macros
  15. * @{
  16. **/
  17.  
  18. /** 32 bit bit accessing macro. **/
  19. #define _BV32(i) (uint32_t)((uint32_t)(1UL << i))
  20.  
  21. /** Bit-level access and test macros. **/
  22. #define SET_BIT(port, bit) ((port) |= (1 << (bit)))
  23. #define CLEAR_BIT(port, bit) ((port) &= ~(1 << (bit)))
  24. #define TOGGLE_BIT(port, bit) ((port) ^= (1 << (bit)))
  25. #define IS_BIT_SET(port, bit) (((port) & (1 << (bit))) ? 1 : 0)
  26. #define IS_BIT_CLEAR(port, bit) (((port) & (1 << (bit))) == 0 ? 1 : 0)
  27.  
  28. /** 8-bit macros. **/
  29. #define SET_BIT8(port, bit) ((port) |= (uint8_t)(1 << (bit)))
  30. #define CLEAR_BIT8(port, bit) ((port) &= (uint8_t)~(1 << (bit)))
  31. #define IS_BIT_SET8(port, bit) (((port) & (uint8_t)(1 << (bit))) ? 1 : 0)
  32. #define IS_BIT_CLEAR8(port, bit) (((port) & (uint8_t)(1 << (bit))) == 0 ? 1 : 0)
  33.  
  34. /** 32-bit macros. **/
  35. #define SET_BIT32(port, bit) ((port) |= (uint32_t)(1UL << (bit)))
  36. #define CLEAR_BIT32(port, bit) ((port) &= (uint32_t)~(1UL << (bit)))
  37. #define TOGGLE_BIT32(port, bit) ((port) ^= (uint32_t)(1UL << (bit)))
  38. #define IS_BIT_SET32(port, bit) (((port) & (uint32_t)(1UL << (bit))) ? 1 : 0)
  39. #define IS_BIT_CLEAR32(port, bit) (((port) & (uint32_t)(1UL << (bit))) == 0 ? 1 : 0)
  40.  
  41. /** 64-bit macros. **/
  42. #define SET_BIT64(port, bit) ((port) |= (uint64_t)(1ULL << (bit)))
  43. #define CLEAR_BIT64(port, bit) ((port) &= (uint64_t)~(1ULL << (bit)))
  44. #define TOGGLE_BIT64(port, bit) ((port) ^= (uint64_t)(1ULL << (bit)))
  45. #define IS_BIT_SET64(port, bit) (((port) & (uint64_t)(1ULL << (bit))) ? 1 : 0)
  46. #define IS_BIT_CLEAR64(port, bit) (((port) & (uint64_t)(1ULL << (bit))) == 0 ? 1 : 0)
  47.  
  48. /** @} */
  49.  
  50. /**
  51. * \addtogroup helpers_common_symbols Commonly used symbols and macros
  52. * @{
  53. **/
  54.  
  55. /** Define commonly used C symbols. **/
  56. #ifndef NULL
  57. #define NULL 0
  58. #endif
  59.  
  60. #define FALSE 0
  61. #define TRUE 1
  62.  
  63. /** Number of elements in a static array. **/
  64. #define countof(arr) ((sizeof(arr)/sizeof(arr[0])))
  65.  
  66. /** Compile-time assertion in C. **/
  67. #define C_ASSERT(e) extern char __C_ASSERT__[(e)?1:-1]
  68.  
  69. /** Compress a C structure to byte aligned boundaries. **/
  70. #define PACKED __attribute__ ((packed))
  71.  
  72. /** @} **/
  73.  
  74. /**
  75. * \addtogroup helpers_math Math macros
  76. * @{
  77. **/
  78.  
  79. /** Get the minimum of x and y. **/
  80. #define MIN(x, y) ((x) > (y) ? (y) : (x))
  81. /** Get the maximum of x and y. **/
  82. #define MAX(x, y) ((x) > (y) ? (x) : (y))
  83. /** Get the absolute value of x. **/
  84. #define ABS(x) ((x) < 0 ? -(x) : (x))
  85.  
  86. #include <util/delay.h>
  87. /** Use the arduino convention to delay inside the code. **/
  88. #define delay(ms) _delay_ms(ms)
  89. #define delayMicroseconds(us) _delay_us(us)
  90.  
  91. /**
  92. * @}
  93. */
  94.  
  95. #ifdef __cplusplus
  96. }
  97. #endif
  98.  
  99. #endif /* COMMON_H__ */
Add Comment
Please, Sign In to add comment