1 /* Copyright (c) 2008-2011 Freescale Semiconductor, Inc.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 * * Redistributions of source code must retain the above copyright
7 * notice, this list of conditions and the following disclaimer.
8 * * Redistributions in binary form must reproduce the above copyright
9 * notice, this list of conditions and the following disclaimer in the
10 * documentation and/or other materials provided with the distribution.
11 * * Neither the name of Freescale Semiconductor nor the
12 * names of its contributors may be used to endorse or promote products
13 * derived from this software without specific prior written permission.
16 * ALTERNATIVELY, this software may be distributed under the terms of the
17 * GNU General Public License ("GPL") as published by the Free Software
18 * Foundation, either version 2 of that License or (at your option) any
21 * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY
22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY
25 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
28 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 /**************************************************************************//**
37 @Description Big/little endian swapping routines.
38 *//***************************************************************************/
40 #ifndef __ENDIAN_EXT_H
41 #define __ENDIAN_EXT_H
46 /**************************************************************************//**
47 @Group gen_id General Drivers Utilities
49 @Description General usage API. This API is intended for usage by both the
50 internal modules and the user's application.
53 *//***************************************************************************/
55 /**************************************************************************//**
56 @Group endian_id Big/Little-Endian Conversion
58 @Description Routines and macros for Big/Little-Endian conversion and
59 general byte swapping.
61 All routines and macros are expecting unsigned values as
62 parameters, but will generate the correct result also for
63 signed values. Therefore, signed/unsigned casting is allowed.
65 *//***************************************************************************/
67 /**************************************************************************//**
68 @Collection Byte-Swap Macros
70 Macros for swapping byte order.
72 @Cautions The parameters of these macros are evaluated multiple times.
73 For calculated expressions or expressions that contain function
74 calls it is recommended to use the byte-swap routines.
77 *//***************************************************************************/
79 /**************************************************************************//**
80 @Description Swaps the byte order of a given 16-bit value.
82 @Param[in] val - The 16-bit value to swap.
84 @Return The byte-swapped value..
86 @Cautions The given value is evaluated multiple times by this macro.
87 For calculated expressions or expressions that contain function
88 calls it is recommended to use the SwapUint16() routine.
91 *//***************************************************************************/
92 #define SWAP_UINT16(val) \
93 ((uint16_t)((((val) & 0x00FF) << 8) | (((val) & 0xFF00) >> 8)))
95 /**************************************************************************//**
96 @Description Swaps the byte order of a given 32-bit value.
98 @Param[in] val - The 32-bit value to swap.
100 @Return The byte-swapped value..
102 @Cautions The given value is evaluated multiple times by this macro.
103 For calculated expressions or expressions that contain function
104 calls it is recommended to use the SwapUint32() routine.
107 *//***************************************************************************/
108 #define SWAP_UINT32(val) \
109 ((uint32_t)((((val) & 0x000000FF) << 24) | \
110 (((val) & 0x0000FF00) << 8) | \
111 (((val) & 0x00FF0000) >> 8) | \
112 (((val) & 0xFF000000) >> 24)))
114 /**************************************************************************//**
115 @Description Swaps the byte order of a given 64-bit value.
117 @Param[in] val - The 64-bit value to swap.
119 @Return The byte-swapped value..
121 @Cautions The given value is evaluated multiple times by this macro.
122 For calculated expressions or expressions that contain function
123 calls it is recommended to use the SwapUint64() routine.
126 *//***************************************************************************/
127 #define SWAP_UINT64(val) \
128 ((uint64_t)((((val) & 0x00000000000000FFULL) << 56) | \
129 (((val) & 0x000000000000FF00ULL) << 40) | \
130 (((val) & 0x0000000000FF0000ULL) << 24) | \
131 (((val) & 0x00000000FF000000ULL) << 8) | \
132 (((val) & 0x000000FF00000000ULL) >> 8) | \
133 (((val) & 0x0000FF0000000000ULL) >> 24) | \
134 (((val) & 0x00FF000000000000ULL) >> 40) | \
135 (((val) & 0xFF00000000000000ULL) >> 56)))
139 /**************************************************************************//**
140 @Collection Byte-Swap Routines
142 Routines for swapping the byte order of a given parameter and
143 returning the swapped value.
145 These inline routines are safer than the byte-swap macros,
146 because they evaluate the parameter expression only once.
148 *//***************************************************************************/
150 /**************************************************************************//**
153 @Description Returns the byte-swapped value of a given 16-bit value.
155 @Param[in] val - The 16-bit value.
157 @Return The byte-swapped value of the parameter.
158 *//***************************************************************************/
159 static __inline__ uint16_t SwapUint16(uint16_t val)
161 return (uint16_t)(((val & 0x00FF) << 8) |
162 ((val & 0xFF00) >> 8));
165 /**************************************************************************//**
168 @Description Returns the byte-swapped value of a given 32-bit value.
170 @Param[in] val - The 32-bit value.
172 @Return The byte-swapped value of the parameter.
173 *//***************************************************************************/
174 static __inline__ uint32_t SwapUint32(uint32_t val)
176 return (uint32_t)(((val & 0x000000FF) << 24) |
177 ((val & 0x0000FF00) << 8) |
178 ((val & 0x00FF0000) >> 8) |
179 ((val & 0xFF000000) >> 24));
182 /**************************************************************************//**
185 @Description Returns the byte-swapped value of a given 64-bit value.
187 @Param[in] val - The 64-bit value.
189 @Return The byte-swapped value of the parameter.
190 *//***************************************************************************/
191 static __inline__ uint64_t SwapUint64(uint64_t val)
193 return (uint64_t)(((val & 0x00000000000000FFULL) << 56) |
194 ((val & 0x000000000000FF00ULL) << 40) |
195 ((val & 0x0000000000FF0000ULL) << 24) |
196 ((val & 0x00000000FF000000ULL) << 8) |
197 ((val & 0x000000FF00000000ULL) >> 8) |
198 ((val & 0x0000FF0000000000ULL) >> 24) |
199 ((val & 0x00FF000000000000ULL) >> 40) |
200 ((val & 0xFF00000000000000ULL) >> 56));
205 /**************************************************************************//**
206 @Collection In-place Byte-Swap-And-Set Routines
208 Routines for swapping the byte order of a given variable and
209 setting the swapped value back to the same variable.
211 *//***************************************************************************/
213 /**************************************************************************//**
214 @Function SwapUint16P
216 @Description Swaps the byte order of a given 16-bit variable.
218 @Param[in] p_Val - Pointer to the 16-bit variable.
221 *//***************************************************************************/
222 static __inline__ void SwapUint16P(uint16_t *p_Val)
224 *p_Val = SwapUint16(*p_Val);
227 /**************************************************************************//**
228 @Function SwapUint32P
230 @Description Swaps the byte order of a given 32-bit variable.
232 @Param[in] p_Val - Pointer to the 32-bit variable.
235 *//***************************************************************************/
236 static __inline__ void SwapUint32P(uint32_t *p_Val)
238 *p_Val = SwapUint32(*p_Val);
241 /**************************************************************************//**
242 @Function SwapUint64P
244 @Description Swaps the byte order of a given 64-bit variable.
246 @Param[in] p_Val - Pointer to the 64-bit variable.
249 *//***************************************************************************/
250 static __inline__ void SwapUint64P(uint64_t *p_Val)
252 *p_Val = SwapUint64(*p_Val);
258 /**************************************************************************//**
259 @Collection Little-Endian Conversion Macros
261 These macros convert given parameters to or from Little-Endian
262 format. Use these macros when you want to read or write a specific
263 Little-Endian value in memory, without a-priori knowing the CPU
266 These macros use the byte-swap routines. For conversion of
267 constants in initialization structures, you may use the CONST
268 versions of these macros (see below), which are using the
269 byte-swap macros instead.
271 *//***************************************************************************/
273 /**************************************************************************//**
274 @Description Converts a given 16-bit value from CPU byte order to
275 Little-Endian byte order.
277 @Param[in] val - The 16-bit value to convert.
279 @Return The converted value.
282 *//***************************************************************************/
283 #define CPU_TO_LE16(val) SwapUint16(val)
285 /**************************************************************************//**
286 @Description Converts a given 32-bit value from CPU byte order to
287 Little-Endian byte order.
289 @Param[in] val - The 32-bit value to convert.
291 @Return The converted value.
294 *//***************************************************************************/
295 #define CPU_TO_LE32(val) SwapUint32(val)
297 /**************************************************************************//**
298 @Description Converts a given 64-bit value from CPU byte order to
299 Little-Endian byte order.
301 @Param[in] val - The 64-bit value to convert.
303 @Return The converted value.
306 *//***************************************************************************/
307 #define CPU_TO_LE64(val) SwapUint64(val)
310 /**************************************************************************//**
311 @Description Converts a given 16-bit value from Little-Endian byte order to
314 @Param[in] val - The 16-bit value to convert.
316 @Return The converted value.
319 *//***************************************************************************/
320 #define LE16_TO_CPU(val) CPU_TO_LE16(val)
322 /**************************************************************************//**
323 @Description Converts a given 32-bit value from Little-Endian byte order to
326 @Param[in] val - The 32-bit value to convert.
328 @Return The converted value.
331 *//***************************************************************************/
332 #define LE32_TO_CPU(val) CPU_TO_LE32(val)
334 /**************************************************************************//**
335 @Description Converts a given 64-bit value from Little-Endian byte order to
338 @Param[in] val - The 64-bit value to convert.
340 @Return The converted value.
343 *//***************************************************************************/
344 #define LE64_TO_CPU(val) CPU_TO_LE64(val)
348 /**************************************************************************//**
349 @Collection Little-Endian Constant Conversion Macros
351 These macros convert given constants to or from Little-Endian
352 format. Use these macros when you want to read or write a specific
353 Little-Endian constant in memory, without a-priori knowing the
356 These macros use the byte-swap macros, therefore can be used for
357 conversion of constants in initialization structures.
359 @Cautions The parameters of these macros are evaluated multiple times.
360 For non-constant expressions, use the non-CONST macro versions.
363 *//***************************************************************************/
365 /**************************************************************************//**
366 @Description Converts a given 16-bit constant from CPU byte order to
367 Little-Endian byte order.
369 @Param[in] val - The 16-bit value to convert.
371 @Return The converted value.
374 *//***************************************************************************/
375 #define CONST_CPU_TO_LE16(val) SWAP_UINT16(val)
377 /**************************************************************************//**
378 @Description Converts a given 32-bit constant from CPU byte order to
379 Little-Endian byte order.
381 @Param[in] val - The 32-bit value to convert.
383 @Return The converted value.
386 *//***************************************************************************/
387 #define CONST_CPU_TO_LE32(val) SWAP_UINT32(val)
389 /**************************************************************************//**
390 @Description Converts a given 64-bit constant from CPU byte order to
391 Little-Endian byte order.
393 @Param[in] val - The 64-bit value to convert.
395 @Return The converted value.
398 *//***************************************************************************/
399 #define CONST_CPU_TO_LE64(val) SWAP_UINT64(val)
402 /**************************************************************************//**
403 @Description Converts a given 16-bit constant from Little-Endian byte order
406 @Param[in] val - The 16-bit value to convert.
408 @Return The converted value.
411 *//***************************************************************************/
412 #define CONST_LE16_TO_CPU(val) CONST_CPU_TO_LE16(val)
414 /**************************************************************************//**
415 @Description Converts a given 32-bit constant from Little-Endian byte order
418 @Param[in] val - The 32-bit value to convert.
420 @Return The converted value.
423 *//***************************************************************************/
424 #define CONST_LE32_TO_CPU(val) CONST_CPU_TO_LE32(val)
426 /**************************************************************************//**
427 @Description Converts a given 64-bit constant from Little-Endian byte order
430 @Param[in] val - The 64-bit value to convert.
432 @Return The converted value.
435 *//***************************************************************************/
436 #define CONST_LE64_TO_CPU(val) CONST_CPU_TO_LE64(val)
441 /** @} */ /* end of endian_id group */
442 /** @} */ /* end of gen_id group */
445 #endif /* __ENDIAN_EXT_H */