Skip to content

Commit

Permalink
can : move bytes2dlc/dlc2bytes to common header file
Browse files Browse the repository at this point in the history
only keep can_bytes2dlc & can_dlc2bytes

Signed-off-by: xucheng5 <[email protected]>
  • Loading branch information
xucheng5 committed Nov 13, 2024
1 parent 8068ba7 commit fb49b6e
Show file tree
Hide file tree
Showing 13 changed files with 144 additions and 281 deletions.
12 changes: 6 additions & 6 deletions Documentation/components/net/socketcan.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ SocketCAN Device Drivers
The structure struct net_driver_s defines the interface and is
passed to the network via ``netdev_register()``.

- ``include/nuttx/can.h``. CAN & CAN FD frame data structures.
- ``include/nuttx/can.h``. CAN & CAN FD frame data structures and dlc to len size

.. code-block:: c
uint8_t can_bytes2dlc(uint8_t nbytes);
uint8_t can_dlc2bytes(uint8_t dlc);
- ``int netdev_register(FAR struct net_driver_s *dev, enum net_lltype_e lltype)'``.
Each driver registers itself by calling ``netdev_register()``.

- ``Include/nuttx/net/can.h``. contains lookup tables for CAN
dlc to CAN FD len sizes named

.. code-block:: c
extern const uint8_t g_can_dlc_to_len[16];
extern const uint8_t g_len_to_can_dlc[65];
- **Initialization sequence is as follows**.

#. ``xxx_netinitialize(void)`` is called on startup of NuttX in this
Expand Down
4 changes: 2 additions & 2 deletions arch/arm/src/imxrt/imxrt_flexcan.c
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ static int imxrt_transmit(struct imxrt_driver_s *priv)

cs.rtr = frame->can_id & FLAGRTR ? 1 : 0;

cs.dlc = g_len_to_can_dlc[frame->len];
cs.dlc = can_bytes2dlc(frame->len);

frame_data_word = (uint32_t *)&frame->data[0];

Expand Down Expand Up @@ -877,7 +877,7 @@ static void imxrt_receive(struct imxrt_driver_s *priv,
frame->can_id |= FLAGRTR;
}

frame->len = g_can_dlc_to_len[rf->cs.dlc];
frame->len = can_dlc2bytes(rf->cs.dlc);

frame_data_word = (uint32_t *)&frame->data[0];

Expand Down
4 changes: 2 additions & 2 deletions arch/arm/src/kinetis/kinetis_flexcan.c
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ static int kinetis_transmit(struct kinetis_driver_s *priv)

cs.rtr = frame->can_id & FLAGRTR ? 1 : 0;

cs.dlc = g_len_to_can_dlc[frame->len];
cs.dlc = can_bytes2dlc(frame->len);

frame_data_word = (uint32_t *)&frame->data[0];

Expand Down Expand Up @@ -877,7 +877,7 @@ static void kinetis_receive(struct kinetis_driver_s *priv,
frame->can_id |= FLAGRTR;
}

frame->len = g_can_dlc_to_len[rf->cs.dlc];
frame->len = can_dlc2bytes(rf->cs.dlc);

frame_data_word = (uint32_t *)&frame->data[0];

Expand Down
4 changes: 2 additions & 2 deletions arch/arm/src/s32k1xx/s32k1xx_flexcan.c
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ static int s32k1xx_transmit(struct s32k1xx_driver_s *priv)

cs.rtr = frame->can_id & FLAGRTR ? 1 : 0;

cs.dlc = g_len_to_can_dlc[frame->len];
cs.dlc = can_bytes2dlc(frame->len);

frame_data_word = (uint32_t *)&frame->data[0];

Expand Down Expand Up @@ -881,7 +881,7 @@ static void s32k1xx_receive(struct s32k1xx_driver_s *priv,
frame->can_id |= FLAGRTR;
}

frame->len = g_can_dlc_to_len[rf->cs.dlc];
frame->len = can_dlc2bytes(rf->cs.dlc);

frame_data_word = (uint32_t *)&frame->data[0];

Expand Down
4 changes: 2 additions & 2 deletions arch/arm/src/s32k3xx/s32k3xx_flexcan.c
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ static int s32k3xx_transmit(struct s32k3xx_driver_s *priv)

cs.rtr = frame->can_id & FLAGRTR ? 1 : 0;

cs.dlc = g_len_to_can_dlc[frame->len];
cs.dlc = can_bytes2dlc(frame->len);

frame_data_word = (uint32_t *)&frame->data[0];

Expand Down Expand Up @@ -1041,7 +1041,7 @@ static void s32k3xx_receive(struct s32k3xx_driver_s *priv, uint32_t flags)
frame->can_id |= FLAGRTR;
}

frame->len = g_can_dlc_to_len[rf->cs.dlc];
frame->len = can_dlc2bytes(rf->cs.dlc);

frame_data_word = (uint32_t *)&frame->data[0];

Expand Down
6 changes: 3 additions & 3 deletions arch/arm/src/stm32/stm32_fdcan_sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -1749,7 +1749,7 @@ static int fdcan_send(struct stm32_fdcan_s *priv)

/* Set DLC */

txbuffer[1] |= BUFFER_R1_DLC(g_len_to_can_dlc[frame->len]);
txbuffer[1] |= BUFFER_R1_DLC(can_bytes2dlc(frame->len));

/* Set flags */

Expand Down Expand Up @@ -2467,8 +2467,8 @@ static void fdcan_receive(struct stm32_fdcan_s *priv,

/* Word R1 contains the DLC and timestamp */

frame->len = g_can_dlc_to_len[((rxbuffer[1] & BUFFER_R1_DLC_MASK) >>
BUFFER_R1_DLC_SHIFT)];
frame->len = can_dlc2bytes(((rxbuffer[1] & BUFFER_R1_DLC_MASK) >>
BUFFER_R1_DLC_SHIFT));

/* Get CANFD flags */

Expand Down
4 changes: 2 additions & 2 deletions arch/arm/src/stm32h7/stm32_fdcan_sock.c
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ static int fdcan_transmit(struct fdcan_driver_s *priv)

header.id.esi = (frame->can_id & CAN_ERR_FLAG) ? 1 : 0;
header.id.rtr = (frame->can_id & CAN_RTR_FLAG) ? 1 : 0;
header.dlc = g_len_to_can_dlc[frame->len];
header.dlc = can_bytes2dlc(frame->len);
header.brs = brs; /* Bitrate switching */
header.fdf = 1; /* CAN-FD frame */
header.efc = 0; /* Don't store Tx events */
Expand Down Expand Up @@ -1161,7 +1161,7 @@ static void fdcan_receive_work(void *arg)
frame->can_id |= CAN_RTR_FLAG;
}

frame->len = g_can_dlc_to_len[rf->header.dlc];
frame->len = can_dlc2bytes(rf->header.dlc);

uint32_t *frame_data_word = (uint32_t *)&frame->data[0];

Expand Down
4 changes: 2 additions & 2 deletions arch/arm64/src/imx9/imx9_flexcan.c
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ static int imx9_transmit(struct imx9_driver_s *priv)
cs |= frame->flags & CANFD_BRS ? CAN_MB_CS_BRS : 0;
can_id = frame->can_id;
len = frame->len;
can_dlc = g_len_to_can_dlc[len];
can_dlc = can_bytes2dlc(len);
frame_data_word = (uint32_t *)&frame->data[0];
}
#endif
Expand Down Expand Up @@ -940,7 +940,7 @@ static void imx9_receive(struct imx9_driver_s *priv)
frame->flags |= CANFD_ESI;
}

frame->len = g_can_dlc_to_len[CAN_MB_CS_DLC(rf->cs)];
frame->len = can_dlc2bytes(CAN_MB_CS_DLC(rf->cs));

frame_data_word = (uint32_t *)&frame->data[0];

Expand Down
118 changes: 1 addition & 117 deletions drivers/can/can.c
Original file line number Diff line number Diff line change
Expand Up @@ -1538,120 +1538,4 @@ int can_txready(FAR struct can_dev_s *dev)
leave_critical_section(flags);
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_TXREADY */
Loading

0 comments on commit fb49b6e

Please sign in to comment.