cancel
Showing results for 
Search instead for 
Did you mean: 

Basic questions about Alternate Functions

laojian
Associate II
Posted on August 12, 2015 at 10:38

Hi all,

I am a beginner of MCU, and right now is digging into STM32F1xx serial MCU, which is pretty old, but simple enough for beginner. 

I have spent a lot of time on some example codes, still with the confusion that how does alternate function of IO pins is defined, or activated? Or in other words, how does a pin know it is configured as AFIO, not default GPIO, and also know what AF it is configured to? Since it may have several alternate functions to choose.

For example, AFIO of pin PB7 of F103 is defined as I2C1_SDA/FSMC_NADV/TIM4_CH2. How can I enable FSMC function, but config PB7 as I2C1_SDA? 

Thanks for your time~~

  

#afio
5 REPLIES 5
Posted on August 12, 2015 at 14:07

The F1 is a bit rougher than the newer parts, but you select the ''AF'' mode either Push-Pull, Open-Drain, or as an Input

Multiple AF functions on a pin are decided by which peripheral is enabled (ie TIM channel and inferred direction), an Input can go to everything without issue.

typedef enum
{ GPIO_Mode_AIN = 0x0,
GPIO_Mode_IN_FLOATING = 0x04,
GPIO_Mode_IPD = 0x28,
GPIO_Mode_IPU = 0x48,
GPIO_Mode_Out_OD = 0x14,
GPIO_Mode_Out_PP = 0x10,
GPIO_Mode_AF_OD = 0x1C,
GPIO_Mode_AF_PP = 0x18
}GPIOMode_TypeDef;

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Nesrine M_O
Lead II
Posted on August 12, 2015 at 16:30

Hi,

In STM32 F1 series:

1. The configuration to use an I/O as alternate function depends on the peripheral mode used. For example, the USART Tx pin should be configured as alternate function pushpull while the USART Rx pin should be configured as input floating or input pull-up. 

2. To optimize the number of peripherals available in MCUs that have fewer pins (smaller package size), it is possible by software to remap some alternate functions to other pins. for example the USART2_RX pin can be mapped on PA3 (default remap) or PD6 (done through software remap) pin.

I suggest you to take a look at the section General-purpose and alternate-function I/Os (GPIOs and AFIOs) in the RM0008 reference manual 

-Syrine-

laojian
Associate II
Posted on August 12, 2015 at 20:44

Hi,

OK, we can differentiate GPIO and AFIO by enabling peripheral modules, and then config the struct below:

typedef enum
{ GPIO_Mode_AIN = 0x0,
GPIO_Mode_IN_FLOATING = 0x04,
GPIO_Mode_IPD = 0x28,
GPIO_Mode_IPU = 0x48,
GPIO_Mode_Out_OD = 0x14,
GPIO_Mode_Out_PP = 0x10,
GPIO_Mode_AF_OD = 0x1C,
GPIO_Mode_AF_PP = 0x18
}GPIOMode_TypeDef;

But how to differentiate different AF of one pin? As the example,PB7 of F103 is defined as I2C1_SDA/FSMC_NADV/TIM4_CH2. If I activate both FSMC and I2C, how can I tell the chip I want to use PB7 asFSMC_NADV, not I2C_SDA? It seems GPIOMODE_TypeDef struct doesn't help any more here, since bothFSMC_NADV andI2C_SDA is output, and we can't differentiate it fromGPIOMODE_TypeDef struct. So how to solve this problem? Remapping is a solution, but it defeats the purpose of the multi AF of a pin. I have studied Reference Manual very carefully for several months, but frankly speaking, it didn't make very clear on this point. It just said you need to set GPIOMODE_TypeDef struct with _AF_ options, that's all. Didn't mention how to differentiate different AFIOs of a pin. Thanks so much~~
Posted on August 12, 2015 at 21:19

Groups of pins on the F1 map/remap at a peripheral level, if you enable both the FSMC and I2C in their default mappings they will interfere with each other.

If you want the FSMC and I2C1 to both function you will need to REMAP the I2C1 peripheral to use a different set of pins. So instead of PB6/PB7 the pin exit via PB8/PB9. I'd probably REMAP the TIM4 away too, if you are using it.

/*!< NL configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_Remap_FSMC_NADV, DISABLE); // Commit NADV on PB7

GPIO_PinRemapConfig(GPIO_Remap_I2C1, ENABLE); // I2C1 Remapped to PB8/PB9

With the F2/F4 parts the peripherals are multiplexed at a pin level.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
laojian
Associate II
Posted on August 12, 2015 at 21:39

Hi clive1, got it! Thanks very much for your immediate, and clear reply~~