共两个例子,实在太长,所以开新日志来写。先来书上的第一个例子,这回按照书上的做又有问题,好在花了点时间还是找到问题所在。以下代码至少在CC2650DK上是可以运行的。
3.6.5.6 Task Hooks示例
下例使用了单个Task hook集。此例演示了如何读写每个hook集关联的Hook上下文指针。
首先是C代码:
/* ======== TaskHookExample.c ========
* This example demonstrates basic task hook processing
* operation for dynamically created tasks. */
#include <xdc/std.h>
#include <xdc/runtime/Error.h>
#include <xdc/runtime/Memory.h>
#include <xdc/runtime/System.h>
#include <xdc/runtime/Types.h>
#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/knl/Task.h>
Task_Handle myTsk0, myTsk1, myTsk2;
Int myHookSetId, myHookSetId2;
/* HookSet functions */
/* ======== myRegister ========
* invoked during Swi module startup before main()
* for each HookSet */
Void myRegister(Int hookSetId)
{
System_printf("myRegister: assigned HookSet Id = %d\n", hookSetId);
myHookSetId = hookSetId;
}
/* ======== myCreate ========
* invoked during Task_create for dynamically
* created Tasks */
Void myCreate(Task_Handle task, Error_Block *eb)
{
String name;
Ptr pEnv;
name = Task_Handle_name(task);
pEnv = Task_getHookContext(task, myHookSetId);
System_printf("myCreate: task name = '%s', pEnv = 0x%x\n", name, pEnv);
Task_setHookContext(task, myHookSetId, (Ptr)0xdead);
}
/* ======== myReady ========
* invoked when Task is made ready to run */
Void myReady(Task_Handle task)
{
String name;
Ptr pEnv;
name = Task_Handle_name(task);
pEnv = Task_getHookContext(task, myHookSetId);
System_printf("myReady: task name = '%s', pEnv = 0x%x\n", name, pEnv);
Task_setHookContext(task, myHookSetId, (Ptr)0xc0de);
}
/* ======== mySwitch ========
* invoked whenever a Task switch occurs/is made ready to run */
Void mySwitch(Task_Handle prev, Task_Handle next)
{
String prevName;
String nextName;
Ptr pPrevEnv;
Ptr pNextEnv;
if (prev == NULL)
{
System_printf("mySwitch: ignoring dummy 1st prev Task\n");
}
else
{
prevName = Task_Handle_name(prev);
pPrevEnv = Task_getHookContext(prev, myHookSetId);
System_printf("mySwitch: prev name = '%s', pPrevEnv = 0x%x\n",
prevName, pPrevEnv);
Task_setHookContext(prev, myHookSetId, (Ptr)0xcafec0de);
}
nextName = Task_Handle_name(next);
pNextEnv = Task_getHookContext(next, myHookSetId);
System_printf(" next name = '%s', pNextEnv = 0x%x\n",
nextName, pNextEnv);
Task_setHookContext(next, myHookSetId, (Ptr)0xc001c0de);
}
/* ======== myExit ========
* invoked whenever a Task calls Task_exit() or falls through
* the bottom of its task function. */
Void myExit(Task_Handle task)
{
Task_Handle curTask = task;
String name;
Ptr pEnv;
name = Task_Handle_name(curTask);
pEnv = Task_getHookContext(curTask, myHookSetId);
System_printf("myExit: curTask name = '%s', pEnv = 0x%x\n", name, pEnv);
Task_setHookContext(curTask, myHookSetId, (Ptr)0xdeadbeef);
}
/*