pierremolinaro / acan2515

MCP2515 CAN Controller Driver for Arduino
MIT License
78 stars 30 forks source link

Problem with dual canbus #23

Open ngochoangimsat opened 4 years ago

ngochoangimsat commented 4 years ago

I tried to connect 2 MCP2515 to MCU via SPI but only first CAN can begin() successful, the second never run ............. ACAN2515 CAN1(MCP2515_CS1, SPI, MCP2515_INT1); ACAN2515 CAN2(MCP2515_CS2, SPI, MCP2515_INT2); ........ //This config will run CAN1 OK, CAN2 hang at begin() const uint16_t errorCode1 = CAN1.begin (settings1, [] { CAN1.isr () ; }) ; const uint16_t errorCode2 = CAN2.begin (settings2, [] { CAN2.isr () ; }) ;

//This config will run CAN2 OK, CAN1 hang at begin() const uint16_t errorCode2 = CAN2.begin (settings2, [] { CAN2.isr () ; }) ; const uint16_t errorCode1 = CAN1.begin (settings1, [] { CAN1.isr () ; }) ;

pierremolinaro commented 4 years ago

Hello,

I never tried to connect 2 MCP2515, but it should work.

I suppose you have called SPI.begin () before CAN1.begin or CAN2.begin ?

What platform do you use ? Arduino Uno ? Can you post the sketch ?

Pierre

Le 23 nov. 2020 à 10:13, ngochoangimsat notifications@github.com a écrit :

I tried to connect 2 MCP2515 to MCU via SPI but only first CAN can begin() successful, the second never run ............. ACAN2515 CAN1(MCP2515_CS1, SPI, MCP2515_INT1); ACAN2515 CAN2(MCP2515_CS2, SPI, MCP2515_INT2); ........ //This config will run CAN1 OK, CAN2 hang at begin() const uint16_t errorCode1 = CAN1.begin (settings1, [] { CAN1.isr () ; }) ; const uint16_t errorCode2 = CAN2.begin (settings2, [] { CAN2.isr () ; }) ;

//This config will run CAN2 OK, CAN1 hang at begin() const uint16_t errorCode2 = CAN2.begin (settings2, [] { CAN2.isr () ; }) ; const uint16_t errorCode1 = CAN1.begin (settings1, [] { CAN1.isr () ; }) ;

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

ngochoangimsat commented 4 years ago

Bellow is the sketch, i used arduino nano I am sure that i connected everything correctly in schematic. If i comment to CAN1 then CAN2 is "Configuration CAN2 OK!" but if i uncomment CAN.begin(..) then only "Configuration CAN1 OK!"

`#include static const byte MCP2515_CS1 = 10 ; // CS input of MCP2515 CAN1 static const byte MCP2515_INT1 = 2 ; // INT output of MCP2515 CAN1 ACAN2515 CAN1(MCP2515_CS1, SPI, MCP2515_INT1);

static const byte MCP2515_CS2 = A3 ; // CS input of MCP2515 CAN2 static const byte MCP2515_INT2 = 3 ; // INT output of MCP2515 CAN2 ACAN2515 CAN2(MCP2515_CS2, SPI, MCP2515_INT2);

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

void setup() { Serial.begin (9600); while (!Serial) { delay (50) ; } SPI.begin(); ACAN2515Settings settings1 (QUARTZ_FREQUENCY, 500UL * 1000UL); settings1.mRequestedMode = ACAN2515Settings::NormalMode; const uint16_t errorCode1 = CAN1.begin (settings1, [] { CAN1.isr () ; }) ; if (errorCode1 != 0) { Serial.print ("Configuration CAN1 error 0x") ; Serial.println (errorCode1, HEX) ; } else { Serial.println ("Configuration CAN1 OK!") ; }

ACAN2515Settings settings2(QUARTZ_FREQUENCY, 500UL * 1000UL); settings2.mRequestedMode = ACAN2515Settings::NormalMode; const uint16_t errorCode2 = CAN2.begin (settings2, [] { CAN2.isr () ; }) ; if (errorCode2 != 0) { Serial.print ("Configuration CAN2 error 0x") ; Serial.println (errorCode2, HEX) ; } else { Serial.println ("Configuration CAN2 OK!") ; }

}

void loop() {

}`

ngochoangimsat commented 4 years ago

1 2

pierremolinaro commented 4 years ago

Hello,

Thank you for your sketch, and I have noted you use an Arduino nano.

The Arduino nano MCU is an ATmega328, with 2 KB of SRAM. 2 KB, it's very little, may be you have a SRAM overflow. Internal receive and transmit buffers are dynamically allocated by the begin function.

The workaround is to decrease the receive and transmit buffer size. Can you try :

ACAN2515Settings settings (QUARTZ_FREQUENCY, 500UL * 1000UL); //CAN1 500kps settings.mReceiveBufferSize = 1 ; // Default value 32, minimum is 1 settings.mTransmitBuffer0Size = 0 ; // Default value 16, 0 is safe settings.mTransmitBuffer1Size = 0 ; // Useless, as default value is 0 settings.mTransmitBuffer2Size = 0 ; // Useless, as default value is 0 const uint16_t errorCode1 = CAN1.begin(settings, [] {CAN1.isr();}) ; const uint16_t errorCode2 = CAN2.begin(settings, [] {CAN2.isr();}) ;

Hint : you can use the same settings variable, if both setting are the same.

Best regards,

Pierre

Le 23 nov. 2020 à 17:10, ngochoangimsat notifications@github.com a écrit :

https://user-images.githubusercontent.com/11856922/99985876-15b4e400-2de1-11eb-9048-8f7a057c3fdd.JPG https://user-images.githubusercontent.com/11856922/99985884-18afd480-2de1-11eb-91c3-50dd817f6e22.JPG — You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/pierremolinaro/acan2515/issues/23#issuecomment-732261277, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEWKZVGX4ZLZ5P6QFPDHZX3SRKCRDANCNFSM4T7GVC4Q.

ngochoangimsat commented 4 years ago

Thank you very much sir! it work now with your settings: 3

pierremolinaro commented 4 years ago

Fine !

Now, you can try to increase the buffer sizes if you need so.

Note you can use the acan2515Tiny library, it requires less RAM and less Flash.

Best regards,

Pierre

Le 23 nov. 2020 à 17:54, ngochoangimsat notifications@github.com a écrit :

Thank you very much sir! it work well now with your settings:

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/pierremolinaro/acan2515/issues/23#issuecomment-732287898, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEWKZVHXJQHSUJH3ZC6QDATSRKHTFANCNFSM4T7GVC4Q.