I'm using STM32F series MCUs with a RTOS. Since the OS provides API to configure the peripherals and their physical output assignment, I didn't pay much attention in the past. Now I want to have full control of this type of hardware configurations.
The point I've confused is that, according to datasheet, a physical pin might be in one of 3 different modes (for STM32F103, in this case):
Main function (after reset)
Primary alternate function (Alternate function/Default)
Remapped alternate function
"Main function" is easy to understand. However, for example, one primary alternate function can be assigned to 3 different peripherals for the same physical pin:
Question
Where are we configuring if PA2
is used for whether USART2_TX
or ADC12_IN2
or TIM2_CH3
?
Best Answer
Every GPIO has a Port configuration register (GPIOx_CRL/GPIOx_CRH).
The STM32F1 series also has AFIO (alternate function input output) registers with remap bits. These are per function, not per pin. See AFIO_MAPR.
The REMAP register is where things get difficult, because this remaps multiple pins.
To solve this puzzle I recommend you use the STM32Cube initialization code generator.
It it physically not possible to configure PA2 as USART2_TX and ADC_IN12.
To put it in
ADC_IN12
, you only set CNF to Analog and MODE to input.To put it in
USART2_TX
set CNFpush-pull
and enable alternate function.To put it in
TIM2_CH3
, set CNF tofloating
orpush-pull
, enable alternate function, and enableTIM2_REMAP
in AFIO_MAPR.Analog mode has priority. This disables the digital features, such as schmitt trigger and output control.
(You might still be able to use the ADC while digital input/output functions are enabled, I have never tried this)
That is because they require the same functionality.
REMAP configures if you use TIM or USART.