comparison byteordering.h @ 19:5f9a40d6991b

Import SD handling from http://www.roland-riegel.de/sd-reader/index.html Use smaller build options
author Matt Johnston <matt@ucc.asn.au>
date Tue, 25 Jun 2013 13:55:11 +0800
parents
children
comparison
equal deleted inserted replaced
18:021e6e0006f4 19:5f9a40d6991b
1
2 /*
3 * Copyright (c) 2006-2012 by Roland Riegel <[email protected]>
4 *
5 * This file is free software; you can redistribute it and/or modify
6 * it under the terms of either the GNU General Public License version 2
7 * or the GNU Lesser General Public License version 2.1, both as
8 * published by the Free Software Foundation.
9 */
10
11 #ifndef BYTEORDERING_H
12 #define BYTEORDERING_H
13
14 #include <stdint.h>
15
16 #ifdef __cplusplus
17 extern "C"
18 {
19 #endif
20
21 /**
22 * \addtogroup byteordering
23 *
24 * @{
25 */
26 /**
27 * \file
28 * Byte-order handling header (license: GPLv2 or LGPLv2.1)
29 *
30 * \author Roland Riegel
31 */
32
33 #define SWAP16(val) ((((uint16_t) (val)) << 8) | \
34 (((uint16_t) (val)) >> 8) \
35 )
36 #define SWAP32(val) (((((uint32_t) (val)) & 0x000000ff) << 24) | \
37 ((((uint32_t) (val)) & 0x0000ff00) << 8) | \
38 ((((uint32_t) (val)) & 0x00ff0000) >> 8) | \
39 ((((uint32_t) (val)) & 0xff000000) >> 24) \
40 )
41
42 #if LITTLE_ENDIAN || __AVR__
43 #define SWAP_NEEDED 0
44 #elif BIG_ENDIAN
45 #define SWAP_NEEDED 1
46 #else
47 #error "Endianess undefined! Please define LITTLE_ENDIAN=1 or BIG_ENDIAN=1."
48 #endif
49
50 /**
51 * \def HTOL16(val)
52 *
53 * Converts a 16-bit integer from host byte order to little-endian byte order.
54 *
55 * Use this macro for compile time constants only. For variable values
56 * use the function htol16() instead. This saves code size.
57 *
58 * \param[in] val A 16-bit integer in host byte order.
59 * \returns The given 16-bit integer converted to little-endian byte order.
60 */
61 /**
62 * \def HTOL32(val)
63 *
64 * Converts a 32-bit integer from host byte order to little-endian byte order.
65 *
66 * Use this macro for compile time constants only. For variable values
67 * use the function htol32() instead. This saves code size.
68 *
69 * \param[in] val A 32-bit integer in host byte order.
70 * \returns The given 32-bit integer converted to little-endian byte order.
71 */
72 /**
73 * \def LTOH16(val)
74 *
75 * Converts a 16-bit integer from little-endian byte order to host byte order.
76 *
77 * Use this macro for compile time constants only. For variable values
78 * use the function ltoh16() instead. This saves code size.
79 *
80 * \param[in] val A 16-bit integer in little-endian byte order.
81 * \returns The given 16-bit integer converted to host byte order.
82 */
83 /**
84 * \def LTOH32(val)
85 *
86 * Converts a 32-bit integer from little-endian byte order to host byte order.
87 *
88 * Use this macro for compile time constants only. For variable values
89 * use the function ltoh32() instead. This saves code size.
90 *
91 * \param[in] val A 32-bit integer in little-endian byte order.
92 * \returns The given 32-bit integer converted to host byte order.
93 */
94
95 #if SWAP_NEEDED
96 #define HTOL16(val) SWAP16(val)
97 #define HTOL32(val) SWAP32(val)
98 #define LTOH16(val) SWAP16(val)
99 #define LTOH32(val) SWAP32(val)
100 #else
101 #define HTOL16(val) (val)
102 #define HTOL32(val) (val)
103 #define LTOH16(val) (val)
104 #define LTOH32(val) (val)
105 #endif
106
107 #if DOXYGEN
108
109 /**
110 * Converts a 16-bit integer from host byte order to little-endian byte order.
111 *
112 * Use this function on variable values instead of the
113 * macro HTOL16(). This saves code size.
114 *
115 * \param[in] h A 16-bit integer in host byte order.
116 * \returns The given 16-bit integer converted to little-endian byte order.
117 */
118 uint16_t htol16(uint16_t h);
119
120 /**
121 * Converts a 32-bit integer from host byte order to little-endian byte order.
122 *
123 * Use this function on variable values instead of the
124 * macro HTOL32(). This saves code size.
125 *
126 * \param[in] h A 32-bit integer in host byte order.
127 * \returns The given 32-bit integer converted to little-endian byte order.
128 */
129 uint32_t htol32(uint32_t h);
130
131 /**
132 * Converts a 16-bit integer from little-endian byte order to host byte order.
133 *
134 * Use this function on variable values instead of the
135 * macro LTOH16(). This saves code size.
136 *
137 * \param[in] l A 16-bit integer in little-endian byte order.
138 * \returns The given 16-bit integer converted to host byte order.
139 */
140 uint16_t ltoh16(uint16_t l);
141
142 /**
143 * Converts a 32-bit integer from little-endian byte order to host byte order.
144 *
145 * Use this function on variable values instead of the
146 * macro LTOH32(). This saves code size.
147 *
148 * \param[in] l A 32-bit integer in little-endian byte order.
149 * \returns The given 32-bit integer converted to host byte order.
150 */
151 uint32_t ltoh32(uint32_t l);
152
153 #elif SWAP_NEEDED
154
155 #define htol16(h) swap16(h)
156 #define htol32(h) swap32(h)
157 #define ltoh16(l) swap16(l)
158 #define ltoh32(l) swap32(l)
159
160 #else
161
162 #define htol16(h) (h)
163 #define htol32(h) (h)
164 #define ltoh16(l) (l)
165 #define ltoh32(l) (l)
166
167 #endif
168
169 uint16_t read16(const uint8_t* p);
170 uint32_t read32(const uint8_t* p);
171 void write16(uint8_t* p, uint16_t i);
172 void write32(uint8_t* p, uint32_t i);
173
174 /**
175 * @}
176 */
177
178 #if SWAP_NEEDED
179 uint16_t swap16(uint16_t i);
180 uint32_t swap32(uint32_t i);
181 #endif
182
183 #ifdef __cplusplus
184 }
185 #endif
186
187 #endif
188