diff --git a/libraries/net/lwip/lwip-sys/arch/sys_arch.c b/libraries/net/lwip/lwip-sys/arch/sys_arch.c index 331b0a2257..78f9827aa3 100644 --- a/libraries/net/lwip/lwip-sys/arch/sys_arch.c +++ b/libraries/net/lwip/lwip-sys/arch/sys_arch.c @@ -426,12 +426,10 @@ sys_thread_t sys_thread_new(const char *pcName, t->def.pthread = (os_pthread)thread; t->def.tpriority = (osPriority)priority; t->def.stacksize = stacksize; -#ifndef __MBED_CMSIS_RTOS_CA9 t->def.stack_pointer = (uint32_t*)malloc(stacksize); if (t->def.stack_pointer == NULL) { error("Error allocating the stack memory"); } -#endif #endif t->id = osThreadCreate(&t->def, arg); if (t->id == NULL) diff --git a/libraries/rtos/rtos/Thread.cpp b/libraries/rtos/rtos/Thread.cpp index 1c6aa9edf9..fd9b6feddd 100644 --- a/libraries/rtos/rtos/Thread.cpp +++ b/libraries/rtos/rtos/Thread.cpp @@ -31,7 +31,6 @@ Thread::Thread(void (*task)(void const *argument), void *argument, _thread_def.pthread = task; _thread_def.tpriority = priority; _thread_def.stacksize = stack_size; -#ifndef __MBED_CMSIS_RTOS_CA9 if (stack_pointer != NULL) { _thread_def.stack_pointer = (uint32_t*)stack_pointer; _dynamic_stack = false; @@ -46,7 +45,6 @@ Thread::Thread(void (*task)(void const *argument), void *argument, for (int i = 0; i < (stack_size / sizeof(uint32_t)); i++) { _thread_def.stack_pointer[i] = 0xE25A2EA5; } -#endif #endif _tid = osThreadCreate(&_thread_def, argument); } @@ -136,11 +134,9 @@ osThreadId Thread::gettid() { Thread::~Thread() { terminate(); -#ifndef __MBED_CMSIS_RTOS_CA9 if (_dynamic_stack) { delete[] (_thread_def.stack_pointer); } -#endif } } diff --git a/libraries/rtos/rtx/TARGET_CORTEX_A/RTX_CM_lib.h b/libraries/rtos/rtx/TARGET_CORTEX_A/RTX_CM_lib.h index 3251ac9726..3d95aac0cc 100644 --- a/libraries/rtos/rtx/TARGET_CORTEX_A/RTX_CM_lib.h +++ b/libraries/rtos/rtx/TARGET_CORTEX_A/RTX_CM_lib.h @@ -85,16 +85,24 @@ OS_RESULT _os_mut_wait (uint32_t p, OS_ID mutex, uint16_t timeout) __svc_indi #if (OS_TIMERS != 0) #define OS_TASK_CNT (OS_TASKCNT + 1) +#ifndef __MBED_CMSIS_RTOS_CA9 #define OS_PRIV_CNT (OS_PRIVCNT + 2) #define OS_STACK_SZ (4*(OS_PRIVSTKSIZE+OS_MAINSTKSIZE+OS_TIMERSTKSZ)) +#endif #else #define OS_TASK_CNT OS_TASKCNT +#ifndef __MBED_CMSIS_RTOS_CA9 #define OS_PRIV_CNT (OS_PRIVCNT + 1) #define OS_STACK_SZ (4*(OS_PRIVSTKSIZE+OS_MAINSTKSIZE)) #endif +#endif uint16_t const os_maxtaskrun = OS_TASK_CNT; +#ifdef __MBED_CMSIS_RTOS_CA9 +uint32_t const os_stackinfo = (OS_STKCHECK<<24)| (OS_IDLESTKSIZE*4); +#else uint32_t const os_stackinfo = (OS_STKCHECK<<24)| (OS_PRIV_CNT<<16) | (OS_STKSIZE*4); +#endif uint32_t const os_rrobin = (OS_ROBIN << 16) | OS_ROBINTOUT; uint32_t const os_trv = OS_TRV; uint8_t const os_flags = OS_RUNPRIV; @@ -107,6 +115,11 @@ __USED uint32_t const os_timernum = 0; _declare_box (mp_tcb, OS_TCB_SIZE, OS_TASK_CNT); uint16_t const mp_tcb_size = sizeof(mp_tcb); +#ifdef __MBED_CMSIS_RTOS_CA9 +/* Memory pool for os_idle_demon stack allocation. */ +_declare_box8 (mp_stk, OS_IDLESTKSIZE*4, 1); +uint32_t const mp_stk_size = sizeof(mp_stk); +#else /* Memory pool for System stack allocation (+os_idle_demon). */ _declare_box8 (mp_stk, OS_STKSIZE*4, OS_TASK_CNT-OS_PRIV_CNT+1); uint32_t const mp_stk_size = sizeof(mp_stk); @@ -114,6 +127,7 @@ uint32_t const mp_stk_size = sizeof(mp_stk); /* Memory pool for user specified stack allocation (+main, +timer) */ uint64_t os_stack_mem[2+OS_PRIV_CNT+(OS_STACK_SZ/8)]; uint32_t const os_stack_sz = sizeof(os_stack_mem); +#endif #ifndef OS_FIFOSZ #define OS_FIFOSZ 16 @@ -129,7 +143,7 @@ void *os_active_TCB[OS_TASK_CNT]; /* User Timers Resources */ #if (OS_TIMERS != 0) extern void osTimerThread (void const *argument); -#if defined (__MBED_CMSIS_RTOS_CA9) +#ifdef __MBED_CMSIS_RTOS_CA9 osThreadDef(osTimerThread, (osPriority)(OS_TIMERPRIO-3), 4*OS_TIMERSTKSZ); #else osThreadDef(osTimerThread, (osPriority)(OS_TIMERPRIO-3), 1, 4*OS_TIMERSTKSZ); @@ -237,7 +251,12 @@ __attribute__((used)) void _mutex_release (OS_ID *mutex) { /* Main Thread definition */ extern int main (void); +#ifdef __MBED_CMSIS_RTOS_CA9 +uint32_t os_thread_def_stack_main [(4 * OS_MAINSTKSIZE) / sizeof(uint32_t)]; +osThreadDef_t os_thread_def_main = {(os_pthread)main, osPriorityNormal, 1, 4*OS_MAINSTKSIZE, os_thread_def_stack_main }; +#else osThreadDef_t os_thread_def_main = {(os_pthread)main, osPriorityNormal, 1, 4*OS_MAINSTKSIZE }; +#endif #if defined (__CC_ARM) diff --git a/libraries/rtos/rtx/TARGET_CORTEX_A/RTX_Conf_CA.c b/libraries/rtos/rtx/TARGET_CORTEX_A/RTX_Conf_CA.c index debb4bc2d7..78863e8e1e 100644 --- a/libraries/rtos/rtx/TARGET_CORTEX_A/RTX_Conf_CA.c +++ b/libraries/rtos/rtx/TARGET_CORTEX_A/RTX_Conf_CA.c @@ -50,12 +50,20 @@ #define OS_TASKCNT 25 #endif +#ifdef __MBED_CMSIS_RTOS_CA9 +// Idle stack size [bytes] <64-4096:8><#/4> +// Defines default stack size for the Idle thread. +#ifndef OS_IDLESTKSIZE + #define OS_IDLESTKSIZE 128 +#endif +#else // __MBED_CMSIS_RTOS_CA9 // Default Thread stack size [bytes] <64-4096:8><#/4> // Defines default stack size for threads with osThreadDef stacksz = 0 // Default: 200 #ifndef OS_STKSIZE #define OS_STKSIZE 200 #endif +#endif // __MBED_CMSIS_RTOS_CA9 // Main Thread stack size [bytes] <64-4096:8><#/4> // Defines stack size for main thread. @@ -64,19 +72,21 @@ #define OS_MAINSTKSIZE 2048 #endif +#ifndef __MBED_CMSIS_RTOS_CA9 // Number of threads with user-provided stack size <0-250> // Defines the number of threads with user-provided stack size. // Default: 0 #ifndef OS_PRIVCNT - #define OS_PRIVCNT 10 + #define OS_PRIVCNT 0 #endif // Total stack size [bytes] for threads with user-provided stack size <0-4096:8><#/4> // Defines the combined stack size for threads with user-provided stack size. // Default: 0 #ifndef OS_PRIVSTKSIZE - #define OS_PRIVSTKSIZE 8192 + #define OS_PRIVSTKSIZE 0 #endif +#endif // __MBED_CMSIS_RTOS_CA9 // Check for stack overflow // Includes the stack checking code for stack overflow. diff --git a/libraries/rtos/rtx/TARGET_CORTEX_A/cmsis_os.h b/libraries/rtos/rtx/TARGET_CORTEX_A/cmsis_os.h index acb31333dc..2d8ef23d88 100644 --- a/libraries/rtos/rtx/TARGET_CORTEX_A/cmsis_os.h +++ b/libraries/rtos/rtx/TARGET_CORTEX_A/cmsis_os.h @@ -264,6 +264,9 @@ typedef struct os_thread_def { osPriority tpriority; ///< initial thread priority uint32_t instances; ///< maximum number of instances of that thread function uint32_t stacksize; ///< stack size requirements in bytes; 0 is default stack size +#ifdef __MBED_CMSIS_RTOS_CA9 + uint32_t *stack_pointer; ///< pointer to the stack memory block +#endif } osThreadDef_t; /// Timer Definition structure contains timer parameters. @@ -356,10 +359,11 @@ int32_t osKernelRunning(void); #define osThreadDef(name, priority, instances, stacksz) \ extern const osThreadDef_t os_thread_def_##name #else // define the object -#if defined (__MBED_CMSIS_RTOS_CA9) +#ifdef __MBED_CMSIS_RTOS_CA9 #define osThreadDef(name, priority, stacksz) \ +uint32_t os_thread_def_stack_##name [stacksz / sizeof(uint32_t)]; \ const osThreadDef_t os_thread_def_##name = \ -{ (name), (priority), 1, (stacksz) } +{ (name), (priority), 1, (stacksz), (os_thread_def_stack_##name) } #else #define osThreadDef(name, priority, instances, stacksz) \ const osThreadDef_t os_thread_def_##name = \ diff --git a/libraries/rtos/rtx/TARGET_CORTEX_A/rt_CMSIS.c b/libraries/rtos/rtx/TARGET_CORTEX_A/rt_CMSIS.c index 8b7b1fad4e..b50aecc73e 100644 --- a/libraries/rtos/rtx/TARGET_CORTEX_A/rt_CMSIS.c +++ b/libraries/rtos/rtx/TARGET_CORTEX_A/rt_CMSIS.c @@ -442,9 +442,11 @@ extern const uint32_t os_section_id$$Base; extern const uint32_t os_section_id$$Limit; #endif +#ifndef __MBED_CMSIS_RTOS_CA9 // OS Stack Memory for Threads definitions extern uint64_t os_stack_mem[]; extern const uint32_t os_stack_sz; +#endif // OS Timers external resources extern const osThreadDef_t os_thread_def_osTimerThread; @@ -546,6 +548,11 @@ osMessageQId svcMessageCreate (const osMessageQDef_t *queue_def, osThreadId thre /// Initialize the RTOS Kernel for creating objects osStatus svcKernelInitialize (void) { +#ifdef __MBED_CMSIS_RTOS_CA9 + if (!os_initialized) { + rt_sys_init(); // RTX System Initialization + } +#else int ret; if (!os_initialized) { @@ -557,6 +564,7 @@ osStatus svcKernelInitialize (void) { rt_sys_init(); // RTX System Initialization } +#endif os_tsk.run->prio = 255; // Highest priority @@ -668,6 +676,13 @@ osThreadId svcThreadCreate (const osThreadDef_t *thread_def, void *argument) { return NULL; } +#ifdef __MBED_CMSIS_RTOS_CA9 + if (thread_def->stacksize != 0) { // Custom stack size + stk = (void *)thread_def->stack_pointer; + } else { // Default stack size + stk = NULL; + } +#else if (thread_def->stacksize != 0) { // Custom stack size stk = rt_alloc_mem( // Allocate stack os_stack_mem, @@ -680,6 +695,7 @@ osThreadId svcThreadCreate (const osThreadDef_t *thread_def, void *argument) { } else { // Default stack size stk = NULL; } +#endif tsk = rt_tsk_create( // Create task (FUNCP)thread_def->pthread, // Task function pointer @@ -690,9 +706,11 @@ osThreadId svcThreadCreate (const osThreadDef_t *thread_def, void *argument) { ); if (tsk == 0) { // Invalid task ID +#ifndef __MBED_CMSIS_RTOS_CA9 if (stk != NULL) { rt_free_mem(os_stack_mem, stk); // Free allocated stack } +#endif sysThreadError(osErrorNoMemory); // Create task failed (Out of memory) return NULL; } @@ -717,20 +735,26 @@ osThreadId svcThreadGetId (void) { osStatus svcThreadTerminate (osThreadId thread_id) { OS_RESULT res; P_TCB ptcb; +#ifndef __MBED_CMSIS_RTOS_CA9 void *stk; +#endif ptcb = rt_tid2ptcb(thread_id); // Get TCB pointer if (ptcb == NULL) return osErrorParameter; +#ifndef __MBED_CMSIS_RTOS_CA9 stk = ptcb->priv_stack ? ptcb->stack : NULL; // Private stack +#endif res = rt_tsk_delete(ptcb->task_id); // Delete task if (res == OS_R_NOK) return osErrorResource; // Delete task failed +#ifndef __MBED_CMSIS_RTOS_CA9 if (stk != NULL) { rt_free_mem(os_stack_mem, stk); // Free private stack } +#endif return osOK; }