Core_cm3.c解析

本文深入解析STM32的CMSIS标准,包括核内外设访问层、设备访问层的功能,以及Core_cm3.c文件中关键函数的作用,如堆栈指针管理、寄存器读写等,为理解STM32的底层操作提供了清晰视角。

    STM32从3.0库开始引入了CMSIS,CMSIS是Cortex微控制器软件接口标准(Cortex MicroController Software Interface Standard)的缩写,这个是ARM定制的一个用于Cortex-M系列的一个标准,主要是为了提供通用api接口来访问内核和一些片上外设,提高代码的可移植性。

CMSIS有三个层:核内外设访问层Core Peripheral Access Layer(CPAL),中间件访问层Middleware Access Layer(MWAL),设备访问层(Device Peripheral Access Layer)。

CPAL用于访问内核的寄存器和组件,如NVIC,调试系统等。该层是由ARM实现的。

MWAL用于对中间件的访问,现在该层还未实现。(也不知道所谓的中间件是什么东西)。

DPAL用于定义一些硬件寄存器的地址和一些外设访问函数,由芯片制造商实现。

 

CPAL层的实现就是Core_cm3.c文件,DPAL层的实现就是system_stm32f10x.c文件(似乎还应该加上外设的函数库)。

 

接下来就来了解一下Core_cm3.c里面有什么东东:

首先是汇编关键字__ASM和__INLINE的宏定义,支持不同的编译器。由于使用的是Keil,所以就只看第一种,__CC_ARM。

这里面的函数调用都只符合ARM过程调用标准的,如R0到R3用作参数和返回值传递,这也是这里面唯一用到的。

此外,在Keil中使用了__asm关键字后,编译器不会为函数增加返回指令,所以需要自己编写返回命令,也就是每个函数后面的  bx lr。

1.     __ASM uint32_t __get_PSP(void):获取进程堆栈指针PSP。

2.     __ASM void __set_PSP(uint32_t topOfProcStack):设置PSP。

3.     __ASM uint32_t __get_MSP(void):获取主堆栈指针MSP。

4.     __ASM void __set_MSP(uint32_t mainStackPointer):设置MSP。

5.     __ASM uint32_t __REV16(uint16_t value):反转半字中字节顺序,如0xABCD反转后得到0xCDAB。

6.     __ASM int32_t __REVSH(int16_t value):反转字节顺序,并做符号拓展。就是在__REV16函数得到的结果上再进行一次符号拓展。这两个函数主要是方便进行大小端的切换。

7.     __ASM void __CLREX(void):清除由LDREX指令造成的互斥锁。LDREX和STREX是Cortex用来实现互斥访问,保护临界资源的指令,LDREX执行后,只有离它最近的一条存储指令(STR,STREX)才能执行,其他的存储指令都会被驳回,而CLREX就是用于清除互斥访问状态的标记。

8.     __ASM uint32_t  __get_BASEPRI(void):获取BASEPRI寄存器的值,优先级号高于该寄存器的中断都会被屏蔽(优先级号越大,优先级越低),为零时不屏蔽任何中断。

9.     __ASM void __set_BASEPRI(uint32_t basePri):设置BASEPRI的值。

10.  __ASM uint32_t __get_PRIMASK(void):PRIMASK是一个只有一位的寄存器,置位时屏蔽绝大部分的异常中断,只剩下NMI和HardFault可以响应。

11.  __ASM void __set_PRIMASK(uint32_t priMask):设置PRIMASK的值。

12.  __ASM uint32_t  __get_FAULTMASK(void):FAULTMASK也是一个只有一位的寄存器,为1时只有NMI才能响应,其他异常与中断全部被屏蔽。

13.  __ASM void __set_FAULTMASK(uint32_t faultMask):设置FAULTMASK的值。

14.  __ASM uint32_t __get_CONTROL(void):获取CONTROL的值。寄存器CONTROL只有两位。CONTROL[0]选择特权级别,0为特权级,1为敌用户级。CONTROL[1]用于选择堆栈指针,0为MSP,1为PSP。

15.  __ASM void __set_CONTROL(uint32_t control):设置CONTROL寄存器的值。

BASEPRI,PRIMASK,FAULTMASK,CONTROL都只能在特权模式下被修改。

转载于:https://www.cnblogs.com/king-77024128/articles/2512997.html

*** Using Compiler 'V5.06 update 5 (build 528)', folder: 'E:\keil5 2\ARM\ARMCC\Bin' Rebuild target 'Target 1' compiling misc.c... compiling stm32f10x_crc.c... compiling stm32f10x_cec.c... compiling stm32f10x_dma.c... compiling stm32f10x_exti.c... compiling stm32f10x_adc.c... compiling stm32f10x_dac.c... compiling stm32f10x_fsmc.c... compiling stm32f10x_bkp.c... compiling stm32f10x_dbgmcu.c... compiling stm32f10x_gpio.c... compiling stm32f10x_flash.c... compiling stm32f10x_can.c... compiling stm32f10x_i2c.c... compiling stm32f10x_iwdg.c... compiling stm32f10x_pwr.c... assembling startup_stm32f10x_md.s... compiling core_cm3.c... "no source": Error: command-line: #564: cannot open embedded assembler output file "C:\WINDOWS\TEMP\p2248-2": No such file or directory Startup\core_cm3.c: 0 warnings, 1 error compiling stm32f10x_rcc.c... compiling stm32f10x_rtc.c... compiling stm32f10x_spi.c... compiling stm32f10x_sdio.c... compiling stm32f10x_wwdg.c... compiling stm32f10x_usart.c... compiling stm32f10x_tim.c... compiling system_stm32f10x.c... compiling LED.c... User\LED.c(14): warning: #1-D: last line of file ends without a newline } User\LED.c: 1 warning, 0 errors compiling main.c... User\LED.h(7): warning: #1-D: last line of file ends without a newline #endif User\main.c: 1 warning, 0 errors compiling stm32f10x_it.c... linking... ..\ARM\PACK\project.axf: error: L6002U: Could not open file ..\arm\pack\core_cm3.o: No such file or directory Finished: 0 information, 0 warning, 0 error and 1 fatal error messages. "..\ARM\PACK\project.axf" - 1 Error(s), 2 Warning(s). Target not created. Build Time Elapsed: 00:00:07
07-30
*** Using Compiler 'V5.06 update 5 (build 528)', folder: 'E:\keil5 2\ARM\ARMCC\Bin' Rebuild target 'Target 1' compiling misc.c... compiling stm32f10x_dma.c... compiling stm32f10x_bkp.c... compiling stm32f10x_cec.c... compiling stm32f10x_flash.c... compiling stm32f10x_can.c... compiling stm32f10x_exti.c... compiling stm32f10x_fsmc.c... compiling stm32f10x_crc.c... compiling stm32f10x_gpio.c... compiling stm32f10x_dac.c... compiling stm32f10x_dbgmcu.c... compiling stm32f10x_adc.c... compiling stm32f10x_i2c.c... compiling stm32f10x_iwdg.c... compiling stm32f10x_pwr.c... compiling stm32f10x_rcc.c... compiling stm32f10x_sdio.c... compiling stm32f10x_rtc.c... compiling stm32f10x_spi.c... compiling stm32f10x_tim.c... compiling stm32f10x_usart.c... compiling core_cm3.c... "no source": Error: command-line: #564: cannot open embedded assembler output file "C:\WINDOWS\TEMP\p59e0-2": No such file or directory Startup\core_cm3.c: 0 warnings, 1 error assembling startup_stm32f10x_md.s... compiling stm32f10x_wwdg.c... compiling system_stm32f10x.c... compiling LED.c... User\LED.c(14): warning: #1-D: last line of file ends without a newline } User\LED.c: 1 warning, 0 errors compiling main.c... User\LED.h(7): warning: #1-D: last line of file ends without a newline #endif User\main.c: 1 warning, 0 errors compiling stm32f10x_it.c... linking... ..\ARM\PACK\project.axf: error: L6002U: Could not open file ..\arm\pack\core_cm3.o: No such file or directory Finished: 0 information, 0 warning, 0 error and 1 fatal error messages. "..\ARM\PACK\project.axf" - 1 Error(s), 2 Warning(s). Target not created. Build Time Elapsed: 00:00:11
最新发布
07-30
*** Using Compiler 'V5.06 update 5 (build 528)', folder: 'E:\keil5 2\ARM\ARMCC\Bin' Rebuild target 'Target 1' compiling misc.c... compiling stm32f10x_bkp.c... compiling stm32f10x_adc.c... compiling stm32f10x_dbgmcu.c... compiling stm32f10x_crc.c... compiling stm32f10x_exti.c... compiling stm32f10x_cec.c... compiling stm32f10x_dac.c... compiling stm32f10x_dma.c... compiling stm32f10x_flash.c... compiling stm32f10x_can.c... compiling stm32f10x_fsmc.c... compiling stm32f10x_gpio.c... compiling stm32f10x_i2c.c... compiling stm32f10x_iwdg.c... compiling stm32f10x_pwr.c... compiling stm32f10x_rtc.c... compiling stm32f10x_rcc.c... compiling stm32f10x_sdio.c... compiling stm32f10x_spi.c... compiling stm32f10x_usart.c... compiling stm32f10x_tim.c... compiling LED.c... User\LED.c(14): warning: #1-D: last line of file ends without a newline } User\LED.c: 1 warning, 0 errors compiling core_cm3.c... "no source": Error: command-line: #564: cannot open embedded assembler output file "C:\WINDOWS\TEMP\p3d58-2": No such file or directory Startup\core_cm3.c: 0 warnings, 1 error compiling stm32f10x_wwdg.c... assembling startup_stm32f10x_md.s... compiling system_stm32f10x.c... compiling main.c... User\LED.h(7): warning: #1-D: last line of file ends without a newline #endif User\main.c: 1 warning, 0 errors compiling stm32f10x_it.c... linking... ..\ARM\PACK\project.axf: error: L6002U: Could not open file ..\arm\pack\core_cm3.o: No such file or directory Finished: 0 information, 0 warning, 0 error and 1 fatal error messages. "..\ARM\PACK\project.axf" - 1 Error(s), 2 Warning(s). Target not created. Build Time Elapsed: 00:00:12
07-30
### 关于 `core_cm3.c` 文件的内容及其在 ARM Cortex-M3 中的位置 #### 1. **核心功能描述** `core_cm3.c` 是 CMSIS(Cortex Microcontroller Software Interface Standard)标准的一部分,主要包含了与 ARM Cortex-M3 内核相关的底层实现代码[^2]。这些代码通常由 ARM 公司提供,并经过优化以便开发者能够更方便地访问和操作硬件资源。 该文件的主要作用在于初始化和配置 Cortex-M3 的关键组件,例如 NVIC (Nested Vectored Interrupt Controller),SysTick 定时器以及调试接口等功能模块。它还提供了用于管理异常处理程序的函数原型声明和支持中断向量表重定位的功能[^3]。 #### 2. **文件内容概述** 以下是 `core_cmos.c` 可能包含的一些典型内容: - **异常处理机制**: 提供了进入和退出异常模式所需的低级支持。 - **系统控制块(SCB)** 配置: 包括复位、电源管理和错误状态检测等方面的操作方法。 - **嵌套矢量中断控制器(NVIC)** 初始化设置: 设置优先级分组方案以及其他相关参数调整。 - **定时器(SysTick)** 功能启用: 实现周期性的滴答事件触发服务。 下面是一个简单的伪代码示例来展示如何利用部分此类功能: ```c // Example of using SysTick configuration from core_cm3.c functions. void setup_systick(uint32_t reload_value){ SysTick->LOAD = reload_value; // Set Reload Value SysTick->VAL = 0; // Clear Current Value Register SysTick->CTRL |= SysTick_CTRL_ENABLE_Msk | SysTick_CTRL_TICKINT_Msk; } ``` 此段代码展示了怎样通过调用来自 `core_cm3.c` 所暴露出来的寄存器结构体成员变量去激活并定制化一个基于 CPU 主频运行频率下的计数倒退型延时循环逻辑[^4]。 #### 3. **文件位置** 对于采用 Keil MDK 工具链进行项目开发的情况而言,在官方发布的 STM32CubeMX 或者 STMicroelectronics 原厂固件包中可以找到预编译版本或者是源码形式存在的 `core_cm3.c` 文件。一般情况下,默认路径如下所示: - 如果是使用 HAL 库,则位于 `<STM32Fxxx_HAL_Driver>/Middlewares/ST/CMSIS/Device/ARM/Include/core_cm3.c`. - 对于经典的标准外设库(Standard Peripheral Library), 则可能是 `<STM32Fxxx_StdPeriph_Lib_Vxx>/Libraries/CMSIS/CM3/CoreSupport/core_cm3.c`. 值得注意的是,在某些特定场景下如果目标 MCU 并不需要直接依赖上述提到的具体实现细节的话,那么即使移除掉该项目中的 `core_cm3.c`, 整个应用程序依旧可以正常工作,因为真正起效的部分往往已经封装进了头文件比如 `Core_cmFunc.h` 当中去了[^5]。 --- ###
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值