util.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * util.h
  3. *
  4. * Utility definitions.
  5. *
  6. * Written & released by Keir Fraser <keir.xen@gmail.com>
  7. *
  8. * This is free and unencumbered software released into the public domain.
  9. * See the file COPYING for more details, or visit <http://unlicense.org>.
  10. */
  11. #ifndef NDEBUG
  12. #define ASSERT(p) do { if (!(p)) illegal(); } while (0)
  13. #else
  14. #define ASSERT(p) do { if (0 && (p)) {} } while (0)
  15. #endif
  16. typedef char bool_t;
  17. #define TRUE 1
  18. #define FALSE 0
  19. #ifndef offsetof
  20. #define offsetof(a,b) __builtin_offsetof(a,b)
  21. #endif
  22. #define container_of(ptr, type, member) ({ \
  23. typeof( ((type *)0)->member ) *__mptr = (ptr); \
  24. (type *)( (char *)__mptr - offsetof(type,member) );})
  25. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  26. #define min(x,y) ({ \
  27. const typeof(x) _x = (x); \
  28. const typeof(y) _y = (y); \
  29. (void) (&_x == &_y); \
  30. _x < _y ? _x : _y; })
  31. #define max(x,y) ({ \
  32. const typeof(x) _x = (x); \
  33. const typeof(y) _y = (y); \
  34. (void) (&_x == &_y); \
  35. _x > _y ? _x : _y; })
  36. #define min_t(type,x,y) \
  37. ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
  38. #define max_t(type,x,y) \
  39. ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
  40. /* Fast memset/memcpy: Pointers must be word-aligned, count must be a non-zero
  41. * multiple of 32 bytes. */
  42. void memset_fast(void *s, int c, size_t n);
  43. void memcpy_fast(void *dest, const void *src, size_t n);
  44. void *memset(void *s, int c, size_t n);
  45. void *memcpy(void *dest, const void *src, size_t n);
  46. void *memmove(void *dest, const void *src, size_t n);
  47. int memcmp(const void *s1, const void *s2, size_t n);
  48. size_t strlen(const char *s);
  49. size_t strnlen(const char *s, size_t maxlen);
  50. int strcmp(const char *s1, const char *s2);
  51. int strncmp(const char *s1, const char *s2, size_t n);
  52. int vsnprintf(char *str, size_t size, const char *format, va_list ap)
  53. __attribute__ ((format (printf, 3, 0)));
  54. int snprintf(char *str, size_t size, const char *format, ...)
  55. __attribute__ ((format (printf, 3, 4)));
  56. #define le16toh(x) (x)
  57. #define le32toh(x) (x)
  58. #define htole16(x) (x)
  59. #define htole32(x) (x)
  60. #define be16toh(x) _rev16(x)
  61. #define be32toh(x) _rev32(x)
  62. #define htobe16(x) _rev16(x)
  63. #define htobe32(x) _rev32(x)
  64. uint32_t udiv64(uint64_t dividend, uint32_t divisor);
  65. /* Board-specific callouts */
  66. void board_init(void);
  67. void act_led(bool_t on);
  68. void act_led2(bool_t on);
  69. #ifndef NDEBUG
  70. /* Serial console control */
  71. void console_init(void);
  72. void console_crash_on_input(void);
  73. /* Serial console output */
  74. int vprintk(const char *format, va_list ap)
  75. __attribute__ ((format (printf, 1, 0)));
  76. int printk(const char *format, ...)
  77. __attribute__ ((format (printf, 1, 2)));
  78. #else /* NDEBUG */
  79. #define console_init() ((void)0)
  80. #define console_crash_on_input() ((void)0)
  81. static inline int vprintk(const char *format, va_list ap) { return 0; }
  82. static inline int printk(const char *format, ...) { return 0; }
  83. #endif
  84. /* Floppy */
  85. void floppy_init(void);
  86. void floppy_process(void);
  87. /* Test mode */
  88. uint8_t testmode_init(void);
  89. void testmode_process(void);
  90. /* CRC-CCITT */
  91. uint16_t crc16_ccitt(const void *buf, size_t len, uint16_t crc);
  92. /* Build info. */
  93. extern const uint8_t fw_major, fw_minor;
  94. /* Bootloader mode flag. */
  95. extern volatile uint32_t _reset_flag;
  96. /* Text/data/BSS address ranges. */
  97. extern char _stext[], _etext[];
  98. extern char _sdat[], _edat[], _ldat[];
  99. extern char _sbss[], _ebss[];
  100. /* Stacks. */
  101. extern uint32_t _thread_stacktop[], _thread_stackbottom[];
  102. extern uint32_t _irq_stacktop[], _irq_stackbottom[];
  103. /* Default exception handler. */
  104. void EXC_unused(void);
  105. /* IRQ priorities, 0 (highest) to 15 (lowest). */
  106. #define RESET_IRQ_PRI 0
  107. #define INDEX_IRQ_PRI 2
  108. #define TIMER_IRQ_PRI 4
  109. #define USB_IRQ_PRI 6
  110. /*
  111. * Local variables:
  112. * mode: C
  113. * c-file-style: "Linux"
  114. * c-basic-offset: 4
  115. * tab-width: 4
  116. * indent-tabs-mode: nil
  117. * End:
  118. */