icc.h 11 KB

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