金牌会员
 
- 积分
- 1424
- 金钱
- 1424
- 注册时间
- 2011-11-27
- 在线时间
- 122 小时
|
发表于 2016-9-26 19:58:12
|
显示全部楼层
本帖最后由 科科1987 于 2016-9-29 09:47 编辑
如果自己定义了任务堆栈大小,创建任务时要从os_stack_mem里分配任务堆栈大小,见下:
/// Create a thread and add it to Active Threads and set it to state READY
osThreadId svcThreadCreate (const osThreadDef_t *thread_def, void *argument) {
P_TCB ptcb;
OS_TID tsk;
void *stk;
if ((thread_def == NULL) ||
(thread_def->pthread == NULL) ||
(thread_def->tpriority < osPriorityIdle) ||
(thread_def->tpriority > osPriorityRealtime)) {
sysThreadError(osErrorParameter);
return NULL;
}
if (thread_def->stacksize != 0) { // Custom stack size
stk = rt_alloc_mem( // Allocate stack
os_stack_mem,
thread_def->stacksize
);
if (stk == NULL) {
sysThreadError(osErrorNoMemory); // Out of memory
return NULL;
}
} else { // Default stack size
stk = NULL;
}
tsk = rt_tsk_create( // Create task
(FUNCP)thread_def->pthread, // Task function pointer
(thread_def->tpriority-osPriorityIdle+1) | // Task priority
(thread_def->stacksize << 8), // Task stack size in bytes
stk, // Pointer to task's stack
argument // Argument to the task
);
if (tsk == 0) { // Invalid task ID
if (stk != NULL) {
rt_free_mem(os_stack_mem, stk); // Free allocated stack
}
sysThreadError(osErrorNoMemory); // Create task failed (Out of memory)
return NULL;
}
ptcb = (P_TCB)os_active_TCB[tsk - 1]; // TCB pointer
*((uint32_t *)ptcb->tsk_stack + 13) = (uint32_t)osThreadExit;
return ptcb;
}
但是如果os_stack_mem的小于分配的堆栈大小的话,创建任务就会失败。
/* Memory pool for user specified stack allocation (+main, +timer) */
uint64_t os_stack_mem[2+OS_PRIV_CNT+(OS_STACK_SZ/8)];
在RTX_Conf_CM.c文件里:
// <o>Total stack size [bytes] for threads with user-provided stack size <0-1048576:8><#/4>
// <i> Defines the combined stack size for threads with user-provided stack size.
// <i> Default: 0
#ifndef OS_PRIVSTKSIZE
#define OS_PRIVSTKSIZE 2 // this stack size value is in words
#endif
把这个值改大一些,比如改成50(按照4个字节算的,200bytes)
总之,自己配置任务堆栈大小 ,根据实际情况配置:
// <o>Number of threads with user-provided stack size <0-250>
// <i> Defines the number of threads with user-provided stack size.
// <i> Default: 0
#ifndef OS_PRIVCNT
#define OS_PRIVCNT 1
#endif
// <o>Total stack size [bytes] for threads with user-provided stack size <0-1048576:8><#/4>
// <i> Defines the combined stack size for threads with user-provided stack size.
// <i> Default: 0
#ifndef OS_PRIVSTKSIZE
#define OS_PRIVSTKSIZE 50
#endif
|
|