1

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]
EmbeddedManiac
  • 189
  • 1
  • 12
  • 1
    Remove the `const` if you really do want to be able to change the `gpioOutPortss` entries. – kaylum Feb 23 '22 at 21:26
  • 1
    I don't think you want to change `gpioOutPortss`. Just add an explicit cast to get rid of the warning. I guess changing the declaration of `HAL_GPIO_WritePin` is not an option? – Eugene Sh. Feb 23 '22 at 21:30
  • 1
    You might want to read up on the differences between const pointers and pointers to consts, such as https://stackoverflow.com/questions/1143262/what-is-the-difference-between-const-int-const-int-const-and-int-const – Neil Feb 23 '22 at 21:36
  • @EugeneSh. `GPIOx->BSRR = GPIO_Pin;` That does seem to imply OP wants to change the entries. – kaylum Feb 23 '22 at 21:37
  • @kaylum `gpioOutPortss` is an array of pointers, which are not being changed. – Eugene Sh. Feb 23 '22 at 21:40
  • @EugeneSh. Right. But the `const` applies to each pointer in the array and not to the array (I think). That C syntax does seem a bit ambiguous so I could be wrong. Though that is what the warning does suggest. – kaylum Feb 23 '22 at 21:41
  • @EugeneSh. Adding a cast makes the warning go away but why make your code ugly just to cover up a mistake that you can fix? More importantly it also wastes RAM which might not be acceptable on an embedded platform. – Tom V Feb 23 '22 at 22:35

1 Answers1

5

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.

Tom V
  • 4,827
  • 2
  • 5
  • 22