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 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 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;