/
Types.h
565 lines (512 loc) · 11.6 KB
/
Types.h
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/**
* @file Cosa/Types.h
* @version 1.0
*
* @section License
* Copyright (C) 2012-2015, Mikael Patel
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* @section Description
* Common literals, data types and syntax abstractions.
*
* This file is part of the Arduino Che Cosa project.
*/
#ifndef COSA_TYPES_H
#define COSA_TYPES_H
#include <avr/io.h>
#include <avr/sleep.h>
#include <avr/eeprom.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <avr/sfr_defs.h>
#include <util/delay_basic.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "Cosa/Errno.h"
#include "Cosa/Board.hh"
#include "Cosa.h"
/**
* Number of bits in a character.
*/
#define CHARBITS 8
/**
* Number of bytes for given number of bits.
*/
#define BYTES(bits) (((bits) + (CHARBITS - 1)) / CHARBITS)
/**
* Standard integer range
*/
#ifndef UINT8_MAX
# define UINT8_MAX 0xffU
#endif
#ifndef UINT16_MAX
# define UINT16_MAX 0xffffU
#endif
#ifndef UINT32_MAX
# define UINT32_MAX 0xffffffffUL
#endif
#ifndef INT_MAX
# define INT_MIN INT16_MIN
# define INT_MAX INT16_MAX
#endif
#ifndef INT8_MAX
# define INT8_MIN 0x80
# define INT8_MAX 0x7f
#endif
#ifndef INT16_MAX
# define INT16_MIN 0x8000
# define INT16_MAX 0x7fff
#endif
#ifndef INT32_MAX
# define INT32_MIN 0x80000000L
# define INT32_MAX 0x7fffffffL
#endif
#ifndef INT_MAX
# define INT_MIN INT16_MIN
# define INT_MAX INT16_MAX
#endif
/**
* Standard null pointer
*/
#ifndef NULL
# define NULL ((void*) 0)
#endif
/**
* Standard floating point number, 32-bit.
*/
typedef float float32_t;
/**
* Universal type union, 16-bit.
*/
union univ16_t {
uint16_t as_uint16;
int16_t as_int16;
uint8_t as_uint8[2];
int8_t as_int8[2];
const void* as_ptr_P;
void* as_ptr;
struct {
uint8_t low;
uint8_t high;
};
};
typedef univ16_t univ_t;
/**
* Universal type union, 32-bit.
*/
union univ32_t {
float32_t as_float32;
uint32_t as_uint32;
int32_t as_int32;
univ16_t as_univ16[2];
uint16_t as_uint16[2];
int16_t as_int16[2];
uint8_t as_uint8[4];
int8_t as_int8[4];
const void* as_ptr_P[2];
void* as_ptr[2];
struct {
uint16_t low;
uint16_t high;
};
};
/**
* Compiler branch prediction hinting.
*/
#define LIKELY(x) __builtin_expect((x), true)
#define UNLIKELY(x) __builtin_expect((x), false)
/**
* Compiler warning on unused varable.
*/
#define UNUSED(x) (void) (x)
/**
* Macro for number of elements in a vector.
* @param[in] x vector.
* @return number of elements.
*/
#define membersof(x) (sizeof(x) / sizeof(x[0]))
/*
* Workaround for gcc offsetof macro usage and program memory data
* warning in Arduino build with older version of AVR-GCC (1.0.5,
* 1.5.6-r2 etc).
*/
#if ((__GNUC__ == 4) && (__GNUC_MINOR__ <= 3))
# undef offsetof
# define offsetof(t,m) \
(__extension__( \
{ \
const t* __p = NULL; \
(size_t) &__p->m; \
} \
))
# define __PROGMEM __attribute__((section(".progmem.data")))
#else
# define __PROGMEM PROGMEM
#endif
/** String in program memory */
typedef const PROGMEM class prog_str* str_P;
/**
* Program string literal that may be used in macro. Is not unique.
* @param[in] s string literal (at compile time).
* @return string literal in program memory.
*/
#define STR_P(s) \
(__extension__( \
{ \
static const char __c[] __PROGMEM = (s); \
(str_P) &__c[0]; \
} \
))
#undef PSTR
#define PSTR(s) STR_P(s)
#define __PSTR(s) STR_P(s)
inline char*
strcat_P(char* s1, str_P s2)
{
return (strcat_P(s1, (const char*) s2));
}
inline str_P
strchr_P(str_P s, int __val)
{
return ((str_P) strchr_P((const char*) s, __val));
}
inline str_P
strchrnul_P(str_P s, int __val)
{
return ((str_P) strchrnul_P((const char*) s, __val));
}
inline int
strcmp_P(const char *s1, str_P s2)
{
return (strcmp_P(s1, (const char*) s2));
}
inline char*
strcpy_P(char* s1, str_P s2)
{
return (strcpy_P(s1, (const char*) s2));
}
inline int
strcasecmp_P(const char *s1, str_P s2)
{
return (strcasecmp_P(s1, (const char*) s2));
}
inline char*
strcasestr_P(const char *s1, str_P s2)
{
return (strcasestr_P(s1, (const char*) s2));
}
inline size_t
strlen_P(str_P s)
{
return (strlen_P((const char*) s));
}
/** Pointer table in program memory */
typedef const PROGMEM void* void_P;
typedef const PROGMEM void_P void_vec_P;
/* Check if static_assert needs to be disabled */
#if (ARDUINO < 150)
# if !defined(__GXX_EXPERIMENTAL_CXX0X__)
# define static_assert(condition,message)
# endif
#endif
/**
* Instruction clock cycles per micro-second. Assumes clock greater
* or equal to 1 MHz.
*/
#define I_CPU (F_CPU / 1000000L)
/**
* Macro for micro-second level delay.
* @param[in] us micro-seconds.
*/
#define DELAY(us) _delay_loop_2(((us) * I_CPU) / 4)
/**
* Delay given number of milli-seconds. This function pointer
* may be redefined to allow low-power and/or multi-tasking duing wait.
* @param[in] ms milli-seconds delay.
*/
extern void (*delay)(uint32_t ms);
/**
* Sleep given number of seconds. This function pointer may be
* redefined to allow low-power and/or multi-tasking duing wait.
* @param[in] s seconds delay.
*/
extern void (*sleep)(uint16_t s);
/**
* Allow context switch to other task if available. The default
* implementation is a low-power sleep and wait for interrupt.
*/
extern void (*yield)();
/**
* Disable interrupts and return flags.
* @return processor flags.
*/
inline uint8_t
lock()
{
uint8_t key = SREG;
cli();
return (key);
}
/**
* Restore processor flags and possible enable of interrupts.
* @param[in] key processor flags.
*/
inline void
unlock(uint8_t key)
{
SREG = key;
}
/**
* Syntactic sugar for synchronized block. Used in the form:
* @code
* synchronized {
* ...
* synchronized_return(expr);
* ...
* synchronized_goto(label);
* ...
* }
* label:
* @endcode
* Interrupts are disabled in the block allowing secure update.
*/
#define synchronized \
for (uint8_t __key = lock(), i = 1; i != 0; i--, unlock(__key))
#define synchronized_return(expr) \
return (unlock(__key), expr)
#define synchronized_goto(label) \
do { unlock(__key); goto label; } while (0)
/**
* Force compiler to store all values in memory at this point.
* Alternative to volatile declaration.
*/
#define barrier() __asm__ __volatile__("nop" ::: "memory")
/**
* Buffer structure for scatter/gather.
*/
struct iovec_t {
void* buf; //!< Buffer pointer.
size_t size; //!< Size of buffer in bytes.
};
/**
* Return total size of null terminated io buffer vector.
* @param[in] vp io vector pointer
* @return size.
*/
inline size_t iovec_size(const iovec_t* vec) __attribute__((always_inline));
inline size_t
iovec_size(const iovec_t* vec)
{
size_t len = 0;
for (const iovec_t* vp = vec; vp->buf != NULL; vp++)
len += vp->size;
return (len);
}
/**
* Set next io-vector buffer. Used in to form:
* @code
* iovec_t vec[N];
* iovec_t* vp = vec;
* iovec_arg(vp, buf, size);
* ..
* iovec_end(vp);
* @endcode
* @param[in,out] vp io vector pointer
* @param[in] buf buffer.
* @param[in] size number of bytes.
*/
inline void iovec_arg(iovec_t* &vp, const void* buf, size_t size)
__attribute__((always_inline));
inline void
iovec_arg(iovec_t* &vp, const void* buf, size_t size)
{
vp->buf = (void*) buf;
vp->size = size;
vp++;
}
/**
* Mark end of io-vector buffer at given index. Used in the form:
* @code
* iovec_t vec[N];
* iovec_t* vp = vec;
* iovec_arg(vp, buf, size);
* ..
* iovec_end(vp);
* @endcode
* @param[in,out] vp io vector.
*/
inline void iovec_end(iovec_t* &vp) __attribute__((always_inline));
inline void
iovec_end(iovec_t* &vp)
{
vp->buf = 0;
vp->size = 0;
}
/**
* Swap bytes in 16-bit unsigned integer.
* @param[in] value to byte swap.
* @return new value.
*/
inline uint16_t swap(uint16_t value) __attribute__((always_inline));
inline uint16_t
swap(uint16_t value)
{
asm volatile("mov __tmp_reg__, %A0" "\n\t"
"mov %A0, %B0" "\n\t"
"mov %B0, __tmp_reg__" "\n\t"
: "=r" (value)
: "0" (value)
);
return (value);
}
/**
* Swap bytes in 16-bit unsigned integer vector.
* @param[in] dest destination buffer.
* @param[in] src source buffer.
* @param[in] size number of integers to swap.
*/
inline void
swap(uint16_t* dest, const uint16_t* src, size_t size)
{
if (size == 0) return;
do {
*dest++ = swap(*src++);
} while (--size);
}
/**
* Swap bytes in 16-bit values in struct.
* @param[in] T type of struct.
* @param[in] dest destination buffer.
* @param[in] src source buffer.
*/
template<class T>
void swap(T* dest, const T* src)
{
swap((uint16_t*) dest, (const uint16_t*) src, sizeof(T) / sizeof(uint16_t));
}
/**
* Destructive swap bytes in 16-bit unsigned integer vector.
* @param[in] buf buffer.
* @param[in] size number of integers to swap.
*/
inline void
swap(uint16_t* buf, size_t size)
{
if (size == 0) return;
do {
int16_t data = *buf;
*buf++ = swap(data);
} while (--size);
}
/**
* Destructive swap bytes in 16-bit integers in struct.
* @param[in] T type of struct.
* @param[in] buf buffer.
*/
template<class T>
void swap(T* buf)
{
swap((uint16_t*) buf, sizeof(T) / sizeof(uint16_t));
}
/**
* Swap bytes in 16-bit signed integer.
* @param[in] value to byte swap.
* @return new value.
*/
inline int16_t swap(int16_t value) __attribute__((always_inline));
inline int16_t
swap(int16_t value)
{
return ((int16_t) swap((uint16_t) value));
}
/**
* Swap bytes in 16-bit integer vector.
* @param[in] dest destination buffer.
* @param[in] src source buffer.
* @param[in] size number of integers to swap.
*/
inline void
swap(int16_t* dest, const int16_t* src, size_t size)
{
if (size == 0) return;
do {
*dest++ = swap(*src++);
} while (--size);
}
/**
* Swap bytes in 32-bit unsigned integer.
* @param[in] value to byte swap.
* @return new value.
*/
inline uint32_t swap(uint32_t value) __attribute__((always_inline));
inline uint32_t
swap(uint32_t value)
{
asm volatile("mov __tmp_reg__, %A0" "\n\t"
"mov %A0, %D0" "\n\t"
"mov %D0, __tmp_reg__" "\n\t"
"mov __tmp_reg__, %B0" "\n\t"
"mov %B0, %C0" "\n\t"
"mov %C0, __tmp_reg__" "\n\t"
: "=r" (value)
: "0" (value)
);
return (value);
}
/**
* Swap bytes in 32-bit signed integer.
* @param[in] value to byte swap.
* @return new value.
*/
inline int32_t swap(int32_t value) __attribute__((always_inline));
inline int32_t
swap(int32_t value)
{
return ((int32_t) swap((uint32_t) value));
}
/**
* Convert values between host and network byte order. AVR is
* littlendian and network is bigendian so byte swap.
*/
#define ntoh swap
#define hton swap
/**
* Convert 4-bit LSB value to hexadecimal character ('0'..'f').
* @param[in] value.
* @return character.
*/
inline char tohex(uint8_t value) __attribute__((always_inline));
inline char
tohex(uint8_t value)
{
value &= 0xf;
if (value > 9)
return (value - 10 + 'a');
return (value + '0');
}
/**
* Convert 4-bit LSB value to hexadecimal character ('0'..'F').
* @param[in] value.
* @return character.
*/
inline char toHEX(uint8_t value) __attribute__((always_inline));
inline char
toHEX(uint8_t value)
{
value &= 0xf;
if (value > 9)
return (value - 10 + 'A');
return (value + '0');
}
#endif