Mistake on this page? Email us
pal_macros.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // Copyright 2016-2019 ARM Ltd.
3 //
4 // SPDX-License-Identifier: Apache-2.0
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 //
10 // http://www.apache.org/licenses/LICENSE-2.0
11 //
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 // ----------------------------------------------------------------------------
18 
19 #ifndef _PAL_MACROS_H
20 #define _PAL_MACROS_H
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 //for PAL_LOG prints
27 #include "pal.h"
28 #include "mbed-trace/mbed_trace.h"
29 #include "assert.h"
30 #include <limits.h>
36 // Maximum integer types.
37 #define PAL_MAX_UINT8 0xFFU
38 #define PAL_MAX_UINT16 0xFFFFU
39 #define PAL_MAX_UINT32 0xFFFFFFFFUL
40 #define PAL_MAX_INT32 0x7FFFFFFFL
41 #define PAL_MIN_INT32 0x80000000L
42 #define PAL_MAX_UINT64 0xFFFFFFFFFFFFFFFFULL
43 #define PAL_MAX_INT64 0x7FFFFFFFFFFFFFFFLL
44 
45 // Useful macros.
46 
47 
48 
49 #if defined(__arm__) || defined(__IAR_SYSTEMS_ICC__) // Compile with ARMCC, GCC_ARM or IAR compilers.
50  #define PAL_TARGET_POINTER_SIZE __sizeof_ptr
51  #ifdef __BIG_ENDIAN
52  #define PAL_COMPILATION_ENDIANITY 1
53  #else
54  #define PAL_COMPILATION_ENDIANITY 0 // Define PAL compilation endian (0 is little endian, 1 is big endian).
55  #endif
56 #elif defined(__GNUC__) // Compiling with GCC.
57  #define PAL_TARGET_POINTER_SIZE __SIZEOF_POINTER__
58  #ifdef __BYTE_ORDER
59  #if __BYTE_ORDER == __BIG_ENDIAN // If both are not defined it is TRUE!
60  #define PAL_COMPILATION_ENDIANITY 1 // Define PAL compilation endian (0 is little endian, 1 is big endian).
61  #elif __BYTE_ORDER == __LITTLE_ENDIAN
62  #define PAL_COMPILATION_ENDIANITY 0// Define PAL compilation endian (0 is little endian, 1 is big endian).
63  #else
64  #error missing endiantiy definition for GCC
65  #endif
66  #endif
67 #else
68  #error neither ARM target compilers nor GCC used for compilation - not supported
69 #endif
70 
71 
72 
73 
74 #define PAL_MAX(a,b) ((a) > (b) ? (a) : (b))
75 
76 #define PAL_MIN(a,b) ((a) < (b) ? (a) : (b))
77 
78 #define PAL_DIVIDE_ROUND_UP(num, divider) (((num) + (divider) - 1) / (divider))
79 
80 #if PAL_COMPILATION_ENDIANITY == 1
81 #define BIG__ENDIAN 1
82 #elif PAL_COMPILATION_ENDIANITY == 0
83 #define LITTLE__ENDIAN 1
84 #else
85 #error neither BIG__ENDIAN nor LITTLE__ENDIAN defined, cannot compile
86 #endif
87 
88 
89 // Endianity macros.
90 #ifdef LITTLE__ENDIAN
91 
92 #define PAL_HTONS(x) (((((unsigned short)(x)) >> 8) & 0xff) | \
93  ((((unsigned short)(x)) & 0xff) << 8))
94 #define PAL_NTOHS(x) (((((unsigned short)(x)) >> 8) & 0xff) | \
95  ((((unsigned short)(x)) & 0xff) << 8) )
96 #define PAL_HTONL(x) ((((x)>>24) & 0xffL) | (((x)>>8) & 0xff00L) | \
97  (((x)<<8) & 0xff0000L) | (((x)<<24) & 0xff000000L))
98 #define PAL_NTOHL(x) ((((x)>>24) & 0xffL) | (((x)>>8) & 0xff00L) | \
99  (((x)<<8) & 0xff0000L) | (((x)<<24) & 0xff000000L))
100 
101 #elif defined(BIG__ENDIAN)
102 
103 #define PAL_HTONS(x) (x)
104 #define PAL_NTOHS(x) (x)
105 #define PAL_HTONL(x) (x)
106 #define PAL_NTOHL(x) (x)
107 #else
108 #error neither BIG__ENDIAN nor LITTLE__ENDIAN defined, cannot compile
109 #endif
110 
111 
112 #define PAL_GET_LOWER_8BITS(x) (x & 0xFF)
113 
114 #define PAL_INVERSE_UINT16_BYTES( val ) \
115  ( ((val) << 8) | (((val) & 0x0000FF00) >> 8))
116 
117 #define PAL_INVERSE_UINT32_BYTES( val ) \
118  ( ((val) >> 24) | (((val) & 0x00FF0000) >> 8) | (((val) & 0x0000FF00) << 8) | (((val) & 0x000000FF) << 24) )
119 
120 #define PAL_INVERSE_UINT64_BYTES( val ) \
121  ((PAL_INVERSE_UINT32_BYTES( ((val >> 16) >> 16)) &0xffffffff) | ((((uint64_t)PAL_INVERSE_UINT32_BYTES(val & 0xffffffff))<<16)<<16))
122 
123 /* Set of Macros similar to the HTONS/L, NTOHS/L ones but converting to/from little endian instead of big endian. */
124 #ifdef LITTLE__ENDIAN
125 #define PAL_LITTLE_ENDIAN_TO_HOST_16BIT(x) (x)
126 #define PAL_LITTLE_ENDIAN_TO_HOST_32BIT(x) (x)
127 #define PAL_LITTLE_ENDIAN_TO_HOST_64BIT(x) (x)
128 #define PAL_HOST_TO_LITTLE_ENDIAN_16BIT(x) (x)
129 #define PAL_HOST_TO_LITTLE_ENDIAN_32BIT(x) (x)
130 #define PAL_HOST_TO_LITTLE_ENDIAN_64BIT(x) (x)
131 
132 
133 
134 
135 #elif defined(BIG__ENDIAN)
136 #define PAL_LITTLE_ENDIAN_TO_HOST_16BIT(x) (PAL_INVERSE_UINT16_BYTES(((uint16_t)x)))
137 #define PAL_LITTLE_ENDIAN_TO_HOST_32BIT(x) (PAL_INVERSE_UINT32_BYTES(((uint32_t)x)))
138 #define PAL_LITTLE_ENDIAN_TO_HOST_64BIT(x) (PAL_INVERSE_UINT64_BYTES(((uint64_t)x)))
139 #define PAL_HOST_TO_LITTLE_ENDIAN_16BIT(x) (PAL_INVERSE_UINT16_BYTES(((uint16_t)x)))
140 #define PAL_HOST_TO_LITTLE_ENDIAN_32BIT(x) (PAL_INVERSE_UINT32_BYTES(((uint32_t)x)))
141 #define PAL_HOST_TO_LITTLE_ENDIAN_64BIT(x) (PAL_INVERSE_UINT64_BYTES(((uint64_t)x)))
142 
143 #else
144 #error neither BIG__ENDIAN nor LITTLE__ENDIAN defined, cannot compile
145 #endif
146 
147 
148 #define PAL_MODULE_INIT(INIT) INIT= 1
149 #define PAL_MODULE_DEINIT(INIT) INIT= 0
150 
152 #define PAL_MILISEC_TO_SEC(milisec) (milisec/1000)
153 #define PAL_ONE_SEC 1
154 #define PAL_SECONDS_PER_MIN 60
155 #define PAL_MINUTES_PER_HOUR 60
156 #define PAL_HOURS_PER_DAY 24
157 #define PAL_SECONDS_PER_HOUR PAL_MINUTES_PER_HOUR * PAL_SECONDS_PER_MIN
158 #define PAL_SECONDS_PER_DAY PAL_HOURS_PER_DAY * PAL_SECONDS_PER_HOUR
159 #define PAL_DAYS_IN_A_YEAR (365U)
160 #define PAL_RATIO_SECONDS_PER_DAY 480
161 #define PAL_MINIMUM_RTC_LATENCY_SEC 100
162 #define PAL_MINIMUM_STORAGE_LATENCY_SEC 500000
163 #define PAL_MINIMUM_FORWARD_LATENCY_SEC 100000
164 #define PAL_MINIMUM_BACKWARD_LATENCY_SEC 100
165 #define PAL_FEB_MONTH 2
166 #define PAL_MILLI_PER_SECOND 1000
167 #define PAL_NANO_PER_MILLI 1000000L
168 #define PAL_NANO_PER_SECOND 1000000000L
169 #define PAL_MILLI_TO_NANO(x) (((x) % PAL_MILLI_PER_SECOND) * PAL_NANO_PER_MILLI)
170 #define PAL_MILISEC_TO_SEC(milisec) (milisec/1000)
171 #define PAL_MIN_SEC_FROM_EPOCH 1483264800
172 #define PAL_MIN_RTC_SET_TIME PAL_MIN_SEC_FROM_EPOCH
173 #define PAL_LAST_SAVED_TIME_LATENCY_SEC 2500000
176 #if defined (__CC_ARM) /* ARM compiler. */
177  #define PAL_INLINE __inline
178 #elif defined (__GNUC__) /* GNU compiler. */
179  #define PAL_INLINE __attribute__((always_inline)) __inline
180 #else
181  #define PAL_INLINE
182 #endif
184 #define PAL_PRIVATE static
185 
186 #if defined (__CC_ARM) /* ARM compiler. */
187 #define PAL_PRAGMA(x)
188 #define PAL_DEPRECATED(x)
189 #else
190 #define PAL_PRAGMA(x) _Pragma (#x)
191 #define PAL_DEPRECATED(x) PAL_PRAGMA(message ("!!! PAL DEPRECATED CODE- " #x))
192 #endif
194 #ifdef DEBUG
195 
196 #define PAL_MODULE_IS_INIT(INIT) if(!INIT) return PAL_ERR_NOT_INITIALIZED;
197 
198 
199 #else
200 #define PAL_MODULE_IS_INIT(INIT) (void)INIT
201 
202 #endif //DEBUG
203 
204 // Compile time assert.
205 #define PAL_ASSERT_STATIC(e) \
206  do { \
207  enum { assert_static__ = 1/(e) }; \
208  } while (0)
209 
210 #define PAL_UNUSED_ARG(x) (void)(x)
211 
213 
214 
215 
216 //for non recoverable errors
217 #define PAL_LOG_ASSERT( ARGS...) \
218 { \
219  tr_err(ARGS); \
220  assert(0);\
221 }
222 
223 #define PAL_LOG_ERR_FUNC tr_err
224 #define PAL_LOG_WARN_FUNC tr_warn
225 #define PAL_LOG_INFO_FUNC tr_info
226 #define PAL_LOG_DBG_FUNC tr_debug
227 #define PAL_LOG_ARRAY_FUNC tr_array
229 // Little trick with mbed-trace error level is equal to function name handling the same level of log output
230 #define PAL_LOG_LEVEL_ERR TRACE_LEVEL_ERROR
231 #define PAL_LOG_LEVEL_WARN TRACE_LEVEL_WARN
232 #define PAL_LOG_LEVEL_INFO TRACE_LEVEL_INFO
233 #define PAL_LOG_LEVEL_DBG TRACE_LEVEL_DEBUG
235 #define PAL_LOG_ERR( ARGS...) PAL_LOG_ERR_FUNC(ARGS);
236 #define PAL_LOG_WARN( ARGS...) PAL_LOG_WARN_FUNC(ARGS);
237 #define PAL_LOG_INFO( ARGS...) PAL_LOG_INFO_FUNC(ARGS);
238 #define PAL_LOG_DBG( ARGS...) PAL_LOG_DBG_FUNC(ARGS);
241 #ifdef DEBUG
242 #ifdef VERBOSE
243 #define PAL_PRINTF( ARGS...) \
244  #define PAL_PRINTF(fmt, ...) PAL_LOG_DBG("%s:%d: " fmt, __FUNCTION__, __LINE__, ##__VA_ARGS__);
245 #else
246 #define PAL_PRINTF( ARGS...) \
247  PAL_LOG_DBG(ARGS);
248 #endif
249 #else
250  #define PAL_PRINTF( ARGS...)
251 #endif
253 #define DEBUG_PRINT(ARGS...) PAL_PRINTF(ARGS)
254 
255 #define PAL_PTR_ADDR_ALIGN_UINT8_TO_UINT32 __attribute__((aligned(4)))
256 
257 #define PAL_INT32_BITS (sizeof(int32_t) * CHAR_BIT)
258 
259 #ifdef DEBUG
260 
261 
262 #define PAL_VALIDATE_CONDITION_WITH_ERROR(condition, error) \
263  {\
264  if ((condition)) \
265  { \
266  PAL_LOG_ERR("(%s,%d): Parameters values is illegal\r\n",__FUNCTION__,__LINE__); \
267  return error; \
268  } \
269  }
270 #define PAL_VALIDATE_ARGUMENTS(condition) PAL_VALIDATE_CONDITION_WITH_ERROR(condition,PAL_ERR_INVALID_ARGUMENT)
271 
272 #else
273  #define PAL_VALIDATE_ARGUMENTS(condition)
274  #define PAL_VALIDATE_CONDITION_WITH_ERROR(condition, error)
275 #endif
277 
278 #define PAL_VALIDATE_ARG_RLZ(condition, error) \
279 {\
280  if ((condition)) \
281  { \
282  return error; \
283  } \
284 }
285 
286 
287 
288 #ifdef __cplusplus
289 }
290 #endif
291 #endif //_PAL_MACROS_H
PAL. This file contains the general API to initiate and destroy the PAL component. This is part of the PAL service API.