Skip to content

Commit 52d38a1

Browse files
committed
Handle can_frequency sync error
add a timeout + return an error message
1 parent 3122abe commit 52d38a1

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

targets/TARGET_STM/can_api.c

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,10 @@ void can_init_freq (can_t *obj, PinName rd, PinName td, int hz)
8585
error("Cannot initialize CAN");
8686
}
8787

88-
// Set initial CAN frequency to requested kb/s
89-
can_frequency(obj, hz);
88+
// Set initial CAN frequency to 100 kb/s
89+
if (can_frequency(obj, 100000) != 1) {
90+
error("Can frequency could not be set\n");
91+
};
9092

9193
uint32_t filter_number = (obj->can == CAN_1) ? 0 : 14;
9294
can_filter(obj, 0, 0, CANStandard, filter_number);
@@ -195,19 +197,34 @@ int can_frequency(can_t *obj, int f)
195197
int pclk = HAL_RCC_GetPCLK1Freq();
196198
int btr = can_speed(pclk, (unsigned int)f, 1);
197199
CAN_TypeDef *can = (CAN_TypeDef *)(obj->can);
200+
uint32_t tickstart = 0;
201+
int status = 0;
198202

199203
if (btr > 0) {
200204
can->MCR |= CAN_MCR_INRQ ;
201205
while ((can->MSR & CAN_MSR_INAK) != CAN_MSR_INAK) {
202206
}
207+
/* Get tick */
208+
tickstart = HAL_GetTick();
203209
can->BTR = btr;
204210
can->MCR &= ~(uint32_t)CAN_MCR_INRQ;
205211
while ((can->MSR & CAN_MSR_INAK) == CAN_MSR_INAK) {
212+
if((HAL_GetTick() - tickstart ) > 2)
213+
{
214+
status = HAL_CAN_STATE_TIMEOUT;
215+
break;
216+
}
217+
}
218+
if (status !=0) {
219+
error("can ESR 0x%04x.%04x + timeout status %d", (can->ESR&0XFFFF0000)>>16, (can->ESR&0XFFFF), status);
220+
status=0;
221+
} else {
222+
status=1; //mbed OK value
206223
}
207-
return 1;
208224
} else {
209-
return 0;
225+
status=0;
210226
}
227+
return status;
211228
}
212229

213230
int can_write(can_t *obj, CAN_Message msg, int cc)
@@ -243,8 +260,8 @@ int can_write(can_t *obj, CAN_Message msg, int cc)
243260
((uint32_t)msg.data[1] << 8) |
244261
((uint32_t)msg.data[0]));
245262
can->sTxMailBox[transmitmailbox].TDHR = (((uint32_t)msg.data[7] << 24) |
246-
((uint32_t)msg.data[6] << 16) |
247-
((uint32_t)msg.data[5] << 8) |
263+
((uint32_t)msg.data[6] << 16) |
264+
((uint32_t)msg.data[5] << 8) |
248265
((uint32_t)msg.data[4]));
249266
/* Request transmission */
250267
can->sTxMailBox[transmitmailbox].TIR |= CAN_TI0R_TXRQ;

0 commit comments

Comments
 (0)