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/LAUNCHXL-F28377S: CAN configuration for TX mod (recive the frame by processor)

Part Number: LAUNCHXL-F28377S

Tool/software: TI C/C++ Compiler

Hi,

I spent two days to configure the CAN in RX mode. (TX mode works properly)

Bellow is my code base on examples (F2837xS v210).

What I am doing wrong, 

I wil waiting for your answer

/*
* v1_main.c
*
* Created on: 21 lis 2017
* Author: Dariusz
*/

//###########################################################################
//
// FILE: can_moje_transmit.c
//
//! - CANA is on GPIO71 (CANTXA) and GPIO70 (CANRXA)
//
//!
//! \b Watch \b Variables \n
//! - TXCOUNT - Adjust to set the number of messages to be transmitted
//! - txMsgCount - A counter for the number of messages sent
//! - rxMsgCount - A counter for the number of messages received
//! - txMsgData - An array with the data being sent
//! - rxMsgData - An array with the data that was received
//! - errorFlag - A flag that indicates an error has occurred
//###########################################################################
//###########################################################################
//
// Included Files
//
#include "F28x_Project.h" // Device Headerfile and Examples Include File
#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "inc/hw_can.h"
#include "driverlib/can.h"

//RX
unsigned char rxMsgData_103h[4];

tCANMsgObject sRXCANMessage_103h;

void main(void)
{
//
// Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
//
InitSysCtrl();
//
// Initialize GPIO and configure GPIO pins for CANTX/CANRX
//
InitGpio();
//
// Setup GPIO pin mux for CAN-A TX/RX
//
GPIO_SetupPinMux(70, GPIO_MUX_CPU1, 5); //GPIO70 - CANRXA
GPIO_SetupPinOptions(70, GPIO_INPUT, GPIO_ASYNC);
GPIO_SetupPinMux(71, GPIO_MUX_CPU1, 5); //GPIO71 - CANTXA
GPIO_SetupPinOptions(71, GPIO_OUTPUT, GPIO_PUSHPULL);
//

// Initialize the CAN controllers
// CANInit(based adress of CAN)
CANInit(CANA_BASE);
//
//CANBitTimingSet(adress of CAN, points to the structure with the clock parameters (pClkParams))
//
//
// Setup CAN source clock. Select option
// CANClkSourceSelect(base adress of CAN, clock source) 0 - Selected CPU SYSCLKOUT
CANClkSourceSelect(CANA_BASE, 0); // 500kHz CAN-Clock
//
//Setup comunication parameters for CAN
// CANBitRateSet(base adress of CAN, clock freq for CAN in Hz, bit rate)
//250kb/s
CANBitRateSet(CANA_BASE, 200000000, 250000);
//
DINT;
//
// Initialize the PIE control registers to their default state.
// The default state is all PIE interrupts disabled and flags
// are cleared.
//
InitPieCtrl();
//
// Disable CPU interrupts and clear all CPU interrupt flags
//
IER = 0x0000;
IFR = 0x0000;

// iniialization frame 103h
//frame_103h
sRXCANMessage_103h.ui32MsgID = 0x103; // either 11 or 29
sRXCANMessage_103h.ui32Flags = MSG_OBJ_RX_INT_ENABLE;
sRXCANMessage_103h.ui32MsgLen = sizeof(rxMsgData_103h);
sRXCANMessage_103h.pucMsgData = rxMsgData_103h;

CANEnable(CANA_BASE);

for(;;)
{
//Data from frame 103h
CANMessageSet(CANA_BASE,1, &sRXCANMessage_103h, MSG_OBJ_TYPE_RX);
}

} // end main
//
// End of File
//

  • Hi user4700872,

    I am trying to understand your example based on the code you shared. So what i understood can you please clarify:

    1. You have split the Tx and Rx part, where you say the Tx is working and the Rx Code you have shared is not.
    2. You have configured the Rx Part in this code and you are waiting for receiving the data sent.

    Where i think you are going wrong is after initialization you don't set the frame and you do it inside the "for(;;)" where you should be waiting for the message to be received.

    You should have CANMessageGet API in the loop where you should receive the message sent by the Tx Code.

    Can you please respond to my question and let me know what is that you are expecting in the Rx Part that is not happening.

    Thanks and Regards
    Harshmeet Singh
  • Hi Harshmeet,
    You understood very well (point 1 and 2).
    As soon as possible i will check your suggestion, way to solve the problem with communication RX.
    Thanks .
  • Hi user4700872,

    One more thing you can also enable the receive interrupt to check if the data is being received maybe that will help you debug better.
    Hope the suggestions can help you.

    Thanks and Regards,
    Harshmeet
  • Hi Harshmeet,

    Thanks for your support. Your suggestion was very helpfully.
    I would like to share with you and others users the cod which test the CAN functions (transmitting and receiving mode of frames)
    Bellow the code
    //###########################################################################
    //
    // FILE: can_moje_transmit.c
    //
    //! - CANA is on GPIO71 (CANTXA) and GPIO70 (CANRXA)
    //for LaunchXL-F28377S
    //###########################################################################


    // Included Files
    //
    #include "F28x_Project.h" // Device Headerfile and Examples Include File
    #include <stdint.h>
    #include <stdbool.h>
    #include <stdlib.h>
    #include "inc/hw_types.h"
    #include "inc/hw_memmap.h"
    #include "inc/hw_can.h"
    #include "driverlib/can.h"
    //
    // Defines
    //
    #define RED_LED_GPIO 12 //RED GPIO 12
    #define BLUE_LED_GPIO 13 //BLUE GPIO 13

    //TX
    unsigned char txMsgData_203h[4];
    unsigned char txMsgData_205h[4];
    tCANMsgObject sTXCANMessage_203h;
    tCANMsgObject sTXCANMessage_205h;

    //RX
    unsigned char a_103h,b_103h,c_103h,d_103h;
    unsigned char a_105h,b_105h,c_105h,d_105h;
    unsigned char rxMsgData_103h[4];
    tCANMsgObject sRXCANMessage_103h;
    unsigned char rxMsgData_105h[4];
    tCANMsgObject sRXCANMessage_105h;


    void main(void)
    {

    // Initialize System Control:
    // PLL, WatchDog, enable Peripheral Clocks
    //
    InitSysCtrl();

    // Initialize GPIO and configure GPIO pins for CANTX/CANRX
    // on module A and B
    //
    InitGpio();

    // Setup GPIO pin mux for CAN-A TX/RX
    //CAN-A is on GPIO71 (CANTXA) and GPIO70 (CANRXA)
    //
    GPIO_SetupPinMux(70, GPIO_MUX_CPU1, 5); //GPIO70 - CANRXA
    GPIO_SetupPinOptions(70, GPIO_INPUT, GPIO_ASYNC);
    GPIO_SetupPinMux(71, GPIO_MUX_CPU1, 5); //GPIO71 - CANTXA
    GPIO_SetupPinOptions(71, GPIO_OUTPUT, GPIO_PUSHPULL);

    // Setup GPIO pin mux for LEDs RED_LED (GPIO12), BLUE_LED (GPIO13)
    //
    GPIO_SetupPinMux(RED_LED_GPIO, GPIO_MUX_CPU1, 0);
    GPIO_SetupPinOptions(RED_LED_GPIO, GPIO_OUTPUT, GPIO_PUSHPULL);
    GPIO_SetupPinMux(BLUE_LED_GPIO, GPIO_MUX_CPU1, 0);
    GPIO_SetupPinOptions(BLUE_LED_GPIO, GPIO_OUTPUT, GPIO_PUSHPULL);

    // Initialize the CAN controllers
    // CANInit(based address of CAN)
    CANInit(CANA_BASE);

    // Setup CAN source clock. Select option
    CANClkSourceSelect(CANA_BASE, 0); // 0 - Selected CPU SYSCLKOUT

    //Setup communication parameters for CAN
    // CANBitRateSet(base address of CAN, clock freq for CAN in Hz, bit rate)
    CANBitRateSet(CANA_BASE, 200000000, 250000); //200MHz,250kb/s

    // Clear all interrupts and initialize PIE vector table:
    // Disable CPU interrupts
    DINT;

    // Initialize the PIE control registers to their default state.
    // The default state is all PIE interrupts disabled and flags
    // are cleared.
    InitPieCtrl();

    // Disable CPU interrupts and clear all CPU interrupt flags
    //
    IER = 0x0000;
    IFR = 0x0000;
    // Initialization TX mode frame 203h and 205h
    //frame_203h
    sTXCANMessage_203h.ui32MsgID = 0x203; // either 11 or 29
    sTXCANMessage_203h.ui32Flags = MSG_OBJ_TX_INT_ENABLE;
    sTXCANMessage_203h.ui32MsgLen = sizeof(txMsgData_203h);
    sTXCANMessage_203h.pucMsgData = txMsgData_203h;
    //205h frame
    sTXCANMessage_205h.ui32MsgID = 0x205; // either 11 or 29
    sTXCANMessage_205h.ui32Flags = MSG_OBJ_TX_INT_ENABLE;
    sTXCANMessage_205h.ui32MsgLen = sizeof(txMsgData_205h);
    sTXCANMessage_205h.pucMsgData = txMsgData_205h;
    // Initial date in frame 203h and 205h
    //frame 203h
    txMsgData_203h[0] = 0x1;
    txMsgData_203h[1] = 0x2;
    txMsgData_203h[2] = 0x3;
    txMsgData_203h[3] = 0x4;
    //frame 205h
    txMsgData_205h[0] = 0x1;
    txMsgData_205h[1] = 0x1;
    txMsgData_205h[2] = 0x1;
    txMsgData_205h[3] = 0x1;

    //Initialization RX mode frame 103h
    sRXCANMessage_103h.ui32MsgID = 0x103; // either 11 or 29
    sRXCANMessage_103h.ui32Flags = MSG_OBJ_RX_INT_ENABLE;
    sRXCANMessage_103h.ui32MsgLen = sizeof(rxMsgData_103h);
    sRXCANMessage_103h.pucMsgData = rxMsgData_103h;

    //Initialization RX mode frame 105h
    sRXCANMessage_105h.ui32MsgID = 0x105; // either 11 or 29
    sRXCANMessage_105h.ui32Flags = MSG_OBJ_RX_INT_ENABLE;
    sRXCANMessage_105h.ui32MsgLen = sizeof(rxMsgData_105h);
    sRXCANMessage_105h.pucMsgData = rxMsgData_105h;

    //Setup RX mode
    //frame 103h
    CANMessageSet(CANA_BASE,3, &sRXCANMessage_103h, MSG_OBJ_TYPE_RX);
    //frame 105h
    CANMessageSet(CANA_BASE,4, &sRXCANMessage_105h, MSG_OBJ_TYPE_RX);

    //CAN enable
    CANEnable(CANA_BASE);

    // Turn off LEDs on LaunchXL F28377S
    GPIO_WritePin(RED_LED_GPIO, 1);
    GPIO_WritePin(BLUE_LED_GPIO, 1);

    for(;;)
    {

    //data update for frame 203h //increase value
    txMsgData_203h[0] += 0x01; // +1
    txMsgData_203h[1] += 0x02; // +1
    txMsgData_203h[2] += 0x03; // +1
    txMsgData_203h[3] += 0x04; // +1

    //data update for frame 205h // constant value
    txMsgData_205h[0] = 0x10;
    txMsgData_205h[1] = 0x20;
    txMsgData_205h[2] = 0x30;
    txMsgData_205h[3] = 0xFF;

    // Delay 0.5s
    DELAY_US(1000 * 500);

    // Send the 203h and 2035h to CAN bus
    CANMessageSet(CANA_BASE,1, &sTXCANMessage_203h,MSG_OBJ_TYPE_TX);
    CANMessageSet(CANA_BASE,2, &sTXCANMessage_205h,MSG_OBJ_TYPE_TX);// Delay 0.5s
    // Delay 0.5s
    DELAY_US(1000 * 500);

    // get the 103hfrom CAN bus
    CANMessageGet(CANA_BASE, 3, &sRXCANMessage_103h, true);
    // get the 105hfrom CAN bus
    CANMessageGet(CANA_BASE, 4, &sRXCANMessage_105h, true);
    // Delay 0.5s
    DELAY_US(1000 * 500);

    //Date got from 103h frame
    a_103h=rxMsgData_103h[0];
    b_103h=rxMsgData_103h[1];
    c_103h=rxMsgData_103h[2];
    d_103h=rxMsgData_103h[3];

    //Date got from 105h frame
    a_105h=rxMsgData_105h[0];
    b_105h=rxMsgData_105h[1];
    c_105h=rxMsgData_105h[2];
    d_105h=rxMsgData_105h[3];

    // action when data a from 103h frame will be receive
    // first data of frame 103h will be 10(dec) A(hex) the BLUE_LED blink
    if(a_103h==10)
    {
    //BLUE LED is blinking
    GpioDataRegs.GPATOGGLE.bit.GPIO13 = 1;
    //Turn off RED LED
    GPIO_WritePin(RED_LED_GPIO, 1);
    DELAY_US(1000 * 500);
    }
    else
    {
    //Turn off BLUE LED
    GPIO_WritePin(BLUE_LED_GPIO, 1);
    }

    // action when data a from 105h frame will be receive
    // second data of frame 105h will be 255(dec) FF(hex) the BLUE_LED blink
    if(b_105h==255)
    {
    //RED LED is blinking
    GpioDataRegs.GPATOGGLE.bit.GPIO12 = 1;
    //Turn off BLUE LED
    GPIO_WritePin(BLUE_LED_GPIO, 1);
    DELAY_US(1000 * 500);
    }
    else
    {
    //Turn off RED LED
    GPIO_WritePin(RED_LED_GPIO, 1);
    }

    }
    }
    //
    // End of File
    //