But then I’d see a “clean” implementation of analogRead()
not checking if it was already set once and rely on it being unchanged but check for the current mode and if needed reactivate AN_INPUT if required.
like
if (adcChannelConfigured != PIN_MAP[pin].adc_channel || PIN_MAP[pin].pin_mode != AN_INPUT)
{
HAL_GPIO_Save_Pin_Mode(PIN_MAP[pin].pin_mode);
HAL_Pin_Mode(pin, AN_INPUT);
}
And/or add and document the possibility to “repair” the pin by using pinMode(pin, AN_INPUT);
after it got “damaged” by a previous other pinMode()
call.