Skip to content

Commit

Permalink
net: can: Added a helper function to adjust the Data Length Code of a…
Browse files Browse the repository at this point in the history
… CAN-FD frame.

Signed-off-by: Adrien Ricciardi <aricciardi@baylibre.com>
  • Loading branch information
RICCIARDI-Adrien committed Oct 6, 2023
1 parent a3d4522 commit 4161018
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
15 changes: 15 additions & 0 deletions net/can/Can.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,19 @@ Std_ReturnType Can_SetBaudrate(uint8 Controller, uint16 BaudRateConfigID);
*/
Std_ReturnType Can_Write(Can_HwHandleType Hth, const Can_PduType *PduInfo);

/**
* Convert a CAN-FD payload size in bytes to the corresponding CAN Data Length
* Code.
*
* @param length The payload length in bytes.
* @param adjusted_length If not NULL, contain on output the payload adjusted
* length in bytes.
*
* @retval 0 if the provided payload length exceeds 64 bytes,
* @retval The DLC code if the provided payload length is valid.
*
* @note See https://www.can-cia.org/can-knowledge/can/can-fd for more details.
*/
uint32 tpl_can_adjust_dlc(uint32 length, uint32 *adjusted_length);

#endif
3 changes: 3 additions & 0 deletions net/can/Can_GeneralTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@
#define TPL_CAN_ID_TYPE_GET(id) ((id & TPL_CAN_ID_TYPE_MASK) >> 30)
#define TPL_CAN_ID_MASK (0x3FFFFFFF)

#define TPL_CAN_CLASSIC_FRAME_MAXIMUM_PAYLOAD_SIZE (8)
#define TPL_CAN_FD_FRAME_MAXIMUM_PAYLOAD_SIZE (64)

/**
* @typedef tpl_can_protocol_version_t
*
Expand Down
33 changes: 33 additions & 0 deletions net/can/tpl_can_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ Std_ReturnType Can_Write(Can_HwHandleType Hth, const Can_PduType *PduInfo)

if (controller == NULL)
return E_NOT_OK;
if (PduInfo == NULL)
return E_NOT_OK;

// TODO check if CAN FD enabled for CAN FD frame, otherwise send as CAN classic if SDU < 8 (see parag 7.12 "CAN FD Support" in AUTOSAR_SWS_CANDriver R22-11)

// No need to check for the transmit callback presence, this has already been done by Can_Init()
ret = controller->transmit(controller, PduInfo);
Expand Down Expand Up @@ -145,3 +149,32 @@ Std_ReturnType CanIf_ReadRxPduData(PduIdType CanIfRxSduId, PduInfoType *CanIfRxI
can_pdu = (Can_PduType *) CanIfRxInfoPtr->SduDataPtr;
return controller->receive(controller, can_pdu);
}

uint32 tpl_can_adjust_dlc(uint32 length, uint32 *adjusted_length)
{
struct length_code_t
{
uint32 dlc;
uint32 adjusted_length;
};
static struct length_code_t length_code_table[TPL_CAN_FD_FRAME_MAXIMUM_PAYLOAD_SIZE + 1] =
{
{ 0x0, 0 }, { 0x1, 1 }, { 0x2, 2 }, { 0x3, 3 }, { 0x4, 4 }, { 0x5, 5 }, { 0x6, 6 }, { 0x7, 7 },
{ 0x8, 8 }, { 0x9, 12 }, { 0x9, 12 }, { 0x9, 12 }, { 0x9, 12 }, { 0xA, 16 }, { 0xA, 16 }, { 0xA, 16 },
{ 0xA, 16 }, { 0xB, 20 }, { 0xB, 20 }, { 0xB, 20 }, { 0xB, 20 }, { 0xC, 24 }, { 0xC, 24 }, { 0xC, 24 },
{ 0xC, 24 }, { 0xD, 32 }, { 0xD, 32 }, { 0xD, 32 }, { 0xD, 32 }, { 0xD, 32 }, { 0xD, 32 }, { 0xD, 32 },
{ 0xD, 32 }, { 0xE, 48 }, { 0xE, 48 }, { 0xE, 48 }, { 0xE, 48 }, { 0xE, 48 }, { 0xE, 48 }, { 0xE, 48 },
{ 0xE, 48 }, { 0xE, 48 }, { 0xE, 48 }, { 0xE, 48 }, { 0xE, 48 }, { 0xE, 48 }, { 0xE, 48 }, { 0xE, 48 },
{ 0xE, 48 }, { 0xF, 64 }, { 0xF, 64 }, { 0xF, 64 }, { 0xF, 64 }, { 0xF, 64 }, { 0xF, 64 }, { 0xF, 64 },
{ 0xF, 64 }, { 0xF, 64 }, { 0xF, 64 }, { 0xF, 64 }, { 0xF, 64 }, { 0xF, 64 }, { 0xF, 64 }, { 0xF, 64 },
{ 0xF, 64 }
};

if (length > TPL_CAN_FD_FRAME_MAXIMUM_PAYLOAD_SIZE)
return 0;

if (adjusted_length != NULL)
*adjusted_length = length_code_table[length].adjusted_length;

return length_code_table[length].dlc;
}

0 comments on commit 4161018

Please sign in to comment.