详细解读CMSIS的相关知识

简介: 详细解读CMSIS的相关知识

CMSIS是Cortex Microcontrollor Software Interface Standard 的缩写,是ARM公司和芯片产商联合推出的一套软件标准,目的是为了加快软件开发速度。

CMSIS可以分为三层

1.核内外设访问层 Core Peripheral Access Layer(CPCL)

2.中间件访问层 Middleware Access Layer (MWAL)

3.设备访问层 Device Peripheral Access Layer(DPAL)

构架图如图所示

CPAL是用来定义处理器内部的的一些寄存器地址及功能函数的。比如内核寄存器,NVIC,调试子系统的访问。一些对特殊寄存器的访问被定义成内联或者内嵌汇编的形式。

这一层由ARM公司提供。

MWAL

该层定义一些中间件常用的API,该层也由arm负责实现,但是芯片厂商也要根据自己的设别进行更新。

DPAL

用来 定义一些寄存器的地址及对外的访问函数。另外芯片厂商也需要对向量表进行拓展,已实现对于自己设备的中断的处理。

CMSIS文件结构

CMSIS首先对文件名的定义给出了标准

core_cm3.h :Cortex-M3 global declarations and definitions, static function definitions

core_cm3.c :Cortex-M3 global definitions

.h :Top-level header file (device specific). To be included by application code.Includes corecm3.h and system.h

system.h: Device specific declarations

system.c :Device specific definitions, e.g. SystemInit()

应用程序只需包含.h 即可。

由于CORTEX-M3有一些可选硬件如MPU,在中包含corecm3.h和system.h时需注意以下一点,

以STM32.h为例。

/ Configuration of the Cortex-M3 Processor and Core Peripherals /

#define MPU_PRESENT 0 /!< STM32 does not provide a MPU present or not/

#define NVIC_PRIO_BITS 4 /!< STM32 uses 4 Bits for the Priority Levels /

#define Vendor_SysTickConfig 0 /!< Set to 1 if different SysTick Config is used /

#include "core_cm3.h" / Cortex-M3 processor and core peripherals /

#include "system_stm32.h" / STM32 System /

即需定义以上三个宏之后,在包含相应的头文件,因为这些头文件中用到了这些宏。

注意:如果Vendor_SysTickConfig 被定义为1,则在cm3_core.h中定义的SysTickConfig()将不被包含,因此厂商必须在中给以实现。

CMSIS支持的工具链

CMSIS目前支持三大主流的工具链,即ARM RealView (armcc), IAR EWARM (iccarm), and GNU Compiler Collection (gcc).

在core_cm3.h中有如下定义:

/ define compiler specific symbols /

#if defined ( CC_ARM )

#define ASM asm /!< asm keyword for armcc /

#define INLINE inline /!< inline keyword for armcc /

#elif defined ( ICCARM )

#define ASM asm /!< asm keyword for iarcc /

#define INLINE inline /!< inline keyword for iarcc. Only

avaiable in High optimization mode! /

#define nop no_operation /!< no operation intrinsic in iarcc /

#elif defined ( GNUC )

#define ASM asm /!< asm keyword for gcc /

#define INLINE inline /!< inline keyword for gcc

#endif

MISRA-C

CMSIS要求定义的API以及编码与MISRA- C 2004规范兼容。MISRA-C是由Motor Industry Software Reliability Association提出的,意在增加代码的安全性,该规范提出了一些标准。

如Rule 12. 不同名空间中的变量名不得相同。

Rule 13. 不得使用char, int, float, double, long等基本类型,应该用自己定义的类型显示表示类型的大小,如CHAR8, UCHAR8, INT16, INT32, FLOAT32, LONG64, ULONG64等。

Rule 37. 不得对有符号数施加位操作,例如 1 [ 4 将被禁止,必须写 1UL [ 4;

CMSIS中的中断定义

中断号的定义,在中

typedef enum IRQn

{

/** Cortex-M3 Processor Exceptions Numbers **

NonMaskableInt_IRQn = -14, /!< 2 Non Maskable Interrupt /

MemoryManagement_IRQn = -12, /!< 4 Cortex-M3 Memory Mgmt Interrupt /

BusFault_IRQn = -11, /!< 5 Cortex-M3 Bus Fault Interrupt /

UsageFault_IRQn = -10, /!< 6 Cortex-M3 Usage Fault Interrupt /

SVCall_IRQn = -5, /!< 11 Cortex-M3 SV Call Interrupt /

DebugMonitor_IRQn = -4, /!< 12 Cortex-M3 Debug Monitor Interrupt /

PendSV_IRQn = -2, /!< 14 Cortex-M3 Pend SV Interrupt /

SysTick_IRQn = -1, /!< 15 Cortex-M3 System Tick Interrupt /

/** Device specific Interrupt Numbers */

UART_IRQn = 0, /!< Example Interrupt /

} IRQn_Type;

系统级的异常号已经确定,不能更改,且必须为负值,以和设备相关的中断区别。

中断处理函数的定义,一般在启动代码中声明,加入weak属性,因此可在其他文件中再一次实现。如下所示:

AREA RESET, DATA, READONLY

EXPORT Vectors

Vectors DCD __initial_sp ; Top of Stack

DCD Reset_Handler ; Reset Handler

DCD NMI_Handler ; NMI Handler

DCD HardFault_Handler ; Hard Fault Handler

DCD MemManage_Handler ; MPU Fault Handler

DCD BusFault_Handler ; Bus Fault Handler

DCD UsageFault_Handler ; Usage Fault Handler

DCD 0 ; Reserved

DCD 0 ; Reserved

DCD 0 ; Reserved

DCD 0 ; Reserved

DCD SVC_Handler ; SVCall Handler

DCD DebugMon_Handler ; Debug Monitor Handler

DCD 0 ; Reserved

DCD PendSV_Handler ; PendSV Handler

DCD SysTick_Handler ; SysTick Handler

; External Interrupts

DCD WWDG_IRQHandler ; Window Watchdog

CMSIS的编程约定

1.标识符

Core Registers, Peripheral Registers, CPU Instructions.用大写字母定义

例如:NVIC->AIRCR, GPIOB, LDMIAEQ

外设访问函数以及中断和中断处理函数用大小写(“CamelCase)定义

例如:SysTickConfig(),DebugMonitor_IRQn

对一些外设的操作函数前面讲相应的前缀

例如:ITM_SendChar(),NVIC_SystemReset()

2.注释

/*

@brief Enable Interrupt in NVIC Interrupt Controller

@param IRQn_Type IRQn specifies the interrupt number

@return none

Enable a device specific interupt in the NVIC interrupt controller.

The interrupt number cannot be a negative value.

/

CMSIS实例

#include "stm32.h"

#include "main.h"

volatile unsigned int seconds=0;

int main(void)

{

SystemInit();

if (SysTick_Config(SystemFrequency / 1000)) / Setup SysTick Timer for 1 msec interrupts /

{

while (1); / Capture error */

}//代码效果参考:http://www.ezhiqi.com/zx/art_4964.html

while(1);

}//代码效果参考:http://www.ezhiqi.com/zx/art_4076.html

void SysTick_Handler(void)

{

static int count=0;

count++;

if(count >= 1000)

{

count=0;

seconds++;

}

}//代码效果参考:http://www.ezhiqi.com/bx/art_5431.html

如果觉得此文对你有所帮助,就请在下面发表你的评论吧!发表评论就是对本站的支持!谢谢

转载请注明来自秦工的博客,本文地址:

努力才可成功!信仰不是别人给你说一百遍的一句话,而是你被撞的头破血流后的幡然悔悟!有时候一个道理你会花几年才会明白。虽然这只是一个简单的道理!

相关文章
|
1天前
|
Serverless
详细解读arcgis投影测试
详细解读arcgis投影测试
12 4
|
1天前
|
Linux 开发工具
详尽分享离线安装telnet
详尽分享离线安装telnet
|
1天前
|
容器
一篇文章讲明白flex布局
一篇文章讲明白flex布局
|
1天前
|
人工智能 Go C#
详细解读100741AQueries
详细解读100741AQueries
12 3
|
1天前
|
算法 前端开发 计算机视觉
一篇文章讲明白Harris角点
一篇文章讲明白Harris角点
12 3
|
1天前
|
存储 程序员 编译器
程序员必知:四,对于变量的理解1——整型变量
程序员必知:四,对于变量的理解1——整型变量
10 2
|
1天前
|
JSON 前端开发 数据格式
经验大分享:TheGeneofBitizens
经验大分享:TheGeneofBitizens
|
1天前
|
C++
经验大分享:QtQwdget汽车仪表知识点拆解6自定义控件
经验大分享:QtQwdget汽车仪表知识点拆解6自定义控件
|
1天前
|
程序员 iOS开发
程序员必知:代理模式简介与实现
程序员必知:代理模式简介与实现
|
1天前
|
XML 存储 数据格式
详尽分享项目分析_xxoo
详尽分享项目分析_xxoo
12 2