关于STM32中的引脚重映射

重映射功能

外设引脚重映射的概念,即一个外设的引脚除了具有默认的端口外,还可以通过设置重映射寄存器的方式,把这个外设的引脚映射到其它的端口。

简单的讲就是把管脚的外设功能映射到另一个管脚,但不是可以随便映射的。

部分重映射 & 完全重映射

  • 部分重映射: 功能外设的部分引脚重新映射,还有一部分引脚是原来的默认引脚。
  • 完全重映射:功能外设的所有引脚都重新映射。

引脚重映射配置(串口1为例):

① 使能 GPIO 时钟(重映射后的IO);
② 使能功能外设时钟(例如串口1);
③ 使能 AFIO 时钟。重映射必须使能 AFIO 时钟:
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE)
④ 开启重映射。
GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE)
根据第一个参数,来确定是部分重映射还是全部重映射

哪些情况需要开启 AFIO 辅助功能时钟?

对寄存器AFIO_MAPR,AFIO_EXTICRX 和 AFIO_EVCR 进行读写操作前,应当首先打开 AFIO 时钟。

  • AFIO_MAPR: 配置复用功能重映射
  • AFIO_EXTICRX: 配置外部中断线映射
  • AFIO_EVCR:配置EVENTOUT 事件输出

个人尝试

本人在尝试过程中发现,即便不开启重映射功能,也可以成功启用重映射引脚,使外设功能正常。但当重映射引脚和默认引脚都使能之后,外设会优先启用默认引脚。当开启重映射功能后,外设则会优先启用重映射引脚。

### STM32 UART Pin Remapping In STM32 microcontrollers, the Alternate Function (AF) remapping feature allows changing the default pins used by peripherals such as UART to alternative ones. This flexibility is particularly useful when designing PCB layouts or avoiding conflicts with other functionalities. To perform UART pin remapping on an STM32 device: The `RCC_APB2PeriphClockCmd` function enables the peripheral clock for GPIO and AFIO modules which are necessary before configuring any alternate functions[^1]. For newer HAL libraries, this step might be handled internally within initialization routines but remains crucial knowledge. For actual pin remapping of UART signals, specific bits inside the `AFIO_MAPR` register must be configured appropriately depending upon the desired UART instance and target pins. Each UART has predefined sets of possible alternate locations that can serve its TX/RX lines; these options vary between different STM32 families and models[^2]. Here's a code snippet demonstrating how one could enable USART1_TX & RX remap using standard peripheral library syntax: ```c // Enable clocks for required peripherals RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); // Configure AFIO MAPR Register according to your needs. GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE); ``` Additionally, after setting up hardware configuration through registers like above, proper initialization structures should also set corresponding parameters indicating usage of alternate functions via their respective fields in structure members related to mode settings during setup phase[^3].
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Harvey1104

感谢您的认可,无限飓风不断进步

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值