avr_compiler.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /* This file has been prepared for Doxygen automatic documentation generation.*/
  2. /*! \file *********************************************************************
  3. *
  4. * \brief This file implements some macros that makes the IAR C-compiler and
  5. * avr-gcc work with the same code base for the AVR architecture.
  6. *
  7. * \par Documentation
  8. * For comprehensive code documentation, supported compilers, compiler
  9. * settings and supported devices see readme.html
  10. *
  11. * \author
  12. * Atmel Corporation: http://www.atmel.com \n
  13. * Support email: avr@atmel.com
  14. *
  15. * $Revision: 1694 $
  16. * $Date: 2008-07-29 14:21:58 +0200 (ti, 29 jul 2008) $ \n
  17. *
  18. * Copyright (c) 2008, Atmel Corporation All rights reserved.
  19. *
  20. * Redistribution and use in source and binary forms, with or without
  21. * modification, are permitted provided that the following conditions are met:
  22. *
  23. * 1. Redistributions of source code must retain the above copyright notice,
  24. * this list of conditions and the following disclaimer.
  25. *
  26. * 2. Redistributions in binary form must reproduce the above copyright notice,
  27. * this list of conditions and the following disclaimer in the documentation
  28. * and/or other materials provided with the distribution.
  29. *
  30. * 3. The name of ATMEL may not be used to endorse or promote products derived
  31. * from this software without specific prior written permission.
  32. *
  33. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  34. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  35. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
  36. * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
  37. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  38. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  40. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  41. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  42. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. ******************************************************************************/
  44. #ifndef COMPILER_AVR_H
  45. #define COMPILER_AVR_H
  46. #ifndef F_CPU
  47. /*! \brief Define default CPU frequency, if this is not already defined. */
  48. #define F_CPU 2000000UL
  49. #endif
  50. #include <stdint.h>
  51. #include <stdbool.h>
  52. #include <stdlib.h>
  53. /*! \brief This macro will protect the following code from interrupts. */
  54. #define AVR_ENTER_CRITICAL_REGION( ) uint8_t volatile saved_sreg = SREG; \
  55. cli();
  56. /*! \brief This macro must always be used in conjunction with AVR_ENTER_CRITICAL_REGION
  57. * so the interrupts are enabled again.
  58. */
  59. #define AVR_LEAVE_CRITICAL_REGION( ) SREG = saved_sreg;
  60. #if defined( __ICCAVR__ )
  61. #include <inavr.h>
  62. #include <ioavr.h>
  63. #include <intrinsics.h>
  64. #include <pgmspace.h>
  65. #ifndef __HAS_ELPM__
  66. #define _MEMATTR __flash
  67. #else /* __HAS_ELPM__ */
  68. #define _MEMATTR __farflash
  69. #endif /* __HAS_ELPM__ */
  70. /*! \brief Perform a delay of \c us microseconds.
  71. *
  72. * The macro F_CPU is supposed to be defined to a constant defining the CPU
  73. * clock frequency (in Hertz).
  74. *
  75. * The maximal possible delay is 262.14 ms / F_CPU in MHz.
  76. *
  77. * \note For the IAR compiler, currently F_CPU must be a
  78. * multiple of 1000000UL (1 MHz).
  79. */
  80. #define delay_us( us ) ( __delay_cycles( ( F_CPU / 1000000UL ) * ( us ) ) )
  81. /*! \brief Preprocessor magic.
  82. *
  83. * Some preprocessor magic to allow for a header file abstraction of
  84. * interrupt service routine declarations for the IAR compiler. This
  85. * requires the use of the C99 _Pragma() directive (rather than the
  86. * old #pragma one that could not be used as a macro replacement), as
  87. * well as two different levels of preprocessor concetanations in
  88. * order to do both, assign the correct interrupt vector name, as well
  89. * as construct a unique function name for the ISR.
  90. *
  91. * \note Do *NOT* try to reorder the macros below, as this will only
  92. * work in the given order.
  93. */
  94. #define PRAGMA(x) _Pragma( #x )
  95. #define ISR(vec) PRAGMA( vector=vec ) __interrupt void handler_##vec(void)
  96. #define sei( ) (__enable_interrupt( ))
  97. #define cli( ) (__disable_interrupt( ))
  98. /*! \brief Define the no operation macro. */
  99. #define nop( ) (__no_operation())
  100. /*! \brief Define the watchdog reset macro. */
  101. #define watchdog_reset( ) (__watchdog_reset( ))
  102. #define INLINE PRAGMA( inline=forced ) static
  103. #define FLASH_DECLARE(x) _MEMATTR x
  104. #define FLASH_STRING(x) ((_MEMATTR const char *)(x))
  105. #define FLASH_STRING_T char const _MEMATTR *
  106. #define FLASH_BYTE_ARRAY_T uint8_t const _MEMATTR *
  107. #define PGM_READ_BYTE(x) *(x)
  108. #define PGM_READ_WORD(x) *(x)
  109. #define SHORTENUM /**/
  110. #elif defined( __GNUC__ )
  111. #include <avr/io.h>
  112. #include <avr/interrupt.h>
  113. #include <avr/pgmspace.h>
  114. #include <util/delay.h>
  115. /*! \brief Define the delay_us macro for GCC. */
  116. #define delay_us( us ) (_delay_us( us ))
  117. #define INLINE static inline
  118. /*! \brief Define the no operation macro. */
  119. #define nop() do { __asm__ __volatile__ ("nop"); } while (0)
  120. #define MAIN_TASK_PROLOGUE int
  121. #define MAIN_TASK_EPILOGUE() return -1;
  122. #define SHORTENUM __attribute__ ((packed))
  123. #else
  124. #error Compiler not supported.
  125. #endif
  126. #endif