Accessing multiplexed I2C and UART pins of STM32F103 in arduino IDE
In order to use two different peripherals on the same pins, you have to initialize I2C and de-intialize UART when using I2C and doing the opposite when using UART.

It’s not possible to initialize two different peripherlas on the same pins, concurrently.