pierremolinaro / acan2515Tiny

Arduino library for the MCP2515 CAN Controller
MIT License
4 stars 3 forks source link

Multiple instantiation of the can object #4

Open Gosa7777 opened 3 years ago

Gosa7777 commented 3 years ago

Hello Pierre, I'm trying to connect two 2515 boards to my Arduino (Mega2560). I defined 2 different chip selects and interrupts (10/3 & 11/2) and taken separately they work. When I try to instantiate 2 can objects (can0, can1 with 2 settings objects), the 2nd one gives an error on can1.begin.

Thanks in advance for your help Claude

This is the setup function adapted from your sample code: static const byte MCP2515_CS0 = 10 ; // CS input of MCP2515 (adapt to your design) static const byte MCP2515_INT0 = 3 ; // INT output of MCP2515 (adapt to your design)

static const byte MCP2515_CS1 = 11 ; // CS input of MCP2515 (adapt to your design) static const byte MCP2515_INT1 = 2 ; // INT output of MCP2515 (adapt to your design)

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

ACAN2515Tiny can0 (MCP2515_CS0, SPI, MCP2515_INT0) ; ACAN2515Tiny can1 (MCP2515_CS1, SPI, MCP2515_INT1) ;

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

static const uint32_t QUARTZ_FREQUENCY = 8UL 1000UL 1000UL ; // 16 MHz

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

void setup () { //--- Switch on builtin led pinMode (LED_BUILTIN, OUTPUT) ; digitalWrite (LED_BUILTIN, HIGH) ; //--- Start serial Serial.begin (38400) ; //--- Wait for serial (blink led at 10 Hz during waiting) while (!Serial) { delay (50) ; digitalWrite (LED_BUILTIN, !digitalRead (LED_BUILTIN)) ; } //--- Begin SPI SPI.begin () ; //--- Configure ACAN2515 Serial.println ("Configure ACAN2515_0") ; ACAN2515TinySettings settings0 (QUARTZ_FREQUENCY, 500UL * 1000UL) ; // CAN bit rate 500 kb/s // settings0.mRequestedMode = ACAN2515TinySettings::LoopBackMode ; // Select loopback mode settings0.mRequestedMode = ACAN2515TinySettings::NormalMode ; const uint16_t errorCode0 = can0.begin (settings0, [] { can0.isr () ; }) ; if (errorCode0 == 0) { Serial.print ("Bit Rate prescaler: ") ; Serial.println (settings0.mBitRatePrescaler) ; Serial.print ("Propagation Segment: ") ; Serial.println (settings0.mPropagationSegment) ; Serial.print ("Phase segment 1: ") ; Serial.println (settings0.mPhaseSegment1) ; Serial.print ("Phase segment 2: ") ; Serial.println (settings0.mPhaseSegment2) ; Serial.print ("SJW: ") ; Serial.println (settings0.mSJW) ; Serial.print ("Triple Sampling: ") ; Serial.println (settings0.mTripleSampling ? "yes" : "no") ; Serial.print ("Actual bit rate: ") ; Serial.print (settings0.actualBitRate ()) ; Serial.println (" bit/s") ; Serial.print ("Exact bit rate ? ") ; Serial.println (settings0.exactBitRate () ? "yes" : "no") ; Serial.print ("Sample point: ") ; Serial.print (settings0.samplePointFromBitStart ()) ; Serial.println ("%") ; }else{ Serial.print ("Configuration error 0x") ; Serial.println (errorCode0, HEX) ; }

Serial.println ("Configure ACAN2515_1") ; ACAN2515TinySettings settings1 (QUARTZ_FREQUENCY, 500UL * 1000UL) ; // CAN bit rate 500 kb/s settings1.mRequestedMode = ACAN2515TinySettings::LoopBackMode ; // Select loopback mode //settings1.mRequestedMode = ACAN2515TinySettings::NormalMode ; const uint16_t errorCode1 = can1.begin (settings1, [] { can1.isr () ; }) ; if (errorCode1 == 0) { Serial.print ("Bit Rate prescaler: ") ; Serial.println (settings1.mBitRatePrescaler) ; Serial.print ("Propagation Segment: ") ; Serial.println (settings1.mPropagationSegment) ; Serial.print ("Phase segment 1: ") ; Serial.println (settings1.mPhaseSegment1) ; Serial.print ("Phase segment 2: ") ; Serial.println (settings1.mPhaseSegment2) ; Serial.print ("SJW: ") ; Serial.println (settings1.mSJW) ; Serial.print ("Triple Sampling: ") ; Serial.println (settings1.mTripleSampling ? "yes" : "no") ; Serial.print ("Actual bit rate: ") ; Serial.print (settings1.actualBitRate ()) ; Serial.println (" bit/s") ; Serial.print ("Exact bit rate ? ") ; Serial.println (settings1.exactBitRate () ? "yes" : "no") ; Serial.print ("Sample point: ") ; Serial.print (settings1.samplePointFromBitStart ()) ; Serial.println ("%") ; }else{ Serial.print ("Configuration error 0x") ; Serial.println (errorCode1, HEX) ; } }

Gosa7777 commented 3 years ago

Somehow it works now, I don't know exactly where the problem was. Thank you for your code!

Gosa7777 commented 3 years ago

The issue still exists, always for the second MCP2515 to be initialized, sometimes it works, sometimes not. I loop through the begin() method until I get no error code and after the 4th time it seems to work consistently. Not sure what the root cause is?

Configure ACAN2515_1 Configuration error 0x1 Configuration error 0x1 Configuration error 0x1 Bit Rate prescaler: 1 Propagation Segment: 2 Phase segment 1: 2 Phase segment 2: 3 SJW: 2 Triple Sampling: no Actual bit rate: 500000 bit/s Exact bit rate ? yes Sample point: 62%

pierremolinaro commented 3 years ago

Hello,

This kind of erratic issue is difficult to fix. Configuration error 0x1 means the MCP2515 cannot be connected with success trought SPI.

Note as the two MCP2515 receives the same setting, you can use the same ACAN2515TinySettings object.

Do you use a MCP2515 module from a manufacturer or do you have you own hardware ?

Do you drive the RESET pin of each MCP2515 ?

How does the 8MHz clock is provided to the MCP2515 ? Do you use a crystal / ceramic resonator ?

What happens if you initialize the MCP2515_1 before the MCP2515_0 ?

Pierre

Le 2 août 2021 à 09:38, Gosa7777 @.***> a écrit :

The issue still exists, always for the second MCP2515 to be initialized, sometimes it works, sometimes not. I loop through the begin() method until I get no error code and after the 4th time it seems to work consistently. Not sure what the root cause is?

Configure ACAN2515_1 Configuration error 0x1 Configuration error 0x1 Configuration error 0x1 Bit Rate prescaler: 1 Propagation Segment: 2 Phase segment 1: 2 Phase segment 2: 3 SJW: 2 Triple Sampling: no Actual bit rate: 500000 bit/s Exact bit rate ? yes Sample point: 62%

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/pierremolinaro/acan2515Tiny/issues/4#issuecomment-890797021, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEWKZVF4YPP2IUEERW7T6HDT2ZDRBANCNFSM5BITPZTA.