2025-12-17 2:47 AM
Hi.
I have Uart pins (Rx/Tx) PA3 and PA2 on STM32F407VET6. Can these pins be changed to I2C (SDA SCL) in the program?
Are the pins on STM32 that have been assigned a protocol (SPI/I2C/Uart) constant and cannot be changed or replaced?
Thank you.
Solved! Go to Solution.
2025-12-17 4:07 AM - edited 2025-12-17 4:16 AM
@No_Name wrote:if I want to use two different protocols (switching) on the same pin, I have to use PB10 and PB11.
Yes, those pins will give you options for both USART3 (RX, TX) and I2C2 (SDA, SCL).
PB10 could be SPI2_SCK, but PB11 has no SPI functions - and you would need some other pins to make a full SPI connection.
There may also be other pin combinations available ...
Again, remember that the external hardware for I2C is usually different from UART - so you'd have to bear that in mind.
Also, you'd have to ensure that all connected devices are happy with the protocol changing.
As @waclawek.jan suggested, there's always bit-banging ...
PS:
For finding available combinations, it's probably easier to use Table 9:
Easier to identify which pins have I2C, SPI, and UART ...
2025-12-17 3:01 AM - edited 2025-12-17 3:15 AM
@No_Name wrote:I have Uart pins (Rx/Tx) PA3 and PA2 on STM32F407VET6. Can these pins be changed to I2C (SDA SCL) in the program?.
The datasheet will tell you if that is physically possible on that chip.
If it is physically possible then, yes - the software can change it at runtime.
However, the external hardware requirements for UART and I2C are rather different - so you'd have to take that into account...
@No_Name wrote:Are the pins on STM32 that have been assigned a protocol (SPI/I2C/Uart) constant and cannot be changed or replaced?
Of course not.
The code generated by by CubeMX is just writing to registers to configure the pin options; it's just standard C code - nothing special or magic - you can do exactly the same in your own code.
You can examine the source code that CubeMX generates to see what it's doing.
PS:
But, of course, CubeMX can only enable configurations that are physically possible on the chip itself;
If the chip itself does not have I2C support on a pin, then CubeMX cannot magically "invent" that.
2025-12-17 3:12 AM
@Andrew Neil wrote:The datasheet will tell you if that is physically possible on that chip.
It's not:
https://www.st.com/resource/en/datasheet/stm32f407ve.pdf#page=50
via: https://www.st.com/en/microcontrollers-microprocessors/stm32f407ve.html#documentation
There is no I2C option on either PA2 or PA3
2025-12-17 3:54 AM
Okay, I understand. That means AF (alternative function) has been determined from the datasheet and is not free. So, if I want to use two different protocols (switching) on the same pin, I have to use PB10 and PB11.
2025-12-17 3:55 AM
I2C master can be easily bit-banged, though.
JW
2025-12-17 4:02 AM
> That means AF (alternative function) has been determined from the datasheet and is not free.
Alternate Functions (AF) describe the hardware implementation in regard to this pin.
And only one can be active at a time.
While I2C can be bit-banged (as mentioned), it will consume quite a bit of core performance with medium or high transfer loads.
2025-12-17 4:07 AM - edited 2025-12-17 4:16 AM
@No_Name wrote:if I want to use two different protocols (switching) on the same pin, I have to use PB10 and PB11.
Yes, those pins will give you options for both USART3 (RX, TX) and I2C2 (SDA, SCL).
PB10 could be SPI2_SCK, but PB11 has no SPI functions - and you would need some other pins to make a full SPI connection.
There may also be other pin combinations available ...
Again, remember that the external hardware for I2C is usually different from UART - so you'd have to bear that in mind.
Also, you'd have to ensure that all connected devices are happy with the protocol changing.
As @waclawek.jan suggested, there's always bit-banging ...
PS:
For finding available combinations, it's probably easier to use Table 9:
Easier to identify which pins have I2C, SPI, and UART ...
2025-12-17 4:13 AM
Okay, I understand now. Thank you.