summaryrefslogtreecommitdiffstats
path: root/firmware/util.h
blob: 3d388d648a1140dd6be56228f3e16f4880adc5ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#ifndef WIFI_UTIL_H_
#define WIFI_UTIL_H_

#include <stdint.h>
#include <avr/eeprom.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <util/delay.h>

/* 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)

#undef offsetof
#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 abs(x)	({			\
	__typeof__(x) __x = (x);	\
	(__x < 0) ? -__x : __x;		\
		})

#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)

/* Progmem pointer annotation. */
#define PROGPTR			/* progmem pointer */

/* Assertions */
void panic(const char PROGPTR *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 reboot(void) __attribute__((noreturn));
void infinite_sleep(void) __attribute__((noreturn));
void wdt_enable_irq_mode(uint8_t timeout);


typedef _Bool		bool;

/* Endianess */
typedef uint16_t le16_t;
typedef uint32_t le32_t;

/* The compiler already uses little endian order for multibyte
 * values in memory. */
static inline uint16_t le16_to_cpu(le16_t x)
{
	return (uint16_t)x;
}
static inline le16_t cpu_to_le16(uint16_t x)
{
	return (le16_t)x;
}
static inline uint32_t le32_to_cpu(le32_t x)
{
	return (uint32_t)x;
}
static inline le32_t cpu_to_le32(uint32_t x)
{
	return (le32_t)x;
}


static inline void irq_disable(void)
{
	cli();
	mb();
}

static inline void irq_enable(void)
{
	mb();
	sei();
}

static inline uint8_t irq_disable_save(void)
{
	uint8_t sreg = SREG;
	cli();
	mb();
	return sreg;
}

static inline void irq_restore(uint8_t sreg_flags)
{
	mb();
	SREG = sreg_flags;
}


#define COMPILE_YEAR ((((__DATE__[7] - '0') * 10 +				\
			(__DATE__[8] - '0')) * 10 +				\
			(__DATE__[9] - '0')) * 10 +				\
			(__DATE__[10] - '0'))

#define COMPILE_MONTH ((__DATE__[2] == 'n' ? (__DATE__[1] == 'a' ? 0 : 5)	\
			: __DATE__[2] == 'b' ? 1				\
			: __DATE__[2] == 'r' ? (__DATE__[0] == 'M' ? 2 : 3)	\
			: __DATE__[2] == 'y' ? 4				\
			: __DATE__[2] == 'l' ? 6				\
			: __DATE__[2] == 'g' ? 7				\
			: __DATE__[2] == 'p' ? 8				\
			: __DATE__[2] == 't' ? 9				\
			: __DATE__[2] == 'v' ? 10 : 11) + 1)

#define COMPILE_DAY ((__DATE__[4] == ' ' ? 0 : __DATE__[4] - '0') * 10 +	\
		     (__DATE__[5] - '0'))

#endif /* WIFI_UTIL_H_ */
bues.ch cgit interface