00001 00002 /* 00003 * Copyright (c) 2006-2010 by Roland Riegel <feedback@roland-riegel.de> 00004 * 00005 * This file is free software; you can redistribute it and/or modify 00006 * it under the terms of either the GNU General Public License version 2 00007 * or the GNU Lesser General Public License version 2.1, both as 00008 * published by the Free Software Foundation. 00009 */ 00010 00011 #ifndef BYTEORDERING_H 00012 #define BYTEORDERING_H 00013 00014 #include <stdint.h> 00015 00016 #ifdef __cplusplus 00017 extern "C" 00018 { 00019 #endif 00020 00021 /** 00022 * \addtogroup byteordering 00023 * 00024 * @{ 00025 */ 00026 /** 00027 * \file 00028 * Byte-order handling header (license: GPLv2 or LGPLv2.1) 00029 * 00030 * \author Roland Riegel 00031 */ 00032 00033 /** 00034 * \def HTOL16(val) 00035 * 00036 * Converts a 16-bit integer to little-endian byte order. 00037 * 00038 * Use this macro for compile time constants only. For variable values 00039 * use the function htol16() instead. This saves code size. 00040 * 00041 * \param[in] val A 16-bit integer in host byte order. 00042 * \returns The given 16-bit integer converted to little-endian byte order. 00043 */ 00044 /** 00045 * \def HTOL32(val) 00046 * 00047 * Converts a 32-bit integer to little-endian byte order. 00048 * 00049 * Use this macro for compile time constants only. For variable values 00050 * use the function htol32() instead. This saves code size. 00051 * 00052 * \param[in] val A 32-bit integer in host byte order. 00053 * \returns The given 32-bit integer converted to little-endian byte order. 00054 */ 00055 00056 #if DOXYGEN || LITTLE_ENDIAN || __AVR__ 00057 #define HTOL16(val) (val) 00058 #define HTOL32(val) (val) 00059 #elif BIG_ENDIAN 00060 #define HTOL16(val) ((((uint16_t) (val)) << 8) | \ 00061 (((uint16_t) (val)) >> 8) \ 00062 ) 00063 #define HTOL32(val) (((((uint32_t) (val)) & 0x000000ff) << 24) | \ 00064 ((((uint32_t) (val)) & 0x0000ff00) << 8) | \ 00065 ((((uint32_t) (val)) & 0x00ff0000) >> 8) | \ 00066 ((((uint32_t) (val)) & 0xff000000) >> 24) \ 00067 ) 00068 #else 00069 #error "Endianess undefined! Please define LITTLE_ENDIAN=1 or BIG_ENDIAN=1." 00070 #endif 00071 00072 uint16_t htol16(uint16_t h); 00073 uint32_t htol32(uint32_t h); 00074 00075 /** 00076 * Converts a 16-bit integer to host byte order. 00077 * 00078 * Use this macro for compile time constants only. For variable values 00079 * use the function ltoh16() instead. This saves code size. 00080 * 00081 * \param[in] val A 16-bit integer in little-endian byte order. 00082 * \returns The given 16-bit integer converted to host byte order. 00083 */ 00084 #define LTOH16(val) HTOL16(val) 00085 00086 /** 00087 * Converts a 32-bit integer to host byte order. 00088 * 00089 * Use this macro for compile time constants only. For variable values 00090 * use the function ltoh32() instead. This saves code size. 00091 * 00092 * \param[in] val A 32-bit integer in little-endian byte order. 00093 * \returns The given 32-bit integer converted to host byte order. 00094 */ 00095 #define LTOH32(val) HTOL32(val) 00096 00097 /** 00098 * Converts a 16-bit integer to host byte order. 00099 * 00100 * Use this function on variable values instead of the 00101 * macro LTOH16(). This saves code size. 00102 * 00103 * \param[in] l A 16-bit integer in little-endian byte order. 00104 * \returns The given 16-bit integer converted to host byte order. 00105 */ 00106 #if DOXYGEN 00107 uint16_t ltoh16(uint16_t l); 00108 #else 00109 #define ltoh16(l) htol16(l) 00110 #endif 00111 00112 /** 00113 * Converts a 32-bit integer to host byte order. 00114 * 00115 * Use this function on variable values instead of the 00116 * macro LTOH32(). This saves code size. 00117 * 00118 * \param[in] l A 32-bit integer in little-endian byte order. 00119 * \returns The given 32-bit integer converted to host byte order. 00120 */ 00121 #if DOXYGEN 00122 uint32_t ltoh32(uint32_t l); 00123 #else 00124 #define ltoh32(l) htol32(l) 00125 #endif 00126 00127 /** 00128 * @} 00129 */ 00130 00131 #if LITTLE_ENDIAN || __AVR__ 00132 #define htol16(h) (h) 00133 #define htol32(h) (h) 00134 #else 00135 uint16_t htol16(uint16_t h); 00136 uint32_t htol32(uint32_t h); 00137 #endif 00138 00139 #ifdef __cplusplus 00140 } 00141 #endif 00142 00143 #endif 00144
1.5.6