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.

EK-TM4C1294XL: Issue with CAN bus transmitter code

Part Number: EK-TM4C1294XL

I am compiling following code for the CAN bus transmitter side:

#include <stdbool.h>
#include <stdint.h>
#include "inc/hw_can.h"
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "driverlib/can.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/pin_map.h"
#include "driverlib/sysctl.h"
#include "driverlib/uart.h"
#include "utils/uartstdio.h"
#include "utils/uartstdio.c"

volatile uint32_t g_ui32IntCount = 0;
volatile uint32_t g_ui32Msg1Count = 0;
volatile uint32_t g_ui32Msg2Count = 0;
volatile uint32_t g_ui32Msg3Count = 0;
volatile bool g_bMsgObj3Sent = 0;
volatile bool g_bErrFlag = 0;

tCANMsgObject g_sCANMsgObject1;
tCANMsgObject g_sCANMsgObject2;
tCANMsgObject g_sCANMsgObject3;

uint8_t g_pui8Msg1[4] = { 0, 0, 0, 0 };
uint8_t g_pui8Msg2[5] = { 2, 2, 2, 2, 2 };
uint8_t g_pui8Msg3[6] = { 3, 3, 3, 3, 3, 3 };
uint8_t g_pui8Msg4[8] = { 4, 4, 4, 4, 5, 5, 5, 5 };

void InitConsole(void)
{
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    GPIOPinConfigure(GPIO_PA0_U0RX);
    GPIOPinConfigure(GPIO_PA1_U0TX);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
    UARTClockSourceSet(UART0_BASE, UART_CLOCK_PIOSC);
    GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
    UARTStdioConfig(0, 115200, 16000000);
}

void PrintCANMessageInfo(tCANMsgObject *psCANMsg, uint32_t ui32MsgObj)
{
    unsigned int uIdx;

    UARTprintf("Sending msg: obj=%d ID=0x%04X msg=0x", ui32MsgObj,
               psCANMsg->ui32MsgID);
    for(uIdx = 0; uIdx < psCANMsg->ui32MsgLen; uIdx++)
    {
        UARTprintf("%02X ", psCANMsg->pui8MsgData[uIdx]);
    }
    UARTprintf("\n");
}

void SimpleDelay(void)
{
    SysCtlDelay(16000000 / 3);
}

void CANIntHandler(void)
{
    uint32_t ui32Status;

    ui32Status = CANIntStatus(CAN1_BASE, CAN_INT_STS_CAUSE);

    if(ui32Status == CAN_INT_INTID_STATUS)
    {
        ui32Status = CANStatusGet(CAN1_BASE, CAN_STS_CONTROL);
        g_bErrFlag = 1;
    }

    else if(ui32Status == 1)
    {
        CANIntClear(CAN1_BASE, 1);
        g_ui32Msg1Count++;
        g_bErrFlag = 0;
    }

    else if(ui32Status == 2)
    {
        CANIntClear(CAN1_BASE, 2);
        g_ui32Msg2Count++;
        g_bErrFlag = 0;
    }

    else if(ui32Status == 3)
    {
        CANIntClear(CAN1_BASE, 3);
        g_ui32Msg3Count++;
        g_bMsgObj3Sent = 1;
        g_bErrFlag = 0;
    }

    else
    {
    }
}

int main(void)
{
    uint32_t ui32SysClock;

    ui32SysClock = SysCtlClockFreqSet((SYSCTL_XTAL_16MHZ |
                                                 SYSCTL_OSC_INT |
                                                 SYSCTL_USE_PLL |
                                                 SYSCTL_CFG_VCO_480), 120000000);
    InitConsole();

    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
    GPIOPinConfigure(GPIO_PB0_CAN1RX);
    GPIOPinConfigure(GPIO_PB1_CAN1TX);
    GPIOPinTypeCAN(GPIO_PORTB_BASE, GPIO_PIN_0 | GPIO_PIN_1);
    SysCtlPeripheralDisable(SYSCTL_PERIPH_CAN1);
    SysCtlPeripheralReset(SYSCTL_PERIPH_CAN1);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN1);
    CANInit(CAN1_BASE);
    CANBitRateSet(CAN1_BASE, ui32SysClock, 500000);
    CANIntRegister(CAN1_BASE, CANIntHandler);
    CANIntEnable(CAN1_BASE, CAN_INT_MASTER | CAN_INT_ERROR | CAN_INT_STATUS);
    IntEnable(INT_CAN1);
    CANEnable(CAN1_BASE);

    g_sCANMsgObject1.ui32MsgID = 0x1001;
    g_sCANMsgObject1.ui32MsgIDMask = 0;
    g_sCANMsgObject1.ui32Flags = MSG_OBJ_TX_INT_ENABLE;
    g_sCANMsgObject1.ui32MsgLen = sizeof(g_pui8Msg1);
    g_sCANMsgObject1.pui8MsgData = g_pui8Msg1;

    g_sCANMsgObject2.ui32MsgID = 0x2001;
    g_sCANMsgObject2.ui32MsgIDMask = 0;
    g_sCANMsgObject2.ui32Flags = MSG_OBJ_TX_INT_ENABLE;
    g_sCANMsgObject2.ui32MsgLen = sizeof(g_pui8Msg2);
    g_sCANMsgObject2.pui8MsgData = g_pui8Msg2;

    for(;;)
    {
        PrintCANMessageInfo(&g_sCANMsgObject1, 1);
        CANMessageSet(CAN1_BASE, 1, &g_sCANMsgObject1, MSG_OBJ_TYPE_TX);

        PrintCANMessageInfo(&g_sCANMsgObject2, 2);
        CANMessageSet(CAN1_BASE, 2, &g_sCANMsgObject2, MSG_OBJ_TYPE_TX);

        g_sCANMsgObject3.ui32MsgID = 0x3001;
        g_sCANMsgObject3.ui32MsgIDMask = 0;
        g_sCANMsgObject3.ui32Flags = MSG_OBJ_TX_INT_ENABLE;
        g_sCANMsgObject3.ui32MsgLen = sizeof(g_pui8Msg3);
        g_sCANMsgObject3.pui8MsgData = g_pui8Msg3;

        g_bMsgObj3Sent = 0;

        PrintCANMessageInfo(&g_sCANMsgObject3, 3);
        CANMessageSet(CAN1_BASE, 3, &g_sCANMsgObject3, MSG_OBJ_TYPE_TX);

        while(!g_bMsgObj3Sent)
        {
        }

        g_sCANMsgObject3.ui32MsgID = 0x3002;
        g_sCANMsgObject3.ui32MsgIDMask = 0;
        g_sCANMsgObject3.ui32Flags = MSG_OBJ_TX_INT_ENABLE;
        g_sCANMsgObject3.ui32MsgLen = sizeof(g_pui8Msg4);
        g_sCANMsgObject3.pui8MsgData = g_pui8Msg4;

        PrintCANMessageInfo(&g_sCANMsgObject3, 3);
        CANMessageSet(CAN1_BASE, 3, &g_sCANMsgObject3, MSG_OBJ_TYPE_TX);

        SimpleDelay();

        if(g_bErrFlag)
        {
            UARTprintf(" error - cable connected?\n");
        }
        else
        {
            UARTprintf(" total count = %u\n",
                       g_ui32Msg1Count + g_ui32Msg2Count + g_ui32Msg3Count);
        }

        (*(uint32_t *)g_pui8Msg1)++;
        (*(uint32_t *)g_pui8Msg2)++;
        (*(uint32_t *)g_pui8Msg3)++;
        (*(uint32_t *)&g_pui8Msg4[0])++;
        (*(uint32_t *)&g_pui8Msg4[4])--;

        UARTprintf("\n\n");
    }


}

I noticed these two issues:

(1) These parameters are not getting incremented: g_ui32Msg1Count, g_ui32Msg2Count, g_ui32Msg3Count 

(2) The program gets stuck before the while(!g_bMsgObj3Sent). But if I remove this loop messages are being sent but still the parameters g_ui32Msg1Count, g_ui32Msg2Count, g_ui32Msg3Count are not getting updated

Please help me to solve this issue

  • Per poster Robert's request/direction - you've presented your code in the proper (highly legible) fashion - good that.

    Your listed "issue parameters" are all (potentially) updated (only) w/in your CAN Interface Handler.      

    Thus shouldn't you:

    • verify that your CAN Handler was entered?
    • if that proves true - does "ui32Status" (ever) equal "1, 2, or 3?"    (these values required for the desired increment to occur)

    You note that, "The program gets stuck before the while(!g_bMsgObj3Sent)" - yet you do not tell us the exact location at which this "getting stuck" occurs - & if this location proves constant.

  • Thanks for using paste code, it does help.

    First comment, Hungarian notation isn't doing you any favours. It took me a while to find the 'parameters' you were referring to, especially since they were not parameters but global variables.

    Second comment, you don't appear to be handling the case of multiple interrupts occurring more or less simultaneously.

    Third how do you tell the counts are not getting updated?

    Robert

  • Yes indeed - "How does poster extract such parameter/variable info?"

    Continued is the belief that "monitoring" variable, "ui32Status" makes great sense...