Hello,
I'm trying to make a device that sends CAN frames to a CAN network, read from an SD card. It works almost as it should with minor issues that I can't figure out what caused them.
The first problem is that it sometimes swaps the order of two of the messages sent.
The second problem is that when it's working for a long time, at some point it starts sending completely different messages than those read from the SD card.
I was debugging the code, and it turns out that there is no problem with reading from the SD card - that is, it reads the frames in the correct sequence.
But when sending via the flexcan_t4 library, something breaks.
The goal is to send messages, the device on the other hand may or not may sends messages, I'm not interested in them.
The bus load is about 30%, which is not that much. Which means that through my device I send messages in about 830-890 microseconds. I'm analyzing the network with a can bus analyzer.
#include <FlexCAN_T4.h>
#include <SPI.h>
#include <SD.h>
FlexCAN_T4<CAN1, RX_SIZE_1024, TX_SIZE_1024> CAN_Network;
uint32_t startTime =0;
void setup()
{
delay(5000);
//------------------------------------------
// CAN Setup
CAN_Network.begin();
CAN_Network.setBaudRate(500000);
//------------------------------------------
if(!SD.begin(BUILTIN_SDCARD))
{
blinkError(3,Init); // 3 blink if SD card is missing
abort();
}
file = SD.open("CanLog.asc");
if(!file)
{
blinkError(4,Init); // 4 blink if the file is missing
abort();
}
startTime = micros();
}
void loop() {
rawData rawData;
static CAN_message_t can_msg;
long long CurrentTime = micros();
static uint32_t nextTimeStamp = 0;
if(CurrentTime - startTime >= nextTimeStamp)
{
CAN_Network.write(can_msg);
rawData = readData();
can_msg = mapRawDataToCanFrame(rawData);
nextTimeStamp = rawData.TimeStamp;
}
}
This is the main part of the code where I believe I have a problem, the other functions are related to reading from the SD card and parsing the data to the can_msg object.
I think the problem is somewhere in how the objects are initialized by the flexcan_t4 library. But I would be happy if anyone else has any suggestions or directions on where to go.