OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [freertos-6.1.1/] [Demo/] [lwIP_AVR32_UC3/] [UTILS/] [compiler.h] - Blame information for rev 800

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 583 jeremybenn
/*This file is prepared for Doxygen automatic documentation generation.*/
2
/*! \file *********************************************************************
3
 *
4
 * \brief Compiler file for AVR32.
5
 *
6
 * This file defines commonly used types and macros.
7
 *
8
 * - Compiler:           IAR EWAVR32 and GNU GCC for AVR32
9
 * - Supported devices:  All AVR32 devices can be used.
10
 * - AppNote:
11
 *
12
 * \author               Atmel Corporation: http://www.atmel.com \n
13
 *                       Support and FAQ: http://support.atmel.no/
14
 *
15
 ******************************************************************************/
16
 
17
/* Copyright (c) 2007, Atmel Corporation All rights reserved.
18
 *
19
 * Redistribution and use in source and binary forms, with or without
20
 * modification, are permitted provided that the following conditions are met:
21
 *
22
 * 1. Redistributions of source code must retain the above copyright notice,
23
 * this list of conditions and the following disclaimer.
24
 *
25
 * 2. Redistributions in binary form must reproduce the above copyright notice,
26
 * this list of conditions and the following disclaimer in the documentation
27
 * and/or other materials provided with the distribution.
28
 *
29
 * 3. The name of ATMEL may not be used to endorse or promote products derived
30
 * from this software without specific prior written permission.
31
 *
32
 * THIS SOFTWARE IS PROVIDED BY ATMEL ``AS IS'' AND ANY EXPRESS OR IMPLIED
33
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
34
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
35
 * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
36
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
37
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
38
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
39
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
41
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42
 */
43
 
44
 
45
#ifndef _COMPILER_H_
46
#define _COMPILER_H_
47
 
48
#if (__GNUC__ && __AVR32__) || (__ICCAVR32__ || __AAVR32__)
49
#  include <avr32/io.h>
50
#endif
51
#if __ICCAVR32__
52
#  include <intrinsics.h>
53
#endif
54
#include "preprocessor.h"
55
 
56
 
57
//_____ D E C L A R A T I O N S ____________________________________________
58
 
59
#ifdef __AVR32_ABI_COMPILER__ // Automatically defined when compiling for AVR32, not when assembling.
60
 
61
#include <stddef.h>
62
#include <stdlib.h>
63
 
64
 
65
#if __ICCAVR32__
66
 
67
/*! \name Compiler Keywords
68
 *
69
 * Port of some keywords from GNU GCC for AVR32 to IAR Embedded Workbench for Atmel AVR32.
70
 */
71
//! @{
72
#define __asm__             asm
73
#define __inline__          inline
74
#define __volatile__
75
//! @}
76
 
77
#endif
78
 
79
 
80
/*! \name Usual Types
81
 */
82
//! @{
83
typedef unsigned char           Bool; //!< Boolean.
84
typedef unsigned char           U8 ;  //!< 8-bit unsigned integer.
85
typedef unsigned short int      U16;  //!< 16-bit unsigned integer.
86
typedef unsigned long int       U32;  //!< 32-bit unsigned integer.
87
typedef unsigned long long int  U64;  //!< 64-bit unsigned integer.
88
typedef signed char             S8 ;  //!< 8-bit signed integer.
89
typedef signed short int        S16;  //!< 16-bit signed integer.
90
typedef signed long int         S32;  //!< 32-bit signed integer.
91
typedef signed long long int    S64;  //!< 64-bit signed integer.
92
typedef float                   F32;  //!< 32-bit floating-point number.
93
typedef double                  F64;  //!< 64-bit floating-point number.
94
//! @}
95
 
96
 
97
/*! \name Status Types
98
 */
99
//! @{
100
typedef Bool                Status_bool_t;  //!< Boolean status.
101
typedef U8                  Status_t;       //!< 8-bit-coded status.
102
//! @}
103
 
104
 
105
/*! \name Aliasing Aggregate Types
106
 */
107
//! @{
108
 
109
//! 16-bit union.
110
typedef union
111
{
112
  U16 u16   ;
113
  U8  u8 [2];
114
} Union16;
115
 
116
//! 32-bit union.
117
typedef union
118
{
119
  U32 u32   ;
120
  U16 u16[2];
121
  U8  u8 [4];
122
} Union32;
123
 
124
//! 64-bit union.
125
typedef union
126
{
127
  U64 u64   ;
128
  U32 u32[2];
129
  U16 u16[4];
130
  U8  u8 [8];
131
} Union64;
132
 
133
//! Union of pointers to 64-, 32-, 16- and 8-bit unsigned integers.
134
typedef union
135
{
136
  U64 *u64ptr;
137
  U32 *u32ptr;
138
  U16 *u16ptr;
139
  U8  *u8ptr ;
140
} UnionPtr;
141
 
142
//! Union of pointers to volatile 64-, 32-, 16- and 8-bit unsigned integers.
143
typedef union
144
{
145
  volatile U64 *u64ptr;
146
  volatile U32 *u32ptr;
147
  volatile U16 *u16ptr;
148
  volatile U8  *u8ptr ;
149
} UnionVPtr;
150
 
151
//! Union of pointers to constant 64-, 32-, 16- and 8-bit unsigned integers.
152
typedef union
153
{
154
  const U64 *u64ptr;
155
  const U32 *u32ptr;
156
  const U16 *u16ptr;
157
  const U8  *u8ptr ;
158
} UnionCPtr;
159
 
160
//! Union of pointers to constant volatile 64-, 32-, 16- and 8-bit unsigned integers.
161
typedef union
162
{
163
  const volatile U64 *u64ptr;
164
  const volatile U32 *u32ptr;
165
  const volatile U16 *u16ptr;
166
  const volatile U8  *u8ptr ;
167
} UnionCVPtr;
168
 
169
//! Structure of pointers to 64-, 32-, 16- and 8-bit unsigned integers.
170
typedef struct
171
{
172
  U64 *u64ptr;
173
  U32 *u32ptr;
174
  U16 *u16ptr;
175
  U8  *u8ptr ;
176
} StructPtr;
177
 
178
//! Structure of pointers to volatile 64-, 32-, 16- and 8-bit unsigned integers.
179
typedef struct
180
{
181
  volatile U64 *u64ptr;
182
  volatile U32 *u32ptr;
183
  volatile U16 *u16ptr;
184
  volatile U8  *u8ptr ;
185
} StructVPtr;
186
 
187
//! Structure of pointers to constant 64-, 32-, 16- and 8-bit unsigned integers.
188
typedef struct
189
{
190
  const U64 *u64ptr;
191
  const U32 *u32ptr;
192
  const U16 *u16ptr;
193
  const U8  *u8ptr ;
194
} StructCPtr;
195
 
196
//! Structure of pointers to constant volatile 64-, 32-, 16- and 8-bit unsigned integers.
197
typedef struct
198
{
199
  const volatile U64 *u64ptr;
200
  const volatile U32 *u32ptr;
201
  const volatile U16 *u16ptr;
202
  const volatile U8  *u8ptr ;
203
} StructCVPtr;
204
 
205
//! @}
206
 
207
#endif  // __AVR32_ABI_COMPILER__
208
 
209
 
210
//_____ M A C R O S ________________________________________________________
211
 
212
/*! \name Usual Constants
213
 */
214
//! @{
215
#define DISABLE   0
216
#define ENABLE    1
217
#define DISABLED  0
218
#define ENABLED   1
219
#define OFF       0
220
#define ON        1
221
#define FALSE     0
222
#define TRUE      1
223
#define KO        0
224
#define OK        1
225
#define PASS      0
226
#define FAIL      1
227
#define LOW       0
228
#define HIGH      1
229
#define CLR       0
230
#define SET       1
231
//! @}
232
 
233
 
234
#ifdef __AVR32_ABI_COMPILER__ // Automatically defined when compiling for AVR32, not when assembling.
235
 
236
/*! \name Bit-Field Handling
237
 */
238
//! @{
239
 
240
/*! \brief Reads the bits of a value specified by a given bit-mask.
241
 *
242
 * \param value Value to read bits from.
243
 * \param mask  Bit-mask indicating bits to read.
244
 *
245
 * \return Read bits.
246
 */
247
#define Rd_bits( value, mask)        ((value) & (mask))
248
 
249
/*! \brief Writes the bits of a C lvalue specified by a given bit-mask.
250
 *
251
 * \param lvalue  C lvalue to write bits to.
252
 * \param mask    Bit-mask indicating bits to write.
253
 * \param bits    Bits to write.
254
 *
255
 * \return Resulting value with written bits.
256
 */
257
#define Wr_bits(lvalue, mask, bits)  ((lvalue) = ((lvalue) & ~(mask)) |\
258
                                                 ((bits  ) &  (mask)))
259
 
260
/*! \brief Tests the bits of a value specified by a given bit-mask.
261
 *
262
 * \param value Value of which to test bits.
263
 * \param mask  Bit-mask indicating bits to test.
264
 *
265
 * \return \c 1 if at least one of the tested bits is set, else \c 0.
266
 */
267
#define Tst_bits( value, mask)  (Rd_bits(value, mask) != 0)
268
 
269
/*! \brief Clears the bits of a C lvalue specified by a given bit-mask.
270
 *
271
 * \param lvalue  C lvalue of which to clear bits.
272
 * \param mask    Bit-mask indicating bits to clear.
273
 *
274
 * \return Resulting value with cleared bits.
275
 */
276
#define Clr_bits(lvalue, mask)  ((lvalue) &= ~(mask))
277
 
278
/*! \brief Sets the bits of a C lvalue specified by a given bit-mask.
279
 *
280
 * \param lvalue  C lvalue of which to set bits.
281
 * \param mask    Bit-mask indicating bits to set.
282
 *
283
 * \return Resulting value with set bits.
284
 */
285
#define Set_bits(lvalue, mask)  ((lvalue) |=  (mask))
286
 
287
/*! \brief Toggles the bits of a C lvalue specified by a given bit-mask.
288
 *
289
 * \param lvalue  C lvalue of which to toggle bits.
290
 * \param mask    Bit-mask indicating bits to toggle.
291
 *
292
 * \return Resulting value with toggled bits.
293
 */
294
#define Tgl_bits(lvalue, mask)  ((lvalue) ^=  (mask))
295
 
296
/*! \brief Reads the bit-field of a value specified by a given bit-mask.
297
 *
298
 * \param value Value to read a bit-field from.
299
 * \param mask  Bit-mask indicating the bit-field to read.
300
 *
301
 * \return Read bit-field.
302
 */
303
#define Rd_bitfield( value, mask)           (Rd_bits( value, mask) >> ctz(mask))
304
 
305
/*! \brief Writes the bit-field of a C lvalue specified by a given bit-mask.
306
 *
307
 * \param lvalue    C lvalue to write a bit-field to.
308
 * \param mask      Bit-mask indicating the bit-field to write.
309
 * \param bitfield  Bit-field to write.
310
 *
311
 * \return Resulting value with written bit-field.
312
 */
313
#define Wr_bitfield(lvalue, mask, bitfield) (Wr_bits(lvalue, mask, (U32)(bitfield) << ctz(mask)))
314
 
315
//! @}
316
 
317
 
318
/*! \brief This macro is used to test fatal errors.
319
 *
320
 * The macro tests if the expression is FALSE. If it is, a fatal error is
321
 * detected and the application hangs up.
322
 *
323
 * \param expr  Expression to evaluate and supposed to be nonzero.
324
 */
325
#ifdef _ASSERT_ENABLE_
326
  #define Assert(expr) \
327
  {\
328
    if (!(expr)) while (TRUE);\
329
  }
330
#else
331
  #define Assert(expr)
332
#endif
333
 
334
 
335
/*! \name Zero-Bit Counting
336
 *
337
 * Under AVR32-GCC, __builtin_clz and __builtin_ctz behave like macros when
338
 * applied to constant expressions (values known at compile time), so they are
339
 * more optimized than the use of the corresponding assembly instructions and
340
 * they can be used as constant expressions e.g. to initialize objects having
341
 * static storage duration, and like the corresponding assembly instructions
342
 * when applied to non-constant expressions (values unknown at compile time), so
343
 * they are more optimized than an assembly periphrasis. Hence, clz and ctz
344
 * ensure a possible and optimized behavior for both constant and non-constant
345
 * expressions.
346
 */
347
//! @{
348
 
349
/*! \brief Counts the leading zero bits of the given value considered as a 32-bit integer.
350
 *
351
 * \param u Value of which to count the leading zero bits.
352
 *
353
 * \return The count of leading zero bits in \a u.
354
 */
355
#if __GNUC__
356
  #define clz(u)              __builtin_clz(u)
357
#elif __ICCAVR32__
358
  #define clz(u)              __count_leading_zeros(u)
359
#endif
360
 
361
/*! \brief Counts the trailing zero bits of the given value considered as a 32-bit integer.
362
 *
363
 * \param u Value of which to count the trailing zero bits.
364
 *
365
 * \return The count of trailing zero bits in \a u.
366
 */
367
#if __GNUC__
368
  #define ctz(u)              __builtin_ctz(u)
369
#elif __ICCAVR32__
370
  #define ctz(u)              __count_trailing_zeros(u)
371
#endif
372
 
373
//! @}
374
 
375
 
376
/*! \name Alignment
377
 */
378
//! @{
379
 
380
/*! \brief Tests alignment of the number \a val with the \a n boundary.
381
 *
382
 * \param val Input value.
383
 * \param n   Boundary.
384
 *
385
 * \return \c 1 if the number \a val is aligned with the \a n boundary, else \c 0.
386
 */
387
#define Test_align(val, n     ) (!Tst_bits( val, (n) - 1     )   )
388
 
389
/*! \brief Gets alignment of the number \a val with respect to the \a n boundary.
390
 *
391
 * \param val Input value.
392
 * \param n   Boundary.
393
 *
394
 * \return Alignment of the number \a val with respect to the \a n boundary.
395
 */
396
#define Get_align( val, n     ) (  Rd_bits( val, (n) - 1     )   )
397
 
398
/*! \brief Sets alignment of the lvalue number \a lval to \a alg with respect to the \a n boundary.
399
 *
400
 * \param lval  Input/output lvalue.
401
 * \param n     Boundary.
402
 * \param alg   Alignment.
403
 *
404
 * \return New value of \a lval resulting from its alignment set to \a alg with respect to the \a n boundary.
405
 */
406
#define Set_align(lval, n, alg) (  Wr_bits(lval, (n) - 1, alg)   )
407
 
408
/*! \brief Aligns the number \a val with the upper \a n boundary.
409
 *
410
 * \param val Input value.
411
 * \param n   Boundary.
412
 *
413
 * \return Value resulting from the number \a val aligned with the upper \a n boundary.
414
 */
415
#define Align_up(  val, n     ) (((val) + ((n) - 1)) & ~((n) - 1))
416
 
417
/*! \brief Aligns the number \a val with the lower \a n boundary.
418
 *
419
 * \param val Input value.
420
 * \param n   Boundary.
421
 *
422
 * \return Value resulting from the number \a val aligned with the lower \a n boundary.
423
 */
424
#define Align_down(val, n     ) ( (val)              & ~((n) - 1))
425
 
426
//! @}
427
 
428
 
429
/*! \name Mathematics
430
 *
431
 * The same considerations as for clz and ctz apply here but AVR32-GCC does not
432
 * provide built-in functions to access the assembly instructions abs, min and
433
 * max and it does not produce them by itself in most cases, so two sets of
434
 * macros are defined here:
435
 *   - Abs, Min and Max to apply to constant expressions (values known at
436
 *     compile time);
437
 *   - abs, min and max to apply to non-constant expressions (values unknown at
438
 *     compile time).
439
 */
440
//! @{
441
 
442
/*! \brief Takes the absolute value of \a a.
443
 *
444
 * \param a Input value.
445
 *
446
 * \return Absolute value of \a a.
447
 *
448
 * \note More optimized if only used with values known at compile time.
449
 */
450
#define Abs(a)              (((a) <  0 ) ? -(a) : (a))
451
 
452
/*! \brief Takes the minimal value of \a a and \a b.
453
 *
454
 * \param a Input value.
455
 * \param b Input value.
456
 *
457
 * \return Minimal value of \a a and \a b.
458
 *
459
 * \note More optimized if only used with values known at compile time.
460
 */
461
#define Min(a, b)           (((a) < (b)) ?  (a) : (b))
462
 
463
/*! \brief Takes the maximal value of \a a and \a b.
464
 *
465
 * \param a Input value.
466
 * \param b Input value.
467
 *
468
 * \return Maximal value of \a a and \a b.
469
 *
470
 * \note More optimized if only used with values known at compile time.
471
 */
472
#define Max(a, b)           (((a) > (b)) ?  (a) : (b))
473
 
474
/*! \brief Takes the absolute value of \a a.
475
 *
476
 * \param a Input value.
477
 *
478
 * \return Absolute value of \a a.
479
 *
480
 * \note More optimized if only used with values unknown at compile time.
481
 */
482
#if __GNUC__
483
  #define abs(a) \
484
  (\
485
    {\
486
      int __value = (a);\
487
      __asm__ ("abs\t%0" : "+r" (__value) :  : "cc");\
488
      __value;\
489
    }\
490
  )
491
#elif __ICCAVR32__
492
  #define abs(a)      Abs(a)
493
#endif
494
 
495
/*! \brief Takes the minimal value of \a a and \a b.
496
 *
497
 * \param a Input value.
498
 * \param b Input value.
499
 *
500
 * \return Minimal value of \a a and \a b.
501
 *
502
 * \note More optimized if only used with values unknown at compile time.
503
 */
504
#if __GNUC__
505
  #define min(a, b) \
506
  (\
507
    {\
508
      int __value, __arg_a = (a), __arg_b = (b);\
509
      __asm__ ("min\t%0, %1, %2" : "=r" (__value) : "r" (__arg_a), "r" (__arg_b));\
510
      __value;\
511
    }\
512
  )
513
#elif __ICCAVR32__
514
  #define min(a, b)   __min(a, b)
515
#endif
516
 
517
/*! \brief Takes the maximal value of \a a and \a b.
518
 *
519
 * \param a Input value.
520
 * \param b Input value.
521
 *
522
 * \return Maximal value of \a a and \a b.
523
 *
524
 * \note More optimized if only used with values unknown at compile time.
525
 */
526
#if __GNUC__
527
  #define max(a, b) \
528
  (\
529
    {\
530
      int __value, __arg_a = (a), __arg_b = (b);\
531
      __asm__ ("max\t%0, %1, %2" : "=r" (__value) : "r" (__arg_a), "r" (__arg_b));\
532
      __value;\
533
    }\
534
  )
535
#elif __ICCAVR32__
536
  #define max(a, b)   __max(a, b)
537
#endif
538
 
539
//! @}
540
 
541
 
542
/*! \brief Calls the routine at address \a addr.
543
 *
544
 * It generates a long call opcode.
545
 *
546
 * For example, `Long_call(0x80000000)' generates a software reset on a UC3 if
547
 * it is invoked from the CPU supervisor mode.
548
 *
549
 * \param addr  Address of the routine to call.
550
 *
551
 * \note It may be used as a long jump opcode in some special cases.
552
 */
553
#define Long_call(addr)                   ((*(void (*)(void))(addr))())
554
 
555
/*! \brief Resets the CPU by software.
556
 *
557
 * \warning It shall not be called from the CPU application mode.
558
 */
559
#if __GNUC__
560
  #define Reset_CPU() \
561
  (\
562
    {\
563
      __asm__ __volatile__ (\
564
        "lddpc   r9, 3f\n\t"\
565
        "mfsr    r8, %[SR]\n\t"\
566
        "bfextu  r8, r8, %[SR_MX_OFFSET], %[SR_MX_SIZE]\n\t"\
567
        "cp.w    r8, 0b001\n\t"\
568
        "breq    0f\n\t"\
569
        "sub     r8, pc, $ - 1f\n\t"\
570
        "pushm   r8-r9\n\t"\
571
        "rete\n"\
572
        "0:\n\t"\
573
        "mtsr    %[SR], r9\n"\
574
        "1:\n\t"\
575
        "mov     r0, 0\n\t"\
576
        "mov     r1, 0\n\t"\
577
        "mov     r2, 0\n\t"\
578
        "mov     r3, 0\n\t"\
579
        "mov     r4, 0\n\t"\
580
        "mov     r5, 0\n\t"\
581
        "mov     r6, 0\n\t"\
582
        "mov     r7, 0\n\t"\
583
        "mov     r8, 0\n\t"\
584
        "mov     r9, 0\n\t"\
585
        "mov     r10, 0\n\t"\
586
        "mov     r11, 0\n\t"\
587
        "mov     r12, 0\n\t"\
588
        "mov     sp, 0\n\t"\
589
        "stdsp   sp[0], sp\n\t"\
590
        "ldmts   sp, sp\n\t"\
591
        "mov     lr, 0\n\t"\
592
        "lddpc   pc, 2f\n\t"\
593
        ".balign 4\n"\
594
        "2:\n\t"\
595
        ".word   _start\n"\
596
        "3:\n\t"\
597
        ".word   %[RESET_SR]"\
598
        :\
599
        : [SR] "i" (AVR32_SR),\
600
          [SR_MX_OFFSET] "i" (AVR32_SR_M0_OFFSET),\
601
          [SR_MX_SIZE] "i" (AVR32_SR_M0_SIZE + AVR32_SR_M1_SIZE + AVR32_SR_M2_SIZE),\
602
          [RESET_SR] "i" (AVR32_SR_GM_MASK | AVR32_SR_EM_MASK | AVR32_SR_M0_MASK)\
603
      );\
604
    }\
605
  )
606
#elif __ICCAVR32__
607
  #define Reset_CPU() \
608
  {\
609
    extern void *volatile __program_start;\
610
    __asm__ __volatile__ (\
611
      "mov     r7, LWRD(__program_start)\n\t"\
612
      "orh     r7, HWRD(__program_start)\n\t"\
613
      "mov     r9, LWRD("ASTRINGZ(AVR32_SR_GM_MASK | AVR32_SR_EM_MASK | AVR32_SR_M0_MASK)")\n\t"\
614
      "orh     r9, HWRD("ASTRINGZ(AVR32_SR_GM_MASK | AVR32_SR_EM_MASK | AVR32_SR_M0_MASK)")\n\t"\
615
      "mfsr    r8, "ASTRINGZ(AVR32_SR)"\n\t"\
616
      "bfextu  r8, r8, "ASTRINGZ(AVR32_SR_M0_OFFSET)", "ASTRINGZ(AVR32_SR_M0_SIZE + AVR32_SR_M1_SIZE + AVR32_SR_M2_SIZE)"\n\t"\
617
      "cp.w    r8, 001b\n\t"\
618
      "breq    $ + 10\n\t"\
619
      "sub     r8, pc, -12\n\t"\
620
      "pushm   r8-r9\n\t"\
621
      "rete\n\t"\
622
      "mtsr    "ASTRINGZ(AVR32_SR)", r9\n\t"\
623
      "mov     r0, 0\n\t"\
624
      "mov     r1, 0\n\t"\
625
      "mov     r2, 0\n\t"\
626
      "mov     r3, 0\n\t"\
627
      "mov     r4, 0\n\t"\
628
      "mov     r5, 0\n\t"\
629
      "mov     r6, 0\n\t"\
630
      "st.w    r0[4], r7\n\t"\
631
      "mov     r7, 0\n\t"\
632
      "mov     r8, 0\n\t"\
633
      "mov     r9, 0\n\t"\
634
      "mov     r10, 0\n\t"\
635
      "mov     r11, 0\n\t"\
636
      "mov     r12, 0\n\t"\
637
      "mov     sp, 0\n\t"\
638
      "stdsp   sp[0], sp\n\t"\
639
      "ldmts   sp, sp\n\t"\
640
      "mov     lr, 0\n\t"\
641
      "ld.w    pc, lr[4]"\
642
    );\
643
    __program_start;\
644
  }
645
#endif
646
 
647
 
648
/*! \name System Register Access
649
 */
650
//! @{
651
 
652
/*! \brief Gets the value of the \a sysreg system register.
653
 *
654
 * \param sysreg  Address of the system register of which to get the value.
655
 *
656
 * \return Value of the \a sysreg system register.
657
 */
658
#if __GNUC__
659
  #define Get_system_register(sysreg)         __builtin_mfsr(sysreg)
660
#elif __ICCAVR32__
661
  #define Get_system_register(sysreg)         __get_system_register(sysreg)
662
#endif
663
 
664
/*! \brief Sets the value of the \a sysreg system register to \a value.
665
 *
666
 * \param sysreg  Address of the system register of which to set the value.
667
 * \param value   Value to set the \a sysreg system register to.
668
 */
669
#if __GNUC__
670
  #define Set_system_register(sysreg, value)  __builtin_mtsr(sysreg, value)
671
#elif __ICCAVR32__
672
  #define Set_system_register(sysreg, value)  __set_system_register(sysreg, value)
673
#endif
674
 
675
//! @}
676
 
677
 
678
/*! \name CPU Status Register Access
679
 */
680
//! @{
681
 
682
/*! \brief Tells whether exceptions are globally enabled.
683
 *
684
 * \return \c 1 if exceptions are globally enabled, else \c 0.
685
 */
686
#define Is_global_exception_enabled()       (!Tst_bits(Get_system_register(AVR32_SR), AVR32_SR_EM_MASK))
687
 
688
/*! \brief Disables exceptions globally.
689
 */
690
#if __GNUC__
691
  #define Disable_global_exception()        ({__asm__ __volatile__ ("ssrf\t%0" :  : "i" (AVR32_SR_EM_OFFSET));})
692
#elif __ICCAVR32__
693
  #define Disable_global_exception()        (__set_status_flag(AVR32_SR_EM_OFFSET))
694
#endif
695
 
696
/*! \brief Enables exceptions globally.
697
 */
698
#if __GNUC__
699
  #define Enable_global_exception()         ({__asm__ __volatile__ ("csrf\t%0" :  : "i" (AVR32_SR_EM_OFFSET));})
700
#elif __ICCAVR32__
701
  #define Enable_global_exception()         (__clear_status_flag(AVR32_SR_EM_OFFSET))
702
#endif
703
 
704
/*! \brief Tells whether interrupts are globally enabled.
705
 *
706
 * \return \c 1 if interrupts are globally enabled, else \c 0.
707
 */
708
#define Is_global_interrupt_enabled()       (!Tst_bits(Get_system_register(AVR32_SR), AVR32_SR_GM_MASK))
709
 
710
/*! \brief Disables interrupts globally.
711
 */
712
#if __GNUC__
713
  #define Disable_global_interrupt()        ({__asm__ __volatile__ ("ssrf\t%0\n\tnop\n\tnop" :  : "i" (AVR32_SR_GM_OFFSET));})
714
#elif __ICCAVR32__
715
  #define Disable_global_interrupt()        {__asm__ __volatile__ ("ssrf\t"ASTRINGZ(AVR32_SR_GM_OFFSET)"\n\tnop\n\tnop");}
716
#endif
717
 
718
/*! \brief Enables interrupts globally.
719
 */
720
#if __GNUC__
721
  #define Enable_global_interrupt()         ({__asm__ __volatile__ ("csrf\t%0" :  : "i" (AVR32_SR_GM_OFFSET));})
722
#elif __ICCAVR32__
723
  #define Enable_global_interrupt()         (__enable_interrupt())
724
#endif
725
 
726
/*! \brief Tells whether interrupt level \a int_lev is enabled.
727
 *
728
 * \param int_lev Interrupt level (0 to 3).
729
 *
730
 * \return \c 1 if interrupt level \a int_lev is enabled, else \c 0.
731
 */
732
#define Is_interrupt_level_enabled(int_lev) (!Tst_bits(Get_system_register(AVR32_SR), TPASTE3(AVR32_SR_I, int_lev, M_MASK)))
733
 
734
/*! \brief Disables interrupt level \a int_lev.
735
 *
736
 * \param int_lev Interrupt level to disable (0 to 3).
737
 */
738
#if __GNUC__
739
  #define Disable_interrupt_level(int_lev)  ({__asm__ __volatile__ ("ssrf\t%0\n\tnop\n\tnop" :  : "i" (TPASTE3(AVR32_SR_I, int_lev, M_OFFSET)));})
740
#elif __ICCAVR32__
741
  #define Disable_interrupt_level(int_lev)  {__asm__ __volatile__ ("ssrf\t"ASTRINGZ(TPASTE3(AVR32_SR_I, int_lev, M_OFFSET))"\n\tnop\n\tnop");}
742
#endif
743
 
744
/*! \brief Enables interrupt level \a int_lev.
745
 *
746
 * \param int_lev Interrupt level to enable (0 to 3).
747
 */
748
#if __GNUC__
749
  #define Enable_interrupt_level(int_lev)   ({__asm__ __volatile__ ("csrf\t%0" :  : "i" (TPASTE3(AVR32_SR_I, int_lev, M_OFFSET)));})
750
#elif __ICCAVR32__
751
  #define Enable_interrupt_level(int_lev)   (__clear_status_flag(TPASTE3(AVR32_SR_I, int_lev, M_OFFSET)))
752
#endif
753
 
754
//! @}
755
 
756
 
757
/*! \name Debug Register Access
758
 */
759
//! @{
760
 
761
/*! \brief Gets the value of the \a dbgreg debug register.
762
 *
763
 * \param dbgreg  Address of the debug register of which to get the value.
764
 *
765
 * \return Value of the \a dbgreg debug register.
766
 */
767
#if __GNUC__
768
  #define Get_debug_register(dbgreg)          __builtin_mfdr(dbgreg)
769
#elif __ICCAVR32__
770
  #define Get_debug_register(dbgreg)          __get_debug_register(dbgreg)
771
#endif
772
 
773
/*! \brief Sets the value of the \a dbgreg debug register to \a value.
774
 *
775
 * \param dbgreg  Address of the debug register of which to set the value.
776
 * \param value   Value to set the \a dbgreg debug register to.
777
 */
778
#if __GNUC__
779
  #define Set_debug_register(dbgreg, value)   __builtin_mtdr(dbgreg, value)
780
#elif __ICCAVR32__
781
  #define Set_debug_register(dbgreg, value)   __set_debug_register(dbgreg, value)
782
#endif
783
 
784
//! @}
785
 
786
#endif  // __AVR32_ABI_COMPILER__
787
 
788
 
789
//! Boolean evaluating MCU little endianism.
790
#if (__GNUC__ && __AVR32__) || (__ICCAVR32__ || __AAVR32__)
791
  #define LITTLE_ENDIAN_MCU     FALSE
792
#endif
793
 
794
// Check that MCU endianism is correctly defined.
795
#ifndef LITTLE_ENDIAN_MCU
796
  #error YOU MUST define the MCU endianism with LITTLE_ENDIAN_MCU: either FALSE or TRUE
797
#endif
798
 
799
//! Boolean evaluating MCU big endianism.
800
#define BIG_ENDIAN_MCU        (!LITTLE_ENDIAN_MCU)
801
 
802
 
803
#ifdef __AVR32_ABI_COMPILER__ // Automatically defined when compiling for AVR32, not when assembling.
804
 
805
/*! \name MCU Endianism Handling
806
 */
807
//! @{
808
 
809
#if LITTLE_ENDIAN_MCU
810
 
811
  #define LSB(u16)        (((U8  *)&(u16))[0])  //!< Least significant byte of \a u16.
812
  #define MSB(u16)        (((U8  *)&(u16))[1])  //!< Most significant byte of \a u16.
813
 
814
  #define LSH(u32)        (((U16 *)&(u32))[0])  //!< Least significant half-word of \a u32.
815
  #define MSH(u32)        (((U16 *)&(u32))[1])  //!< Most significant half-word of \a u32.
816
  #define LSB0W(u32)      (((U8  *)&(u32))[0])  //!< Least significant byte of 1st rank of \a u32.
817
  #define LSB1W(u32)      (((U8  *)&(u32))[1])  //!< Least significant byte of 2nd rank of \a u32.
818
  #define LSB2W(u32)      (((U8  *)&(u32))[2])  //!< Least significant byte of 3rd rank of \a u32.
819
  #define LSB3W(u32)      (((U8  *)&(u32))[3])  //!< Least significant byte of 4th rank of \a u32.
820
  #define MSB3W(u32)      LSB0W(u32)            //!< Most significant byte of 4th rank of \a u32.
821
  #define MSB2W(u32)      LSB1W(u32)            //!< Most significant byte of 3rd rank of \a u32.
822
  #define MSB1W(u32)      LSB2W(u32)            //!< Most significant byte of 2nd rank of \a u32.
823
  #define MSB0W(u32)      LSB3W(u32)            //!< Most significant byte of 1st rank of \a u32.
824
 
825
  #define LSW(u64)        (((U32 *)&(u64))[0])  //!< Least significant word of \a u64.
826
  #define MSW(u64)        (((U32 *)&(u64))[1])  //!< Most significant word of \a u64.
827
  #define LSH0(u64)       (((U16 *)&(u64))[0])  //!< Least significant half-word of 1st rank of \a u64.
828
  #define LSH1(u64)       (((U16 *)&(u64))[1])  //!< Least significant half-word of 2nd rank of \a u64.
829
  #define LSH2(u64)       (((U16 *)&(u64))[2])  //!< Least significant half-word of 3rd rank of \a u64.
830
  #define LSH3(u64)       (((U16 *)&(u64))[3])  //!< Least significant half-word of 4th rank of \a u64.
831
  #define MSH3(u64)       LSH0(u64)             //!< Most significant half-word of 4th rank of \a u64.
832
  #define MSH2(u64)       LSH1(u64)             //!< Most significant half-word of 3rd rank of \a u64.
833
  #define MSH1(u64)       LSH2(u64)             //!< Most significant half-word of 2nd rank of \a u64.
834
  #define MSH0(u64)       LSH3(u64)             //!< Most significant half-word of 1st rank of \a u64.
835
  #define LSB0D(u64)      (((U8  *)&(u64))[0])  //!< Least significant byte of 1st rank of \a u64.
836
  #define LSB1D(u64)      (((U8  *)&(u64))[1])  //!< Least significant byte of 2nd rank of \a u64.
837
  #define LSB2D(u64)      (((U8  *)&(u64))[2])  //!< Least significant byte of 3rd rank of \a u64.
838
  #define LSB3D(u64)      (((U8  *)&(u64))[3])  //!< Least significant byte of 4th rank of \a u64.
839
  #define LSB4D(u64)      (((U8  *)&(u64))[4])  //!< Least significant byte of 5th rank of \a u64.
840
  #define LSB5D(u64)      (((U8  *)&(u64))[5])  //!< Least significant byte of 6th rank of \a u64.
841
  #define LSB6D(u64)      (((U8  *)&(u64))[6])  //!< Least significant byte of 7th rank of \a u64.
842
  #define LSB7D(u64)      (((U8  *)&(u64))[7])  //!< Least significant byte of 8th rank of \a u64.
843
  #define MSB7D(u64)      LSB0D(u64)            //!< Most significant byte of 8th rank of \a u64.
844
  #define MSB6D(u64)      LSB1D(u64)            //!< Most significant byte of 7th rank of \a u64.
845
  #define MSB5D(u64)      LSB2D(u64)            //!< Most significant byte of 6th rank of \a u64.
846
  #define MSB4D(u64)      LSB3D(u64)            //!< Most significant byte of 5th rank of \a u64.
847
  #define MSB3D(u64)      LSB4D(u64)            //!< Most significant byte of 4th rank of \a u64.
848
  #define MSB2D(u64)      LSB5D(u64)            //!< Most significant byte of 3rd rank of \a u64.
849
  #define MSB1D(u64)      LSB6D(u64)            //!< Most significant byte of 2nd rank of \a u64.
850
  #define MSB0D(u64)      LSB7D(u64)            //!< Most significant byte of 1st rank of \a u64.
851
 
852
#else // BIG_ENDIAN_MCU
853
 
854
  #define MSB(u16)        (((U8  *)&(u16))[0])  //!< Most significant byte of \a u16.
855
  #define LSB(u16)        (((U8  *)&(u16))[1])  //!< Least significant byte of \a u16.
856
 
857
  #define MSH(u32)        (((U16 *)&(u32))[0])  //!< Most significant half-word of \a u32.
858
  #define LSH(u32)        (((U16 *)&(u32))[1])  //!< Least significant half-word of \a u32.
859
  #define MSB0W(u32)      (((U8  *)&(u32))[0])  //!< Most significant byte of 1st rank of \a u32.
860
  #define MSB1W(u32)      (((U8  *)&(u32))[1])  //!< Most significant byte of 2nd rank of \a u32.
861
  #define MSB2W(u32)      (((U8  *)&(u32))[2])  //!< Most significant byte of 3rd rank of \a u32.
862
  #define MSB3W(u32)      (((U8  *)&(u32))[3])  //!< Most significant byte of 4th rank of \a u32.
863
  #define LSB3W(u32)      MSB0W(u32)            //!< Least significant byte of 4th rank of \a u32.
864
  #define LSB2W(u32)      MSB1W(u32)            //!< Least significant byte of 3rd rank of \a u32.
865
  #define LSB1W(u32)      MSB2W(u32)            //!< Least significant byte of 2nd rank of \a u32.
866
  #define LSB0W(u32)      MSB3W(u32)            //!< Least significant byte of 1st rank of \a u32.
867
 
868
  #define MSW(u64)        (((U32 *)&(u64))[0])  //!< Most significant word of \a u64.
869
  #define LSW(u64)        (((U32 *)&(u64))[1])  //!< Least significant word of \a u64.
870
  #define MSH0(u64)       (((U16 *)&(u64))[0])  //!< Most significant half-word of 1st rank of \a u64.
871
  #define MSH1(u64)       (((U16 *)&(u64))[1])  //!< Most significant half-word of 2nd rank of \a u64.
872
  #define MSH2(u64)       (((U16 *)&(u64))[2])  //!< Most significant half-word of 3rd rank of \a u64.
873
  #define MSH3(u64)       (((U16 *)&(u64))[3])  //!< Most significant half-word of 4th rank of \a u64.
874
  #define LSH3(u64)       MSH0(u64)             //!< Least significant half-word of 4th rank of \a u64.
875
  #define LSH2(u64)       MSH1(u64)             //!< Least significant half-word of 3rd rank of \a u64.
876
  #define LSH1(u64)       MSH2(u64)             //!< Least significant half-word of 2nd rank of \a u64.
877
  #define LSH0(u64)       MSH3(u64)             //!< Least significant half-word of 1st rank of \a u64.
878
  #define MSB0D(u64)      (((U8  *)&(u64))[0])  //!< Most significant byte of 1st rank of \a u64.
879
  #define MSB1D(u64)      (((U8  *)&(u64))[1])  //!< Most significant byte of 2nd rank of \a u64.
880
  #define MSB2D(u64)      (((U8  *)&(u64))[2])  //!< Most significant byte of 3rd rank of \a u64.
881
  #define MSB3D(u64)      (((U8  *)&(u64))[3])  //!< Most significant byte of 4th rank of \a u64.
882
  #define MSB4D(u64)      (((U8  *)&(u64))[4])  //!< Most significant byte of 5th rank of \a u64.
883
  #define MSB5D(u64)      (((U8  *)&(u64))[5])  //!< Most significant byte of 6th rank of \a u64.
884
  #define MSB6D(u64)      (((U8  *)&(u64))[6])  //!< Most significant byte of 7th rank of \a u64.
885
  #define MSB7D(u64)      (((U8  *)&(u64))[7])  //!< Most significant byte of 8th rank of \a u64.
886
  #define LSB7D(u64)      MSB0D(u64)            //!< Least significant byte of 8th rank of \a u64.
887
  #define LSB6D(u64)      MSB1D(u64)            //!< Least significant byte of 7th rank of \a u64.
888
  #define LSB5D(u64)      MSB2D(u64)            //!< Least significant byte of 6th rank of \a u64.
889
  #define LSB4D(u64)      MSB3D(u64)            //!< Least significant byte of 5th rank of \a u64.
890
  #define LSB3D(u64)      MSB4D(u64)            //!< Least significant byte of 4th rank of \a u64.
891
  #define LSB2D(u64)      MSB5D(u64)            //!< Least significant byte of 3rd rank of \a u64.
892
  #define LSB1D(u64)      MSB6D(u64)            //!< Least significant byte of 2nd rank of \a u64.
893
  #define LSB0D(u64)      MSB7D(u64)            //!< Least significant byte of 1st rank of \a u64.
894
 
895
#endif
896
 
897
//! @}
898
 
899
 
900
/*! \name Endianism Conversion
901
 *
902
 * The same considerations as for clz and ctz apply here but AVR32-GCC's
903
 * __builtin_bswap_16 and __builtin_bswap_32 do not behave like macros when
904
 * applied to constant expressions, so two sets of macros are defined here:
905
 *   - Swap16, Swap32 and Swap64 to apply to constant expressions (values known
906
 *     at compile time);
907
 *   - swap16, swap32 and swap64 to apply to non-constant expressions (values
908
 *     unknown at compile time).
909
 */
910
//! @{
911
 
912
/*! \brief Toggles the endianism of \a u16 (by swapping its bytes).
913
 *
914
 * \param u16 U16 of which to toggle the endianism.
915
 *
916
 * \return Value resulting from \a u16 with toggled endianism.
917
 *
918
 * \note More optimized if only used with values known at compile time.
919
 */
920
#define Swap16(u16) ((U16)(((U16)(u16) >> 8) |\
921
                           ((U16)(u16) << 8)))
922
 
923
/*! \brief Toggles the endianism of \a u32 (by swapping its bytes).
924
 *
925
 * \param u32 U32 of which to toggle the endianism.
926
 *
927
 * \return Value resulting from \a u32 with toggled endianism.
928
 *
929
 * \note More optimized if only used with values known at compile time.
930
 */
931
#define Swap32(u32) ((U32)(((U32)Swap16((U32)(u32) >> 16)) |\
932
                           ((U32)Swap16((U32)(u32)) << 16)))
933
 
934
/*! \brief Toggles the endianism of \a u64 (by swapping its bytes).
935
 *
936
 * \param u64 U64 of which to toggle the endianism.
937
 *
938
 * \return Value resulting from \a u64 with toggled endianism.
939
 *
940
 * \note More optimized if only used with values known at compile time.
941
 */
942
#define Swap64(u64) ((U64)(((U64)Swap32((U64)(u64) >> 32)) |\
943
                           ((U64)Swap32((U64)(u64)) << 32)))
944
 
945
/*! \brief Toggles the endianism of \a u16 (by swapping its bytes).
946
 *
947
 * \param u16 U16 of which to toggle the endianism.
948
 *
949
 * \return Value resulting from \a u16 with toggled endianism.
950
 *
951
 * \note More optimized if only used with values unknown at compile time.
952
 */
953
#if __GNUC__
954
  #define swap16(u16) ((U16)__builtin_bswap_16((U16)(u16)))
955
#elif __ICCAVR32__
956
  #define swap16(u16) ((U16)__swap_bytes_in_halfwords((U16)(u16)))
957
#endif
958
 
959
/*! \brief Toggles the endianism of \a u32 (by swapping its bytes).
960
 *
961
 * \param u32 U32 of which to toggle the endianism.
962
 *
963
 * \return Value resulting from \a u32 with toggled endianism.
964
 *
965
 * \note More optimized if only used with values unknown at compile time.
966
 */
967
#if __GNUC__
968
  #define swap32(u32) ((U32)__builtin_bswap_32((U32)(u32)))
969
#elif __ICCAVR32__
970
  #define swap32(u32) ((U32)__swap_bytes((U32)(u32)))
971
#endif
972
 
973
/*! \brief Toggles the endianism of \a u64 (by swapping its bytes).
974
 *
975
 * \param u64 U64 of which to toggle the endianism.
976
 *
977
 * \return Value resulting from \a u64 with toggled endianism.
978
 *
979
 * \note More optimized if only used with values unknown at compile time.
980
 */
981
#define swap64(u64) ((U64)(((U64)swap32((U64)(u64) >> 32)) |\
982
                           ((U64)swap32((U64)(u64)) << 32)))
983
 
984
//! @}
985
 
986
 
987
/*! \name Target Abstraction
988
 */
989
//! @{
990
 
991
#define _GLOBEXT_           extern      //!< extern storage-class specifier.
992
#define _CONST_TYPE_        const       //!< const type qualifier.
993
#define _MEM_TYPE_SLOW_                 //!< Slow memory type.
994
#define _MEM_TYPE_MEDFAST_              //!< Fairly fast memory type.
995
#define _MEM_TYPE_FAST_                 //!< Fast memory type.
996
 
997
typedef U8                  Byte;       //!< 8-bit unsigned integer.
998
 
999
#define memcmp_ram2ram      memcmp      //!< Target-specific memcmp of RAM to RAM.
1000
#define memcmp_code2ram     memcmp      //!< Target-specific memcmp of RAM to NVRAM.
1001
#define memcpy_ram2ram      memcpy      //!< Target-specific memcpy from RAM to RAM.
1002
#define memcpy_code2ram     memcpy      //!< Target-specific memcpy from NVRAM to RAM.
1003
 
1004
#define LSB0(u32)           LSB0W(u32)  //!< Least significant byte of 1st rank of \a u32.
1005
#define LSB1(u32)           LSB1W(u32)  //!< Least significant byte of 2nd rank of \a u32.
1006
#define LSB2(u32)           LSB2W(u32)  //!< Least significant byte of 3rd rank of \a u32.
1007
#define LSB3(u32)           LSB3W(u32)  //!< Least significant byte of 4th rank of \a u32.
1008
#define MSB3(u32)           MSB3W(u32)  //!< Most significant byte of 4th rank of \a u32.
1009
#define MSB2(u32)           MSB2W(u32)  //!< Most significant byte of 3rd rank of \a u32.
1010
#define MSB1(u32)           MSB1W(u32)  //!< Most significant byte of 2nd rank of \a u32.
1011
#define MSB0(u32)           MSB0W(u32)  //!< Most significant byte of 1st rank of \a u32.
1012
 
1013
//! @}
1014
 
1015
#endif  // __AVR32_ABI_COMPILER__
1016
 
1017
 
1018
#endif  // _COMPILER_H_

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.