1111
1212#include " same51.h"
1313
14+ #define DEBUG_CAN (0 )
15+ #if DEBUG_CAN
16+ #define DEBUG_PRINT (...) (Serial.print(__VA_ARGS__), ((void )0 ))
17+ #define DEBUG_PRINTLN (...) (Serial.println(__VA_ARGS__), ((void )0 ))
18+ #else
19+ #define DEBUG_PRINT (...) ((void )0 )
20+ #define DEBUG_PRINTLN (...) ((void )0 )
21+ #endif
22+
1423namespace {
1524#include " CANSAME5x_port.h"
1625}
@@ -129,8 +138,8 @@ EPioType find_pin(const can_function *table, size_t n, int arduino_pin,
129138 for (size_t i = 0 ; i < n; i++) {
130139 if (table[i].port == port && table[i].pin == pin) {
131140 if (instance == -1 || table[i].instance == instance) {
132- // Serial.print ("found #");
133- // Serial.println (i);
141+ DEBUG_PRINT (" found #" );
142+ DEBUG_PRINTLN (i);
134143 instance = table[i].instance ;
135144 return EPioType (table[i].mux );
136145 }
@@ -156,43 +165,41 @@ int CANSAME5x::begin(long baudrate) {
156165 return 0 ;
157166 }
158167
159- /*
160- Serial.print("_rx ");
161- Serial.print(_rx);
162- Serial.print(" ulPort=");
163- Serial.print(g_APinDescription[_rx].ulPort);
164- Serial.print(" ulPin=");
165- Serial.println(g_APinDescription[_rx].ulPin);
168+ DEBUG_PRINT (" _rx " );
169+ DEBUG_PRINT (_rx);
170+ DEBUG_PRINT (" ulPort=" );
171+ DEBUG_PRINT (g_APinDescription[_rx].ulPort );
172+ DEBUG_PRINT (" ulPin=" );
173+ DEBUG_PRINTLN (g_APinDescription[_rx].ulPin );
166174
167- Serial.println ("rx pin table");
175+ DEBUG_PRINTLN (" rx pin table" );
168176 for (size_t i = 0 ; i < size (can_rx); i++) {
169- Serial.print (i);
170- Serial.print (" port=");
171- Serial.print (can_rx[i].port);
172- Serial.print (" pin=");
173- Serial.print (can_rx[i].pin);
174- Serial.print (" instance=");
175- Serial.println (can_rx[i].instance);
176- }
177-
178- Serial.print ("_tx ");
179- Serial.print (_tx);
180- Serial.print (" ulPort=");
181- Serial.print (g_APinDescription[_tx].ulPort);
182- Serial.print (" ulPin=");
183- Serial.println (g_APinDescription[_tx].ulPin);
184-
185- Serial.println ("tx pin table");
177+ DEBUG_PRINT (i);
178+ DEBUG_PRINT (" port=" );
179+ DEBUG_PRINT (can_rx[i].port );
180+ DEBUG_PRINT (" pin=" );
181+ DEBUG_PRINT (can_rx[i].pin );
182+ DEBUG_PRINT (" instance=" );
183+ DEBUG_PRINTLN (can_rx[i].instance );
184+ }
185+
186+ DEBUG_PRINT (" _tx " );
187+ DEBUG_PRINT (_tx);
188+ DEBUG_PRINT (" ulPort=" );
189+ DEBUG_PRINT (g_APinDescription[_tx].ulPort );
190+ DEBUG_PRINT (" ulPin=" );
191+ DEBUG_PRINTLN (g_APinDescription[_tx].ulPin );
192+
193+ DEBUG_PRINTLN (" tx pin table" );
186194 for (size_t i = 0 ; i < size (can_tx); i++) {
187- Serial.print (i);
188- Serial.print (" port=");
189- Serial.print (can_tx[i].port);
190- Serial.print (" pin=");
191- Serial.print (can_tx[i].pin);
192- Serial.print (" instance=");
193- Serial.println (can_tx[i].instance);
195+ DEBUG_PRINT (i);
196+ DEBUG_PRINT (" port=" );
197+ DEBUG_PRINT (can_tx[i].port );
198+ DEBUG_PRINT (" pin=" );
199+ DEBUG_PRINT (can_tx[i].pin );
200+ DEBUG_PRINT (" instance=" );
201+ DEBUG_PRINTLN (can_tx[i].instance );
194202 }
195- */
196203
197204 int instance = -1 ;
198205 EPioType tx_function = find_pin (can_tx, size (can_tx), _tx, instance);
@@ -340,6 +347,8 @@ int CANSAME5x::endPacket() {
340347 return 0 ;
341348 }
342349
350+ bus_autorecover ();
351+
343352 // TODO wait for TX buffer to free
344353
345354 _canSAME5x_tx_buf &buf = state->tx_buffer [0 ];
@@ -409,6 +418,7 @@ int CANSAME5x::_parsePacket() {
409418
410419int CANSAME5x::parsePacket () {
411420 cpu_irq_enter_critical ();
421+ bus_autorecover ();
412422 int result = _parsePacket ();
413423 cpu_irq_leave_critical ();
414424 return result;
@@ -481,21 +491,17 @@ int CANSAME5x::observe() {
481491
482492int CANSAME5x::loopback () {
483493 hw->CCCR .bit .INIT = 1 ;
484- Serial.println (" loopback start" );
485494 while (!hw->CCCR .bit .INIT ) {
486495 }
487496 hw->CCCR .bit .CCE = 1 ;
488497
489498 hw->CCCR .bit .TEST = 1 ;
490499 hw->TEST .bit .LBCK = 1 ;
491- Serial.println (" loopback b" );
492500
493501 hw->CCCR .bit .CCE = 0 ;
494502 hw->CCCR .bit .INIT = 0 ;
495- Serial.println (" loopback c" );
496503 while (hw->CCCR .bit .INIT ) {
497504 }
498- Serial.println (" loopback d" );
499505 return 1 ;
500506}
501507
@@ -522,6 +528,16 @@ int CANSAME5x::wakeup() {
522528 }
523529 return 1 ;
524530}
531+
532+ void CANSAME5x::bus_autorecover () {
533+ if (hw->PSR .bit .BO ) {
534+ DEBUG_PRINTLN (" bus autorecovery activated" );
535+ hw->CCCR .bit .INIT = 0 ;
536+ while (hw->CCCR .bit .INIT ) {
537+ }
538+ }
539+ }
540+
525541void CANSAME5x::onInterrupt () {
526542 for (int i = 0 ; i < size (instances); i++) {
527543 CANSAME5x *instance = instances[i];
0 commit comments