SAMV71 Xplained Ultra Software Package 1.3

compiler.h

00001 /* ----------------------------------------------------------------------------
00002  *         SAM Software Package License
00003  * ----------------------------------------------------------------------------
00004  * Copyright (c) 2011, Atmel Corporation
00005  *
00006  * All rights reserved.
00007  *
00008 
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted provided that the following conditions are met:
00011  *
00012  * - Redistributions of source code must retain the above copyright notice,
00013  * this list of conditions and the disclaimer below.
00014  *
00015  * Atmel's name may not be used to endorse or promote products derived from
00016  * this software without specific prior written permission.
00017  *
00018  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
00019  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00020  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
00021  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
00022  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00023  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00024  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00025  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00026  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00027  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00028  * ----------------------------------------------------------------------------
00029  */
00030 
00031 #ifndef _COMPILER_H_
00032 #define _COMPILER_H_
00033 
00034 /*
00035  * Peripherals registers definitions
00036  */
00037 #include "include/samv7/samv71.h"
00038 
00039 
00040 //_____ D E C L A R A T I O N S ____________________________________________
00041 
00042 #ifndef __ASSEMBLY__
00043 
00044 #include <stddef.h>
00045 #include <stdlib.h>
00046 #include <stdbool.h>
00047 #include <stdint.h>
00048 
00049 /* Define WEAK attribute */
00050 #if defined   ( __CC_ARM   )
00051     #define WEAK __attribute__ ((weak))
00052 #elif defined ( __ICCARM__ )
00053     #define WEAK __weak
00054 #elif defined (  __GNUC__  )
00055     #define WEAK __attribute__ ((weak))
00056 #endif
00057 
00058 /* Define Compiler name of tool chains */
00059 #if defined   ( __CC_ARM   )
00060     #define COMPILER_NAME               "KEIL"
00061 #elif defined ( __ICCARM__ )
00062     #define COMPILER_NAME               "IAR"
00063 #elif defined (  __GNUC__  )
00064     #define COMPILER_NAME               "GCC"
00065 #endif
00066 
00067 /* Define NO_INIT attribute */
00068 #if defined   ( __CC_ARM   )
00069     #define NO_INIT
00070 #elif defined ( __ICCARM__ )
00071     #define NO_INIT __no_init
00072 #elif defined (  __GNUC__  )
00073     #define NO_INIT
00074 #endif
00075    
00076 
00077 /* Define memory sync for tool chains */
00078 #if defined   ( __CC_ARM   )
00079     #define memory_sync()        __dsb(15);__isb(15);
00080 #elif defined ( __ICCARM__ )
00081     #define memory_sync()        __DSB();__ISB();
00082 #elif defined (  __GNUC__  )
00083     #define memory_sync()        __DSB();__ISB();
00084 #endif
00085 
00086 /* Define memory barrier for tool chains */
00087 #if defined   ( __CC_ARM   )
00088     #define memory_barrier()        __dmb(15);
00089 #elif defined ( __ICCARM__ )
00090     #define memory_barrier()        __DMB();
00091 #elif defined (  __GNUC__  )
00092     #define memory_barrier()        __DMB();
00093 #endif
00094 
00095 /*! \name Token Paste
00096  *
00097  * Paste N preprocessing tokens together, these tokens being allowed to be \#defined.
00098  *
00099  * May be used only within macros with the tokens passed as arguments if the tokens are \#defined.
00100  *
00101  * For example, writing TPASTE2(U, WIDTH) within a macro \#defined by
00102  * UTYPE(WIDTH) and invoked as UTYPE(UL_WIDTH) with UL_WIDTH \#defined as 32 is
00103  * equivalent to writing U32.
00104  */
00105 //! @{
00106 #define TPASTE2( a, b)                            a##b
00107 #define TPASTE3( a, b, c)                         a##b##c
00108 //! @}
00109 
00110 /*! \name Absolute Token Paste
00111  *
00112  * Paste N preprocessing tokens together, these tokens being allowed to be \#defined.
00113  *
00114  * No restriction of use if the tokens are \#defined.
00115  *
00116  * For example, writing ATPASTE2(U, UL_WIDTH) anywhere with UL_WIDTH \#defined
00117  * as 32 is equivalent to writing U32.
00118  */
00119 //! @{
00120 #define ATPASTE2( a, b)                           TPASTE2( a, b)
00121 #define ATPASTE3( a, b, c)                        TPASTE3( a, b, c)
00122 //! @}
00123 
00124 
00125 /**
00126  * \brief Emit the compiler pragma \a arg.
00127  *
00128  * \param arg The pragma directive as it would appear after \e \#pragma
00129  * (i.e. not stringified).
00130  */
00131 #define COMPILER_PRAGMA(arg)            _Pragma(#arg)
00132 
00133 /**
00134  * \def COMPILER_PACK_SET(alignment)
00135  * \brief Set maximum alignment for subsequent structure and union
00136  * definitions to \a alignment.
00137  */
00138 #define COMPILER_PACK_SET(alignment)   COMPILER_PRAGMA(pack(alignment))
00139 
00140 /**
00141  * \def COMPILER_PACK_RESET()
00142  * \brief Set default alignment for subsequent structure and union
00143  * definitions.
00144  */
00145 #define COMPILER_PACK_RESET()          COMPILER_PRAGMA(pack())
00146 
00147 /**
00148  * \brief Set user-defined section.
00149  * Place a data object or a function in a user-defined section.
00150  */
00151 #if defined   ( __CC_ARM   )
00152     #define COMPILER_SECTION(a)    __attribute__((__section__(a)))
00153 #elif defined ( __ICCARM__ )
00154     #define COMPILER_SECTION(a)    COMPILER_PRAGMA(location = a)
00155 #elif defined (  __GNUC__  )
00156     #define COMPILER_SECTION(a)    __attribute__((__section__(a)))
00157 #endif
00158 
00159 /**
00160  * \brief Set aligned boundary.
00161  */
00162 #if defined   ( __CC_ARM   )
00163     #define COMPILER_ALIGNED(a)    __attribute__((__aligned__(a)))
00164 #elif defined ( __ICCARM__ )
00165     #define COMPILER_ALIGNED(a)    COMPILER_PRAGMA(data_alignment = a)
00166 #elif defined (  __GNUC__  )
00167     #define COMPILER_ALIGNED(a)    __attribute__((__aligned__(a)))
00168 #endif
00169 
00170 /**
00171  * \brief Set word-aligned boundary.
00172  */
00173 
00174 #if defined   ( __CC_ARM   )
00175     #define COMPILER_WORD_ALIGNED    __attribute__((__aligned__(4)))
00176 #elif defined ( __ICCARM__ )
00177     #define COMPILER_WORD_ALIGNED    COMPILER_PRAGMA(data_alignment = 4)
00178 #elif defined (  __GNUC__  )
00179     #define COMPILER_WORD_ALIGNED    __attribute__((__aligned__(4)))
00180 #endif
00181 
00182 
00183 
00184 /*! \name Mathematics
00185  *
00186  * The same considerations as for clz and ctz apply here but GCC does not
00187  * provide built-in functions to access the assembly instructions abs, min and
00188  * max and it does not produce them by itself in most cases, so two sets of
00189  * macros are defined here:
00190  *   - Abs, Min and Max to apply to constant expressions (values known at
00191  *     compile time);
00192  *   - abs, min and max to apply to non-constant expressions (values unknown at
00193  *     compile time), abs is found in stdlib.h.
00194  */
00195 //! @{
00196 
00197 /*! \brief Takes the absolute value of \a a.
00198  *
00199  * \param a Input value.
00200  *
00201  * \return Absolute value of \a a.
00202  *
00203  * \note More optimized if only used with values known at compile time.
00204  */
00205 #define Abs(a)              (((a) <  0 ) ? -(a) : (a))
00206 
00207 /*! \brief Takes the minimal value of \a a and \a b.
00208  *
00209  * \param a Input value.
00210  * \param b Input value.
00211  *
00212  * \return Minimal value of \a a and \a b.
00213  *
00214  * \note More optimized if only used with values known at compile time.
00215  */
00216 #define Min(a, b)           (((a) < (b)) ?  (a) : (b))
00217 
00218 /*! \brief Takes the maximal value of \a a and \a b.
00219  *
00220  * \param a Input value.
00221  * \param b Input value.
00222  *
00223  * \return Maximal value of \a a and \a b.
00224  *
00225  * \note More optimized if only used with values known at compile time.
00226  */
00227 #define Max(a, b)           (((a) > (b)) ?  (a) : (b))
00228 
00229 // abs() is already defined by stdlib.h
00230 
00231 /*! \brief Takes the minimal value of \a a and \a b.
00232  *
00233  * \param a Input value.
00234  * \param b Input value.
00235  *
00236  * \return Minimal value of \a a and \a b.
00237  *
00238  * \note More optimized if only used with values unknown at compile time.
00239  */
00240 #define min(a, b)   Min(a, b)
00241 
00242 /*! \brief Takes the maximal value of \a a and \a b.
00243  *
00244  * \param a Input value.
00245  * \param b Input value.
00246  *
00247  * \return Maximal value of \a a and \a b.
00248  *
00249  * \note More optimized if only used with values unknown at compile time.
00250  */
00251 #define max(a, b)   Max(a, b)
00252 
00253 //! @}
00254 
00255 #define  be32_to_cpu(x) __REV(x)
00256 #define  cpu_to_be32(x) __REV(x)
00257 #define  BE32_TO_CPU(x) __REV(x)
00258 #define  CPU_TO_BE32(x) __REV(x)
00259 
00260 /**
00261  * \def UNUSED
00262  * \brief Marking \a v as a unused parameter or value.
00263  */
00264 #define UNUSED(v)          (void)(v)
00265 
00266 /**
00267  * \weakgroup interrupt_group
00268  *
00269  * @{
00270  */
00271 
00272 /**
00273  * \name Interrupt Service Routine definition
00274  *
00275  * @{
00276  */
00277 
00278 /**
00279  * \brief Initialize interrupt vectors
00280  *
00281  * For NVIC the interrupt vectors are put in vector table. So nothing
00282  * to do to initialize them, except defined the vector function with
00283  * right name.
00284  *
00285  * This must be called prior to \ref irq_register_handler.
00286  */
00287 #  define irq_initialize_vectors()   \
00288     do {                             \
00289     } while(0)
00290 
00291 /**
00292  * \brief Register handler for interrupt
00293  *
00294  * For NVIC the interrupt vectors are put in vector table. So nothing
00295  * to do to register them, except defined the vector function with
00296  * right name.
00297  *
00298  * Usage:
00299  * \code
00300     irq_initialize_vectors();
00301     irq_register_handler(foo_irq_handler);
00302 \endcode
00303  *
00304  * \note The function \a func must be defined with the \ref ISR macro.
00305  * \note The functions prototypes can be found in the device exception header
00306  *       files (exceptions.h).
00307  */
00308 #  define irq_register_handler(int_num, int_prio)                      \
00309     NVIC_ClearPendingIRQ(    (IRQn_Type)int_num);                      \
00310     NVIC_SetPriority(    (IRQn_Type)int_num, int_prio);                \
00311     NVIC_EnableIRQ(      (IRQn_Type)int_num);                          \
00312 
00313 //@}
00314       
00315 
00316 #  define cpu_irq_enable()                     \
00317     do {                                       \
00318         /*g_interrupt_enabled = true; */           \
00319         __DMB();                               \
00320         __enable_irq();                        \
00321     } while (0)
00322 #  define cpu_irq_disable()                    \
00323     do {                                       \
00324         __disable_irq();                       \
00325         __DMB();                               \
00326         /*g_interrupt_enabled = false; */          \
00327     } while (0)
00328 
00329 typedef uint32_t irqflags_t;
00330 
00331 #if !defined(__DOXYGEN__)
00332 extern volatile bool g_interrupt_enabled;
00333 #endif
00334 
00335 #define cpu_irq_is_enabled()    (__get_PRIMASK() == 0)
00336 
00337 static volatile uint32_t cpu_irq_critical_section_counter;
00338 static volatile bool     cpu_irq_prev_interrupt_state;
00339 
00340 static inline irqflags_t cpu_irq_save(void)
00341 {
00342     irqflags_t flags = cpu_irq_is_enabled();
00343     cpu_irq_disable();
00344     return flags;
00345 }
00346 
00347 static inline bool cpu_irq_is_enabled_flags(irqflags_t flags)
00348 {
00349     return (flags);
00350 }
00351 
00352 static inline void cpu_irq_restore(irqflags_t flags)
00353 {
00354     if (cpu_irq_is_enabled_flags(flags))
00355         cpu_irq_enable();
00356 }
00357 /*
00358 void cpu_irq_enter_critical(void);
00359 void cpu_irq_leave_critical(void);*/
00360 
00361 /**
00362  * \weakgroup interrupt_deprecated_group
00363  * @{
00364  */
00365 
00366 #define Enable_global_interrupt()            cpu_irq_enable()
00367 #define Disable_global_interrupt()           cpu_irq_disable()
00368 #define Is_global_interrupt_enabled()        cpu_irq_is_enabled()
00369 
00370 
00371 //_____ M A C R O S ________________________________________________________
00372 
00373 /*! \name Usual Constants
00374  */
00375 //! @{
00376 #define DISABLE   0
00377 #define ENABLE    1
00378 #define DISABLED  0
00379 #define ENABLED   1
00380 #define OFF       0
00381 #define ON        1
00382 #define FALSE     0
00383 #define TRUE      1
00384 #ifndef __cplusplus
00385 #if !defined(__bool_true_false_are_defined)
00386 #define false     FALSE
00387 #define true      TRUE
00388 #endif
00389 #endif
00390 #define KO        0
00391 #define OK        1
00392 #define PASS      0
00393 #define FAIL      1
00394 #define LOW       0
00395 #define HIGH      1
00396 #define CLR       0
00397 #define SET       1
00398 //! @}
00399 
00400 /*! \brief Counts the trailing zero bits of the given value considered as a 32-bit integer.
00401  *
00402  * \param u Value of which to count the trailing zero bits.
00403  *
00404  * \return The count of trailing zero bits in \a u.
00405  */
00406 #define ctz(u)              ((u) & (1ul <<  0) ?  0 : \
00407                                 (u) & (1ul <<  1) ?  1 : \
00408                                 (u) & (1ul <<  2) ?  2 : \
00409                                 (u) & (1ul <<  3) ?  3 : \
00410                                 (u) & (1ul <<  4) ?  4 : \
00411                                 (u) & (1ul <<  5) ?  5 : \
00412                                 (u) & (1ul <<  6) ?  6 : \
00413                                 (u) & (1ul <<  7) ?  7 : \
00414                                 (u) & (1ul <<  8) ?  8 : \
00415                                 (u) & (1ul <<  9) ?  9 : \
00416                                 (u) & (1ul << 10) ? 10 : \
00417                                 (u) & (1ul << 11) ? 11 : \
00418                                 (u) & (1ul << 12) ? 12 : \
00419                                 (u) & (1ul << 13) ? 13 : \
00420                                 (u) & (1ul << 14) ? 14 : \
00421                                 (u) & (1ul << 15) ? 15 : \
00422                                 (u) & (1ul << 16) ? 16 : \
00423                                 (u) & (1ul << 17) ? 17 : \
00424                                 (u) & (1ul << 18) ? 18 : \
00425                                 (u) & (1ul << 19) ? 19 : \
00426                                 (u) & (1ul << 20) ? 20 : \
00427                                 (u) & (1ul << 21) ? 21 : \
00428                                 (u) & (1ul << 22) ? 22 : \
00429                                 (u) & (1ul << 23) ? 23 : \
00430                                 (u) & (1ul << 24) ? 24 : \
00431                                 (u) & (1ul << 25) ? 25 : \
00432                                 (u) & (1ul << 26) ? 26 : \
00433                                 (u) & (1ul << 27) ? 27 : \
00434                                 (u) & (1ul << 28) ? 28 : \
00435                                 (u) & (1ul << 29) ? 29 : \
00436                                 (u) & (1ul << 30) ? 30 : \
00437                                 (u) & (1ul << 31) ? 31 : \
00438                                 32)
00439 
00440 #endif // __ASSEMBLY__
00441 
00442 #endif  // _COMPILER_H_
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines