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
<device>.h :Top-level header file (device specific). To be included by application code.Includes core_cm3.h and system_<device>.h
system_<device>.h: Device specific declarations
system_<device>.c :Device specific definitions, e.g. SystemInit()
应用程序只需包含<device>.h 即可。

由于CORTEX-M3有一些可选硬件如MPU,在<device.h>中包含core_cm3.h和system_<device>.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()将不被包含,因此厂商必须在<device.h>中给以实现。

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中的中断定义
中断号的定义,在<device.h>中
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 */
  }     
while(1);
}
void SysTick_Handler(void)
{
   static int count=0; 
   count++;   
   if(count >= 1000)
  {
    count=0;
    seconds++;
  }
}
如果觉得此文对你有所帮助,就请在下面发表你的评论吧!发表评论就是对本站的支持!谢谢

转载请注明来自秦工的博客,本文地址:http://www.arm32.com/post/10915.html

原文地址:https://www.cnblogs.com/51mcu/p/3132676.html