SAMV71 Xplained Ultra Software Package 1.5

compiler.h

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