#ifndef WIFI_UTIL_H_ #define WIFI_UTIL_H_ #include #include #include #include /* Some of these macros are derived from the Linux Kernel sources. */ #define likely(x) __builtin_expect(!!(x), 1) #define unlikely(x) __builtin_expect(!!(x), 0) #define offsetof(type, member) ((size_t)&((type *)0)->member) #define min(a, b) ((a) < (b) ? (a) : (b)) #define max(a, b) ((a) > (b) ? (a) : (b)) #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) /* Memory barrier. * The CPU doesn't have runtime reordering, so we just * need a compiler memory clobber. */ #define mb() __asm__ __volatile__("" : : : "memory") /* Convert something indirectly to a string */ #define __stringify(x) #x #define stringify(x) __stringify(x) /* Assertions */ void panic(const prog_char *msg) __attribute__((noreturn)); #define BUILD_BUG_ON(x) ((void)sizeof(char[1 - 2 * !!(x)])) #define BUG_ON(x) \ do { \ if (unlikely(x)) \ panic(PSTR(__FILE__ \ stringify(__LINE__))); \ } while (0) void infinite_sleep(void) __attribute__((noreturn)); /* Delay */ void mdelay(uint16_t msecs); void udelay(uint16_t usecs); typedef _Bool bool; /* EEPROM access */ typedef char eeprom_char; typedef uint8_t eeprom_uint8_t; typedef uint16_t eeprom_uint16_t; uint8_t eeprom_read8(const eeprom_uint8_t *addr); uint16_t eeprom_read16(const eeprom_uint16_t *addr); void eeprom_write8(eeprom_uint8_t *addr, uint8_t value); void eeprom_write16(eeprom_uint16_t *addr, uint16_t value); /* Convert a number (0-F) to a hexadecimal ASCII digit */ uint8_t hexdigit_to_ascii(uint8_t digit); #define NUM16_NR_DIGITS 5 /* Convert a number to ascii. * buf must be at least (NUM16_NR_DIGITS + 1) long */ void num16_to_ascii(uint8_t *buf, uint16_t v); /* IRQ handling. * We use a macro for irq_disable_save(), because stupid gcc is not * able to properly optimize an inline function that returns a value. */ #define irq_disable_save() ({ \ uint8_t sreg_flags = SREG; \ cli(); \ sreg_flags; \ }) static inline void irq_restore(uint8_t sreg_flags) { SREG = sreg_flags; } #endif /* WIFI_UTIL_H_ */