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.

Compiler/EK-TM4C1294XL: Review the code

Part Number: EK-TM4C1294XL

Tool/software: TI C/C++ Compiler

Please review my code and check whether it is correct or not? I want to use it for CAN transmission. Also, is it possible to blink the led at the same time when the CAN messages is transmitting?

#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"

#ifdef DEBUG
void
__error__(char *pcFilename, uint32_t ui32Line)
{
    while(1);
}
#endif



volatile uint32_t g_ui32MsgCount = 0;
volatile bool g_bErrFlag = 0;
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
SimpleDelay(void)
{
   
    SysCtlDelay(16000000 / 3);
}
void
CANIntHandler(void)
{
    uint32_t ui32Status;
ui32Status = CANIntStatus(CAN0_BASE, CAN_INT_STS_CAUSE);
if(ui32Status == CAN_INT_INTID_STATUS)
    {
ui32Status = CANStatusGet(CAN0_BASE, CAN_STS_CONTROL);
 g_bErrFlag = 1;
    }
    else if(ui32Status == 1)
    {
    CANIntClear(CAN0_BASE, 1);
    g_ui32MsgCount++;
    g_bErrFlag = 0;
    }
    else
    {
    }
}

int
main(void)
{
#if defined(TARGET_IS_TM4C129_RA0) ||                                         \
    defined(TARGET_IS_TM4C129_RA1) ||                                         \
    defined(TARGET_IS_TM4C129_RA2)
    uint32_t ui32SysClock;
#endif

    tCANMsgObject sCANMessage;
    uint32_t ui32MsgData;
    uint8_t *pui8MsgData;

    pui8MsgData = (uint8_t *)&ui32MsgData;
#if defined(TARGET_IS_TM4C129_RA0) ||       
\
    defined(TARGET_IS_TM4C129_RA1) ||                                         \
    defined(TARGET_IS_TM4C129_RA2)
    ui32SysClock = SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ |
                                       SYSCTL_OSC_MAIN |
                                       SYSCTL_USE_OSC)
                                       25000000);
#else
    SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
                   SYSCTL_XTAL_16MHZ);
#endif
InitConsole();
 SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
GPIOPinConfigure(GPIO_PB4_CAN0RX);
    GPIOPinConfigure(GPIO_PB5_CAN0TX);
GPIOPinTypeCAN(GPIO_PORTB_BASE, GPIO_PIN_4 | GPIO_PIN_5);
SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0);
 CANInit(CAN0_BASE);
 #if defined(TARGET_IS_TM4C129_RA0) ||                                         \
    defined(TARGET_IS_TM4C129_RA1) ||                                         \
    defined(TARGET_IS_TM4C129_RA2)
    CANBitRateSet(CAN0_BASE, ui32SysClock, 500000);
#else
    CANBitRateSet(CAN0_BASE, SysCtlClockGet(), 500000);
#endif
    CANIntEnable(CAN0_BASE, CAN_INT_MASTER | CAN_INT_ERROR | CAN_INT_STATUS);

    IntEnable(INT_CAN0);

    CANEnable(CAN0_BASE);


    ui32MsgData = 0;
    sCANMessage.ui32MsgID = 1;
    sCANMessage.ui32MsgIDMask = 0;
    sCANMessage.ui32Flags = MSG_OBJ_TX_INT_ENABLE;
    sCANMessage.ui32MsgLen = sizeof(pui8MsgData);
    sCANMessage.pui8MsgData = pui8MsgData;

    while(1)
    {
        
        UARTprintf("Sending msg: 0x%02X %02X %02X %02X",
                   pui8MsgData[0], pui8MsgData[1], pui8MsgData[2],
                   pui8MsgData[3]);
        CANMessageSet(CAN0_BASE, 1, &sCANMessage, MSG_OBJ_TYPE_TX);
        SimpleDelay();
        if(g_bErrFlag)
        {
            UARTprintf(" error - cable connected?\n");
        }
        else
        {
            UARTprintf(" total count = %u\n", g_ui32MsgCount);
        }

        ui32MsgData++;
    }
     return(0);
}


  • Hello Kumar,

    Kumar Anubhav said:
    Please review my code and check whether it is correct or not?

    We do not do full code reviews on E2E. If you are having an issue with the software written, then describe the issue you are facing and we can focus on the portions of your code pertaining to the issue.

    Kumar Anubhav said:

    Also, is it possible to blink the led at the same time when the CAN messages is transmitting?

    From a human perspective, yes. But the instructions wouldn't actually be executing at the same time. You would definitely be able to perceive an LED blinking in between CAN transmissions though due to the low amount of time for the CAN communication relative to what a human eye can see.