From 4fc627f08b942df8312ad02f24a361b18c4b790b Mon Sep 17 00:00:00 2001 From: gdisirio Date: Sun, 18 Nov 2007 09:45:45 +0000 Subject: git-svn-id: svn://svn.code.sf.net/p/chibios/svn/trunk@95 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- src/chinit.c | 36 ++++++++++++++++++------------------ src/templates/chcore.c | 4 +--- src/templates/chcore.h | 16 ++++++++++++++-- 3 files changed, 33 insertions(+), 23 deletions(-) (limited to 'src') diff --git a/src/chinit.c b/src/chinit.c index 4a25567a8..918da1796 100644 --- a/src/chinit.c +++ b/src/chinit.c @@ -24,27 +24,17 @@ #include -static Thread idlethread; /** * ChibiOS/RT initialization. After executing this function the current - * instructions stream becomes the idle thread. The thread must execute the - * first user thread and then go to sleep into the \p chSysPause() where it - * will just serve the interrupts while keeping the lowest possible power - * mode.

- * @code - * chSysInit(); - * chThdCreate(...); // Starts one or more user threads. - * chSysPause(); - * @endcode + * instructions stream becomes the main thread. * @note Interrupts should be still disabled when \p chSysInit() is invoked * and are internally enabled. - * @note The idle thread has absolute priority when exiting from the - * \p chSysInit(), this is done to make sure that all the initializations - * performed in the \p main() procedure are completed before any thread - * starts. The priority is set to \p IDLEPRIO into the \p chSysPause(). + * @note The main thread is created with priority \p NORMALPRIO. */ void chSysInit(void) { + static Thread mainthread; + static BYTE8 waIdleThread[UserStackSize(IDLE_THREAD_STACK_SIZE)]; chSchInit(); chDbgInit(); @@ -52,13 +42,23 @@ void chSysInit(void) { chVTInit(); #endif /* - * Now this instructions flow becomes the idle thread. + * Now this instructions flow becomes the main thread. */ - _InitThread(ABSPRIO, 0, &idlethread); - idlethread.p_state = PRCURR; - currp = &idlethread; + _InitThread(NORMALPRIO, 0, &mainthread); + mainthread.p_state = PRCURR; + currp = &mainthread; chSysUnlock(); + + /* + * The idle thread is created using the port-provided implementation. + * This thread has the lowest priority in the system, its role is just to + * execute the chSysPause() and serve interrupts in its context. + * In ChibiOS/RT at least one thread in the system *must* execute + * chThdPause(), it can be done in a dedicated thread or in the main() + * function (that would never exit the call). + */ + chThdCreate(IDLEPRIO, 0, waIdleThread, sizeof(waIdleThread), (t_tfunc)_IdleThread, NULL); } /** @} */ diff --git a/src/templates/chcore.c b/src/templates/chcore.c index bc8c68b90..ec2b39592 100644 --- a/src/templates/chcore.c +++ b/src/templates/chcore.c @@ -36,9 +36,7 @@ * The priority is internally set to the minimum system value so that this * thread is executed only if there are no other ready threads in the system. */ -void chSysPause(void) { - - chThdSetPriority(IDLEPRIO); +void _IdleThread(void *p) { while (TRUE) ; diff --git a/src/templates/chcore.h b/src/templates/chcore.h index e917ffa6f..7f2c5fb0b 100644 --- a/src/templates/chcore.h +++ b/src/templates/chcore.h @@ -42,8 +42,20 @@ typedef struct { { \ } -#define INT_REQUIRED_STACK 0 // Must include registers and stack frames. +/** + * Stack size for the system idle thread. + */ +#define IDLE_THREAD_STACK_SIZE 0 +/** + * Per-thread stack overhead for interrupts servicing. + */ +#define INT_REQUIRED_STACK 0 + +/** + * Macro to be used when allocating stack spaces, it adds the system-specific + * overhead. + */ #define UserStackSize(n) (sizeof(Thread) + \ sizeof(struct stackregs) + (n) + (INT_REQUIRED_STACK)) @@ -74,8 +86,8 @@ typedef struct { */ #define chSysPuts(msg) {} +void _IdleThread(void *p); void chSysHalt(void); -void chSysPause(void); void chSysSwitchI(Context *oldp, Context *newp); #endif /* _CHCORE_H_ */ -- cgit v1.2.3