nutinit.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * Copyright (C) 2001-2006 by egnite Software GmbH
  3. * Copyright (C) 2013 by egnite GmbH
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. Neither the name of the copyright holders nor the names of
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  27. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  28. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  29. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  30. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * For additional information see http://www.ethernut.de/
  34. *
  35. */
  36. /*
  37. * $Id: nutinit.c 5217 2013-06-28 18:43:40Z haraldkipp $
  38. */
  39. #include <sys/thread.h>
  40. #include <sys/heap.h>
  41. #include <cfg/memory.h>
  42. #include <cfg/os.h>
  43. #include <cfg/arch/avr.h>
  44. #include <cfg/arch.h>
  45. #include <dev/board.h>
  46. #include <dev/gpio.h>
  47. /*!
  48. * \addtogroup xgNutArchAvrInit
  49. */
  50. /*@{*/
  51. #ifdef NUTXMEM_SIZE
  52. /*!
  53. * \brief Last memory address using external SRAM.
  54. */
  55. #define NUTMEM_END (uint16_t)(NUTXMEM_START + (uint16_t)NUTXMEM_SIZE - 1U)
  56. #else
  57. /*!
  58. * \brief Last memory address without using external SRAM.
  59. *
  60. * \todo Shall we support NUTRAMEND for backward compatibility? If, then
  61. * let's do it in cfg/memory.h.
  62. */
  63. #define NUTMEM_END (uint16_t)(NUTMEM_START + (uint16_t)NUTMEM_SIZE - 1U)
  64. #endif
  65. #ifndef NUT_THREAD_MAINSTACK
  66. #define NUT_THREAD_MAINSTACK 1024
  67. #endif
  68. #define MAINTHREAD_STACK_SIZE (((NUT_THREAD_MAINSTACK) * (NUT_THREAD_STACK_MULT)) + (NUT_THREAD_STACK_ADD))
  69. #ifndef NUT_THREAD_IDLESTACK
  70. #if defined(__GNUC__)
  71. /* avr-gcc optimized code used 36 bytes. */
  72. #define NUT_THREAD_IDLESTACK 128
  73. #else
  74. /* icc-avr v7.19 used 132 bytes. */
  75. #define NUT_THREAD_IDLESTACK 256
  76. #endif
  77. #endif
  78. #define IDLETHREAD_STACK_SIZE (((NUT_THREAD_IDLESTACK) * (NUT_THREAD_STACK_MULT)) + (NUT_THREAD_STACK_ADD))
  79. /* Running with avr-gccdbg and only internal stack may result in hard to
  80. * find errors where the stack got overwritten
  81. *
  82. * Do some sanity check!
  83. */
  84. #ifndef NUTXMEM_SIZE
  85. #if MAINTHREAD_STACK_SIZE + IDLETHREAD_STACK_SIZE + 1024 > NUTMEM_SIZE
  86. #warning "Potential stack overflow, reduce stack sizes of main or idle thread."
  87. #endif
  88. #endif
  89. #ifdef NUTMEM_RESERVED
  90. /*!
  91. * \brief Number of bytes reserved in on-chip memory.
  92. *
  93. * AVR offers the option to temporarily use address and data bus
  94. * lines as general purpose I/O. If such drivers need data memory,
  95. * this must be located at internal memory addresses.
  96. *
  97. * \todo Not a nice implementation but works as long as this module
  98. * is linked first. Should be made a linker option.
  99. */
  100. uint8_t nutmem_onchip[NUTMEM_RESERVED];
  101. #endif
  102. /* sleep mode to put avr in idle thread, SLEEP_MODE_NONE is used for for non sleeping */
  103. #if defined(__GNUC__) && defined(__AVR_ENHANCED__)
  104. uint8_t idle_sleep_mode = SLEEP_MODE_NONE;
  105. /* AT90CAN128 uses a different register to enter sleep mode */
  106. #if defined(SMCR)
  107. #define AVR_SLEEP_CTRL_REG SMCR
  108. #else
  109. #define AVR_SLEEP_CTRL_REG MCUCR
  110. #endif
  111. #endif
  112. #ifdef __GNUC__
  113. /*
  114. * Some special declarations for AVRGCC. The runtime library
  115. * executes section .init8 before finally jumping to main().
  116. * We never let it do that jump, but start main() as a
  117. * separate thread. This introduces new problems:
  118. * 1. The compiler reinitializes the stack pointer when
  119. * entering main, at least version 3.3 does it.
  120. * 2. The compiler doesn't allow to redeclare main to make
  121. * it fit for NutThreadCreate().
  122. * 3. The runtime library requires, that main is defined
  123. * somewhere.
  124. * Solutions:
  125. * 1. We do not use main at all, but let the preprocessor
  126. * redefine it to NutAppMain() in the application code.
  127. * See compiler.h. Note, that the declaration of NutAppMain
  128. * in this module differs from the one in the application
  129. * code. Fortunately the linker doesn't detect this hack.
  130. * 2. We use a linker option to set the symbol main to zero.
  131. *
  132. * Thanks to Joerg Wunsch, who helped to solve this.
  133. */
  134. void NutInit(void) NUT_NAKED_FUNC NUT_LINKER_SECT(".init8");
  135. extern void NutAppMain(void *arg) NUT_NORETURN_FUNC;
  136. #else
  137. extern void main(void *);
  138. #endif
  139. /*
  140. * External memory interface for GCC.
  141. */
  142. #if defined(__GNUC__) && defined(NUTXMEM_SIZE)
  143. /*
  144. * At the very beginning enable extended memory interface.
  145. */
  146. static void NutInitXRAM(void) NUT_NAKED_FUNC NUT_LINKER_SECT(".init1") NUT_USED_FUNC;
  147. void NutInitXRAM(void)
  148. {
  149. #if defined(__AVR_AT90CAN128__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega2561__) || defined(MCU_AT90USB1287)
  150. /*
  151. * Note: Register structure of ATCAN128 differs from ATMEGA128 in regards
  152. * to wait states.
  153. */
  154. #ifdef NUT_3WAITSTATES /* One wait state 1 for low, 3 for high memory range */
  155. XMCRA = _BV(SRE) | _BV(SRL2) | _BV(SRW00) | _BV(SRW10) | _BV(SRW11);
  156. #else
  157. XMCRA = _BV(SRE) | _BV(SRW10); /* One wait state for the whole memory range */
  158. #endif
  159. #elif defined(__AVR_ATmega128__)
  160. MCUCR = _BV(SRE) | _BV(SRW10);
  161. /* Configure two sectors, lower sector = 0x1100 - 0x7FFF,
  162. * Upper sector = 0x8000 - 0xFFFF and run 3 wait states for the
  163. * upper sector (NIC), 1 wait state for lower sector (XRAM).
  164. */
  165. #ifdef NUT_3WAITSTATES
  166. XMCRA |= _BV(SRL2) | _BV(SRW00) | _BV(SRW11); /* SRW10 is set in MCUCR */
  167. XMCRB = 0;
  168. #endif
  169. #else /* ATmega103 */
  170. MCUCR = _BV(SRE) | _BV(SRW);
  171. #endif
  172. }
  173. #endif
  174. /*! \fn NutThreadSetSleepMode(uint8_t mode)
  175. * \brief Sets the sleep mode to enter in Idle thread.
  176. *
  177. * If the idle thread is running, no other thread is active
  178. * so we can safely put the mcu to sleep.
  179. *
  180. * \param mode one of the sleep modes defined in avr/sleep.h or
  181. * sleep_mode_none (don't enter sleep mode)
  182. *
  183. * \return previous sleep mode
  184. */
  185. #if defined(__GNUC__) && defined(__AVR_ENHANCED__)
  186. uint8_t NutThreadSetSleepMode(uint8_t mode)
  187. {
  188. uint8_t old_mode = idle_sleep_mode;
  189. idle_sleep_mode = mode;
  190. return old_mode;
  191. }
  192. #endif
  193. static NutIdleCallback IdleCall;
  194. NutIdleCallback NutRegisterIdleCallback(NutIdleCallback func)
  195. {
  196. NutIdleCallback last = IdleCall;
  197. IdleCall = func;
  198. return last;
  199. }
  200. /*!
  201. * \brief AVR Idle thread.
  202. *
  203. * Running at priority 254 in an endless loop.
  204. */
  205. THREAD(NutIdle, arg)
  206. {
  207. #if defined(__GNUC__) && defined(__AVR_ENHANCED__)
  208. uint8_t sleep_mode;
  209. #endif
  210. #ifdef NUT_INIT_IDLE
  211. NutIdleInit();
  212. #endif
  213. /* Initialize system timers. */
  214. NutTimerInit();
  215. #ifdef NUT_INIT_MAIN
  216. NutMainInit();
  217. #endif
  218. /* Create the main application thread. */
  219. NutThreadCreate("main", main, 0, MAINTHREAD_STACK_SIZE);
  220. /*
  221. * Run in an idle loop at the lowest priority. We can still
  222. * do something useful here, like killing terminated threads
  223. * or putting the CPU into sleep mode.
  224. */
  225. NutThreadSetPriority(254);
  226. for (;;) {
  227. NutThreadYield();
  228. NutThreadDestroy();
  229. if (IdleCall) {
  230. IdleCall();
  231. }
  232. #if defined(__GNUC__) && defined(__AVR_ENHANCED__)
  233. if (idle_sleep_mode != SLEEP_MODE_NONE) {
  234. sleep_mode = AVR_SLEEP_CTRL_REG & _SLEEP_MODE_MASK;
  235. set_sleep_mode(idle_sleep_mode);
  236. #ifdef IDLE_THREAD_ADC_OFF
  237. uint8_t adc = bit_is_set(ADCSR, ADEN);
  238. cbi(ADCSR, ADEN); // disable ADC
  239. #endif
  240. #ifdef IDLE_THREAD_BUSKEEPER_OFF
  241. uint8_t bitkeeper = bit_is_set(XMCRB, XMBK);
  242. cbi(XMCRB, XMBK); // disable buskeeper
  243. #endif
  244. /* Note: avr-libc has a sleep_mode() function, but it's broken for
  245. AT90CAN128 with avr-libc version earlier than 1.2 */
  246. AVR_SLEEP_CTRL_REG |= _BV(SE);
  247. __asm__ __volatile__ ("sleep" "\n\t" :: );
  248. AVR_SLEEP_CTRL_REG &= ~_BV(SE);
  249. #ifdef IDLE_THREAD_ADC_OFF
  250. if (bitkeeper) {
  251. sbi(XMCRB, XMBK); // re-enable buskeeper
  252. }
  253. #endif
  254. #ifdef IDLE_THREAD_BUSKEEPER_OFF
  255. if (adc) {
  256. sbi(ADCSR, ADEN); // re-enable ADC
  257. }
  258. #endif
  259. set_sleep_mode(sleep_mode);
  260. }
  261. #endif
  262. }
  263. }
  264. #if defined(__GNUC__)
  265. static void NutInitSP(void) NUT_NAKED_FUNC NUT_LINKER_SECT(".init5") NUT_USED_FUNC;
  266. void NutInitSP(void)
  267. {
  268. #if defined (NUTMEM_STACKHEAP)
  269. /* Stack must remain in internal RAM where avr-libc's runtime lib init placed it */
  270. #else
  271. /* Initialize stack pointer to end of external RAM while starting up the system
  272. * to avoid overwriting .data and .bss section.
  273. */
  274. SP = (uint16_t)(NUTMEM_END);
  275. #endif
  276. }
  277. #endif
  278. #if defined(__GNUC__)
  279. static void NutInitHeap(void) NUT_NAKED_FUNC NUT_LINKER_SECT(".init5") NUT_USED_FUNC;
  280. #endif
  281. void NutInitHeap()
  282. {
  283. #if defined (NUTMEM_STACKHEAP) /* Stack resides in internal memory */
  284. NutStackAdd((void *) NUTMEM_START, NUTMEM_STACKHEAP);
  285. #endif
  286. /* Then add the remaining RAM to heap.
  287. *
  288. * 20.Aug.2004 haraldkipp: This had been messed up somehow. It's nice to have
  289. * one continuous heap area, but we lost the ability to have systems with
  290. * a gap between internal and external RAM.
  291. */
  292. if ((uint16_t)NUTMEM_END - (uint16_t) (&__heap_start) > 384) {
  293. NutHeapAdd(&__heap_start, (uint16_t) NUTMEM_END - 256 - (uint16_t) (&__heap_start));
  294. }
  295. }
  296. /*!
  297. * \brief Nut/OS Initialization.
  298. *
  299. * Initializes the memory management and the thread system and starts
  300. * an idle thread, which in turn initializes the timer management.
  301. * Finally the application's main() function is called.
  302. *
  303. * Depending on the compiler, different methods are used to execute this
  304. * function before main() is called.
  305. *
  306. * For ICCAVR the default crtatmega.o startup file is replaced by
  307. * crtnut.o, which calls NutInit instead of main(). This is done
  308. * by adding the following compiler options in the project:
  309. * \code -ucrtnut.o nutinit.o \endcode
  310. *
  311. * crtnut.o should be replaced by crtnutram.o, if the application's
  312. * variable space exceeds 4kB. For boards with RTL8019AS and EEPROM
  313. * emulation (like Ethernut 1.3 Rev-G) use crtenut.o or crtenutram.o.
  314. *
  315. * For AVRGCC this function is located in section .init8, which is
  316. * called immediately before jumping to main(). NutInit is defined
  317. * as:
  318. * \code
  319. * void NutInit(void) NUT_NAKED_FUNC NUT_LINKER_SECT(".init8");
  320. * \endcode
  321. *
  322. * \todo Make heap threshold configurable, currently hardcoded at 384.
  323. *
  324. * \todo Make wait states for external memory access configuratble.
  325. *
  326. * \todo Make early UART initialization for kernel debugging configurable.
  327. */
  328. void NutInit(void)
  329. {
  330. /*
  331. * We can't use local variables in naked functions.
  332. */
  333. #ifdef NUTDEBUG
  334. /* Note: The platform's default baudrate will be set in NutCustomInit() */
  335. outb(UCR, BV(RXEN) | BV(TXEN));
  336. #endif
  337. #ifdef NUT_INIT_BOARD
  338. NutBoardInit();
  339. #endif
  340. #ifndef __GNUC__
  341. /* Initialize stack pointer to end of external RAM while starting up the system
  342. * to avoid overwriting .data and .bss section.
  343. */
  344. SP = (uint16_t)(NUTMEM_END);
  345. /* Initialize the heap memory
  346. */
  347. NutInitHeap();
  348. #endif /* __GNUC__ */
  349. /* Read OS configuration from non-volatile memory. */
  350. NutLoadConfig();
  351. /* Create idle thread
  352. */
  353. NutThreadCreate("idle", NutIdle, 0, IDLETHREAD_STACK_SIZE);
  354. }
  355. /*@}*/