Skip to content

How to resolve observed warning when -Wcast-align flag is used during compile time #335

@rxa1031

Description

@rxa1031

Hi,

On using compiler flag -Wcast-align below errors are observed in stm32h7xx_hal_uart.c , tm32h7xx_hal_uart_ex.c and ethernetif.c files.

C:/ST/Repository/STM32Cube_FW_H7_V1.12.1/Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_uart.c: In function 'HAL_UART_Transmit':
C:/ST/Repository/STM32Cube_FW_H7_V1.12.1/Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_uart.c:1147:21: warning: cast increases required alignment of target type [-Wcast-align]
 1147 |       pdata16bits = (const uint16_t *) pData;
      |                     ^
C:/ST/Repository/STM32Cube_FW_H7_V1.12.1/Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_uart.c: In function 'HAL_UART_Receive':
C:/ST/Repository/STM32Cube_FW_H7_V1.12.1/Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_uart.c:1243:21: warning: cast increases required alignment of target type [-Wcast-align]
 1243 |       pdata16bits = (uint16_t *) pData;
      |                     ^
C:/ST/Repository/STM32Cube_FW_H7_V1.12.1/Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_uart.c: In function 'UART_TxISR_16BIT':
C:/ST/Repository/STM32Cube_FW_H7_V1.12.1/Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_uart.c:4107:13: warning: cast increases required alignment of target type [-Wcast-align]
 4107 |       tmp = (const uint16_t *) huart->pTxBuffPtr;
      |             ^
C:/ST/Repository/STM32Cube_FW_H7_V1.12.1/Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_uart.c: In function 'UART_TxISR_16BIT_FIFOEN':
C:/ST/Repository/STM32Cube_FW_H7_V1.12.1/Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_uart.c:4184:15: warning: cast increases required alignment of target type [-Wcast-align]
 4184 |         tmp = (const uint16_t *) huart->pTxBuffPtr;
      |               ^
C:/ST/Repository/STM32Cube_FW_H7_V1.12.1/Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_uart.c: In function 'UART_RxISR_16BIT':
C:/ST/Repository/STM32Cube_FW_H7_V1.12.1/Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_uart.c:4329:11: warning: cast increases required alignment of target type [-Wcast-align]
 4329 |     tmp = (uint16_t *) huart->pRxBuffPtr ;
      |           ^
C:/ST/Repository/STM32Cube_FW_H7_V1.12.1/Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_uart.c: In function 'UART_RxISR_16BIT_FIFOEN':
C:/ST/Repository/STM32Cube_FW_H7_V1.12.1/Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_uart.c:4593:13: warning: cast increases required alignment of target type [-Wcast-align]
 4593 |       tmp = (uint16_t *) huart->pRxBuffPtr ;
      |             ^
C:/ST/Repository/STM32Cube_FW_H7_V1.12.1/Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_uart_ex.c: In function 'HAL_UARTEx_ReceiveToIdle':
C:/ST/Repository/STM32Cube_FW_H7_V1.12.1/Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_uart_ex.c:748:21: warning: cast increases required alignment of target type [-Wcast-align]
  748 |       pdata16bits = (uint16_t *) pData;
      |                     ^
D:/IMPORTANT_CODES/KEIL_plus_STM32CUBEIDE/lwIP_Ethernet/Src/ethernetif.c: In function 'HAL_ETH_RxLinkCallback':
D:/IMPORTANT_CODES/KEIL_plus_STM32CUBEIDE/lwIP_Ethernet/Src/ethernetif.c:750:7: warning: cast increases required alignment of target type [-Wcast-align]
  750 |   p = (struct pbuf *)(buff - offsetof(RxBuff_t, buff));
      |       ^
D:/IMPORTANT_CODES/KEIL_plus_STM32CUBEIDE/lwIP_Ethernet/Src/ethernetif.c:776:32: warning: cast increases required alignment of target type [-Wcast-align]
  776 |   SCB_InvalidateDCache_by_Addr((uint32_t *)buff, Length);

I request a suggestion on how to resolve the concern without removing the compiler flag -Wcast-align.
As these files are coded by ST, and are added to project when the code is generated using STM32CubeMX, I am unable to use code similar to below to remove the observed warnings:

Option 1:
Cast argument passed to function to const void * instead of const uint8_t *
like:
HAL_StatusTypeDef HAL_UART_Transmit(UART_HandleTypeDef *huart, const void *pData, uint16_t Size, uint32_t Timeout)

Option 2:
Use pdata16bits = (uint16_t *)__builtin_assume_aligned(pData, 2); or similar in place of pdata16bits = (const uint16_t *) pData;

Option 3:

typedef union {
	uint8_t *puc;
	uint16_t *pu16;
} pDataTypeDef;
pDataTypeDef pDataUnion;
pDataUnion.puc = pData;
    /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */
    if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
    {
      pdata8bits  = NULL;
      pdata16bits = (uint16_t *) pDataUnion.pu16;
    }
    else
    {
      pdata8bits  = pData;
      pdata16bits = NULL;
    }

in place of:

    /* In case of 9bits/No Parity transfer, pRxData needs to be handled as a uint16_t pointer */
    if ((huart->Init.WordLength == UART_WORDLENGTH_9B) && (huart->Init.Parity == UART_PARITY_NONE))
    {
      pdata8bits  = NULL;
      pdata16bits = (uint16_t *) pData;
    }
    else
    {
      pdata8bits  = pData;
      pdata16bits = NULL;
    }

Please suggest how to fix the issue.

Thanks!
Rajeev

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions