diff --git a/components/finsh/cmd.c b/components/finsh/cmd.c index a97ef3caab2ee89d0b0971db08fefab0cb0b3fea..c66f72e650938dff48a10c886f96d0e2dbce8b7c 100644 --- a/components/finsh/cmd.c +++ b/components/finsh/cmd.c @@ -101,16 +101,28 @@ static long _list_thread(struct rt_list_node *list) else if (stat == RT_THREAD_INIT) rt_kprintf(" init "); else if (stat == RT_THREAD_CLOSE) rt_kprintf(" close "); +#if defined(ARCH_CPU_STACK_GROWS_UPWARD) + ptr = (rt_uint8_t *)thread->stack_addr + thread->stack_size; + while (*ptr == '#')ptr --; + + rt_kprintf(" 0x%08x 0x%08x %02d%% 0x%08x %03d\n", + ((rt_uint32_t)thread->sp - (rt_uint32_t)thread->stack_addr), + thread->stack_size, + ((rt_uint32_t)ptr - (rt_uint32_t)thread->stack_addr) * 100 / thread->stack_size, + thread->remaining_tick, + thread->error); +#else ptr = (rt_uint8_t *)thread->stack_addr; while (*ptr == '#')ptr ++; rt_kprintf(" 0x%08x 0x%08x %02d%% 0x%08x %03d\n", - thread->stack_size + ((rt_uint32_t)thread->stack_addr - (rt_uint32_t)thread->sp), + (thread->stack_size + (rt_uint32_t)thread->stack_addr - (rt_uint32_t)thread->sp), thread->stack_size, - (thread->stack_size - ((rt_uint32_t) ptr - (rt_uint32_t) thread->stack_addr)) * 100 + (thread->stack_size + (rt_uint32_t)thread->stack_addr - (rt_uint32_t) ptr) * 100 / thread->stack_size, thread->remaining_tick, thread->error); +#endif } return 0; diff --git a/libcpu/Kconfig b/libcpu/Kconfig index 0a280d4b5cb2be2ae4cdb372471ff8298ec9f26e..3c80428b5d89b457b0fa774b99853f72f9fdfb00 100644 --- a/libcpu/Kconfig +++ b/libcpu/Kconfig @@ -88,3 +88,7 @@ config ARCH_IA32 config ARCH_HOST_SIMULATOR bool + +config ARCH_CPU_STACK_GROWS_UPWARD + bool + default n diff --git a/src/scheduler.c b/src/scheduler.c index 95b5d487df3078db436234ea38647cf51ffb0f8b..0b8b54ae92bd6b468a537f79ad7750f782b8c815 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -91,11 +91,19 @@ static void _rt_scheduler_stack_check(struct rt_thread *thread) level = rt_hw_interrupt_disable(); while (level); } +#if defined(ARCH_CPU_STACK_GROWS_UPWARD) + else if ((rt_uint32_t)thread->sp > ((rt_uint32_t)thread->stack_addr + thread->stack_size)) + { + rt_kprintf("warning: %s stack is close to the top of stack address.\n", + thread->name); + } +#else else if ((rt_uint32_t)thread->sp <= ((rt_uint32_t)thread->stack_addr + 32)) { - rt_kprintf("warning: %s stack is close to end of stack address.\n", + rt_kprintf("warning: %s stack is close to the bottom of stack address.\n", thread->name); } +#endif } #endif diff --git a/src/thread.c b/src/thread.c index 6d79ece54bfe09eda5f8053492dbe814952c4541..687852e92e19108c105facfdd749c1626c97e92c 100644 --- a/src/thread.c +++ b/src/thread.c @@ -136,9 +136,15 @@ static rt_err_t _rt_thread_init(struct rt_thread *thread, /* init thread stack */ rt_memset(thread->stack_addr, '#', thread->stack_size); +#ifdef ARCH_CPU_STACK_GROWS_UPWARD + thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter, + (void *)((char *)thread->stack_addr), + (void *)rt_thread_exit); +#else thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter, (void *)((char *)thread->stack_addr + thread->stack_size - 4), (void *)rt_thread_exit); +#endif /* priority init */ RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX);