USB Host Shield 2.0
macros.h
Go to the documentation of this file.
1 /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
2 
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2 of the License, or
6 (at your option) any later version.
7 
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12 
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 
17 Contact information
18 -------------------
19 
20 Circuits At Home, LTD
21 Web : http://www.circuitsathome.com
22 e-mail : support@circuitsathome.com
23  */
24 
25 #if !defined(_usb_h_) || defined(MACROS_H)
26 #error "Never include macros.h directly; include Usb.h instead"
27 #else
28 #define MACROS_H
29 
31 // HANDY MACROS
33 
34 #define VALUE_BETWEEN(v,l,h) (((v)>(l)) && ((v)<(h)))
35 #define VALUE_WITHIN(v,l,h) (((v)>=(l)) && ((v)<=(h)))
36 #define output_pgm_message(wa,fp,mp,el) wa = &mp, fp((char *)pgm_read_pointer(wa), el)
37 #define output_if_between(v,l,h,wa,fp,mp,el) if(VALUE_BETWEEN(v,l,h)) output_pgm_message(wa,fp,mp[v-(l+1)],el);
38 
39 #define SWAP(a, b) (((a) ^= (b)), ((b) ^= (a)), ((a) ^= (b)))
40 #ifndef __BYTE_GRABBING_DEFINED__
41 #define __BYTE_GRABBING_DEFINED__ 1
42 #ifdef BROKEN_OPTIMIZER_LITTLE_ENDIAN
43 // Note: Use this if your compiler generates horrible assembler!
44 #define BGRAB0(__usi__) (((uint8_t *)&(__usi__))[0])
45 #define BGRAB1(__usi__) (((uint8_t *)&(__usi__))[1])
46 #define BGRAB2(__usi__) (((uint8_t *)&(__usi__))[2])
47 #define BGRAB3(__usi__) (((uint8_t *)&(__usi__))[3])
48 #define BGRAB4(__usi__) (((uint8_t *)&(__usi__))[4])
49 #define BGRAB5(__usi__) (((uint8_t *)&(__usi__))[5])
50 #define BGRAB6(__usi__) (((uint8_t *)&(__usi__))[6])
51 #define BGRAB7(__usi__) (((uint8_t *)&(__usi__))[7])
52 #else
53 // Note: The cast alone to uint8_t is actually enough.
54 // GCC throws out the "& 0xff", and the size is no different.
55 // Some compilers need it.
56 #define BGRAB0(__usi__) ((uint8_t)((__usi__) & 0xff ))
57 #define BGRAB1(__usi__) ((uint8_t)(((__usi__) >> 8) & 0xff))
58 #define BGRAB2(__usi__) ((uint8_t)(((__usi__) >> 16) & 0xff))
59 #define BGRAB3(__usi__) ((uint8_t)(((__usi__) >> 24) & 0xff))
60 #define BGRAB4(__usi__) ((uint8_t)(((__usi__) >> 32) & 0xff))
61 #define BGRAB5(__usi__) ((uint8_t)(((__usi__) >> 40) & 0xff))
62 #define BGRAB6(__usi__) ((uint8_t)(((__usi__) >> 48) & 0xff))
63 #define BGRAB7(__usi__) ((uint8_t)(((__usi__) >> 56) & 0xff))
64 #endif
65 #define BOVER1(__usi__) ((uint16_t)(__usi__) << 8)
66 #define BOVER2(__usi__) ((uint32_t)(__usi__) << 16)
67 #define BOVER3(__usi__) ((uint32_t)(__usi__) << 24)
68 #define BOVER4(__usi__) ((uint64_t)(__usi__) << 32)
69 #define BOVER5(__usi__) ((uint64_t)(__usi__) << 40)
70 #define BOVER6(__usi__) ((uint64_t)(__usi__) << 48)
71 #define BOVER7(__usi__) ((uint64_t)(__usi__) << 56)
72 
73 // These are the smallest and fastest ways I have found so far in pure C/C++.
74 #define BMAKE16(__usc1__,__usc0__) ((uint16_t)((uint16_t)(__usc0__) | (uint16_t)BOVER1(__usc1__)))
75 #define BMAKE32(__usc3__,__usc2__,__usc1__,__usc0__) ((uint32_t)((uint32_t)(__usc0__) | (uint32_t)BOVER1(__usc1__) | (uint32_t)BOVER2(__usc2__) | (uint32_t)BOVER3(__usc3__)))
76 #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__)))
77 #endif
78 
79 /*
80  * Debug macros: Strings are stored in progmem (flash) instead of RAM.
81  */
82 #define USBTRACE(s) (Notify(PSTR(s), 0x80))
83 #define USBTRACE1(s,l) (Notify(PSTR(s), l))
84 #define USBTRACE2(s,r) (Notify(PSTR(s), 0x80), D_PrintHex((r), 0x80), Notify(PSTR("\r\n"), 0x80))
85 #define USBTRACE3(s,r,l) (Notify(PSTR(s), l), D_PrintHex((r), l), Notify(PSTR("\r\n"), l))
86 
87 
88 #endif /* MACROS_H */
89