USB Host Shield 2.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
macros.h
Go to the documentation of this file.
1 /*
2  * File: macros.h
3  * Author: AJK
4  *
5  * Created on September 23, 2013, 12:31 AM
6  */
7 
8 #if !defined(_usb_h_) || defined(MACROS_H)
9 #error "Never include macros.h directly; include Usb.h instead"
10 #else
11 #define MACROS_H
12 
14 // HANDY MACROS
16 
17 #define SWAP(a, b) (((a) ^= (b)), ((b) ^= (a)), ((a) ^= (b)))
18 #ifndef __BYTE_GRABBING_DEFINED__
19 #define __BYTE_GRABBING_DEFINED__ 1
20 #ifdef BROKEN_OPTIMIZER_LITTLE_ENDIAN
21 // Note: Use this if your compiler generates horrible assembler!
22 #define BGRAB0(__usi__) (((uint8_t *)&(__usi__))[0])
23 #define BGRAB1(__usi__) (((uint8_t *)&(__usi__))[1])
24 #define BGRAB2(__usi__) (((uint8_t *)&(__usi__))[2])
25 #define BGRAB3(__usi__) (((uint8_t *)&(__usi__))[3])
26 #define BGRAB4(__usi__) (((uint8_t *)&(__usi__))[4])
27 #define BGRAB5(__usi__) (((uint8_t *)&(__usi__))[5])
28 #define BGRAB6(__usi__) (((uint8_t *)&(__usi__))[6])
29 #define BGRAB7(__usi__) (((uint8_t *)&(__usi__))[7])
30 #else
31 // Note: The cast alone to uint8_t is actually enough.
32 // GCC throws out the "& 0xff", and the size is no different.
33 // Some compilers need it.
34 #define BGRAB0(__usi__) ((uint8_t)((__usi__) & 0xff ))
35 #define BGRAB1(__usi__) ((uint8_t)(((__usi__) >> 8) & 0xff))
36 #define BGRAB2(__usi__) ((uint8_t)(((__usi__) >> 16) & 0xff))
37 #define BGRAB3(__usi__) ((uint8_t)(((__usi__) >> 24) & 0xff))
38 #define BGRAB4(__usi__) ((uint8_t)(((__usi__) >> 32) & 0xff))
39 #define BGRAB5(__usi__) ((uint8_t)(((__usi__) >> 40) & 0xff))
40 #define BGRAB6(__usi__) ((uint8_t)(((__usi__) >> 48) & 0xff))
41 #define BGRAB7(__usi__) ((uint8_t)(((__usi__) >> 56) & 0xff))
42 #endif
43 #define BOVER1(__usi__) ((uint16_t)(__usi__) << 8)
44 #define BOVER2(__usi__) ((uint32_t)(__usi__) << 16)
45 #define BOVER3(__usi__) ((uint32_t)(__usi__) << 24)
46 #define BOVER4(__usi__) ((uint64_t)(__usi__) << 32)
47 #define BOVER5(__usi__) ((uint64_t)(__usi__) << 40)
48 #define BOVER6(__usi__) ((uint64_t)(__usi__) << 48)
49 #define BOVER7(__usi__) ((uint64_t)(__usi__) << 56)
50 
51 // These are the smallest and fastest ways I have found so far in pure C/C++.
52 #define BMAKE16(__usc1__,__usc0__) ((uint16_t)((uint16_t)(__usc0__) | (uint16_t)BOVER1(__usc1__)))
53 #define BMAKE32(__usc3__,__usc2__,__usc1__,__usc0__) ((uint32_t)((uint32_t)(__usc0__) | (uint32_t)BOVER1(__usc1__) | (uint32_t)BOVER2(__usc2__) | (uint32_t)BOVER3(__usc3__)))
54 #define BMAKE64(__usc7__,__usc6__,__usc5__,__usc4__,__usc3__,__usc2__,__usc1__,__usc0__) ((uint64_t)((uint64_t)__usc0__ | (uint64_t)BOVER1(__usc1__) | (uint64_t)BOVER2(__usc2__) | (uint64_t)BOVER3(__usc3__) | (uint64_t)BOVER4(__usc4__) | (uint64_t)BOVER5(__usc5__) | (uint64_t)BOVER6(__usc6__) | (uint64_t)BOVER1(__usc7__)))
55 #endif
56 
57 /*
58  * Debug macros: Strings are stored in progmem (flash) instead of RAM.
59  */
60 #define USBTRACE(s) (Notify(PSTR(s), 0x80))
61 #define USBTRACE2(s,r) (Notify(PSTR(s), 0x80), D_PrintHex((r), 0x80), Notify(PSTR("\r\n"), 0x80))
62 
63 
64 #endif /* MACROS_H */
65