mirror of
https://github.com/candle-usb/candleLight_fw.git
synced 2025-01-14 05:42:53 +08:00
generate linux error frames
This commit is contained in:
parent
e06d2b8682
commit
01a40f66a1
@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "stm32f0xx_hal.h"
|
||||
#include <gs_usb.h>
|
||||
@ -13,3 +14,6 @@ bool can_receive(CAN_HandleTypeDef *hcan, struct gs_host_frame *rx_frame);
|
||||
bool can_is_rx_pending(CAN_HandleTypeDef *hcan);
|
||||
|
||||
bool can_send(CAN_HandleTypeDef *hcan, struct gs_host_frame *frame);
|
||||
|
||||
uint32_t can_get_error_status(CAN_HandleTypeDef *hcan);
|
||||
bool can_parse_error_status(uint32_t err, struct gs_host_frame *frame);
|
||||
|
@ -22,6 +22,82 @@
|
||||
#define CAN_RTR_FLAG 0x40000000U /* remote transmission request */
|
||||
#define CAN_ERR_FLAG 0x20000000U /* error message frame */
|
||||
|
||||
#define CAN_ERR_DLC 8 /* dlc for error message frames */
|
||||
|
||||
/* error class (mask) in can_id */
|
||||
#define CAN_ERR_TX_TIMEOUT 0x00000001U /* TX timeout (by netdevice driver) */
|
||||
#define CAN_ERR_LOSTARB 0x00000002U /* lost arbitration / data[0] */
|
||||
#define CAN_ERR_CRTL 0x00000004U /* controller problems / data[1] */
|
||||
#define CAN_ERR_PROT 0x00000008U /* protocol violations / data[2..3] */
|
||||
#define CAN_ERR_TRX 0x00000010U /* transceiver status / data[4] */
|
||||
#define CAN_ERR_ACK 0x00000020U /* received no ACK on transmission */
|
||||
#define CAN_ERR_BUSOFF 0x00000040U /* bus off */
|
||||
#define CAN_ERR_BUSERROR 0x00000080U /* bus error (may flood!) */
|
||||
#define CAN_ERR_RESTARTED 0x00000100U /* controller restarted */
|
||||
|
||||
/* arbitration lost in bit ... / data[0] */
|
||||
#define CAN_ERR_LOSTARB_UNSPEC 0x00 /* unspecified */
|
||||
/* else bit number in bitstream */
|
||||
|
||||
/* error status of CAN-controller / data[1] */
|
||||
#define CAN_ERR_CRTL_UNSPEC 0x00 /* unspecified */
|
||||
#define CAN_ERR_CRTL_RX_OVERFLOW 0x01 /* RX buffer overflow */
|
||||
#define CAN_ERR_CRTL_TX_OVERFLOW 0x02 /* TX buffer overflow */
|
||||
#define CAN_ERR_CRTL_RX_WARNING 0x04 /* reached warning level for RX errors */
|
||||
#define CAN_ERR_CRTL_TX_WARNING 0x08 /* reached warning level for TX errors */
|
||||
#define CAN_ERR_CRTL_RX_PASSIVE 0x10 /* reached error passive status RX */
|
||||
#define CAN_ERR_CRTL_TX_PASSIVE 0x20 /* reached error passive status TX */
|
||||
/* (at least one error counter exceeds */
|
||||
/* the protocol-defined level of 127) */
|
||||
#define CAN_ERR_CRTL_ACTIVE 0x40 /* recovered to error active state */
|
||||
|
||||
/* error in CAN protocol (type) / data[2] */
|
||||
#define CAN_ERR_PROT_UNSPEC 0x00 /* unspecified */
|
||||
#define CAN_ERR_PROT_BIT 0x01 /* single bit error */
|
||||
#define CAN_ERR_PROT_FORM 0x02 /* frame format error */
|
||||
#define CAN_ERR_PROT_STUFF 0x04 /* bit stuffing error */
|
||||
#define CAN_ERR_PROT_BIT0 0x08 /* unable to send dominant bit */
|
||||
#define CAN_ERR_PROT_BIT1 0x10 /* unable to send recessive bit */
|
||||
#define CAN_ERR_PROT_OVERLOAD 0x20 /* bus overload */
|
||||
#define CAN_ERR_PROT_ACTIVE 0x40 /* active error announcement */
|
||||
#define CAN_ERR_PROT_TX 0x80 /* error occurred on transmission */
|
||||
|
||||
/* error in CAN protocol (location) / data[3] */
|
||||
#define CAN_ERR_PROT_LOC_UNSPEC 0x00 /* unspecified */
|
||||
#define CAN_ERR_PROT_LOC_SOF 0x03 /* start of frame */
|
||||
#define CAN_ERR_PROT_LOC_ID28_21 0x02 /* ID bits 28 - 21 (SFF: 10 - 3) */
|
||||
#define CAN_ERR_PROT_LOC_ID20_18 0x06 /* ID bits 20 - 18 (SFF: 2 - 0 )*/
|
||||
#define CAN_ERR_PROT_LOC_SRTR 0x04 /* substitute RTR (SFF: RTR) */
|
||||
#define CAN_ERR_PROT_LOC_IDE 0x05 /* identifier extension */
|
||||
#define CAN_ERR_PROT_LOC_ID17_13 0x07 /* ID bits 17-13 */
|
||||
#define CAN_ERR_PROT_LOC_ID12_05 0x0F /* ID bits 12-5 */
|
||||
#define CAN_ERR_PROT_LOC_ID04_00 0x0E /* ID bits 4-0 */
|
||||
#define CAN_ERR_PROT_LOC_RTR 0x0C /* RTR */
|
||||
#define CAN_ERR_PROT_LOC_RES1 0x0D /* reserved bit 1 */
|
||||
#define CAN_ERR_PROT_LOC_RES0 0x09 /* reserved bit 0 */
|
||||
#define CAN_ERR_PROT_LOC_DLC 0x0B /* data length code */
|
||||
#define CAN_ERR_PROT_LOC_DATA 0x0A /* data section */
|
||||
#define CAN_ERR_PROT_LOC_CRC_SEQ 0x08 /* CRC sequence */
|
||||
#define CAN_ERR_PROT_LOC_CRC_DEL 0x18 /* CRC delimiter */
|
||||
#define CAN_ERR_PROT_LOC_ACK 0x19 /* ACK slot */
|
||||
#define CAN_ERR_PROT_LOC_ACK_DEL 0x1B /* ACK delimiter */
|
||||
#define CAN_ERR_PROT_LOC_EOF 0x1A /* end of frame */
|
||||
#define CAN_ERR_PROT_LOC_INTERM 0x12 /* intermission */
|
||||
|
||||
/* error status of CAN-transceiver / data[4] */
|
||||
/* CANH CANL */
|
||||
#define CAN_ERR_TRX_UNSPEC 0x00 /* 0000 0000 */
|
||||
#define CAN_ERR_TRX_CANH_NO_WIRE 0x04 /* 0000 0100 */
|
||||
#define CAN_ERR_TRX_CANH_SHORT_TO_BAT 0x05 /* 0000 0101 */
|
||||
#define CAN_ERR_TRX_CANH_SHORT_TO_VCC 0x06 /* 0000 0110 */
|
||||
#define CAN_ERR_TRX_CANH_SHORT_TO_GND 0x07 /* 0000 0111 */
|
||||
#define CAN_ERR_TRX_CANL_NO_WIRE 0x40 /* 0100 0000 */
|
||||
#define CAN_ERR_TRX_CANL_SHORT_TO_BAT 0x50 /* 0101 0000 */
|
||||
#define CAN_ERR_TRX_CANL_SHORT_TO_VCC 0x60 /* 0110 0000 */
|
||||
#define CAN_ERR_TRX_CANL_SHORT_TO_GND 0x70 /* 0111 0000 */
|
||||
#define CAN_ERR_TRX_CANL_SHORT_TO_CANH 0x80 /* 1000 0000 */
|
||||
|
||||
|
||||
enum gs_usb_breq {
|
||||
GS_USB_BREQ_HOST_FORMAT = 0,
|
||||
GS_USB_BREQ_BITTIMING,
|
||||
|
70
src/can.c
70
src/can.c
@ -173,3 +173,73 @@ bool can_send(CAN_HandleTypeDef *hcan, struct gs_host_frame *frame)
|
||||
return retval;
|
||||
}
|
||||
|
||||
uint32_t can_get_error_status(CAN_HandleTypeDef *hcan)
|
||||
{
|
||||
return hcan->Instance->ESR;
|
||||
}
|
||||
|
||||
bool can_parse_error_status(uint32_t err, struct gs_host_frame *frame)
|
||||
{
|
||||
frame->can_id = CAN_ERR_FLAG;
|
||||
frame->can_dlc = CAN_ERR_DLC;
|
||||
frame->data[1] = CAN_ERR_CRTL_UNSPEC;
|
||||
frame->data[2] = CAN_ERR_PROT_UNSPEC;
|
||||
frame->data[3] = CAN_ERR_PROT_LOC_UNSPEC;
|
||||
|
||||
if ((err & 0x04) != 0) { /* bus off flag */
|
||||
frame->can_id |= CAN_ERR_BUSOFF;
|
||||
}
|
||||
|
||||
uint8_t tx_error_cnt = (err>>16) & 0xFF;
|
||||
if (tx_error_cnt >= 96) { /* tx error warning level reached */
|
||||
frame->can_id |= CAN_ERR_CRTL;
|
||||
frame->data[1] |= CAN_ERR_CRTL_TX_WARNING;
|
||||
}
|
||||
if (tx_error_cnt > 127) { /* tx error passive level reached */
|
||||
frame->can_id |= CAN_ERR_CRTL;
|
||||
frame->data[1] |= CAN_ERR_CRTL_TX_PASSIVE;
|
||||
}
|
||||
|
||||
uint8_t rx_error_cnt = (err>>24) & 0xFF;
|
||||
if (rx_error_cnt >= 96) { /* rx error warning level reached */
|
||||
frame->can_id |= CAN_ERR_CRTL;
|
||||
frame->data[1] |= CAN_ERR_CRTL_RX_WARNING;
|
||||
}
|
||||
if (rx_error_cnt > 127) { /* rx error passive level reached */
|
||||
frame->can_id |= CAN_ERR_CRTL;
|
||||
frame->data[1] |= CAN_ERR_CRTL_RX_PASSIVE;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
15
src/main.c
15
src/main.c
@ -36,6 +36,7 @@ bool send_to_host_or_enqueue(struct gs_host_frame *frame)
|
||||
|
||||
int main(void)
|
||||
{
|
||||
uint32_t last_can_error_status = 0;
|
||||
|
||||
HAL_Init();
|
||||
SystemClock_Config();
|
||||
@ -111,6 +112,20 @@ int main(void)
|
||||
|
||||
}
|
||||
|
||||
uint32_t can_err = can_get_error_status(&hCAN);
|
||||
if (can_err != last_can_error_status) {
|
||||
struct gs_host_frame *frame = queue_pop_front(q_frame_pool);
|
||||
if (frame != 0) {
|
||||
if (can_parse_error_status(can_err, frame)) {
|
||||
send_to_host_or_enqueue(frame);
|
||||
last_can_error_status = can_err;
|
||||
} else {
|
||||
queue_push_back(q_frame_pool, frame);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
led_update(&hLED);
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user