pierremolinaro / acan2515

MCP2515 CAN Controller Driver for Arduino
MIT License
74 stars 29 forks source link

ESP32 Works in LoopbackMode but not NormalMode #42

Open nickdaria opened 1 year ago

nickdaria commented 1 year ago

Sketch working in loopback mode just fine, however when I switch to NormalMode, it doesn't get any messages through, and it always starts having Message Send Failures after send # 17. Any thoughts?

Only major change was adding the STBY pin as mapped for the tranciever.

//——————————————————————————————————————————————————————————————————————————————
//  ACAN2515 Demo in loopback mode, for ESP32
//——————————————————————————————————————————————————————————————————————————————

#ifndef ARDUINO_ARCH_ESP32
  #error "Select an ESP32 board" 
#endif

//——————————————————————————————————————————————————————————————————————————————

#include <ACAN2515.h>

//——————————————————————————————————————————————————————————————————————————————
//  For using SPI on ESP32, see demo sketch SPI_Multiple_Buses
//  Two SPI busses are available in Arduino, HSPI and VSPI.
//  By default, Arduino SPI uses VSPI, leaving HSPI unused.
//  Default VSPI pins are: SCK=18, MISO=19, MOSI=23.
//  You can change the default pin with additional begin arguments
//    SPI.begin (MCP2515_SCK, MCP2515_MISO, MCP2515_MOSI)
//  CS input of MCP2515 should be connected to a digital output port
//  INT output of MCP2515 should be connected to a digital input port, with interrupt capability
//  Notes:
//    - GPIOs 34 to 39 are GPIs – input only pins. These pins don’t have internal pull-ups or
//      pull-down resistors. They can’t be used as outputs.
//    - some pins do not support INPUT_PULLUP (see https://www.esp32.com/viewtopic.php?t=439) 
//    - All GPIOs can be configured as interrupts
// See https://randomnerdtutorials.com/esp32-pinout-reference-gpios/
//——————————————————————————————————————————————————————————————————————————————

static const byte MCP2515_SCK  = 8 ; // SCK input of MCP2515 
static const byte MCP2515_MOSI = 10 ; // SDI input of MCP2515  
static const byte MCP2515_MISO = 9 ; // SDO output of MCP2515 

static const byte MCP2515_CS  = 2 ; // CS input of MCP2515 (adapt to your design) 
static const byte MCP2515_INT = 3 ; // INT output of MCP2515 (adapt to your design)
static const byte MCP2515_RESET = 5 ; // RESET input of MCP2515 (adapt to your design)
static const byte MCP2515_STBY = 4; //  STBY input

//——————————————————————————————————————————————————————————————————————————————
//  MCP2515 Driver object
//——————————————————————————————————————————————————————————————————————————————

ACAN2515 can (MCP2515_CS, SPI, MCP2515_INT) ;

//——————————————————————————————————————————————————————————————————————————————
//  MCP2515 Quartz: adapt to your design
//——————————————————————————————————————————————————————————————————————————————

static const uint32_t QUARTZ_FREQUENCY = 20UL * 1000UL * 1000UL ; // 20 MHz

//——————————————————————————————————————————————————————————————————————————————
//   SETUP
//——————————————————————————————————————————————————————————————————————————————

void setup () {
//  STBY OFF
  pinMode(MCP2515_STBY, OUTPUT);
  digitalWrite(MCP2515_STBY, HIGH);

//--- RESET MCP2515
  pinMode (MCP2515_RESET, OUTPUT) ;
  digitalWrite (MCP2515_RESET, LOW) ;
  delay (10) ;
  digitalWrite (MCP2515_RESET, HIGH) ;
//--- Start serial
  Serial.begin (115200) ;
//--- Wait for serial (blink led at 10 Hz during waiting)
  while (!Serial) {
    delay (50) ;
  }
//--- Begin SPI
  SPI.begin (MCP2515_SCK, MCP2515_MISO, MCP2515_MOSI) ;
//--- Configure ACAN2515
  Serial.println ("Configure ACAN2515") ;
  ACAN2515Settings settings (QUARTZ_FREQUENCY, 500UL * 1000UL) ; // CAN bit rate 500 kb/s
  settings.mRequestedMode = ACAN2515Settings::NormalMode ; // Select loopback mode
  const uint16_t errorCode = can.begin (settings, [] { can.isr () ; }) ;
  if (errorCode == 0) {
    Serial.print ("Bit Rate prescaler: ") ;
    Serial.println (settings.mBitRatePrescaler) ;
    Serial.print ("Propagation Segment: ") ;
    Serial.println (settings.mPropagationSegment) ;
    Serial.print ("Phase segment 1: ") ;
    Serial.println (settings.mPhaseSegment1) ;
    Serial.print ("Phase segment 2: ") ;
    Serial.println (settings.mPhaseSegment2) ;
    Serial.print ("SJW: ") ;
    Serial.println (settings.mSJW) ;
    Serial.print ("Triple Sampling: ") ;
    Serial.println (settings.mTripleSampling ? "yes" : "no") ;
    Serial.print ("Actual bit rate: ") ;
    Serial.print (settings.actualBitRate ()) ;
    Serial.println (" bit/s") ;
    Serial.print ("Exact bit rate ? ") ;
    Serial.println (settings.exactBitRate () ? "yes" : "no") ;
    Serial.print ("Sample point: ") ;
    Serial.print (settings.samplePointFromBitStart ()) ;
    Serial.println ("%") ;
  }else{
    Serial.print ("Configuration error 0x") ;
    Serial.println (errorCode, HEX) ;
  }
}

//----------------------------------------------------------------------------------------------------------------------

static uint32_t gBlinkLedDate = 0 ;
static uint32_t gReceivedFrameCount = 0 ;
static uint32_t gSentFrameCount = 0 ;

//——————————————————————————————————————————————————————————————————————————————

void printFrm(CANMessage frame) {
  Serial.print(frame.id, HEX);
  Serial.print(", ");
  for(uint8_t i = 0; i < 8; i++) {
    Serial.print(frame.data[i], HEX);
    Serial.print(" ");
  }
}

void loop () {
  CANMessage frame;
  CANMessage frameRx;
  frame.id = 0x01;
  frame.len = 8;
  frame.ext = true;
  frame.data64 = 0xFF;
  if (gBlinkLedDate < millis ()) {
    gBlinkLedDate += 2000 ;
    const bool ok = can.tryToSend (frame) ;
    if (ok) {
      gSentFrameCount += 1 ;
      Serial.print ("Sent: ") ;
      Serial.println (gSentFrameCount) ;
    }else{
      Serial.println ("Send failure") ;
    }
  }
  if (can.available ()) {
    can.receive (frameRx) ;
    printFrm(frameRx);
    gReceivedFrameCount ++ ;
    Serial.print ("Received: ") ;
    Serial.println (gReceivedFrameCount) ;
  }
}

//——————————————————————————————————————————————————————————————————————————————
pierremolinaro commented 1 year ago

Hello,

I think the STBY pin of the transceiver should be LOW for enabling it for receiving and transmitting frame. What is the reference of your transceiver ?

Pierre

Le 21 juin 2023 à 19:29, Nick Daria @.***> a écrit :

Sketch working in loopback mode just fine, however when I switch to NormalMode, it doesn't get any messages through, and it always starts having Message Send Failures after send # 17. Any thoughts?

Only major change was adding the STBY pin as mapped for the tranciever.

//—————————————————————————————————————————————————————————————————————————————— // ACAN2515 Demo in loopback mode, for ESP32 //——————————————————————————————————————————————————————————————————————————————

ifndef ARDUINO_ARCH_ESP32

error "Select an ESP32 board"

endif

//——————————————————————————————————————————————————————————————————————————————

include

//—————————————————————————————————————————————————————————————————————————————— // For using SPI on ESP32, see demo sketch SPI_Multiple_Buses // Two SPI busses are available in Arduino, HSPI and VSPI. // By default, Arduino SPI uses VSPI, leaving HSPI unused. // Default VSPI pins are: SCK=18, MISO=19, MOSI=23. // You can change the default pin with additional begin arguments // SPI.begin (MCP2515_SCK, MCP2515_MISO, MCP2515_MOSI) // CS input of MCP2515 should be connected to a digital output port // INT output of MCP2515 should be connected to a digital input port, with interrupt capability // Notes: // - GPIOs 34 to 39 are GPIs – input only pins. These pins don’t have internal pull-ups or // pull-down resistors. They can’t be used as outputs. // - some pins do not support INPUT_PULLUP (see https://www.esp32.com/viewtopic.php?t=439) // - All GPIOs can be configured as interrupts // See https://randomnerdtutorials.com/esp32-pinout-reference-gpios/ //——————————————————————————————————————————————————————————————————————————————

static const byte MCP2515_SCK = 8 ; // SCK input of MCP2515 static const byte MCP2515_MOSI = 10 ; // SDI input of MCP2515
static const byte MCP2515_MISO = 9 ; // SDO output of MCP2515

static const byte MCP2515_CS = 2 ; // CS input of MCP2515 (adapt to your design) static const byte MCP2515_INT = 3 ; // INT output of MCP2515 (adapt to your design) static const byte MCP2515_RESET = 5 ; // RESET input of MCP2515 (adapt to your design) static const byte MCP2515_STBY = 4; // STBY input

//—————————————————————————————————————————————————————————————————————————————— // MCP2515 Driver object //——————————————————————————————————————————————————————————————————————————————

ACAN2515 can (MCP2515_CS, SPI, MCP2515_INT) ;

//—————————————————————————————————————————————————————————————————————————————— // MCP2515 Quartz: adapt to your design //——————————————————————————————————————————————————————————————————————————————

static const uint32_t QUARTZ_FREQUENCY = 20UL 1000UL 1000UL ; // 20 MHz

//—————————————————————————————————————————————————————————————————————————————— // SETUP //——————————————————————————————————————————————————————————————————————————————

void setup () { // STBY OFF pinMode(MCP2515_STBY, OUTPUT); digitalWrite(MCP2515_STBY, HIGH);

//--- RESET MCP2515 pinMode (MCP2515_RESET, OUTPUT) ; digitalWrite (MCP2515_RESET, LOW) ; delay (10) ; digitalWrite (MCP2515_RESET, HIGH) ; //--- Start serial Serial.begin (115200) ; //--- Wait for serial (blink led at 10 Hz during waiting) while (!Serial) { delay (50) ; } //--- Begin SPI SPI.begin (MCP2515_SCK, MCP2515_MISO, MCP2515_MOSI) ; //--- Configure ACAN2515 Serial.println ("Configure ACAN2515") ; ACAN2515Settings settings (QUARTZ_FREQUENCY, 500UL * 1000UL) ; // CAN bit rate 500 kb/s settings.mRequestedMode = ACAN2515Settings::NormalMode ; // Select loopback mode const uint16_t errorCode = can.begin (settings, [] { can.isr () ; }) ; if (errorCode == 0) { Serial.print ("Bit Rate prescaler: ") ; Serial.println (settings.mBitRatePrescaler) ; Serial.print ("Propagation Segment: ") ; Serial.println (settings.mPropagationSegment) ; Serial.print ("Phase segment 1: ") ; Serial.println (settings.mPhaseSegment1) ; Serial.print ("Phase segment 2: ") ; Serial.println (settings.mPhaseSegment2) ; Serial.print ("SJW: ") ; Serial.println (settings.mSJW) ; Serial.print ("Triple Sampling: ") ; Serial.println (settings.mTripleSampling ? "yes" : "no") ; Serial.print ("Actual bit rate: ") ; Serial.print (settings.actualBitRate ()) ; Serial.println (" bit/s") ; Serial.print ("Exact bit rate ? ") ; Serial.println (settings.exactBitRate () ? "yes" : "no") ; Serial.print ("Sample point: ") ; Serial.print (settings.samplePointFromBitStart ()) ; Serial.println ("%") ; }else{ Serial.print ("Configuration error 0x") ; Serial.println (errorCode, HEX) ; } }

//----------------------------------------------------------------------------------------------------------------------

static uint32_t gBlinkLedDate = 0 ; static uint32_t gReceivedFrameCount = 0 ; static uint32_t gSentFrameCount = 0 ;

//——————————————————————————————————————————————————————————————————————————————

void printFrm(CANMessage frame) { Serial.print(frame.id, HEX); Serial.print(", "); for(uint8_t i = 0; i < 8; i++) { Serial.print(frame.data[i], HEX); Serial.print(" "); } }

void loop () { CANMessage frame; CANMessage frameRx; frame.id = 0x01; frame.len = 8; frame.ext = true; frame.data64 = 0xFF; if (gBlinkLedDate < millis ()) { gBlinkLedDate += 2000 ; const bool ok = can.tryToSend (frame) ; if (ok) { gSentFrameCount += 1 ; Serial.print ("Sent: ") ; Serial.println (gSentFrameCount) ; }else{ Serial.println ("Send failure") ; } } if (can.available ()) { can.receive (frameRx) ; printFrm(frameRx); gReceivedFrameCount ++ ; Serial.print ("Received: ") ; Serial.println (gReceivedFrameCount) ; } }

//——————————————————————————————————————————————————————————————————————————————

— Reply to this email directly, view it on GitHub https://github.com/pierremolinaro/acan2515/issues/42, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEWKZVFCDIBIO6X2IZPOBD3XMMVOJANCNFSM6AAAAAAZPCKSJU. You are receiving this because you are subscribed to this thread.

nickdaria commented 11 months ago

Pierre, sorry for the late response. I did verify it occurred with STBY both low and high.