1296177Sjhibbits/* Copyright (c) 2008-2011 Freescale Semiconductor, Inc. 2296177Sjhibbits * All rights reserved. 3296177Sjhibbits * 4296177Sjhibbits * Redistribution and use in source and binary forms, with or without 5296177Sjhibbits * modification, are permitted provided that the following conditions are met: 6296177Sjhibbits * * Redistributions of source code must retain the above copyright 7296177Sjhibbits * notice, this list of conditions and the following disclaimer. 8296177Sjhibbits * * Redistributions in binary form must reproduce the above copyright 9296177Sjhibbits * notice, this list of conditions and the following disclaimer in the 10296177Sjhibbits * documentation and/or other materials provided with the distribution. 11296177Sjhibbits * * Neither the name of Freescale Semiconductor nor the 12296177Sjhibbits * names of its contributors may be used to endorse or promote products 13296177Sjhibbits * derived from this software without specific prior written permission. 14296177Sjhibbits * 15296177Sjhibbits * 16296177Sjhibbits * ALTERNATIVELY, this software may be distributed under the terms of the 17296177Sjhibbits * GNU General Public License ("GPL") as published by the Free Software 18296177Sjhibbits * Foundation, either version 2 of that License or (at your option) any 19296177Sjhibbits * later version. 20296177Sjhibbits * 21296177Sjhibbits * THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY 22296177Sjhibbits * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 23296177Sjhibbits * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 24296177Sjhibbits * DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY 25296177Sjhibbits * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 26296177Sjhibbits * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 27296177Sjhibbits * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 28296177Sjhibbits * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29296177Sjhibbits * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30296177Sjhibbits * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31296177Sjhibbits */ 32296177Sjhibbits 33296177Sjhibbits/**************************************************************************//** 34296177Sjhibbits 35296177Sjhibbits @File endian_ext.h 36296177Sjhibbits 37296177Sjhibbits @Description Big/little endian swapping routines. 38296177Sjhibbits*//***************************************************************************/ 39296177Sjhibbits 40296177Sjhibbits#ifndef __ENDIAN_EXT_H 41296177Sjhibbits#define __ENDIAN_EXT_H 42296177Sjhibbits 43296177Sjhibbits#include "std_ext.h" 44296177Sjhibbits 45296177Sjhibbits 46296177Sjhibbits/**************************************************************************//** 47296177Sjhibbits @Group gen_id General Drivers Utilities 48296177Sjhibbits 49296177Sjhibbits @Description General usage API. This API is intended for usage by both the 50296177Sjhibbits internal modules and the user's application. 51296177Sjhibbits 52296177Sjhibbits @{ 53296177Sjhibbits*//***************************************************************************/ 54296177Sjhibbits 55296177Sjhibbits/**************************************************************************//** 56296177Sjhibbits @Group endian_id Big/Little-Endian Conversion 57296177Sjhibbits 58296177Sjhibbits @Description Routines and macros for Big/Little-Endian conversion and 59296177Sjhibbits general byte swapping. 60296177Sjhibbits 61296177Sjhibbits All routines and macros are expecting unsigned values as 62296177Sjhibbits parameters, but will generate the correct result also for 63296177Sjhibbits signed values. Therefore, signed/unsigned casting is allowed. 64296177Sjhibbits @{ 65296177Sjhibbits*//***************************************************************************/ 66296177Sjhibbits 67296177Sjhibbits/**************************************************************************//** 68296177Sjhibbits @Collection Byte-Swap Macros 69296177Sjhibbits 70296177Sjhibbits Macros for swapping byte order. 71296177Sjhibbits 72296177Sjhibbits @Cautions The parameters of these macros are evaluated multiple times. 73296177Sjhibbits For calculated expressions or expressions that contain function 74296177Sjhibbits calls it is recommended to use the byte-swap routines. 75296177Sjhibbits 76296177Sjhibbits @{ 77296177Sjhibbits*//***************************************************************************/ 78296177Sjhibbits 79296177Sjhibbits/**************************************************************************//** 80296177Sjhibbits @Description Swaps the byte order of a given 16-bit value. 81296177Sjhibbits 82296177Sjhibbits @Param[in] val - The 16-bit value to swap. 83296177Sjhibbits 84296177Sjhibbits @Return The byte-swapped value.. 85296177Sjhibbits 86296177Sjhibbits @Cautions The given value is evaluated multiple times by this macro. 87296177Sjhibbits For calculated expressions or expressions that contain function 88296177Sjhibbits calls it is recommended to use the SwapUint16() routine. 89296177Sjhibbits 90296177Sjhibbits @hideinitializer 91296177Sjhibbits*//***************************************************************************/ 92296177Sjhibbits#define SWAP_UINT16(val) \ 93296177Sjhibbits ((uint16_t)((((val) & 0x00FF) << 8) | (((val) & 0xFF00) >> 8))) 94296177Sjhibbits 95296177Sjhibbits/**************************************************************************//** 96296177Sjhibbits @Description Swaps the byte order of a given 32-bit value. 97296177Sjhibbits 98296177Sjhibbits @Param[in] val - The 32-bit value to swap. 99296177Sjhibbits 100296177Sjhibbits @Return The byte-swapped value.. 101296177Sjhibbits 102296177Sjhibbits @Cautions The given value is evaluated multiple times by this macro. 103296177Sjhibbits For calculated expressions or expressions that contain function 104296177Sjhibbits calls it is recommended to use the SwapUint32() routine. 105296177Sjhibbits 106296177Sjhibbits @hideinitializer 107296177Sjhibbits*//***************************************************************************/ 108296177Sjhibbits#define SWAP_UINT32(val) \ 109296177Sjhibbits ((uint32_t)((((val) & 0x000000FF) << 24) | \ 110296177Sjhibbits (((val) & 0x0000FF00) << 8) | \ 111296177Sjhibbits (((val) & 0x00FF0000) >> 8) | \ 112296177Sjhibbits (((val) & 0xFF000000) >> 24))) 113296177Sjhibbits 114296177Sjhibbits/**************************************************************************//** 115296177Sjhibbits @Description Swaps the byte order of a given 64-bit value. 116296177Sjhibbits 117296177Sjhibbits @Param[in] val - The 64-bit value to swap. 118296177Sjhibbits 119296177Sjhibbits @Return The byte-swapped value.. 120296177Sjhibbits 121296177Sjhibbits @Cautions The given value is evaluated multiple times by this macro. 122296177Sjhibbits For calculated expressions or expressions that contain function 123296177Sjhibbits calls it is recommended to use the SwapUint64() routine. 124296177Sjhibbits 125296177Sjhibbits @hideinitializer 126296177Sjhibbits*//***************************************************************************/ 127296177Sjhibbits#define SWAP_UINT64(val) \ 128296177Sjhibbits ((uint64_t)((((val) & 0x00000000000000FFULL) << 56) | \ 129296177Sjhibbits (((val) & 0x000000000000FF00ULL) << 40) | \ 130296177Sjhibbits (((val) & 0x0000000000FF0000ULL) << 24) | \ 131296177Sjhibbits (((val) & 0x00000000FF000000ULL) << 8) | \ 132296177Sjhibbits (((val) & 0x000000FF00000000ULL) >> 8) | \ 133296177Sjhibbits (((val) & 0x0000FF0000000000ULL) >> 24) | \ 134296177Sjhibbits (((val) & 0x00FF000000000000ULL) >> 40) | \ 135296177Sjhibbits (((val) & 0xFF00000000000000ULL) >> 56))) 136296177Sjhibbits 137296177Sjhibbits/* @} */ 138296177Sjhibbits 139296177Sjhibbits/**************************************************************************//** 140296177Sjhibbits @Collection Byte-Swap Routines 141296177Sjhibbits 142296177Sjhibbits Routines for swapping the byte order of a given parameter and 143296177Sjhibbits returning the swapped value. 144296177Sjhibbits 145296177Sjhibbits These inline routines are safer than the byte-swap macros, 146296177Sjhibbits because they evaluate the parameter expression only once. 147296177Sjhibbits @{ 148296177Sjhibbits*//***************************************************************************/ 149296177Sjhibbits 150296177Sjhibbits/**************************************************************************//** 151296177Sjhibbits @Function SwapUint16 152296177Sjhibbits 153296177Sjhibbits @Description Returns the byte-swapped value of a given 16-bit value. 154296177Sjhibbits 155296177Sjhibbits @Param[in] val - The 16-bit value. 156296177Sjhibbits 157296177Sjhibbits @Return The byte-swapped value of the parameter. 158296177Sjhibbits*//***************************************************************************/ 159296177Sjhibbitsstatic __inline__ uint16_t SwapUint16(uint16_t val) 160296177Sjhibbits{ 161296177Sjhibbits return (uint16_t)(((val & 0x00FF) << 8) | 162296177Sjhibbits ((val & 0xFF00) >> 8)); 163296177Sjhibbits} 164296177Sjhibbits 165296177Sjhibbits/**************************************************************************//** 166296177Sjhibbits @Function SwapUint32 167296177Sjhibbits 168296177Sjhibbits @Description Returns the byte-swapped value of a given 32-bit value. 169296177Sjhibbits 170296177Sjhibbits @Param[in] val - The 32-bit value. 171296177Sjhibbits 172296177Sjhibbits @Return The byte-swapped value of the parameter. 173296177Sjhibbits*//***************************************************************************/ 174296177Sjhibbitsstatic __inline__ uint32_t SwapUint32(uint32_t val) 175296177Sjhibbits{ 176296177Sjhibbits return (uint32_t)(((val & 0x000000FF) << 24) | 177296177Sjhibbits ((val & 0x0000FF00) << 8) | 178296177Sjhibbits ((val & 0x00FF0000) >> 8) | 179296177Sjhibbits ((val & 0xFF000000) >> 24)); 180296177Sjhibbits} 181296177Sjhibbits 182296177Sjhibbits/**************************************************************************//** 183296177Sjhibbits @Function SwapUint64 184296177Sjhibbits 185296177Sjhibbits @Description Returns the byte-swapped value of a given 64-bit value. 186296177Sjhibbits 187296177Sjhibbits @Param[in] val - The 64-bit value. 188296177Sjhibbits 189296177Sjhibbits @Return The byte-swapped value of the parameter. 190296177Sjhibbits*//***************************************************************************/ 191296177Sjhibbitsstatic __inline__ uint64_t SwapUint64(uint64_t val) 192296177Sjhibbits{ 193296177Sjhibbits return (uint64_t)(((val & 0x00000000000000FFULL) << 56) | 194296177Sjhibbits ((val & 0x000000000000FF00ULL) << 40) | 195296177Sjhibbits ((val & 0x0000000000FF0000ULL) << 24) | 196296177Sjhibbits ((val & 0x00000000FF000000ULL) << 8) | 197296177Sjhibbits ((val & 0x000000FF00000000ULL) >> 8) | 198296177Sjhibbits ((val & 0x0000FF0000000000ULL) >> 24) | 199296177Sjhibbits ((val & 0x00FF000000000000ULL) >> 40) | 200296177Sjhibbits ((val & 0xFF00000000000000ULL) >> 56)); 201296177Sjhibbits} 202296177Sjhibbits 203296177Sjhibbits/* @} */ 204296177Sjhibbits 205296177Sjhibbits/**************************************************************************//** 206296177Sjhibbits @Collection In-place Byte-Swap-And-Set Routines 207296177Sjhibbits 208296177Sjhibbits Routines for swapping the byte order of a given variable and 209296177Sjhibbits setting the swapped value back to the same variable. 210296177Sjhibbits @{ 211296177Sjhibbits*//***************************************************************************/ 212296177Sjhibbits 213296177Sjhibbits/**************************************************************************//** 214296177Sjhibbits @Function SwapUint16P 215296177Sjhibbits 216296177Sjhibbits @Description Swaps the byte order of a given 16-bit variable. 217296177Sjhibbits 218296177Sjhibbits @Param[in] p_Val - Pointer to the 16-bit variable. 219296177Sjhibbits 220296177Sjhibbits @Return None. 221296177Sjhibbits*//***************************************************************************/ 222296177Sjhibbitsstatic __inline__ void SwapUint16P(uint16_t *p_Val) 223296177Sjhibbits{ 224296177Sjhibbits *p_Val = SwapUint16(*p_Val); 225296177Sjhibbits} 226296177Sjhibbits 227296177Sjhibbits/**************************************************************************//** 228296177Sjhibbits @Function SwapUint32P 229296177Sjhibbits 230296177Sjhibbits @Description Swaps the byte order of a given 32-bit variable. 231296177Sjhibbits 232296177Sjhibbits @Param[in] p_Val - Pointer to the 32-bit variable. 233296177Sjhibbits 234296177Sjhibbits @Return None. 235296177Sjhibbits*//***************************************************************************/ 236296177Sjhibbitsstatic __inline__ void SwapUint32P(uint32_t *p_Val) 237296177Sjhibbits{ 238296177Sjhibbits *p_Val = SwapUint32(*p_Val); 239296177Sjhibbits} 240296177Sjhibbits 241296177Sjhibbits/**************************************************************************//** 242296177Sjhibbits @Function SwapUint64P 243296177Sjhibbits 244296177Sjhibbits @Description Swaps the byte order of a given 64-bit variable. 245296177Sjhibbits 246296177Sjhibbits @Param[in] p_Val - Pointer to the 64-bit variable. 247296177Sjhibbits 248296177Sjhibbits @Return None. 249296177Sjhibbits*//***************************************************************************/ 250296177Sjhibbitsstatic __inline__ void SwapUint64P(uint64_t *p_Val) 251296177Sjhibbits{ 252296177Sjhibbits *p_Val = SwapUint64(*p_Val); 253296177Sjhibbits} 254296177Sjhibbits 255296177Sjhibbits/* @} */ 256296177Sjhibbits 257296177Sjhibbits 258296177Sjhibbits/**************************************************************************//** 259296177Sjhibbits @Collection Little-Endian Conversion Macros 260296177Sjhibbits 261296177Sjhibbits These macros convert given parameters to or from Little-Endian 262296177Sjhibbits format. Use these macros when you want to read or write a specific 263296177Sjhibbits Little-Endian value in memory, without a-priori knowing the CPU 264296177Sjhibbits byte order. 265296177Sjhibbits 266296177Sjhibbits These macros use the byte-swap routines. For conversion of 267296177Sjhibbits constants in initialization structures, you may use the CONST 268296177Sjhibbits versions of these macros (see below), which are using the 269296177Sjhibbits byte-swap macros instead. 270296177Sjhibbits @{ 271296177Sjhibbits*//***************************************************************************/ 272296177Sjhibbits 273296177Sjhibbits/**************************************************************************//** 274296177Sjhibbits @Description Converts a given 16-bit value from CPU byte order to 275296177Sjhibbits Little-Endian byte order. 276296177Sjhibbits 277296177Sjhibbits @Param[in] val - The 16-bit value to convert. 278296177Sjhibbits 279296177Sjhibbits @Return The converted value. 280296177Sjhibbits 281296177Sjhibbits @hideinitializer 282296177Sjhibbits*//***************************************************************************/ 283296177Sjhibbits#define CPU_TO_LE16(val) SwapUint16(val) 284296177Sjhibbits 285296177Sjhibbits/**************************************************************************//** 286296177Sjhibbits @Description Converts a given 32-bit value from CPU byte order to 287296177Sjhibbits Little-Endian byte order. 288296177Sjhibbits 289296177Sjhibbits @Param[in] val - The 32-bit value to convert. 290296177Sjhibbits 291296177Sjhibbits @Return The converted value. 292296177Sjhibbits 293296177Sjhibbits @hideinitializer 294296177Sjhibbits*//***************************************************************************/ 295296177Sjhibbits#define CPU_TO_LE32(val) SwapUint32(val) 296296177Sjhibbits 297296177Sjhibbits/**************************************************************************//** 298296177Sjhibbits @Description Converts a given 64-bit value from CPU byte order to 299296177Sjhibbits Little-Endian byte order. 300296177Sjhibbits 301296177Sjhibbits @Param[in] val - The 64-bit value to convert. 302296177Sjhibbits 303296177Sjhibbits @Return The converted value. 304296177Sjhibbits 305296177Sjhibbits @hideinitializer 306296177Sjhibbits*//***************************************************************************/ 307296177Sjhibbits#define CPU_TO_LE64(val) SwapUint64(val) 308296177Sjhibbits 309296177Sjhibbits 310296177Sjhibbits/**************************************************************************//** 311296177Sjhibbits @Description Converts a given 16-bit value from Little-Endian byte order to 312296177Sjhibbits CPU byte order. 313296177Sjhibbits 314296177Sjhibbits @Param[in] val - The 16-bit value to convert. 315296177Sjhibbits 316296177Sjhibbits @Return The converted value. 317296177Sjhibbits 318296177Sjhibbits @hideinitializer 319296177Sjhibbits*//***************************************************************************/ 320296177Sjhibbits#define LE16_TO_CPU(val) CPU_TO_LE16(val) 321296177Sjhibbits 322296177Sjhibbits/**************************************************************************//** 323296177Sjhibbits @Description Converts a given 32-bit value from Little-Endian byte order to 324296177Sjhibbits CPU byte order. 325296177Sjhibbits 326296177Sjhibbits @Param[in] val - The 32-bit value to convert. 327296177Sjhibbits 328296177Sjhibbits @Return The converted value. 329296177Sjhibbits 330296177Sjhibbits @hideinitializer 331296177Sjhibbits*//***************************************************************************/ 332296177Sjhibbits#define LE32_TO_CPU(val) CPU_TO_LE32(val) 333296177Sjhibbits 334296177Sjhibbits/**************************************************************************//** 335296177Sjhibbits @Description Converts a given 64-bit value from Little-Endian byte order to 336296177Sjhibbits CPU byte order. 337296177Sjhibbits 338296177Sjhibbits @Param[in] val - The 64-bit value to convert. 339296177Sjhibbits 340296177Sjhibbits @Return The converted value. 341296177Sjhibbits 342296177Sjhibbits @hideinitializer 343296177Sjhibbits*//***************************************************************************/ 344296177Sjhibbits#define LE64_TO_CPU(val) CPU_TO_LE64(val) 345296177Sjhibbits 346296177Sjhibbits/* @} */ 347296177Sjhibbits 348296177Sjhibbits/**************************************************************************//** 349296177Sjhibbits @Collection Little-Endian Constant Conversion Macros 350296177Sjhibbits 351296177Sjhibbits These macros convert given constants to or from Little-Endian 352296177Sjhibbits format. Use these macros when you want to read or write a specific 353296177Sjhibbits Little-Endian constant in memory, without a-priori knowing the 354296177Sjhibbits CPU byte order. 355296177Sjhibbits 356296177Sjhibbits These macros use the byte-swap macros, therefore can be used for 357296177Sjhibbits conversion of constants in initialization structures. 358296177Sjhibbits 359296177Sjhibbits @Cautions The parameters of these macros are evaluated multiple times. 360296177Sjhibbits For non-constant expressions, use the non-CONST macro versions. 361296177Sjhibbits 362296177Sjhibbits @{ 363296177Sjhibbits*//***************************************************************************/ 364296177Sjhibbits 365296177Sjhibbits/**************************************************************************//** 366296177Sjhibbits @Description Converts a given 16-bit constant from CPU byte order to 367296177Sjhibbits Little-Endian byte order. 368296177Sjhibbits 369296177Sjhibbits @Param[in] val - The 16-bit value to convert. 370296177Sjhibbits 371296177Sjhibbits @Return The converted value. 372296177Sjhibbits 373296177Sjhibbits @hideinitializer 374296177Sjhibbits*//***************************************************************************/ 375296177Sjhibbits#define CONST_CPU_TO_LE16(val) SWAP_UINT16(val) 376296177Sjhibbits 377296177Sjhibbits/**************************************************************************//** 378296177Sjhibbits @Description Converts a given 32-bit constant from CPU byte order to 379296177Sjhibbits Little-Endian byte order. 380296177Sjhibbits 381296177Sjhibbits @Param[in] val - The 32-bit value to convert. 382296177Sjhibbits 383296177Sjhibbits @Return The converted value. 384296177Sjhibbits 385296177Sjhibbits @hideinitializer 386296177Sjhibbits*//***************************************************************************/ 387296177Sjhibbits#define CONST_CPU_TO_LE32(val) SWAP_UINT32(val) 388296177Sjhibbits 389296177Sjhibbits/**************************************************************************//** 390296177Sjhibbits @Description Converts a given 64-bit constant from CPU byte order to 391296177Sjhibbits Little-Endian byte order. 392296177Sjhibbits 393296177Sjhibbits @Param[in] val - The 64-bit value to convert. 394296177Sjhibbits 395296177Sjhibbits @Return The converted value. 396296177Sjhibbits 397296177Sjhibbits @hideinitializer 398296177Sjhibbits*//***************************************************************************/ 399296177Sjhibbits#define CONST_CPU_TO_LE64(val) SWAP_UINT64(val) 400296177Sjhibbits 401296177Sjhibbits 402296177Sjhibbits/**************************************************************************//** 403296177Sjhibbits @Description Converts a given 16-bit constant from Little-Endian byte order 404296177Sjhibbits to CPU byte order. 405296177Sjhibbits 406296177Sjhibbits @Param[in] val - The 16-bit value to convert. 407296177Sjhibbits 408296177Sjhibbits @Return The converted value. 409296177Sjhibbits 410296177Sjhibbits @hideinitializer 411296177Sjhibbits*//***************************************************************************/ 412296177Sjhibbits#define CONST_LE16_TO_CPU(val) CONST_CPU_TO_LE16(val) 413296177Sjhibbits 414296177Sjhibbits/**************************************************************************//** 415296177Sjhibbits @Description Converts a given 32-bit constant from Little-Endian byte order 416296177Sjhibbits to CPU byte order. 417296177Sjhibbits 418296177Sjhibbits @Param[in] val - The 32-bit value to convert. 419296177Sjhibbits 420296177Sjhibbits @Return The converted value. 421296177Sjhibbits 422296177Sjhibbits @hideinitializer 423296177Sjhibbits*//***************************************************************************/ 424296177Sjhibbits#define CONST_LE32_TO_CPU(val) CONST_CPU_TO_LE32(val) 425296177Sjhibbits 426296177Sjhibbits/**************************************************************************//** 427296177Sjhibbits @Description Converts a given 64-bit constant from Little-Endian byte order 428296177Sjhibbits to CPU byte order. 429296177Sjhibbits 430296177Sjhibbits @Param[in] val - The 64-bit value to convert. 431296177Sjhibbits 432296177Sjhibbits @Return The converted value. 433296177Sjhibbits 434296177Sjhibbits @hideinitializer 435296177Sjhibbits*//***************************************************************************/ 436296177Sjhibbits#define CONST_LE64_TO_CPU(val) CONST_CPU_TO_LE64(val) 437296177Sjhibbits 438296177Sjhibbits/* @} */ 439296177Sjhibbits 440296177Sjhibbits 441296177Sjhibbits/** @} */ /* end of endian_id group */ 442296177Sjhibbits/** @} */ /* end of gen_id group */ 443296177Sjhibbits 444296177Sjhibbits 445296177Sjhibbits#endif /* __ENDIAN_EXT_H */ 446296177Sjhibbits 447