Contiki 2.5
platform-common.h
Go to the documentation of this file.
1 /** @file hal/micro/generic/compiler/platform-common.h
2  * See @ref platform_common for detailed documentation.
3  *
4  * <!--(C) COPYRIGHT 2010 STMicroelectronics. All rights reserved. -->
5  */
6 
7 /** @addtogroup platform_common
8  * @brief Compiler and Platform specific definitions and typedefs common to
9  * all platforms.
10  *
11  * platform-common.h provides PLATFORM_HEADER defaults and common definitions.
12  * This head should never be included directly, it should only be included
13  * by the specific PLATFORM_HEADER used by your platform.
14  *
15  * See platform-common.h for source code.
16  *@{
17  */
18 
19 #ifndef PLATCOMMONOKTOINCLUDE
20  // This header should only be included by a PLATFORM_HEADER
21  #error platform-common.h should not be included directly
22 #endif
23 
24 #ifndef __PLATFORMCOMMON_H__
25 #define __PLATFORMCOMMON_H__
26 ////////////////////////////////////////////////////////////////////////////////
27 // Many of the common definitions must be explicitly enabled by the
28 // particular PLATFORM_HEADER being used
29 ////////////////////////////////////////////////////////////////////////////////
30 
31 
32 ////////////////////////////////////////////////////////////////////////////////
33 #ifdef _HAL_USE_COMMON_PGM_
34  /** \name Master Program Memory Declarations
35  * These are a set of defines for simple declarations of program memory.
36  */
37  //@{
38  /**
39  * @brief Standard program memory delcaration.
40  */
41  #define PGM const
42 
43  /**
44  * @brief Char pointer to program memory declaration.
45  */
46  #define PGM_P const char *
47 
48  /**
49  * @brief Unsigned char pointer to program memory declaration.
50  */
51  #define PGM_PU const unsigned char *
52 
53 
54  /**
55  * @brief Sometimes a second PGM is needed in a declaration. Having two
56  * 'const' declarations generates a warning so we have a second PGM that turns
57  * into nothing under gcc.
58  */
59  #define PGM_NO_CONST
60  //@} \\END MASTER PROGRAM MEMORY DECLARATIONS
61 #endif //_HAL_USE_COMMON_PGM_
62 
63 
64 ////////////////////////////////////////////////////////////////////////////////
65 #ifdef _HAL_USE_COMMON_DIVMOD_
66  /** \name Divide and Modulus Operations
67  * Some platforms can perform divide and modulus operations on 32 bit
68  * quantities more efficiently when the divisor is only a 16 bit quantity.
69  * C compilers will always promote the divisor to 32 bits before performing the
70  * operation, so the following utility functions are instead required to take
71  * advantage of this optimisation.
72  */
73  //@{
74  /**
75  * @brief Provide a portable name for the int32u by int16u division
76  * library function (which can perform the division with only a single
77  * assembly instruction on some platforms)
78  */
79  #define halCommonUDiv32By16(x, y) ((int16u) (((int32u) (x)) / ((int16u) (y))))
80 
81  /**
82  * @brief Provide a portable name for the int32s by int16s division
83  * library function (which can perform the division with only a single
84  * assembly instruction on some platforms)
85  */
86  #define halCommonSDiv32By16(x, y) ((int16s) (((int32s) (x)) / ((int16s) (y))))
87 
88  /**
89  * @brief Provide a portable name for the int32u by int16u modulo
90  * library function (which can perform the division with only a single
91  * assembly instruction on some platforms)
92  */
93  #define halCommonUMod32By16(x, y) ((int16u) (((int32u) (x)) % ((int16u) (y))))
94 
95  /**
96  * @brief Provide a portable name for the int32s by int16s modulo
97  * library function (which can perform the division with only a single
98  * assembly instruction on some platforms)
99  */
100  #define halCommonSMod32By16(x, y) ((int16s) (((int32s) (x)) % ((int16s) (y))))
101  //@} \\END DIVIDE and MODULUS OPERATIONS
102 #endif //_HAL_USE_COMMON_DIVMOD_
103 
104 
105 ////////////////////////////////////////////////////////////////////////////////
106 #ifdef _HAL_USE_COMMON_MEMUTILS_
107  /** \name C Standard Library Memory Utilities
108  * These should be used in place of the standard library functions.
109  *
110  * These functions have the same parameters and expected results as their C
111  * Standard Library equivalents but may take advantage of certain implementation
112  * optimizations.
113  *
114  * Unless otherwise noted, these functions are utilized by the StStack and are
115  * therefore required to be implemented in the HAL. Additionally, unless otherwise
116  * noted, applications that find these functions useful may utilze them.
117  */
118  //@{
119 
120  /**
121  * @brief Refer to the C stdlib memcpy().
122  */
123  void halCommonMemCopy(void *dest, const void *src, int8u bytes);
124 
125 
126  /**
127  * @brief Refer to the C stdlib memset().
128  */
129  void halCommonMemSet(void *dest, int8u val, int16u bytes);
130 
131 
132  /**
133  * @brief Refer to the C stdlib memcmp().
134  */
135  int8s halCommonMemCompare(const void *source0, const void *source1, int8u bytes);
136 
137 
138  /**
139  * @brief Friendly convenience macro pointing to the full HAL function.
140  */
141  #define MEMSET(d,v,l) halCommonMemSet(d,v,l)
142  #define MEMCOPY(d,s,l) halCommonMemCopy(d,s,l)
143  #define MEMCOMPARE(s0,s1,l) halCommonMemCompare(s0, s1, l)
144  #define MEMPGMCOMPARE(s0,s1,l) halCommonMemPGMCompare(s0, s1, l)
145 
146  //@} // end of C Standard Library Memory Utilities
147 #endif //_HAL_USE_COMMON_MEMUTILS_
148 
149 
150 
151 
152 
153 
154 
155 
156 
157 ////////////////////////////////////////////////////////////////////////////////
158 // The following sections are common on all platforms
159 ////////////////////////////////////////////////////////////////////////////////
160 
161 ////////////////////////////////////////////////////////////////////////////////
162 /**
163  * @name Generic Types
164  *@{
165  */
166 
167 /**
168  * @brief An alias for one, used for clarity.
169  */
170 #define TRUE 1
171 
172 /**
173  * @brief An alias for zero, used for clarity.
174  */
175 #define FALSE 0
176 
177 #ifndef NULL
178 /**
179  * @brief The null pointer.
180  */
181 #define NULL ((void *)0)
182 #endif
183 
184 //@} \\END Generic Types
185 
186 
187 /**
188  * @name Bit Manipulation Macros
189  */
190 //@{
191 
192 /**
193  * @brief Useful to reference a single bit of a byte.
194  */
195 #define BIT(x) (1U << (x)) // Unsigned avoids compiler warnings re BIT(15)
196 
197 /**
198  * @brief Useful to reference a single bit of an int32u type.
199  */
200 #define BIT32(x) (((int32u) 1) << (x))
201 
202 /**
203  * @brief Sets \c bit in the \c reg register or byte.
204  * @note Assuming \c reg is an IO register, some platforms
205  * can implement this in a single atomic operation.
206 */
207 #define SETBIT(reg, bit) reg |= BIT(bit)
208 
209 /**
210  * @brief Sets the bits in the \c reg register or the byte
211  * as specified in the bitmask \c bits.
212  * @note This is never a single atomic operation.
213  */
214 #define SETBITS(reg, bits) reg |= (bits)
215 
216 /**
217  * @brief Clears a bit in the \c reg register or byte.
218  * @note Assuming \c reg is an IO register, some platforms (such as the AVR)
219  * can implement this in a single atomic operation.
220  */
221 #define CLEARBIT(reg, bit) reg &= ~(BIT(bit))
222 
223 /**
224  * @brief Clears the bits in the \c reg register or byte
225  * as specified in the bitmask \c bits.
226  * @note This is never a single atomic operation.
227  */
228 #define CLEARBITS(reg, bits) reg &= ~(bits)
229 
230 /**
231  * @brief Returns the value of \c bit within the register or byte \c reg.
232 */
233 #define READBIT(reg, bit) (reg & (BIT(bit)))
234 
235 /**
236  * @brief Returns the value of the bitmask \c bits within
237  * the register or byte \c reg.
238  */
239 #define READBITS(reg, bits) (reg & (bits))
240 
241 //@} \\END Bit Manipulation Macros
242 
243 
244 ////////////////////////////////////////////////////////////////////////////////
245 /**
246  * @name Byte Manipulation Macros
247  */
248 //@{
249 
250 /**
251  * @brief Returns the low byte of the 16-bit value \c n as an \c int8u.
252  */
253 #define LOW_BYTE(n) ((int8u)((n) & 0xFF))
254 
255 /**
256  * @brief Returns the high byte of the 16-bit value \c n as an \c int8u.
257  */
258 #define HIGH_BYTE(n) ((int8u)(LOW_BYTE((n) >> 8)))
259 
260 /**
261  * @brief Returns the value built from the two \c int8u
262  * values \c high and \c low.
263  */
264 #define HIGH_LOW_TO_INT(high, low) ( \
265  (( (int16u) (high) ) << 8) + \
266  ( (int16u) ( (low) & 0xFF)) \
267  )
268 
269 /**
270  * @brief Returns the low byte of the 32-bit value \c n as an \c int8u.
271  */
272 #define BYTE_0(n) ((int8u)((n) & 0xFF))
273 
274 /**
275  * @brief Returns the second byte of the 32-bit value \c n as an \c int8u.
276  */
277 #define BYTE_1(n) ((int8u)(BYTE_0((n) >> 8)))
278 
279 /**
280  * @brief Returns the third byte of the 32-bit value \c n as an \c int8u.
281  */
282 #define BYTE_2(n) ((int8u)(BYTE_0((n) >> 16)))
283 
284 /**
285  * @brief Returns the high byte of the 32-bit value \c n as an \c int8u.
286  */
287 #define BYTE_3(n) ((int8u)(BYTE_0((n) >> 24)))
288 
289 //@} \\END Byte manipulation macros
290 
291 
292 ////////////////////////////////////////////////////////////////////////////////
293 /**
294  * @name Time Manipulation Macros
295  */
296 //@{
297 
298 /**
299  * @brief Returns the elapsed time between two 8 bit values.
300  * Result may not be valid if the time samples differ by more than 127
301  */
302 #define elapsedTimeInt8u(oldTime, newTime) \
303  ((int8u) ((int8u)(newTime) - (int8u)(oldTime)))
304 
305 /**
306  * @brief Returns the elapsed time between two 16 bit values.
307  * Result may not be valid if the time samples differ by more than 32767
308  */
309 #define elapsedTimeInt16u(oldTime, newTime) \
310  ((int16u) ((int16u)(newTime) - (int16u)(oldTime)))
311 
312 /**
313  * @brief Returns the elapsed time between two 32 bit values.
314  * Result may not be valid if the time samples differ by more than 2147483647
315  */
316 #define elapsedTimeInt32u(oldTime, newTime) \
317  ((int32u) ((int32u)(newTime) - (int32u)(oldTime)))
318 
319 /**
320  * @brief Returns TRUE if t1 is greater than t2. Can only account for 1 wrap
321  * around of the variable before it is wrong.
322  */
323 #define MAX_INT8U_VALUE 0xFF
324 #define timeGTorEqualInt8u(t1, t2) \
325  (elapsedTimeInt8u(t2, t1) <= ((MAX_INT8U_VALUE + 1) / 2))
326 
327 /**
328  * @brief Returns TRUE if t1 is greater than t2. Can only account for 1 wrap
329  * around of the variable before it is wrong.
330  */
331 #define MAX_INT16U_VALUE 0xFFFF
332 #define timeGTorEqualInt16u(t1, t2) \
333  (elapsedTimeInt16u(t2, t1) <= ((MAX_INT16U_VALUE + 1) / 2))
334 
335 /**
336  * @brief Returns TRUE if t1 is greater than t2. Can only account for 1 wrap
337  * around of the variable before it is wrong.
338  */
339 #define MAX_INT32U_VALUE 0xFFFFFFFF
340 #define timeGTorEqualInt32u(t1, t2) \
341  (elapsedTimeInt32u(t2, t1) <= ((MAX_INT32U_VALUE + 1) / 2))
342 
343 //@} \\END Time manipulation macros
344 
345 
346 
347 #endif //__PLATFORMCOMMON_H__
348 
349 /** @} END addtogroup */
350