当前位置:网站首页>寄存器地址名映射
寄存器地址名映射
2022-07-07 06:26:00 【一只大喵咪1201】
描述
在STM32中有很多功能单元,总线以及各种外设还有很多的寄存器,每一类功能单元,总线,各个外设以及寄存器都有一个地址,它们的地址是由STM32的硬件电路决定的,我们如果在每次使用都使用它的地址,这样就会太过复杂,所以我们把它们的地址映射成一个寄存器地址名,这样在使用起来就会方便很多。
映射原理
以外设为例:
首先来看下STM32的总线结构:
我们以GPIO为例,来找一下它所在的位置。
- 我们首先找到的就是矩阵总线。这是STM32的生命线,各种外设以及各种功能单元与CPU的联系几乎都需要通过矩阵总线。
- 再往下我们又找到了AHB总线。它是挂靠在矩阵总线上的,当然,AHB总线上也又很多的外设。
- 再往下我们找到了APB2总线。这条总线上挂载的都是一些高速外设。
- 最后我们找的了GPIO。GPIO都挂靠在APB2总线上。

以图形的方式表示,GPIO的位置就是这样的。
在STM官网提供的标准库函数中,已经将各个外设全部映像好,接下来本喵给大家介绍一下。
- 外设基地址:
#define PERIPH_BASE ((uint32_t)0x40000000)
PERIPH_BASE代表的就是外设基地址,各种外设最终都是在这基地址的基础上加一些偏移量得出来的,其实质是地址0x40000000。
- 总线外设基地址
#define APB2PERIPH_BASE (PERIPH_BASE + 0x10000)
APB2PERIPH_BASE代表APB2总线外设基地址,它的实质是在外设基地址的基础上增加一个偏移量后得到的一个新地址,比如APB2的地址就是0x4000000000+0x10000=0x4000010000。
- 寄存器组基地址
#define GPIOA_BASE (APB2PERIPH_BASE + 0x0800)
#define GPIOB_BASE (APB2PERIPH_BASE + 0x0C00)
#define GPIOC_BASE (APB2PERIPH_BASE + 0x1000)
#define GPIOD_BASE (APB2PERIPH_BASE + 0x1400)
#define GPIOE_BASE (APB2PERIPH_BASE + 0x1800)
#define GPIOF_BASE (APB2PERIPH_BASE + 0x1C00)
#define GPIOG_BASE (APB2PERIPH_BASE + 0x2000)
这里仅列举7组GPIO的映射,其他外设的映射也是如此。GPIO_XBASE就是代表该组GPIO的基地址,其实质就是APB2总线外设基地址加一个偏移量后得到的地址。
- GPIO中各个寄存器的地址

通过手册查找到GPIO中各个寄存器的偏移值,加到GPIO基地址上就是各个寄存器的具体地址。
如果这样查找的话,在每次使用的时候都需要进行相应的计算,为了方便使用,对寄存器组进行了如下的处理:
以GPIOA为例:
typedef struct
{
__IO uint32_t CRL;
__IO uint32_t CRH;
__IO uint32_t IDR;
__IO uint32_t ODR;
__IO uint32_t BSRR;
__IO uint32_t BRR;
__IO uint32_t LCKR;
} GPIO_TypeDef;
将每组GPIO中的7个寄存器创建成一个结构体。
#define GPIOA ((GPIO_TypeDef *) GPIOA_BASE)
然后将GPIOA的基地址强制类型转化成一个结构体指针变量,这样一来在使用GPIOA的每个寄存器时候只需要直接访问该结构体中的成员就可以。
例如使用GPIOA_CRL寄存器:
GPIOA->CRL=0x00000000;
不用每次都在GPIOA基地址的基础上加一个偏移值,这样一来就方便了很多。
对于寄存器组中的其他寄存器也是这样来使用的。
其他单元
#define FLASH_BASE ((uint32_t)0x08000000)
#define SRAM_BASE ((uint32_t)0x20000000)
#define SRAM_BB_BASE ((uint32_t)0x22000000)
#define PERIPH_BB_BASE ((uint32_t)0x42000000)
#define FSMC_R_BASE ((uint32_t)0xA0000000)
这是其他功能单元的基地址,例如FLASH,SRAM等等。
#define APB1PERIPH_BASE PERIPH_BASE
#define AHBPERIPH_BASE (PERIPH_BASE + 0x20000)
这是其他总线外设基地址,APB1PERIPH_BASE,AHBPERIPH_BASE分别代表APB1外设总线的基地址,AHB外设总线的基地址。
#define TIM2_BASE (APB1PERIPH_BASE + 0x0000)
#define TIM3_BASE (APB1PERIPH_BASE + 0x0400)
#define TIM4_BASE (APB1PERIPH_BASE + 0x0800)
#define TIM5_BASE (APB1PERIPH_BASE + 0x0C00)
#define TIM6_BASE (APB1PERIPH_BASE + 0x1000)
#define TIM7_BASE (APB1PERIPH_BASE + 0x1400)
#define TIM12_BASE (APB1PERIPH_BASE + 0x1800)
#define TIM13_BASE (APB1PERIPH_BASE + 0x1C00)
#define TIM14_BASE (APB1PERIPH_BASE + 0x2000)
#define RTC_BASE (APB1PERIPH_BASE + 0x2800)
#define WWDG_BASE (APB1PERIPH_BASE + 0x2C00)
#define IWDG_BASE (APB1PERIPH_BASE + 0x3000)
#define SPI2_BASE (APB1PERIPH_BASE + 0x3800)
#define SPI3_BASE (APB1PERIPH_BASE + 0x3C00)
#define USART2_BASE (APB1PERIPH_BASE + 0x4400)
#define USART3_BASE (APB1PERIPH_BASE + 0x4800)
#define UART4_BASE (APB1PERIPH_BASE + 0x4C00)
#define UART5_BASE (APB1PERIPH_BASE + 0x5000)
这是其他寄存器组的基地址,只是列举了一部分,还有很多没有列举,但都是这样的映射方法。
总结
寄存器地址名的映射就是为了使用各种寄存器的时候更加方便,每一个寄存器名字的实质其实就是一个地址,操作寄存器名比操作地址更加方便直观。
如果以上内容对您有帮助,麻烦给本喵一键三连支持一下。
边栏推荐
- 年薪50w阿裏P8親自下場,教你如何從測試進階
- 面板显示技术:LCD与OLED
- LeetCode 736. Lisp 语法解析
- [Yugong series] February 2022 U3D full stack class 006 unity toolbar
- How to realize sliding operation component in fast application
- OpenGL三维图形绘制
- 为不同类型设备构建应用的三大更新 | 2022 I/O 重点回顾
- Simulation volume leetcode [general] 1706 Where does the ball meet
- Un salaire annuel de 50 W Ali P8 vous montrera comment passer du test
- 阿里p8推荐,测试覆盖率工具—Jacoco,实用性极佳
猜你喜欢
![[MySQL] detailed explanation of trigger content of database advanced](/img/6c/8aad649e4ba1160db3aea857ecf4a1.png)
[MySQL] detailed explanation of trigger content of database advanced

oracle一次性说清楚,多种分隔符的一个字段拆分多行,再多行多列多种分隔符拆多行,最终处理超亿亿。。亿级别数据量

Greenplum6.x搭建_环境配置

UnityShader入门精要个人总结--基础篇(一)

Goldbach conjecture C language

Image segmentation in opencv

NCS Chengdu Xindian interview experience

为不同类型设备构建应用的三大更新 | 2022 I/O 重点回顾

数字三角形模型 AcWing 275. 传纸条

LeetCode 715. Range module
随机推荐
Interpretation of MySQL optimization principle
Port occupation troubleshooting
个人力扣题目分类记录
Introduction to data fragmentation
leetcode135. Distribute candy
硬件大熊原创合集(2022/05更新)
Implement custom memory allocator
Frequently Asked Coding Problems
模拟卷Leetcode【普通】1609. 奇偶树
ESP32-ULP协处理器低功耗模式RTC GPIO中断唤醒
Golang etcdv3 reports an error. The attribute in grpc does not exist
Tronapi wave field interface - source code without encryption - can be opened twice - interface document attached - package based on thinkphp5 - detailed guidance of the author - July 6, 2022 - Novice
Simulation volume leetcode [general] 1706 Where does the ball meet
如何统计项目代码行数
【ChaosBlade:节点磁盘填充、杀节点上指定进程、挂起节点上指定进程】
Common operating commands of Linux
MAC OSX php dyld: Library not loaded: /usr/local/xxxx. dylib
With an annual salary of 50W, Alibaba P8 will come out in person to teach you how to advance from testing
Find the original code, inverse code and complement of signed numbers [C language]
NCS Chengdu New Electric interview Experience