can: enhance API to allow better CAN FD handling

This commit moves can_bytes2dlc and can_dlc2bytes from kernel internal
functions to API. These functions are necessary to convert bytes to
dlc for CAN FD frames and has to be accessible from the application
since can_hdr_s does not store message length in bytes but directly in
dlc.

Signed-off-by: Michal Lenc <michallenc@seznam.cz>
This commit is contained in:
Michal Lenc 2024-07-08 16:08:59 +02:00 committed by Alan Carvalho de Assis
parent 80c37c7b36
commit 816b1b28a0
2 changed files with 157 additions and 124 deletions

View File

@ -96,12 +96,6 @@
* Private Function Prototypes
****************************************************************************/
/* CAN helpers */
static uint8_t can_dlc2bytes(uint8_t dlc);
#if 0 /* Not used */
static uint8_t can_bytes2dlc(uint8_t nbytes);
#endif
#ifdef CONFIG_CAN_TXREADY
static void can_txready_work(FAR void *arg);
#endif
@ -144,124 +138,6 @@ static const struct file_operations g_canops =
* Private Functions
****************************************************************************/
/****************************************************************************
* 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.
*
****************************************************************************/
static 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;
}
/****************************************************************************
* 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.
*
****************************************************************************/
#if 0 /* Not used */
static uint8_t can_bytes2dlc(FAR struct sam_can_s *priv, 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
}
#endif
/****************************************************************************
* Name: can_txready_work
*
@ -1694,4 +1570,121 @@ 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;
}
#endif /* CONFIG_CAN */

View File

@ -936,6 +936,46 @@ 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)
}