mirror of
https://github.com/candle-usb/candleLight_fw.git
synced 2025-01-14 05:42:53 +08:00
Better bit error reporting
It reports more information with more accurate ordering with respect to other frames sent and received. Fixes candle-usb/candleLight_fw#59
This commit is contained in:
parent
8b3a7b4565
commit
ceeac236c4
@ -51,4 +51,4 @@ bool can_is_rx_pending(can_data_t *hcan);
|
||||
bool can_send(can_data_t *hcan, struct gs_host_frame *frame);
|
||||
|
||||
uint32_t can_get_error_status(can_data_t *hcan);
|
||||
bool can_parse_error_status(uint32_t err, struct gs_host_frame *frame);
|
||||
bool can_parse_error_status(uint32_t err, uint32_t last_err, can_data_t *hcan, struct gs_host_frame *frame);
|
||||
|
124
src/can.c
124
src/can.c
@ -232,8 +232,18 @@ uint32_t can_get_error_status(can_data_t *hcan)
|
||||
return can->ESR;
|
||||
}
|
||||
|
||||
bool can_parse_error_status(uint32_t err, struct gs_host_frame *frame)
|
||||
static bool status_is_active(uint32_t err)
|
||||
{
|
||||
return !(err & (CAN_ESR_BOFF | CAN_ESR_EPVF));
|
||||
}
|
||||
|
||||
bool can_parse_error_status(uint32_t err, uint32_t last_err, can_data_t *hcan, struct gs_host_frame *frame)
|
||||
{
|
||||
/* We build up the detailed error information at the same time as we decide
|
||||
* whether there's anything worth sending. This variable tracks that final
|
||||
* result. */
|
||||
bool should_send = false;
|
||||
|
||||
frame->echo_id = 0xFFFFFFFF;
|
||||
frame->can_id = CAN_ERR_FLAG | CAN_ERR_CRTL;
|
||||
frame->can_dlc = CAN_ERR_DLC;
|
||||
@ -246,51 +256,99 @@ bool can_parse_error_status(uint32_t err, struct gs_host_frame *frame)
|
||||
frame->data[6] = 0;
|
||||
frame->data[7] = 0;
|
||||
|
||||
if ((err & CAN_ESR_BOFF) != 0) {
|
||||
if (err & CAN_ESR_BOFF) {
|
||||
frame->can_id |= CAN_ERR_BUSOFF;
|
||||
if (!(last_err & CAN_ESR_BOFF)) {
|
||||
/* We transitioned to bus-off. */
|
||||
should_send = true;
|
||||
}
|
||||
} else if (last_err & CAN_ESR_BOFF) {
|
||||
/* We transitioned out of bus-off. */
|
||||
should_send = true;
|
||||
}
|
||||
|
||||
/* We transitioned from passive/bus-off to active, so report the edge. */
|
||||
if (!status_is_active(last_err) && status_is_active(err)) {
|
||||
frame->data[1] |= CAN_ERR_CRTL_ACTIVE;
|
||||
should_send = true;
|
||||
}
|
||||
|
||||
/*
|
||||
uint8_t tx_error_cnt = (err>>16) & 0xFF;
|
||||
uint8_t rx_error_cnt = (err>>24) & 0xFF;
|
||||
*/
|
||||
/* The Linux sja1000 driver puts these counters here. Seems like as good a
|
||||
* place as any. */
|
||||
frame->data[6] = tx_error_cnt;
|
||||
frame->data[7] = rx_error_cnt;
|
||||
|
||||
uint8_t last_tx_error_cnt = (last_err>>16) & 0xFF;
|
||||
uint8_t last_rx_error_cnt = (last_err>>24) & 0xFF;
|
||||
/* If either error counter transitioned to/from 0. */
|
||||
if ((tx_error_cnt == 0) != (last_tx_error_cnt == 0)) {
|
||||
should_send = true;
|
||||
}
|
||||
if ((rx_error_cnt == 0) != (last_rx_error_cnt == 0)) {
|
||||
should_send = true;
|
||||
}
|
||||
/* If either error counter increased by 15. */
|
||||
if (((int)last_tx_error_cnt + 15) < tx_error_cnt) {
|
||||
should_send = true;
|
||||
}
|
||||
if (((int)last_rx_error_cnt + 15) < rx_error_cnt) {
|
||||
should_send = true;
|
||||
}
|
||||
|
||||
if (err & CAN_ESR_EPVF) {
|
||||
frame->data[1] |= CAN_ERR_CRTL_RX_PASSIVE | CAN_ERR_CRTL_TX_PASSIVE;
|
||||
if (!(last_err & CAN_ESR_EPVF)) {
|
||||
should_send = true;
|
||||
}
|
||||
} else if (err & CAN_ESR_EWGF) {
|
||||
frame->data[1] |= CAN_ERR_CRTL_RX_WARNING | CAN_ERR_CRTL_TX_WARNING;
|
||||
if (!(last_err & CAN_ESR_EWGF)) {
|
||||
should_send = true;
|
||||
}
|
||||
} else if (last_err & (CAN_ESR_EPVF | CAN_ESR_EWGF)) {
|
||||
should_send = true;
|
||||
}
|
||||
|
||||
uint8_t lec = (err>>4) & 0x07;
|
||||
if (lec!=0) { /* protocol error */
|
||||
switch (lec) {
|
||||
case 0x01: /* stuff error */
|
||||
frame->can_id |= CAN_ERR_PROT;
|
||||
frame->data[2] |= CAN_ERR_PROT_STUFF;
|
||||
break;
|
||||
case 0x02: /* form error */
|
||||
frame->can_id |= CAN_ERR_PROT;
|
||||
frame->data[2] |= CAN_ERR_PROT_FORM;
|
||||
break;
|
||||
case 0x03: /* ack error */
|
||||
frame->can_id |= CAN_ERR_ACK;
|
||||
break;
|
||||
case 0x04: /* bit recessive error */
|
||||
frame->can_id |= CAN_ERR_PROT;
|
||||
frame->data[2] |= CAN_ERR_PROT_BIT1;
|
||||
break;
|
||||
case 0x05: /* bit dominant error */
|
||||
frame->can_id |= CAN_ERR_PROT;
|
||||
frame->data[2] |= CAN_ERR_PROT_BIT0;
|
||||
break;
|
||||
case 0x06: /* CRC error */
|
||||
frame->can_id |= CAN_ERR_PROT;
|
||||
frame->data[3] |= CAN_ERR_PROT_LOC_CRC_SEQ;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
switch (lec) {
|
||||
case 0x01: /* stuff error */
|
||||
frame->can_id |= CAN_ERR_PROT;
|
||||
frame->data[2] |= CAN_ERR_PROT_STUFF;
|
||||
should_send = true;
|
||||
break;
|
||||
case 0x02: /* form error */
|
||||
frame->can_id |= CAN_ERR_PROT;
|
||||
frame->data[2] |= CAN_ERR_PROT_FORM;
|
||||
should_send = true;
|
||||
break;
|
||||
case 0x03: /* ack error */
|
||||
frame->can_id |= CAN_ERR_ACK;
|
||||
should_send = true;
|
||||
break;
|
||||
case 0x04: /* bit recessive error */
|
||||
frame->can_id |= CAN_ERR_PROT;
|
||||
frame->data[2] |= CAN_ERR_PROT_BIT1;
|
||||
should_send = true;
|
||||
break;
|
||||
case 0x05: /* bit dominant error */
|
||||
frame->can_id |= CAN_ERR_PROT;
|
||||
frame->data[2] |= CAN_ERR_PROT_BIT0;
|
||||
should_send = true;
|
||||
break;
|
||||
case 0x06: /* CRC error */
|
||||
frame->can_id |= CAN_ERR_PROT;
|
||||
frame->data[3] |= CAN_ERR_PROT_LOC_CRC_SEQ;
|
||||
should_send = true;
|
||||
break;
|
||||
default: /* 0=no error, 7=no change */
|
||||
break;
|
||||
}
|
||||
|
||||
return true;
|
||||
CAN_TypeDef *can = hcan->instance;
|
||||
/* Write 7 to LEC so we know if it gets set to the same thing again */
|
||||
can->ESR = 7<<4;
|
||||
|
||||
return should_send;
|
||||
}
|
||||
|
13
src/main.c
13
src/main.c
@ -145,20 +145,21 @@ int main(void)
|
||||
queue_push_back(q_frame_pool, frame);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t can_err = can_get_error_status(&hCAN);
|
||||
if (can_err != last_can_error_status) {
|
||||
// If there are frames to receive, don't report any error frames. The
|
||||
// best we can localize the errors to is "after the last successfully
|
||||
// received frame", so wait until we get there. LEC will hold some error
|
||||
// to report even if multiple pass by.
|
||||
} else {
|
||||
uint32_t can_err = can_get_error_status(&hCAN);
|
||||
struct gs_host_frame *frame = queue_pop_front(q_frame_pool);
|
||||
if (frame != 0) {
|
||||
frame->timestamp_us = timer_get();
|
||||
if (can_parse_error_status(can_err, frame)) {
|
||||
if (can_parse_error_status(can_err, last_can_error_status, &hCAN, frame)) {
|
||||
send_to_host_or_enqueue(frame);
|
||||
last_can_error_status = can_err;
|
||||
} else {
|
||||
queue_push_back(q_frame_pool, frame);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user