#include "inc/tm4c123gh6pm.h" #include #include #include "driverlib/can.h" #include "driverlib/sysctl.h" #define MSG_OBJ_TX_INT_ENABLE #define MSG_OBJ_USE_DIR_FILTER //***************************************************************************** //! //! This example shows the basic setup of CAN in order to transmit messages //! on the CAN bus. The CAN peripheral is configured to transmit messages //! with a specific CAN ID. A message is then transmitted once per second, //! using a simple delay loop for timing. The message that is sent is a 4 //! byte message that contains an incrementing pattern. A CAN interrupt //! handler is used to confirm message transmission and count the number of //! messages that have been sent. //! //! This code uses the following peripherals and I/O signals. You must //! review these and change as needed for your own board: //! - CAN0 peripheral //! - GPIO Port B peripheral (for CAN0 pins) ==> Changed to Port E //! - CAN0RX - PB4 ==> Changed to PE4 //! - CAN0TX - PB5 ==> Changed to PE5 //! // //***************************************************************************** // xmit object and buffer tCANMsgObject xCANMessage; uint32_t ui32xMsgData; // buffer // receive object and buffer tCANMsgObject rCANMessage; uint32_t ui32rMsgData; // buffer //***************************************************************************** // // Configure the CAN and enter a loop to transmit periodic CAN messages. // //***************************************************************************** void setup(void) { Serial.begin(9600); Serial.println(); Serial.println("============================="); Serial.println(); Serial.println("CAN polling test with Loop-back and Silent Modes"); delay(1000); Serial.println(); // For this example CAN0 is used with RX and TX pins on port E4 and E5. // The actual port and pins used may be different on your part, consult // the data sheet for more information. // The GPIO port E needs to be enabled so these pins can be used. // TODO: change this to whichever GPIO port you are using SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE); Serial.println(" (1) Enable portE"); // Configure the GPIO pin muxing to select CAN0 functions for these pins. // This step selects which alternate function is available for these pins. // This is necessary if your part supports GPIO pin function muxing. // Consult the data sheet to see which functions are allocated per pin. // TODO: change this to select the port/pin you are using GPIOPinConfigure(GPIO_PE4_CAN0RX); GPIOPinConfigure(GPIO_PE5_CAN0TX); Serial.println(" (2) Configured pins"); // Enable the alternate function on the GPIO pins. The above step selects // which alternate function is available. This step actually enables the // alternate function instead of GPIO for these pins. // TODO: change this to match the port/pin you are using GPIOPinTypeCAN(GPIO_PORTE_BASE, GPIO_PIN_4 | GPIO_PIN_5); Serial.println(" (3) Set CAN pins"); // The GPIO port and pins have been set up for CAN. The CAN peripheral // must be enabled. SysCtlPeripheralEnable(SYSCTL_PERIPH_CAN0); Serial.println(" (4) Enable CAN Peripheral"); // Initialize the CAN controller CANInit(CAN0_BASE); Serial.println(" (5) Initd CAN"); // Set up the bit rate for the CAN bus. This function sets up the CAN // bus timing for a nominal configuration. You can achieve more control // over the CAN bus timing by using the function CANBitTimingSet() instead // of this one, if needed. // In this example, the CAN bus is set to 125 kHz. In the function below, // the call to SysCtlClockGet() is used to determine the clock rate that // is used for clocking the CAN peripheral. This can be replaced with a // fixed value if you know the value of the system clock, saving the extra // function call. For some parts, the CAN peripheral is clocked by a fixed // 8 MHz regardless of the system clock in which case the call to // SysCtlClockGet() should be replaced with 8000000. Consult the data // sheet for more information about CAN peripheral clocking. CANBitRateSet(CAN0_BASE, SysCtlClockGet(), 125000); Serial.println(" (6) Bitrate set"); // This example uses the CAN controller in Loop-back and silent mode for testing. // It must be put into Test-mode, and then the loop-back and silent set. CAN0_CTL_R |= CAN_CTL_TEST; CAN0_TST_R |= CAN_TST_LBACK | CAN_TST_SILENT; Serial.println(" (7) Loop-back and Silent modes set"); // Enable CAN CANEnable(CAN0_BASE); Serial.println(" (8) CAN Enabled"); // Initialize the message object that will be used for sending CAN // messages. The message will be 4 bytes that will contain an incrementing // value. Initially it will be set to 0. ui32rMsgData = 0; rCANMessage.ui32MsgID = 0; // To accept all traffic, this must be 0 rCANMessage.ui32MsgIDMask = 0; rCANMessage.ui32Flags = MSG_OBJ_USE_ID_FILTER; rCANMessage.ui32MsgLen = sizeof(ui32rMsgData); rCANMessage.pui8MsgData = (uint8_t *)&ui32rMsgData; CANMessageSet(CAN0_BASE, 3, &rCANMessage, MSG_OBJ_TYPE_RX); delay(100); Serial.println(" (9) Rx object set up"); // Initialize a xmit object ui32xMsgData = 10000; xCANMessage.ui32MsgID = 0x00001560; xCANMessage.ui32MsgIDMask = 0x1FFFFFF0; xCANMessage.ui32Flags = MSG_OBJ_USE_ID_FILTER; xCANMessage.ui32MsgLen = sizeof(ui32xMsgData); xCANMessage.pui8MsgData = (uint8_t *)&ui32xMsgData; delay(100); Serial.println(" (10) Tx object set up"); // Enter loop to send messages. A new message will be sent once per // second. The 4 bytes of message content will be treated as an uint32_t // and incremented by one each time. // Initialize the xmit buffer: Serial.println("== Init Complete =================="); Serial.println(); } void loop () { // Send the CAN message using object number 1 (not the same thing as // CAN ID, which is also 1 in this example). This function will cause // the message to be transmitted right away. // CANMessageSet(CAN0_BASE, 1, &xCANMessage, MSG_OBJ_TYPE_TX); Serial.println("Msg xmitted"); Serial.println(ui32xMsgData); // Increment the id and value in the message data. //xCANMessage.ui32MsgID++; ui32xMsgData++; // Now wait some time before continuing delay(1000); //* // Receive a message CANMessageGet( CAN0_BASE, 3, &rCANMessage, 0); // Check flags to see if new data has arrived. //*/ if((rCANMessage.ui32Flags & MSG_OBJ_NEW_DATA)!=0) { Serial.print(" Received: ["); Serial.print(rCANMessage.ui32MsgID); Serial.print("]("); Serial.print(rCANMessage.ui32MsgLen); Serial.print(") "); Serial.println(*(uint32_t *)(rCANMessage.pui8MsgData)); } }