1. 程式人生 > >基於TI-RTOS的CC2650DK開發(9)---任務示例

基於TI-RTOS的CC2650DK開發(9)---任務示例

/* ======== 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);
}
/* ======== myDelete ========
* invoked upon Task deletion */
Void myDelete(Task_Handle task)
{
    String name;
    Ptr pEnv;
    name = Task_Handle_name(task);
    pEnv = Task_getHookContext(task, myHookSetId);
    System_printf("myDelete: task name = '%s', pEnv = 0x%x\n", name, pEnv);
}
/* Define 3 identical tasks */
Void myTsk0Func(UArg arg0, UArg arg1)
{
    System_printf("myTsk0 Entering\n");
    System_printf("myTsk0 Calling Task_yield\n");
    Task_yield();
    System_printf("myTsk0 Exiting\n");
}
Void myTsk1Func(UArg arg0, UArg arg1)
{
    System_printf("myTsk1 Entering\n");
    System_printf("myTsk1 Calling Task_yield\n");
    Task_yield();
    System_printf("myTsk1 Exiting\n");
}
Void myTsk2Func(UArg arg0, UArg arg1)
{
    System_printf("myTsk2 Entering\n");
    System_printf("myTsk2 Calling Task_yield\n");
    Task_yield();
    System_printf("myTsk2 Exiting\n");
}
/* ======== main ======== */
Int main(Int argc, Char *argv[])
{
    Task_Params params;
    Error_Block eb;
    Error_init(&eb);
    Task_Params_init(?ms);
    params.instance->name = "myTsk0";
    myTsk0 = Task_create(myTsk0Func, ?ms, &eb);
    if (myTsk0 == NULL)
    {
        System_abort("myTsk0 create failed");
    }
    params.instance->name = "myTsk1";
    myTsk1 = Task_create(myTsk1Func, ?ms, &eb);
    if (myTsk1 == NULL)
    {
        System_abort("myTsk1 create failed");
    }
    params.instance->name = "myTsk2";
    myTsk2 = Task_create(myTsk2Func, ?ms, &eb);
    if (myTsk2 == NULL)
    {
        System_abort("myTsk2 create failed");
    }
    BIOS_start();
    return (0);
}
/* ======== myIdleFunc ======== */
Void myIdleFunc()
{
    System_printf("Entering idleFunc().\n");
    Task_delete(&myTsk0);
    Task_delete(&myTsk1);
    Task_delete(&myTsk2);
    System_exit(0);
}
接下來是配置cfg程式碼