From ae51a22fbb46a05bd51dee796104cf6c5291a2f5 Mon Sep 17 00:00:00 2001 From: Gilles DOFFE Date: Fri, 5 Apr 2024 23:41:17 +0200 Subject: [PATCH] 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 --- cpu/esp32/periph/can.c | 4 ++-- cpu/native/periph/can.c | 2 +- cpu/stm32/periph/can.c | 8 ++++---- drivers/mcp2515/mcp2515.c | 18 +++++++++--------- sys/can/isotp/isotp.c | 24 ++++++++++++------------ sys/shell/cmds/conn_can.c | 8 ++++---- tests/drivers/candev/main.c | 22 +++++++++++----------- tests/sys/conn_can/main.c | 14 +++++++------- 8 files changed, 50 insertions(+), 50 deletions(-) diff --git a/cpu/esp32/periph/can.c b/cpu/esp32/periph/can.c index cdb55201ac..3a6e9561b9 100644 --- a/cpu/esp32/periph/can.c +++ b/cpu/esp32/periph/can.c @@ -240,7 +240,7 @@ static int _esp_can_send(candev_t *candev, const struct can_frame *frame) /* prepare the frame as expected by ESP32 */ 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.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.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 */ unsigned f_id = 0; diff --git a/cpu/native/periph/can.c b/cpu/native/periph/can.c index 78ce8ddf96..e7a8d5eee2 100644 --- a/cpu/native/periph/can.c +++ b/cpu/native/periph/can.c @@ -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)); - if (nbytes < frame->can_dlc) { + if (nbytes < frame->len) { real_printf("CAN write op failed, nbytes=%i\n", nbytes); return -1; } diff --git a/cpu/stm32/periph/can.c b/cpu/stm32/periph/can.c index 73dbfad93f..ca4e2943b9 100644 --- a/cpu/stm32/periph/can.c +++ b/cpu/stm32/periph/can.c @@ -412,14 +412,14 @@ static int _send(candev_t *candev, const struct can_frame *frame) else { 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].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)); } - 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))); } @@ -470,7 +470,7 @@ static int read_frame(can_t *dev, struct can_frame *frame, int mailbox) } /* Get DLC */ - frame->can_dlc = can->sFIFOMailBox[mailbox].RDTR & CAN_RDT0R_DLC; + frame->len = can->sFIFOMailBox[mailbox].RDTR & CAN_RDT0R_DLC; /* Get Data */ for (int j = 0; j < 4; j++) { diff --git a/drivers/mcp2515/mcp2515.c b/drivers/mcp2515/mcp2515.c index 158537fa99..6d4f194976 100644 --- a/drivers/mcp2515/mcp2515.c +++ b/drivers/mcp2515/mcp2515.c @@ -40,7 +40,7 @@ #define MCP2515_TX_CTRL(mailbox) ((MCP2515_TXB0CTRL) + ((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 /* oscillator startup time @@ -165,13 +165,13 @@ int mcp2515_send(candev_mcp2515_t *dev, const struct can_frame *frame, struct can_frame framebuf; - if (frame->can_dlc > CAN_MAX_DLEN) { + if (frame->len > CAN_MAX_DLEN) { return -1; } framebuf.can_id = frame->can_id; - framebuf.can_dlc = frame->can_dlc; - for (int i = 0; i < framebuf.can_dlc; i++) { + framebuf.len = frame->len; + for (int i = 0; i < framebuf.len; 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); } - outbuf[4] = framebuf.can_dlc; - memcpy(&outbuf[CAN_FIXED_LEN], framebuf.data, framebuf.can_dlc); + outbuf[4] = framebuf.len; + memcpy(&outbuf[CAN_FIXED_LEN], framebuf.data, framebuf.len); /* set mailbox priority */ mcp2515_spi_write(dev, MCP2515_TX_CTRL(mailbox), &prio, 1); 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_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); } - frame->can_dlc = inbuf[4]; - memcpy(frame->data, inbuf + 5, frame->can_dlc); + frame->len = inbuf[4]; + memcpy(frame->data, inbuf + 5, frame->len); return mailbox; } diff --git a/sys/can/isotp/isotp.c b/sys/can/isotp/isotp.c index 37baed2066..8e69a9a762 100644 --- a/sys/can/isotp/isotp.c +++ b/sys/can/isotp/isotp.c @@ -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); - if (frame->can_dlc < ae + FC_CONTENT_SZ) { + if (frame->len < ae + FC_CONTENT_SZ) { /* Invalid length */ isotp->tx.state = ISOTP_IDLE; 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; 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; } @@ -307,7 +307,7 @@ static int _isotp_rcv_ff(struct isotp *isotp, struct can_frame *frame, int ae) isotp->rx.snip = snip; 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]; } @@ -352,7 +352,7 @@ static int _isotp_rcv_cf(struct isotp *isotp, struct can_frame *frame, int ae) isotp->rx.sn++; 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]; if (isotp->rx.idx >= isotp->rx.snip->size) { break; @@ -393,7 +393,7 @@ static int _isotp_rcv(struct isotp *isotp, struct can_frame *frame) if (IS_ACTIVE(ENABLE_DEBUG)) { 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("\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) { memset(fc.data, isotp->opt.txpad_content, CAN_MAX_DLEN); - fc.can_dlc = CAN_MAX_DLEN; + fc.len = CAN_MAX_DLEN; } else { - fc.can_dlc = ae + FC_CONTENT_SZ; + fc.len = ae + FC_CONTENT_SZ; } 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)) { 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("\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_dlc = CAN_MAX_DLEN; + frame->len = CAN_MAX_DLEN; if (ae) { 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); 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", num_bytes, pci_len); if (num_bytes < space) { if (isotp->opt.flags & CAN_ISOTP_TX_PADDING) { - frame->can_dlc = CAN_MAX_DLEN; - memset(frame->data, isotp->opt.txpad_content, frame->can_dlc); + frame->len = CAN_MAX_DLEN; + memset(frame->data, isotp->opt.txpad_content, frame->len); } } diff --git a/sys/shell/cmds/conn_can.c b/sys/shell/cmds/conn_can.c index 031e933965..a0db1eac08 100644 --- a/sys/shell/cmds/conn_can.c +++ b/sys/shell/cmds/conn_can.c @@ -67,8 +67,8 @@ static int _send(int argc, char **argv) return 1; } frame.can_id = strtoul(argv[2], NULL, 16); - frame.can_dlc = argc - 3; - for (int i = 0; i < frame.can_dlc; i++) { + frame.len = argc - 3; + for (int i = 0; i < frame.len; i++) { frame.data[i] = strtoul(argv[3 + i], NULL, 16); } conn_can_raw_create(&conn, NULL, 0, ifnum, 0); @@ -133,8 +133,8 @@ static int _dump(int argc, char **argv) == sizeof(struct can_frame))) { printf("%-8s(%d) %8" PRIX32 " [%x] ", raw_can_get_name_by_ifnum(ifnum), ifnum, - frame.can_id, frame.can_dlc); - for (int i = 0; i < frame.can_dlc; i++) { + frame.can_id, frame.len); + for (int i = 0; i < frame.len; i++) { printf(" %02X", frame.data[i]); } printf("\n"); diff --git a/tests/drivers/candev/main.c b/tests/drivers/candev/main.c index 6376d0a96d..ba17f4d885 100644 --- a/tests/drivers/candev/main.c +++ b/tests/drivers/candev/main.c @@ -69,7 +69,7 @@ static int _send(int argc, char **argv) struct can_frame frame = { .can_id = 1, - .can_dlc = 3, + .len = 3, .data[0] = 0xAB, .data[1] = 0xCD, .data[2] = 0xEF, @@ -83,7 +83,7 @@ static int _send(int argc, char **argv) for (int i = 1; i < argc; i++) { frame.data[i - 1] = atoi(argv[i]); } - frame.can_dlc = argc - 1; + frame.len = argc - 1; } ret = candev->driver->send(candev, &frame); @@ -115,12 +115,12 @@ static int _receive(int argc, char **argv) puts("Reading from Rxbuf..."); isrpipe_read(&rxbuf, (uint8_t *)&(frame.can_id), sizeof(frame.can_id)); frame.can_id &= 0x1FFFFFFF; /* clear invalid bits */ - isrpipe_read(&rxbuf, (uint8_t *)&(frame.can_dlc), 1); - printf("id: %" PRIx32 " dlc: %" PRIx8, frame.can_id, frame.can_dlc); - if (frame.can_dlc > 0) { + isrpipe_read(&rxbuf, (uint8_t *)&(frame.len), 1); + printf("id: %" PRIx32 " dlc: %" PRIx8, frame.can_id, frame.len); + if (frame.len > 0) { printf(" data: "); - isrpipe_read(&rxbuf, frame.data, frame.can_dlc); /* data */ - for (int i = 0; i < frame.can_dlc; i++) { + isrpipe_read(&rxbuf, frame.data, frame.len); /* data */ + for (int i = 0; i < frame.len; 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; DEBUG(" id: %" PRIx32 " dlc: %u data: ", frame->can_id & 0x1FFFFFFF, - frame->can_dlc); - for (uint8_t i = 0; i < frame->can_dlc; i++) { + frame->len); + for (uint8_t i = 0; i < frame->len; i++) { DEBUG("0x%X ", frame->data[i]); } DEBUG_PUTS(""); /* Store in buffer until user requests the data */ isrpipe_write(&rxbuf, (uint8_t *)&(frame->can_id), sizeof(frame->can_id)); - isrpipe_write_one(&rxbuf, frame->can_dlc); - isrpipe_write(&rxbuf, frame->data, frame->can_dlc); + isrpipe_write_one(&rxbuf, frame->len); + isrpipe_write(&rxbuf, frame->data, frame->len); break; case CANDEV_EVENT_RX_ERROR: diff --git a/tests/sys/conn_can/main.c b/tests/sys/conn_can/main.c index 2a588905d6..62e72204ac 100644 --- a/tests/sys/conn_can/main.c +++ b/tests/sys/conn_can/main.c @@ -145,22 +145,22 @@ static int _send(int argc, char **argv, bool rtr) if (rtr) { 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 { 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"); return 1; } if (rtr) { - for (int i = 0; i < frame.can_dlc; i++) { + for (int i = 0; i < frame.len; i++) { frame.data[i] = 0x0; } } 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); } } @@ -614,8 +614,8 @@ static void *_receive_thread(void *args) thread_nb, raw_can_get_name_by_ifnum(conn[thread_nb].ifnum), frame.can_id, - frame.can_dlc); - for (int i = 0; i < frame.can_dlc; i++) { + frame.len); + for (int i = 0; i < frame.len; i++) { printf(" %02X", frame.data[i]); } printf("\n");