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.

CCS/SW-EK-TM4C1294XL: Error and Exception Hook

Part Number: SW-EK-TM4C1294XL

Tool/software: Code Composer Studio

Hello,

i am trying to print out information via uart when an error/exception occurs. My TI-RTOS cfg File looks like this:

m3Hwi.enableException = true;

m3Hwi.excHookFunc = "&exceptionHook";

Error.policy = Error.TERMINATE;

Error.policyFxn = Error.policyDefault;

Error.raiseHook = "&errorHook";

Error.maxDepth = 1;

My code looks like this:

void exceptionHook(Hwi_ExcContext *ex)
{
    int i;
    char buffer[256] = "Expetion occured!\n\r";

    reInitUart();

    for (i = 0; i < strlen(buffer); i++)
        UARTCharPut(DEBUGUART, buffer[i]);

    int length = snprintf(buffer, sizeof(buffer), "ThreadType=%s ThreadHandle=%#x threadStack=%#x stackSize=%d\n\r",
             threadTypeToString(ex->threadType), ex->threadHandle, ex->threadStack, ex->threadStackSize);

    for (i = 0; i < length; i++)
        UARTCharPut(DEBUGUART, buffer[i]);

    length = snprintf(buffer, sizeof(buffer), "r0=%#x r1=%#x r2=%#x r3=%#x r4=%#x r5=%#x r6=%#x r7=%#x r8=%#x r9=%#x r10=%#x r11=%#x r12=%#x\n\r",
             ex->r0, ex->r1, ex->r2, ex->r3, ex->r4, ex->r5, ex->r6, ex->r7, ex->r8, ex->r9, ex->r10, ex->r11, ex->r12);

    for (i = 0; i < length; i++)
        UARTCharPut(DEBUGUART, buffer[i]);

    length = snprintf(buffer, sizeof(buffer), "sp=%#x lr=%#x pc=%#x psr=%#x ICSR=%#x MMFSR=%#x BFSR=%#x UFSR=%#x HFSR=%#x DFSR=%#x MMAR=%#x BFAR=%#x AFSR=%#x\n\r",
             ex->sp, ex->lr, ex->pc, ex->psr, ex->ICSR, ex->MMFSR, ex->BFSR, ex->UFSR, ex->HFSR, ex->DFSR, ex->MMAR, ex->BFAR, ex->AFSR);

    for (i = 0; i < length; i++)
        UARTCharPut(DEBUGUART, buffer[i]);
}

void errorHook(Error_Block *eb)
{
    int i;
    char buffer[256];

    reInitUart();

    const int length = snprintf(buffer, sizeof(buffer), Error_getMsg(eb), Error_getCode(eb));

    UARTCharPut(DEBUGUART, '\n');
    UARTCharPut(DEBUGUART, '\r');

    for (i = 0; i < length; i++)
    {
        UARTCharPut(DEBUGUART, buffer[i]);
    }
}

When an exception occurs "excetionHook" is not called but when i overwrite the exceptionHandler it it called. Sadly the parameter "Hwi_ExcContext *ex" ex is invalid.

Why is the parameter of "void exceptionHook(Hwi_ExcContext *ex)" invalid? What am i doing wrong?

Thanks in advance!

Best Regards,

AkosexceptionErrorHook.zip

  • Akos Balassa said:
    Why is the parameter of "void exceptionHook(Hwi_ExcContext *ex)" invalid?

    Your original .cfg file was setting the excHookFunc as NULL, and excHandlerFunc as your exceptionHook function:

    m3Hwi.excHookFunc = null;
    m3Hwi.excHandlerFunc = "&exceptionHook";

    The signature for the excHandlerFunc function prototype is:

    Void myExceptionHandler(UInt *excStack, UInt lr);

    Where 'excStack' is the address of the stack containing the register context at the time of the exception, and 'lr' is the link register value when the low-level-assembly-coded exception handler was vectored to.

    Whereas the signature for the excHookFunc function prototype is:

    typedef Void (*Hwi_ExceptionHookFuncPtr)(Hwi_ExcContext*);

    The excHandlerFunc is called first, and needs to decode the stack and 'lr' to populate the Hwi_ExcContext structure. I.e. you need to leave excHandlerFunc as the function provided by TI-RTOS and instead in the .cfg file:

    m3Hwi.excHookFunc = "&exceptionHook";

    Also, the following should be removed from the .cfg file as it causes the program to be terminated before the application supplied excHookFunc is called:

    Error.policy = Error.TERMINATE;

    I have tried the attached updated example, and it now displays the following on the UART output:

    test
    test
    test
    
    E_hardFault: 
    E_usageFault: Expetion occured!
    ThreadType=task ThreadHandle=0x200017bc threadStack=0x20000360 stackSize=2048
    r0=0x3 r1=0x0 r2=0x0 r3=0x40064000 r4=0xffffffff r5=0xffffffff r6=0xffffffff r7=0xffffffff r8=0xffffffff r9=0xffffffff r10=0xffffffff r11=0xffffffff r12=0x20001b10
    sp=0x20000b38 lr=0x529d pc=0x6e1c psr=0x61000000 ICSR=0x803 MMFSR=0x0 BFSR=0x0 UFSR=0x1 HFSR=0x40000000 DFSR=0xb MMAR=0xe000ed34 BFAR=0xe000ed38 AFSR=0x0

    Which matches the exception information displayed by ROV:

    4010.empty_EK_TM4C1294XL_TI.zip

  • Akos,

    The issue is that you are setting Error.policy = Error.TERMINATE. 

    I had the exception hook be your function (m3Hwi.excHookFunc = "&exceptionHook") and removed the Error.policy setting from the .cfg file. I had the invalid asm statement get hit then. I got output from the UART then (via your hook function).

    During the exception handling the Error_raise function is called. Since you had TERMINATE as the setting, the program terminated before your hook function was called. With the default (UNWIND), the error has processed and the program continue...thus hitting your hook callback.

    Todd

  • Hi Todd,

    thank you very much. The exception hook now works as intented. But the error hook is not able to print the error message. Do you know why? What am i doing wrong with the error hook?

    Best regards,

    Akos

  • Akos,

    Is the error hook getting called? What is the state of the system when this occurs? For example, during an exception, interrupts are going to be disabled.

    Todd

  • Hi Todd,

    yes the error hook is called. The error msg is not working. If i raise an error with

    Error_Block eb;
    Error_init(&eb);
    eb.msg = "error msg\n";
    eb.id = 1337;
    Error_raise(&eb, Error_E_generic, 1337, 1338);

    I can't get the proper error message with "Error_getMsg(eb)" in the error hook when i raise an error myself. If i cause an out of memory error with "Memory_alloc(NULL, 2048, 0, &eb);" i'm getting the proper error messge "out of memory: handle=0x0, size=536873748".

    The error block message is just a "xdc_String" and an xdc_String is just a "typedef char            *xdc_String;    /* null terminated string */". I am not understanding what i'm doing wrong.

    Best regards,

    Akos

  • Hi Akos,

    The parameter after the Error_E_generic needs to be a format string. For example

    Error_raise(&eb, Error_E_generic, "Error occurred, code: %d", 1338);

    Generates the following output

    Error occurred, code: 1338

    Todd

  • Hi Todd,

    thank you for the reply.

    If i raise an error with

    Error_raise(&eb, Error_E_generic, "Error occurred, code: %d", 1338);


    i get only get this "%$S" by looking at "eb->msg" or calling "Error_getMsg(eb)".

    So my errorHook is wrong?

     

    void errorHook(Error_Block *eb)
    {
        int i;
        char buffer[256];
    
        reInitUart();
    
    //    const int length = snprintf(buffer, sizeof(buffer), eb->msg, Error_getCode(eb));
        const int length = snprintf(buffer, sizeof(buffer), Error_getMsg(eb), Error_getCode(eb));
    
        UARTCharPut(DEBUGUART, '\n');
        UARTCharPut(DEBUGUART, '\r');
    
        for (i = 0; i < length; i++)
        {
            UARTCharPut(DEBUGUART, buffer[i]);
        }
    }

     

  • I have to dereference arg0 as a character pointer and now it works.

    void errorHook(Error_Block *eb)
    {
        int i;
        char buffer[256];
        char foobar[64];
    
        reInitUart();
    
        const xdc_runtime_Types_Site site = *Error_getSite(eb);
    
        snprintf(foobar, sizeof(foobar), (char*)eb->data.arg[0], (int*)eb->data.arg[1]);
        const int length = snprintf(buffer, sizeof(buffer), "%s file=%s line=%d", foobar, site.file, site.line);
    
        UARTCharPut(DEBUGUART, '\n');
        UARTCharPut(DEBUGUART, '\r');
    
        for (i = 0; i < length; i++)
        {
            UARTCharPut(DEBUGUART, buffer[i]);
        }
    }