1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-12-16 01:53:51 +01:00

can: use frame len instead of can_dlc

RIOT implementation of CAN bus relies on SocketCAN model.
Since commit c398e56 (can: add optional DLC element to Classical CAN
frame structure), '__u8 can_dlc' attribute of struct can_frame is
considered as deprecated in SocketCAN and kept for legacy support.
Attribute '__u8 len' should be used instead.

	union {
		/* CAN frame payload length in byte (0 .. CAN_MAX_DLEN)
		 * was previously named can_dlc so we need to carry that
		 * name for legacy support
		 */
		__u8 len;
		__u8 can_dlc; /* deprecated */
	};

Moreover, CAN FD frame structure does not support legacy attribute
'can_dlc', making 'len' mandatory for incoming CAN FD support in RIOT.

	struct canfd_frame {
		canid_t can_id;  /* 32 bit CAN_ID + EFF/RTR/ERR flags */
		__u8    len;     /* frame payload length in byte */
		__u8    flags;   /* additional flags for CAN FD */
		__u8    __res0;  /* reserved / padding */
		__u8    __res1;  /* reserved / padding */
		__u8    data[CANFD_MAX_DLEN]
__attribute__((aligned(8)));
	};

Signed-off-by: Gilles DOFFE <gilles.doffe@rtone.fr>
This commit is contained in:
Gilles DOFFE 2024-04-05 23:41:17 +02:00
parent 57993ef263
commit ae51a22fbb
8 changed files with 50 additions and 50 deletions

View File

@ -240,7 +240,7 @@ static int _esp_can_send(candev_t *candev, const struct can_frame *frame)
/* prepare the frame as expected by ESP32 */ /* prepare the frame as expected by ESP32 */
twai_hal_frame_t esp_frame = { }; twai_hal_frame_t esp_frame = { };
esp_frame.dlc = frame->can_dlc; esp_frame.dlc = frame->len;
esp_frame.rtr = (frame->can_id & CAN_RTR_FLAG); esp_frame.rtr = (frame->can_id & CAN_RTR_FLAG);
esp_frame.frame_format = (frame->can_id & CAN_EFF_FLAG); esp_frame.frame_format = (frame->can_id & CAN_EFF_FLAG);
@ -825,7 +825,7 @@ static void IRAM_ATTR _esp_can_intr_handler(void *arg)
} }
frame.can_id |= esp_frame.rtr ? CAN_RTR_FLAG : 0; frame.can_id |= esp_frame.rtr ? CAN_RTR_FLAG : 0;
frame.can_id |= esp_frame.frame_format ? CAN_EFF_FLAG : 0; frame.can_id |= esp_frame.frame_format ? CAN_EFF_FLAG : 0;
frame.can_dlc = esp_frame.dlc; frame.len = esp_frame.dlc;
/* apply acceptance filters only if they are set */ /* apply acceptance filters only if they are set */
unsigned f_id = 0; unsigned f_id = 0;

View File

@ -217,7 +217,7 @@ static int _send(candev_t *candev, const struct can_frame *frame)
nbytes = real_write(dev->sock, frame, sizeof(struct can_frame)); nbytes = real_write(dev->sock, frame, sizeof(struct can_frame));
if (nbytes < frame->can_dlc) { if (nbytes < frame->len) {
real_printf("CAN write op failed, nbytes=%i\n", nbytes); real_printf("CAN write op failed, nbytes=%i\n", nbytes);
return -1; return -1;
} }

View File

@ -412,14 +412,14 @@ static int _send(candev_t *candev, const struct can_frame *frame)
else { else {
can->sTxMailBox[mailbox].TIR = (frame->can_id & CAN_SFF_MASK) << CAN_TIxR_SFF_SHIFT; can->sTxMailBox[mailbox].TIR = (frame->can_id & CAN_SFF_MASK) << CAN_TIxR_SFF_SHIFT;
} }
can->sTxMailBox[mailbox].TDTR = frame->can_dlc & CAN_TDT0R_DLC; can->sTxMailBox[mailbox].TDTR = frame->len & CAN_TDT0R_DLC;
can->sTxMailBox[mailbox].TDLR = 0; can->sTxMailBox[mailbox].TDLR = 0;
can->sTxMailBox[mailbox].TDHR = 0; can->sTxMailBox[mailbox].TDHR = 0;
for (int j = 0; j < 4 && frame->can_dlc > j; j++) { for (int j = 0; j < 4 && frame->len > j; j++) {
can->sTxMailBox[mailbox].TDLR |= (uint32_t)(frame->data[j] << (8 * j)); can->sTxMailBox[mailbox].TDLR |= (uint32_t)(frame->data[j] << (8 * j));
} }
for (int j = 4; j < 8 && frame->can_dlc > j; j++) { for (int j = 4; j < 8 && frame->len > j; j++) {
can->sTxMailBox[mailbox].TDHR |= (uint32_t)(frame->data[j] << (8 * (j - 4))); can->sTxMailBox[mailbox].TDHR |= (uint32_t)(frame->data[j] << (8 * (j - 4)));
} }
@ -470,7 +470,7 @@ static int read_frame(can_t *dev, struct can_frame *frame, int mailbox)
} }
/* Get DLC */ /* Get DLC */
frame->can_dlc = can->sFIFOMailBox[mailbox].RDTR & CAN_RDT0R_DLC; frame->len = can->sFIFOMailBox[mailbox].RDTR & CAN_RDT0R_DLC;
/* Get Data */ /* Get Data */
for (int j = 0; j < 4; j++) { for (int j = 0; j < 4; j++) {

View File

@ -40,7 +40,7 @@
#define MCP2515_TX_CTRL(mailbox) ((MCP2515_TXB0CTRL) + ((mailbox) << 4)) #define MCP2515_TX_CTRL(mailbox) ((MCP2515_TXB0CTRL) + ((mailbox) << 4))
#define MCP2515_RX_CTRL(mailbox) ((MCP2515_RXB0CTRL) + ((mailbox) << 4)) #define MCP2515_RX_CTRL(mailbox) ((MCP2515_RXB0CTRL) + ((mailbox) << 4))
/* length of the fixed part of a can message: 4 bytes can_id + 1 byte can_dlc */ /* length of the fixed part of a can message: 4 bytes can_id + 1 byte len */
#define CAN_FIXED_LEN 5 #define CAN_FIXED_LEN 5
/* oscillator startup time /* oscillator startup time
@ -165,13 +165,13 @@ int mcp2515_send(candev_mcp2515_t *dev, const struct can_frame *frame,
struct can_frame framebuf; struct can_frame framebuf;
if (frame->can_dlc > CAN_MAX_DLEN) { if (frame->len > CAN_MAX_DLEN) {
return -1; return -1;
} }
framebuf.can_id = frame->can_id; framebuf.can_id = frame->can_id;
framebuf.can_dlc = frame->can_dlc; framebuf.len = frame->len;
for (int i = 0; i < framebuf.can_dlc; i++) { for (int i = 0; i < framebuf.len; i++) {
framebuf.data[i] = frame->data[i]; framebuf.data[i] = frame->data[i];
} }
@ -192,14 +192,14 @@ int mcp2515_send(candev_mcp2515_t *dev, const struct can_frame *frame,
_fill_standard_id(framebuf.can_id, outbuf); _fill_standard_id(framebuf.can_id, outbuf);
} }
outbuf[4] = framebuf.can_dlc; outbuf[4] = framebuf.len;
memcpy(&outbuf[CAN_FIXED_LEN], framebuf.data, framebuf.can_dlc); memcpy(&outbuf[CAN_FIXED_LEN], framebuf.data, framebuf.len);
/* set mailbox priority */ /* set mailbox priority */
mcp2515_spi_write(dev, MCP2515_TX_CTRL(mailbox), &prio, 1); mcp2515_spi_write(dev, MCP2515_TX_CTRL(mailbox), &prio, 1);
mcp2515_spi_write_txbuf(dev, mailbox, outbuf, mcp2515_spi_write_txbuf(dev, mailbox, outbuf,
CAN_FIXED_LEN + framebuf.can_dlc); CAN_FIXED_LEN + framebuf.len);
_mcp2515_enable_irq(dev, MCP2515_CANINTE_TX0IE << mailbox); _mcp2515_enable_irq(dev, MCP2515_CANINTE_TX0IE << mailbox);
mcp2515_spi_rts(dev, mailbox); mcp2515_spi_rts(dev, mailbox);
@ -228,8 +228,8 @@ int mcp2515_receive(candev_mcp2515_t *dev, struct can_frame *frame, int mailbox)
(((uint32_t)inbuf[1] & 0xE0) >> 5); (((uint32_t)inbuf[1] & 0xE0) >> 5);
} }
frame->can_dlc = inbuf[4]; frame->len = inbuf[4];
memcpy(frame->data, inbuf + 5, frame->can_dlc); memcpy(frame->data, inbuf + 5, frame->len);
return mailbox; return mailbox;
} }

View File

@ -197,7 +197,7 @@ static int _isotp_rcv_fc(struct isotp *isotp, struct can_frame *frame, int ae)
ztimer_remove(ZTIMER_USEC, &isotp->tx_timer); ztimer_remove(ZTIMER_USEC, &isotp->tx_timer);
if (frame->can_dlc < ae + FC_CONTENT_SZ) { if (frame->len < ae + FC_CONTENT_SZ) {
/* Invalid length */ /* Invalid length */
isotp->tx.state = ISOTP_IDLE; isotp->tx.state = ISOTP_IDLE;
return 1; return 1;
@ -260,7 +260,7 @@ static int _isotp_rcv_sf(struct isotp *isotp, struct can_frame *frame, int ae)
isotp->rx.state = ISOTP_IDLE; isotp->rx.state = ISOTP_IDLE;
int len = (frame->data[ae] & 0x0F); int len = (frame->data[ae] & 0x0F);
if (len > frame->can_dlc - (SF_PCI_SZ + ae)) { if (len > frame->len - (SF_PCI_SZ + ae)) {
return 1; return 1;
} }
@ -307,7 +307,7 @@ static int _isotp_rcv_ff(struct isotp *isotp, struct can_frame *frame, int ae)
isotp->rx.snip = snip; isotp->rx.snip = snip;
isotp->rx.idx = 0; isotp->rx.idx = 0;
for (int i = ae + FF_PCI_SZ; i < frame->can_dlc; i++) { for (int i = ae + FF_PCI_SZ; i < frame->len; i++) {
((uint8_t *)isotp->rx.snip->data)[isotp->rx.idx++] = frame->data[i]; ((uint8_t *)isotp->rx.snip->data)[isotp->rx.idx++] = frame->data[i];
} }
@ -352,7 +352,7 @@ static int _isotp_rcv_cf(struct isotp *isotp, struct can_frame *frame, int ae)
isotp->rx.sn++; isotp->rx.sn++;
isotp->rx.sn %= 16; isotp->rx.sn %= 16;
for (int i = ae + N_PCI_SZ; i < frame->can_dlc; i++) { for (int i = ae + N_PCI_SZ; i < frame->len; i++) {
((uint8_t *)isotp->rx.snip->data)[isotp->rx.idx++] = frame->data[i]; ((uint8_t *)isotp->rx.snip->data)[isotp->rx.idx++] = frame->data[i];
if (isotp->rx.idx >= isotp->rx.snip->size) { if (isotp->rx.idx >= isotp->rx.snip->size) {
break; break;
@ -393,7 +393,7 @@ static int _isotp_rcv(struct isotp *isotp, struct can_frame *frame)
if (IS_ACTIVE(ENABLE_DEBUG)) { if (IS_ACTIVE(ENABLE_DEBUG)) {
DEBUG("_isotp_rcv: id=%" PRIx32 " data=", frame->can_id); DEBUG("_isotp_rcv: id=%" PRIx32 " data=", frame->can_id);
for (int i = 0; i < frame->can_dlc; i++) { for (int i = 0; i < frame->len; i++) {
DEBUG("%02hhx", frame->data[i]); DEBUG("%02hhx", frame->data[i]);
} }
DEBUG("\n"); DEBUG("\n");
@ -431,10 +431,10 @@ static int _isotp_send_fc(struct isotp *isotp, int ae, uint8_t status)
if (isotp->opt.flags & CAN_ISOTP_TX_PADDING) { if (isotp->opt.flags & CAN_ISOTP_TX_PADDING) {
memset(fc.data, isotp->opt.txpad_content, CAN_MAX_DLEN); memset(fc.data, isotp->opt.txpad_content, CAN_MAX_DLEN);
fc.can_dlc = CAN_MAX_DLEN; fc.len = CAN_MAX_DLEN;
} }
else { else {
fc.can_dlc = ae + FC_CONTENT_SZ; fc.len = ae + FC_CONTENT_SZ;
} }
fc.data[ae] = N_PCI_FC | status; fc.data[ae] = N_PCI_FC | status;
@ -449,7 +449,7 @@ static int _isotp_send_fc(struct isotp *isotp, int ae, uint8_t status)
if (IS_ACTIVE(ENABLE_DEBUG)) { if (IS_ACTIVE(ENABLE_DEBUG)) {
DEBUG("_isotp_send_fc: id=%" PRIx32 " data=", fc.can_id); DEBUG("_isotp_send_fc: id=%" PRIx32 " data=", fc.can_id);
for (int i = 0; i < fc.can_dlc; i++) { for (int i = 0; i < fc.len; i++) {
DEBUG("%02hhx", fc.data[i]); DEBUG("%02hhx", fc.data[i]);
} }
DEBUG("\n"); DEBUG("\n");
@ -472,7 +472,7 @@ static void _isotp_create_ff(struct isotp *isotp, struct can_frame *frame, int a
{ {
frame->can_id = isotp->opt.tx_id; frame->can_id = isotp->opt.tx_id;
frame->can_dlc = CAN_MAX_DLEN; frame->len = CAN_MAX_DLEN;
if (ae) { if (ae) {
frame->data[0] = isotp->opt.ext_address; frame->data[0] = isotp->opt.ext_address;
@ -495,15 +495,15 @@ static void _isotp_fill_dataframe(struct isotp *isotp, struct can_frame *frame,
size_t num_bytes = MIN(space, isotp->tx.snip->size - isotp->tx.idx); size_t num_bytes = MIN(space, isotp->tx.snip->size - isotp->tx.idx);
frame->can_id = isotp->opt.tx_id; frame->can_id = isotp->opt.tx_id;
frame->can_dlc = num_bytes + pci_len; frame->len = num_bytes + pci_len;
DEBUG("_isotp_fill_dataframe: num_bytes=%" PRIuSIZE ", pci_len=%" PRIuSIZE "\n", DEBUG("_isotp_fill_dataframe: num_bytes=%" PRIuSIZE ", pci_len=%" PRIuSIZE "\n",
num_bytes, pci_len); num_bytes, pci_len);
if (num_bytes < space) { if (num_bytes < space) {
if (isotp->opt.flags & CAN_ISOTP_TX_PADDING) { if (isotp->opt.flags & CAN_ISOTP_TX_PADDING) {
frame->can_dlc = CAN_MAX_DLEN; frame->len = CAN_MAX_DLEN;
memset(frame->data, isotp->opt.txpad_content, frame->can_dlc); memset(frame->data, isotp->opt.txpad_content, frame->len);
} }
} }

View File

@ -67,8 +67,8 @@ static int _send(int argc, char **argv)
return 1; return 1;
} }
frame.can_id = strtoul(argv[2], NULL, 16); frame.can_id = strtoul(argv[2], NULL, 16);
frame.can_dlc = argc - 3; frame.len = argc - 3;
for (int i = 0; i < frame.can_dlc; i++) { for (int i = 0; i < frame.len; i++) {
frame.data[i] = strtoul(argv[3 + i], NULL, 16); frame.data[i] = strtoul(argv[3 + i], NULL, 16);
} }
conn_can_raw_create(&conn, NULL, 0, ifnum, 0); conn_can_raw_create(&conn, NULL, 0, ifnum, 0);
@ -133,8 +133,8 @@ static int _dump(int argc, char **argv)
== sizeof(struct can_frame))) { == sizeof(struct can_frame))) {
printf("%-8s(%d) %8" PRIX32 " [%x] ", printf("%-8s(%d) %8" PRIX32 " [%x] ",
raw_can_get_name_by_ifnum(ifnum), ifnum, raw_can_get_name_by_ifnum(ifnum), ifnum,
frame.can_id, frame.can_dlc); frame.can_id, frame.len);
for (int i = 0; i < frame.can_dlc; i++) { for (int i = 0; i < frame.len; i++) {
printf(" %02X", frame.data[i]); printf(" %02X", frame.data[i]);
} }
printf("\n"); printf("\n");

View File

@ -69,7 +69,7 @@ static int _send(int argc, char **argv)
struct can_frame frame = { struct can_frame frame = {
.can_id = 1, .can_id = 1,
.can_dlc = 3, .len = 3,
.data[0] = 0xAB, .data[0] = 0xAB,
.data[1] = 0xCD, .data[1] = 0xCD,
.data[2] = 0xEF, .data[2] = 0xEF,
@ -83,7 +83,7 @@ static int _send(int argc, char **argv)
for (int i = 1; i < argc; i++) { for (int i = 1; i < argc; i++) {
frame.data[i - 1] = atoi(argv[i]); frame.data[i - 1] = atoi(argv[i]);
} }
frame.can_dlc = argc - 1; frame.len = argc - 1;
} }
ret = candev->driver->send(candev, &frame); ret = candev->driver->send(candev, &frame);
@ -115,12 +115,12 @@ static int _receive(int argc, char **argv)
puts("Reading from Rxbuf..."); puts("Reading from Rxbuf...");
isrpipe_read(&rxbuf, (uint8_t *)&(frame.can_id), sizeof(frame.can_id)); isrpipe_read(&rxbuf, (uint8_t *)&(frame.can_id), sizeof(frame.can_id));
frame.can_id &= 0x1FFFFFFF; /* clear invalid bits */ frame.can_id &= 0x1FFFFFFF; /* clear invalid bits */
isrpipe_read(&rxbuf, (uint8_t *)&(frame.can_dlc), 1); isrpipe_read(&rxbuf, (uint8_t *)&(frame.len), 1);
printf("id: %" PRIx32 " dlc: %" PRIx8, frame.can_id, frame.can_dlc); printf("id: %" PRIx32 " dlc: %" PRIx8, frame.can_id, frame.len);
if (frame.can_dlc > 0) { if (frame.len > 0) {
printf(" data: "); printf(" data: ");
isrpipe_read(&rxbuf, frame.data, frame.can_dlc); /* data */ isrpipe_read(&rxbuf, frame.data, frame.len); /* data */
for (int i = 0; i < frame.can_dlc; i++) { for (int i = 0; i < frame.len; i++) {
printf("0x%X ", frame.data[i]); printf("0x%X ", frame.data[i]);
} }
} }
@ -240,16 +240,16 @@ static void _can_event_callback(candev_t *dev, candev_event_t event, void *arg)
frame = (struct can_frame *)arg; frame = (struct can_frame *)arg;
DEBUG(" id: %" PRIx32 " dlc: %u data: ", frame->can_id & 0x1FFFFFFF, DEBUG(" id: %" PRIx32 " dlc: %u data: ", frame->can_id & 0x1FFFFFFF,
frame->can_dlc); frame->len);
for (uint8_t i = 0; i < frame->can_dlc; i++) { for (uint8_t i = 0; i < frame->len; i++) {
DEBUG("0x%X ", frame->data[i]); DEBUG("0x%X ", frame->data[i]);
} }
DEBUG_PUTS(""); DEBUG_PUTS("");
/* Store in buffer until user requests the data */ /* Store in buffer until user requests the data */
isrpipe_write(&rxbuf, (uint8_t *)&(frame->can_id), sizeof(frame->can_id)); isrpipe_write(&rxbuf, (uint8_t *)&(frame->can_id), sizeof(frame->can_id));
isrpipe_write_one(&rxbuf, frame->can_dlc); isrpipe_write_one(&rxbuf, frame->len);
isrpipe_write(&rxbuf, frame->data, frame->can_dlc); isrpipe_write(&rxbuf, frame->data, frame->len);
break; break;
case CANDEV_EVENT_RX_ERROR: case CANDEV_EVENT_RX_ERROR:

View File

@ -145,22 +145,22 @@ static int _send(int argc, char **argv, bool rtr)
if (rtr) { if (rtr) {
frame.can_id = CAN_RTR_FLAG | strtoul(argv[3], NULL, 16); frame.can_id = CAN_RTR_FLAG | strtoul(argv[3], NULL, 16);
frame.can_dlc = strtoul(argv[4], NULL, 10); frame.len = strtoul(argv[4], NULL, 10);
} else { } else {
frame.can_id = strtoul(argv[3], NULL, 16); frame.can_id = strtoul(argv[3], NULL, 16);
frame.can_dlc = argc - 4; frame.len = argc - 4;
} }
if (frame.can_dlc > 8) { if (frame.len > 8) {
puts("Invalid length"); puts("Invalid length");
return 1; return 1;
} }
if (rtr) { if (rtr) {
for (int i = 0; i < frame.can_dlc; i++) { for (int i = 0; i < frame.len; i++) {
frame.data[i] = 0x0; frame.data[i] = 0x0;
} }
} else { } else {
for (int i = 0; i < frame.can_dlc; i++) { for (int i = 0; i < frame.len; i++) {
frame.data[i] = strtol(argv[4 + i], NULL, 16); frame.data[i] = strtol(argv[4 + i], NULL, 16);
} }
} }
@ -614,8 +614,8 @@ static void *_receive_thread(void *args)
thread_nb, thread_nb,
raw_can_get_name_by_ifnum(conn[thread_nb].ifnum), raw_can_get_name_by_ifnum(conn[thread_nb].ifnum),
frame.can_id, frame.can_id,
frame.can_dlc); frame.len);
for (int i = 0; i < frame.can_dlc; i++) { for (int i = 0; i < frame.len; i++) {
printf(" %02X", frame.data[i]); printf(" %02X", frame.data[i]);
} }
printf("\n"); printf("\n");