This is an automated email from the ASF dual-hosted git repository. jerpelea pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit dc9481383ed383ff33e6d61089e4f90aab3c0eee Author: wangjinjing1 <[email protected]> AuthorDate: Wed Jan 21 18:06:21 2026 +0800 drivers/can: make can_bytes2dlc() and can_dlc2bytes() inline. Move can_bytes2dlc() and can_dlc2bytes() from drivers/can/can_common.c to static inline functions in <nuttx/can/can_common.h> and remove can_common.c. The CAN FD lengths are now also used when only SocketCAN FD (CONFIG_NET_CAN_CANFD) is enabled; before, such a configuration mapped can_bytes2dlc(64) to 8. Assisted-by: Claude:claude-opus-5-5 Signed-off-by: wangjinjing1 <[email protected]> --- drivers/can/CMakeLists.txt | 4 -- drivers/can/Make.defs | 4 -- drivers/can/can_common.c | 149 ----------------------------------------- include/nuttx/can/can_common.h | 84 ++++++++++++++++++++++- 4 files changed, 82 insertions(+), 159 deletions(-) diff --git a/drivers/can/CMakeLists.txt b/drivers/can/CMakeLists.txt index 0dd15638b23..937375f64a8 100644 --- a/drivers/can/CMakeLists.txt +++ b/drivers/can/CMakeLists.txt @@ -22,10 +22,6 @@ set(SRCS) -if(CONFIG_CAN OR CONFIG_NET_CAN) - list(APPEND SRCS can_common.c) -endif() - if(CONFIG_CAN) list(APPEND SRCS can.c can_sender.c) endif() diff --git a/drivers/can/Make.defs b/drivers/can/Make.defs index b1e6a505be3..1a0e16b1341 100644 --- a/drivers/can/Make.defs +++ b/drivers/can/Make.defs @@ -22,10 +22,6 @@ # Don't build anything if there is no CAN support -ifneq ($(CONFIG_CAN)$(CONFIG_NET_CAN),) -CSRCS += can_common.c -endif - ifeq ($(CONFIG_CAN),y) CSRCS += can.c can_sender.c endif diff --git a/drivers/can/can_common.c b/drivers/can/can_common.c deleted file mode 100644 index 9e610a47064..00000000000 --- a/drivers/can/can_common.c +++ /dev/null @@ -1,149 +0,0 @@ -/**************************************************************************** - * drivers/can/can_common.c - * - * SPDX-License-Identifier: Apache-2.0 - * - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. The - * ASF licenses this file to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance with the - * License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the - * License for the specific language governing permissions and limitations - * under the License. - * - ****************************************************************************/ - -/**************************************************************************** - * Included Files - ****************************************************************************/ - -#include <nuttx/config.h> - -#include <nuttx/can/can_common.h> - -/**************************************************************************** - * Public Functions - ****************************************************************************/ - -/**************************************************************************** - * Name: can_bytes2dlc - * - * Description: - * In the CAN FD format, the coding of the DLC differs from the standard - * CAN format. The DLC codes 0 to 8 have the same coding as in standard - * CAN. But the codes 9 to 15 all imply a data field of 8 bytes with - * standard CAN. In CAN FD mode, the values 9 to 15 are encoded to values - * in the range 12 to 64. - * - * Input Parameters: - * nbytes - the byte count to convert to a DLC value - * - * Returned Value: - * The encoded DLC value corresponding to at least that number of bytes. - * - ****************************************************************************/ - -uint8_t can_bytes2dlc(uint8_t nbytes) -{ - if (nbytes <= 8) - { - return nbytes; - } -#ifdef CONFIG_CAN_FD - else if (nbytes <= 12) - { - return 9; - } - else if (nbytes <= 16) - { - return 10; - } - else if (nbytes <= 20) - { - return 11; - } - else if (nbytes <= 24) - { - return 12; - } - else if (nbytes <= 32) - { - return 13; - } - else if (nbytes <= 48) - { - return 14; - } - else /* if (nbytes <= 64) */ - { - return 15; - } -#else - else - { - return 8; - } -#endif -} - -/**************************************************************************** - * Name: can_dlc2bytes - * - * Description: - * In the CAN FD format, the coding of the DLC differs from the standard - * CAN format. The DLC codes 0 to 8 have the same coding as in standard - * CAN. But the codes 9 to 15 all imply a data field of 8 bytes with - * standard CAN. In CAN FD mode, the values 9 to 15 are encoded to values - * in the range 12 to 64. - * - * Input Parameters: - * dlc - the DLC value to convert to a byte count - * - * Returned Value: - * The number of bytes corresponding to the DLC value. - * - ****************************************************************************/ - -uint8_t can_dlc2bytes(uint8_t dlc) -{ - if (dlc > 8) - { -#ifdef CONFIG_CAN_FD - switch (dlc) - { - case 9: - return 12; - - case 10: - return 16; - - case 11: - return 20; - - case 12: - return 24; - - case 13: - return 32; - - case 14: - return 48; - - default: - case 15: - return 64; - } -#else - return 8; -#endif - } - - return dlc; -} diff --git a/include/nuttx/can/can_common.h b/include/nuttx/can/can_common.h index 7f1174c5caa..8403c4a22a3 100644 --- a/include/nuttx/can/can_common.h +++ b/include/nuttx/can/can_common.h @@ -40,6 +40,10 @@ extern "C" { #endif +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + /**************************************************************************** * Name: can_bytes2dlc * @@ -58,7 +62,48 @@ extern "C" * ****************************************************************************/ -uint8_t can_bytes2dlc(uint8_t nbytes); +static inline uint8_t can_bytes2dlc(uint8_t nbytes) +{ + if (nbytes <= 8) + { + return nbytes; + } +#if defined(CONFIG_CAN_FD) || defined(CONFIG_NET_CAN_CANFD) + else if (nbytes <= 12) + { + return 9; + } + else if (nbytes <= 16) + { + return 10; + } + else if (nbytes <= 20) + { + return 11; + } + else if (nbytes <= 24) + { + return 12; + } + else if (nbytes <= 32) + { + return 13; + } + else if (nbytes <= 48) + { + return 14; + } + else /* if (nbytes <= 64) */ + { + return 15; + } +#else + else + { + return 8; + } +#endif +} /**************************************************************************** * Name: can_dlc2bytes @@ -78,7 +123,42 @@ uint8_t can_bytes2dlc(uint8_t nbytes); * ****************************************************************************/ -uint8_t can_dlc2bytes(uint8_t dlc); +static inline uint8_t can_dlc2bytes(uint8_t dlc) +{ + if (dlc > 8) + { +#if defined(CONFIG_CAN_FD) || defined(CONFIG_NET_CAN_CANFD) + switch (dlc) + { + case 9: + return 12; + + case 10: + return 16; + + case 11: + return 20; + + case 12: + return 24; + + case 13: + return 32; + + case 14: + return 48; + + default: + case 15: + return 64; + } +#else + return 8; +#endif + } + + return dlc; +} #if defined(__cplusplus) }
