1# Using CMSIS in Embedded Applications {#using_pg}
2
3To use the CMSIS-Core-A the following files are added to the embedded application:
4 - \ref startup_c_pg with reset handler and exception vectors.
5 - \ref system_c_pg with general device configuration (i.e. for clock and bus setup).
6 - \ref device_h_pg gives access to processor core and all peripherals.
7 - \ref mem_h_pg contains basic memory configurations.
8 - \ref mmu_c_pg contains the memory management unit setup.
9
10> **Note**
11> - The files \ref startup_c_pg, \ref system_c_pg, \ref mem_h_pg, and \ref mmu_c_pg may require application specific adaptations and therefore should be copied into the application project folder prior configuration. The \ref device_h_pg is included in all source files that need device access and can be stored on a central include folder that is generic for all projects.
12
13The `Reset_Handler` defined in \ref startup_c_pg is executed after reset.
14The default initialization sequence is
15 - set the vector base address register (\ref __set_VBAR),
16 - set stacks for each exception mode (\ref __set_mode, \ref __set_SP),
17 - call \ref SystemInit.
18
19After the system initialization control is transferred to the C/C++ run-time
20library which performs initialization and calls the \b main function in the user code. In addition the \ref startup_c_pg contains a weak default handler
21implementation for every exception. It may also contain stack and heap configurations for the user application.
22
23The \ref system_c_pg performs the setup for the processor clock and the initialization of memory caches, memory management unit, generic interrupt interface
24and floating point unit. The variable \ref SystemCoreClock indicates the CPU clock speed.
25\ref system_init_gr describes the minimum feature set. In addition the file may contain functions for the memory bus setup and clock re-configuration.
26
27The \ref device_h_pg is the central include file that the application programmer is using in the C/C++ source code. It provides the following features:
28 - \ref peripheral_gr provides a standardized register layout for all peripherals. Optionally functions for device-specific peripherals may be available.
29 - \ref GIC_functions can be accessed with standardized symbols and functions for the General Interrupt Controller (GIC) are provided.
30 - \ref CMSIS_Core_InstructionInterface allow to access special instructions, for example for activating sleep mode or the NOP instruction.
31 - \ref PL1_timer_functions "Generic" and \ref PTM_timer_functions "Private" Timer functions to configure and start a periodic timer interrupt.
32 - \ref L1_cache_functions "Level 1" and \ref L2_cache_functions "Level 2" Cache controller functions to enable, disable, clean and invalidate caches.
33
34The use of \ref device_h_pg can be abstracted with the `#define CMSIS_header_file` provided in [RTE_Components.h](https://open-cmsis-pack.github.io/Open-CMSIS-Pack-Spec/main/html/cp_Packs.html#cp_RTECompH). This allows to have uniform include code in the application independent of the target device.
35
36```c
37#include "RTE_Components.h"                      // include information about project configuration
38#include CMSIS_device_header                     // include <Device>.h file
39```
40
41![CMSIS-Core-A User Files](./images/CMSIS_CORE_A_Files_user.png)
42
43The CMSIS-Core-A user files are device specific. In addition, the \ref startup_c_pg is also compiler vendor specific.
44The various compiler vendor tool chains may provide folders that contain the CMSIS files for each supported device.
45
46> **Note**
47> - The silicon vendors create these device-specific CMSIS-Core-A files based on \ref templates_pg provide by Arm.
48
49Thereafter, the functions described under [API Reference](modules.html) can be used in the application.
50
51**Examples:**
52 - \subpage using_CMSIS is a simple example that shows the usage of the CMSIS layer.
53 - \subpage using_ARM_pg explains how to use CMSIS-Core-M for Arm processors.
54
55## CMSIS Basic Example {#using_CMSIS}
56
57A typical example for using the CMSIS layer is provided below. The example is based on an unspecific Cortex-A9 Device.
58
59```c
60#include <ARMCA9.h>                              // File name depends on device used
61
62static const uint32_t TICK_RATE_HZ = 1000U;
63
64uint32_t volatile msTicks;                       // Counter for millisecond Interval
65
66static void SysTick_Handler( void )
67{
68  msTicks++;                                     // Increment Counter
69}
70
71// We use the Private Tiemer (PTIM) of the Cortex-A9 FVP Model here.
72// In general the available Timers are highly vendor specific for Cortex-A processors.
73void private_timer_init(void) {
74
75  PTIM_SetLoadValue ((SystemCoreClock/TICK_RATE_HZ) - 1U);
76  PTIM_SetControl (PTIM_GetControl() | 7U);
77
78  /* Install SysTick_Handler as the interrupt function for PTIM */
79  IRQ_SetHandler((IRQn_ID_t)PrivTimer_IRQn, SysTick_Handler);
80
81  /* Determine number of implemented priority bits */
82  IRQ_SetPriority ((IRQn_ID_t)PrivTimer_IRQn, IRQ_PRIORITY_Msk);
83
84  /* Set lowest priority -1 */
85  IRQ_SetPriority ((IRQn_ID_t)PrivTimer_IRQn, GIC_GetPriority((IRQn_ID_t)PrivTimer_IRQn)-1);
86
87  /* Enable IRQ */
88  IRQ_Enable ((IRQn_ID_t)PrivTimer_IRQn);
89}
90
91/* Delay execution for given amount of ticks */
92void Delay(uint32_t ticks)  {
93  uint32_t tgtTicks = msTicks + ticks;             // target tick count to delay execution to
94  while (msTicks == tgtTicks)  {
95    __WFE ();                                      // Power-Down until next Event/Interrupt
96  }
97}
98
99/* main function */
100int main(void)
101{
102  /* Initialize device HAL here */
103  private_timer_init();
104
105  static uint8_t ledState = 0;
106
107  /* Infinite loop */
108  while (1)
109  {
110    /* Add application code here */
111    ledState = !ledState;
112    Delay(500);
113  }
114}
115```
116
117## Using CMSIS with generic Arm Processors {#using_ARM_pg}
118
119The [Cortex_DFP pack](https://github.com/ARM-software/Cortex_DFP) provides generic device definitions for standard Arm Cortex-A cores and contains corresponding. These generic Arm devices can be used as a target for embedded programs, with execution, for example, on processor simulation models.
120