drivers/ctucanfd_pci.c: fix pointer unaligned GCC error

fix an unaligned pointer error for arm32 build:

  ctucanfd_pci.c:1361:7: warning: converting a packed 'struct ctucanfd_frame_s'
  pointer (alignment 1) to a 'uint32_t' {aka 'long unsigned int'} pointer (alignment 4)
  may result in an unaligned pointer value [-Waddress-of-packed-member]
   1361 |       ptr = (FAR uint32_t *)&rxframe;

Signed-off-by: p-szafonimateusz <p-szafonimateusz@xiaomi.com>
This commit is contained in:
p-szafonimateusz
2025-05-14 13:28:21 +02:00
committed by Xiang Xiao
parent ea8744631a
commit ce99762b36
+59 -51
View File
@@ -742,14 +742,14 @@ static bool ctucanfd_chrdev_txempty(FAR struct can_dev_s *dev)
static void ctucanfd_chardev_receive(FAR struct ctucanfd_can_s *priv) static void ctucanfd_chardev_receive(FAR struct ctucanfd_can_s *priv)
{ {
FAR struct can_dev_s *dev = (FAR struct can_dev_s *)priv; FAR struct can_dev_s *dev = (FAR struct can_dev_s *)priv;
FAR uint32_t *ptr = NULL; FAR struct ctucanfd_frame_s *frame;
struct ctucanfd_frame_s frame; struct can_hdr_s hdr;
struct can_hdr_s hdr; uint32_t buff[sizeof(struct ctucanfd_frame_s) / 4];
int i = 0; int i = 0;
int ret = 0; int ret = 0;
uint16_t frc = 0; uint16_t frc = 0;
uint32_t regval = 0; uint32_t regval = 0;
/* Get frame count */ /* Get frame count */
@@ -760,38 +760,42 @@ static void ctucanfd_chardev_receive(FAR struct ctucanfd_can_s *priv)
while (frc-- > 0) while (frc-- > 0)
{ {
ptr = (FAR uint32_t *)&frame; /* We use a pointer to buffer to avoid an unaligned pointer
* compiler errors
*/
frame = (struct ctucanfd_frame_s *)&buff;
for (i = 0; i < sizeof(struct ctucanfd_frame_s) / 4; i++) for (i = 0; i < sizeof(struct ctucanfd_frame_s) / 4; i++)
{ {
/* RX buffer in automatic mode */ /* RX buffer in automatic mode */
ptr[i] = ctucanfd_getreg(priv, CTUCANFD_RXDATA); buff[i] = ctucanfd_getreg(priv, CTUCANFD_RXDATA);
} }
/* Get the DLC */ /* Get the DLC */
hdr.ch_dlc = frame.fmt.dlc; hdr.ch_dlc = frame->fmt.dlc;
/* Get RTR bit */ /* Get RTR bit */
hdr.ch_rtr = frame.fmt.rtr; hdr.ch_rtr = frame->fmt.rtr;
#ifdef CONFIG_CAN_EXTID #ifdef CONFIG_CAN_EXTID
/* Get the CAN identifier. */ /* Get the CAN identifier. */
hdr.ch_extid = frame.fmt.ide; hdr.ch_extid = frame->fmt.ide;
if (hdr.ch_extid) if (hdr.ch_extid)
{ {
hdr.ch_id = frame.id.id_ext; hdr.ch_id = frame->id.id_ext;
} }
else else
{ {
hdr.ch_id = frame.id.id; hdr.ch_id = frame->id.id;
} }
#else #else
if (frame.fmt.ide) if (frame->fmt.ide)
{ {
canerr("ERROR: Received message with extended" canerr("ERROR: Received message with extended"
" identifier. Dropped\n"); " identifier. Dropped\n");
@@ -799,7 +803,7 @@ static void ctucanfd_chardev_receive(FAR struct ctucanfd_can_s *priv)
continue; continue;
} }
hdr.ch_id = frame.id.id; hdr.ch_id = frame->id.id;
#endif #endif
/* Clear the error indication and unused bits */ /* Clear the error indication and unused bits */
@@ -810,11 +814,11 @@ static void ctucanfd_chardev_receive(FAR struct ctucanfd_can_s *priv)
hdr.ch_tcf = 0; hdr.ch_tcf = 0;
#ifdef CONFIG_CAN_FD #ifdef CONFIG_CAN_FD
hdr.ch_esi = frame.fmt.esi_rsv; hdr.ch_esi = frame->fmt.esi_rsv;
hdr.ch_edl = frame.fmt.fdf; hdr.ch_edl = frame->fmt.fdf;
hdr.ch_brs = frame.fmt.brs; hdr.ch_brs = frame->fmt.brs;
#else #else
if (frame.fmt.fdf) if (frame->fmt.fdf)
{ {
/* Drop any FD CAN messages if not supported */ /* Drop any FD CAN messages if not supported */
@@ -826,7 +830,7 @@ static void ctucanfd_chardev_receive(FAR struct ctucanfd_can_s *priv)
/* Provide the data to the upper half driver */ /* Provide the data to the upper half driver */
ret = can_receive(dev, &hdr, (FAR uint8_t *)&frame.data); ret = can_receive(dev, &hdr, (FAR uint8_t *)&frame->data);
if (ret < 0) if (ret < 0)
{ {
canerr("ERROR: can_receive failed %d\n", ret); canerr("ERROR: can_receive failed %d\n", ret);
@@ -1342,12 +1346,12 @@ static int ctucanfd_sock_send(FAR struct ctucanfd_can_s *priv)
static void ctucanfd_sock_receive(FAR struct ctucanfd_can_s *priv) static void ctucanfd_sock_receive(FAR struct ctucanfd_can_s *priv)
{ {
struct ctucanfd_frame_s rxframe; FAR struct ctucanfd_frame_s *rxframe;
FAR uint32_t *ptr = NULL; uint32_t buff[sizeof(struct ctucanfd_frame_s) / 4];
int i = 0; int i = 0;
uint16_t frc = 0; uint16_t frc = 0;
uint32_t regval = 0; uint32_t regval = 0;
uint8_t bytes; uint8_t bytes;
/* Get frame count */ /* Get frame count */
@@ -1358,52 +1362,56 @@ static void ctucanfd_sock_receive(FAR struct ctucanfd_can_s *priv)
while (frc-- > 0) while (frc-- > 0)
{ {
ptr = (FAR uint32_t *)&rxframe; /* We use a pointer to buffer to avoid an unaligned pointer
* compiler errors
*/
rxframe = (struct ctucanfd_frame_s *)&buff;
for (i = 0; i < sizeof(struct ctucanfd_frame_s) / 4; i++) for (i = 0; i < sizeof(struct ctucanfd_frame_s) / 4; i++)
{ {
/* RX buffer in automatic mode */ /* RX buffer in automatic mode */
ptr[i] = ctucanfd_getreg(priv, CTUCANFD_RXDATA); buff[i] = ctucanfd_getreg(priv, CTUCANFD_RXDATA);
} }
/* CAN 2.0 or CAN FD */ /* CAN 2.0 or CAN FD */
#ifdef CONFIG_NET_CAN_CANFD #ifdef CONFIG_NET_CAN_CANFD
if (rxframe.fmt.fdf) if (rxframe->fmt.fdf)
{ {
struct canfd_frame *frame = (struct canfd_frame *)priv->rx_pool; struct canfd_frame *frame = (struct canfd_frame *)priv->rx_pool;
/* Get the DLC */ /* Get the DLC */
frame->len = can_dlc2bytes(rxframe.fmt.dlc); frame->len = can_dlc2bytes(rxframe->fmt.dlc);
#ifdef CONFIG_NET_CAN_EXTID #ifdef CONFIG_NET_CAN_EXTID
/* Get the CAN identifier. */ /* Get the CAN identifier. */
if (rxframe.fmt.ide) if (rxframe->fmt.ide)
{ {
frame->can_id = rxframe.id.id_ext; frame->can_id = rxframe->id.id_ext;
frame->can_id |= CAN_EFF_FLAG; frame->can_id |= CAN_EFF_FLAG;
} }
else else
{ {
frame->can_id = rxframe.id.id; frame->can_id = rxframe->id.id;
} }
#else #else
if (rxframe.fmt.ide) if (rxframe->fmt.ide)
{ {
canerr("ERROR: Received message with extended" canerr("ERROR: Received message with extended"
" identifier. Dropped\n"); " identifier. Dropped\n");
continue; continue;
} }
frame->can_id = rxframe.id.id; frame->can_id = rxframe->id.id;
#endif #endif
/* Extract the RTR bit */ /* Extract the RTR bit */
if (rxframe.fmt.rtr) if (rxframe->fmt.rtr)
{ {
frame->can_id |= CAN_RTR_FLAG; frame->can_id |= CAN_RTR_FLAG;
} }
@@ -1412,22 +1420,22 @@ static void ctucanfd_sock_receive(FAR struct ctucanfd_can_s *priv)
frame->flags = 0; frame->flags = 0;
if (rxframe.fmt.esi_rsv) if (rxframe->fmt.esi_rsv)
{ {
frame->flags |= CANFD_ESI; frame->flags |= CANFD_ESI;
} }
if (rxframe.fmt.brs) if (rxframe->fmt.brs)
{ {
frame->flags |= CANFD_BRS; frame->flags |= CANFD_BRS;
} }
/* Get data */ /* Get data */
bytes = can_dlc2bytes(rxframe.fmt.dlc); bytes = can_dlc2bytes(rxframe->fmt.dlc);
for (i = 0; i < bytes; i++) for (i = 0; i < bytes; i++)
{ {
frame->data[i] = rxframe.data[i]; frame->data[i] = rxframe->data[i];
} }
/* Copy the buffer pointer to priv->dev.. Set amount of data /* Copy the buffer pointer to priv->dev.. Set amount of data
@@ -1460,43 +1468,43 @@ static void ctucanfd_sock_receive(FAR struct ctucanfd_can_s *priv)
/* Get the DLC */ /* Get the DLC */
frame->can_dlc = rxframe.fmt.dlc; frame->can_dlc = rxframe->fmt.dlc;
#ifdef CONFIG_NET_CAN_EXTID #ifdef CONFIG_NET_CAN_EXTID
/* Get the CAN identifier. */ /* Get the CAN identifier. */
if (rxframe.fmt.ide) if (rxframe->fmt.ide)
{ {
frame->can_id = rxframe.id.id_ext; frame->can_id = rxframe->id.id_ext;
frame->can_id |= CAN_EFF_FLAG; frame->can_id |= CAN_EFF_FLAG;
} }
else else
{ {
frame->can_id = rxframe.id.id; frame->can_id = rxframe->id.id;
} }
#else #else
if (rxframe.fmt.ide) if (rxframe->fmt.ide)
{ {
canerr("ERROR: Received message with extended" canerr("ERROR: Received message with extended"
" identifier. Dropped\n"); " identifier. Dropped\n");
continue; continue;
} }
frame->can_id = rxframe.id.id; frame->can_id = rxframe->id.id;
#endif #endif
/* Extract the RTR bit */ /* Extract the RTR bit */
if (rxframe.fmt.rtr) if (rxframe->fmt.rtr)
{ {
frame->can_id |= CAN_RTR_FLAG; frame->can_id |= CAN_RTR_FLAG;
} }
/* Get data */ /* Get data */
for (i = 0; i < rxframe.fmt.dlc; i++) for (i = 0; i < rxframe->fmt.dlc; i++)
{ {
frame->data[i] = rxframe.data[i]; frame->data[i] = rxframe->data[i];
} }
/* Copy the buffer pointer to priv->dev.. Set amount of data /* Copy the buffer pointer to priv->dev.. Set amount of data