Skip to content

Commit 168b50f

Browse files
Merge pull request #312 from marckleinebudde/cleanup-CAN-bus-error-handling
cleanup CAN bus error handling
2 parents 2e77af7 + 48d09b2 commit 168b50f

2 files changed

Lines changed: 25 additions & 27 deletions

File tree

src/can/bxcan.c

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -312,27 +312,31 @@ bool can_send(can_data_t *channel, struct gs_host_frame *frame)
312312
}
313313
}
314314

315-
uint32_t can_drv_read_reg_status(const struct can_channel *channel)
316-
{
317-
return channel->instance->ESR;
318-
}
319-
320315
bool can_drv_bus_error_pending(const uint32_t reg_esr)
321316
{
322317
const uint32_t lec = FIELD_GET(CAN_ESR_LEC, reg_esr);
323318

324319
return can_is_lec_error(lec);
325320
}
326321

322+
uint32_t can_drv_read_reg_status(const struct can_channel *channel)
323+
{
324+
const uint32_t reg_esr = channel->instance->ESR;
325+
326+
if (can_drv_bus_error_pending(reg_esr)) {
327+
/* mark as handled by software */
328+
channel->instance->ESR |= FIELD_PREP(CAN_ESR_LEC, CAN_LEC_SOFTWARE);
329+
}
330+
331+
return reg_esr;
332+
}
333+
327334
bool can_drv_handle_bus_error(const struct can_channel __maybe_unused *channel, struct gs_host_frame *frame,
328335
const uint32_t reg_esr)
329336
{
330337
const uint8_t tx_err = FIELD_GET(CAN_ESR_TEC, reg_esr);
331338
const uint8_t rx_err = FIELD_GET(CAN_ESR_REC, reg_esr);
332339

333-
/* mark as handled by software */
334-
channel->instance->ESR |= FIELD_PREP(CAN_ESR_LEC, CAN_LEC_SOFTWARE);
335-
336340
if (tx_err == 0 && rx_err == 0)
337341
return false;
338342

@@ -371,7 +375,7 @@ void can_drv_get_device_state(const struct can_channel __maybe_unused *channel,
371375
state->txerr = FIELD_GET(CAN_ESR_TEC, reg_esr);
372376
}
373377

374-
void can_drv_handle_state_change(const struct can_channel *channel, struct gs_host_frame *frame,
378+
void can_drv_handle_state_change(const struct can_channel __maybe_unused *channel, struct gs_host_frame *frame,
375379
const uint32_t reg_esr)
376380
{
377381
enum gs_can_state tx_state, rx_state;
@@ -387,9 +391,4 @@ void can_drv_handle_state_change(const struct can_channel *channel, struct gs_ho
387391
frame->classic_can->data[1] |= gs_can_tx_state_to_frame(tx_state);
388392
if (tx_state <= rx_state)
389393
frame->classic_can->data[1] |= gs_can_rx_state_to_frame(rx_state);
390-
391-
frame->classic_can->data[6] = tx_err;
392-
frame->classic_can->data[7] = rx_err;
393-
394-
can_drv_handle_bus_error(channel, frame, reg_esr);
395394
}

src/can_common.c

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,15 @@ static void can_handle_bus_error(USBD_GS_CAN_HandleTypeDef *hcan, const struct c
255255
}
256256
}
257257

258+
static bool can_bus_error_pending(const struct can_channel *channel, const uint32_t reg_status)
259+
{
260+
if (!(channel->feature & GS_CAN_FEATURE_BERR_REPORTING)) {
261+
return false;
262+
}
263+
264+
return can_drv_bus_error_pending(reg_status);
265+
}
266+
258267
static void can_handle_state_change(USBD_GS_CAN_HandleTypeDef *hcan, struct can_channel *channel,
259268
const uint32_t reg_status)
260269
{
@@ -270,20 +279,12 @@ static void can_handle_state_change(USBD_GS_CAN_HandleTypeDef *hcan, struct can_
270279
} else {
271280
frame->can_id |= CAN_ERR_CRTL | CAN_ERR_CNT;
272281
can_drv_handle_state_change(channel, frame, reg_status);
282+
can_drv_handle_bus_error(channel, frame, reg_status);
273283
}
274284

275285
list_add_tail_locked(&frame_object->list, &hcan->list_to_host);
276286
}
277287

278-
static bool can_bus_error_pending(const struct can_channel *channel, const uint32_t reg_status)
279-
{
280-
if (!(channel->feature & GS_CAN_FEATURE_BERR_REPORTING)) {
281-
return false;
282-
}
283-
284-
return can_drv_bus_error_pending(reg_status);
285-
}
286-
287288
static bool can_state_change_pending(struct can_channel *channel, const uint32_t reg_status)
288289
{
289290
if (channel->state >= GS_CAN_STATE_STOPPED)
@@ -310,11 +311,9 @@ void CAN_HandleError(USBD_GS_CAN_HandleTypeDef *hcan, can_data_t *channel)
310311

311312
const uint32_t reg_status = can_drv_read_reg_status(channel);
312313

313-
if (can_bus_error_pending(channel, reg_status)) {
314-
can_handle_bus_error(hcan, channel, reg_status);
315-
}
316-
317314
if (can_state_change_pending(channel, reg_status)) {
318315
can_handle_state_change(hcan, channel, reg_status);
316+
} else if (can_bus_error_pending(channel, reg_status)) {
317+
can_handle_bus_error(hcan, channel, reg_status);
319318
}
320319
}

0 commit comments

Comments
 (0)