ST Micro HAL, stm32f7xx_hal_crc.c Как решить [Предупреждение] разыменование указателя типа-перфоратора нарушит правила строгого наложения имен - PullRequest
0 голосов
/ 17 мая 2018

В STM32Cube_FW_F7_V1.3.0 и в STM32Cube_FW_F7_V1.11.0

я получаю следующее предупреждение при компиляции библиотеки HAL stm32f7 с -Os или -O2.
Примечание: -O1 компилируется без предупреждений.

[Warning] dereferencing type-punned pointer will break strict-aliasing rules 

Предупреждение оправдано.Код, кажется, работает правильно.Однако я хотел бы рассматривать предупреждения как ошибки.

Вопрос: Как можно изменить код, чтобы удалить предупреждение, не нарушая модуль crc?

Примечание: я нигде не мог найти решение этой проблемы в Интернете.

Это одна из строк, которая вызывает предупреждение

 *(__IO uint16_t*) (&hcrc->Instance->DR) = (uint16_t)(((uint16_t)(pBuffer[4*i])<<8) | (uint16_t)(pBuffer[4*i+1]));

Я считаю, что это немного сложно и пугающе изменить, потому что он имеет дело с аппаратным CRCзарегистрироваться.

Это лицензия ST Micro, в которой говорится, что я должен указывать при распространении исходного кода, она не имеет отношения к вопросу

  /******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */

Соответствующий код:

#define     __IO    volatile             /*!< Defines 'read / write' permissions */

typedef struct
{
  __IO uint32_t  DR;          /*!< CRC Data register,                           Address offset: 0x00 */
  __IO uint8_t   IDR;         /*!< CRC Independent data register,               Address offset: 0x04 */
  uint8_t        RESERVED0;   /*!< Reserved, 0x05                                                    */
  uint16_t       RESERVED1;   /*!< Reserved, 0x06                                                    */
  __IO uint32_t  CR;          /*!< CRC Control register,                        Address offset: 0x08 */
  uint32_t       RESERVED2;   /*!< Reserved,                                                    0x0C */
  __IO uint32_t  INIT;        /*!< Initial CRC value register,                  Address offset: 0x10 */
  __IO uint32_t  POL;         /*!< CRC polynomial register,                     Address offset: 0x14 */
} CRC_TypeDef;


/**             
  * @brief  Enter 8-bit input data to the CRC calculator.
  *         Specific data handling to optimize processing time.  
  * @param  hcrc: CRC handle
  * @param  pBuffer: pointer to the input data buffer
  * @param  BufferLength: input data buffer length
  * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
  */
static uint32_t CRC_Handle_8(CRC_HandleTypeDef *hcrc, uint8_t pBuffer[], uint32_t BufferLength)
{
  uint32_t i = 0; /* input data buffer index */

   /* Processing time optimization: 4 bytes are entered in a row with a single word write,
    * last bytes must be carefully fed to the CRC calculator to ensure a correct type
    * handling by the IP */
   for(i = 0; i < (BufferLength/4); i++)
   {
     hcrc->Instance->DR = (uint32_t)(((uint32_t)(pBuffer[4*i])<<24) | ((uint32_t)(pBuffer[4*i+1])<<16) | ((uint32_t)(pBuffer[4*i+2])<<8) | (uint32_t)(pBuffer[4*i+3]));
   }
   /* last bytes specific handling */
   if((BufferLength%4) != 0)
   {
     if(BufferLength%4 == 1)
     {
       *(__IO uint8_t*) (&hcrc->Instance->DR) = pBuffer[4*i];
     }
     if(BufferLength%4 == 2)
     {
       //the following line gives the warning (rightfully) '[Warning] dereferencing type-punned pointer will break strict-aliasing rules'
       *(__IO uint16_t*) (&hcrc->Instance->DR) = (uint16_t)(((uint16_t)(pBuffer[4*i])<<8) | (uint16_t)(pBuffer[4*i+1]));
     }
     if(BufferLength%4 == 3)
     {
       //the following line gives the warning (rightfully) '[Warning] dereferencing type-punned pointer will break strict-aliasing rules'
       *(__IO uint16_t*) (&hcrc->Instance->DR) = (uint16_t)(((uint16_t)(pBuffer[4*i])<<8) | (uint16_t)(pBuffer[4*i+1]));
       *(__IO uint8_t*) (&hcrc->Instance->DR) = pBuffer[4*i+2];       
     }
   }

  /* Return the CRC computed value */ 
  return hcrc->Instance->DR;
}

Предупреждение также появляется в 16-битной функции CRC

1 Ответ

0 голосов
/ 18 мая 2018

Используйте анонимный союз, чтобы избежать наказания c type.

Измените определение CRC_TypeDef в заголовочном файле для использования анонимного объединения.

typedef struct
{
  union
  {
    __IO uint32_t  DR;          /*!< CRC Data register 32 bit access,            Address offset: 0x00 */
    __IO uint16_t  DR16;        /*!< CRC Data register 16 bit access,            Address offset: 0x00 */
    __IO uint8_t   DR8;         /*!< CRC Data register 8 bit access,             Address offset: 0x00 */
  };
  __IO uint8_t   IDR;         /*!< CRC Independent data register,               Address offset: 0x04 */
  uint8_t        RESERVED0;   /*!< Reserved, 0x05                                                    */
  uint16_t       RESERVED1;   /*!< Reserved, 0x06                                                    */
  __IO uint32_t  CR;          /*!< CRC Control register,                        Address offset: 0x08 */
  uint32_t       RESERVED2;   /*!< Reserved,                                                    0x0C */
  __IO uint32_t  INIT;        /*!< Initial CRC value register,                  Address offset: 0x10 */
  __IO uint32_t  POL;         /*!< CRC polynomial register,                     Address offset: 0x14 */
} CRC_TypeDef;

Используйте это новое объединение в функциях CRC_Handle_8 и CRC_Handle_16.

/**             
  * @brief  Enter 8-bit input data to the CRC calculator.
  *         Specific data handling to optimize processing time.  
  * @param  hcrc: CRC handle
  * @param  pBuffer: pointer to the input data buffer
  * @param  BufferLength: input data buffer length
  * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
  */
static uint32_t CRC_Handle_8(CRC_HandleTypeDef *hcrc, uint8_t pBuffer[], uint32_t BufferLength)
{
  uint32_t i = 0; /* input data buffer index */

   /* Processing time optimization: 4 bytes are entered in a row with a single word write,
    * last bytes must be carefully fed to the CRC calculator to ensure a correct type
    * handling by the IP */
   for(i = 0; i < (BufferLength/4); i++)
   {
     hcrc->Instance->DR = (uint32_t)(((uint32_t)(pBuffer[4*i])<<24) | ((uint32_t)(pBuffer[4*i+1])<<16) | ((uint32_t)(pBuffer[4*i+2])<<8) | (uint32_t)(pBuffer[4*i+3]));
   }
   /* last bytes specific handling */
   if((BufferLength%4) != 0)
   {
     if(BufferLength%4 == 1)
     {
       hcrc->Instance->DR8 = pBuffer[4*i];
     }
     if(BufferLength%4 == 2)
     {
       hcrc->Instance->DR16 = (uint16_t)(((uint16_t)(pBuffer[4*i])<<8) | (uint16_t)(pBuffer[4*i+1]));
     }
     if(BufferLength%4 == 3)
     {
       hcrc->Instance->DR16 = (uint16_t)(((uint16_t)(pBuffer[4*i])<<8) | (uint16_t)(pBuffer[4*i+1]));
       hcrc->Instance->DR8 = pBuffer[4*i+2];       
     }
   }

  /* Return the CRC computed value */ 
  return hcrc->Instance->DR;
}

/**             
  * @brief  Enter 16-bit input data to the CRC calculator.
  *         Specific data handling to optimize processing time.  
  * @param  hcrc: CRC handle
  * @param  pBuffer: pointer to the input data buffer
  * @param  BufferLength: input data buffer length
  * @retval uint32_t CRC (returned value LSBs for CRC shorter than 32 bits)
  */  
static uint32_t CRC_Handle_16(CRC_HandleTypeDef *hcrc, uint16_t pBuffer[], uint32_t BufferLength)
{
  uint32_t i = 0;  /* input data buffer index */

  /* Processing time optimization: 2 HalfWords are entered in a row with a single word write,
   * in case of odd length, last HalfWord must be carefully fed to the CRC calculator to ensure 
   * a correct type handling by the IP */
  for(i = 0; i < (BufferLength/2); i++)
  {
    hcrc->Instance->DR = (((uint32_t)(pBuffer[2*i])<<16) | (uint32_t)(pBuffer[2*i+1]));
  }
  if((BufferLength%2) != 0)
  {
    hcrc->Instance->DR16 = pBuffer[2*i];
  }

  /* Return the CRC computed value */ 
  return hcrc->Instance->DR;
}

Обязательная лицензия ST Micro и отказ от ответственности.

  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */

Благодарим Олафа, который намекнул на этот ответ в комментариях.

...