icc.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. #ifndef _ARCH_AVR_ICC_H_
  2. #define _ARCH_AVR_ICC_H_
  3. /*
  4. * Copyright (C) 2001-2005 by egnite Software GmbH. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. /*
  35. * $Log$
  36. * Revision 1.11 2009/02/06 15:58:08 haraldkipp
  37. * We now have strdup() and calloc().
  38. *
  39. * Revision 1.10 2008/09/18 09:48:07 haraldkipp
  40. * The old Marv_XXX do no longer work with ICCAVR 7.18B.
  41. *
  42. * Revision 1.9 2008/08/11 11:51:20 thiagocorrea
  43. * Preliminary Atmega2560 compile options, but not yet supported.
  44. * It builds, but doesn't seam to run properly at this time.
  45. *
  46. * Revision 1.8 2008/08/11 06:59:58 haraldkipp
  47. * BSD types replaced by stdint types (feature request #1282721).
  48. *
  49. * Revision 1.7 2008/06/25 08:37:56 freckle
  50. * escape realloc for icc
  51. *
  52. * Revision 1.6 2006/09/29 12:22:07 haraldkipp
  53. * Corrected internal RAM size and check compiler environment for ATmega2561.
  54. *
  55. * Revision 1.5 2006/07/10 08:48:03 haraldkipp
  56. * Distinguish between enhanced and extended AVR.
  57. *
  58. * Revision 1.4 2006/05/25 09:35:27 haraldkipp
  59. * Dummy macros added to support the avr-libc special function register
  60. * definitions.
  61. *
  62. * Revision 1.3 2006/02/08 15:20:56 haraldkipp
  63. * ATmega2561 Support
  64. *
  65. * Revision 1.2 2005/10/24 18:04:25 haraldkipp
  66. * Duplicate macro definitions removed.
  67. * Parameter order of cstrcmp() corrected.
  68. * Generic port and flag names added for ATmega103 support.
  69. *
  70. * Revision 1.1 2005/10/24 10:46:05 haraldkipp
  71. * First check in.
  72. * Contents taken from avr.h in the parent directory.
  73. *
  74. */
  75. /*
  76. * Include some basic header files of the ImageCraft runtime library.
  77. */
  78. #include <stddef.h>
  79. #include <macros.h>
  80. #include <eeprom.h>
  81. /* ICCV7 incompatibility nightmare. */
  82. #if defined(_MCU_Enhanced) && !defined(_MCU_enhanced)
  83. #define _MCU_enhanced
  84. #endif
  85. #if defined(_MCU_Extended) && !defined(_MCU_extended)
  86. #define _MCU_extended
  87. #endif
  88. /*!
  89. * \brief Specify enhanced AVR target.
  90. *
  91. * For backward compatibility this macro is automatically defined if
  92. * _MCU_enhanced is defined.
  93. *
  94. */
  95. #if defined(_MCU_enhanced) || defined(_MCU_extended)
  96. #undef __AVR_ENHANCED__
  97. #define __AVR_ENHANCED__ /* Generic test for enhanced AVRs like ATMEGA128, AT09CAN128 */
  98. #endif
  99. /*!
  100. * \brief Disable const keyword.
  101. *
  102. * ICCAVR doesn't allow to mark function parameters unmodifable
  103. * by the 'const' modifier. Even worse, the compiler redefines
  104. * the meaning as a program memory attribute.
  105. */
  106. #ifndef CONST
  107. #define CONST const
  108. #endif
  109. /*!
  110. * \brief Disable inline keyword.
  111. *
  112. * ICCAVR doesn't allow to explicitly mark a function for inline
  113. * code generation.
  114. */
  115. #ifndef INLINE
  116. #define INLINE
  117. #endif
  118. /*!
  119. * \brief Redefined standard library routines.
  120. *
  121. * ImageCraft has a multipass linker, which is fine for complicated
  122. * dependencies in most cases. However, there is the potential risk,
  123. * that standard library calls with the same name are linked from
  124. * the wrong library. To avoid this, an additional postfix is added
  125. * to routines, which are implemented in Nut/OS libraries.
  126. */
  127. #define printf printf_M
  128. #define puts puts_M
  129. #define sprintf sprintf_M
  130. #define vprintf vprintf_M
  131. #define scanf scanf_M
  132. #define gets gets_M
  133. #define malloc malloc_M
  134. #define calloc calloc_M
  135. #define realloc realloc_M
  136. #define free free_M
  137. /* device specific functions map to special name forms so the linker
  138. * can give more meaningful error messages
  139. */
  140. #pragma device_specific_function putchar getchar
  141. /*!
  142. * \brief Redirected stdio routines.
  143. *
  144. * Native stdio routines with format strings in program space are
  145. * redirected to their Nut/OS implementation.
  146. */
  147. #define cprintf printf_P
  148. #define csprintf sprintf_P
  149. #define cscanf scanf_P
  150. #define csscanf sscanf_P
  151. #define memcpy_P(dst, src_P, n) cmemcpy(dst, src_P, n)
  152. #define strcat_P(s1, s2_P) cstrcat(s1, s2_P)
  153. #define strcmp_P(s1, s2_P) (-cstrcmp(s2_P, s1))
  154. #define strlen_P(s_P) cstrlen(s_P)
  155. #define strncat_P(s1, s2_P, n) cstrncat(s1, s2_P, n)
  156. #define strncmp_P(s1_P, s2, n) cstrncmp(s1_P, s2, n)
  157. #define strcpy_P(dst, src_P) cstrcpy(dst, src_P)
  158. #define strncpy_P(x,y,z) cstrncpy(x,y,z)
  159. /*!
  160. * \brief Case insensitive string comparisions.
  161. *
  162. * Not supported by ICCAVR and temporarly redirected to
  163. * the case sensitive routines.
  164. *
  165. * \bug Case insensitive string comparisions fail with ICCAVR.
  166. */
  167. #define strcasecmp(s1, s2) strcmp(s1, s2)
  168. #define strncasecmp(s1, s2, n) strncmp(s1, s2, n)
  169. /*!
  170. * \brief Start of heap area.
  171. */
  172. #define __heap_start _bss_end
  173. /*!
  174. * \brief Object attribute support.
  175. *
  176. * Not supported by ICCAVR.
  177. */
  178. #define __attribute__(x)
  179. /*!
  180. * \brief Declare static pointer to strings in program space.
  181. *
  182. * No chance with ICCAVR.
  183. */
  184. #define PSTR(p) (p)
  185. /*! \def PRG_RDB(p)
  186. * \brief Read byte from program space.
  187. */
  188. #define PRG_RDB(p) (*((__flash char *)(p)))
  189. /*! \def prog_char
  190. * \brief Character in program space.
  191. */
  192. #define prog_char __flash char
  193. /*! \def prog_int
  194. * \brief Integer in program space.
  195. */
  196. #define prog_int __flash int
  197. /*! \def PGM_P
  198. * \brief Pointer to character in program space.
  199. */
  200. #define PGM_P prog_char *
  201. /* ================================================================ */
  202. /* To be sorted out. */
  203. /* ================================================================ */
  204. #define wdt_enable(tmo) \
  205. { \
  206. register unsigned char s = _BV(WDCE) | _BV(WDE); \
  207. register unsigned char r = tmo | _BV(WDE); \
  208. asm("in R0, 0x3F\n" \
  209. "cli\n" \
  210. "wdr\n" \
  211. "out 0x21, %s\n" \
  212. "out 0x21, %r\n" \
  213. "out 0x3F, R0\n"); \
  214. }
  215. #define wdt_disable() \
  216. { \
  217. register unsigned char s = _BV(WDCE) | _BV(WDE); \
  218. register unsigned char r = 0; \
  219. asm("in R0, $3F\n" \
  220. "cli\n" \
  221. "out 0x21, %s\n" \
  222. "out 0x21, %r\n" \
  223. "out 0x3F, R0\n"); \
  224. }
  225. #define wdt_reset() \
  226. { \
  227. _WDR(); \
  228. }
  229. #define __SFR_OFFSET 0
  230. #define SFR_IO_ADDR(sfr) ((sfr) - __SFR_OFFSET)
  231. #define SFR_MEM_ADDR(sfr) (sfr)
  232. #define SFR_IO_REG_P(sfr) ((sfr) < 0x40 + __SFR_OFFSET)
  233. #define _SFR_MEM8(addr) (addr)
  234. #define _SFR_MEM16(addr) (addr)
  235. #define BV(x) BIT(x)
  236. #define _BV(x) BIT(x)
  237. #define cli() CLI()
  238. #define sei() SEI()
  239. #define cbi(reg, bit) (reg &= ~BIT(bit))
  240. #define sbi(reg, bit) (reg |= BIT(bit))
  241. #define loop_until_bit_is_set(reg, bit) while((reg & BIT(bit)) == 0)
  242. #define bit_is_clear(reg, bit) ((reg & BIT(bit)) == 0)
  243. #define bit_is_set(reg, bit) ((reg & BIT(bit)) != 0)
  244. /* FIXME */
  245. #define parity_even_bit(x) (0)
  246. /* FIXME */
  247. #define SIGNAL(x) void x(void)
  248. #define outb(reg, val) (reg = val)
  249. #define inb(reg) (reg)
  250. #include <eeprom.h>
  251. #if defined(_MCU_enhanced) || defined(_MCU_extended)
  252. #ifdef ATMega2560
  253. #include <iom2560v.h>
  254. #define __AVR_ATmega2560__
  255. #ifndef _EE_EXTIO
  256. #error "Looks like wrong platform. Select avrext-icc, not avr-icc."
  257. #endif
  258. #elif defined(ATMega2561)
  259. #include <iom2561v.h>
  260. #define __AVR_ATmega2561__
  261. #ifndef _EE_EXTIO
  262. #error "Looks like wrong platform. Select avrext-icc, not avr-icc."
  263. #endif
  264. #else
  265. #include <iom128v.h>
  266. #define __AVR_ATmega128__
  267. #endif
  268. #ifndef RAMEND
  269. #if defined(ATMega2560) || defined(ATMega2561)
  270. #define RAMEND 0x21FF
  271. #else
  272. #define RAMEND 0x10FF
  273. #endif
  274. #endif
  275. #ifndef SRW
  276. #define SRW 6
  277. #endif
  278. /* ICC doesn't define generic ports and flags. */
  279. #ifndef TXC
  280. #define TXC TXC0
  281. #endif
  282. #ifndef ADCSR
  283. #define ADCSR ADCSRA
  284. #endif
  285. /* Master */
  286. #define TW_START 0x08
  287. #define TW_REP_START 0x10
  288. /* Master Transmitter */
  289. #define TW_MT_SLA_ACK 0x18
  290. #define TW_MT_SLA_NACK 0x20
  291. #define TW_MT_DATA_ACK 0x28
  292. #define TW_MT_DATA_NACK 0x30
  293. #define TW_MT_ARB_LOST 0x38
  294. /* Master Receiver */
  295. #define TW_MR_ARB_LOST 0x38
  296. #define TW_MR_SLA_ACK 0x40
  297. #define TW_MR_SLA_NACK 0x48
  298. #define TW_MR_DATA_ACK 0x50
  299. #define TW_MR_DATA_NACK 0x58
  300. /* Slave Transmitter */
  301. #define TW_ST_SLA_ACK 0xA8
  302. #define TW_ST_ARB_LOST_SLA_ACK 0xB0
  303. #define TW_ST_DATA_ACK 0xB8
  304. #define TW_ST_DATA_NACK 0xC0
  305. #define TW_ST_LAST_DATA 0xC8
  306. /* Slave Receiver */
  307. #define TW_SR_SLA_ACK 0x60
  308. #define TW_SR_ARB_LOST_SLA_ACK 0x68
  309. #define TW_SR_GCALL_ACK 0x70
  310. #define TW_SR_ARB_LOST_GCALL_ACK 0x78
  311. #define TW_SR_DATA_ACK 0x80
  312. #define TW_SR_DATA_NACK 0x88
  313. #define TW_SR_GCALL_DATA_ACK 0x90
  314. #define TW_SR_GCALL_DATA_NACK 0x98
  315. #define TW_SR_STOP 0xA0
  316. /* Misc */
  317. #define TW_NO_INFO 0xF8
  318. #define TW_BUS_ERROR 0x00
  319. #else /* ATmega103 */
  320. #include <iom103v.h>
  321. #define __AVR_ATmega103__
  322. #ifndef DOR
  323. #define DOR OVR
  324. #endif
  325. #ifndef RAMEND
  326. #define RAMEND 0x0FFF
  327. #endif
  328. #ifndef WDCE
  329. #define WDCE WDTOE
  330. #endif
  331. #endif
  332. #define eeprom_read_block(dst, addr, size) EEPROMReadBytes((int)addr, dst, size)
  333. #define eeprom_write_byte(addr, src) EEPROMwrite((int)addr, src)
  334. #define eeprom_read_byte(addr) EEPROMread((int)addr)
  335. extern void *calloc(size_t num, size_t size);
  336. extern char *strdup(const char *str);
  337. #include <string.h>
  338. #endif /* _ARCH_AVR_ICC_H_ */