nutinit.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*!
  2. * Copyright (C) 2001-2010 by egnite Software GmbH
  3. *
  4. * 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. * $Id$
  36. */
  37. #include <cfg/clock.h>
  38. #include <cfg/arch.h>
  39. #include <cfg/memory.h>
  40. #include <cfg/os.h>
  41. #include <sys/heap.h>
  42. #include <sys/confos.h>
  43. #include <sys/thread.h>
  44. #include <sys/timer.h>
  45. #include <arch/avr32.h>
  46. #include <arch/avr32/pm.h>
  47. #include <arch/avr32/ihndlr.h>
  48. /*!
  49. * \addtogroup xgNutArchAvr32Init
  50. */
  51. /*@{*/
  52. #ifndef NUT_THREAD_MAINSTACK
  53. #define NUT_THREAD_MAINSTACK 1024
  54. #endif
  55. /* Unknown optimized value */
  56. #ifndef NUT_THREAD_IDLESTACK
  57. #define NUT_THREAD_IDLESTACK 512
  58. #endif
  59. extern void *__heap_start;
  60. extern void *__ram_start__;
  61. extern void *__ram_size__;
  62. /*!
  63. * \brief Last memory address.
  64. */
  65. #undef NUTMEM_START
  66. #define NUTMEM_START ((uint32_t)&__ram_start__)
  67. #undef NUTMEM_SIZE
  68. #define NUTMEM_SIZE ((uint32_t)&__ram_size__)
  69. #define NUTMEM_END (uptr_t)(NUTMEM_START + NUTMEM_SIZE - 1U)
  70. extern void *__heap_start;
  71. #define HEAP_START &__heap_start
  72. #define HEAP_SIZE ((uptr_t) (NUTMEM_END - 256 - (uptr_t) (&__heap_start)))
  73. #if !defined(__cplusplus)
  74. extern void NutAppMain(void *arg) NUT_NORETURN_FUNC;
  75. #else
  76. extern void main(void *);
  77. #endif
  78. /*!
  79. * \brief Idle thread.
  80. *
  81. * \param arg Ignored by the idle thread.
  82. *
  83. * This function runs in an endless loop as a lowest priority thread.
  84. */
  85. THREAD(NutIdle, arg)
  86. {
  87. /* Init INTC */
  88. init_interrupts();
  89. /* Initialize system timers. */
  90. NutTimerInit();
  91. /* Read OS configuration from non-volatile memory. We can't do this
  92. ** earlier, because the low level driver may be interrupt driven. */
  93. NutLoadConfig();
  94. /* Create the main application thread. Watch this carefully when
  95. ** changing compilers and compiler versions. Some handle main()
  96. ** in a special way, like setting the stack pointer and other
  97. ** weird stuff that may break this code. */
  98. NutThreadCreate("main", main, 0, (NUT_THREAD_MAINSTACK * NUT_THREAD_STACK_MULT) + NUT_THREAD_STACK_ADD);
  99. /*
  100. * Run in an idle loop at the lowest priority. We can still
  101. * do something useful here, like killing terminated threads
  102. * or putting the CPU into sleep mode.
  103. */
  104. NutThreadSetPriority(254);
  105. for (;;) {
  106. NutThreadYield();
  107. NutThreadDestroy();
  108. }
  109. }
  110. /*!
  111. * \brief Nut/OS Initialization.
  112. *
  113. * Initializes the memory management and the thread system and starts
  114. * an idle thread, which in turn initializes the timer management.
  115. * Finally the application's main() function is called.
  116. */
  117. void NutInit(void)
  118. {
  119. Avr32InitClockTree();
  120. #ifdef EARLY_STDIO_DEV
  121. /* We may optionally initialize stdout as early as possible.
  122. ** Be aware, that no heap is available and no threads are
  123. ** running. We need a very basic driver here, which won't
  124. ** use interrupts or call malloc, NutEventXxx, NutSleep etc. */
  125. {
  126. extern NUTFILE *__fds[FOPEN_MAX];
  127. extern FILE *__iob[FOPEN_MAX];
  128. extern NUTDEVICE EARLY_STDIO_DEV;
  129. static struct __iobuf early_stdout;
  130. /* Initialize the output device. */
  131. EARLY_STDIO_DEV.dev_init(&EARLY_STDIO_DEV);
  132. /* Assign a static iobuf. */
  133. stdout = &early_stdout;
  134. /* Open the device. */
  135. __fds[1] = (int) EARLY_STDIO_DEV.dev_open(&EARLY_STDIO_DEV, "", 0, 0);
  136. __iob[1] = stdout;
  137. stdout->iob_fd = 1;
  138. /* Set the mode. No idea if this is required. */
  139. stdout->iob_mode = _O_WRONLY | _O_CREAT | _O_TRUNC;
  140. /* A first trial. */
  141. puts("\nStarting Nut/OS");
  142. }
  143. #endif
  144. /* Initialize our heap memory. */
  145. NutHeapAdd(HEAP_START, HEAP_SIZE);
  146. /* Create idle thread. Note, that the first call to NutThreadCreate
  147. ** will never return. */
  148. NutThreadCreate("idle", NutIdle, 0, (NUT_THREAD_IDLESTACK * NUT_THREAD_STACK_MULT) + NUT_THREAD_STACK_ADD);
  149. }
  150. /*@}*/