Toyomasa Watarai / Mbed OS Mbed-example-WS-W27

Dependencies:   MMA7660 LM75B

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 (c) 2016 ARM Limited. All rights reserved.
00003 * SPDX-License-Identifier: Apache-2.0
00004 * Licensed under the Apache License, Version 2.0 (the License); you may
00005 * 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, WITHOUT
00012 * 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 #include "pal_errors.h"
00026 #include "pal_types.h" // is needed for PRIu types
00027 //for PAL_LOG prints
00028 #include "mbed-trace/mbed_trace.h"
00029 #include "assert.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 #define PAL_MAX(a,b)            ((a) > (b) ? (a) : (b))
00047 
00048 #define PAL_MIN(a,b)            ((a) < (b) ? (a) : (b))
00049 
00050 #define PAL_DIVIDE_ROUND_UP(num, divider)           (((num) + (divider) - 1) / (divider))
00051 
00052 #if PAL_COMPILATION_ENDIANITY == 1
00053 #define BIG__ENDIAN 1
00054 #elif PAL_COMPILATION_ENDIANITY == 0
00055 #define LITTLE__ENDIAN 1
00056 #else 
00057 #error neither BIG__ENDIAN nor LITTLE__ENDIAN defined, cannot compile
00058 #endif
00059 
00060 // Endianity macros.
00061 #ifdef LITTLE__ENDIAN
00062 
00063 #define PAL_HTONS(x) (((((unsigned short)(x)) >> 8) & 0xff) | \
00064             ((((unsigned short)(x)) & 0xff) << 8))
00065 #define PAL_NTOHS(x) (((((unsigned short)(x)) >> 8) & 0xff) | \
00066             ((((unsigned short)(x)) & 0xff) << 8) )
00067 #define PAL_HTONL(x) ((((x)>>24) & 0xffL) | (((x)>>8) & 0xff00L) | \
00068             (((x)<<8) & 0xff0000L) | (((x)<<24) & 0xff000000L))
00069 #define PAL_NTOHL(x) ((((x)>>24) & 0xffL) | (((x)>>8) & 0xff00L) | \
00070             (((x)<<8) & 0xff0000L) | (((x)<<24) & 0xff000000L))
00071 
00072 #elif defined(BIG__ENDIAN)
00073 
00074 #define PAL_HTONS(x) (x)
00075 #define PAL_NTOHS(x) (x)
00076 #define PAL_HTONL(x) (x)
00077 #define PAL_NTOHL(x) (x)
00078 #else
00079 #error neither BIG__ENDIAN nor LITTLE__ENDIAN defined, cannot compile
00080 #endif
00081 
00082 
00083 #define PAL_GET_LOWER_8BITS(x) (x & 0xFF)
00084 #define PAL_GET_THREAD_INDEX(x) (PAL_GET_LOWER_8BITS(x))
00085 
00086 #define PAL_INVERSE_UINT16_BYTES( val ) \
00087     ( ((val) << 8) | (((val) & 0x0000FF00) >> 8))
00088 
00089 #define PAL_INVERSE_UINT32_BYTES( val ) \
00090    ( ((val) >> 24) | (((val) & 0x00FF0000) >> 8) | (((val) & 0x0000FF00) << 8) | (((val) & 0x000000FF) << 24) )
00091 
00092 #define PAL_INVERSE_UINT64_BYTES( val ) \
00093     ((PAL_INVERSE_UINT32_BYTES( ((val >> 16) >> 16)) &0xffffffff)  | ((((uint64_t)PAL_INVERSE_UINT32_BYTES(val & 0xffffffff))<<16)<<16)) 
00094 
00095 /* Set of Macros similar to the HTONS/L, NTOHS/L ones but converting to/from little endian instead of big endian. */
00096 #ifdef LITTLE__ENDIAN 
00097 #define PAL_LITTLE_ENDIAN_TO_HOST_16BIT(x) (x)
00098 #define PAL_LITTLE_ENDIAN_TO_HOST_32BIT(x) (x)
00099 #define PAL_LITTLE_ENDIAN_TO_HOST_64BIT(x) (x)
00100 #define PAL_HOST_TO_LITTLE_ENDIAN_16BIT(x) (x)
00101 #define PAL_HOST_TO_LITTLE_ENDIAN_32BIT(x) (x)
00102 #define PAL_HOST_TO_LITTLE_ENDIAN_64BIT(x) (x)
00103 
00104 
00105 
00106 #if defined(__arm__) || defined(__IAR_SYSTEMS_ICC__) // Compile with ARMCC, GCC_ARM or IAR compilers.
00107     #define PAL_TARGET_POINTER_SIZE __sizeof_ptr
00108     #ifdef __BIG_ENDIAN
00109         #define PAL_COMPILATION_ENDIANITY 1 // Define PAL compilation endian (0 is little endian, 1 is big endian).
00110     #else
00111         #define PAL_COMPILATION_ENDIANITY 0 // Define PAL compilation endian (0 is little endian, 1 is big endian).
00112     #endif
00113 #elif defined(__GNUC__) // Compiling with GCC.
00114     #define PAL_TARGET_POINTER_SIZE __SIZEOF_POINTER__
00115     #ifdef __BYTE_ORDER
00116         #if __BYTE_ORDER == __BIG_ENDIAN // If both are not defined it is TRUE!
00117             #define PAL_COMPILATION_ENDIANITY 1 // Define PAL compilation endian (0 is little endian, 1 is big endian).
00118         #elif __BYTE_ORDER == __LITTLE_ENDIAN
00119             #define PAL_COMPILATION_ENDIANITY 0// Define PAL compilation endian (0 is little endian, 1 is big endian).
00120         #else
00121             #error missing endiantiy defintion for GCC
00122         #endif
00123     #endif
00124 #else
00125     #error neither ARM target compilers nor GCC used for compilation - not supported
00126 #endif
00127 
00128 
00129 
00130 #elif defined(BIG__ENDIAN)
00131 #define PAL_LITTLE_ENDIAN_TO_HOST_16BIT(x) (PAL_INVERSE_UINT16_BYTES(((uint16_t)x)))
00132 #define PAL_LITTLE_ENDIAN_TO_HOST_32BIT(x) (PAL_INVERSE_UINT32_BYTES(((uint32_t)x)))
00133 #define PAL_LITTLE_ENDIAN_TO_HOST_64BIT(x) (PAL_INVERSE_UINT64_BYTES(((uint64_t)x)))
00134 #define PAL_HOST_TO_LITTLE_ENDIAN_16BIT(x) (PAL_INVERSE_UINT16_BYTES(((uint16_t)x)))
00135 #define PAL_HOST_TO_LITTLE_ENDIAN_32BIT(x) (PAL_INVERSE_UINT32_BYTES(((uint32_t)x)))
00136 #define PAL_HOST_TO_LITTLE_ENDIAN_64BIT(x) (PAL_INVERSE_UINT64_BYTES(((uint64_t)x)))
00137 
00138 #else
00139 #error neither BIG__ENDIAN nor LITTLE__ENDIAN defined, cannot compile
00140 #endif
00141 
00142 
00143 #define PAL_MODULE_INIT(INIT) INIT= 1
00144 #define PAL_MODULE_DEINIT(INIT) INIT= 0
00145 
00146 //!< Time utility values
00147 #define PAL_SECONDS_PER_MIN 60
00148 #define PAL_SECONDS_PER_HOUR 3600
00149 #define PAL_SECONDS_PER_DAY 86400
00150 #define PAL_FEB_MONTH 2
00151 #define PAL_MILLI_PER_SECOND 1000
00152 #define PAL_NANO_PER_MILLI 1000000L
00153 #define PAL_NANO_PER_SECOND 1000000000L
00154 #define PAL_MILLI_TO_NANO(x) (((x) % PAL_MILLI_PER_SECOND) * PAL_NANO_PER_MILLI)
00155 #define PAL_MIN_SEC_FROM_EPOCH 1487015542 ////at least 47 years passed from 1.1.1970 in seconds
00156 
00157 //!< Define static function and inline function.
00158 #if defined (__CC_ARM)          /* ARM compiler. */
00159     #define PAL_INLINE  __inline
00160 #elif defined (__GNUC__)        /* GNU compiler. */
00161     #define PAL_INLINE  __attribute__((always_inline)) __inline
00162 #else
00163     #define PAL_INLINE  //!< User should provide the compiler inline function command.
00164 #endif
00165 
00166 #define PAL_PRIVATE static
00167 
00168 #if defined (__CC_ARM)          /* ARM compiler. */
00169 #define PAL_PRAGMA(x)
00170 #define PAL_DEPRECATED(x)
00171 #else
00172 #define PAL_PRAGMA(x) _Pragma (#x)
00173 #define PAL_DEPRECATED(x) PAL_PRAGMA(message ("!!! PAL DEPRECATED CODE- " #x))
00174 #endif
00175 
00176 #ifdef DEBUG
00177 
00178 #define PAL_MODULE_IS_INIT(INIT) if(!INIT) return PAL_ERR_NOT_INITIALIZED;
00179 
00180 
00181 #else
00182 #define PAL_MODULE_IS_INIT(INIT) (void)INIT
00183 
00184 #endif //DEBUG
00185 
00186 // Compile time assert.
00187 #define PAL_ASSERT_STATIC(e) \
00188    do { \
00189       enum { assert_static__ = 1/(e) }; \
00190       } while (0)
00191 
00192 #define PAL_UNUSED_ARG(x) (void)(x)
00193 
00194 
00195 
00196 
00197 
00198 //for non recoverable errors
00199 #define PAL_LOG_ASSERT( ARGS...) \
00200 { \
00201     tr_err(ARGS); \
00202     assert(0);\
00203 }
00204 
00205 
00206 
00207 #define PAL_LOG_ERR_FUNC  tr_err
00208 #define PAL_LOG_WARN_FUNC tr_warn
00209 #define PAL_LOG_INFO_FUNC tr_info
00210 #define PAL_LOG_DBG_FUNC  tr_debug
00211 
00212 // Little trick with mbed-trace error level is equal to function name handling the same level of log output
00213 #define PAL_LOG_LEVEL_ERR  TRACE_LEVEL_ERROR
00214 #define PAL_LOG_LEVEL_WARN TRACE_LEVEL_WARN
00215 #define PAL_LOG_LEVEL_INFO TRACE_LEVEL_INFO
00216 #define PAL_LOG_LEVEL_DBG  TRACE_LEVEL_DEBUG
00217 
00218 #define PAL_LOG_ERR( ARGS...)   PAL_LOG_ERR_FUNC(ARGS);
00219 #define PAL_LOG_WARN( ARGS...)  PAL_LOG_WARN_FUNC(ARGS);
00220 #define PAL_LOG_INFO( ARGS...)  PAL_LOG_INFO_FUNC(ARGS);
00221 #define PAL_LOG_DBG( ARGS...)   PAL_LOG_DBG_FUNC(ARGS);
00222 
00223 
00224 #define PAL_LOG(LOG_LEVEL, ARGS...)  tracef(PAL_LOG_LEVEL_##LOG_LEVEL, "PAL" , ARGS);
00225 
00226 
00227 #ifdef DEBUG
00228 #ifdef VERBOSE
00229 #define PAL_PRINTF( ARGS...) \
00230         #define PAL_PRINTF(fmt, ...) PAL_LOG(DBG, "%s:%d: " fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__);
00231 #else
00232 #define PAL_PRINTF( ARGS...) \
00233         PAL_LOG(DBG, ARGS);
00234 #endif
00235 #else
00236     #define PAL_PRINTF( ARGS...)
00237 #endif
00238 
00239 #define DEBUG_PRINT(ARGS...) PAL_PRINTF(ARGS)
00240 
00241 
00242 #ifdef __cplusplus
00243 }
00244 #endif
00245 #endif //_PAL_MACROS_H