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 <raiden00@railab.me>
This commit is contained in:
parent
6d9971296b
commit
5d95d0871f
9 changed files with 260 additions and 159 deletions
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
149
drivers/can/can_common.c
Normal file
149
drivers/can/can_common.c
Normal file
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -33,6 +33,8 @@
|
|||
#include <sys/socket.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <nuttx/can/can_common.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
92
include/nuttx/can/can_common.h
Normal file
92
include/nuttx/can/can_common.h
Normal file
|
|
@ -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 */
|
||||
Loading…
Add table
Reference in a new issue