#include <FlexCAN_T4.h>
#define DRIVER1_ADDRESS 0x601
FlexCAN_T4<CAN2, RX_SIZE_256, TX_SIZE_16> can;
CAN_message_t msg;
int counter = 0;
void sendMessage();
void setup(void) {
can.begin();
can.setBaudRate(1000000);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(2, INPUT_PULLDOWN); // button on pin 2
}
void loop() {
if( digitalRead(2) )
{
digitalWrite( LED_BUILTIN, HIGH );
delay(200); // debounce
sendMessage();
digitalWrite( LED_BUILTIN, LOW );
}
}
void sendMessage()
{
uint16_t index = 0x6040;
uint32_t data = 0x80;
msg.id = DRIVER1_ADDRESS;
msg.buf[0] = 0x2B; // Command specifier
msg.buf[1] = (index >> 8) & 0xFF; // Index bit 1
msg.buf[2] = (index >> 0) & 0xFF; // Index bit 2
msg.buf[3] = 0x00; // Subindex
msg.buf[4] = (data >> 24) & 0xFF; // Data bit 1
msg.buf[5] = (data >> 16) & 0xFF; // Data bit 2
msg.buf[6] = (data >> 8) & 0xFF; // Data bit 3
msg.buf[7] = (data >> 0) & 0xFF; // Data bit 4
can.write(msg);
}
#include <Arduino.h>
#include <FlexCAN_T4.h>
FlexCAN_T4<CAN2, RX_SIZE_256, TX_SIZE_16> Can0;
static CAN_message_t msg;
//-------------------------------VARIABLE-----------------------------------------------
unsigned int rpm;
unsigned int temp;
unsigned int tps;
unsigned int kpa;
//----------------------------------------------------------------------------------
//---------------------------------SETUP--------------------------------------------
//----------------------------------------------------------------------------------
void setup() {
Serial.begin(9600); delay(400);
Can0.begin();
Can0.setClock(CLK_60MHz); // on and off no différence !
Can0.setBaudRate(500000); //, LISTEN_ONLY); on and off no différence !
//Can0.setMaxMB(16); //activate no différence
//Can0.enableFIFO(); //activate no différence
//Can0.enableFIFOInterrupt(); //activate no différence
//Can0.onReceive(canSniff); //activate no différence FIGED value
//Can0.mailboxStatus(); // RX ----> MB6
//Can0.enableMBInterrupts (); //activate no différence
}
//---------------------------------FIN SETUP----------------------------------------
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[i], HEX); Serial.print(" ");
} //Serial.println();
//Serial.print(" ID: "); Serial.print(msg.id);
switch (msg.id) {
case 1512: // hex= 0x5F0
rpm = 256*msg.buf[2]+msg.buf[3];
tps= (256*msg.buf[6]+ msg.buf[7])/10;
kpa = ((256*msg.buf[0]+ msg.buf[1])/10);
temp = ((256*msg.buf[4] + msg.buf[5])/10 -32) *5/9; //CLT
break;
}
}
//-----------------------------------------BOUCLE----------------------------------------------
void loop() {
//Can0.events();
Can0.read (msg); //read lance une nouvelle lecture
// switch (msg.id) {
// case 1512: // hex= 0x5F0
rpm = 256*msg.buf[2]+msg.buf[3];
tps= (256*msg.buf[6]+ msg.buf[7])/10;
kpa= ((256*msg.buf[0]+ msg.buf[1])/10);
temp= ((256*msg.buf[4] + msg.buf[5])/10 -32) *5/9; //CLT
// break;
// }
Serial.print("tps: "); Serial.println(tps); //Serial.print("temp: "); Serial.print(temp); Serial.print("kpa: "); Serial.println(kpa);
//power on Tps = 0 ok!
//tps 100% Tps = 0 no ok !
//restart teensy and TPS 100% = Tps 100% ok
//tps 0 = tps =100% !
//no refresh data ?
//thanks
//Can0.mailboxStatus();
}
check your CAN termination, if you got 1 frame and it stopped it may not be ACKing the megasquirt
if ( Can0.read(msg) ) {
// do whatever with a read message
}
#include <FlexCAN_T4.h>
FlexCAN_T4<CAN2, RX_SIZE_256, TX_SIZE_16> Can0;
unsigned int rpm;
unsigned int temp;
unsigned int tps;
unsigned int kpa;
void setup(void) {
Serial.begin(115200); delay(400);
//pinMode(6, OUTPUT); digitalWrite(6, LOW); /* optional tranceiver enable pin */
Can0.begin();
Can0.setBaudRate(500000);
Can0.setMaxMB(16);
Can0.enableFIFO();
Can0.enableFIFOInterrupt();
Can0.onReceive(canSniff);
Can0.mailboxStatus();
}
void canSniff(const CAN_message_t &msg) {
if (msg.id==1512) {
//case 1512: // hex= 0x5F0
rpm = 256*msg.buf[2]+msg.buf[3];
tps= (256*msg.buf[6]+ msg.buf[7])/10;
kpa = ((256*msg.buf[0]+ msg.buf[1])/10);
temp = ((256*msg.buf[4] + msg.buf[5])/10 -32) *5/9; //CLT
//Serial.print("tps: "); Serial.print(tps);Serial.print("temp: "); Serial.print(temp); Serial.print("kpa: "); Serial.println(kpa);
}
}
void loop() {
Can0.events();
Serial.print("tps: "); Serial.print(tps);Serial.print("temp: "); Serial.print(temp); Serial.print("kpa: "); Serial.println(kpa);
}
for (int i = 0; i<NUM_RX_MAILBOXES; i++){
Can0.setMB(i,RX,EXT);
}
for (int i = NUM_RX_MAILBOXES; i<(NUM_TX_MAILBOXES + NUM_RX_MAILBOXES); i++){
Can0.setMB(i,TX,EXT);
}
Can0.setMBFilter(REJECT_ALL);
Can0.enableMBInterrupts();
Can0.onReceive(MB0,canSniff);
Can0.onReceive(MB1,canSniff);
Can0.onReceive(MB2,canSniff);
Can0.setMBFilterProcessing(MB0,0x00,0xFF);
Can0.setMBFilterProcessing(MB1,0x03,0xFF);
Can0.setMBFilterProcessing(MB2,0x0B,0xFF);
you can try setClock() method, older teensies ran at 16mhz osc clock i believe, t4 runs at 24mhz osc by default, you can go up to 60MHz peripheral clock if needed. I am using 60MHz accross 3 busses, 2 busses at 500kbps and one bus at 125kbps