This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

AWR1843: 使用Hook函数来监控任务的创建和删除时出现报错,请指导一下。

Part Number: AWR1843
Other Parts Discussed in Thread: SYSBIOS

/* Standard Include Files. */
#include <stdint.h>
#include <stdlib.h>
#include <stddef.h>
#include <string.h>
#include <stdio.h>

/* BIOS/XDC Include Files. */
#include <xdc/std.h>
#include <xdc/runtime/Error.h>
#include <xdc/runtime/System.h>
#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/knl/Task.h>
#include <ti/sysbios/knl/Semaphore.h>

/* mmWave SDK Include Files: */
#include <ti/drivers/pinmux/pinmux.h>
#include <ti/drivers/uart/UART.h>
#include <ti/utils/testlogger/logger.h>


/**************************************************************************
 *************************** Global Definitions ***************************
 **************************************************************************/

/* Global Variable which holds the CPU Clock Frequency */
uint32_t gCPUClockFrequency = (200 * 1000000);

/* Global Variable which holds the DMA Instance */
DMA_Handle   gDMAHandle;

/* MAX Data Pattern Test Size for the Data Tests: */
#define MAX_TEST_BUFFER_SIZE        128

Int                 myHookSetId, myHookSetId2;   // Hook functions params

/* Hook function
 * */
/* HookSet functions */
/* ======== myRegister ========
 * invoked during Swi module startup before main()
 * for each HookSet */
Void myRegister(Int hookSetId)
{
    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);
    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);
    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) {
        printf("mySwitch: ignoring dummy 1st prev Task\n");
    }
    else {
        prevName = Task_Handle_name(prev);
        pPrevEnv = Task_getHookContext(prev, myHookSetId);
        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);
    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);
    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);
    printf("myDelete: task name = '%s', pEnv = 0x%x\n", name, pEnv);
}

/**
 *  @b Description
 *  @n
 *      Test Task: This sends out the data on the UART
 *
 *  @retval
 *      Not Applicable.
 */
void Test_nonLoopbackMode(UArg arg0, UArg arg1)
{
    UART_Params         params;
    UART_Handle         uartHandle;
    uint32_t            baudRate = 115200;
    int32_t             status;
    char                messageString[MAX_TEST_BUFFER_SIZE];
    uint8_t             rxMessageString[MAX_TEST_BUFFER_SIZE];
    uint8_t             TxMessageString[MAX_TEST_BUFFER_SIZE];
    uint8_t             *TxMessageAdd = &TxMessageString[0];

//    char            delimitter[] = " \r\n";
//    char*           ptrCLICommand;
//    int32_t         numBytes;


    /* Debug Message: */
    printf("*************************************************************************\n");
    printf("Debug: UART NON-Loopback Mode Tests starting [Baud Rate: %d]\n", baudRate);
    printf("Please Ensure that console applications are running on the UART Ports\n");
    printf("*************************************************************************\n");

    /* Setup the default UART Parameters */
    UART_Params_init(&params);
    params.clockFrequency = gCPUClockFrequency;
    params.baudRate       = baudRate;
    params.isPinMuxDone   = 1;
    params.readEcho       = UART_ECHO_OFF;   // 不返回

    /* Open the UART Instance */
    uartHandle = UART_open(0, &params);
    if (uartHandle == NULL)
    {
        printf("Error: Unable to open the UART Instance 0\n");
//        MCPI_setFeatureTestResult ("UART Non Loopback", MCPI_TestResult_FAIL);
        return;
    }

    /****************************************************************************
     * Sanity Check: UART-1 is capable of receiving/transmitting the data
     *  TEST: Blocking Mode
     ****************************************************************************/
    {
        while(1){
        /* Read the response back: */
        memset ((void *)&rxMessageString[0], 0, sizeof(rxMessageString));
//        status = UART_read(uartHandle, (uint8_t*)&rxMessageString[0], strlen(messageString)+1);
        status = UART_read(uartHandle, (uint8_t*)&rxMessageString[0], sizeof(rxMessageString)-1);

        printf ("Debug: Got %d bytes '%s'\n", status, rxMessageString);
        TxMessageAdd = &rxMessageString[0];

        UART_writePolling(uartHandle, TxMessageAdd, 5);

        printf ("Debug: End write ");
        }


    }

    /* Clean up:*/
    UART_close(uartHandle);

    /* Exit BIOS: All the tests are completed */
    BIOS_exit(0);
    return;
}

/**
 *  @b Description
 *  @n
 *      System Initialization Task which initializes the various
 *      components in the system.
 *
 *  @retval
 *      Not Applicable.
 */
static void Test_initTask(UArg arg0, UArg arg1)
{
    Task_Params         taskParams;
    DMA_Params          dmaParams;
    int32_t             errCode;

    /* Initialize the UART */
    UART_init();

    /* Initialize the DMA */
    DMA_init ();

    /* Open the DMA Instance */
    DMA_Params_init(&dmaParams);
    gDMAHandle = DMA_open(0, &dmaParams, &errCode);
    if (gDMAHandle == NULL)
    {
        printf ("Error: Unable to open the DMA Instance [Error code %d]\n", errCode);
        return;
    }

    /* Setup the PINMUX to bring out the MSS UART-1 */
    Pinmux_Set_FuncSel(SOC_XWR18XX_PINN5_PADBE, SOC_XWR18XX_PINN5_PADBE_MSS_UARTA_TX);
    Pinmux_Set_OverrideCtrl(SOC_XWR18XX_PINN5_PADBE, PINMUX_OUTEN_RETAIN_HW_CTRL, PINMUX_INPEN_RETAIN_HW_CTRL);
    Pinmux_Set_FuncSel(SOC_XWR18XX_PINN4_PADBD, SOC_XWR18XX_PINN4_PADBD_MSS_UARTA_RX);
    Pinmux_Set_OverrideCtrl(SOC_XWR18XX_PINN4_PADBD, PINMUX_OUTEN_RETAIN_HW_CTRL, PINMUX_INPEN_RETAIN_HW_CTRL);

    /* Setup the PINMUX to bring out the MSS UART-3 */
    Pinmux_Set_OverrideCtrl(SOC_XWR18XX_PINF14_PADAJ, PINMUX_OUTEN_RETAIN_HW_CTRL, PINMUX_INPEN_RETAIN_HW_CTRL);
    Pinmux_Set_FuncSel(SOC_XWR18XX_PINF14_PADAJ, SOC_XWR18XX_PINF14_PADAJ_MSS_UARTB_TX);

    /* Launch the NON-Loopback Tests */
    Task_Params_init(&taskParams);
    taskParams.stackSize = 4*1024;
    taskParams.priority  = 1;
    taskParams.instance->name = "Test_nonLoopbackMode_task";
    Task_create(Test_nonLoopbackMode, &taskParams, NULL);

    return;
}

/*
 *  ======== main ========
 */
Int main()
{ 
    Task_Handle     task;
    Error_Block     eb;
    Task_Params     taskParams;

    printf("enter main()\n");

    Error_init(&eb);
    Task_Params_init(&taskParams);
    taskParams.instance->name = "Test_initTask";
    task = Task_create(Test_initTask, &taskParams, &eb);
    if (task == NULL) {
        printf("Test_initTask() failed!\n");
        BIOS_exit(0);
    }

    BIOS_start();    /* does not return */
    return(0);
}

以上是写的测试代码,目的是为了测试串口的收发任务,其中串口读函数使用的UART_read,如果按照代码所描述的添加了一系列Hook函数进行Task的监控就会出现下列报错信息:

“ti.sysbios.gates.GateMutex: line 99: assertion failure: A_badContext: bad calling context. See GateMutex API doc for details. xdc.runtime.Error.raise: terminating execution”;

测试发现就是在执行UART_read时出现的这个Bug,如果我将Hook函数给注释掉不再执行,那么整个代码就可以正常运行不报错。
此外还发现如果将UART_read改成UART_readPolling,那么在使用Hook函数的情况下代码也可以正常运行。

那么请问在使用UART_read且应用Hook函数时,那个报错是如何产生的?又该怎么解决这个问题呢?