Contiki 3.x
gnu.h
Go to the documentation of this file.
1 /** @file hal/micro/cortexm3/compiler/gnu.h
2  * See @ref gnu for detailed documentation.
3  *
4  */
5 
6 /**
7  * @addtogroup stm32w-cpu
8  * @{ */
9 
10 /** @defgroup gnu
11  * @brief Compiler and Platform specific definitions and typedefs for the
12  * GNU C ARM compiler.
13  *
14  * @note gnu.h should be included first in all source files by setting the
15  * preprocessor macro PLATFORM_HEADER to point to it. gnu.h automatically
16  * includes platform-common.h.
17  *
18  * See gnu.h and platform-common.h for source code.
19  *@{
20  */
21 
22 #ifndef GNU_H_
23 #define GNU_H_
24 
25 #include <stdint.h>
26 
27 
28 #ifndef __GNUC__
29  #error Improper PLATFORM_HEADER
30 #endif
31 
32 #if (__GNUC__ < 4)
33  #error Only GNU C versions later than 4.3.2 are supported
34 #elif (__GNUC__ == 4)
35  #if (__GNUC_MINOR__ < 3)
36  #error Only GNU C versions later than 4.3.2 are supported
37  #elif (__GNUC_MINOR__ == 3)
38  #if (__GNUC_PATCHLEVEL__ < 2)
39  #error Only GNU C versions later than 4.3.2 are supported
40  #endif
41  #endif
42 #endif // __GNUC__
43 
44 
45 #ifndef DOXYGEN_SHOULD_SKIP_THIS
46 //#include <intrinsics.h>
47  #include <stdarg.h>
48  #if defined (CORTEXM3_STM32W108)
49  #include "micro/cortexm3/stm32w108/regs.h"
50  #include "micro/cortexm3/stm32w108/stm32w108-type.h"
51  #else
52  #error Unknown CORTEXM3 micro
53  #endif
54  //Provide a default NVIC configuration file. The build process can
55  //override this if it needs to.
56  #ifndef NVIC_CONFIG
57  #define NVIC_CONFIG "hal/micro/cortexm3/nvic-config.h"
58  #endif
59 //[[
60 #ifdef EMU_TEST
61  #ifdef I_AM_AN_EMULATOR
62  // This register is defined for both the chip and the emulator with
63  // with distinct reset values. Need to undefine to avoid preprocessor
64  // collision.
65  #undef DATA_EMU_REGS_BASE
66  #undef DATA_EMU_REGS_END
67  #undef DATA_EMU_REGS_SIZE
68  #undef I_AM_AN_EMULATOR
69  #undef I_AM_AN_EMULATOR_REG
70  #undef I_AM_AN_EMULATOR_ADDR
71  #undef I_AM_AN_EMULATOR_RESET
72  #undef I_AM_AN_EMULATOR_I_AM_AN_EMULATOR
73  #undef I_AM_AN_EMULATOR_I_AM_AN_EMULATOR_MASK
74  #undef I_AM_AN_EMULATOR_I_AM_AN_EMULATOR_BIT
75  #undef I_AM_AN_EMULATOR_I_AM_AN_EMULATOR_BITS
76  #endif//I_AM_AN_EMULATOR
77  #error MICRO currently not supported for emulator builds.
78 #endif//EMU_TEST
79 //]]
80 
81 // suppress warnings about unknown pragmas
82 // (as they may be pragmas known to other platforms)
83 //#pragma diag_suppress = pe161
84 
85 #endif // DOXYGEN_SHOULD_SKIP_THIS
86 
87 // Define that the minimal hal is being used.
88 #define MINIMAL_HAL
89 
90 
91 /** \name Master Variable Types
92  * These are a set of typedefs to make the size of all variable declarations
93  * explicitly known.
94  */
95 //@{
96 /**
97  * @brief A typedef to make the size of the variable explicitly known.
98  */
99 typedef unsigned char boolean;
100 //typedef unsigned char uint8_t;
101 //typedef signed char int8_t;
102 //typedef unsigned short uint16_t;
103 //typedef signed short int16_t;
104 //typedef unsigned int uint32_t;
105 //typedef signed int int32_t;
106 typedef unsigned int PointerType;
107 //@} \\END MASTER VARIABLE TYPES
108 
109 /**
110  * @brief Use the Master Program Memory Declarations from platform-common.h
111  */
112 #define _HAL_USE_COMMON_PGM_
113 
114 
115 
116 ////////////////////////////////////////////////////////////////////////////////
117 /** \name Miscellaneous Macros
118  */
119 ////////////////////////////////////////////////////////////////////////////////
120 //@{
121 
122 /**
123  * @brief A convenient method for code to know what endiannes processor
124  * it is running on. For the Cortex-M3, we are little endian.
125  */
126 #define BIGENDIAN_CPU FALSE
127 
128 
129 /**
130  * @brief A friendlier name for the compiler's intrinsic for not
131  * stripping.
132  */
133 #define NO_STRIPPING /* __root ??? */
134 #define __no_init /*__attribute__((section (".noinit")))*/
135 
136 /**
137  * @brief A friendlier name for the compiler's intrinsic for eeprom
138  * reference.
139  */
140 #define EEPROM errorerror
141 
142 
143 #ifndef __SOURCEFILE__
144  /**
145  * @brief The __SOURCEFILE__ macro is used by asserts to list the
146  * filename if it isn't otherwise defined, set it to the compiler intrinsic
147  * which specifies the whole filename and path of the sourcefile
148  */
149  #define __SOURCEFILE__ __FILE__
150 #endif
151 
152 
153 //#include <assert.h>
154 #undef assert
155 #define assert(condition) do { if (! (condition)) {while (1); }} while(0)
156 #if 0
157  do { if (! (condition)) { \
158  printf ("Assert failed %s %d\r\n",__SOURCEFILE__, __LINE__); }} while(0)
159 #endif
160 
161 #ifndef BOOTLOADER
162  #undef __delay_cycles
163  /**
164  * @brief __delay_cycles() is an intrinsic IAR call; however, we
165  * have explicity disallowed it since it is too specific to the system clock.
166  * \note Please use halCommonDelayMicroseconds() instead, because it correctly
167  * accounts for various system clock speeds.
168  */
169  #define __delay_cycles(x) please_use_halCommonDelayMicroseconds_instead_of_delay_cycles
170 #endif
171 
172 /**
173  * @brief Set debug level based on whether or DEBUG is defined.
174  * basic debugging support is included if DEBUG is not defined.
175  */
176 #ifndef DEBUG_LEVEL
177  #ifdef DEBUG
178  #define DEBUG_LEVEL FULL_DEBUG
179  #else
180  #define DEBUG_LEVEL BASIC_DEBUG
181  #endif
182 #endif
183 
184 /**
185  * @brief Set the application start and end address.
186  * This are useful to detect whether an image is for bootloader mode or not.
187  * This can be used also to clone image to another node via bootloader.
188  */
189 #define APPLICATION_IMAGE_START ((u32) &__ApplicationFlashStart)
190 #define APPLICATION_IMAGE_END ((u32) &__ApplicationFlashEnd)
191 extern char __ApplicationFlashStart;
192 extern char __ApplicationFlashEnd;
193 
194 /**
195  * @brief Macro to reset the watchdog timer. Note: be very very
196  * careful when using this as you can easily get into an infinite loop if you
197  * are not careful.
198  */
199 void halInternalResetWatchDog(void);
200 #define halResetWatchdog() halInternalResetWatchDog()
201 
202 
203 /**
204  * @brief Define __attribute__ to nothing since it isn't handled by IAR.
205  */
206 
207 
208 /**
209  * @brief Declare a variable as unused to avoid a warning. Has no effect
210  * in IAR builds
211  */
212 #define UNUSED
213 
214 /**
215  * @brief Some platforms need to cast enum values that have the high bit set.
216  */
217 #define SIGNED_ENUM
218 
219 
220 /**
221  * @brief Define the magic value that is interpreted by IAR C-SPY's Stack View.
222  */
223 #define STACK_FILL_VALUE 0xCDCDCDCD
224 
225 /**
226  * @brief Define a generic RAM function identifier to a compiler specific one.
227  */
228 #ifdef RAMEXE
229  //If the whole build is running out of RAM, as chosen by the RAMEXE build
230  //define, then define RAMFUNC to nothing since it's not needed.
231  #define RAMFUNC
232 #else //RAMEXE
233  #define RAMFUNC __ramfunc
234 #endif //RAMEXE
235 
236 /**
237  * @brief Define a generic no operation identifier to a compiler specific one.
238  */
239 #define NO_OPERATION() __no_operation()
240 
241 /**
242  * @brief A convenience macro that makes it easy to change the field of a
243  * register to any value.
244  */
245 #define SET_REG_FIELD(reg, field, value) \
246  do{ \
247  reg = ((reg & (~field##_MASK)) | (value << field##_BIT)); \
248  }while(0)
249 
250 /**
251  * @brief Stub for code not running in simulation.
252  */
253 #define simulatedTimePasses()
254 /**
255  * @brief Stub for code not running in simulation.
256  */
257 #define simulatedTimePassesMs(x)
258 /**
259  * @brief Stub for code not running in simulation.
260  */
261 #define simulatedSerialTimePasses()
262 
263 
264 /**
265  * @brief Use the Divide and Modulus Operations from platform-common.h
266  */
267 #define _HAL_USE_COMMON_DIVMOD_
268 
269 
270 /**
271  * @brief Provide a portable way to specify the segment where a variable
272  * lives.
273  */
274 #define VAR_AT_SEGMENT(__variableDeclaration, __segmentName) \
275  __variableDeclaration __attribute__ ((section (__segmentName)))
276 
277 #define ALIGN_VAR(__variableDeclaration, alignment) \
278  __variableDeclaration __attribute__ ((aligned(alignment)))
279 
280 ////////////////////////////////////////////////////////////////////////////////
281 //@} // end of Miscellaneous Macros
282 ////////////////////////////////////////////////////////////////////////////////
283 
284 /** @name Portable segment names
285  *@{
286  */
287 /**
288  * @brief Portable segment names
289  */
290 #define __NO_INIT__ ".noinit"
291 #define __INTVEC__ ".intvec"
292 #define __CSTACK__ "CSTACK"
293 #define __DATA_INIT__ ".data_init"
294 #define __DATA__ ".data"
295 #define __BSS__ ".bss"
296 #define __CONST__ ".rodata"
297 #define __TEXT__ ".text"
298 #define __TEXTRW_INIT__ ".textrw_init"
299 #define __TEXTRW__ ".textrw"
300 #define __FAT__ "FAT" // Fixed address table
301 #define __NVM__ "NVM" //Non-Volatile Memory data storage
302 
303 //=============================================================================
304 // The '#pragma segment=' declaration must be used before attempting to access
305 // the segments so the compiler properly handles the __segment_*() functions.
306 //
307 // The segment names used here are the default segment names used by IAR. Refer
308 // to the IAR Compiler Reference Guide for a proper description of these
309 // segments.
310 //=============================================================================
311 #if 0
312 #pragma segment=__NO_INIT__
313 #pragma segment=__INTVEC__
314 #pragma segment=__CSTACK__
315 #pragma segment=__DATA_INIT__
316 #pragma segment=__DATA__
317 #pragma segment=__BSS__
318 #pragma segment=__CONST__
319 #pragma segment=__TEXT__
320 #pragma segment=__TEXTRW_INIT__
321 #pragma segment=__TEXTRW__
322 #pragma segment=__FAT__
323 #pragma segment=__NVM__
324 #endif
325 /**@} */
326 
327 //A utility function for inserting barrier instructions. These
328 //instructions should be used whenever the MPU is enabled or disabled so
329 //that all memory/instruction accesses can complete before the MPU changes
330 //state.
331 void _executeBarrierInstructions(void);
332 
333 ////////////////////////////////////////////////////////////////////////////////
334 /** \name Global Interrupt Manipulation Macros
335  *
336  * \b Note: The special purpose BASEPRI register is used to enable and disable
337  * interrupts while permitting faults.
338  * When BASEPRI is set to 1 no interrupts can trigger. The configurable faults
339  * (usage, memory management, and bus faults) can trigger if enabled as well as
340  * the always-enabled exceptions (reset, NMI and hard fault).
341  * When BASEPRI is set to 0, it is disabled, so any interrupt can triggger if
342  * its priority is higher than the current priority.
343  */
344 ////////////////////////////////////////////////////////////////////////////////
345 //@{
346 
347 #define ATOMIC_LITE(blah) ATOMIC(blah)
348 #define DECLARE_INTERRUPT_STATE_LITE DECLARE_INTERRUPT_STATE
349 #define DISABLE_INTERRUPTS_LITE() DISABLE_INTERRUPTS()
350 #define RESTORE_INTERRUPTS_LITE() RESTORE_INTERRUPTS()
351 
352 #ifdef BOOTLOADER
353  #ifndef DOXYGEN_SHOULD_SKIP_THIS
354  // The bootloader does not use interrupts
355  #define DECLARE_INTERRUPT_STATE
356  #define DISABLE_INTERRUPTS() do { } while(0)
357  #define RESTORE_INTERRUPTS() do { } while(0)
358  #define INTERRUPTS_ON() do { } while(0)
359  #define INTERRUPTS_OFF() do { } while(0)
360  #define INTERRUPTS_ARE_OFF() (FALSE)
361  #define ATOMIC(blah) { blah }
362  #define HANDLE_PENDING_INTERRUPTS() do { } while(0)
363  #define SET_BASE_PRIORITY_LEVEL(basepri) do { } while(0)
364  #endif // DOXYGEN_SHOULD_SKIP_THIS
365 #else // BOOTLOADER
366 
367  #ifndef DOXYGEN_SHOULD_SKIP_THIS
368  /**
369  * @brief This macro should be called in the local variable
370  * declarations section of any function which calls DISABLE_INTERRUPTS()
371  * or RESTORE_INTERRUPTS().
372  */
373  #define DECLARE_INTERRUPT_STATE uint8_t _emIsrState
374 
375  // Prototypes for the BASEPRI and PRIMASK access functions. They are very
376  // basic and instantiated in assembly code in the file spmr.s37 (since
377  // there are no C functions that cause the compiler to emit code to access
378  // the BASEPRI/PRIMASK). This will inhibit the core from taking interrupts
379  // with a priority equal to or less than the BASEPRI value.
380  // Note that the priority values used by these functions are 5 bits and
381  // right-aligned
382  extern uint8_t _readBasePri(void);
383  extern void _writeBasePri(uint8_t priority);
384 
385  // Prototypes for BASEPRI functions used to disable and enable interrupts
386  // while still allowing enabled faults to trigger.
387  extern void _enableBasePri(void);
388  extern uint8_t _disableBasePri(void);
389  extern boolean _basePriIsDisabled(void);
390 
391  // Prototypes for setting and clearing PRIMASK for global interrupt
392  // enable/disable.
393  extern void _setPriMask(void);
394  extern void _clearPriMask(void);
395  #endif // DOXYGEN_SHOULD_SKIP_THIS
396 
397  //The core Global Interrupt Manipulation Macros start here.
398 
399  /**
400  * @brief Disable interrupts, saving the previous state so it can be
401  * later restored with RESTORE_INTERRUPTS().
402  * \note Do not fail to call RESTORE_INTERRUPTS().
403  * \note It is safe to nest this call.
404  */
405  #define DISABLE_INTERRUPTS() \
406  do { \
407  _emIsrState = _disableBasePri(); \
408  } while(0)
409 
410 
411  /**
412  * @brief Restore the global interrupt state previously saved by
413  * DISABLE_INTERRUPTS()
414  * \note Do not call without having first called DISABLE_INTERRUPTS()
415  * to have saved the state.
416  * \note It is safe to nest this call.
417  */
418  #define RESTORE_INTERRUPTS() \
419  do { \
420  _writeBasePri(_emIsrState); \
421  } while(0)
422 
423 
424  /**
425  * @brief Enable global interrupts without regard to the current or
426  * previous state.
427  */
428  #define INTERRUPTS_ON() \
429  do { \
430  _enableBasePri(); \
431  } while(0)
432 
433 
434  /**
435  * @brief Disable global interrupts without regard to the current or
436  * previous state.
437  */
438  #define INTERRUPTS_OFF() \
439  do { \
440  (void)_disableBasePri(); \
441  } while(0)
442 
443 
444  /**
445  * @returns TRUE if global interrupts are disabled.
446  */
447  #define INTERRUPTS_ARE_OFF() ( _basePriIsDisabled() )
448 
449  /**
450  * @returns TRUE if global interrupt flag was enabled when
451  * ::DISABLE_INTERRUPTS() was called.
452  */
453  #define INTERRUPTS_WERE_ON() (_emIsrState == 0)
454 
455  /**
456  * @brief A block of code may be made atomic by wrapping it with this
457  * macro. Something which is atomic cannot be interrupted by interrupts.
458  */
459  #define ATOMIC(blah) \
460  { \
461  DECLARE_INTERRUPT_STATE; \
462  DISABLE_INTERRUPTS(); \
463  { blah } \
464  RESTORE_INTERRUPTS(); \
465  }
466 
467 
468  /**
469  * @brief Allows any pending interrupts to be executed. Usually this
470  * would be called at a safe point while interrupts are disabled (such as
471  * within an ISR).
472  *
473  * Takes no action if interrupts are already enabled.
474  */
475  #define HANDLE_PENDING_INTERRUPTS() \
476  do { \
477  if (INTERRUPTS_ARE_OFF()) { \
478  INTERRUPTS_ON(); \
479  INTERRUPTS_OFF(); \
480  } \
481  } while (0)
482 
483 
484  /**
485  * @brief Sets the base priority mask (BASEPRI) to the value passed,
486  * bit shifted up by PRIGROUP_POSITION+1. This will inhibit the core from
487  * taking all interrupts with a preemptive priority equal to or less than
488  * the BASEPRI mask. This macro is dependent on the value of
489  * PRIGROUP_POSITION in nvic-config.h. Note that the value 0 disables the
490  * the base priority mask.
491  *
492  * Refer to the "PRIGROUP" table in nvic-config.h to know the valid values
493  * for this macro depending on the value of PRIGROUP_POSITION. With respect
494  * to the table, this macro can only take the preemptive priority group
495  * numbers denoted by the parenthesis.
496  */
497  #define SET_BASE_PRIORITY_LEVEL(basepri) \
498  do { \
499  _writeBasePri(basepri); \
500  } while(0)
501 
502 #endif // BOOTLOADER
503 ////////////////////////////////////////////////////////////////////////////////
504 //@} // end of Global Interrupt Manipulation Macros
505 ////////////////////////////////////////////////////////////////////////////////
506 
507 /**
508  * @brief Use the C Standard Library Memory Utilities from platform-common.h
509  */
510 #define _HAL_USE_COMMON_MEMUTILS_
511 
512 ////////////////////////////////////////////////////////////////////////////////
513 /** \name External Declarations
514  * These are routines that are defined in certain header files that we don't
515  * want to include, e.g. stdlib.h
516  */
517 ////////////////////////////////////////////////////////////////////////////////
518 //@{
519 
520 /**
521  * @brief Returns the absolute value of I (also called the magnitude of I).
522  * That is, if I is negative, the result is the opposite of I, but if I is
523  * nonnegative the result is I.
524  *
525  * @param I An integer.
526  *
527  * @return A nonnegative integer.
528  */
529 int abs(int I);
530 
531 ////////////////////////////////////////////////////////////////////////////////
532 //@} // end of External Declarations
533 ////////////////////////////////////////////////////////////////////////////////
534 
535 
536 /**
537  * @brief Include platform-common.h last to pick up defaults and common definitions.
538  */
539 #define PLATCOMMONOKTOINCLUDE
541 #undef PLATCOMMONOKTOINCLUDE
542 
543 #endif // GNU_H_
544 
545 /** @} END addtogroup */
546 /** @} */
547 
#define __SOURCEFILE__
The SOURCEFILE macro is used by asserts to list the filename if it isn&#39;t otherwise defined...
Definition: gnu.h:149
int abs(int I)
Returns the absolute value of I (also called the magnitude of I).
See platform_common for detailed documentation.
void halInternalResetWatchDog(void)
Resets the watchdog timer.
Definition: micro-common.c:25