1 | /* ---------------------------------------------------------------------------- */ |
---|
2 | /* Atmel Microcontroller Software Support */ |
---|
3 | /* SAM Software Package License */ |
---|
4 | /* ---------------------------------------------------------------------------- */ |
---|
5 | /* Copyright (c) 2015, Atmel Corporation */ |
---|
6 | /* */ |
---|
7 | /* All rights reserved. */ |
---|
8 | /* */ |
---|
9 | /* Redistribution and use in source and binary forms, with or without */ |
---|
10 | /* modification, are permitted provided that the following condition is met: */ |
---|
11 | /* */ |
---|
12 | /* - Redistributions of source code must retain the above copyright notice, */ |
---|
13 | /* this list of conditions and the disclaimer below. */ |
---|
14 | /* */ |
---|
15 | /* Atmel's name may not be used to endorse or promote products derived from */ |
---|
16 | /* this software without specific prior written permission. */ |
---|
17 | /* */ |
---|
18 | /* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR */ |
---|
19 | /* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */ |
---|
20 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE */ |
---|
21 | /* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, */ |
---|
22 | /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */ |
---|
23 | /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, */ |
---|
24 | /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF */ |
---|
25 | /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING */ |
---|
26 | /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, */ |
---|
27 | /* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ |
---|
28 | /* ---------------------------------------------------------------------------- */ |
---|
29 | |
---|
30 | #ifndef _COMPILER_H_ |
---|
31 | #define _COMPILER_H_ |
---|
32 | |
---|
33 | #ifdef __rtems__ |
---|
34 | #include <bspopts.h> |
---|
35 | #endif /* __rtems__ */ |
---|
36 | /* |
---|
37 | * Peripherals registers definitions |
---|
38 | */ |
---|
39 | #if defined __SAMV71J19__ \ |
---|
40 | || defined __SAMV71J20__ \ |
---|
41 | || defined __SAMV71J21__ \ |
---|
42 | || defined __SAMV71N19__ \ |
---|
43 | || defined __SAMV71N20__ \ |
---|
44 | || defined __SAMV71N21__ \ |
---|
45 | || defined __SAMV71Q19__ \ |
---|
46 | || defined __SAMV71Q20__ \ |
---|
47 | || defined __SAMV71Q21__ |
---|
48 | #include "include/samv71/samv71.h" |
---|
49 | #elif defined __SAMS70J19__ \ |
---|
50 | || defined __SAMS70J20__ \ |
---|
51 | || defined __SAMS70J21__ \ |
---|
52 | || defined __SAMS70N19__ \ |
---|
53 | || defined __SAMS70N20__ \ |
---|
54 | || defined __SAMS70N21__ \ |
---|
55 | || defined __SAMS70Q19__ \ |
---|
56 | || defined __SAMS70Q20__ \ |
---|
57 | || defined __SAMS70Q21__ |
---|
58 | #include "include/sams70/sams70.h" |
---|
59 | #elif defined __SAME70J19__ \ |
---|
60 | || defined __SAME70J20__ \ |
---|
61 | || defined __SAME70J21__ \ |
---|
62 | || defined __SAME70N19__ \ |
---|
63 | || defined __SAME70N20__ \ |
---|
64 | || defined __SAME70N21__ \ |
---|
65 | || defined __SAME70Q19__ \ |
---|
66 | || defined __SAME70Q20__ \ |
---|
67 | || defined __SAME70Q21__ |
---|
68 | #include "include/same70/same70.h" |
---|
69 | #else |
---|
70 | #error "please define correct macro for the chip first!" |
---|
71 | #endif |
---|
72 | |
---|
73 | |
---|
74 | //_____ D E C L A R A T I O N S ____________________________________________ |
---|
75 | |
---|
76 | #ifndef __ASSEMBLY__ |
---|
77 | |
---|
78 | #include <stddef.h> |
---|
79 | #include <stdlib.h> |
---|
80 | #include <stdbool.h> |
---|
81 | #include <stdint.h> |
---|
82 | |
---|
83 | /* Define WEAK attribute */ |
---|
84 | #if defined (__CC_ARM) |
---|
85 | #define WEAK __attribute__ ((weak)) |
---|
86 | #elif defined (__ICCARM__) |
---|
87 | #define WEAK __weak |
---|
88 | #elif defined (__GNUC__) |
---|
89 | #define WEAK __attribute__ ((weak)) |
---|
90 | #endif |
---|
91 | |
---|
92 | /* Define Compiler name of tool chains */ |
---|
93 | #if defined (__CC_ARM) |
---|
94 | #define COMPILER_NAME "KEIL" |
---|
95 | #elif defined (__ICCARM__) |
---|
96 | #define COMPILER_NAME "IAR" |
---|
97 | #elif defined (__GNUC__) |
---|
98 | #define COMPILER_NAME "GCC" |
---|
99 | #endif |
---|
100 | |
---|
101 | /* Define NO_INIT attribute */ |
---|
102 | #if defined (__CC_ARM) |
---|
103 | #define NO_INIT |
---|
104 | #elif defined (__ICCARM__) |
---|
105 | #define NO_INIT __no_init |
---|
106 | #elif defined (__GNUC__) |
---|
107 | #define NO_INIT |
---|
108 | #endif |
---|
109 | |
---|
110 | |
---|
111 | /* Define memory sync for tool chains */ |
---|
112 | #if defined (__CC_ARM) |
---|
113 | #define memory_sync() __dsb(15);__isb(15); |
---|
114 | #elif defined (__ICCARM__) |
---|
115 | #define memory_sync() __DSB();__ISB(); |
---|
116 | #elif defined (__GNUC__) |
---|
117 | #define memory_sync() __DSB();__ISB(); |
---|
118 | #endif |
---|
119 | |
---|
120 | /* Define memory barrier for tool chains */ |
---|
121 | #if defined (__CC_ARM) |
---|
122 | #define memory_barrier() __dmb(15); |
---|
123 | #elif defined (__ICCARM__) |
---|
124 | #define memory_barrier() __DMB(); |
---|
125 | #elif defined (__GNUC__) |
---|
126 | #define memory_barrier() __DMB(); |
---|
127 | #endif |
---|
128 | |
---|
129 | /*! \name Token Paste |
---|
130 | * |
---|
131 | * Paste N preprocessing tokens together, these tokens being allowed to be \#defined. |
---|
132 | * |
---|
133 | * May be used only within macros with the tokens passed as arguments if the tokens are \#defined. |
---|
134 | * |
---|
135 | * For example, writing TPASTE2(U, WIDTH) within a macro \#defined by |
---|
136 | * UTYPE(WIDTH) and invoked as UTYPE(UL_WIDTH) with UL_WIDTH \#defined as 32 is |
---|
137 | * equivalent to writing U32. |
---|
138 | */ |
---|
139 | //! @{ |
---|
140 | #define TPASTE2(a, b) a##b |
---|
141 | #define TPASTE3(a, b, c) a##b##c |
---|
142 | //! @} |
---|
143 | |
---|
144 | /*! \name Absolute Token Paste |
---|
145 | * |
---|
146 | * Paste N preprocessing tokens together, these tokens being allowed to be \#defined. |
---|
147 | * |
---|
148 | * No restriction of use if the tokens are \#defined. |
---|
149 | * |
---|
150 | * For example, writing ATPASTE2(U, UL_WIDTH) anywhere with UL_WIDTH \#defined |
---|
151 | * as 32 is equivalent to writing U32. |
---|
152 | */ |
---|
153 | //! @{ |
---|
154 | #define ATPASTE2(a, b) TPASTE2(a, b) |
---|
155 | #define ATPASTE3(a, b, c) TPASTE3(a, b, c) |
---|
156 | //! @} |
---|
157 | |
---|
158 | |
---|
159 | /** |
---|
160 | * \brief Emit the compiler pragma \a arg. |
---|
161 | * |
---|
162 | * \param arg The pragma directive as it would appear after \e \#pragma |
---|
163 | * (i.e. not stringified). |
---|
164 | */ |
---|
165 | #define COMPILER_PRAGMA(arg) _Pragma(#arg) |
---|
166 | |
---|
167 | /** |
---|
168 | * \def COMPILER_PACK_SET(alignment) |
---|
169 | * \brief Set maximum alignment for subsequent structure and union |
---|
170 | * definitions to \a alignment. |
---|
171 | */ |
---|
172 | #define COMPILER_PACK_SET(alignment) COMPILER_PRAGMA(pack(alignment)) |
---|
173 | |
---|
174 | /** |
---|
175 | * \def COMPILER_PACK_RESET() |
---|
176 | * \brief Set default alignment for subsequent structure and union |
---|
177 | * definitions. |
---|
178 | */ |
---|
179 | #define COMPILER_PACK_RESET() COMPILER_PRAGMA(pack()) |
---|
180 | |
---|
181 | /** |
---|
182 | * \brief Set user-defined section. |
---|
183 | * Place a data object or a function in a user-defined section. |
---|
184 | */ |
---|
185 | #if defined (__CC_ARM) |
---|
186 | #define COMPILER_SECTION(a) __attribute__((__section__(a))) |
---|
187 | #elif defined (__ICCARM__) |
---|
188 | #define COMPILER_SECTION(a) COMPILER_PRAGMA(location = a) |
---|
189 | #elif defined (__GNUC__) |
---|
190 | #define COMPILER_SECTION(a) __attribute__((__section__(a))) |
---|
191 | #endif |
---|
192 | |
---|
193 | /** |
---|
194 | * \brief Set aligned boundary. |
---|
195 | */ |
---|
196 | #if defined (__CC_ARM) |
---|
197 | #define COMPILER_ALIGNED(a) __attribute__((__aligned__(a))) |
---|
198 | #elif defined (__ICCARM__) |
---|
199 | #define COMPILER_ALIGNED(a) COMPILER_PRAGMA(data_alignment = a) |
---|
200 | #elif defined (__GNUC__) |
---|
201 | #define COMPILER_ALIGNED(a) __attribute__((__aligned__(a))) |
---|
202 | #endif |
---|
203 | |
---|
204 | /** |
---|
205 | * \brief Set word-aligned boundary. |
---|
206 | */ |
---|
207 | |
---|
208 | #if defined (__CC_ARM) |
---|
209 | #define COMPILER_WORD_ALIGNED __attribute__((__aligned__(4))) |
---|
210 | #elif defined (__ICCARM__) |
---|
211 | #define COMPILER_WORD_ALIGNED COMPILER_PRAGMA(data_alignment = 4) |
---|
212 | #elif defined (__GNUC__) |
---|
213 | #define COMPILER_WORD_ALIGNED __attribute__((__aligned__(4))) |
---|
214 | #endif |
---|
215 | |
---|
216 | |
---|
217 | |
---|
218 | /*! \name Mathematics |
---|
219 | * |
---|
220 | * The same considerations as for clz and ctz apply here but GCC does not |
---|
221 | * provide built-in functions to access the assembly instructions abs, min and |
---|
222 | * max and it does not produce them by itself in most cases, so two sets of |
---|
223 | * macros are defined here: |
---|
224 | * - Abs, Min and Max to apply to constant expressions (values known at |
---|
225 | * compile time); |
---|
226 | * - abs, min and max to apply to non-constant expressions (values unknown at |
---|
227 | * compile time), abs is found in stdlib.h. |
---|
228 | */ |
---|
229 | //! @{ |
---|
230 | |
---|
231 | /*! \brief Takes the absolute value of \a a. |
---|
232 | * |
---|
233 | * \param a Input value. |
---|
234 | * |
---|
235 | * \return Absolute value of \a a. |
---|
236 | * |
---|
237 | * \note More optimized if only used with values known at compile time. |
---|
238 | */ |
---|
239 | #define Abs(a) (((a) < 0) ? -(a) : (a)) |
---|
240 | |
---|
241 | /*! \brief Takes the minimal value of \a a and \a b. |
---|
242 | * |
---|
243 | * \param a Input value. |
---|
244 | * \param b Input value. |
---|
245 | * |
---|
246 | * \return Minimal value of \a a and \a b. |
---|
247 | * |
---|
248 | * \note More optimized if only used with values known at compile time. |
---|
249 | */ |
---|
250 | #define Min(a, b) (((a) < (b)) ? (a) : (b)) |
---|
251 | |
---|
252 | /*! \brief Takes the maximal value of \a a and \a b. |
---|
253 | * |
---|
254 | * \param a Input value. |
---|
255 | * \param b Input value. |
---|
256 | * |
---|
257 | * \return Maximal value of \a a and \a b. |
---|
258 | * |
---|
259 | * \note More optimized if only used with values known at compile time. |
---|
260 | */ |
---|
261 | #define Max(a, b) (((a) > (b)) ? (a) : (b)) |
---|
262 | |
---|
263 | // abs() is already defined by stdlib.h |
---|
264 | |
---|
265 | /*! \brief Takes the minimal value of \a a and \a b. |
---|
266 | * |
---|
267 | * \param a Input value. |
---|
268 | * \param b Input value. |
---|
269 | * |
---|
270 | * \return Minimal value of \a a and \a b. |
---|
271 | * |
---|
272 | * \note More optimized if only used with values unknown at compile time. |
---|
273 | */ |
---|
274 | #define min(a, b) Min(a, b) |
---|
275 | |
---|
276 | /*! \brief Takes the maximal value of \a a and \a b. |
---|
277 | * |
---|
278 | * \param a Input value. |
---|
279 | * \param b Input value. |
---|
280 | * |
---|
281 | * \return Maximal value of \a a and \a b. |
---|
282 | * |
---|
283 | * \note More optimized if only used with values unknown at compile time. |
---|
284 | */ |
---|
285 | #define max(a, b) Max(a, b) |
---|
286 | |
---|
287 | //! @} |
---|
288 | |
---|
289 | #define be32_to_cpu(x) __REV(x) |
---|
290 | #define cpu_to_be32(x) __REV(x) |
---|
291 | #define BE32_TO_CPU(x) __REV(x) |
---|
292 | #define CPU_TO_BE32(x) __REV(x) |
---|
293 | |
---|
294 | /** |
---|
295 | * \def UNUSED |
---|
296 | * \brief Marking \a v as a unused parameter or value. |
---|
297 | */ |
---|
298 | #define UNUSED(v) (void)(v) |
---|
299 | |
---|
300 | /** |
---|
301 | * \weakgroup interrupt_group |
---|
302 | * |
---|
303 | * @{ |
---|
304 | */ |
---|
305 | |
---|
306 | /** |
---|
307 | * \name Interrupt Service Routine definition |
---|
308 | * |
---|
309 | * @{ |
---|
310 | */ |
---|
311 | |
---|
312 | /** |
---|
313 | * \brief Initialize interrupt vectors |
---|
314 | * |
---|
315 | * For NVIC the interrupt vectors are put in vector table. So nothing |
---|
316 | * to do to initialize them, except defined the vector function with |
---|
317 | * right name. |
---|
318 | * |
---|
319 | * This must be called prior to \ref irq_register_handler. |
---|
320 | */ |
---|
321 | # define irq_initialize_vectors() \ |
---|
322 | do { \ |
---|
323 | } while (0) |
---|
324 | |
---|
325 | /** |
---|
326 | * \brief Register handler for interrupt |
---|
327 | * |
---|
328 | * For NVIC the interrupt vectors are put in vector table. So nothing |
---|
329 | * to do to register them, except defined the vector function with |
---|
330 | * right name. |
---|
331 | * |
---|
332 | * Usage: |
---|
333 | * \code |
---|
334 | irq_initialize_vectors(); |
---|
335 | irq_register_handler(foo_irq_handler); |
---|
336 | \endcode |
---|
337 | * |
---|
338 | * \note The function \a func must be defined with the \ref ISR macro. |
---|
339 | * \note The functions prototypes can be found in the device exception header |
---|
340 | * files (exceptions.h). |
---|
341 | */ |
---|
342 | # define irq_register_handler(int_num, int_prio) \ |
---|
343 | NVIC_ClearPendingIRQ((IRQn_Type)int_num); \ |
---|
344 | NVIC_SetPriority((IRQn_Type)int_num, int_prio); \ |
---|
345 | NVIC_EnableIRQ((IRQn_Type)int_num); \ |
---|
346 | |
---|
347 | //@} |
---|
348 | |
---|
349 | |
---|
350 | # define cpu_irq_enable() \ |
---|
351 | do { \ |
---|
352 | /*g_interrupt_enabled = true; */ \ |
---|
353 | __DMB(); \ |
---|
354 | __enable_irq(); \ |
---|
355 | } while (0) |
---|
356 | # define cpu_irq_disable() \ |
---|
357 | do { \ |
---|
358 | __disable_irq(); \ |
---|
359 | __DMB(); \ |
---|
360 | /*g_interrupt_enabled = false; */ \ |
---|
361 | } while (0) |
---|
362 | |
---|
363 | typedef uint32_t irqflags_t; |
---|
364 | |
---|
365 | #if !defined(__DOXYGEN__) |
---|
366 | extern volatile bool g_interrupt_enabled; |
---|
367 | #endif |
---|
368 | |
---|
369 | #define cpu_irq_is_enabled() (__get_PRIMASK() == 0) |
---|
370 | |
---|
371 | static volatile uint32_t cpu_irq_critical_section_counter; |
---|
372 | static volatile bool cpu_irq_prev_interrupt_state; |
---|
373 | |
---|
374 | static inline irqflags_t cpu_irq_save(void) |
---|
375 | { |
---|
376 | irqflags_t flags = cpu_irq_is_enabled(); |
---|
377 | cpu_irq_disable(); |
---|
378 | return flags; |
---|
379 | } |
---|
380 | |
---|
381 | static inline bool cpu_irq_is_enabled_flags(irqflags_t flags) |
---|
382 | { |
---|
383 | return (flags); |
---|
384 | } |
---|
385 | |
---|
386 | static inline void cpu_irq_restore(irqflags_t flags) |
---|
387 | { |
---|
388 | if (cpu_irq_is_enabled_flags(flags)) |
---|
389 | cpu_irq_enable(); |
---|
390 | } |
---|
391 | /* |
---|
392 | void cpu_irq_enter_critical(void); |
---|
393 | void cpu_irq_leave_critical(void);*/ |
---|
394 | |
---|
395 | /** |
---|
396 | * \weakgroup interrupt_deprecated_group |
---|
397 | * @{ |
---|
398 | */ |
---|
399 | |
---|
400 | #define Enable_global_interrupt() cpu_irq_enable() |
---|
401 | #define Disable_global_interrupt() cpu_irq_disable() |
---|
402 | #define Is_global_interrupt_enabled() cpu_irq_is_enabled() |
---|
403 | |
---|
404 | |
---|
405 | //_____ M A C R O S ________________________________________________________ |
---|
406 | |
---|
407 | /*! \name Usual Constants |
---|
408 | */ |
---|
409 | //! @{ |
---|
410 | #define DISABLE 0 |
---|
411 | #define ENABLE 1 |
---|
412 | #define DISABLED 0 |
---|
413 | #define ENABLED 1 |
---|
414 | #define OFF 0 |
---|
415 | #define ON 1 |
---|
416 | #define FALSE 0 |
---|
417 | #define TRUE 1 |
---|
418 | #ifndef __cplusplus |
---|
419 | #if !defined(__bool_true_false_are_defined) |
---|
420 | #define false FALSE |
---|
421 | #define true TRUE |
---|
422 | #endif |
---|
423 | #endif |
---|
424 | #define KO 0 |
---|
425 | #define OK 1 |
---|
426 | #define PASS 0 |
---|
427 | #define FAIL 1 |
---|
428 | #define LOW 0 |
---|
429 | #define HIGH 1 |
---|
430 | #define CLR 0 |
---|
431 | #define SET 1 |
---|
432 | //! @} |
---|
433 | |
---|
434 | /*! \brief Counts the trailing zero bits of the given value considered as a 32-bit integer. |
---|
435 | * |
---|
436 | * \param u Value of which to count the trailing zero bits. |
---|
437 | * |
---|
438 | * \return The count of trailing zero bits in \a u. |
---|
439 | */ |
---|
440 | #define ctz(u) ((u) & (1ul << 0) ? 0 : \ |
---|
441 | (u) & (1ul << 1) ? 1 : \ |
---|
442 | (u) & (1ul << 2) ? 2 : \ |
---|
443 | (u) & (1ul << 3) ? 3 : \ |
---|
444 | (u) & (1ul << 4) ? 4 : \ |
---|
445 | (u) & (1ul << 5) ? 5 : \ |
---|
446 | (u) & (1ul << 6) ? 6 : \ |
---|
447 | (u) & (1ul << 7) ? 7 : \ |
---|
448 | (u) & (1ul << 8) ? 8 : \ |
---|
449 | (u) & (1ul << 9) ? 9 : \ |
---|
450 | (u) & (1ul << 10) ? 10 : \ |
---|
451 | (u) & (1ul << 11) ? 11 : \ |
---|
452 | (u) & (1ul << 12) ? 12 : \ |
---|
453 | (u) & (1ul << 13) ? 13 : \ |
---|
454 | (u) & (1ul << 14) ? 14 : \ |
---|
455 | (u) & (1ul << 15) ? 15 : \ |
---|
456 | (u) & (1ul << 16) ? 16 : \ |
---|
457 | (u) & (1ul << 17) ? 17 : \ |
---|
458 | (u) & (1ul << 18) ? 18 : \ |
---|
459 | (u) & (1ul << 19) ? 19 : \ |
---|
460 | (u) & (1ul << 20) ? 20 : \ |
---|
461 | (u) & (1ul << 21) ? 21 : \ |
---|
462 | (u) & (1ul << 22) ? 22 : \ |
---|
463 | (u) & (1ul << 23) ? 23 : \ |
---|
464 | (u) & (1ul << 24) ? 24 : \ |
---|
465 | (u) & (1ul << 25) ? 25 : \ |
---|
466 | (u) & (1ul << 26) ? 26 : \ |
---|
467 | (u) & (1ul << 27) ? 27 : \ |
---|
468 | (u) & (1ul << 28) ? 28 : \ |
---|
469 | (u) & (1ul << 29) ? 29 : \ |
---|
470 | (u) & (1ul << 30) ? 30 : \ |
---|
471 | (u) & (1ul << 31) ? 31 : \ |
---|
472 | 32) |
---|
473 | |
---|
474 | #endif // __ASSEMBLY__ |
---|
475 | |
---|
476 | #endif // _COMPILER_H_ |
---|