This is an automated email from the ASF dual-hosted git repository. acassis pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 5d95d0871f6b2e14c59dd94abe1a207c57273739 Author: raiden00pl <raide...@railab.me> AuthorDate: Wed May 14 12:31:08 2025 +0200 drivers/can: move CAN utils to CAN common files Move can_bytes2dlc() and can_dlc2bytes() to a common CAN file that can be shared between socketCAN implementation and CAN character driver. This is the first step to simplifying the logic repeated in many CAN drivers. Signed-off-by: raiden00pl <raide...@railab.me> --- drivers/can/CMakeLists.txt | 6 +- drivers/can/Make.defs | 4 ++ drivers/can/can.c | 116 -------------------------------- drivers/can/can_common.c | 149 +++++++++++++++++++++++++++++++++++++++++ drivers/can/ctucanfd_pci.c | 4 +- drivers/can/kvaser_pci.c | 4 +- include/nuttx/can.h | 2 + include/nuttx/can/can.h | 42 +----------- include/nuttx/can/can_common.h | 92 +++++++++++++++++++++++++ 9 files changed, 260 insertions(+), 159 deletions(-) diff --git a/drivers/can/CMakeLists.txt b/drivers/can/CMakeLists.txt index e6d6ea3137..0dd15638b2 100644 --- a/drivers/can/CMakeLists.txt +++ b/drivers/can/CMakeLists.txt @@ -22,8 +22,12 @@ set(SRCS) +if(CONFIG_CAN OR CONFIG_NET_CAN) + list(APPEND SRCS can_common.c) +endif() + if(CONFIG_CAN) - set(SRCS can.c can_sender.c) + list(APPEND SRCS can.c can_sender.c) endif() if(CONFIG_CAN_MCP2515) diff --git a/drivers/can/Make.defs b/drivers/can/Make.defs index 1a0e16b134..b1e6a505be 100644 --- a/drivers/can/Make.defs +++ b/drivers/can/Make.defs @@ -22,6 +22,10 @@ # 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.c b/drivers/can/can.c index 628eb584ec..6465e22216 100644 --- a/drivers/can/can.c +++ b/drivers/can/can.c @@ -1547,119 +1547,3 @@ int can_txready(FAR struct can_dev_s *dev) return ret; } #endif /* CONFIG_CAN_TXREADY */ - -/**************************************************************************** - * 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/drivers/can/can_common.c b/drivers/can/can_common.c new file mode 100644 index 0000000000..9e610a4706 --- /dev/null +++ b/drivers/can/can_common.c @@ -0,0 +1,149 @@ +/**************************************************************************** + * 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/drivers/can/ctucanfd_pci.c b/drivers/can/ctucanfd_pci.c index 8f48ad4e09..954b837a36 100644 --- a/drivers/can/ctucanfd_pci.c +++ b/drivers/can/ctucanfd_pci.c @@ -35,7 +35,9 @@ #include <nuttx/kmalloc.h> #include <nuttx/pci/pci.h> -#include <nuttx/can/can.h> +#ifdef CONFIG_CAN_CTUCANFD_CHARDEV +# include <nuttx/can/can.h> +#endif #ifdef CONFIG_CAN_CTUCANFD_SOCKET # include <nuttx/wqueue.h> diff --git a/drivers/can/kvaser_pci.c b/drivers/can/kvaser_pci.c index bf19a79177..88ffcfe3e9 100644 --- a/drivers/can/kvaser_pci.c +++ b/drivers/can/kvaser_pci.c @@ -35,7 +35,9 @@ #include <nuttx/kmalloc.h> #include <nuttx/pci/pci.h> -#include <nuttx/can/can.h> +#ifdef CONFIG_CAN_KVASER_CHARDEV +# include <nuttx/can/can.h> +#endif #ifdef CONFIG_CAN_KVASER_SOCKET # include <nuttx/wqueue.h> diff --git a/include/nuttx/can.h b/include/nuttx/can.h index 1b2f11e5b2..bcb17d08fb 100644 --- a/include/nuttx/can.h +++ b/include/nuttx/can.h @@ -33,6 +33,8 @@ #include <sys/socket.h> #include <stdint.h> +#include <nuttx/can/can_common.h> + /**************************************************************************** * Pre-processor Definitions ****************************************************************************/ diff --git a/include/nuttx/can/can.h b/include/nuttx/can/can.h index 1a82ca3560..c9100d5a96 100644 --- a/include/nuttx/can/can.h +++ b/include/nuttx/can/can.h @@ -40,6 +40,8 @@ #include <nuttx/fs/ioctl.h> #include <nuttx/mutex.h> +#include <nuttx/can/can_common.h> + #ifdef CONFIG_CAN_TXREADY # include <nuttx/wqueue.h> #endif @@ -1090,46 +1092,6 @@ int can_txdone(FAR struct can_dev_s *dev); int can_txready(FAR struct can_dev_s *dev); #endif -/**************************************************************************** - * 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); - -/**************************************************************************** - * 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); - #undef EXTERN #if defined(__cplusplus) } diff --git a/include/nuttx/can/can_common.h b/include/nuttx/can/can_common.h new file mode 100644 index 0000000000..ed89b7f2be --- /dev/null +++ b/include/nuttx/can/can_common.h @@ -0,0 +1,92 @@ +/**************************************************************************** + * include/nuttx/can/can_common.h + * + * 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. + * + ****************************************************************************/ + +#ifndef __INCLUDE_NUTTX_CAN_CAN_COMMON_H +#define __INCLUDE_NUTTX_CAN_CAN_COMMON_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +#include <stdint.h> + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +#undef EXTERN +#if defined(__cplusplus) +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +/**************************************************************************** + * 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); + +/**************************************************************************** + * 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); + +#undef EXTERN +#if defined(__cplusplus) +} +#endif + +#endif /* __INCLUDE_NUTTX_CAN_CAN_COMMON_H */