Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:22:48

0001 /* ---------------------------------------------------------------------------- */
0002 /*                  Atmel Microcontroller Software Support                      */
0003 /*                       SAM Software Package License                           */
0004 /* ---------------------------------------------------------------------------- */
0005 /* Copyright (c) 2015, Atmel Corporation                                        */
0006 /*                                                                              */
0007 /* All rights reserved.                                                         */
0008 /*                                                                              */
0009 /* Redistribution and use in source and binary forms, with or without           */
0010 /* modification, are permitted provided that the following condition is met:    */
0011 /*                                                                              */
0012 /* - Redistributions of source code must retain the above copyright notice,     */
0013 /* this list of conditions and the disclaimer below.                            */
0014 /*                                                                              */
0015 /* Atmel's name may not be used to endorse or promote products derived from     */
0016 /* this software without specific prior written permission.                     */
0017 /*                                                                              */
0018 /* DISCLAIMER:  THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR   */
0019 /* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
0020 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE   */
0021 /* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,      */
0022 /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */
0023 /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,  */
0024 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    */
0025 /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING         */
0026 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, */
0027 /* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                           */
0028 /* ---------------------------------------------------------------------------- */
0029 
0030 #ifndef _COMPILER_H_
0031 #define _COMPILER_H_
0032 
0033 #ifdef __rtems__
0034 #include <bspopts.h>
0035 #endif /* __rtems__ */
0036 /*
0037  * Peripherals registers definitions
0038  */
0039 #if defined __SAMV71J19__ \
0040     || defined __SAMV71J20__ \
0041     || defined __SAMV71J21__ \
0042     || defined __SAMV71N19__ \
0043     || defined __SAMV71N20__ \
0044     || defined __SAMV71N21__ \
0045     || defined __SAMV71Q19__ \
0046     || defined __SAMV71Q20__ \
0047     || defined __SAMV71Q21__
0048     #include "include/samv71/samv71.h"
0049 #elif defined __SAMS70J19__ \
0050     || defined __SAMS70J20__ \
0051     || defined __SAMS70J21__ \
0052     || defined __SAMS70N19__ \
0053     || defined __SAMS70N20__ \
0054     || defined __SAMS70N21__ \
0055     || defined __SAMS70Q19__ \
0056     || defined __SAMS70Q20__ \
0057     || defined __SAMS70Q21__
0058     #include "include/sams70/sams70.h"
0059 #elif defined __SAME70J19__ \
0060     || defined __SAME70J20__ \
0061     || defined __SAME70J21__ \
0062     || defined __SAME70N19__ \
0063     || defined __SAME70N20__ \
0064     || defined __SAME70N21__ \
0065     || defined __SAME70Q19__ \
0066     || defined __SAME70Q20__ \
0067     || defined __SAME70Q21__
0068     #include "include/same70/same70.h"
0069 #else
0070     #error "please define correct macro for the chip first!"
0071 #endif
0072 
0073 
0074 //_____ D E C L A R A T I O N S ____________________________________________
0075 
0076 #ifndef __ASSEMBLY__
0077 
0078 #include <stddef.h>
0079 #include <stdlib.h>
0080 #include <stdbool.h>
0081 #include <stdint.h>
0082 
0083 /* Define WEAK attribute */
0084 #if defined   (__CC_ARM)
0085     #define WEAK __attribute__ ((weak))
0086 #elif defined (__ICCARM__)
0087     #define WEAK __weak
0088 #elif defined (__GNUC__)
0089     #define WEAK __attribute__ ((weak))
0090 #endif
0091 
0092 /* Define Compiler name of tool chains */
0093 #if defined   (__CC_ARM)
0094     #define COMPILER_NAME               "KEIL"
0095 #elif defined (__ICCARM__)
0096     #define COMPILER_NAME               "IAR"
0097 #elif defined (__GNUC__)
0098     #define COMPILER_NAME               "GCC"
0099 #endif
0100 
0101 /* Define NO_INIT attribute */
0102 #if defined   (__CC_ARM)
0103     #define NO_INIT
0104 #elif defined (__ICCARM__)
0105     #define NO_INIT __no_init
0106 #elif defined (__GNUC__)
0107     #define NO_INIT
0108 #endif
0109 
0110 
0111 /* Define memory sync for tool chains */
0112 #if defined   (__CC_ARM)
0113     #define memory_sync()        __dsb(15);__isb(15);
0114 #elif defined (__ICCARM__)
0115     #define memory_sync()        __DSB();__ISB();
0116 #elif defined (__GNUC__)
0117     #define memory_sync()        __DSB();__ISB();
0118 #endif
0119 
0120 /* Define memory barrier for tool chains */
0121 #if defined   (__CC_ARM)
0122     #define memory_barrier()        __dmb(15);
0123 #elif defined (__ICCARM__)
0124     #define memory_barrier()        __DMB();
0125 #elif defined (__GNUC__)
0126     #define memory_barrier()        __DMB();
0127 #endif
0128 
0129 /*! \name Token Paste
0130  *
0131  * Paste N preprocessing tokens together, these tokens being allowed to be \#defined.
0132  *
0133  * May be used only within macros with the tokens passed as arguments if the tokens are \#defined.
0134  *
0135  * For example, writing TPASTE2(U, WIDTH) within a macro \#defined by
0136  * UTYPE(WIDTH) and invoked as UTYPE(UL_WIDTH) with UL_WIDTH \#defined as 32 is
0137  * equivalent to writing U32.
0138  */
0139 //! @{
0140 #define TPASTE2(a, b)                            a##b
0141 #define TPASTE3(a, b, c)                         a##b##c
0142 //! @}
0143 
0144 /*! \name Absolute Token Paste
0145  *
0146  * Paste N preprocessing tokens together, these tokens being allowed to be \#defined.
0147  *
0148  * No restriction of use if the tokens are \#defined.
0149  *
0150  * For example, writing ATPASTE2(U, UL_WIDTH) anywhere with UL_WIDTH \#defined
0151  * as 32 is equivalent to writing U32.
0152  */
0153 //! @{
0154 #define ATPASTE2(a, b)                           TPASTE2(a, b)
0155 #define ATPASTE3(a, b, c)                        TPASTE3(a, b, c)
0156 //! @}
0157 
0158 
0159 /**
0160  * \brief Emit the compiler pragma \a arg.
0161  *
0162  * \param arg The pragma directive as it would appear after \e \#pragma
0163  * (i.e. not stringified).
0164  */
0165 #define COMPILER_PRAGMA(arg)            _Pragma(#arg)
0166 
0167 /**
0168  * \def COMPILER_PACK_SET(alignment)
0169  * \brief Set maximum alignment for subsequent structure and union
0170  * definitions to \a alignment.
0171  */
0172 #define COMPILER_PACK_SET(alignment)   COMPILER_PRAGMA(pack(alignment))
0173 
0174 /**
0175  * \def COMPILER_PACK_RESET()
0176  * \brief Set default alignment for subsequent structure and union
0177  * definitions.
0178  */
0179 #define COMPILER_PACK_RESET()          COMPILER_PRAGMA(pack())
0180 
0181 /**
0182  * \brief Set user-defined section.
0183  * Place a data object or a function in a user-defined section.
0184  */
0185 #if defined   (__CC_ARM)
0186     #define COMPILER_SECTION(a)    __attribute__((__section__(a)))
0187 #elif defined (__ICCARM__)
0188     #define COMPILER_SECTION(a)    COMPILER_PRAGMA(location = a)
0189 #elif defined (__GNUC__)
0190     #define COMPILER_SECTION(a)    __attribute__((__section__(a)))
0191 #endif
0192 
0193 /**
0194  * \brief Set aligned boundary.
0195  */
0196 #if defined   (__CC_ARM)
0197     #define COMPILER_ALIGNED(a)    __attribute__((__aligned__(a)))
0198 #elif defined (__ICCARM__)
0199     #define COMPILER_ALIGNED(a)    COMPILER_PRAGMA(data_alignment = a)
0200 #elif defined (__GNUC__)
0201     #define COMPILER_ALIGNED(a)    __attribute__((__aligned__(a)))
0202 #endif
0203 
0204 /**
0205  * \brief Set word-aligned boundary.
0206  */
0207 
0208 #if defined   (__CC_ARM)
0209     #define COMPILER_WORD_ALIGNED    __attribute__((__aligned__(4)))
0210 #elif defined (__ICCARM__)
0211     #define COMPILER_WORD_ALIGNED    COMPILER_PRAGMA(data_alignment = 4)
0212 #elif defined (__GNUC__)
0213     #define COMPILER_WORD_ALIGNED    __attribute__((__aligned__(4)))
0214 #endif
0215 
0216 
0217 
0218 /*! \name Mathematics
0219  *
0220  * The same considerations as for clz and ctz apply here but GCC does not
0221  * provide built-in functions to access the assembly instructions abs, min and
0222  * max and it does not produce them by itself in most cases, so two sets of
0223  * macros are defined here:
0224  *   - Abs, Min and Max to apply to constant expressions (values known at
0225  *     compile time);
0226  *   - abs, min and max to apply to non-constant expressions (values unknown at
0227  *     compile time), abs is found in stdlib.h.
0228  */
0229 //! @{
0230 
0231 /*! \brief Takes the absolute value of \a a.
0232  *
0233  * \param a Input value.
0234  *
0235  * \return Absolute value of \a a.
0236  *
0237  * \note More optimized if only used with values known at compile time.
0238  */
0239 #define Abs(a)              (((a) <  0) ? -(a) : (a))
0240 
0241 /*! \brief Takes the minimal value of \a a and \a b.
0242  *
0243  * \param a Input value.
0244  * \param b Input value.
0245  *
0246  * \return Minimal value of \a a and \a b.
0247  *
0248  * \note More optimized if only used with values known at compile time.
0249  */
0250 #define Min(a, b)           (((a) < (b)) ?  (a) : (b))
0251 
0252 /*! \brief Takes the maximal value of \a a and \a b.
0253  *
0254  * \param a Input value.
0255  * \param b Input value.
0256  *
0257  * \return Maximal value of \a a and \a b.
0258  *
0259  * \note More optimized if only used with values known at compile time.
0260  */
0261 #define Max(a, b)           (((a) > (b)) ?  (a) : (b))
0262 
0263 // abs() is already defined by stdlib.h
0264 
0265 /*! \brief Takes the minimal value of \a a and \a b.
0266  *
0267  * \param a Input value.
0268  * \param b Input value.
0269  *
0270  * \return Minimal value of \a a and \a b.
0271  *
0272  * \note More optimized if only used with values unknown at compile time.
0273  */
0274 #define min(a, b)   Min(a, b)
0275 
0276 /*! \brief Takes the maximal value of \a a and \a b.
0277  *
0278  * \param a Input value.
0279  * \param b Input value.
0280  *
0281  * \return Maximal value of \a a and \a b.
0282  *
0283  * \note More optimized if only used with values unknown at compile time.
0284  */
0285 #define max(a, b)   Max(a, b)
0286 
0287 //! @}
0288 
0289 #define  be32_to_cpu(x) __REV(x)
0290 #define  cpu_to_be32(x) __REV(x)
0291 #define  BE32_TO_CPU(x) __REV(x)
0292 #define  CPU_TO_BE32(x) __REV(x)
0293 
0294 /**
0295  * \def UNUSED
0296  * \brief Marking \a v as a unused parameter or value.
0297  */
0298 #define UNUSED(v)          (void)(v)
0299 
0300 /**
0301  * \weakgroup interrupt_group
0302  *
0303  * @{
0304  */
0305 
0306 /**
0307  * \name Interrupt Service Routine definition
0308  *
0309  * @{
0310  */
0311 
0312 /**
0313  * \brief Initialize interrupt vectors
0314  *
0315  * For NVIC the interrupt vectors are put in vector table. So nothing
0316  * to do to initialize them, except defined the vector function with
0317  * right name.
0318  *
0319  * This must be called prior to \ref irq_register_handler.
0320  */
0321 #  define irq_initialize_vectors()   \
0322     do {                             \
0323     } while (0)
0324 
0325 /**
0326  * \brief Register handler for interrupt
0327  *
0328  * For NVIC the interrupt vectors are put in vector table. So nothing
0329  * to do to register them, except defined the vector function with
0330  * right name.
0331  *
0332  * Usage:
0333  * \code
0334     irq_initialize_vectors();
0335     irq_register_handler(foo_irq_handler);
0336 \endcode
0337  *
0338  * \note The function \a func must be defined with the \ref ISR macro.
0339  * \note The functions prototypes can be found in the device exception header
0340  *       files (exceptions.h).
0341  */
0342 #  define irq_register_handler(int_num, int_prio)                      \
0343     NVIC_ClearPendingIRQ((IRQn_Type)int_num);                      \
0344     NVIC_SetPriority((IRQn_Type)int_num, int_prio);                \
0345     NVIC_EnableIRQ((IRQn_Type)int_num);                          \
0346 
0347     //@}
0348 
0349 
0350 #  define cpu_irq_enable()                     \
0351     do {                                       \
0352         /*g_interrupt_enabled = true; */           \
0353         __DMB();                               \
0354         __enable_irq();                        \
0355     } while (0)
0356 #  define cpu_irq_disable()                    \
0357     do {                                       \
0358         __disable_irq();                       \
0359         __DMB();                               \
0360         /*g_interrupt_enabled = false; */          \
0361     } while (0)
0362 
0363     typedef uint32_t irqflags_t;
0364 
0365 #if !defined(__DOXYGEN__)
0366     extern volatile bool g_interrupt_enabled;
0367 #endif
0368 
0369 #define cpu_irq_is_enabled()    (__get_PRIMASK() == 0)
0370 
0371     static volatile uint32_t cpu_irq_critical_section_counter;
0372     static volatile bool     cpu_irq_prev_interrupt_state;
0373 
0374     static inline irqflags_t cpu_irq_save(void)
0375     {
0376         irqflags_t flags = cpu_irq_is_enabled();
0377         cpu_irq_disable();
0378         return flags;
0379     }
0380 
0381     static inline bool cpu_irq_is_enabled_flags(irqflags_t flags)
0382     {
0383         return (flags);
0384     }
0385 
0386     static inline void cpu_irq_restore(irqflags_t flags)
0387     {
0388         if (cpu_irq_is_enabled_flags(flags))
0389             cpu_irq_enable();
0390     }
0391     /*
0392     void cpu_irq_enter_critical(void);
0393     void cpu_irq_leave_critical(void);*/
0394 
0395     /**
0396      * \weakgroup interrupt_deprecated_group
0397      * @{
0398      */
0399 
0400 #define Enable_global_interrupt()            cpu_irq_enable()
0401 #define Disable_global_interrupt()           cpu_irq_disable()
0402 #define Is_global_interrupt_enabled()        cpu_irq_is_enabled()
0403 
0404 
0405     //_____ M A C R O S ________________________________________________________
0406 
0407     /*! \name Usual Constants
0408      */
0409     //! @{
0410 #define DISABLE   0
0411 #define ENABLE    1
0412 #define DISABLED  0
0413 #define ENABLED   1
0414 #define OFF       0
0415 #define ON        1
0416 #define FALSE     0
0417 #define TRUE      1
0418 #ifndef __cplusplus
0419     #if !defined(__bool_true_false_are_defined)
0420         #define false     FALSE
0421         #define true      TRUE
0422     #endif
0423 #endif
0424 #define KO        0
0425 #define OK        1
0426 #define PASS      0
0427 #define FAIL      1
0428 #define LOW       0
0429 #define HIGH      1
0430 #define CLR       0
0431 #define SET       1
0432     //! @}
0433 
0434     /*! \brief Counts the trailing zero bits of the given value considered as a 32-bit integer.
0435      *
0436      * \param u Value of which to count the trailing zero bits.
0437      *
0438      * \return The count of trailing zero bits in \a u.
0439      */
0440 #define ctz(u)              ((u) & (1ul <<  0) ?  0 : \
0441                              (u) & (1ul <<  1) ?  1 : \
0442                              (u) & (1ul <<  2) ?  2 : \
0443                              (u) & (1ul <<  3) ?  3 : \
0444                              (u) & (1ul <<  4) ?  4 : \
0445                              (u) & (1ul <<  5) ?  5 : \
0446                              (u) & (1ul <<  6) ?  6 : \
0447                              (u) & (1ul <<  7) ?  7 : \
0448                              (u) & (1ul <<  8) ?  8 : \
0449                              (u) & (1ul <<  9) ?  9 : \
0450                              (u) & (1ul << 10) ? 10 : \
0451                              (u) & (1ul << 11) ? 11 : \
0452                              (u) & (1ul << 12) ? 12 : \
0453                              (u) & (1ul << 13) ? 13 : \
0454                              (u) & (1ul << 14) ? 14 : \
0455                              (u) & (1ul << 15) ? 15 : \
0456                              (u) & (1ul << 16) ? 16 : \
0457                              (u) & (1ul << 17) ? 17 : \
0458                              (u) & (1ul << 18) ? 18 : \
0459                              (u) & (1ul << 19) ? 19 : \
0460                              (u) & (1ul << 20) ? 20 : \
0461                              (u) & (1ul << 21) ? 21 : \
0462                              (u) & (1ul << 22) ? 22 : \
0463                              (u) & (1ul << 23) ? 23 : \
0464                              (u) & (1ul << 24) ? 24 : \
0465                              (u) & (1ul << 25) ? 25 : \
0466                              (u) & (1ul << 26) ? 26 : \
0467                              (u) & (1ul << 27) ? 27 : \
0468                              (u) & (1ul << 28) ? 28 : \
0469                              (u) & (1ul << 29) ? 29 : \
0470                              (u) & (1ul << 30) ? 30 : \
0471                              (u) & (1ul << 31) ? 31 : \
0472                              32)
0473 
0474 #endif // __ASSEMBLY__
0475 
0476 #endif  // _COMPILER_H_