util.h 4.5 KB

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