PAL
A Platform Abstraction Layer connects the mbed-client with the underlying platform.
pal_macros.h
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2016 ARM Limited. All rights reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 * Licensed under the Apache License, Version 2.0 (the License); you may
5 * not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 
17 
18 #ifndef _PAL_MACROS_H
19 #define _PAL_MACROS_H
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 #include "pal_errors.h"
26 // PAL success value
27 #define PAL_SUCCESS 0
28 
29 // maximum integer types
30 #define PAL_MAX_UINT8 0xFFU
31 #define PAL_MAX_UINT16 0xFFFFU
32 #define PAL_MAX_UINT32 0xFFFFFFFFUL
33 #define PAL_MAX_INT32 0x7FFFFFFFL
34 #define PAL_MIN_INT32 0x80000000L
35 #define PAL_MAX_UINT64 0xFFFFFFFFFFFFFFFFULL
36 #define PAL_MAX_INT64 0x7FFFFFFFFFFFFFFFLL
37 
38 // useful macros
39 
40 #define PAL_MAX(a,b) ((a) > (b) ? (a) : (b))
41 
42 #define PAL_MIN(a,b) ((a) < (b) ? (a) : (b))
43 
44 #define PAL_DIVIDE_ROUND_UP(num, divider) (((num) + (divider) - 1) / (divider))
45 
46 #if PAL_COMPILATION_ENDIANITY == 1
47 #define BIG__ENDIAN 1
48 #elif PAL_COMPILATION_ENDIANITY == 0
49 #define LITTLE__ENDIAN 1
50 #else
51 #error neither BIG__ENDIAN nor LITTLE__ENDIAN defined, cannot compile
52 #endif
53 
54 // endianity macros
55 #ifdef LITTLE__ENDIAN
56 
57 #define PAL_HTONS(x) (((((unsigned short)(x)) >> 8) & 0xff) | \
58  ((((unsigned short)(x)) & 0xff) << 8))
59 #define PAL_NTOHS(x) (((((unsigned short)(x)) >> 8) & 0xff) | \
60  ((((unsigned short)(x)) & 0xff) << 8) )
61 #define PAL_HTONL(x) ((((x)>>24) & 0xffL) | (((x)>>8) & 0xff00L) | \
62  (((x)<<8) & 0xff0000L) | (((x)<<24) & 0xff000000L))
63 #define PAL_NTOHL(x) ((((x)>>24) & 0xffL) | (((x)>>8) & 0xff00L) | \
64  (((x)<<8) & 0xff0000L) | (((x)<<24) & 0xff000000L))
65 
66 #elif defined(BIG__ENDIAN)
67 
68 #define PAL_HTONS(x) (x)
69 #define PAL_NTOHS(x) (x)
70 #define PAL_HTONL(x) (x)
71 #define PAL_NTOHL(x) (x)
72 #else
73 #error neither BIG__ENDIAN nor LITTLE__ENDIAN defined, cannot compile
74 #endif
75 
76 
77 
78 #define PAL_INVERSE_UINT16_BYTES( val ) \
79  ( ((val) << 8) | (((val) & 0x0000FF00) >> 8))
80 
81 #define PAL_INVERSE_UINT32_BYTES( val ) \
82  ( ((val) >> 24) | (((val) & 0x00FF0000) >> 8) | (((val) & 0x0000FF00) << 8) | (((val) & 0x000000FF) << 24) )
83 
84 #define PAL_INVERSE_UINT64_BYTES( val ) \
85  ((PAL_INVERSE_UINT32_BYTES( ((val >> 16) >> 16)) &0xffffffff) | ((((uint64_t)PAL_INVERSE_UINT32_BYTES(val & 0xffffffff))<<16)<<16))
86 
87 /* Set of Macros similar to the HTONS/L, NTOHS/L ones but converting to/from little endian instead of big endian*/
88 #ifdef LITTLE__ENDIAN
89 #define PAL_LITTLE_ENDIAN_TO_HOST_16BIT(x) (x)
90 #define PAL_LITTLE_ENDIAN_TO_HOST_32BIT(x) (x)
91 #define PAL_LITTLE_ENDIAN_TO_HOST_64BIT(x) (x)
92 #define PAL_HOST_TO_LITTLE_ENDIAN_16BIT(x) (x)
93 #define PAL_HOST_TO_LITTLE_ENDIAN_32BIT(x) (x)
94 #define PAL_HOST_TO_LITTLE_ENDIAN_64BIT(x) (x)
95 
96 
97 #elif defined(BIG__ENDIAN)
98 #define PAL_LITTLE_ENDIAN_TO_HOST_16BIT(x) (PAL_INVERSE_UINT16_BYTES(((uint16_t)x)))
99 #define PAL_LITTLE_ENDIAN_TO_HOST_32BIT(x) (PAL_INVERSE_UINT32_BYTES(((uint32_t)x)))
100 #define PAL_LITTLE_ENDIAN_TO_HOST_64BIT(x) (PAL_INVERSE_UINT64_BYTES(((uint64_t)x)))
101 #define PAL_HOST_TO_LITTLE_ENDIAN_16BIT(x) (PAL_INVERSE_UINT16_BYTES(((uint16_t)x)))
102 #define PAL_HOST_TO_LITTLE_ENDIAN_32BIT(x) (PAL_INVERSE_UINT32_BYTES(((uint32_t)x)))
103 #define PAL_HOST_TO_LITTLE_ENDIAN_64BIT(x) (PAL_INVERSE_UINT64_BYTES(((uint64_t)x)))
104 
105 #else
106 #error neither BIG__ENDIAN nor LITTLE__ENDIAN defined, cannot compile
107 #endif
108 
109 
110 #define PAL_MODULE_INIT(INIT) INIT= 1
111 #define PAL_MODULE_DEINIT(INIT) INIT= 0
112 
113 #ifdef DEBUG
114 #include "pal.h"
115 #define DEBUG_PRINT(ARGS...) PAL_PRINTF(ARGS)
116 
117 #define DEBUG_PRINT(ARGS...) PAL_PRINTF(ARGS)
118 #define PAL_MODULE_IS_INIT(INIT) if(!INIT) return PAL_ERR_NOT_INITIALIZED;
119 
120 
121 #else
122 #define PAL_MODULE_IS_INIT(INIT)
123 
124 #define DEBUG_PRINT(ARGS...)
125 
126 #endif //DEBUG
127 
128 #ifdef __cplusplus
129 }
130 #endif
131 #endif //_PAL_MACROS_H