From 71454bd90c54c9bbf62cc939030e4d69ddd53f4f Mon Sep 17 00:00:00 2001 From: rishabsingh3003 Date: Thu, 19 Nov 2026 09:29:18 +0000 Subject: [PATCH 1/3] drivers: fixed linux driver build with CAN FD enabled LinuxCANInit passed can_fd to the mcast and socketcan init functions, but the parameter is named canfd, so the driver failed to compile with CANARD_ENABLE_CANFD=1 --- drivers/linux/linux.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/linux/linux.c b/drivers/linux/linux.c index 7ece084..b786f58 100644 --- a/drivers/linux/linux.c +++ b/drivers/linux/linux.c @@ -26,7 +26,7 @@ int16_t LinuxCANInit(LinuxCANInstance* out_ins, const char* can_iface_name) return -ENOMEM; } #if CANARD_ENABLE_CANFD - return mcastInit(out_ins->mcast, can_iface_name, can_fd); + return mcastInit(out_ins->mcast, can_iface_name, canfd); #else return mcastInit(out_ins->mcast, can_iface_name); #endif @@ -37,7 +37,7 @@ int16_t LinuxCANInit(LinuxCANInstance* out_ins, const char* can_iface_name) return -ENOMEM; } #if CANARD_ENABLE_CANFD - return socketcanInit(out_ins->socketcan, can_iface_name, can_fd); + return socketcanInit(out_ins->socketcan, can_iface_name, canfd); #else return socketcanInit(out_ins->socketcan, can_iface_name); #endif From 6e86dd74ad25b736497686377d6a310c9a2e03ab Mon Sep 17 00:00:00 2001 From: rishabsingh3003 Date: Thu, 19 Nov 2026 09:29:18 +0000 Subject: [PATCH 2/3] socketcan: fixed receive with CAN FD enabled The canfd flag of a received frame was never set, so FD transfers were decoded using the classic CAN tail array optimisation rules. A CAN FD socket also delivers classic frames, which are shorter than struct canfd_frame. These were rejected with -EIO, which stopped a node from receiving classic frames from other nodes on the bus. --- drivers/socketcan/socketcan.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/drivers/socketcan/socketcan.c b/drivers/socketcan/socketcan.c index 6ca3f4f..a62e052 100644 --- a/drivers/socketcan/socketcan.c +++ b/drivers/socketcan/socketcan.c @@ -208,10 +208,12 @@ int16_t socketcanReceive(SocketCANInstance* ins, CanardCANFrame* out_frame, int3 { return getErrorCode(); } - if ((size_t)nbytes != sizeof(receive_frame)) + // classic frames are also received on a CAN FD socket + if ((size_t)nbytes != CAN_MTU && (size_t)nbytes != CANFD_MTU) { return -EIO; } + out_frame->canfd = ((size_t)nbytes == CANFD_MTU); if (receive_frame.len > CANFD_MAX_DLEN) // Appeasing Coverity Scan { @@ -244,6 +246,9 @@ int16_t socketcanReceive(SocketCANInstance* ins, CanardCANFrame* out_frame, int3 out_frame->id = receive_frame.can_id; // TODO: Map flags properly out_frame->data_len = receive_frame.can_dlc; memcpy(out_frame->data, &receive_frame.data, receive_frame.can_dlc); +#if CANARD_ENABLE_CANFD + out_frame->canfd = false; +#endif } // assume a single interface From 83eee190ebfcd29b57517a51b9a34b0c1dd3ce88 Mon Sep 17 00:00:00 2001 From: rishabsingh3003 Date: Thu, 19 Nov 2026 09:29:18 +0000 Subject: [PATCH 3/3] examples: added CAN FD support to ServoNode Adding canfd as a second argument sends all transfers as CAN FD frames. Without it the behaviour is unchanged. When sending CAN FD the messages are encoded without tail array optimisation. A CAN FD frame is padded to the next valid length, so the receiver cannot work out the length of a trailing array from the transfer length, and DroneCAN GUI Tool fails to decode the transfer with "Not enough bits". Node ID allocation requests are still sent as classic CAN, as the request is hand-encoded for 8 byte frames. The memory pool is larger as the block size goes from 32 to 128 bytes when CAN FD is enabled. With the old 1024 byte pool a GetNodeInfo response no longer fits in the transmit queue when sending classic CAN. --- examples/ServoNode/Makefile | 2 +- examples/ServoNode/servo_node.c | 52 +++++++++++++++++++++------------ 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/examples/ServoNode/Makefile b/examples/ServoNode/Makefile index 59bd748..9559b05 100644 --- a/examples/ServoNode/Makefile +++ b/examples/ServoNode/Makefile @@ -2,7 +2,7 @@ CANARD_BASE=../.. CC=gcc -CFLAGS = -g -Wall -Idsdl_generated/include -I$(CANARD_BASE) -I$(CANARD_BASE)/drivers/linux +CFLAGS = -g -Wall -DCANARD_ENABLE_CANFD=1 -Idsdl_generated/include -I$(CANARD_BASE) -I$(CANARD_BASE)/drivers/linux LIBS=$(CANARD_BASE)/canard.c diff --git a/examples/ServoNode/servo_node.c b/examples/ServoNode/servo_node.c index 501cddb..419bace 100644 --- a/examples/ServoNode/servo_node.c +++ b/examples/ServoNode/servo_node.c @@ -13,6 +13,7 @@ This example uses socketcan on Linux for CAN transport Example usage: ./servo_node vcan0 + For CAN FD: ./servo_node vcan0 canfd */ /* This example application is distributed under the terms of CC0 (public domain dedication). @@ -46,7 +47,13 @@ libcanard library instance and a memory pool for it to use */ static CanardInstance canard; -static uint8_t memory_pool[1024]; +static uint8_t memory_pool[4096]; + +/* + send as CAN FD frames. Tail array optimisation must not be used when + encoding for CAN FD, as the frame is padded + */ +static bool canfd; /* in this example we will use dynamic node allocation if MY_NODE_ID is zero @@ -175,7 +182,7 @@ static void handle_GetNodeInfo(CanardInstance *ins, CanardRxTransfer *transfer) strncpy((char*)pkt.name.data, "ServoNode", sizeof(pkt.name.data)); pkt.name.len = strnlen((char*)pkt.name.data, sizeof(pkt.name.data)); - uint16_t total_size = uavcan_protocol_GetNodeInfoResponse_encode(&pkt, buffer); + uint16_t total_size = uavcan_protocol_GetNodeInfoResponse_encode(&pkt, buffer, !canfd); canardRequestOrRespond(ins, transfer->source_node_id, @@ -185,7 +192,8 @@ static void handle_GetNodeInfo(CanardInstance *ins, CanardRxTransfer *transfer) transfer->priority, CanardResponse, &buffer[0], - total_size); + total_size, + canfd); } /* @@ -281,7 +289,7 @@ static void handle_param_GetSet(CanardInstance* ins, CanardRxTransfer* transfer) } uint8_t buffer[UAVCAN_PROTOCOL_PARAM_GETSET_RESPONSE_MAX_SIZE]; - uint16_t total_size = uavcan_protocol_param_GetSetResponse_encode(&pkt, buffer); + uint16_t total_size = uavcan_protocol_param_GetSetResponse_encode(&pkt, buffer, !canfd); canardRequestOrRespond(ins, transfer->source_node_id, @@ -291,7 +299,8 @@ static void handle_param_GetSet(CanardInstance* ins, CanardRxTransfer* transfer) transfer->priority, CanardResponse, &buffer[0], - total_size); + total_size, + canfd); } /* @@ -316,7 +325,7 @@ static void handle_param_ExecuteOpcode(CanardInstance* ins, CanardRxTransfer* tr pkt.ok = true; uint8_t buffer[UAVCAN_PROTOCOL_PARAM_EXECUTEOPCODE_RESPONSE_MAX_SIZE]; - uint16_t total_size = uavcan_protocol_param_ExecuteOpcodeResponse_encode(&pkt, buffer); + uint16_t total_size = uavcan_protocol_param_ExecuteOpcodeResponse_encode(&pkt, buffer, !canfd); canardRequestOrRespond(ins, transfer->source_node_id, @@ -326,7 +335,8 @@ static void handle_param_ExecuteOpcode(CanardInstance* ins, CanardRxTransfer* tr transfer->priority, CanardResponse, &buffer[0], - total_size); + total_size, + canfd); } /* @@ -429,7 +439,8 @@ static void request_DNA() &node_id_allocation_transfer_id, CANARD_TRANSFER_PRIORITY_LOW, &allocation_request[0], - (uint16_t) (uid_size + 1)); + (uint16_t) (uid_size + 1), + false); if (bcast_res < 0) { printf("Could not broadcast ID allocation req; error %d\n", bcast_res); } @@ -511,7 +522,7 @@ static void handle_begin_firmware_update(CanardInstance* ins, CanardRxTransfer* memset(&reply, 0, sizeof(reply)); reply.error = UAVCAN_PROTOCOL_FILE_BEGINFIRMWAREUPDATE_RESPONSE_ERROR_OK; - uint32_t total_size = uavcan_protocol_file_BeginFirmwareUpdateResponse_encode(&reply, buffer); + uint32_t total_size = uavcan_protocol_file_BeginFirmwareUpdateResponse_encode(&reply, buffer, !canfd); canardRequestOrRespond(ins, transfer->source_node_id, @@ -521,7 +532,8 @@ static void handle_begin_firmware_update(CanardInstance* ins, CanardRxTransfer* transfer->priority, CanardResponse, &buffer[0], - total_size); + total_size, + canfd); printf("Started firmware update\n"); @@ -552,7 +564,7 @@ static void send_firmware_read(void) pkt.offset = fwupdate.offset; memcpy(pkt.path.path.data, fwupdate.path, pkt.path.path.len); - uint16_t total_size = uavcan_protocol_file_ReadRequest_encode(&pkt, buffer); + uint16_t total_size = uavcan_protocol_file_ReadRequest_encode(&pkt, buffer, !canfd); canardRequestOrRespond(&canard, fwupdate.node_id, @@ -562,7 +574,8 @@ static void send_firmware_read(void) CANARD_TRANSFER_PRIORITY_HIGH, CanardRequest, &buffer[0], - total_size); + total_size, + canfd); } /* @@ -737,7 +750,7 @@ static void send_NodeStatus(void) node_status.mode = UAVCAN_PROTOCOL_NODESTATUS_MODE_SOFTWARE_UPDATE; } - uint32_t len = uavcan_protocol_NodeStatus_encode(&node_status, buffer); + uint32_t len = uavcan_protocol_NodeStatus_encode(&node_status, buffer, !canfd); // we need a static variable for the transfer ID. This is // incremeneted on each transfer, allowing for detection of packet @@ -750,7 +763,8 @@ static void send_NodeStatus(void) &transfer_id, CANARD_TRANSFER_PRIORITY_LOW, buffer, - len); + len, + canfd); } /* @@ -787,7 +801,7 @@ static void send_ServoStatus(void) pkt.speed = 0.12; // m/s or rad/s pkt.power_rating_pct = 17; - uint32_t len = uavcan_equipment_actuator_Status_encode(&pkt, buffer); + uint32_t len = uavcan_equipment_actuator_Status_encode(&pkt, buffer, !canfd); // we need a static variable for the transfer ID. This is // incremeneted on each transfer, allowing for detection of packet @@ -800,7 +814,8 @@ static void send_ServoStatus(void) &transfer_id, CANARD_TRANSFER_PRIORITY_LOW, buffer, - len); + len, + canfd); } } @@ -848,19 +863,20 @@ int main(int argc, char** argv) if (argc < 2) { (void)fprintf(stderr, "Usage:\n" - "\t%s \n", + "\t%s [canfd]\n", argv[0]); return 1; } fwupdate.fd = -1; + canfd = argc > 2 && strcmp(argv[2], "canfd") == 0; /* * Initializing the CAN backend driver */ LinuxCANInstance can; const char* const can_iface_name = argv[1]; - int16_t res = LinuxCANInit(&can, can_iface_name); + int16_t res = LinuxCANInit(&can, can_iface_name, canfd); if (res < 0) { (void)fprintf(stderr, "Failed to open CAN iface '%s'\n", can_iface_name); return 1;