Skip to content

Commit a8155d4

Browse files
committed
Use retry mechanism used for initialization of Sparkfun CAN BUS Shield too.
1 parent 9c2b3d9 commit a8155d4

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

VSCP/examples/Seeed-Studio-CAN_BUS_Shield/Seeed-Studio-CAN_BUS_Shield.ino

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,21 +117,22 @@ void setup() {
117117

118118
// Initialize CAN controller with 125 kbit/s (VSCP default bitrate)
119119
if (CAN_OK != canCom.begin(CAN_125KBPS)) {
120+
120121
// Try again
121122
delay(100);
122123
--retry;
123124

124125
if (0 == retry) {
125126
isError = true;
126127
}
127-
}
128-
// Successful initialized
129-
else
130-
{
128+
129+
} else {
130+
131+
// Successful initialized
131132
retry = 0;
132133
}
133134

134-
}while(0 < retry);
135+
} while(0 < retry);
135136

136137
if (true == isError) {
137138

VSCP/examples/Sparkfun_CAN-BUS_Shield/Sparkfun_CAN-BUS_Shield.ino

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,17 +123,37 @@ void actionExecute(unsigned char action, unsigned char par, vscp_RxMessage const
123123

124124
void setup() {
125125

126-
bool status = false;
126+
unsigned char retry = 3; // Max. number of retries for CAN controller initialization
127+
bool isError = false; // Error flag
127128

128129
// Set the baudrate of the serial connection to the PC
129130
Serial.begin(115200);
130131
Serial.println("VSCP node starts up ...");
131132

132-
// Initialize CAN controller with 125 kbit/s (VSCP default bitrate)
133-
if (false == MCP2515::initCAN(CAN_BAUD_125K)) {
133+
do {
134+
// Initialize CAN controller with 125 kbit/s (VSCP default bitrate)
135+
if (false == MCP2515::initCAN(CAN_BAUD_125K)) {
136+
137+
// Try again
138+
delay(100);
139+
--retry;
140+
141+
if (0 == retry) {
142+
isError = true;
143+
}
144+
145+
} else {
146+
147+
// Successful initialized
148+
retry = 0;
149+
}
150+
151+
} while(0 < retry);
152+
153+
if (true == isError) {
134154

135155
Serial.println("Failed to initialize CAN controller!");
136-
156+
137157
}
138158
// Set to normal mode non single shot
139159
else if (false == MCP2515::setCANNormalMode(LOW)) {

0 commit comments

Comments
 (0)