avrlibc.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. #ifndef _TOOLCHAIN_AVRLIBC_H_
  2. #define _TOOLCHAIN_AVRLIBC_H_
  3. /*
  4. * Copyright (C) 2012 by egnite GmbH
  5. * Copyright (C) 2001-2005 by egnite Software GmbH
  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 conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in the
  17. * documentation and/or other materials provided with the distribution.
  18. * 3. Neither the name of the copyright holders nor the names of
  19. * contributors may be used to endorse or promote products derived
  20. * from this software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  29. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  30. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  31. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  32. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33. * SUCH DAMAGE.
  34. *
  35. * For additional information see http://www.ethernut.de/
  36. */
  37. /*
  38. * Include library specific header files, which are required by the
  39. * kernel here. Avoid polluting kernel code with target specific ifdefs.
  40. */
  41. #include <avr/io.h>
  42. #include <avr/interrupt.h>
  43. #include <avr/version.h>
  44. #if __AVR_LIBC_VERSION__ < 10400UL
  45. #include <avr/signal.h>
  46. #endif
  47. #include <avr/eeprom.h>
  48. #include <avr/pgmspace.h>
  49. #include <avr/sleep.h>
  50. #include <stdlib.h>
  51. /*
  52. * test for a macro added in avr-libc 1.2.0, if yes use different path for twi.h
  53. * note: has to be after #include <eeprom.h>
  54. */
  55. #ifdef eeprom_rb
  56. #include <avr/twi.h>
  57. #else
  58. #include <compat/twi.h>
  59. #endif
  60. /*!
  61. * \name Library-Specific Workarounds
  62. */
  63. /*@{*/
  64. /*!
  65. * \brief Main function entry.
  66. *
  67. * In order to save a few bytes of code on very tiny AVR devices, in
  68. * some versions of AVR Libc the main function entry differs form normal
  69. * function entries in that the stack pointer is set at the beginning of
  70. * main(). Actually this is done by the compiler, but tightly coupled to
  71. * the library.
  72. *
  73. * For Nut/OS this is fatal, because main is started as a separate
  74. * thread by the idle thread, assuming the stack pointer at the main
  75. * thread's local stack. As a dirty workaround, main() is redefined as
  76. * NutAppMain(). Dirty, because some debuggers (and developers) may
  77. * become confused.
  78. *
  79. * \todo Limit redefinition of main to specific library versions.
  80. */
  81. #define main NutAppMain
  82. /*@}*/
  83. /*!
  84. * \name Register Compatibility Macros
  85. */
  86. /*@{*/
  87. /*!
  88. * \brief Special function register offset.
  89. *
  90. * Automatically subtracts 0x20 from I/O space addresses, but according
  91. * to the AVR Libc documentation, this is a hack, and it is recommended
  92. * to change the source code, wrapping such addresses in macros.
  93. */
  94. #ifndef __SFR_OFFSET
  95. #define __SFR_OFFSET 0
  96. #endif
  97. /*
  98. * Since version 1.1.0 of avr-libc, some former deprecated macros are deleted.
  99. * But we need them futher on, so they are defined here.
  100. */
  101. #ifndef inb
  102. #define inb(sfr) _SFR_BYTE(sfr)
  103. #endif
  104. #ifndef outb
  105. #define outb(sfr, val) (_SFR_BYTE(sfr) = (val))
  106. #endif
  107. #ifndef inw
  108. #define inw(sfr) _SFR_WORD(sfr)
  109. #endif
  110. #ifndef outw
  111. #define outw(sfr, val) (_SFR_WORD(sfr) = (val))
  112. #endif
  113. #ifndef cbi
  114. #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
  115. #endif
  116. #ifndef sbi
  117. #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
  118. #endif
  119. #if __AVR_LIBC_VERSION__ >= 10800UL
  120. typedef const char PROGMEM prog_char;
  121. #endif
  122. #ifndef PGM_P
  123. #define PGM_P prog_char *
  124. #endif
  125. #ifndef PRG_RDB
  126. #define PRG_RDB(addr) pgm_read_byte(addr)
  127. #endif
  128. /*@}*/
  129. #include <toolchain/generic.h>
  130. #define __bss_end __heap_start
  131. extern void *__heap_start;
  132. #ifndef atof
  133. #define atof(s) strtod(s, 0)
  134. #endif
  135. /*!
  136. * \name EEPROM Access Compatibility Macros
  137. */
  138. /*@{*/
  139. #define EEPROMReadBytes(addr, ptr, size) eeprom_read_block((char *)(addr), ptr, size)
  140. /*!
  141. * \brief Read multibyte types from the EEPROM.
  142. */
  143. #define EEPROM_READ(addr, dst) eeprom_read_block((char *)(addr), &dst, sizeof(dst))
  144. #define EEPROMread(addr) eeprom_read_byte((char *)(addr))
  145. /*!
  146. * \brief Write multibyte types to the EEPROM.
  147. */
  148. #define EEPROM_WRITE(addr, src) \
  149. { \
  150. unsigned short __i; \
  151. for(__i = 0; __i < sizeof(src); __i++) \
  152. eeprom_write_byte(((char *)(addr)) + __i, *(((char *)(&(src))) + __i)); \
  153. }
  154. #define EEPROMWriteBytes(addr, ptr, size) \
  155. { \
  156. unsigned short __i; \
  157. for(__i = 0; __i < size; __i++) \
  158. eeprom_write_byte(((char *)(addr)) + __i, *(((char *)(ptr)) + __i)); \
  159. }
  160. /*@}*/
  161. #if defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__)
  162. #if !defined(TXC)
  163. #define TXC TXC0
  164. #endif
  165. #if !defined(TXB8)
  166. #define TXB8 TXB80
  167. #endif
  168. #if !defined(UMSEL)
  169. #define UMSEL UMSEL00
  170. #endif
  171. #if !defined(U2X)
  172. #define U2X U2X0
  173. #endif
  174. #if !defined(UCSZ0)
  175. #define UCSZ0 UCSZ00
  176. #endif
  177. #if !defined(UCSZ1)
  178. #define UCSZ1 UCSZ01
  179. #endif
  180. #if !defined(UCSZ2)
  181. #define UCSZ2 UCSZ02
  182. #endif
  183. #if !defined(UPM0)
  184. #define UPM0 UPM00
  185. #endif
  186. #if !defined(UPM1)
  187. #define UPM1 UPM01
  188. #endif
  189. #if !defined(USBS)
  190. #define USBS USBS0
  191. #endif
  192. #if !defined(UPE)
  193. #define UPE UPE0
  194. #endif
  195. #if !defined(MPCM)
  196. #define MPCM MPCM0
  197. #endif
  198. #if !defined(UCPOL)
  199. #define UCPOL UCPOL0
  200. #endif
  201. #elif defined(__AVR_AT90USB1287__)
  202. #if !defined(TXC)
  203. #define TXC TXC1
  204. #endif
  205. #if !defined(TXB8)
  206. #define TXB8 TXB81
  207. #endif
  208. #if !defined(UMSEL)
  209. #define UMSEL UMSEL10
  210. #endif
  211. #if !defined(U2X)
  212. #define U2X U2X1
  213. #endif
  214. #if !defined(UCSZ0)
  215. #define UCSZ0 UCSZ10
  216. #endif
  217. #if !defined(UCSZ1)
  218. #define UCSZ1 UCSZ11
  219. #endif
  220. #if !defined(UCSZ2)
  221. #define UCSZ2 UCSZ12
  222. #endif
  223. #if !defined(UPM0)
  224. #define UPM0 UPM10
  225. #endif
  226. #if !defined(UPM1)
  227. #define UPM1 UPM11
  228. #endif
  229. #if !defined(USBS)
  230. #define USBS USBS1
  231. #endif
  232. #if !defined(UPE)
  233. #define UPE UPE1
  234. #endif
  235. #if !defined(MPCM)
  236. #define MPCM MPCM1
  237. #endif
  238. #if !defined(UCPOL)
  239. #define UCPOL UCPOL1
  240. #endif
  241. #endif
  242. #ifdef __AVR_ENHANCED__
  243. /* Nut/OS is still using the original ATmega103 register names for
  244. backward compatibility. */
  245. #if defined(__AVR_AT90USB1287__)
  246. /* AT90USB1287 without USART0 */
  247. #ifndef UDR
  248. #define UDR UDR1
  249. #endif
  250. #ifndef UBRR
  251. #define UBRR UBRR1L
  252. #endif
  253. #ifndef USR
  254. #define USR UCSR1A
  255. #endif
  256. #ifndef UCR
  257. #define UCR UCSR1B
  258. #endif
  259. #ifndef EICR
  260. #define EICR EICRB
  261. #endif
  262. #ifndef RXC
  263. #define RXC RXC1
  264. #endif
  265. #ifndef UDRE
  266. #define UDRE UDRE1
  267. #endif
  268. #ifndef FE
  269. #define FE FE1
  270. #endif
  271. #ifndef DOR
  272. #define DOR DOR1
  273. #endif
  274. #ifndef RXCIE
  275. #define RXCIE RXCIE1
  276. #endif
  277. #ifndef TXCIE
  278. #define TXCIE TXCIE1
  279. #endif
  280. #ifndef UDRIE
  281. #define UDRIE UDRIE1
  282. #endif
  283. #ifndef RXEN
  284. #define RXEN RXEN1
  285. #endif
  286. #ifndef TXEN
  287. #define TXEN TXEN1
  288. #endif
  289. #else
  290. #ifndef UDR
  291. #define UDR UDR0
  292. #endif
  293. #ifndef UBRR
  294. #define UBRR UBRR0L
  295. #endif
  296. #ifndef USR
  297. #define USR UCSR0A
  298. #endif
  299. #ifndef UCR
  300. #define UCR UCSR0B
  301. #endif
  302. #ifndef EICR
  303. #define EICR EICRB
  304. #endif
  305. #ifndef RXC
  306. #define RXC RXC0
  307. #endif
  308. #ifndef UDRE
  309. #define UDRE UDRE0
  310. #endif
  311. #ifndef FE
  312. #define FE FE0
  313. #endif
  314. #ifndef DOR
  315. #define DOR DOR0
  316. #endif
  317. #ifndef RXCIE
  318. #define RXCIE RXCIE0
  319. #endif
  320. #ifndef TXCIE
  321. #define TXCIE TXCIE0
  322. #endif
  323. #ifndef UDRIE
  324. #define UDRIE UDRIE0
  325. #endif
  326. #ifndef RXEN
  327. #define RXEN RXEN0
  328. #endif
  329. #ifndef TXEN
  330. #define TXEN TXEN0
  331. #endif
  332. #endif
  333. /* Some ATC90CAN128 SFR names are different to ATMEGA128. Define some
  334. compatibilty macros. */
  335. #if defined(__AVR_AT90CAN128__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__) || defined(__AVR_AT90USB1287__)
  336. #ifndef ADCW
  337. #define ADCW ADC
  338. #endif
  339. #ifndef ADCSR
  340. #define ADCSR ADCSRA
  341. #endif
  342. #ifndef ADFR
  343. #define ADFR ADATE
  344. #endif
  345. #ifndef OCIE0
  346. #define OCIE0 OCIE0A
  347. #endif
  348. #ifndef TCCR0
  349. #define TCCR0 TCCR0A
  350. #endif
  351. #ifndef TCCR2
  352. #define TCCR2 TCCR2A
  353. #endif
  354. #ifndef OCR0
  355. #define OCR0 OCR0A
  356. #endif
  357. #ifndef TIMSK
  358. #define TIMSK TIMSK0
  359. #endif
  360. #ifndef TIFR
  361. #define TIFR TIFR0
  362. #endif
  363. #endif /* __AVR_AT90CAN128__ || __AVR_ATmega2560__ || __AVR_ATmega2561__ || __AVR_AT90USB1287__)*/
  364. #endif /* __AVR_ENHANCED__ */
  365. /* Define internal _SLEEP_MODE_MASK that is no longer public in avrlibc. */
  366. #ifndef _SLEEP_MODE_MASK
  367. #if defined(SM) && !defined(SM0) && !defined(SM1) && !defined(SM2)
  368. #define _SLEEP_MODE_MASK _BV(SM)
  369. #elif !defined(SM) && defined(SM0) && defined(SM1) && !defined(SM2)
  370. #define _SLEEP_MODE_MASK (_BV(SM0) | _BV(SM1))
  371. #elif !defined(SM) && defined(SM0) && defined(SM1) && defined(SM2)
  372. #define _SLEEP_MODE_MASK (_BV(SM0) | _BV(SM1) | _BV(SM2))
  373. #endif
  374. #endif
  375. #endif