leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pal_macros.h Source File

pal_macros.h

Go to the documentation of this file.
00001 // ----------------------------------------------------------------------------
00002 // Copyright 2016-2019 ARM Ltd.
00003 //
00004 // SPDX-License-Identifier: Apache-2.0
00005 //
00006 // Licensed under the Apache License, Version 2.0 (the "License");
00007 // you may not use this file except in compliance with the License.
00008 // You may obtain a copy of the License at
00009 //
00010 //     http://www.apache.org/licenses/LICENSE-2.0
00011 //
00012 // Unless required by applicable law or agreed to in writing, software
00013 // distributed under the License is distributed on an "AS IS" BASIS,
00014 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 // See the License for the specific language governing permissions and
00016 // limitations under the License.
00017 // ----------------------------------------------------------------------------
00018 
00019 #ifndef _PAL_MACROS_H
00020 #define _PAL_MACROS_H
00021 
00022 #ifdef __cplusplus
00023 extern "C" {
00024 #endif
00025 
00026 //for PAL_LOG prints
00027 #include "pal.h"
00028 #include "mbed-trace/mbed_trace.h"
00029 #include "assert.h"
00030 #include <limits.h> 
00031 /*! \file pal_macros.h
00032 *  \brief PAL macros.
00033 *   This file contains macros defined by PAL for constant values and network purposes.
00034 */
00035 
00036 // Maximum integer types.
00037 #define PAL_MAX_UINT8       0xFFU
00038 #define PAL_MAX_UINT16      0xFFFFU
00039 #define PAL_MAX_UINT32      0xFFFFFFFFUL
00040 #define PAL_MAX_INT32       0x7FFFFFFFL
00041 #define PAL_MIN_INT32       0x80000000L
00042 #define PAL_MAX_UINT64      0xFFFFFFFFFFFFFFFFULL
00043 #define PAL_MAX_INT64       0x7FFFFFFFFFFFFFFFLL
00044 
00045 // Useful macros.
00046 
00047 
00048 
00049 #if defined(__arm__) || defined(__IAR_SYSTEMS_ICC__) // Compile with ARMCC, GCC_ARM or IAR compilers.
00050     #define PAL_TARGET_POINTER_SIZE __sizeof_ptr
00051     #ifdef __BIG_ENDIAN
00052         #define PAL_COMPILATION_ENDIANITY 1 //!< Define PAL compilation endian (0 is little endian, 1 is big endian).
00053     #else
00054         #define PAL_COMPILATION_ENDIANITY 0 // Define PAL compilation endian (0 is little endian, 1 is big endian).
00055     #endif
00056 #elif defined(__GNUC__) // Compiling with GCC.
00057     #define PAL_TARGET_POINTER_SIZE __SIZEOF_POINTER__
00058     #ifdef __BYTE_ORDER
00059         #if __BYTE_ORDER == __BIG_ENDIAN // If both are not defined it is TRUE!
00060             #define PAL_COMPILATION_ENDIANITY 1 // Define PAL compilation endian (0 is little endian, 1 is big endian).
00061         #elif __BYTE_ORDER == __LITTLE_ENDIAN
00062             #define PAL_COMPILATION_ENDIANITY 0// Define PAL compilation endian (0 is little endian, 1 is big endian).
00063         #else
00064             #error missing endiantiy definition for GCC
00065         #endif
00066     #endif
00067 #else
00068     #error neither ARM target compilers nor GCC used for compilation - not supported
00069 #endif
00070 
00071 
00072 
00073 
00074 #define PAL_MAX(a,b)            ((a) > (b) ? (a) : (b))
00075 
00076 #define PAL_MIN(a,b)            ((a) < (b) ? (a) : (b))
00077 
00078 #define PAL_DIVIDE_ROUND_UP(num, divider)           (((num) + (divider) - 1) / (divider))
00079 
00080 #if PAL_COMPILATION_ENDIANITY == 1
00081 #define BIG__ENDIAN 1
00082 #elif PAL_COMPILATION_ENDIANITY == 0
00083 #define LITTLE__ENDIAN 1
00084 #else
00085 #error neither BIG__ENDIAN nor LITTLE__ENDIAN defined, cannot compile
00086 #endif
00087 
00088 
00089 // Endianity macros.
00090 #ifdef LITTLE__ENDIAN
00091 
00092 #define PAL_HTONS(x) (((((unsigned short)(x)) >> 8) & 0xff) | \
00093             ((((unsigned short)(x)) & 0xff) << 8))
00094 #define PAL_NTOHS(x) (((((unsigned short)(x)) >> 8) & 0xff) | \
00095             ((((unsigned short)(x)) & 0xff) << 8) )
00096 #define PAL_HTONL(x) ((((x)>>24) & 0xffL) | (((x)>>8) & 0xff00L) | \
00097             (((x)<<8) & 0xff0000L) | (((x)<<24) & 0xff000000L))
00098 #define PAL_NTOHL(x) ((((x)>>24) & 0xffL) | (((x)>>8) & 0xff00L) | \
00099             (((x)<<8) & 0xff0000L) | (((x)<<24) & 0xff000000L))
00100 
00101 #elif defined(BIG__ENDIAN)
00102 
00103 #define PAL_HTONS(x) (x)
00104 #define PAL_NTOHS(x) (x)
00105 #define PAL_HTONL(x) (x)
00106 #define PAL_NTOHL(x) (x)
00107 #else
00108 #error neither BIG__ENDIAN nor LITTLE__ENDIAN defined, cannot compile
00109 #endif
00110 
00111 
00112 #define PAL_GET_LOWER_8BITS(x) (x & 0xFF)
00113 
00114 #define PAL_INVERSE_UINT16_BYTES( val ) \
00115     ( ((val) << 8) | (((val) & 0x0000FF00) >> 8))
00116 
00117 #define PAL_INVERSE_UINT32_BYTES( val ) \
00118    ( ((val) >> 24) | (((val) & 0x00FF0000) >> 8) | (((val) & 0x0000FF00) << 8) | (((val) & 0x000000FF) << 24) )
00119 
00120 #define PAL_INVERSE_UINT64_BYTES( val ) \
00121     ((PAL_INVERSE_UINT32_BYTES( ((val >> 16) >> 16)) &0xffffffff)  | ((((uint64_t)PAL_INVERSE_UINT32_BYTES(val & 0xffffffff))<<16)<<16))
00122 
00123 /* Set of Macros similar to the HTONS/L, NTOHS/L ones but converting to/from little endian instead of big endian. */
00124 #ifdef LITTLE__ENDIAN
00125 #define PAL_LITTLE_ENDIAN_TO_HOST_16BIT(x) (x)
00126 #define PAL_LITTLE_ENDIAN_TO_HOST_32BIT(x) (x)
00127 #define PAL_LITTLE_ENDIAN_TO_HOST_64BIT(x) (x)
00128 #define PAL_HOST_TO_LITTLE_ENDIAN_16BIT(x) (x)
00129 #define PAL_HOST_TO_LITTLE_ENDIAN_32BIT(x) (x)
00130 #define PAL_HOST_TO_LITTLE_ENDIAN_64BIT(x) (x)
00131 
00132 
00133 
00134 
00135 #elif defined(BIG__ENDIAN)
00136 #define PAL_LITTLE_ENDIAN_TO_HOST_16BIT(x) (PAL_INVERSE_UINT16_BYTES(((uint16_t)x)))
00137 #define PAL_LITTLE_ENDIAN_TO_HOST_32BIT(x) (PAL_INVERSE_UINT32_BYTES(((uint32_t)x)))
00138 #define PAL_LITTLE_ENDIAN_TO_HOST_64BIT(x) (PAL_INVERSE_UINT64_BYTES(((uint64_t)x)))
00139 #define PAL_HOST_TO_LITTLE_ENDIAN_16BIT(x) (PAL_INVERSE_UINT16_BYTES(((uint16_t)x)))
00140 #define PAL_HOST_TO_LITTLE_ENDIAN_32BIT(x) (PAL_INVERSE_UINT32_BYTES(((uint32_t)x)))
00141 #define PAL_HOST_TO_LITTLE_ENDIAN_64BIT(x) (PAL_INVERSE_UINT64_BYTES(((uint64_t)x)))
00142 
00143 #else
00144 #error neither BIG__ENDIAN nor LITTLE__ENDIAN defined, cannot compile
00145 #endif
00146 
00147 
00148 #define PAL_MODULE_INIT(INIT) INIT= 1
00149 #define PAL_MODULE_DEINIT(INIT) INIT= 0
00150 
00151 //!< Time utility values
00152 #define PAL_MILISEC_TO_SEC(milisec) (milisec/1000)
00153 #define PAL_ONE_SEC                   1
00154 #define PAL_SECONDS_PER_MIN           60
00155 #define PAL_MINUTES_PER_HOUR          60
00156 #define PAL_HOURS_PER_DAY              24
00157 #define PAL_SECONDS_PER_HOUR          PAL_MINUTES_PER_HOUR * PAL_SECONDS_PER_MIN
00158 #define PAL_SECONDS_PER_DAY           PAL_HOURS_PER_DAY * PAL_SECONDS_PER_HOUR
00159 #define PAL_DAYS_IN_A_YEAR            (365U)
00160 #define PAL_RATIO_SECONDS_PER_DAY     480
00161 #define PAL_MINIMUM_RTC_LATENCY_SEC       100
00162 #define PAL_MINIMUM_STORAGE_LATENCY_SEC   500000
00163 #define PAL_MINIMUM_FORWARD_LATENCY_SEC      100000
00164 #define PAL_MINIMUM_BACKWARD_LATENCY_SEC      100
00165 #define PAL_FEB_MONTH 2
00166 #define PAL_MILLI_PER_SECOND 1000
00167 #define PAL_NANO_PER_MILLI 1000000L
00168 #define PAL_NANO_PER_SECOND 1000000000L
00169 #define PAL_MILLI_TO_NANO(x) (((x) % PAL_MILLI_PER_SECOND) * PAL_NANO_PER_MILLI)
00170 #define PAL_MILISEC_TO_SEC(milisec) (milisec/1000)
00171 #define PAL_MIN_SEC_FROM_EPOCH   1483264800 ////at least 47 years passed from 1.1.1970 in seconds (this is also the minimum time for SX OS)
00172 #define PAL_MIN_RTC_SET_TIME    PAL_MIN_SEC_FROM_EPOCH
00173 #define PAL_LAST_SAVED_TIME_LATENCY_SEC     2500000
00174 
00175 //! Define static function and inline function.
00176 #if defined (__CC_ARM)          /* ARM compiler. */
00177     #define PAL_INLINE  __inline
00178 #elif defined (__GNUC__)        /* GNU compiler. */
00179     #define PAL_INLINE  __attribute__((always_inline)) __inline
00180 #else
00181     #define PAL_INLINE  //!< User should provide the compiler inline function command.
00182 #endif
00183 
00184 #define PAL_PRIVATE static
00185 
00186 #if defined (__CC_ARM)          /* ARM compiler. */
00187 #define PAL_PRAGMA(x)
00188 #define PAL_DEPRECATED(x)
00189 #else
00190 #define PAL_PRAGMA(x) _Pragma (#x)
00191 #define PAL_DEPRECATED(x) PAL_PRAGMA(message ("!!! PAL DEPRECATED CODE- " #x))
00192 #endif
00193 
00194 #ifdef DEBUG
00195 
00196 #define PAL_MODULE_IS_INIT(INIT) if(!INIT) return PAL_ERR_NOT_INITIALIZED;
00197 
00198 
00199 #else
00200 #define PAL_MODULE_IS_INIT(INIT) (void)INIT
00201 
00202 #endif //DEBUG
00203 
00204 // Compile time assert.
00205 #define PAL_ASSERT_STATIC(e) \
00206    do { \
00207       enum { assert_static__ = 1/(e) }; \
00208       } while (0)
00209 
00210 #define PAL_UNUSED_ARG(x) (void)(x)
00211 
00212 
00213 
00214 
00215 
00216 //for non recoverable errors
00217 #define PAL_LOG_ASSERT( ARGS...) \
00218 { \
00219     tr_err(ARGS); \
00220     assert(0);\
00221 }
00222 
00223 #define PAL_LOG_ERR_FUNC  tr_err
00224 #define PAL_LOG_WARN_FUNC tr_warn
00225 #define PAL_LOG_INFO_FUNC tr_info
00226 #define PAL_LOG_DBG_FUNC  tr_debug
00227 
00228 // Little trick with mbed-trace error level is equal to function name handling the same level of log output
00229 #define PAL_LOG_LEVEL_ERR  TRACE_LEVEL_ERROR
00230 #define PAL_LOG_LEVEL_WARN TRACE_LEVEL_WARN
00231 #define PAL_LOG_LEVEL_INFO TRACE_LEVEL_INFO
00232 #define PAL_LOG_LEVEL_DBG  TRACE_LEVEL_DEBUG
00233 
00234 #define PAL_LOG_ERR( ARGS...)   PAL_LOG_ERR_FUNC(ARGS);
00235 #define PAL_LOG_WARN( ARGS...)  PAL_LOG_WARN_FUNC(ARGS);
00236 #define PAL_LOG_INFO( ARGS...)  PAL_LOG_INFO_FUNC(ARGS);
00237 #define PAL_LOG_DBG( ARGS...)   PAL_LOG_DBG_FUNC(ARGS);
00238 
00239 
00240 #ifdef DEBUG
00241 #ifdef VERBOSE
00242 #define PAL_PRINTF( ARGS...) \
00243         #define PAL_PRINTF(fmt, ...) PAL_LOG_DBG("%s:%d: " fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__);
00244 #else
00245 #define PAL_PRINTF( ARGS...) \
00246         PAL_LOG_DBG(ARGS);
00247 #endif
00248 #else
00249     #define PAL_PRINTF( ARGS...)
00250 #endif
00251 
00252 #define DEBUG_PRINT(ARGS...) PAL_PRINTF(ARGS)
00253 
00254 #define PAL_PTR_ADDR_ALIGN_UINT8_TO_UINT32 __attribute__((aligned(4)))
00255 
00256 #define PAL_INT32_BITS (sizeof(int32_t) * CHAR_BIT)
00257 
00258 #ifdef DEBUG
00259 
00260 
00261 #define PAL_VALIDATE_CONDITION_WITH_ERROR(condition, error) \
00262     {\
00263         if ((condition)) \
00264         { \
00265             PAL_LOG_ERR("(%s,%d): Parameters  values is illegal\r\n",__FUNCTION__,__LINE__); \
00266             return error; \
00267         } \
00268     }
00269 #define PAL_VALIDATE_ARGUMENTS(condition) PAL_VALIDATE_CONDITION_WITH_ERROR(condition,PAL_ERR_INVALID_ARGUMENT)
00270 
00271 #else
00272     #define PAL_VALIDATE_ARGUMENTS(condition)
00273     #define PAL_VALIDATE_CONDITION_WITH_ERROR(condition, error)
00274 #endif
00275 
00276 
00277 #define PAL_VALIDATE_ARG_RLZ(condition, error) \
00278 {\
00279     if ((condition)) \
00280     { \
00281         return error; \
00282     } \
00283 }
00284 
00285 
00286 
00287 #ifdef __cplusplus
00288 }
00289 #endif
00290 #endif //_PAL_MACROS_H