util.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #define LONG_MAX ((long int)((~0UL)>>1))
  20. #define LONG_MIN ((long int)~LONG_MAX)
  21. #ifndef offsetof
  22. #define offsetof(a,b) __builtin_offsetof(a,b)
  23. #endif
  24. #define container_of(ptr, type, member) ({ \
  25. typeof( ((type *)0)->member ) *__mptr = (ptr); \
  26. (type *)( (char *)__mptr - offsetof(type,member) );})
  27. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  28. #define min(x,y) ({ \
  29. const typeof(x) _x = (x); \
  30. const typeof(y) _y = (y); \
  31. (void) (&_x == &_y); \
  32. _x < _y ? _x : _y; })
  33. #define max(x,y) ({ \
  34. const typeof(x) _x = (x); \
  35. const typeof(y) _y = (y); \
  36. (void) (&_x == &_y); \
  37. _x > _y ? _x : _y; })
  38. #define min_t(type,x,y) \
  39. ({ type __x = (x); type __y = (y); __x < __y ? __x: __y; })
  40. #define max_t(type,x,y) \
  41. ({ type __x = (x); type __y = (y); __x > __y ? __x: __y; })
  42. /* Fast memset/memcpy: Pointers must be word-aligned, count must be a non-zero
  43. * multiple of 32 bytes. */
  44. void memset_fast(void *s, int c, size_t n);
  45. void memcpy_fast(void *dest, const void *src, size_t n);
  46. void *memset(void *s, int c, size_t n);
  47. void *memcpy(void *dest, const void *src, size_t n);
  48. void *memmove(void *dest, const void *src, size_t n);
  49. int memcmp(const void *s1, const void *s2, size_t n);
  50. size_t strlen(const char *s);
  51. size_t strnlen(const char *s, size_t maxlen);
  52. int strcmp(const char *s1, const char *s2);
  53. int strncmp(const char *s1, const char *s2, size_t n);
  54. char *strrchr(const char *s, int c);
  55. int tolower(int c);
  56. int isspace(int c);
  57. long int strtol(const char *nptr, char **endptr, int base);
  58. int vsnprintf(char *str, size_t size, const char *format, va_list ap)
  59. __attribute__ ((format (printf, 3, 0)));
  60. int snprintf(char *str, size_t size, const char *format, ...)
  61. __attribute__ ((format (printf, 3, 4)));
  62. #define le16toh(x) (x)
  63. #define le32toh(x) (x)
  64. #define htole16(x) (x)
  65. #define htole32(x) (x)
  66. #define be16toh(x) _rev16(x)
  67. #define be32toh(x) _rev32(x)
  68. #define htobe16(x) _rev16(x)
  69. #define htobe32(x) _rev32(x)
  70. /* Arena-based memory allocation */
  71. void *arena_alloc(uint32_t sz);
  72. uint32_t arena_total(void);
  73. uint32_t arena_avail(void);
  74. void arena_init(void);
  75. /* Board-specific callouts */
  76. void board_init(void);
  77. extern uint8_t board_id;
  78. #ifndef NDEBUG
  79. /* Serial console control */
  80. void console_init(void);
  81. void console_sync(void);
  82. void console_crash_on_input(void);
  83. /* Serial console output */
  84. int vprintk(const char *format, va_list ap)
  85. __attribute__ ((format (printf, 1, 0)));
  86. int printk(const char *format, ...)
  87. __attribute__ ((format (printf, 1, 2)));
  88. #else /* NDEBUG */
  89. #define console_init() ((void)0)
  90. #define console_sync() IRQ_global_disable()
  91. #define console_crash_on_input() ((void)0)
  92. static inline int vprintk(const char *format, va_list ap) { return 0; }
  93. static inline int printk(const char *format, ...) { return 0; }
  94. #endif
  95. /* USB */
  96. void usb_init(void);
  97. void usb_process(void);
  98. int ep_rx_ready(uint8_t ep);
  99. void usb_read(uint8_t ep, void *buf, uint32_t len);
  100. bool_t ep_tx_ready(uint8_t ep);
  101. void usb_write(uint8_t ep, const void *buf, uint32_t len);
  102. /* Floppy */
  103. void floppy_init(void);
  104. void floppy_reset(void);
  105. void floppy_configured(void);
  106. void floppy_process(void);
  107. /* CRC-CCITT */
  108. uint16_t crc16_ccitt(const void *buf, size_t len, uint16_t crc);
  109. /* Build info. */
  110. extern const char fw_ver[];
  111. /* Text/data/BSS address ranges. */
  112. extern char _stext[], _etext[];
  113. extern char _sdat[], _edat[], _ldat[];
  114. extern char _sbss[], _ebss[];
  115. /* Stacks. */
  116. extern uint32_t _thread_stacktop[], _thread_stackbottom[];
  117. extern uint32_t _irq_stacktop[], _irq_stackbottom[];
  118. /* Default exception handler. */
  119. void EXC_unused(void);
  120. /* IRQ priorities, 0 (highest) to 15 (lowest). */
  121. #define RESET_IRQ_PRI 0
  122. #define INDEX_IRQ_PRI 2
  123. #define TIMER_IRQ_PRI 4
  124. #define USB_IRQ_PRI 14
  125. #define CONSOLE_IRQ_PRI 15
  126. /*
  127. * Local variables:
  128. * mode: C
  129. * c-file-style: "Linux"
  130. * c-basic-offset: 4
  131. * tab-width: 4
  132. * indent-tabs-mode: nil
  133. * End:
  134. */