Simple interface for Mbed Cloud Client
Embed:
(wiki syntax)
Show/hide line numbers
pal_macros.h
Go to the documentation of this file.
00001 /******************************************************************************* 00002 * Copyright 2016, 2017 ARM Ltd. 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 *******************************************************************************/ 00016 00017 00018 #ifndef _PAL_MACROS_H 00019 #define _PAL_MACROS_H 00020 00021 #ifdef __cplusplus 00022 extern "C" { 00023 #endif 00024 00025 //for PAL_LOG prints 00026 #include "pal.h" 00027 #include "mbed-trace/mbed_trace.h" 00028 #include "assert.h" 00029 #include <limits.h> 00030 /*! \file pal_macros.h 00031 * \brief PAL macros. 00032 * This file contains macros defined by PAL for constant values and network purposes. 00033 */ 00034 00035 // Maximum integer types. 00036 #define PAL_MAX_UINT8 0xFFU 00037 #define PAL_MAX_UINT16 0xFFFFU 00038 #define PAL_MAX_UINT32 0xFFFFFFFFUL 00039 #define PAL_MAX_INT32 0x7FFFFFFFL 00040 #define PAL_MIN_INT32 0x80000000L 00041 #define PAL_MAX_UINT64 0xFFFFFFFFFFFFFFFFULL 00042 #define PAL_MAX_INT64 0x7FFFFFFFFFFFFFFFLL 00043 00044 // Useful macros. 00045 00046 00047 00048 #if defined(__arm__) || defined(__IAR_SYSTEMS_ICC__) // Compile with ARMCC, GCC_ARM or IAR compilers. 00049 #define PAL_TARGET_POINTER_SIZE __sizeof_ptr 00050 #ifdef __BIG_ENDIAN 00051 #define PAL_COMPILATION_ENDIANITY 1 // Define PAL compilation endian (0 is little endian, 1 is big endian). 00052 #else 00053 #define PAL_COMPILATION_ENDIANITY 0 // Define PAL compilation endian (0 is little endian, 1 is big endian). 00054 #endif 00055 #elif defined(__GNUC__) // Compiling with GCC. 00056 #define PAL_TARGET_POINTER_SIZE __SIZEOF_POINTER__ 00057 #ifdef __BYTE_ORDER 00058 #if __BYTE_ORDER == __BIG_ENDIAN // If both are not defined it is TRUE! 00059 #define PAL_COMPILATION_ENDIANITY 1 // Define PAL compilation endian (0 is little endian, 1 is big endian). 00060 #elif __BYTE_ORDER == __LITTLE_ENDIAN 00061 #define PAL_COMPILATION_ENDIANITY 0// Define PAL compilation endian (0 is little endian, 1 is big endian). 00062 #else 00063 #error missing endiantiy defintion for GCC 00064 #endif 00065 #endif 00066 #else 00067 #error neither ARM target compilers nor GCC used for compilation - not supported 00068 #endif 00069 00070 00071 00072 00073 #define PAL_MAX(a,b) ((a) > (b) ? (a) : (b)) 00074 00075 #define PAL_MIN(a,b) ((a) < (b) ? (a) : (b)) 00076 00077 #define PAL_DIVIDE_ROUND_UP(num, divider) (((num) + (divider) - 1) / (divider)) 00078 00079 #if PAL_COMPILATION_ENDIANITY == 1 00080 #define BIG__ENDIAN 1 00081 #elif PAL_COMPILATION_ENDIANITY == 0 00082 #define LITTLE__ENDIAN 1 00083 #else 00084 #error neither BIG__ENDIAN nor LITTLE__ENDIAN defined, cannot compile 00085 #endif 00086 00087 00088 // Endianity macros. 00089 #ifdef LITTLE__ENDIAN 00090 00091 #define PAL_HTONS(x) (((((unsigned short)(x)) >> 8) & 0xff) | \ 00092 ((((unsigned short)(x)) & 0xff) << 8)) 00093 #define PAL_NTOHS(x) (((((unsigned short)(x)) >> 8) & 0xff) | \ 00094 ((((unsigned short)(x)) & 0xff) << 8) ) 00095 #define PAL_HTONL(x) ((((x)>>24) & 0xffL) | (((x)>>8) & 0xff00L) | \ 00096 (((x)<<8) & 0xff0000L) | (((x)<<24) & 0xff000000L)) 00097 #define PAL_NTOHL(x) ((((x)>>24) & 0xffL) | (((x)>>8) & 0xff00L) | \ 00098 (((x)<<8) & 0xff0000L) | (((x)<<24) & 0xff000000L)) 00099 00100 #elif defined(BIG__ENDIAN) 00101 00102 #define PAL_HTONS(x) (x) 00103 #define PAL_NTOHS(x) (x) 00104 #define PAL_HTONL(x) (x) 00105 #define PAL_NTOHL(x) (x) 00106 #else 00107 #error neither BIG__ENDIAN nor LITTLE__ENDIAN defined, cannot compile 00108 #endif 00109 00110 00111 #define PAL_GET_LOWER_8BITS(x) (x & 0xFF) 00112 #define PAL_GET_THREAD_INDEX(x) (PAL_GET_LOWER_8BITS(x)) 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_SOTP_FORWARD_LATENCY_SEC 100000 00164 #define PAL_MINIMUM_SOTP_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 1487015542 ////at least 47 years passed from 1.1.1970 in seconds 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 00224 00225 #define PAL_LOG_ERR_FUNC tr_err 00226 #define PAL_LOG_WARN_FUNC tr_warn 00227 #define PAL_LOG_INFO_FUNC tr_info 00228 #define PAL_LOG_DBG_FUNC tr_debug 00229 00230 // Little trick with mbed-trace error level is equal to function name handling the same level of log output 00231 #define PAL_LOG_LEVEL_ERR TRACE_LEVEL_ERROR 00232 #define PAL_LOG_LEVEL_WARN TRACE_LEVEL_WARN 00233 #define PAL_LOG_LEVEL_INFO TRACE_LEVEL_INFO 00234 #define PAL_LOG_LEVEL_DBG TRACE_LEVEL_DEBUG 00235 00236 #define PAL_LOG_ERR( ARGS...) PAL_LOG_ERR_FUNC(ARGS); 00237 #define PAL_LOG_WARN( ARGS...) PAL_LOG_WARN_FUNC(ARGS); 00238 #define PAL_LOG_INFO( ARGS...) PAL_LOG_INFO_FUNC(ARGS); 00239 #define PAL_LOG_DBG( ARGS...) PAL_LOG_DBG_FUNC(ARGS); 00240 00241 00242 #define PAL_LOG(LOG_LEVEL, ARGS...) tracef(PAL_LOG_LEVEL_##LOG_LEVEL, "PAL" , ARGS); 00243 00244 00245 #ifdef DEBUG 00246 #ifdef VERBOSE 00247 #define PAL_PRINTF( ARGS...) \ 00248 #define PAL_PRINTF(fmt, ...) PAL_LOG(DBG, "%s:%d: " fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__); 00249 #else 00250 #define PAL_PRINTF( ARGS...) \ 00251 PAL_LOG(DBG, ARGS); 00252 #endif 00253 #else 00254 #define PAL_PRINTF( ARGS...) 00255 #endif 00256 00257 #define DEBUG_PRINT(ARGS...) PAL_PRINTF(ARGS) 00258 00259 #define PAL_PTR_ADDR_ALIGN_UINT8_TO_UINT32 __attribute__((aligned(4))) 00260 00261 #define PAL_INT32_BITS (sizeof(int32_t) * CHAR_BIT) 00262 00263 #ifdef DEBUG 00264 00265 00266 #define PAL_VALIDATE_CONDITION_WITH_ERROR(condition, error) \ 00267 {\ 00268 if ((condition)) \ 00269 { \ 00270 PAL_LOG(ERR,"(%s,%d): Parameters values is illegal\r\n",__FUNCTION__,__LINE__); \ 00271 return error; \ 00272 } \ 00273 } 00274 #define PAL_VALIDATE_ARGUMENTS(condition) PAL_VALIDATE_CONDITION_WITH_ERROR(condition,PAL_ERR_INVALID_ARGUMENT) 00275 00276 #else 00277 #define PAL_VALIDATE_ARGUMENTS(condition) PAL_VALIDATE_ARG_RLZ(condition,PAL_ERR_INVALID_ARGUMENT) 00278 #define PAL_VALIDATE_CONDITION_WITH_ERROR(condition, error) PAL_VALIDATE_ARG_RLZ(condition, error) 00279 #endif 00280 00281 00282 #define PAL_VALIDATE_ARG_RLZ(condition, error) \ 00283 {\ 00284 if ((condition)) \ 00285 { \ 00286 return error; \ 00287 } \ 00288 } 00289 00290 00291 00292 #ifdef __cplusplus 00293 } 00294 #endif 00295 #endif //_PAL_MACROS_H
Generated on Tue Jul 12 2022 19:01:36 by 1.7.2