Ram Gandikota / Mbed OS ABCD
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers pal_macros.h Source File

pal_macros.h

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 // PAL success value
00027 #define PAL_SUCCESS 0
00028 
00029 // maximum integer types
00030 #define PAL_MAX_UINT8       0xFFU
00031 #define PAL_MAX_UINT16      0xFFFFU
00032 #define PAL_MAX_UINT32      0xFFFFFFFFUL
00033 #define PAL_MAX_INT32       0x7FFFFFFFL
00034 #define PAL_MIN_INT32       0x80000000L
00035 #define PAL_MAX_UINT64      0xFFFFFFFFFFFFFFFFULL
00036 #define PAL_MAX_INT64       0x7FFFFFFFFFFFFFFFLL
00037 
00038 // useful macros
00039 
00040 #define PAL_MAX(a,b)            ((a) > (b) ? (a) : (b))
00041 
00042 #define PAL_MIN(a,b)            ((a) < (b) ? (a) : (b))
00043 
00044 #define PAL_DIVIDE_ROUND_UP(num, divider)           (((num) + (divider) - 1) / (divider))
00045 
00046 #if PAL_COMPILATION_ENDIANITY == 1
00047 #define BIG__ENDIAN 1
00048 #elif PAL_COMPILATION_ENDIANITY == 0
00049 #define LITTLE__ENDIAN 1
00050 #else 
00051 #error neither BIG__ENDIAN nor LITTLE__ENDIAN defined, cannot compile
00052 #endif
00053 
00054 // endianity macros
00055 #ifdef LITTLE__ENDIAN
00056 
00057 #define PAL_HTONS(x) (((((unsigned short)(x)) >> 8) & 0xff) | \
00058             ((((unsigned short)(x)) & 0xff) << 8))
00059 #define PAL_NTOHS(x) (((((unsigned short)(x)) >> 8) & 0xff) | \
00060             ((((unsigned short)(x)) & 0xff) << 8) )
00061 #define PAL_HTONL(x) ((((x)>>24) & 0xffL) | (((x)>>8) & 0xff00L) | \
00062             (((x)<<8) & 0xff0000L) | (((x)<<24) & 0xff000000L))
00063 #define PAL_NTOHL(x) ((((x)>>24) & 0xffL) | (((x)>>8) & 0xff00L) | \
00064             (((x)<<8) & 0xff0000L) | (((x)<<24) & 0xff000000L))
00065 
00066 #elif defined(BIG__ENDIAN)
00067 
00068 #define PAL_HTONS(x) (x)
00069 #define PAL_NTOHS(x) (x)
00070 #define PAL_HTONL(x) (x)
00071 #define PAL_NTOHL(x) (x)
00072 #else
00073 #error neither BIG__ENDIAN nor LITTLE__ENDIAN defined, cannot compile
00074 #endif
00075 
00076 
00077 
00078 #define PAL_INVERSE_UINT16_BYTES( val ) \
00079     ( ((val) << 8) | (((val) & 0x0000FF00) >> 8))
00080 
00081 #define PAL_INVERSE_UINT32_BYTES( val ) \
00082    ( ((val) >> 24) | (((val) & 0x00FF0000) >> 8) | (((val) & 0x0000FF00) << 8) | (((val) & 0x000000FF) << 24) )
00083 
00084 #define PAL_INVERSE_UINT64_BYTES( val ) \
00085     ((PAL_INVERSE_UINT32_BYTES( ((val >> 16) >> 16)) &0xffffffff)  | ((((uint64_t)PAL_INVERSE_UINT32_BYTES(val & 0xffffffff))<<16)<<16)) 
00086 
00087 /* Set of Macros similar to the HTONS/L, NTOHS/L ones but converting to/from little endian instead of big endian*/
00088 #ifdef LITTLE__ENDIAN 
00089 #define PAL_LITTLE_ENDIAN_TO_HOST_16BIT(x) (x)
00090 #define PAL_LITTLE_ENDIAN_TO_HOST_32BIT(x) (x)
00091 #define PAL_LITTLE_ENDIAN_TO_HOST_64BIT(x) (x)
00092 #define PAL_HOST_TO_LITTLE_ENDIAN_16BIT(x) (x)
00093 #define PAL_HOST_TO_LITTLE_ENDIAN_32BIT(x) (x)
00094 #define PAL_HOST_TO_LITTLE_ENDIAN_64BIT(x) (x)
00095 
00096 
00097 #elif defined(BIG__ENDIAN)
00098 #define PAL_LITTLE_ENDIAN_TO_HOST_16BIT(x) (PAL_INVERSE_UINT16_BYTES(((uint16_t)x)))
00099 #define PAL_LITTLE_ENDIAN_TO_HOST_32BIT(x) (PAL_INVERSE_UINT32_BYTES(((uint32_t)x)))
00100 #define PAL_LITTLE_ENDIAN_TO_HOST_64BIT(x) (PAL_INVERSE_UINT64_BYTES(((uint64_t)x)))
00101 #define PAL_HOST_TO_LITTLE_ENDIAN_16BIT(x) (PAL_INVERSE_UINT16_BYTES(((uint16_t)x)))
00102 #define PAL_HOST_TO_LITTLE_ENDIAN_32BIT(x) (PAL_INVERSE_UINT32_BYTES(((uint32_t)x)))
00103 #define PAL_HOST_TO_LITTLE_ENDIAN_64BIT(x) (PAL_INVERSE_UINT64_BYTES(((uint64_t)x)))
00104 
00105 #else
00106 #error neither BIG__ENDIAN nor LITTLE__ENDIAN defined, cannot compile
00107 #endif
00108 
00109 
00110 #define PAL_MODULE_INIT(INIT) INIT= 1
00111 #define PAL_MODULE_DEINIT(INIT) INIT= 0
00112 
00113 #ifdef DEBUG
00114 #include "pal.h"
00115 #define DEBUG_PRINT(ARGS...) PAL_PRINTF(ARGS)
00116 
00117 #define DEBUG_PRINT(ARGS...) PAL_PRINTF(ARGS)
00118 #define PAL_MODULE_IS_INIT(INIT) if(!INIT) return PAL_ERR_NOT_INITIALIZED;
00119 
00120 
00121 #else
00122 #define PAL_MODULE_IS_INIT(INIT)
00123 
00124 #define DEBUG_PRINT(ARGS...)
00125 
00126 #endif //DEBUG
00127 
00128 #ifdef __cplusplus
00129 }
00130 #endif
00131 #endif //_PAL_MACROS_H