'Why will CAN transmission error counter will increase and go in bus off state?

I am trying to establish Communication between Teensy 4.1with TJA1050 transceiver and 4 Arduino Nano. teensy has to transmitting CAN frame with different ids on bus and specific message ids has to be received by respective Arduino Nano. 0x011 to Nano 1;0x012 to Nano 2; 0x013 to Nano 3; 0x014 to Nano 4; Bus impedance is maintained at 60 ohm. Bus voltage level at CANH to ground and CANL to ground is in prescribed level. Issue: Getting transmission error and going to bus of state. I would be grate help if you fix the issue.

Thankyou.

// Demo for Teensy 4.x  CAN3

#ifndef __IMXRT1062__
  #error "This sketch should be compiled for Teensy 4.x"
#endif
 
//-----------------------------------------------------------------

#include <ACAN_T4.h>

//-----------------------------------------------------------------
//-----------------------------------------------------------------
static uint32_t gSentCount3_x = 0 ;
static uint32_t gSentCount3_y = 0 ;
static uint32_t gSentCount3_w = 0 ;
static uint32_t gReceivedCount3 = 0 ;


int16_t T1 =1, T2=2, T3=3, T4=4;  
uint8_t V1=11, V2=22, V3=33, V4=44;
bool    Vd1=11, Vd2=22, Vd3=33, Vd4=44;

//----------------------------------------------------
CANMessage  message_Tx_MC1,message_Tx_MC2, message_Tx_MC3, message_Tx_MC4 ;// For transmitting x,y,w varibles
CANMessage message_Rx_MC1_S;  //front right steer encoder value
CANMessage message_Rx_MC1_D;  //front right drive encoder value
CANMessage message_Rx_MC2_S;  //front left steer encoder value
CANMessage message_Rx_MC2_D;  //front left drive encoder value
CANMessage message_Rx_MC3_S;  //Back left steer encoder value
CANMessage message_Rx_MC3_D;  //Back left drive encoder value
CANMessage message_Rx_MC4_S;  //Back right steer encoder value
CANMessage message_Rx_MC4_D;  //Back right drive encoder value



//-----------------------------------------------------------------
//   PRIMARY FILTERS CAN3
//-----------------------------------------------------------------
static void handlePrimaryFilterReception_0(const CANMessage &Rx_MC1_S) {message_Rx_MC1_S = Rx_MC1_S; } //MC1_S

static void handlePrimaryFilterReception_1(const CANMessage &Rx_MC1_D) {message_Rx_MC1_D = Rx_MC1_D; } //MC1_D

static void handlePrimaryFilterReception_2(const CANMessage &Rx_MC2_S) {message_Rx_MC2_S = Rx_MC2_S; } //MC2_S

static void handlePrimaryFilterReception_3(const CANMessage &Rx_MC2_D) {message_Rx_MC2_D = Rx_MC2_D; } //MC2_D

static void handlePrimaryFilterReception_4(const CANMessage &Rx_MC3_S) {message_Rx_MC3_S = Rx_MC3_S;} //MC3_S

static void handlePrimaryFilterReception_5(const CANMessage &Rx_MC3_D) {message_Rx_MC3_D = Rx_MC3_D;} //MC3_D

static void handlePrimaryFilterReception_6(const CANMessage &Rx_MC4_S) {message_Rx_MC4_S = Rx_MC4_S;} //MC4_S

static void handlePrimaryFilterReception_7(const CANMessage &Rx_MC4_D) {message_Rx_MC4_D = Rx_MC4_D;} //MC4_D
//--------------------------------------------------------------------




void setup () {
  Serial.begin (9600) ;
  while (!Serial){}

  ACAN_T4_Settings settings (125UL * 1000UL) ;
  settings.mTxPinIsOpenCollector = true ;
  settings.mRxPinConfiguration = ACAN_T4_Settings::PULLUP_22k ;

  //settings.mTxPin =255;//default settings
  //settings.mRxPin =255;//default settings
 
  const ACANPrimaryFilter primaryFilters [8] = {
    ACANPrimaryFilter (kData, kStandard, 0x020, handlePrimaryFilterReception_0), //(msg id:0x020)(MC1)front right steer encoder value
    ACANPrimaryFilter (kData, kStandard, 0x021, handlePrimaryFilterReception_1), //(msg id:0x021)(MC1)front right drive encoder value 
    ACANPrimaryFilter (kData, kStandard, 0x022, handlePrimaryFilterReception_2), // (msg id:0x022)(MC2)front left steer encoder value
    ACANPrimaryFilter (kData, kStandard, 0x023, handlePrimaryFilterReception_3), // (msg id:0x023)(MC2)front left drive encoder value
    ACANPrimaryFilter (kData, kStandard, 0x024, handlePrimaryFilterReception_4), //(msg id:0x024) (MC3) Back left steer encoder value
    ACANPrimaryFilter (kData, kStandard, 0x025, handlePrimaryFilterReception_5), // (msg id:0x025)(MC3)Back left drive encoder value
    ACANPrimaryFilter (kData, kStandard, 0x026, handlePrimaryFilterReception_6), // (msg id:0x026)(MC4)Back right steer encoder value
    ACANPrimaryFilter (kData, kStandard, 0x027, handlePrimaryFilterReception_7), // (msg id:0x027)(MC4)Back right drive encoder value
  };
  
  uint32_t errorCode = ACAN_T4::can3.begin (settings, primaryFilters,8) ;
  if (0 == errorCode) {
    Serial.println ("can3 ok") ;
  }else{
    Serial.print ("Error can3: 0x") ;
    Serial.println (errorCode, HEX) ;
  }
  delay (2) ;


}
 
void loop () {

 //-----MC1-----
  message_Tx_MC1.id = 0x011;
  message_Tx_MC1.ext =false;
  message_Tx_MC1.rtr =false;
  message_Tx_MC1.len = 6;
  message_Tx_MC1.data16[0] = T1;
  message_Tx_MC1.data16[1] = V1;
  message_Tx_MC1.data16[2] = Vd1;


//-----MC2-----
  message_Tx_MC2.id = 0x012;
  message_Tx_MC2.ext =false;
  message_Tx_MC2.rtr =false;
  message_Tx_MC2.len = 6;
  message_Tx_MC2.data16[0] = T2;
  message_Tx_MC2.data16[1] = V2;
  message_Tx_MC2.data16[2] = Vd2;
//-----MC3-----
  message_Tx_MC3.id = 0x013;
  message_Tx_MC3.ext =false;
  message_Tx_MC3.rtr =false;
  message_Tx_MC3.len = 6;
  message_Tx_MC3.data16[0] = T3;
  message_Tx_MC3.data16[2] = V3;
  message_Tx_MC3.data16[2] = Vd3;
//-----MC4-----
  message_Tx_MC4.id = 0x014;
  message_Tx_MC4.ext =false;
  message_Tx_MC4.rtr =false;
  message_Tx_MC4.len = 6;
  message_Tx_MC4.data16[0] = T4;
  message_Tx_MC4.data16[1] = V4;
  message_Tx_MC4.data16[2] = Vd4;
 

//--- CAN3 send 
   
    const bool ok = ACAN_T4::can3.tryToSend (message_Tx_MC1) ;

    if (ok) {
      gSentCount3_x += 1 ;
      Serial.print ("Sent message count: ") ;
       Serial.println (gSentCount3_x); 
      Serial.print ("Sent to MC1"); 
//    Serial.println (x) ;
    
    }
    else Serial.println ("not sent");
   
    Serial.print("transmitErrorCounter=");
    Serial.println(ACAN_T4::can3.transmitErrorCounter());
    
   const bool ok1 = ACAN_T4::can3.tryToSend (message_Tx_MC2) ;
    if (ok1) {
      gSentCount3_y += 1 ;
      Serial.print ("Sent message count: ") ;
       Serial.println (gSentCount3_y); 
      Serial.print ("Sent to MC2"); 
  //  Serial.println (y) ;
    
    }
    else Serial.println ("not sent");

    Serial.print("transmitErrorCounter=");
    Serial.println(ACAN_T4::can3.transmitErrorCounter());
    
    const bool ok2 = ACAN_T4::can3.tryToSend (message_Tx_MC3) ;
    if (ok2) {
      gSentCount3_w += 1 ;
      Serial.print ("Sent message count: ") ;
       Serial.println (gSentCount3_w); 
      Serial.print ("Sent to MC3"); 
   // Serial.println (w) ;
    }
    else Serial.println ("not sent");
    
Serial.print("transmitErrorCounter=");
Serial.println(ACAN_T4::can3.transmitErrorCounter());
    
  const bool ok3 = ACAN_T4::can3.tryToSend (message_Tx_MC4) ;
    if (ok3) {
      gSentCount3_w += 1 ;
      Serial.print ("Sent message count: ") ;
       Serial.println (gSentCount3_w); 
      Serial.print ("Sent to MC4"); 
    //Serial.println (w) ;
    }
    else Serial.println ("not sent");
Serial.print("transmitErrorCounter=");
Serial.println(ACAN_T4::can3.transmitErrorCounter());


//--- CAN3 receive
//while(ACAN_T4::can3.available()){
//  ACAN_T4::can3.dispatchReceivedMessage () ; // Do not use can.receive any more
//} 
//    Serial.print ("MC1_S_Enc value:") ;
//    Serial.println (message_Rx_MC1_S.data64); 
//    Serial.print ("MC1_D_Enc value:") ;
//    Serial.println (message_Rx_MC1_D.data64); 
//        
//    Serial.print ("MC2_S_Enc value:") ;
//    Serial.println (message_Rx_MC2_S.data64); 
//    Serial.print ("MC2_D_Enc value:") ;
//    Serial.println (message_Rx_MC2_D.data64); 

//  if (ACAN_T4::can3.receive (message_Rx)) {
//    gReceivedCount3 += 1 ;
//    Serial.print ("received message count: ") ;
//       Serial.println (gReceivedCount3); 
//      Serial.print ("message_Rx.data64"); 
//    Serial.println (message_Rx.data64) ;
//      }
    
delay(500);
}
#include <ams_as5048b.h>  //For Position Encoder ams
#include <ACAN2515.h>     // For CAN on NANO

//---------For CAN-----------------------
 CANMessage frame_Rx_x,frame_Rx_y, frame_Rx_w, frame_Tx_Enc_1, frame_Tx_Enc_2; // Rx fo receiving, Tx for Transmitting
// Message Ids : 0x011 for 'x' ;0x012 for 'y'; 0x013 for 'w'

static uint32_t gSendDate = 0 ;
static uint32_t gSentcount = 0 ;
static uint32_t gReceivedcount = 0 ;

static const byte MCP2515_CS  = 10 ; // CS input of MCP2515  (D10 pin)
static const byte MCP2515_INT = 2 ; // INT output of MCP2515  (D2 pin)
//static const byte MCP2515_SI = 11;
//static const byte MCP2515_SO = 12;
//static const byte MCP2515_SCK = 13;

ACAN2515 can (MCP2515_CS, SPI, MCP2515_INT) ; // You can use SPI2, SPI3, if provided by your microcontroller

static const uint32_t QUARTZ_FREQUENCY = 8 * 1000 * 1000 ; // don't use 16 MHz because mcp2515 is using 8 MHz crystal
static const uint32_t CAN_BIT_RATE = 125*1000 ;

  static void receive0 (const CANMessage & x){frame_Rx_x = x;}
//  static void receive1 (const CANMessage & y){frame_Rx_y = y;}
//  static void receive2 (const CANMessage & w){frame_Rx_w = w;}

  
//-------------------END For CAN-------------------


//------------------For Encoder via I2c----------------------------------

AMS_AS5048B mysensor1(0b01000000), mysensor2(0b01000001); 

double Encoder_1  = 13.13, Encoder_2 = 14.14 ;// To store angle value from encoder

//////////////END For Encoder via I2c/////////

double x,y,w;// kinematics parameters coming Teensy via CAN. 


//-------------------Setup--------------------------------------------------------------------

void setup() 
{
  Serial.begin(9600);
  delay(10);
  //while (!Serial) ; 
  
//CAN SPI setup//
//  SPI.setMOSI (MCP2515_SI) ;
//  SPI.setMISO (MCP2515_SO) ;
 // SPI.setSCK (MCP2515_SCK) ;
 SPI.begin () ;
  ACAN2515Settings settings (QUARTZ_FREQUENCY, 500UL*1000UL) ;
  //ACAN2515Settings settings (QUARTZ_FREQUENCY, 4,2,8,5,1) ; // 125 kbit/s
  //pre scaler=4 ;Prop Seg =2; Phase 1=8; Phase 2= 5; SJW=1; 66%
  settings.mRequestedMode = ACAN2515Settings::NormalMode  ; // Select Normal Mode
  //-------------For reception filtering
  const ACAN2515Mask rxm0 = standard2515Mask (0x7FF, 0x00, 0x00) ; // For filter #0 and #1
  const ACAN2515Mask rxm1 = standard2515Mask (0x7FF, 0x00, 0x00) ; // For filter #2 to #5
  const ACAN2515AcceptanceFilter filters [] = {
    {standard2515Filter (0x011, 0x00, 0x00), receive0}
  //  {standard2515Filter (0x012, 0x00, 0x00), receive1},
  //  {standard2515Filter (0x013, 0x00, 0x00), receive2}
  } ;
  //--------------
  const uint8_t errorCode = can.begin (settings, [] { can.isr () ; },rxm0,rxm1, filters, 1) ;
  if (0 == errorCode) {
    Serial.println ("Can ok") ;
  }
  else{
    Serial.print ("Error Can: 0x") ;
    Serial.println (errorCode, DEC) ;
  }
//END CAN SPI setup //
} 

void loop() {

//CAN
while(can.available ()){
 can.dispatchReceivedMessage (); // Do not use can.receive any more
 Serial.print("dispatch");
}
    Serial.print ("Receive 0: id-0x") ;
    Serial.println (frame_Rx_x.id,HEX) ;
    Serial.print ("Receive 0: data_s16[0]=") ;
    Serial.println (frame_Rx_x.data16[0]);
    Serial.print ("Receive 0: data_s16[1]=") ;
    Serial.println (frame_Rx_x.data16[1]);
    Serial.print ("Receive 0: data_s16[2]=") ;
    Serial.println (frame_Rx_x.data16[2]);

Serial.print ("receiveErrorCounter=" );   
Serial.println (can.receiveErrorCounter () );

//    Serial.print ("Receive 1: id-0x") ;
//    Serial.println (frame_Rx_y.id,HEX) ;
//    Serial.print ("Receive 1: data-") ;
//    Serial.println (frame_Rx_y.data[0]);
//        
//    Serial.print ("Receive 2: id-0x") ;
//    Serial.println (frame_Rx_w.id,HEX) ;
//    Serial.print ("Receive 2: data-") ;
//    Serial.println (frame_Rx_w.data[0]);
   
 //Receiving
//    if(can.available()) {
//    if(can.receive (frame_Rx_x))
//    {
//    x = frame_Rx_x.data64;    
//    gReceivedcount ++ ;
//    Serial.print ("Received: ") ;
//    Serial.println (gReceivedcount) ;
//    Serial.print(" x:");
//    Serial.println(x);
//    }
//    else Serial.print("Not Received ");
//    }

//    //Transmitting
//    frame_Tx_Enc_1.id = 0x020;   // Steer angle
//    frame_Tx_Enc_1.len = 6;
//    frame_Tx_Enc_1.data_s16[0] = 11;  //data is in double format
//    frame_Tx_Enc_1.data_s16[1] = 12;
//    frame_Tx_Enc_1.data_s16[2] = 13;
// 
//    frame_Tx_Enc_2.id = 0x021;  // Drive motor Speed
//    frame_Tx_Enc_2.len = 6;
//    frame_Tx_Enc_2.data_s16[0] = 21;  //data is in double format
//    frame_Tx_Enc_2.data_s16[1] = 22;
//    frame_Tx_Enc_2.data_s16[2] = 23;

    
//    const bool ok = can.tryToSend (frame_Tx_Enc_1) ;
//    if (ok) {
//      gSentcount += 1 ;
//      Serial.println("transmitted Enc_1");
//    }  
//    delay(10);
//      const bool   ok1 = can.tryToSend (frame_Tx_Enc_2) ;
//    if (ok1) {
//      gSentcount += 1 ;
//      Serial.println("transmitted Enc_2");
//    }
//    delay(10);

 
//END CAN

}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source