nutinit.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Copyright 2012 by Embedded Technologies s.r.o
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. */
  32. #include <cfg/arch.h>
  33. #include <arch/m68k.h>
  34. #include <dev/board.h>
  35. #ifdef EARLY_STDIO_DEV
  36. #include <sys/device.h>
  37. #include <stdio.h>
  38. #include <fcntl.h>
  39. #include <dev/debug.h>
  40. struct __iobuf {
  41. int iob_fd;
  42. uint16_t iob_mode;
  43. uint8_t iob_flags;
  44. int iob_unget;
  45. };
  46. #endif
  47. #ifndef NUT_THREAD_MAINSTACK
  48. #define NUT_THREAD_MAINSTACK 1024
  49. #endif
  50. #ifndef NUT_THREAD_IDLESTACK
  51. #define NUT_THREAD_IDLESTACK 512
  52. #endif
  53. /*!
  54. * \brief Memory address and sizes.
  55. */
  56. extern void *__heap_start;
  57. extern void *__heap_size;
  58. extern void *__heap2_start;
  59. extern void *__heap2_size;
  60. //extern void *__stack_init_start;
  61. //extern void *__stack_init_size;
  62. #define HEAP_START &__heap_start
  63. #define HEAP_SIZE ((size_t)&__heap_size)
  64. #define HEAP2_START &__heap2_start
  65. #define HEAP2_SIZE ((size_t)&__heap2_size)
  66. //#define STACK_INIT_START &__stack_init_start
  67. //#define STACK_INIT_SIZE ((size_t)&__stack_init_size)
  68. #ifdef NUTMEM_STACKHEAP
  69. extern void *__stack_heap_start;
  70. extern void *__stack_heap_size;
  71. #define STACK_HEAP_START &__stack_heap_start
  72. #define STACK_HEAP_SIZE ((size_t)&__stack_heap_size)
  73. #endif
  74. #define NUT_THREAD_CONFIGSTACK NUT_THREAD_IDLESTACK
  75. extern void main(void *arg) __attribute__ ((noreturn));
  76. /*!
  77. * \brief M68K Start thread.
  78. *
  79. * Configures OS and starts the main application thread.
  80. *
  81. */
  82. THREAD( NutStart, arg)
  83. {
  84. /*
  85. * Read OS configuration from non-volatile memory.
  86. * This cannot be called sooner, because NonVolatile memory driver
  87. * may use interrupts, timers and thread switching.
  88. */
  89. NutLoadConfig();
  90. #ifdef NUT_INIT_MAIN
  91. /*
  92. * Call custom initialization.
  93. * NOTE: All OS parts are ready. Whole Nut/OS API may be used.
  94. */
  95. NutMainInit();
  96. #endif
  97. /*
  98. * Create the main application thread.
  99. */
  100. NutThreadCreate("main", main, 0, (NUT_THREAD_MAINSTACK * NUT_THREAD_STACK_MULT) + NUT_THREAD_STACK_ADD);
  101. /*
  102. * Terminate the thread
  103. */
  104. NutThreadExit();
  105. while(1)
  106. NutThreadYield();
  107. }
  108. /*!
  109. * \brief Idle thread.
  110. *
  111. * Running at priority 254 in an endless loop.
  112. */
  113. THREAD( NutIdle, arg)
  114. {
  115. /*
  116. * Enable interrupts.
  117. * SR[IPL] = 0; (See NutThreadCreate(): ef->sr = 0x2000;)
  118. * IMRL[0] = 0;
  119. */
  120. #ifdef MCU_MCF5225X // JS TODO
  121. MCF_INTC_IMRL(0) &= ~MCF_INTC_IMRL_MASKALL;
  122. #endif
  123. #ifdef NUT_INIT_IDLE
  124. /*
  125. * Call custom initialization
  126. * NOTE: context switching and timers cannot be used yet.
  127. */
  128. NutIdleInit();
  129. #endif
  130. /*
  131. * Initialize system timers.
  132. */
  133. NutTimerInit();
  134. /*
  135. * Create the Start thread.
  136. */
  137. NutThreadCreate("config", NutStart, 0, (NUT_THREAD_CONFIGSTACK * NUT_THREAD_STACK_MULT) + NUT_THREAD_STACK_ADD);
  138. /*
  139. * Run in an idle loop at the lowest priority. We can still
  140. * do something useful here, like killing terminated threads
  141. * or putting the CPU into sleep mode.
  142. */
  143. NutThreadSetPriority(254);
  144. for (;;) {
  145. NutThreadYield();
  146. NutThreadDestroy();
  147. }
  148. }
  149. /*!
  150. * \brief Nut/OS Initialization.
  151. *
  152. * Initializes the memory management and the thread system and starts
  153. * an idle thread, which in turn initializes the timer management.
  154. * Finally the application's main() function is called.
  155. */
  156. void NutInit(void)
  157. {
  158. size_t heap2_size = HEAP2_SIZE;
  159. #ifdef NUTMEM_STACKHEAP
  160. size_t stackheap_size = STACK_HEAP_SIZE;
  161. #endif
  162. #ifdef EARLY_STDIO_DEV
  163. /* We may optionally initialize stdout as early as possible.
  164. ** Be aware, that no heap is available and no threads are
  165. ** running. We need a very basic driver here, which won't
  166. ** use interrupts or call malloc, NutEventXxx, NutSleep etc. */
  167. {
  168. extern NUTFILE *__fds[FOPEN_MAX];
  169. extern FILE *__iob[FOPEN_MAX];
  170. extern NUTDEVICE EARLY_STDIO_DEV;
  171. static struct __iobuf early_stdout;
  172. /* Initialize the output device. */
  173. EARLY_STDIO_DEV.dev_init(&EARLY_STDIO_DEV);
  174. /* Assign a static iobuf. */
  175. stdout = &early_stdout;
  176. /* Open the device. */
  177. __fds[1] = (int) EARLY_STDIO_DEV.dev_open(&EARLY_STDIO_DEV, "", 0, 0);
  178. __iob[1] = stdout;
  179. stdout->iob_fd = 1;
  180. /* Set the mode. No idea if this is required. */
  181. stdout->iob_mode = _O_WRONLY | _O_CREAT | _O_TRUNC;
  182. /* A first trial. */
  183. puts("\nStarting Nut/OS");
  184. }
  185. #endif
  186. #ifdef NUT_INIT_BOARD
  187. NutBoardInit();
  188. #endif
  189. /* Initialize heap memory. */
  190. NutHeapAdd(HEAP_START, HEAP_SIZE);
  191. /* Add next heap region if defined */
  192. if (heap2_size)
  193. NutHeapAdd(HEAP2_START, heap2_size);
  194. /* Initialize stack heap (fast internal memory) if defined */
  195. #ifdef NUTMEM_STACKHEAP
  196. if (stackheap_size)
  197. NutStackAdd(STACK_HEAP_START, stackheap_size);
  198. #endif
  199. /* Create idle thread */
  200. NutThreadCreate("idle", NutIdle, 0, (NUT_THREAD_IDLESTACK * NUT_THREAD_STACK_MULT) + NUT_THREAD_STACK_ADD);
  201. }
  202. /*@}*/