4 * Copyright 2003, 2004 Develer S.r.l. (http://www.develer.com/)
5 * Copyright 2001, 2002, 2003 Bernardo Innocenti <bernie@codewiz.org>
6 * This file is part of DevLib - See devlib/README for information.
11 * \author Bernardo Innocenti <bernie@develer.com>
13 * \brief Additional support macros for compiler independance
18 * Revision 1.7 2004/07/20 23:26:48 bernie
19 * Fix two errors introduced by previous commit.
21 * Revision 1.6 2004/07/20 23:12:43 bernie
22 * *** empty log message ***
24 * Revision 1.5 2004/07/20 17:08:03 bernie
25 * Cleanup documentation
27 * Revision 1.4 2004/06/27 15:20:26 aleph
28 * Change UNUSED() macro to accept two arguments: type and name;
29 * Add macro GNUC_PREREQ to detect GCC version during build;
30 * Some spacing cleanups and typo fix
32 * Revision 1.3 2004/06/06 18:00:39 bernie
33 * PP_CAT(): New macro.
35 * Revision 1.2 2004/06/03 11:27:09 bernie
36 * Add dual-license information.
38 * Revision 1.1 2004/05/23 17:48:35 bernie
39 * Add top-level files.
45 #include "arch_config.h"
48 #if defined __GNUC__ && defined __GNUC_MINOR__
49 #define GNUC_PREREQ(maj, min) \
50 ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
52 #define GNUC_PREREQ(maj, min) 0
55 #if defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__)
56 #pragma language=extended
57 #define INTERRUPT(x) interrupt [x]
58 #define REGISTER shortad
59 #define INLINE /* unsupported */
61 /* Imported from <longjmp.h>. Unfortunately, we can't just include
62 * this header because it typedefs jmp_buf to be an array of chars.
63 * This would allow the compiler to place the buffer on an odd address.
64 * The CPU _should_ be able to perform word accesses to
65 * unaligned data, but there are *BUGS* in the 80196KC with
66 * some combinations of opcodes and addressing modes. One of
67 * these, "ST SP,[?GR]+" is used in the longjmp() implementation
68 * provided by the IAR compiler ANSI C library. When ?GR contains
69 * an odd address, surprisingly the CPU will copy the high order
70 * byte of the source operand (SP) in the low order byte of the
71 * destination operand (the memory location pointed to by ?GR).
73 * We also need to replace the library setjmp()/longjmp() with
74 * our own versions because the IAR implementation "forgets" to
75 * save the contents of local registers (?LR).
79 void * sp; /* Stack pointer */
80 void * return_addr; /* Return address */
81 int lr[6]; /* 6 local registers */
84 typedef struct _JMP_BUF jmp_buf[1];
86 int setjmp(jmp_buf env);
87 void longjmp(jmp_buf env, int val);
89 /* Fake bool support */
92 typedef unsigned char bool;
94 #elif defined(_MSC_VER) /* Win32 emulation support */
97 #include <time.h> /* for time_t */
100 /* Ouch, ReleaseSemaphore() conflicts with a WIN32 call ;-( */
101 #define ReleaseSemaphore KReleaseSemaphore
103 /* Fake bool support */
108 #endif /* !__cplusplus */
110 #elif defined(__GNUC__)
113 #define FORMAT(type,fmt,first) __attribute__((__format__(type, fmt, first)))
114 #define NORETURN __attribute__((__noreturn__))
115 #define UNUSED(type,arg) __attribute__((__unused__)) type arg
116 #define INLINE extern inline
118 #define DEPRECATED __attribute__((__deprecated__))
121 #if defined(__i386__)
123 /* hack to avoid conflicts with system type */
124 #define sigset_t system_sigset_t
128 #undef system_sigset_t
130 #elif defined(__AVR__)
135 /* Missing printf-family functions in avr-libc/stdio.h */
137 #include <avr/pgmspace.h>
138 #if !GNUC_PREREQ(3,4)
139 int vsprintf(char *buf, const char *fmt, va_list ap);
141 int vsprintf_P(char *buf, const char * PROGMEM fmt, va_list ap);
143 /* Support for harvard architectures */
145 #define PGM_READ_CHAR(s) pgm_read_byte(s)
146 #define PGM_FUNC(x) x ## _P
147 #define PGM_ATTR PROGMEM
152 #elif defined(__MWERKS__) && (defined(__m56800E__) || defined(__m56800__))
160 #error unknown compiler
164 /* A few defaults for missing compiler features. */
166 #define INLINE static inline
169 #define NORETURN /* nothing */
172 #define FORMAT(type,fmt,first) /* nothing */
175 #define DEPRECATED /* nothing */
178 #define UNUSED(type,arg) type arg
181 #define REGISTER /* nothing */
184 #define INTERRUPT(x) ERROR_NOT_IMPLEMENTED
187 /* Support for harvard architectures */
189 #define PSTR /* nothing */
191 #ifndef PGM_READ_CHAR
192 #define PGM_READ_CHAR(s) (*(s))
195 #define PGM_FUNC(x) x
198 #define PGM_ATTR /* nothing */
202 /* Misc definitions */
211 /* Support for hybrid C/C++ applications. */
213 #define EXTERN_C_BEGIN extern "C" {
214 #define EXTERN_C_END }
216 #define EXTERN_C_BEGIN /* nothing */
217 #define EXTERN_C_END /* nothing */
221 /* Quasi-ANSI macros */
223 /*! offsetof(s,m) - Return the byte offset of the member \a m in struct \a s */
224 #define offsetof(s,m) (size_t)&(((s *)0)->m)
227 /*! Count the number of elements in the static array \a a */
228 #define countof(a) (sizeof(a) / sizeof(*(a)))
233 #define ABS(a) (((a) < 0) ? -(a) : (a))
234 #define MIN(a,b) (((a) < (b)) ? (a) : (b))
235 #define MAX(a,b) (((a) > (b)) ? (a) : (b))
238 /*! Convert a bit value to a binary flag */
239 #define BV(x) (1<<(x))
242 /*! Round up \a x to an even multiple of the 2's power \a pad */
243 #define ROUND2(x, pad) (((x) + ((pad) - 1)) & ~((pad) - 1))
245 /*! Calculate a compile-time log2 for a uint8_t */
246 #define UINT8_LOG2(x) \
253 ((x) < 128 ? 6 : 7)))))))
255 /*! Calculate a compile-time log2 for a uint16_t */
256 #define UINT16_LOG2(x) \
257 ((x < 256) ? UINT8_LOG2(x) : UINT8_LOG2((x) >> 8) + 8)
259 /*! Calculate a compile-time log2 for a uint32_t */
260 #define UINT32_LOG2(x) \
261 ((x < 65536UL) ? UINT16_LOG2(x) : UINT16_LOG2((x) >> 16) + 16)
263 /*! Concatenate two different preprocessor tokens (allowing macros to expand) */
264 #define PP_CAT(x,y) PP_CAT__(x,y)
265 #define PP_CAT__(x,y) x ## y
269 * Standard type definitions
270 * These should be in <sys/types.h>, but many compilers lack them.
272 #if !(defined(size_t) || defined(_SIZE_T_DEFINED))
273 #define size_t unsigned int
275 #if !(defined(_TIME_T_DEFINED) || defined(__time_t_defined))
277 #endif /* _TIME_T_DEFINED || __time_t_defined */
279 /*! Storage for pointers and integers */
282 typedef long utime_t;
283 typedef unsigned char sig_t;
284 typedef unsigned char sigset_t;
285 typedef unsigned char page_t;
287 #if (defined(_MSC_VER) || defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__))
289 * ISO C99 fixed-size types
290 * These should be in <stdint.h>, but many compilers lack them.
292 typedef signed char int8_t;
293 typedef short int int16_t;
294 typedef long int int32_t;
295 typedef unsigned char uint8_t;
296 typedef unsigned short int uint16_t;
297 typedef unsigned long int uint32_t;
298 #elif defined(__AVR__)
299 /* avr-libc is weird... */
300 #include <inttypes.h>
302 /* This is the correct location. */
307 * \name Types for hardware registers.
309 * Only use these types for registers whose contents can
310 * be changed asynchronously by external hardware.
314 #if (defined(__m56800E__) || defined(__m56800__))
315 /* Registers can be accessed only through 16-bit pointers */
316 typedef volatile uint16_t reg16_t;
318 typedef volatile uint8_t reg8_t;
319 typedef volatile uint16_t reg16_t;
320 typedef volatile uint32_t reg32_t;
324 #endif /* COMPILER_H */