gnu.h

Go to the documentation of this file.
00001 /** @file hal/micro/cortexm3/compiler/gnu.h
00002  * See @ref gnu for detailed documentation.
00003  *
00004  */
00005 
00006 /** @addtogroup gnu
00007  * @brief Compiler and Platform specific definitions and typedefs for the
00008  *  GNU C ARM compiler.
00009  *
00010  * @note gnu.h should be included first in all source files by setting the
00011  *  preprocessor macro PLATFORM_HEADER to point to it.  gnu.h automatically
00012  *  includes platform-common.h.
00013  *
00014  *  See gnu.h and platform-common.h for source code.
00015  *@{
00016  */
00017 
00018 #ifndef __GNU_H__
00019 #define __GNU_H__
00020 
00021 #ifndef __GNUC__
00022   #error Improper PLATFORM_HEADER
00023 #endif
00024 
00025 #if (__GNUC__ < 4)
00026   #error Only GNU C versions later than 4.3.2 are supported
00027 #elif (__GNUC__ == 4)
00028   #if (__GNUC_MINOR__ < 3)
00029     #error Only GNU C versions later than 4.3.2 are supported
00030   #elif (__GNUC_MINOR__ == 3)
00031     #if (__GNUC_PATCHLEVEL__ < 2)
00032       #error Only GNU C versions later than 4.3.2 are supported
00033     #endif
00034   #endif
00035 #endif // __GNUC__
00036 
00037 
00038 #ifndef DOXYGEN_SHOULD_SKIP_THIS
00039 //#include <intrinsics.h>
00040   #include <stdarg.h>
00041   #if defined (CORTEXM3_STM32W108)
00042     #include "micro/cortexm3/stm32w108/regs.h"
00043     #include "micro/cortexm3/stm32w108/stm32w108_type.h"
00044   #else
00045     #error Unknown CORTEXM3 micro
00046   #endif
00047   //Provide a default NVIC configuration file.  The build process can
00048   //override this if it needs to.
00049   #ifndef NVIC_CONFIG
00050     #define NVIC_CONFIG "hal/micro/cortexm3/nvic-config.h"
00051   #endif
00052 //[[
00053 #ifdef  EMU_TEST
00054   #ifdef  I_AM_AN_EMULATOR
00055     // This register is defined for both the chip and the emulator with
00056     // with distinct reset values.  Need to undefine to avoid preprocessor
00057     // collision.
00058     #undef DATA_EMU_REGS_BASE
00059     #undef DATA_EMU_REGS_END
00060     #undef DATA_EMU_REGS_SIZE
00061     #undef I_AM_AN_EMULATOR
00062     #undef I_AM_AN_EMULATOR_REG
00063     #undef I_AM_AN_EMULATOR_ADDR
00064     #undef I_AM_AN_EMULATOR_RESET
00065     #undef I_AM_AN_EMULATOR_I_AM_AN_EMULATOR
00066     #undef I_AM_AN_EMULATOR_I_AM_AN_EMULATOR_MASK
00067     #undef I_AM_AN_EMULATOR_I_AM_AN_EMULATOR_BIT
00068     #undef I_AM_AN_EMULATOR_I_AM_AN_EMULATOR_BITS
00069   #endif//I_AM_AN_EMULATOR
00070     #error MICRO currently not supported for emulator builds.
00071 #endif//EMU_TEST
00072 //]]
00073 
00074 // suppress warnings about unknown pragmas
00075 //  (as they may be pragmas known to other platforms)
00076 //#pragma diag_suppress = pe161
00077 
00078 #endif  // DOXYGEN_SHOULD_SKIP_THIS
00079 
00080 // Define that the minimal hal is being used.
00081 #define MINIMAL_HAL
00082 
00083 
00084 /** \name Master Variable Types
00085  * These are a set of typedefs to make the size of all variable declarations
00086  * explicitly known.
00087  */
00088 //@{
00089 /**
00090  * @brief A typedef to make the size of the variable explicitly known.
00091  */
00092 typedef unsigned char  boolean;
00093 typedef unsigned char  int8u;
00094 typedef signed   char  int8s;
00095 typedef unsigned short int16u;
00096 typedef signed   short int16s;
00097 typedef unsigned int   int32u;
00098 typedef signed   int   int32s;
00099 typedef unsigned int   PointerType;
00100 //@} \\END MASTER VARIABLE TYPES
00101 
00102 /**
00103  * @brief Use the Master Program Memory Declarations from platform-common.h
00104  */
00105 #define _HAL_USE_COMMON_PGM_
00106 
00107 
00108 
00109 ////////////////////////////////////////////////////////////////////////////////
00110 /** \name Miscellaneous Macros
00111  */
00112 ////////////////////////////////////////////////////////////////////////////////
00113 //@{
00114 
00115 /**
00116  * @brief A convenient method for code to know what endiannes processor
00117  * it is running on.  For the Cortex-M3, we are little endian.
00118  */
00119 #define BIGENDIAN_CPU  FALSE
00120 
00121 
00122 /**
00123  * @brief A friendlier name for the compiler's intrinsic for not
00124  * stripping.
00125  */
00126 #define NO_STRIPPING /* __root ??? */
00127 #define __no_init  /*__attribute__((section (".noinit")))*/
00128 
00129 /**
00130  * @brief A friendlier name for the compiler's intrinsic for eeprom
00131  * reference.
00132  */
00133 #define EEPROM  errorerror
00134 
00135 
00136 #ifndef __SOURCEFILE__
00137   /**
00138    * @brief The __SOURCEFILE__ macro is used by asserts to list the
00139    * filename if it isn't otherwise defined, set it to the compiler intrinsic
00140    * which specifies the whole filename and path of the sourcefile
00141    */
00142   #define __SOURCEFILE__ __FILE__
00143 #endif
00144 
00145 
00146 //#include <assert.h>
00147 #undef assert
00148 #define assert(condition) do { if (! (condition)) {while (1); }} while(0)
00149 #if 0
00150       do { if (! (condition)) {                                 \
00151           printf ("Assert failed %s %d\r\n",__SOURCEFILE__, __LINE__); }} while(0)
00152 #endif
00153 
00154 #ifndef BOOTLOADER
00155   #undef __delay_cycles
00156   /**
00157    * @brief __delay_cycles() is an intrinsic IAR call; however, we
00158    * have explicity disallowed it since it is too specific to the system clock.
00159    * \note Please use halCommonDelayMicroseconds() instead, because it correctly
00160    * accounts for various system clock speeds.
00161    */
00162   #define __delay_cycles(x)  please_use_halCommonDelayMicroseconds_instead_of_delay_cycles
00163 #endif
00164 
00165 /**
00166  * @brief Set debug level based on whether or DEBUG is defined.
00167  * basic debugging support is included if DEBUG is not defined.
00168  */
00169 #ifndef DEBUG_LEVEL
00170   #ifdef DEBUG
00171     #define DEBUG_LEVEL FULL_DEBUG
00172   #else
00173     #define DEBUG_LEVEL BASIC_DEBUG
00174   #endif
00175 #endif
00176 
00177 /**
00178  * @brief Set the application start and end address.
00179  * This are useful to detect whether an image is for bootloader mode or not.
00180  * This can be used also to clone image to another node via bootloader.
00181  */
00182 #define APPLICATION_IMAGE_START ((u32) &__ApplicationFlashStart)
00183 #define APPLICATION_IMAGE_END ((u32) &__ApplicationFlashEnd)
00184 extern char __ApplicationFlashStart;
00185 extern char __ApplicationFlashEnd;
00186 
00187 /**
00188  * @brief Macro to reset the watchdog timer.  Note:  be very very
00189  * careful when using this as you can easily get into an infinite loop if you
00190  * are not careful.
00191  */
00192 void halInternalResetWatchDog(void);
00193 #define halResetWatchdog()  halInternalResetWatchDog()
00194 
00195 
00196 /**
00197  * @brief Define __attribute__ to nothing since it isn't handled by IAR.
00198  */
00199 
00200 
00201 /**
00202  * @brief Declare a variable as unused to avoid a warning.  Has no effect
00203  * in IAR builds
00204  */
00205 #define UNUSED
00206 
00207 /**
00208  * @brief Some platforms need to cast enum values that have the high bit set.
00209  */
00210 #define SIGNED_ENUM
00211 
00212 
00213 /**
00214  * @brief Define the magic value that is interpreted by IAR C-SPY's Stack View.
00215  */
00216 #define STACK_FILL_VALUE  0xCDCDCDCD
00217 
00218 /**
00219  * @brief Define a generic RAM function identifier to a compiler specific one.
00220  */
00221 #ifdef RAMEXE
00222   //If the whole build is running out of RAM, as chosen by the RAMEXE build
00223   //define, then define RAMFUNC to nothing since it's not needed.
00224   #define RAMFUNC
00225 #else //RAMEXE
00226   #define RAMFUNC __ramfunc
00227 #endif //RAMEXE
00228 
00229 /**
00230  * @brief Define a generic no operation identifier to a compiler specific one.
00231  */
00232 #define NO_OPERATION() __no_operation()
00233 
00234 /**
00235  * @brief A convenience macro that makes it easy to change the field of a
00236  * register to any value.
00237  */
00238 #define SET_REG_FIELD(reg, field, value)                      \
00239   do{                                                         \
00240     reg = ((reg & (~field##_MASK)) | (value << field##_BIT)); \
00241   }while(0)
00242 
00243 /**
00244  * @brief Stub for code not running in simulation.
00245  */
00246 #define simulatedTimePasses()
00247 /**
00248  * @brief Stub for code not running in simulation.
00249  */
00250 #define simulatedTimePassesMs(x)
00251 /**
00252  * @brief Stub for code not running in simulation.
00253  */
00254 #define simulatedSerialTimePasses()
00255 
00256 
00257 /**
00258  * @brief Use the Divide and Modulus Operations from platform-common.h
00259  */
00260 #define _HAL_USE_COMMON_DIVMOD_
00261 
00262 
00263 /**
00264  * @brief Provide a portable way to specify the segment where a variable
00265  * lives.
00266  */
00267 #define VAR_AT_SEGMENT(__variableDeclaration, __segmentName) \
00268    __variableDeclaration __attribute__ ((section (__segmentName)))
00269 
00270 #define ALIGN_VAR(__variableDeclaration, alignment)             \
00271   __variableDeclaration __attribute__ ((aligned(alignment)))
00272 
00273 ////////////////////////////////////////////////////////////////////////////////
00274 //@}  // end of Miscellaneous Macros
00275 ////////////////////////////////////////////////////////////////////////////////
00276 
00277 /** @name Portable segment names
00278  *@{
00279  */
00280 /**
00281  * @brief Portable segment names
00282  */
00283 #define __NO_INIT__       ".noinit"
00284 #define __INTVEC__ ".intvec"
00285 #define __CSTACK__ "CSTACK"
00286 #define __DATA_INIT__ ".data_init"
00287 #define __DATA__ ".data"
00288 #define __BSS__ ".bss"
00289 #define __CONST__ ".rodata"
00290 #define __TEXT__ ".text"
00291 #define __TEXTRW_INIT__ ".textrw_init"
00292 #define __TEXTRW__ ".textrw"
00293 #define __FAT__ "FAT" // Fixed address table
00294 #define __NVM__ "NVM" //Non-Volatile Memory data storage
00295 
00296 //=============================================================================
00297 // The '#pragma segment=' declaration must be used before attempting to access
00298 // the segments so the compiler properly handles the __segment_*() functions.
00299 //
00300 // The segment names used here are the default segment names used by IAR. Refer
00301 // to the IAR Compiler Reference Guide for a proper description of these
00302 // segments.
00303 //=============================================================================
00304 #if 0
00305 #pragma segment=__NO_INIT__
00306 #pragma segment=__INTVEC__
00307 #pragma segment=__CSTACK__
00308 #pragma segment=__DATA_INIT__
00309 #pragma segment=__DATA__
00310 #pragma segment=__BSS__
00311 #pragma segment=__CONST__
00312 #pragma segment=__TEXT__
00313 #pragma segment=__TEXTRW_INIT__
00314 #pragma segment=__TEXTRW__
00315 #pragma segment=__FAT__
00316 #pragma segment=__NVM__
00317 #endif
00318 /**@} */
00319 
00320 //A utility function for inserting barrier instructions.  These
00321 //instructions should be used whenever the MPU is enabled or disabled so
00322 //that all memory/instruction accesses can complete before the MPU changes
00323 //state.  
00324 void _executeBarrierInstructions(void);
00325 
00326 ////////////////////////////////////////////////////////////////////////////////
00327 /** \name Global Interrupt Manipulation Macros
00328  *
00329  * \b Note: The special purpose BASEPRI register is used to enable and disable
00330  * interrupts while permitting faults.
00331  * When BASEPRI is set to 1 no interrupts can trigger. The configurable faults
00332  * (usage, memory management, and bus faults) can trigger if enabled as well as 
00333  * the always-enabled exceptions (reset, NMI and hard fault).
00334  * When BASEPRI is set to 0, it is disabled, so any interrupt can triggger if 
00335  * its priority is higher than the current priority.
00336  */
00337 ////////////////////////////////////////////////////////////////////////////////
00338 //@{
00339 
00340 #define ATOMIC_LITE(blah) ATOMIC(blah)
00341 #define DECLARE_INTERRUPT_STATE_LITE DECLARE_INTERRUPT_STATE
00342 #define DISABLE_INTERRUPTS_LITE() DISABLE_INTERRUPTS()
00343 #define RESTORE_INTERRUPTS_LITE() RESTORE_INTERRUPTS()
00344 
00345 #ifdef BOOTLOADER
00346   #ifndef DOXYGEN_SHOULD_SKIP_THIS
00347     // The bootloader does not use interrupts
00348     #define DECLARE_INTERRUPT_STATE
00349     #define DISABLE_INTERRUPTS() do { } while(0)
00350     #define RESTORE_INTERRUPTS() do { } while(0)
00351     #define INTERRUPTS_ON() do { } while(0)
00352     #define INTERRUPTS_OFF() do { } while(0)
00353     #define INTERRUPTS_ARE_OFF() (FALSE)
00354     #define ATOMIC(blah) { blah }
00355     #define HANDLE_PENDING_INTERRUPTS() do { } while(0)
00356     #define SET_BASE_PRIORITY_LEVEL(basepri) do { } while(0)
00357   #endif  // DOXYGEN_SHOULD_SKIP_THIS
00358 #else  // BOOTLOADER
00359   
00360   #ifndef DOXYGEN_SHOULD_SKIP_THIS
00361       /**
00362        * @brief This macro should be called in the local variable
00363        * declarations section of any function which calls DISABLE_INTERRUPTS()
00364        * or RESTORE_INTERRUPTS().
00365        */
00366       #define DECLARE_INTERRUPT_STATE int8u _emIsrState
00367     
00368     // Prototypes for the BASEPRI and PRIMASK access functions.  They are very
00369     // basic and instantiated in assembly code in the file spmr.s37 (since
00370     // there are no C functions that cause the compiler to emit code to access
00371     // the BASEPRI/PRIMASK). This will inhibit the core from taking interrupts
00372     // with a priority equal to or less than the BASEPRI value.
00373     // Note that the priority values used by these functions are 5 bits and 
00374     // right-aligned
00375     extern int8u _readBasePri(void);
00376     extern void _writeBasePri(int8u priority);
00377 
00378     // Prototypes for BASEPRI functions used to disable and enable interrupts
00379     // while still allowing enabled faults to trigger.
00380     extern void _enableBasePri(void);
00381     extern int8u _disableBasePri(void);
00382     extern boolean _basePriIsDisabled(void);
00383     
00384     // Prototypes for setting and clearing PRIMASK for global interrupt
00385     // enable/disable.
00386     extern void _setPriMask(void);
00387     extern void _clearPriMask(void);
00388   #endif  // DOXYGEN_SHOULD_SKIP_THIS
00389 
00390   //The core Global Interrupt Manipulation Macros start here.
00391   
00392   /**
00393    * @brief Disable interrupts, saving the previous state so it can be
00394    * later restored with RESTORE_INTERRUPTS().
00395    * \note Do not fail to call RESTORE_INTERRUPTS().
00396    * \note It is safe to nest this call.
00397    */
00398   #define DISABLE_INTERRUPTS()                    \
00399     do {                                          \
00400       _emIsrState = _disableBasePri();            \
00401     } while(0)
00402   
00403   
00404   /** 
00405    * @brief Restore the global interrupt state previously saved by
00406    * DISABLE_INTERRUPTS()
00407    * \note Do not call without having first called DISABLE_INTERRUPTS()
00408    * to have saved the state.
00409    * \note It is safe to nest this call.
00410    */
00411   #define RESTORE_INTERRUPTS()      \
00412     do {                            \
00413       _writeBasePri(_emIsrState); \
00414     } while(0)
00415   
00416   
00417   /**
00418    * @brief Enable global interrupts without regard to the current or
00419    * previous state.
00420    */
00421   #define INTERRUPTS_ON()               \
00422     do {                                \
00423       _enableBasePri();                 \
00424     } while(0)
00425   
00426   
00427   /**
00428    * @brief Disable global interrupts without regard to the current or
00429    * previous state.
00430    */
00431   #define INTERRUPTS_OFF()      \
00432     do {                        \
00433       (void)_disableBasePri();  \
00434     } while(0)
00435   
00436   
00437   /**
00438    * @returns TRUE if global interrupts are disabled.
00439    */
00440   #define INTERRUPTS_ARE_OFF() ( _basePriIsDisabled() )
00441   
00442   /**
00443    * @returns TRUE if global interrupt flag was enabled when 
00444    * ::DISABLE_INTERRUPTS() was called.
00445    */
00446   #define INTERRUPTS_WERE_ON() (_emIsrState == 0)
00447   
00448   /**
00449    * @brief A block of code may be made atomic by wrapping it with this
00450    * macro.  Something which is atomic cannot be interrupted by interrupts.
00451    */
00452   #define ATOMIC(blah)         \
00453   {                            \
00454     DECLARE_INTERRUPT_STATE;   \
00455     DISABLE_INTERRUPTS();      \
00456     { blah }                   \
00457     RESTORE_INTERRUPTS();      \
00458   }
00459   
00460   
00461   /**
00462    * @brief Allows any pending interrupts to be executed. Usually this
00463    * would be called at a safe point while interrupts are disabled (such as
00464    * within an ISR).
00465    * 
00466    * Takes no action if interrupts are already enabled.
00467    */
00468   #define HANDLE_PENDING_INTERRUPTS() \
00469     do {                              \
00470       if (INTERRUPTS_ARE_OFF()) {     \
00471         INTERRUPTS_ON();              \
00472         INTERRUPTS_OFF();             \
00473       }                               \
00474    } while (0)
00475   
00476   
00477   /**
00478    * @brief Sets the base priority mask (BASEPRI) to the value passed,
00479    * bit shifted up by PRIGROUP_POSITION+1.  This will inhibit the core from
00480    * taking all interrupts with a preemptive priority equal to or less than
00481    * the BASEPRI mask.  This macro is dependent on the value of
00482    * PRIGROUP_POSITION in nvic-config.h. Note that the value 0 disables the
00483    * the base priority mask.
00484    *
00485    * Refer to the "PRIGROUP" table in nvic-config.h to know the valid values
00486    * for this macro depending on the value of PRIGROUP_POSITION.  With respect
00487    * to the table, this macro can only take the preemptive priority group
00488    * numbers denoted by the parenthesis.
00489    */
00490   #define SET_BASE_PRIORITY_LEVEL(basepri) \
00491     do {                                   \
00492       _writeBasePri(basepri);              \
00493     } while(0)
00494   
00495 #endif // BOOTLOADER
00496 ////////////////////////////////////////////////////////////////////////////////
00497 //@}  // end of Global Interrupt Manipulation Macros
00498 ////////////////////////////////////////////////////////////////////////////////
00499 
00500 /**
00501  * @brief Use the C Standard Library Memory Utilities from platform-common.h
00502  */
00503 #define _HAL_USE_COMMON_MEMUTILS_
00504 
00505 ////////////////////////////////////////////////////////////////////////////////
00506 /** \name External Declarations
00507  * These are routines that are defined in certain header files that we don't
00508  * want to include, e.g. stdlib.h
00509  */
00510 ////////////////////////////////////////////////////////////////////////////////
00511 //@{
00512 
00513 /**
00514  * @brief Returns the absolute value of I (also called the magnitude of I).
00515  * That is, if I is negative, the result is the opposite of I, but if I is
00516  * nonnegative the result is I.
00517  *
00518  * @param I  An integer.
00519  *
00520  * @return A nonnegative integer.
00521  */
00522 int abs(int I);
00523 
00524 ////////////////////////////////////////////////////////////////////////////////
00525 //@}  // end of External Declarations
00526 ////////////////////////////////////////////////////////////////////////////////
00527 
00528 
00529 /**
00530  * @brief Include platform-common.h last to pick up defaults and common definitions.
00531  */
00532 #define PLATCOMMONOKTOINCLUDE
00533   #include "hal/micro/generic/compiler/platform-common.h"
00534 #undef PLATCOMMONOKTOINCLUDE
00535 
00536 #endif // __GNU_H__
00537 
00538 /** @}  END addtogroup */
00539 

Generated on Mon Apr 11 14:23:40 2011 for Contiki 2.5 by  doxygen 1.6.1