# What happens on the STM32 when two pins are configured to have the same alternate function?

Reading through the STM32F051 manual, it looks like it's possible to configure the same alternate function on two pins; for example, you could have multiple USART1_TX pins at the same time. Or for example the more problematic case of two USART_RX pins. The manual is silent on this case. Is this legal/safe? Is this useful?

• Interesting question. I can see why it would sometimes be useful though, for example while debugging to get the TX output to another pin or to get buffered clocks in a few places. It might be worth adding in the relevant manual link though to save anyone having to search for it. – PeterJ Oct 27 '13 at 2:59
• Again - Interesting question. But how about the case of assigning USART_RX from two different pins? One would hope that the chip designers had planned a priority scheme for one over the other as opposed to allowing for internal contention from the two sources. – Michael Karas Oct 27 '13 at 5:38
• I've never tried this, but based on what I recall from the pin diagrams, you would basically end up connecting the pins together internally. – rjp Oct 31 '13 at 2:57

On processors which use per-pin registers to select I/O functions, it is generally possible to route an output function to multiple pins without conflict; all pins will echo the same output. The effect of having multiple pins connected to an input is often unspecified; if e.g. a UART had I/O pins 3 and 4 connected to a UART, it might behave as though the UART was connected to an "AND" gate which took pins 3 and 4 as inputs, or it might behave as though it was connected to one pin and ignore the other, or it might connect both pins to the UART's input buffer through transistors that had a moderate amount of resistance, or it might draw extra current when pin 3 is high and 4 is low or vice versa, or it might do just about anything else imaginable. I don't recall having seen any particular guarantees that any of the STM32 family chips I looked at would favor any particular approach over any other.

A somewhat nicer design approach, used in some Microchip parts (perhaps some STM parts as well) is to have each I/O function include a multiplexer to select which pin it should accept input from, and have pin include a multiplexer to indicate the I/O function from which it should output data. Such a design makes it possible to have one pin feed multiple I/O functions, while simultaneously eliminating any ambiguities posed by conflicting configurations.

Yes, it is usefull. Result from experiment on STM32F407VGT6: Both AF pins can be used as AF simultaneously, at least if using these pins as AF inputs. The experiment was to trigger TIM1 on rising edge of external trigger - signal "TIM1_ETR". "TIM1_ETR" signal can be mapped to pins PE7 and/or PA12 of MCU package.

The experiment: Initialize both pins PE7 and PA12 as AF "TIM1_ETR" for TIM1:

{
GPIO_InitTypeDef GPIO_InitStruct = { 0 };
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Pin = GPIO_PIN_7;
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct); // configure PE7 as AF

GPIO_InitStruct.Pin = GPIO_PIN_12;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); // configure PA12 as AF
}


The result - TIM1 is started by rising edge on any of the pins PE7 or PA12.

This reveals the way to MUX (change) AF pins in run-time:

main
{
GPIO_InitTypeDef GPIO_InitStruct = { 0 };

// Deactivate PA12 input (set as regular input):
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pin = GPIO_PIN_12;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

// select PE7 as a trigger source,
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pin = GPIO_PIN_7;
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);

// Arm the timer
arm_tim1();

/* Now timer will be started by rising edge only on PE7 */

// ... later one can select PA12 as trigger source. Deactivate PE7 (set as regular input):
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pin = GPIO_PIN_7;
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);

// Activate PA12
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pin = GPIO_PIN_12;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

// Arm the timer
arm_tim1();
/* timer will be started by rising edge only on PA12 */
}


This can be helpfull then two different sources are needed for timer triggering, this can save us offboard MUX IC :). Both inputs can be used simultaneously, if initialized as shown in the beginning of the post. Inputs are not connected together, I mean signal on PA12 is not transfered to PE7 and vice versa (because GPIOs are set as inputs).

Code is generated by "STM32CubeF4 v5.3" + "Firmware Package V1.24.0 / 08-February-2019."

Basically, the pins are GPIOs which can be assigned to alternate functions, e.g. UART_RX. Every alternate function has it's default pin and alternative pins to which the function can be remapped. If you check out the reference manual of the STM32F103 in section 9.1.5, they say:

To optimize the number of peripheral I/O functions for different device packages, it is possible to remap some alternate functions to some other pins. This is achieved by software, by programming the corresponding registers (refer to AFIO registers on page 177. In that case, the alternate functions are no longer mapped to their original assignations.

• The way I grok this is: once you assign the new pins, the previous assignment is removed, so you cannot technically have multiple pins connected to the same peripheral function. – dext0rb Jan 8 '14 at 20:00
• It's not possible, because (at least on the F103, I haven't worked with the other derivatives of the STM32), the alternative pin cannot be assigned to the function without using the remap feature, even if you set it up as AF_Output in the GPIO setup. And after remapping, the function i no longer available on the original pin. I hope this clarifies what I meant. – Manu3l0us Jan 8 '14 at 20:08
• The way this is done is very different on the different subfamilies. The F1 register structure may prohibit it, but the F3 structure is different, where you configure the pin to an alternate function instead of the function to a pin, so duplicate mappings seem possible. In effect, you are not answering the situation of the question asked, which seems to be about a part where the register structure permits it. – Chris Stratton Jan 8 '14 at 20:21
• @Chris Stratton STM32F302 Ref Manual section 8.3.2: "The device I/O pins are connected to onboard peripherals/modules through a multiplexer that allows only one peripheral’s alternate function (AF) connected to an I/O pin at a time. In this way, there can be no conflict between peripherals available on the same I/O pin." But actually, this doesn't say anything about multiple I/O on one AF. Are there even multiple options for input pins? – Manu3l0us Jan 9 '14 at 8:55
• I appreciate the F1x information too, I've used that one too :-) – joeforker Jan 9 '14 at 20:10