Simulated product dispenser

Dependencies:   HTS221

Fork of mbed-cloud-workshop-connect-HTS221 by Jim Carver

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, 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 
00113 #define PAL_INVERSE_UINT16_BYTES( val ) \
00114     ( ((val) << 8) | (((val) & 0x0000FF00) >> 8))
00115 
00116 #define PAL_INVERSE_UINT32_BYTES( val ) \
00117    ( ((val) >> 24) | (((val) & 0x00FF0000) >> 8) | (((val) & 0x0000FF00) << 8) | (((val) & 0x000000FF) << 24) )
00118 
00119 #define PAL_INVERSE_UINT64_BYTES( val ) \
00120     ((PAL_INVERSE_UINT32_BYTES( ((val >> 16) >> 16)) &0xffffffff)  | ((((uint64_t)PAL_INVERSE_UINT32_BYTES(val & 0xffffffff))<<16)<<16)) 
00121 
00122 /* Set of Macros similar to the HTONS/L, NTOHS/L ones but converting to/from little endian instead of big endian. */
00123 #ifdef LITTLE__ENDIAN 
00124 #define PAL_LITTLE_ENDIAN_TO_HOST_16BIT(x) (x)
00125 #define PAL_LITTLE_ENDIAN_TO_HOST_32BIT(x) (x)
00126 #define PAL_LITTLE_ENDIAN_TO_HOST_64BIT(x) (x)
00127 #define PAL_HOST_TO_LITTLE_ENDIAN_16BIT(x) (x)
00128 #define PAL_HOST_TO_LITTLE_ENDIAN_32BIT(x) (x)
00129 #define PAL_HOST_TO_LITTLE_ENDIAN_64BIT(x) (x)
00130 
00131 
00132 
00133 
00134 #elif defined(BIG__ENDIAN)
00135 #define PAL_LITTLE_ENDIAN_TO_HOST_16BIT(x) (PAL_INVERSE_UINT16_BYTES(((uint16_t)x)))
00136 #define PAL_LITTLE_ENDIAN_TO_HOST_32BIT(x) (PAL_INVERSE_UINT32_BYTES(((uint32_t)x)))
00137 #define PAL_LITTLE_ENDIAN_TO_HOST_64BIT(x) (PAL_INVERSE_UINT64_BYTES(((uint64_t)x)))
00138 #define PAL_HOST_TO_LITTLE_ENDIAN_16BIT(x) (PAL_INVERSE_UINT16_BYTES(((uint16_t)x)))
00139 #define PAL_HOST_TO_LITTLE_ENDIAN_32BIT(x) (PAL_INVERSE_UINT32_BYTES(((uint32_t)x)))
00140 #define PAL_HOST_TO_LITTLE_ENDIAN_64BIT(x) (PAL_INVERSE_UINT64_BYTES(((uint64_t)x)))
00141 
00142 #else
00143 #error neither BIG__ENDIAN nor LITTLE__ENDIAN defined, cannot compile
00144 #endif
00145 
00146 
00147 #define PAL_MODULE_INIT(INIT) INIT= 1
00148 #define PAL_MODULE_DEINIT(INIT) INIT= 0
00149 
00150 //!< Time utility values
00151 #define PAL_MILISEC_TO_SEC(milisec) (milisec/1000)
00152 #define PAL_ONE_SEC                   1
00153 #define PAL_SECONDS_PER_MIN           60
00154 #define PAL_MINUTES_PER_HOUR          60
00155 #define PAL_HOURS_PER_DAY              24
00156 #define PAL_SECONDS_PER_HOUR          PAL_MINUTES_PER_HOUR * PAL_SECONDS_PER_MIN
00157 #define PAL_SECONDS_PER_DAY           PAL_HOURS_PER_DAY * PAL_SECONDS_PER_HOUR
00158 #define PAL_DAYS_IN_A_YEAR            (365U)
00159 #define PAL_RATIO_SECONDS_PER_DAY     480
00160 #define PAL_MINIMUM_RTC_LATENCY_SEC       100
00161 #define PAL_MINIMUM_STORAGE_LATENCY_SEC   500000
00162 #define PAL_MINIMUM_SOTP_FORWARD_LATENCY_SEC      100000
00163 #define PAL_MINIMUM_SOTP_BACKWARD_LATENCY_SEC      100
00164 #define PAL_FEB_MONTH 2
00165 #define PAL_MILLI_PER_SECOND 1000
00166 #define PAL_NANO_PER_MILLI 1000000L
00167 #define PAL_NANO_PER_SECOND 1000000000L
00168 #define PAL_MILLI_TO_NANO(x) (((x) % PAL_MILLI_PER_SECOND) * PAL_NANO_PER_MILLI)
00169 #define PAL_MILISEC_TO_SEC(milisec) (milisec/1000)
00170 #define PAL_MIN_SEC_FROM_EPOCH   1487015542 ////at least 47 years passed from 1.1.1970 in seconds
00171 #define PAL_MIN_RTC_SET_TIME    PAL_MIN_SEC_FROM_EPOCH
00172 #define PAL_LAST_SAVED_TIME_LATENCY_SEC     2500000
00173 
00174 //!< Define static function and inline function.
00175 #if defined (__CC_ARM)          /* ARM compiler. */
00176     #define PAL_INLINE  __inline
00177 #elif defined (__GNUC__)        /* GNU compiler. */
00178     #define PAL_INLINE  __attribute__((always_inline)) __inline
00179 #else
00180     #define PAL_INLINE  //!< User should provide the compiler inline function command.
00181 #endif
00182 
00183 #define PAL_PRIVATE static
00184 
00185 #if defined (__CC_ARM)          /* ARM compiler. */
00186 #define PAL_PRAGMA(x)
00187 #define PAL_DEPRECATED(x)
00188 #else
00189 #define PAL_PRAGMA(x) _Pragma (#x)
00190 #define PAL_DEPRECATED(x) PAL_PRAGMA(message ("!!! PAL DEPRECATED CODE- " #x))
00191 #endif
00192 
00193 #ifdef DEBUG
00194 
00195 #define PAL_MODULE_IS_INIT(INIT) if(!INIT) return PAL_ERR_NOT_INITIALIZED;
00196 
00197 
00198 #else
00199 #define PAL_MODULE_IS_INIT(INIT) (void)INIT
00200 
00201 #endif //DEBUG
00202 
00203 // Compile time assert.
00204 #define PAL_ASSERT_STATIC(e) \
00205    do { \
00206       enum { assert_static__ = 1/(e) }; \
00207       } while (0)
00208 
00209 #define PAL_UNUSED_ARG(x) (void)(x)
00210 
00211 
00212 
00213 
00214 
00215 //for non recoverable errors
00216 #define PAL_LOG_ASSERT( ARGS...) \
00217 { \
00218     tr_err(ARGS); \
00219     assert(0);\
00220 }
00221 
00222 
00223 
00224 #define PAL_LOG_ERR_FUNC  tr_err
00225 #define PAL_LOG_WARN_FUNC tr_warn
00226 #define PAL_LOG_INFO_FUNC tr_info
00227 #define PAL_LOG_DBG_FUNC  tr_debug
00228 
00229 // Little trick with mbed-trace error level is equal to function name handling the same level of log output
00230 #define PAL_LOG_LEVEL_ERR  TRACE_LEVEL_ERROR
00231 #define PAL_LOG_LEVEL_WARN TRACE_LEVEL_WARN
00232 #define PAL_LOG_LEVEL_INFO TRACE_LEVEL_INFO
00233 #define PAL_LOG_LEVEL_DBG  TRACE_LEVEL_DEBUG
00234 
00235 #define PAL_LOG_ERR( ARGS...)   PAL_LOG_ERR_FUNC(ARGS);
00236 #define PAL_LOG_WARN( ARGS...)  PAL_LOG_WARN_FUNC(ARGS);
00237 #define PAL_LOG_INFO( ARGS...)  PAL_LOG_INFO_FUNC(ARGS);
00238 #define PAL_LOG_DBG( ARGS...)   PAL_LOG_DBG_FUNC(ARGS);
00239 
00240 
00241 #define PAL_LOG(LOG_LEVEL, ARGS...)  tracef(PAL_LOG_LEVEL_##LOG_LEVEL, "PAL" , ARGS);
00242 
00243 
00244 #ifdef DEBUG
00245 #ifdef VERBOSE
00246 #define PAL_PRINTF( ARGS...) \
00247         #define PAL_PRINTF(fmt, ...) PAL_LOG(DBG, "%s:%d: " fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__);
00248 #else
00249 #define PAL_PRINTF( ARGS...) \
00250         PAL_LOG(DBG, ARGS);
00251 #endif
00252 #else
00253     #define PAL_PRINTF( ARGS...)
00254 #endif
00255 
00256 #define DEBUG_PRINT(ARGS...) PAL_PRINTF(ARGS)
00257 
00258 #define PAL_PTR_ADDR_ALIGN_UINT8_TO_UINT32 __attribute__((aligned(4)))
00259 
00260 #define PAL_INT32_BITS (sizeof(int32_t) * CHAR_BIT)
00261 
00262 #ifdef DEBUG
00263 
00264 
00265 #define PAL_VALIDATE_CONDITION_WITH_ERROR(condition, error) \
00266     {\
00267         if ((condition)) \
00268         { \
00269             PAL_LOG(ERR,"(%s,%d): Parameters  values is illegal\r\n",__FUNCTION__,__LINE__); \
00270             return error; \
00271         } \
00272     }
00273 #define PAL_VALIDATE_ARGUMENTS(condition) PAL_VALIDATE_CONDITION_WITH_ERROR(condition,PAL_ERR_INVALID_ARGUMENT)
00274 
00275 #else
00276     #define PAL_VALIDATE_ARGUMENTS(condition) 
00277     #define PAL_VALIDATE_CONDITION_WITH_ERROR(condition, error) 
00278 #endif
00279 
00280 
00281 #define PAL_VALIDATE_ARG_RLZ(condition, error) \
00282 {\
00283     if ((condition)) \
00284     { \
00285         return error; \
00286     } \
00287 }
00288 
00289 
00290 
00291 #ifdef __cplusplus
00292 }
00293 #endif
00294 #endif //_PAL_MACROS_H