Hello,
I have written a code set in below to communicate two LPC54618 CAN-FD cards. After the running code, the messages are sending and receiving correctly for both cards. However, after a few minutes, the communication stops (I don't know why). Is there anybody help me to solve this issue kindly? Thanks.
#include "board.h"
#include "fsl_debug_console.h"
#include "fsl_gpio.h"
#include "can.h"
#include "pin_mux.h"
#include <stdbool.h>
/*******************************************************************************
* Definitions
******************************************************************************/
#define APP_BOARD_TEST_GPIO_PORT1 BOARD_LED3_GPIO_PORT
#define APP_BOARD_TEST_GPIO_PORT2 BOARD_LED1_GPIO_PORT
#define APP_BOARD_TEST_GPIO_PORT3 BOARD_LED2_GPIO_PORT
#define APP_BOARD_TEST_LED1_PIN BOARD_LED3_GPIO_PIN
#define APP_BOARD_TEST_LED2_PIN BOARD_LED1_GPIO_PIN
#define APP_BOARD_TEST_LED3_PIN BOARD_LED2_GPIO_PIN
/*******************************************************************************
* Prototypes
******************************************************************************/
/*******************************************************************************
* Variables
******************************************************************************/
#define TICKRATE_HZ (1000) /* 1000 ticks per second */
#define TRANSMIT_PERIOD (100) /* milliseconds between transmission */
static volatile uint32_t gTimCnt = 0; /* incremented every millisecond */
/*******************************************************************************
* Code
******************************************************************************/
/*!
* @brief Keeps track of time
*/
void SysTick_Handler(void)
{
// count milliseconds
gTimCnt++;
}
/*!
* @brief Main function
*/
int main(void)
{
can_config_t config;
can_frame_t txmsg = { 0 };
can_frame_t rxmsg = { 0 };
int b;
bool message_transmitted = false;
uint32_t next_id = 0x100;
/* Define the init structure for the output LED pin*/
gpio_pin_config_t led_config = {
kGPIO_DigitalOutput, 0,
};
/* Board pin, clock, debug console init */
/* attach 12 MHz clock to FLEXCOMM0 (debug console) */
CLOCK_AttachClk(BOARD_DEBUG_UART_CLK_ATTACH);
CLOCK_EnableClock(kCLOCK_Gpio0);
CLOCK_EnableClock(kCLOCK_Gpio1);
CLOCK_EnableClock(kCLOCK_Gpio2);
CLOCK_EnableClock(kCLOCK_Gpio3);
BOARD_InitPins();
BOARD_BootClockFROHF48M();
BOARD_InitDebugConsole();
/* configure for 4Mbps data 1Mbps nominal, CAN-FD */
CAN_GetDefaultConfig(&config);
config.baseAddress = 0x20010000;
config.nominalBaudRate = 1000000;
config.dataBaudRate = 4000000;
config.timestampClock_Hz = 100000;
CAN_Init(CAN0, &config, SystemCoreClock);
CAN_Init(CAN1, &config, SystemCoreClock);
/* receive 0x100 in CAN1 rx message buffer 0 by setting mask 0 */
CAN_SetRxIndividualMask(CAN1, 0, CAN_RX_MB_STD(0x100, 0));
/* receive 0x00000200 (29-bit id) in CAN1 rx message buffer 1 by setting mask 1 */
CAN_SetRxExtIndividualMask(CAN1, 1, CAN_RX_MB_EXT_LOW(0x200, 1), CAN_RX_MB_EXT_HIGH(0x200, 1));
/* receive 0x00000300 (29-bit id) in CAN1 rx message buffer 2 by setting mask 2 */
CAN_SetRxExtIndividualMask(CAN1, 2, CAN_RX_MB_EXT_LOW(0x300, 2), CAN_RX_MB_EXT_HIGH(0x300, 2));
/* enable CAN 0 */
CAN_Enable(CAN0, true);
/* enable CAN 1 */
CAN_Enable(CAN1, true);
/* Enable SysTick Timer */
SysTick_Config(SystemCoreClock / TICKRATE_HZ);
/* Init output LED GPIO. */
GPIO_PinInit(GPIO, BOARD_LED1_GPIO_PORT, BOARD_LED1_GPIO_PIN, &led_config);
GPIO_WritePinOutput(GPIO, BOARD_LED1_GPIO_PORT, BOARD_LED1_GPIO_PIN, 1);
GPIO_PinInit(GPIO, BOARD_LED2_GPIO_PORT, BOARD_LED2_GPIO_PIN, &led_config);
GPIO_WritePinOutput(GPIO, BOARD_LED2_GPIO_PORT, BOARD_LED2_GPIO_PIN, 1);
GPIO_PinInit(GPIO, BOARD_LED3_GPIO_PORT, BOARD_LED3_GPIO_PIN, &led_config);
GPIO_WritePinOutput(GPIO, BOARD_LED3_GPIO_PORT, BOARD_LED3_GPIO_PIN, 1);
while (true)
{
/* time to send messages from CAN1 */
if ((gTimCnt % TRANSMIT_PERIOD == 0) && !message_transmitted)
{
txmsg.id = next_id;
txmsg.format = kCAN_FrameFormatStandard;
txmsg.type = kCAN_FrameTypeData;
txmsg.proto = kCAN_ProtoTypeFD;
txmsg.bitratemode = kCAN_BitrateModeTypeSwitch;
txmsg.length = 64;
for (b = 0; b < txmsg.length; b++) txmsg.dataByte[b] = b;
/* use message buffer 3 */
if (CAN_TransferSendBlocking(CAN1, 3, &txmsg) != kStatus_Success)
{
PRINTF("Failed to transmit message\r\n");
}
else
{
message_transmitted = true;
}
/* send 0x200 (29-bit) on tx message buffer 4 */
txmsg.id = 0x200;
txmsg.format = kCAN_FrameFormatExtend;
txmsg.type = kCAN_FrameTypeData;
txmsg.proto = kCAN_ProtoTypeFD;
txmsg.bitratemode = kCAN_BitrateModeTypeSwitch;
txmsg.length = 64;
for (b = 0; b < txmsg.length; b++) txmsg.dataByte[b] = b;
/* use message buffer 4 */
if (CAN_TransferSendBlocking(CAN1, 4, &txmsg) != kStatus_Success)
{
PRINTF("Failed to transmit message 0x200 (29-bit)\r\n");
}
else
{
message_transmitted = true;
}
/* send 0x300 (29-bit) on tx message buffer 5 */
txmsg.id = 0x300;
txmsg.format = kCAN_FrameFormatExtend;
txmsg.type = kCAN_FrameTypeData;
txmsg.proto = kCAN_ProtoTypeFD;
txmsg.bitratemode = kCAN_BitrateModeTypeSwitch;
txmsg.length = 64;
for (b = 0; b < txmsg.length; b++) txmsg.dataByte[b] = b;
/* use message buffer 5 */
if (CAN_TransferSendBlocking(CAN1, 5, &txmsg) != kStatus_Success)
{
PRINTF("Failed to transmit message 0x300 (29-bit)\r\n");
}
else
{
message_transmitted = true;
}
}
else if (gTimCnt % TRANSMIT_PERIOD != 0)
{
message_transmitted = false;
}
/* check for any received messages on CAN1 message buffer 0 */
if (CAN_ReadRxMb(CAN1, 0, &rxmsg) == kStatus_Success)
{
/* toggle LED1 */
GPIO_TogglePinsOutput(GPIO, BOARD_LED1_GPIO_PORT, 1u << BOARD_LED1_GPIO_PIN);
}
/* check for any received messages on CAN1 message buffer 1 */
if (CAN_ReadRxMb(CAN1, 1, &rxmsg) == kStatus_Success)
{
/* toggle LED2 */
GPIO_TogglePinsOutput(GPIO, BOARD_LED2_GPIO_PORT, 1u << BOARD_LED2_GPIO_PIN);
}
if (CAN_ReadRxMb(CAN1, 2, &rxmsg) == kStatus_Success)
{
/* toggle LED3 */
GPIO_TogglePinsOutput(GPIO, BOARD_LED3_GPIO_PORT, 1u << BOARD_LED3_GPIO_PIN);
}
}
}
Is there anybody to solve the issue ?
Many thanks.
What have you done to debug this ?? What happens when "the communication stops" ??
(and, have you remembered termination resistors etc ?)