From 15a54ae359d342b296b7740dcfaf0580d606b3c6 Mon Sep 17 00:00:00 2001 From: gdisirio Date: Thu, 5 Sep 2013 14:54:24 +0000 Subject: Nil working on M3/M4. git-svn-id: svn://svn.code.sf.net/p/chibios/svn/branches/kernel_3_dev@6265 35acf78f-673a-0410-8e92-d51de3d6d3f4 --- demos/nil/NIL-STM32F051-DISCOVERY/main.c | 6 ++--- demos/nil/NIL-STM32F051-DISCOVERY/nilconf.h | 7 ++++-- demos/nil/NIL-STM32F303-DISCOVERY/main.c | 10 ++++---- demos/nil/NIL-STM32F303-DISCOVERY/nilconf.h | 7 ++++-- os/nil/include/nil.h | 38 ++++++++++++++++++++--------- os/nil/osal/osal.h | 2 +- os/nil/ports/ARMCMx/nilcore_v6m.h | 7 +++--- os/nil/ports/ARMCMx/nilcore_v7m.h | 7 +++--- os/nil/src/nil.c | 27 +++++++++++++------- os/rt/ports/ARMCMx/chcore_v6m.h | 2 +- os/rt/ports/ARMCMx/chcore_v7m.h | 2 +- 11 files changed, 71 insertions(+), 44 deletions(-) diff --git a/demos/nil/NIL-STM32F051-DISCOVERY/main.c b/demos/nil/NIL-STM32F051-DISCOVERY/main.c index b27cd5911..d4c17e90a 100644 --- a/demos/nil/NIL-STM32F051-DISCOVERY/main.c +++ b/demos/nil/NIL-STM32F051-DISCOVERY/main.c @@ -79,9 +79,9 @@ THD_FUNCTION(Thread3, arg) { * match NIL_CFG_NUM_THREADS. */ THD_TABLE_BEGIN - THD_TABLE_ENTRY("blinker1", Thread1, NULL, waThread1, sizeof(waThread1)) - THD_TABLE_ENTRY("blinker2", Thread2, NULL, waThread2, sizeof(waThread2)) - THD_TABLE_ENTRY("hello", Thread3, NULL, waThread3, sizeof(waThread3)) + THD_TABLE_ENTRY(waThread1, "blinker1", Thread1, NULL) + THD_TABLE_ENTRY(waThread2, "blinker2", Thread2, NULL) + THD_TABLE_ENTRY(waThread3, "hello", Thread3, NULL) THD_TABLE_END /* diff --git a/demos/nil/NIL-STM32F051-DISCOVERY/nilconf.h b/demos/nil/NIL-STM32F051-DISCOVERY/nilconf.h index 2805beb50..5276a3f2a 100644 --- a/demos/nil/NIL-STM32F051-DISCOVERY/nilconf.h +++ b/demos/nil/NIL-STM32F051-DISCOVERY/nilconf.h @@ -59,15 +59,18 @@ * * @note The default is @p TRUE. */ -#if !defined(NIL_CFG_USE_EVENTS) || defined(__DOXYGEN__) #define NIL_CFG_USE_EVENTS TRUE -#endif /** * @brief System assertions. */ #define NIL_CFG_ENABLE_ASSERTS FALSE +/** + * @brief Stack check. + */ +#define NIL_CFG_ENABLE_STACK_CHECK FALSE + /** * @brief Threads descriptor structure extension. * @details User fields added to the end of the @p thread_t structure. diff --git a/demos/nil/NIL-STM32F303-DISCOVERY/main.c b/demos/nil/NIL-STM32F303-DISCOVERY/main.c index 245e31226..36c3793e0 100644 --- a/demos/nil/NIL-STM32F303-DISCOVERY/main.c +++ b/demos/nil/NIL-STM32F303-DISCOVERY/main.c @@ -65,8 +65,8 @@ THD_FUNCTION(Thread3, arg) { * PA9 and PA10 are routed to USART1. */ sdStart(&SD1, NULL); - palSetPadMode(GPIOA, 9, PAL_MODE_ALTERNATE(1)); /* USART1 TX. */ - palSetPadMode(GPIOA, 10, PAL_MODE_ALTERNATE(1)); /* USART1 RX. */ + palSetPadMode(GPIOA, 9, PAL_MODE_ALTERNATE(7)); /* USART1 TX. */ + palSetPadMode(GPIOA, 10, PAL_MODE_ALTERNATE(7)); /* USART1 RX. */ while (true) { chnWrite(&SD1, (const uint8_t *)"Hello World!\r\n", 14); @@ -79,9 +79,9 @@ THD_FUNCTION(Thread3, arg) { * match NIL_CFG_NUM_THREADS. */ THD_TABLE_BEGIN - THD_TABLE_ENTRY("blinker1", Thread1, NULL, waThread1, sizeof(waThread1)) - THD_TABLE_ENTRY("blinker2", Thread2, NULL, waThread2, sizeof(waThread2)) - THD_TABLE_ENTRY("hello", Thread3, NULL, waThread3, sizeof(waThread3)) + THD_TABLE_ENTRY(waThread1, "blinker1", Thread1, NULL) + THD_TABLE_ENTRY(waThread2, "blinker2", Thread2, NULL) + THD_TABLE_ENTRY(waThread3, "hello", Thread3, NULL) THD_TABLE_END /* diff --git a/demos/nil/NIL-STM32F303-DISCOVERY/nilconf.h b/demos/nil/NIL-STM32F303-DISCOVERY/nilconf.h index 2805beb50..5276a3f2a 100644 --- a/demos/nil/NIL-STM32F303-DISCOVERY/nilconf.h +++ b/demos/nil/NIL-STM32F303-DISCOVERY/nilconf.h @@ -59,15 +59,18 @@ * * @note The default is @p TRUE. */ -#if !defined(NIL_CFG_USE_EVENTS) || defined(__DOXYGEN__) #define NIL_CFG_USE_EVENTS TRUE -#endif /** * @brief System assertions. */ #define NIL_CFG_ENABLE_ASSERTS FALSE +/** + * @brief Stack check. + */ +#define NIL_CFG_ENABLE_STACK_CHECK FALSE + /** * @brief Threads descriptor structure extension. * @details User fields added to the end of the @p thread_t structure. diff --git a/os/nil/include/nil.h b/os/nil/include/nil.h index ee575d466..1c8aaf359 100644 --- a/os/nil/include/nil.h +++ b/os/nil/include/nil.h @@ -30,8 +30,11 @@ #ifndef _NIL_H_ #define _NIL_H_ +typedef struct nil_thread thread_t; + #include "nilconf.h" #include "niltypes.h" +#include "nilcore.h" /*===========================================================================*/ /* Module constants. */ @@ -197,9 +200,21 @@ #endif #if NIL_CFG_ENABLE_ASSERTS -#define CH_DBG_ENABLED TRUE +#define NIL_DBG_ENABLED TRUE +#else +#define NIL_DBG_ENABLED FALSE +#endif + +/** Boundaries of the idle thread boundaries, only required if stack checking + is enabled.*/ +#if NIL_CFG_ENABLE_STACK_CHECK +extern stkalign_t __main_thread_stack_base__, __main_thread_stack_end__; + +#define THD_IDLE_BASE (&__main_thread_stack_base__) +#define THD_IDLE_END (&__main_thread_stack_end__) #else -#define CH_DBG_ENABLED FALSE +#define THD_IDLE_BASE NULL +#define THD_IDLE_END NULL #endif /*===========================================================================*/ @@ -237,11 +252,11 @@ typedef struct nil_thread thread_t; * @brief Structure representing a thread static configuration. */ struct nil_thread_cfg { + stkalign_t *wbase; /**< @brief Thread working area base. */ + stkalign_t *wend; /**< @brief Thread working area end. */ const char *namep; /**< @brief Thread name, for debugging. */ tfunc_t funcp; /**< @brief Thread function. */ void *arg; /**< @brief Thread function argument. */ - void *wap; /**< @brief Thread working area base. */ - size_t size; /**< @brief Thread working area size. */ }; /** @@ -270,6 +285,9 @@ struct nil_thread { if disabled. */ #if NIL_CFG_USE_EVENTS eventmask_t epmask; /**< @brief Pending events mask. */ +#endif +#if NIL_CFG_ENABLE_STACK_CHECK || defined(__DOXYGEN__) + stkalign_t *stklim;/**< @brief Thread stack boundary. */ #endif /* Optional extra fields.*/ NIL_CFG_THREAD_EXT_FIELDS @@ -311,7 +329,7 @@ typedef struct { * @brief Thread structures for all the defined threads. */ thread_t threads[NIL_CFG_NUM_THREADS + 1]; -#if CH_DBG_ENABLED || defined(__DOXYGEN__) +#if NIL_DBG_ENABLED || defined(__DOXYGEN__) /** * @brief Panic message. * @note This field is only present if some debug options have been @@ -343,14 +361,14 @@ typedef struct { /** * @brief Entry of user threads table */ -#define THD_TABLE_ENTRY(name, funcp, arg, wap, size) \ - {name, funcp, arg, wap, size}, +#define THD_TABLE_ENTRY(wap, name, funcp, arg) \ + {wap, (wap) + sizeof (wap), name, funcp, arg}, /** * @brief End of user threads table. */ #define THD_TABLE_END \ - {"idle", 0, NULL, NULL, 0} \ + {THD_IDLE_BASE, THD_IDLE_END, "idle", 0, NULL} \ }; /** @} */ @@ -754,10 +772,6 @@ extern "C" { } #endif -/* Late inclusion, this is done in order to let the port layer access - the OS services like assertions.*/ -#include "nilcore.h" - #endif /* _NIL_H_ */ /** @} */ diff --git a/os/nil/osal/osal.h b/os/nil/osal/osal.h index a974452da..d4394687e 100644 --- a/os/nil/osal/osal.h +++ b/os/nil/osal/osal.h @@ -246,7 +246,7 @@ typedef struct { * * @api */ -#define osalDbgCheck(c) /*chDbgCheck(c)*/ +#define osalDbgCheck(c) chDbgAssert(c, "parameter check") /** * @brief I-Class state check. diff --git a/os/nil/ports/ARMCMx/nilcore_v6m.h b/os/nil/ports/ARMCMx/nilcore_v6m.h index 60a2549a2..159ce9989 100644 --- a/os/nil/ports/ARMCMx/nilcore_v6m.h +++ b/os/nil/ports/ARMCMx/nilcore_v6m.h @@ -192,9 +192,8 @@ struct port_intctx { * @details This code usually setup the context switching frame represented * by an @p port_intctx structure. */ -#define PORT_SETUP_CONTEXT(tp, workspace, wsize, pf, arg) { \ - (tp)->ctxp = (struct port_intctx *)((uint8_t *)workspace + \ - (size_t)wsize - \ +#define PORT_SETUP_CONTEXT(tp, wend, pf, arg) { \ + (tp)->ctxp = (struct port_intctx *)(((uint8_t *)(wend)) - \ sizeof(struct port_intctx)); \ (tp)->ctxp->r4 = (regarm_t)(pf); \ (tp)->ctxp->r5 = (regarm_t)(arg); \ @@ -254,7 +253,7 @@ struct port_intctx { #else #define port_switch(ntp, otp) { \ struct port_intctx *r13 = (struct port_intctx *)__get_PSP(); \ - if ((stkalign_t *)(r13 - 1) < otp->p_stklimit) \ + if ((stkalign_t *)(r13 - 1) < (otp)->stklim) \ chSysHalt("stack overflow"); \ _port_switch(ntp, otp); \ } diff --git a/os/nil/ports/ARMCMx/nilcore_v7m.h b/os/nil/ports/ARMCMx/nilcore_v7m.h index 4cbcc6e28..0bc4fdf05 100644 --- a/os/nil/ports/ARMCMx/nilcore_v7m.h +++ b/os/nil/ports/ARMCMx/nilcore_v7m.h @@ -291,9 +291,8 @@ struct port_intctx { * @details This code usually setup the context switching frame represented * by an @p port_intctx structure. */ -#define PORT_SETUP_CONTEXT(tp, workspace, wsize, pf, arg) { \ - (tp)->ctxp = (struct port_intctx *)((uint8_t *)workspace + \ - (size_t)wsize - \ +#define PORT_SETUP_CONTEXT(tp, wend, pf, arg) { \ + (tp)->ctxp = (struct port_intctx *)(((uint8_t *)(wend)) - \ sizeof(struct port_intctx)); \ (tp)->ctxp->r4 = (regarm_t)(pf); \ (tp)->ctxp->r5 = (regarm_t)(arg); \ @@ -351,7 +350,7 @@ struct port_intctx { #else #define port_switch(ntp, otp) { \ struct port_intctx *r13 = (struct port_intctx *)__get_PSP(); \ - if ((stkalign_t *)(r13 - 1) < otp->p_stklimit) \ + if ((stkalign_t *)(r13 - 1) < (otp)->stklim) \ chSysHalt("stack overflow"); \ _port_switch(ntp, otp); \ } diff --git a/os/nil/src/nil.c b/os/nil/src/nil.c index 546854280..aefe4ccb2 100644 --- a/os/nil/src/nil.c +++ b/os/nil/src/nil.c @@ -62,6 +62,9 @@ nil_system_t nil; * @details Initializes the kernel structures, the current instructions flow * becomes the idle thread upon return. The idle thread must not * invoke any kernel primitive able to change state to not runnable. + * @note This function assumes that the @p nil global variable has been + * zeroed by the runtime environment. If this is not the case then + * make sure to clear it before calling this function. * * @special */ @@ -73,28 +76,34 @@ void chSysInit(void) { port_init(); /* Iterates through the list of defined threads.*/ - for (tp = &nil.threads[0], tcp = nil_thd_configs; - tp < &nil.threads[NIL_CFG_NUM_THREADS]; - tp++, tcp++) { - tp->state = NIL_STATE_READY; - tp->timeout = 0; -#if NIL_CFG_USE_EVENTS - tp->epmask = 0; + tp = &nil.threads[0]; + tcp = nil_thd_configs; + while (tp < &nil.threads[NIL_CFG_NUM_THREADS]) { +#if NIL_CFG_ENABLE_STACK_CHECK + tp->stklim = (stkalign_t *)tcp->wbase; #endif /* Port dependent thread initialization.*/ - PORT_SETUP_CONTEXT(tp, tcp->wap, tcp->size, tcp->funcp, tcp->arg); + PORT_SETUP_CONTEXT(tp, tcp->wend, tcp->funcp, tcp->arg); /* Initialization hook.*/ #if defined(NIL_CFG_THREAD_EXT_INIT_HOOK) NIL_CFG_THREAD_EXT_INIT_HOOK(tp); #endif + + tp++, tcp++; } +#if NIL_CFG_ENABLE_STACK_CHECK + /* The idle thread is a special case because its stack is set up by the + runtime environment.*/ + tp->stklim = THD_IDLE_BASE; +#endif + /* Runs the highest priority thread, the current one becomes the null thread.*/ nil.current = nil.next = nil.threads; - port_switch(nil.threads, &nil.threads[NIL_CFG_NUM_THREADS]); + port_switch(nil.current, tp); /* Interrupts enabled for the idle thread.*/ chSysEnable(); diff --git a/os/rt/ports/ARMCMx/chcore_v6m.h b/os/rt/ports/ARMCMx/chcore_v6m.h index 62dde2b30..fc53c0202 100644 --- a/os/rt/ports/ARMCMx/chcore_v6m.h +++ b/os/rt/ports/ARMCMx/chcore_v6m.h @@ -264,7 +264,7 @@ struct context { #else #define port_switch(ntp, otp) { \ struct port_intctx *r13 = (struct port_intctx *)__get_PSP(); \ - if ((stkalign_t *)(r13 - 1) < otp->p_stklimit) \ + if ((stkalign_t *)(r13 - 1) < (otp)->p_stklimit) \ chSysHalt("stack overflow"); \ _port_switch(ntp, otp); \ } diff --git a/os/rt/ports/ARMCMx/chcore_v7m.h b/os/rt/ports/ARMCMx/chcore_v7m.h index fb5bd4476..c802a980d 100644 --- a/os/rt/ports/ARMCMx/chcore_v7m.h +++ b/os/rt/ports/ARMCMx/chcore_v7m.h @@ -361,7 +361,7 @@ struct context { #else #define port_switch(ntp, otp) { \ struct port_intctx *r13 = (struct port_intctx *)__get_PSP(); \ - if ((stkalign_t *)(r13 - 1) < otp->p_stklimit) \ + if ((stkalign_t *)(r13 - 1) < (otp)->p_stklimit) \ chSysHalt("stack overflow"); \ _port_switch(ntp, otp); \ } -- cgit v1.2.3