Micropolis
Member
Hello,
I'm trying to communicate two teensy using CAN communication.
I connected VP230 to CAN1 to the first teensy using pins 22-23 for CTX1 and CRX1, and I connected the the CANL and CANH of the VP230 IC to CANL and CANH of a second IC that is connected to the second Teensy 4.1 board.
i checked with the oscilloscope and saw the CAN message from the first Teensy and after conversion to CANL and CANH and the CAN message is available, but when I connect the second Teensy to Read from it using also CAN1, the message on the oscilloscope disappear. My problem is i can't read the CAN message that I'm sending.
Can anyone help me to solve this problem or tell me if i have a connection mistake or coding mistake?
thank you in advance
This is my sender code:
#include <FlexCAN_T4.h>
FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> Can1;
#define sw1 35
#define sw2 36
void setup(void) {
Serial.begin(115200);
delay(400);
Can1.begin();
Can1.setBaudRate(1000000);
Can1.setMaxMB(16);
Can1.enableFIFO();
Can1.enableFIFOInterrupt();
Can1.onReceive(canSniff);
Can1.mailboxStatus();
Can1.enhanceFilter(MB6);
}
void canSniff(const CAN_message_t &msg) {
Serial.print("MB ");
Serial.print(msg.mb);
Serial.print(" OVERRUN: ");
Serial.print(msg.flags.overrun);
Serial.print(" LEN: ");
Serial.print(msg.len);
Serial.print(" EXT: ");
Serial.print(msg.flags.extended);
Serial.print(" TS: ");
Serial.print(msg.timestamp);
Serial.print(" ID: ");
Serial.print(msg.id, HEX);
Serial.print(" Buffer: ");
for ( uint8_t i = 0; i < msg.len; i++ ) {
Serial.print(msg.buf, HEX); Serial.print(" ");
}
Serial.println();
}
void loop() {
CAN_message_t msg;
msg.id = 0x1;
Can1.events();
enableMode();
delay(1000);
CW();
delay(1000);
CCW();
delay(1000);
Exit();
delay(1000);
}
void enableMode()
{
Serial.println("Enter Motor Mode");
CAN_message_t msg;
msg.id = 0x88;
msg.buf[0] = 0xFF;
msg.buf[1] = 0xFF;
msg.buf[2] = 0xFF;
msg.buf[3] = 0xFF;
msg.buf[4] = 0xFF;
msg.buf[5] = 0xFF;
msg.buf[6] = 0xFF;
msg.buf[7] = 0xFC;
Can1.write(msg);
}
void CW()
{
Serial.println("CW");
CAN_message_t msg;
msg.id = 0x1;
msg.buf[0] = 0x85;
msg.buf[1] = 0x52;
msg.buf[2] = 0x7F;
msg.buf[3] = 0xF0;
msg.buf[4] = 0x51;
msg.buf[5] = 0x11;
msg.buf[6] = 0xE7;
msg.buf[7] = 0xFF;
Can1.write(msg);
}
void CCW()
{
Serial.println("CCW");
CAN_message_t msg;
msg.id = 0x1;
msg.buf[0] = 0x7A;
msg.buf[1] = 0xAC;
msg.buf[2] = 0x7F;
msg.buf[3] = 0xF0;
msg.buf[4] = 0x51;
msg.buf[5] = 0x11;
msg.buf[6] = 0xE7;
msg.buf[7] = 0xFF;
Can1.write(msg);
}
void Exit()
{
Serial.println("Exit");
CAN_message_t msg;
msg.id = 0x1;
msg.buf[0] = 0xFF;
msg.buf[1] = 0xFF;
msg.buf[2] = 0xFF;
msg.buf[3] = 0xFF;
msg.buf[4] = 0xFF;
msg.buf[5] = 0xFF;
msg.buf[6] = 0xFF;
msg.buf[7] = 0xFD;
Can1.write(msg);
}
Receiver code:
#include <FlexCAN_T4.h>
#define debug(msg) Serial.print("["); Serial.print(__FILE__); Serial.print("::"); Serial.print(__LINE__); Serial.print("::"); Serial.print(msg); Serial.println("]");
void debug_pause(void)
{
Serial.print("Paused...");
while (!Serial.available());
while (Serial.available()) Serial.read();
Serial.println("Restarted.");
}
FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> Can1;
FlexCAN_T4<CAN2, RX_SIZE_256, TX_SIZE_16> Can2;
static CAN_message_t msg;
static uint8_t hex[17] = "0123456789abcdef";
// -------------------------------------------------------------
static void hexDump(uint8_t dumpLen, uint8_t *bytePtr)
{
uint8_t working;
while( dumpLen-- ) {
working = *bytePtr++;
Serial.write( hex[ working>>4 ] );
Serial.write( hex[ working&15 ] );
}
Serial.write('\r');
Serial.write('\n');
}
// -------------------------------------------------------------
void setup(void)
{
Serial.begin(115200);
int iSerialTimeout = 1000000;
delay(100);
while (!Serial && (iSerialTimeout-- != 0));
debug (F("start setup"));
Can1.setBaudRate(1000000); // I like to set the baud rates just to be on the safe side
Can2.setBaudRate(1000000);
Can1.begin();
Can2.begin();
// t4 missing msg.ext = 0;
msg.id = 0x100;
msg.len = 8;
msg.flags.extended = 0;
msg.flags.remote = 0;
msg.flags.overrun = 0;
msg.flags.reserved = 0;
msg.buf[0] = 10;
msg.buf[1] = 20;
msg.buf[2] = 0;
msg.buf[3] = 100;
msg.buf[4] = 128;
msg.buf[5] = 64;
msg.buf[6] = 32;
msg.buf[7] = 16;
debug (F("setup complete"));
//debug_pause();
}
// -------------------------------------------------------------
void loop(void)
{
msg.buf[0]++; //since you are in a loop i just incremented each time in stead of doing it 5 times
Can1.write(msg); //you could do it your way as well
Serial.println("T4.0cantest - Repeat: Read bus2, Write bus1");
CAN_message_t inMsg;
if (Can2.read(inMsg)!=0) // Changed this to if as as opposed to while - the way you had it just gets stuck since you haven't even sent a message yet
{
Serial.print("W RD bus 2: "); hexDump(8, inMsg.buf);
}
delay(20);
}
I'm trying to communicate two teensy using CAN communication.
I connected VP230 to CAN1 to the first teensy using pins 22-23 for CTX1 and CRX1, and I connected the the CANL and CANH of the VP230 IC to CANL and CANH of a second IC that is connected to the second Teensy 4.1 board.
i checked with the oscilloscope and saw the CAN message from the first Teensy and after conversion to CANL and CANH and the CAN message is available, but when I connect the second Teensy to Read from it using also CAN1, the message on the oscilloscope disappear. My problem is i can't read the CAN message that I'm sending.
Can anyone help me to solve this problem or tell me if i have a connection mistake or coding mistake?
thank you in advance
This is my sender code:
#include <FlexCAN_T4.h>
FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> Can1;
#define sw1 35
#define sw2 36
void setup(void) {
Serial.begin(115200);
delay(400);
Can1.begin();
Can1.setBaudRate(1000000);
Can1.setMaxMB(16);
Can1.enableFIFO();
Can1.enableFIFOInterrupt();
Can1.onReceive(canSniff);
Can1.mailboxStatus();
Can1.enhanceFilter(MB6);
}
void canSniff(const CAN_message_t &msg) {
Serial.print("MB ");
Serial.print(msg.mb);
Serial.print(" OVERRUN: ");
Serial.print(msg.flags.overrun);
Serial.print(" LEN: ");
Serial.print(msg.len);
Serial.print(" EXT: ");
Serial.print(msg.flags.extended);
Serial.print(" TS: ");
Serial.print(msg.timestamp);
Serial.print(" ID: ");
Serial.print(msg.id, HEX);
Serial.print(" Buffer: ");
for ( uint8_t i = 0; i < msg.len; i++ ) {
Serial.print(msg.buf, HEX); Serial.print(" ");
}
Serial.println();
}
void loop() {
CAN_message_t msg;
msg.id = 0x1;
Can1.events();
enableMode();
delay(1000);
CW();
delay(1000);
CCW();
delay(1000);
Exit();
delay(1000);
}
void enableMode()
{
Serial.println("Enter Motor Mode");
CAN_message_t msg;
msg.id = 0x88;
msg.buf[0] = 0xFF;
msg.buf[1] = 0xFF;
msg.buf[2] = 0xFF;
msg.buf[3] = 0xFF;
msg.buf[4] = 0xFF;
msg.buf[5] = 0xFF;
msg.buf[6] = 0xFF;
msg.buf[7] = 0xFC;
Can1.write(msg);
}
void CW()
{
Serial.println("CW");
CAN_message_t msg;
msg.id = 0x1;
msg.buf[0] = 0x85;
msg.buf[1] = 0x52;
msg.buf[2] = 0x7F;
msg.buf[3] = 0xF0;
msg.buf[4] = 0x51;
msg.buf[5] = 0x11;
msg.buf[6] = 0xE7;
msg.buf[7] = 0xFF;
Can1.write(msg);
}
void CCW()
{
Serial.println("CCW");
CAN_message_t msg;
msg.id = 0x1;
msg.buf[0] = 0x7A;
msg.buf[1] = 0xAC;
msg.buf[2] = 0x7F;
msg.buf[3] = 0xF0;
msg.buf[4] = 0x51;
msg.buf[5] = 0x11;
msg.buf[6] = 0xE7;
msg.buf[7] = 0xFF;
Can1.write(msg);
}
void Exit()
{
Serial.println("Exit");
CAN_message_t msg;
msg.id = 0x1;
msg.buf[0] = 0xFF;
msg.buf[1] = 0xFF;
msg.buf[2] = 0xFF;
msg.buf[3] = 0xFF;
msg.buf[4] = 0xFF;
msg.buf[5] = 0xFF;
msg.buf[6] = 0xFF;
msg.buf[7] = 0xFD;
Can1.write(msg);
}
Receiver code:
#include <FlexCAN_T4.h>
#define debug(msg) Serial.print("["); Serial.print(__FILE__); Serial.print("::"); Serial.print(__LINE__); Serial.print("::"); Serial.print(msg); Serial.println("]");
void debug_pause(void)
{
Serial.print("Paused...");
while (!Serial.available());
while (Serial.available()) Serial.read();
Serial.println("Restarted.");
}
FlexCAN_T4<CAN1, RX_SIZE_256, TX_SIZE_16> Can1;
FlexCAN_T4<CAN2, RX_SIZE_256, TX_SIZE_16> Can2;
static CAN_message_t msg;
static uint8_t hex[17] = "0123456789abcdef";
// -------------------------------------------------------------
static void hexDump(uint8_t dumpLen, uint8_t *bytePtr)
{
uint8_t working;
while( dumpLen-- ) {
working = *bytePtr++;
Serial.write( hex[ working>>4 ] );
Serial.write( hex[ working&15 ] );
}
Serial.write('\r');
Serial.write('\n');
}
// -------------------------------------------------------------
void setup(void)
{
Serial.begin(115200);
int iSerialTimeout = 1000000;
delay(100);
while (!Serial && (iSerialTimeout-- != 0));
debug (F("start setup"));
Can1.setBaudRate(1000000); // I like to set the baud rates just to be on the safe side
Can2.setBaudRate(1000000);
Can1.begin();
Can2.begin();
// t4 missing msg.ext = 0;
msg.id = 0x100;
msg.len = 8;
msg.flags.extended = 0;
msg.flags.remote = 0;
msg.flags.overrun = 0;
msg.flags.reserved = 0;
msg.buf[0] = 10;
msg.buf[1] = 20;
msg.buf[2] = 0;
msg.buf[3] = 100;
msg.buf[4] = 128;
msg.buf[5] = 64;
msg.buf[6] = 32;
msg.buf[7] = 16;
debug (F("setup complete"));
//debug_pause();
}
// -------------------------------------------------------------
void loop(void)
{
msg.buf[0]++; //since you are in a loop i just incremented each time in stead of doing it 5 times
Can1.write(msg); //you could do it your way as well
Serial.println("T4.0cantest - Repeat: Read bus2, Write bus1");
CAN_message_t inMsg;
if (Can2.read(inMsg)!=0) // Changed this to if as as opposed to while - the way you had it just gets stuck since you haven't even sent a message yet
{
Serial.print("W RD bus 2: "); hexDump(8, inMsg.buf);
}
delay(20);
}