/*1* Copyright 2008-2012 Freescale Semiconductor Inc.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions are met:5* * Redistributions of source code must retain the above copyright6* notice, this list of conditions and the following disclaimer.7* * Redistributions in binary form must reproduce the above copyright8* notice, this list of conditions and the following disclaimer in the9* documentation and/or other materials provided with the distribution.10* * Neither the name of Freescale Semiconductor nor the11* names of its contributors may be used to endorse or promote products12* derived from this software without specific prior written permission.13*14*15* ALTERNATIVELY, this software may be distributed under the terms of the16* GNU General Public License ("GPL") as published by the Free Software17* Foundation, either version 2 of that License or (at your option) any18* later version.19*20* THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY21* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED22* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE23* DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY24* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES25* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;26* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND27* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT28* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS29* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.30*/313233/**************************************************************************//**3435@File endian_ext.h3637@Description Big/little endian swapping routines.38*//***************************************************************************/3940#ifndef __ENDIAN_EXT_H41#define __ENDIAN_EXT_H4243#include "std_ext.h"444546/**************************************************************************//**47@Group gen_id General Drivers Utilities4849@Description General usage API. This API is intended for usage by both the50internal modules and the user's application.5152@{53*//***************************************************************************/5455/**************************************************************************//**56@Group endian_id Big/Little-Endian Conversion5758@Description Routines and macros for Big/Little-Endian conversion and59general byte swapping.6061All routines and macros are expecting unsigned values as62parameters, but will generate the correct result also for63signed values. Therefore, signed/unsigned casting is allowed.64@{65*//***************************************************************************/6667/**************************************************************************//**68@Collection Byte-Swap Macros6970Macros for swapping byte order.7172@Cautions The parameters of these macros are evaluated multiple times.73For calculated expressions or expressions that contain function74calls it is recommended to use the byte-swap routines.7576@{77*//***************************************************************************/7879/**************************************************************************//**80@Description Swaps the byte order of a given 16-bit value.8182@Param[in] val - The 16-bit value to swap.8384@Return The byte-swapped value..8586@Cautions The given value is evaluated multiple times by this macro.87For calculated expressions or expressions that contain function88calls it is recommended to use the SwapUint16() routine.8990@hideinitializer91*//***************************************************************************/92#define SWAP_UINT16(val) \93((uint16_t)((((val) & 0x00FF) << 8) | (((val) & 0xFF00) >> 8)))9495/**************************************************************************//**96@Description Swaps the byte order of a given 32-bit value.9798@Param[in] val - The 32-bit value to swap.99100@Return The byte-swapped value..101102@Cautions The given value is evaluated multiple times by this macro.103For calculated expressions or expressions that contain function104calls it is recommended to use the SwapUint32() routine.105106@hideinitializer107*//***************************************************************************/108#define SWAP_UINT32(val) \109((uint32_t)((((val) & 0x000000FF) << 24) | \110(((val) & 0x0000FF00) << 8) | \111(((val) & 0x00FF0000) >> 8) | \112(((val) & 0xFF000000) >> 24)))113114/**************************************************************************//**115@Description Swaps the byte order of a given 64-bit value.116117@Param[in] val - The 64-bit value to swap.118119@Return The byte-swapped value..120121@Cautions The given value is evaluated multiple times by this macro.122For calculated expressions or expressions that contain function123calls it is recommended to use the SwapUint64() routine.124125@hideinitializer126*//***************************************************************************/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)))136137/* @} */138139/**************************************************************************//**140@Collection Byte-Swap Routines141142Routines for swapping the byte order of a given parameter and143returning the swapped value.144145These inline routines are safer than the byte-swap macros,146because they evaluate the parameter expression only once.147@{148*//***************************************************************************/149150/**************************************************************************//**151@Function SwapUint16152153@Description Returns the byte-swapped value of a given 16-bit value.154155@Param[in] val - The 16-bit value.156157@Return The byte-swapped value of the parameter.158*//***************************************************************************/159static __inline__ uint16_t SwapUint16(uint16_t val)160{161return (uint16_t)(((val & 0x00FF) << 8) |162((val & 0xFF00) >> 8));163}164165/**************************************************************************//**166@Function SwapUint32167168@Description Returns the byte-swapped value of a given 32-bit value.169170@Param[in] val - The 32-bit value.171172@Return The byte-swapped value of the parameter.173*//***************************************************************************/174static __inline__ uint32_t SwapUint32(uint32_t val)175{176return (uint32_t)(((val & 0x000000FF) << 24) |177((val & 0x0000FF00) << 8) |178((val & 0x00FF0000) >> 8) |179((val & 0xFF000000) >> 24));180}181182/**************************************************************************//**183@Function SwapUint64184185@Description Returns the byte-swapped value of a given 64-bit value.186187@Param[in] val - The 64-bit value.188189@Return The byte-swapped value of the parameter.190*//***************************************************************************/191static __inline__ uint64_t SwapUint64(uint64_t val)192{193return (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));201}202203/* @} */204205/**************************************************************************//**206@Collection In-place Byte-Swap-And-Set Routines207208Routines for swapping the byte order of a given variable and209setting the swapped value back to the same variable.210@{211*//***************************************************************************/212213/**************************************************************************//**214@Function SwapUint16P215216@Description Swaps the byte order of a given 16-bit variable.217218@Param[in] p_Val - Pointer to the 16-bit variable.219220@Return None.221*//***************************************************************************/222static __inline__ void SwapUint16P(uint16_t *p_Val)223{224*p_Val = SwapUint16(*p_Val);225}226227/**************************************************************************//**228@Function SwapUint32P229230@Description Swaps the byte order of a given 32-bit variable.231232@Param[in] p_Val - Pointer to the 32-bit variable.233234@Return None.235*//***************************************************************************/236static __inline__ void SwapUint32P(uint32_t *p_Val)237{238*p_Val = SwapUint32(*p_Val);239}240241/**************************************************************************//**242@Function SwapUint64P243244@Description Swaps the byte order of a given 64-bit variable.245246@Param[in] p_Val - Pointer to the 64-bit variable.247248@Return None.249*//***************************************************************************/250static __inline__ void SwapUint64P(uint64_t *p_Val)251{252*p_Val = SwapUint64(*p_Val);253}254255/* @} */256257258/**************************************************************************//**259@Collection Little-Endian Conversion Macros260261These macros convert given parameters to or from Little-Endian262format. Use these macros when you want to read or write a specific263Little-Endian value in memory, without a-priori knowing the CPU264byte order.265266These macros use the byte-swap routines. For conversion of267constants in initialization structures, you may use the CONST268versions of these macros (see below), which are using the269byte-swap macros instead.270@{271*//***************************************************************************/272273/**************************************************************************//**274@Description Converts a given 16-bit value from CPU byte order to275Little-Endian byte order.276277@Param[in] val - The 16-bit value to convert.278279@Return The converted value.280281@hideinitializer282*//***************************************************************************/283#define CPU_TO_LE16(val) SwapUint16(val)284285/**************************************************************************//**286@Description Converts a given 32-bit value from CPU byte order to287Little-Endian byte order.288289@Param[in] val - The 32-bit value to convert.290291@Return The converted value.292293@hideinitializer294*//***************************************************************************/295#define CPU_TO_LE32(val) SwapUint32(val)296297/**************************************************************************//**298@Description Converts a given 64-bit value from CPU byte order to299Little-Endian byte order.300301@Param[in] val - The 64-bit value to convert.302303@Return The converted value.304305@hideinitializer306*//***************************************************************************/307#define CPU_TO_LE64(val) SwapUint64(val)308309310/**************************************************************************//**311@Description Converts a given 16-bit value from Little-Endian byte order to312CPU byte order.313314@Param[in] val - The 16-bit value to convert.315316@Return The converted value.317318@hideinitializer319*//***************************************************************************/320#define LE16_TO_CPU(val) CPU_TO_LE16(val)321322/**************************************************************************//**323@Description Converts a given 32-bit value from Little-Endian byte order to324CPU byte order.325326@Param[in] val - The 32-bit value to convert.327328@Return The converted value.329330@hideinitializer331*//***************************************************************************/332#define LE32_TO_CPU(val) CPU_TO_LE32(val)333334/**************************************************************************//**335@Description Converts a given 64-bit value from Little-Endian byte order to336CPU byte order.337338@Param[in] val - The 64-bit value to convert.339340@Return The converted value.341342@hideinitializer343*//***************************************************************************/344#define LE64_TO_CPU(val) CPU_TO_LE64(val)345346/* @} */347348/**************************************************************************//**349@Collection Little-Endian Constant Conversion Macros350351These macros convert given constants to or from Little-Endian352format. Use these macros when you want to read or write a specific353Little-Endian constant in memory, without a-priori knowing the354CPU byte order.355356These macros use the byte-swap macros, therefore can be used for357conversion of constants in initialization structures.358359@Cautions The parameters of these macros are evaluated multiple times.360For non-constant expressions, use the non-CONST macro versions.361362@{363*//***************************************************************************/364365/**************************************************************************//**366@Description Converts a given 16-bit constant from CPU byte order to367Little-Endian byte order.368369@Param[in] val - The 16-bit value to convert.370371@Return The converted value.372373@hideinitializer374*//***************************************************************************/375#define CONST_CPU_TO_LE16(val) SWAP_UINT16(val)376377/**************************************************************************//**378@Description Converts a given 32-bit constant from CPU byte order to379Little-Endian byte order.380381@Param[in] val - The 32-bit value to convert.382383@Return The converted value.384385@hideinitializer386*//***************************************************************************/387#define CONST_CPU_TO_LE32(val) SWAP_UINT32(val)388389/**************************************************************************//**390@Description Converts a given 64-bit constant from CPU byte order to391Little-Endian byte order.392393@Param[in] val - The 64-bit value to convert.394395@Return The converted value.396397@hideinitializer398*//***************************************************************************/399#define CONST_CPU_TO_LE64(val) SWAP_UINT64(val)400401402/**************************************************************************//**403@Description Converts a given 16-bit constant from Little-Endian byte order404to CPU byte order.405406@Param[in] val - The 16-bit value to convert.407408@Return The converted value.409410@hideinitializer411*//***************************************************************************/412#define CONST_LE16_TO_CPU(val) CONST_CPU_TO_LE16(val)413414/**************************************************************************//**415@Description Converts a given 32-bit constant from Little-Endian byte order416to CPU byte order.417418@Param[in] val - The 32-bit value to convert.419420@Return The converted value.421422@hideinitializer423*//***************************************************************************/424#define CONST_LE32_TO_CPU(val) CONST_CPU_TO_LE32(val)425426/**************************************************************************//**427@Description Converts a given 64-bit constant from Little-Endian byte order428to CPU byte order.429430@Param[in] val - The 64-bit value to convert.431432@Return The converted value.433434@hideinitializer435*//***************************************************************************/436#define CONST_LE64_TO_CPU(val) CONST_CPU_TO_LE64(val)437438/* @} */439440441/** @} */ /* end of endian_id group */442/** @} */ /* end of gen_id group */443444445#endif /* __ENDIAN_EXT_H */446447448449