170 likes | 368 Views
Cortex 微控制器软件接口标准. 张国琛 武汉理工大学 UP研发中心. CMSIS. CMSIS 简介 CMSIS 架构 CMSIS 文件结构 CMSIS 支持的工具链 CMSIS 中的中断定义 CMSIS 中的编程约定 CMSIS 实例 总结. CMSIS 的简介.
E N D
Cortex微控制器软件接口标准 张国琛 武汉理工大学 UP研发中心
CMSIS • CMSIS简介 • CMSIS架构 • CMSIS文件结构 • CMSIS支持的工具链 • CMSIS中的中断定义 • CMSIS中的编程约定 • CMSIS实例 • 总结
CMSIS的简介 Cortex微控制器软件接口标准(Cortex Microcontroller Software Interface Standard)是ARM和一些编译器厂家以及半导体厂家共同遵循的一套标准,是由ARM提出,专门针对CORTEX-M系列的标准。在该标准的约定下,ARM和芯片厂商会提供一些通用的API接口来访问CORTEX内核以及一些专用外设,以减少更换芯片以及开发工具等移植工作所带来的金钱以及时间上的消耗。只要都是基于M3的芯片,代码均是可以复用的。
CMSIS简介 根据近期的研究调查,发现在嵌入式开发领域,软件的花费在不断提高,相反硬件的花费却逐年降低,因此嵌入式领域的公司,越来越把精力放到了软件上,但软件在更换芯片或是开发工具的更新换代中,代码的重用性不高,随着CORTEX-M3处理器大量投放市场,ARM意识到建立一套软件开发标准的重要性,因此CMSIS应运而生。
CMSIS的架构 • CMSIS可以分为以下3个基本功能层 • 核内外设访问层 Core Peripheral Access Layer (CPAL) • 中间件访问层 Middleware Access Layer (MWAL) • 设备访问层 Device Peripheral Access Layer (DPAL)
CMSIS架构 • CMSIS的架构如下图所示:
CMSIS架构 • Core Peripheral Access Layer (CPAL) 该层用来定义一些CORTEX-M处理器内部的一些寄存器地址以及功能函数。如对内核寄存器,NVIC,调试子系统的访问。一些对特殊用途寄存器的访问被定义成内联函数或是内嵌汇编的形式。 该层的实现由ARM提供。 • Middleware Access Layer (MWAL) 该层定义访问中间件的一些通用API,该层也由ARM负责实现,但芯片厂商需要根据自己的设备特性进行更新。目前该层仍在开发中,还没有更进一步的消息。 • Device Peripheral Access Layer (DPAL) 该层和CPAL层类似,用来定义一些硬件寄存器的地址以及对外设的访问函数。另外芯片厂商还需要对异常向量表进行扩展,以实现对自己设备的中断处理。该层可引用CPAL层定义的地址和函数,该层由具体的芯片厂商提供。
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 即可。
以STM32为例,来看下相关的文件名定义,以及它们的相互关系。以STM32为例,来看下相关的文件名定义,以及它们的相互关系。
由于CORTEX-M3有一些可选硬件如MPU,在<device.h>中包含core_cm3.h和system_<device>.h时需注意以下一点,以STM32.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; 系统级的异常号已经确定,不能更改,且必须为负值,以和设备相关的中断区别。
CMSIS的中断定义 中断处理函数的定义,一般在启动代码中声明,加入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++; } }