util.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #ifndef NDEBUG
  69. /* Serial console control */
  70. void console_init(void);
  71. void console_crash_on_input(void);
  72. /* Serial console output */
  73. int vprintk(const char *format, va_list ap)
  74. __attribute__ ((format (printf, 1, 0)));
  75. int printk(const char *format, ...)
  76. __attribute__ ((format (printf, 1, 2)));
  77. #else /* NDEBUG */
  78. #define console_init() ((void)0)
  79. #define console_crash_on_input() ((void)0)
  80. static inline int vprintk(const char *format, va_list ap) { return 0; }
  81. static inline int printk(const char *format, ...) { return 0; }
  82. #endif
  83. /* Floppy */
  84. void floppy_init(void);
  85. void floppy_process(void);
  86. /* Test mode */
  87. uint8_t testmode_init(void);
  88. void testmode_process(void);
  89. /* CRC-CCITT */
  90. uint16_t crc16_ccitt(const void *buf, size_t len, uint16_t crc);
  91. /* Build info. */
  92. extern const uint8_t fw_major, fw_minor;
  93. /* Bootloader mode flag. */
  94. extern volatile uint32_t _reset_flag;
  95. /* Text/data/BSS address ranges. */
  96. extern char _stext[], _etext[];
  97. extern char _sdat[], _edat[], _ldat[];
  98. extern char _sbss[], _ebss[];
  99. /* Stacks. */
  100. extern uint32_t _thread_stacktop[], _thread_stackbottom[];
  101. extern uint32_t _irq_stacktop[], _irq_stackbottom[];
  102. /* Default exception handler. */
  103. void EXC_unused(void);
  104. /* IRQ priorities, 0 (highest) to 15 (lowest). */
  105. #define RESET_IRQ_PRI 0
  106. #define INDEX_IRQ_PRI 2
  107. #define TIMER_IRQ_PRI 4
  108. #define USB_IRQ_PRI 6
  109. /*
  110. * Local variables:
  111. * mode: C
  112. * c-file-style: "Linux"
  113. * c-basic-offset: 4
  114. * tab-width: 4
  115. * indent-tabs-mode: nil
  116. * End:
  117. */