Search code examples
cstm32c11stm32cubeide

Can't resolve C warning related to pointer array in STM32CubeIDE


Facing a a warning which we are not able to get rid of. I am using stm32 MCU and STM32CubeIDE with a standard C11 compiler. I think I can understand why the compiler is throwing the warning but the problem is I am not able to resolve. Any help is appreciated. Thank you.

The array of pointer is defined this way

static const GPIO_TypeDef *gpioOutPortss[GPIO_OUT_CH_NR] =
{
    DOUT_OD_OUT4_GPIO_Port,
    DOUT_OD_OUT6_GPIO_Port,
    DOUT_OD_OUT5_GPIO_Port,
    DOUT_OD_OUT7_GPIO_Port,
    DOUT_LED_DISABLE_GPIO_Port,
    DOUT_BUZZ_GPIO_Port,
    DOUT_OD_OUT8_GPIO_Port,
    DOUT_OD_OUT3_GPIO_Port,
    DOUT_OD_OUT2_GPIO_Port,
    DOUT_OD_OUT1_GPIO_Port,
    DOUT_ALARM_GPIO_Port,
    DOUT_12V_PWR_GPIO_Port,
    DOUT_12V_PWR_GPIO_Port
};

The function to be called is defined this way:

void HAL_GPIO_WritePin(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
{
  /* Check the parameters */
  assert_param(IS_GPIO_PIN(GPIO_Pin));
  assert_param(IS_GPIO_PIN_ACTION(PinState));

  if(PinState != GPIO_PIN_RESET)
  {
    GPIOx->BSRR = GPIO_Pin;
  }
  else
  {
    GPIOx->BSRR = (uint32_t)GPIO_Pin << 16U;
  }
}

The actual function call looks like this:

if (gpioOutPolarity[channel])
{
    HAL_GPIO_WritePin(gpioOutPortss[channel], gpioOutPins[channel],
    GPIO_PIN_SET);
}

The warning generated by the compiler is this:

warning: passing argument 1 of 'HAL_GPIO_WritePin' discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]

Solution

  • You need to write:

    static GPIO_TypeDef * const gpioOutPortss[GPIO_OUT_CH_NR] =
    

    not

    static const GPIO_TypeDef *gpioOutPortss[GPIO_OUT_CH_NR] =
    

    The GPIO blocks are not constant (otherwise you couldn't write to them), only the pointers to them are constant.