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