util.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #ifndef UTIL_H_
  2. #define UTIL_H_
  3. #include <stdint.h>
  4. #include <avr/eeprom.h>
  5. #include <avr/interrupt.h>
  6. #include <avr/pgmspace.h>
  7. #define likely(x) __builtin_expect(!!(x), 1)
  8. #define unlikely(x) __builtin_expect(!!(x), 0)
  9. #undef offsetof
  10. #define offsetof(type, member) ((size_t)&((type *)0)->member)
  11. #define min(a, b) ((a) < (b) ? (a) : (b))
  12. #define max(a, b) ((a) > (b) ? (a) : (b))
  13. #define abs(x) ({ \
  14. __typeof__(x) __x = (x); \
  15. (__x < 0) ? -__x : __x; \
  16. })
  17. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  18. /* Memory barrier.
  19. * The CPU doesn't have runtime reordering, so we just
  20. * need a compiler memory clobber. */
  21. #define mb() __asm__ __volatile__("" : : : "memory")
  22. /* Convert something indirectly to a string */
  23. #define __stringify(x) #x
  24. #define stringify(x) __stringify(x)
  25. #define BUILD_BUG_ON(x) ((void)sizeof(char[1 - 2 * !!(x)]))
  26. typedef _Bool bool;
  27. #define true ((bool)(!!1))
  28. #define false ((bool)(!!0))
  29. static inline void irq_disable(void)
  30. {
  31. cli();
  32. mb();
  33. }
  34. static inline void irq_enable(void)
  35. {
  36. mb();
  37. sei();
  38. }
  39. static inline uint8_t irq_disable_save(void)
  40. {
  41. uint8_t sreg = SREG;
  42. cli();
  43. mb();
  44. return sreg;
  45. }
  46. static inline void irq_restore(uint8_t sreg_flags)
  47. {
  48. mb();
  49. SREG = sreg_flags;
  50. }
  51. #endif /* UTIL_H_ */