]> begriffs open source - cmsis/blob - CMSIS/DoxyGen/Core/src/Using.txt
Initial CMSIS-RTOS v2 API Documentation, Example and Component Viewer Description
[cmsis] / CMSIS / DoxyGen / Core / src / Using.txt
1 /** 
2 \page using_pg  Using CMSIS in Embedded Applications
3
4
5 \details 
6
7 To use the CMSIS-CORE the following files are added to the embedded application:
8  - \ref startup_s_pg with reset handler and exception vectors.
9  - \ref system_c_pg with general device configuration (i.e. for clock and BUS setup).
10  - \ref device_h_pg gives access to processor core and all peripherals.
11
12 \note The files \ref startup_s_pg and \ref system_c_pg may require application specific adaptations and therefore should be copied 
13       into the application project folder prior configuration. The \ref device_h_pg is included in all source files that need device access 
14           and can be stored on a central include folder that is generic for all projects.
15
16 The \ref startup_s_pg is executed after reset and calls \ref SystemInit. After the system initialization control is transferred to the C/C++ run-time
17 library which performs initialization and calls the \b main function in the user code. In addition the \ref startup_s_pg contains all exception and
18 interrupt vectors and implements a default function for every interrupt. It may also contain stack and heap configurations for the user application.
19
20 The \ref system_c_pg performs the setup for the processor clock. The variable \ref SystemCoreClock indicates the CPU clock speed.
21 \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. 
22
23 The \ref device_h_pg is the central include file that the application programmer is using in the C source code.  It provides the following features:
24  - \ref peripheral_gr provides a standardized register layout for all peripherals. Optionally functions for device-specific peripherals may be available.
25  - \ref NVIC_gr can be accessed with standardized symbols and functions for the Nested Interrupt Vector Controller (NVIC) are provided.
26  - \ref intrinsic_CPU_gr allow to access special instructions, for example for activating sleep mode or the NOP instruction.
27  - \ref intrinsic_SIMD_gr provide access to the DSP-oriented instructions.
28  - \ref SysTick_gr function to configure and start a periodic timer interrupt.
29  - \ref ITM_Debug_gr are functions that allow printf-style I/O via the CoreSight Debug Unit and ITM communication.
30
31
32 \image html "CMSIS_CORE_Files_user.png" "CMSIS-CORE User Files"
33
34 The CMSIS-CORE are device specific. In addition, the \ref startup_s_pg is also compiler vendor specific. 
35 The various compiler vendor tool chains may provide folders that contain the CMSIS files for each supported device.
36   
37
38 For example, the following files are provided in MDK-ARM to support the STM32F10x Connectivity Line device variants:
39
40 <table class="cmtable">
41     <tr>
42       <th>File</th>
43       <th>Description</th>
44     </tr>
45     <tr>
46       <td>".\ARM\Startup\ST\STM32F10x\startup_stm32f10x_cl.s"</td>
47       <td>\ref startup_s_pg for the STM32F10x Connectivity Line device variants.</td>
48     </tr>
49     <tr>
50       <td>".\ARM\Startup\ST\STM32F10x\system_stmf10x.c"</td>
51       <td>\ref system_c_pg for the STM32F10x device families.</td>
52     </tr>
53     <tr>
54       <td>".\ARM\INC\ST\STM32F10x\stm32f10x.h"</td>
55       <td>\ref device_h_pg for the STM32F10x device families.</td>
56     </tr>
57     <tr>
58       <td>".\ARM\INC\ST\STM32F10x\system_stm32f10x.h"</td>
59       <td>\ref system_Device_h_sec for the STM32F10x device families.</td>
60     </tr>
61 </table>
62
63
64 \note The silicon vendors create these device-specific CMSIS-CORE files based on \ref templates_pg provide by ARM.
65
66 Thereafter, the functions described under <a href="Modules.html">\b Reference </a> can be used in the application.
67
68 \b Examples
69  - \subpage using_CMSIS is a simple example that shows the usage of the CMSIS layer.
70  - \subpage using_VTOR_pg shows how to remap the interrupt vector table.
71  - \subpage using_ARM_pg explains how to use CMSIS-CORE for ARM processors.
72
73
74 \page using_CMSIS Basic CMSIS Example
75
76 A typical example for using the CMSIS layer is provided below. The example is based on a STM32F10x Device. 
77     
78 \code
79 #include <stm32f10x.h>                           // File name depends on device used
80  
81 uint32_t volatile msTicks;                       // Counter for millisecond Interval
82  
83 void SysTick_Handler (void) {                    // SysTick Interrupt Handler
84   msTicks++;                                     // Increment Counter
85 }
86  
87 void WaitForTick (void)  {
88   uint32_t curTicks;
89  
90   curTicks = msTicks;                            // Save Current SysTick Value
91   while (msTicks == curTicks)  {                 // Wait for next SysTick Interrupt
92     __WFE ();                                    // Power-Down until next Event/Interrupt
93   }
94 }
95  
96 void TIM1_UP_IRQHandler (void) {                 // Timer Interrupt Handler
97   ;                                              // Add user code here
98 }
99  
100 void timer1_init(int frequency) {                // Set up Timer (device specific)
101   NVIC_SetPriority (TIM1_UP_IRQn, 1);            // Set Timer priority
102   NVIC_EnableIRQ (TIM1_UP_IRQn);                 // Enable Timer Interrupt
103 }
104  
105  
106 void Device_Initialization (void)  {             // Configure & Initialize MCU
107   if (SysTick_Config (SystemCoreClock / 1000)) { // SysTick 1mSec
108        : // Handle Error 
109   }
110   timer1_init ();                                // setup device-specific timer
111 }
112  
113  
114 // The processor clock is initialized by CMSIS startup + system file
115 void main (void) {                                   // user application starts here
116   Device_Initialization ();                      // Configure & Initialize MCU
117   while (1)  {                                   // Endless Loop (the Super-Loop)
118     __disable_irq ();                            // Disable all interrupts
119     Get_InputValues ();                          // Read Values
120     __enable_irq ();                             // Enable all interrupts 
121     Calculation_Response ();                     // Calculate Results
122     Output_Response ();                          // Output Results
123     WaitForTick ();                              // Synchronize to SysTick Timer
124   }
125 }
126 \endcode
127
128 \page using_VTOR_pg Using Interrupt Vector Remap
129
130 Most Cortex-M processors provide VTOR register for remapping interrupt vectors. The following example shows
131 a typical use case where the interrupt vectors are copied to RAM and the SysTick_Handler is replaced.
132
133 \code
134 #include "ARMCM3.h"                     // Device header
135  
136 /* externals from startup_ARMCM3.s */
137 extern uint32_t __Vectors[];                             /* vector table ROM  */
138  
139 #define VECTORTABLE_SIZE        (256)          /* size Cortex-M3 vector table */
140 #define VECTORTABLE_ALIGNMENT   (0x100ul) /* 16 Cortex + 32 ARMCM3 = 48 words */
141                                           /* next power of 2 = 256            */
142  
143 /* new vector table in RAM */
144 uint32_t vectorTable_RAM[VECTORTABLE_SIZE] __attribute__(( aligned (VECTORTABLE_ALIGNMENT) ));
145  
146 /*----------------------------------------------------------------------------
147   SysTick_Handler
148  *----------------------------------------------------------------------------*/
149 volatile uint32_t msTicks = 0;                        /* counts 1ms timeTicks */
150 void SysTick_Handler(void) {
151   msTicks++;                                             /* increment counter */
152 }
153  
154 /*----------------------------------------------------------------------------
155   SysTick_Handler (RAM)
156  *----------------------------------------------------------------------------*/
157 volatile uint32_t msTicks_RAM = 0;                    /* counts 1ms timeTicks */
158 void SysTick_Handler_RAM(void) {
159   msTicks_RAM++;                                         /* increment counter */
160 }
161  
162 /*----------------------------------------------------------------------------
163   MAIN function
164  *----------------------------------------------------------------------------*/
165 int main (void) {
166   uint32_t i;
167    
168   for (i = 0; i < VECTORTABLE_SIZE; i++) {
169     vectorTable_RAM[i] = __Vectors[i];            /* copy vector table to RAM */
170   }
171                                                    /* replace SysTick Handler */
172   vectorTable_RAM[SysTick_IRQn + 16] = (uint32_t)SysTick_Handler_RAM;
173         
174   /* relocate vector table */ 
175   __disable_irq();
176     SCB->VTOR = (uint32_t)&vectorTable_RAM;
177   __DSB();
178   __enable_irq();
179  
180   SystemCoreClockUpdate();                        /* Get Core Clock Frequency */
181   SysTick_Config(SystemCoreClock / 1000ul); /* Setup SysTick Timer for 1 msec */
182    
183   while(1);
184 }
185 \endcode
186     
187 \page using_ARM_pg Using CMSIS with generic ARM Processors
188
189 ARM provides CMSIS-CORE files for the supported ARM Processors and for various compiler vendors. 
190 These files can be used when standard ARM processors should be used in a project.
191 The table below lists the folder and device names of the ARM processors.
192   
193 <table class="cmtable">
194     <tr>
195       <th>Folder</th>
196       <th>Processor</th>
197       <th>Description</th>
198     </tr>
199     <tr>
200       <td>".\Device\ARM\ARMCM0"</td>
201       <td>Cortex-M0</td>
202       <td>Contains \b Include and \b Source template files configured for the Cortex-M0 processor.
203               The device name is ARMCM0 and the name of the \ref device_h_pg is <ARMCM0.h>.
204           </td>
205     </tr>
206     <tr>
207       <td>".\Device\ARM\ARMCM0plus"</td>
208       <td>Cortex-M0+</td>
209       <td>Contains \b Include and \b Source template files configured for the Cortex-M0+ processor.
210               The device name is ARMCM0plus and the name of the \ref device_h_pg is <ARMCM0plus.h>.
211           </td>
212     </tr>
213     <tr>
214       <td>".\Device\ARM\ARMCM3"</td>
215       <td>Cortex-M3</td>
216       <td>Contains \b Include and \b Source template files configured for the Cortex-M3 processor.
217               The device name is ARMCM3 and the name of the \ref device_h_pg is <ARMCM3.h>.
218           </td>
219     </tr>
220     <tr>
221       <td>".\Device\ARM\ARMCM4"</td>
222       <td>Cortex-M4</td>
223       <td>Contains \b Include and \b Source template files configured for the Cortex-M4 processor.
224               The device name is ARMCM4 and the name of the \ref device_h_pg is <ARMCM4.h>.
225           </td>
226     </tr>
227     <tr>
228       <td>".\Device\ARM\ARMCM7"</td>
229       <td>Cortex-M7</td>
230       <td>Contains \b Include and \b Source template files configured for the Cortex-M7 processor.
231               The device name is ARMCM7 and the name of the \ref device_h_pg is <ARMCM7.h>.
232           </td>
233     </tr>
234     <tr>
235       <td>".\Device\ARM\ARMSC000"</td>
236       <td>SecurCore SC000</td>
237       <td>Contains \b Include and \b Source template files configured for the SecurCore SC000 processor.
238               The device name is ARMSC000 and the name of the \ref device_h_pg is <ARMSC000.h>.
239           </td>
240     </tr>
241     <tr>
242       <td>".\Device\ARM\ARMSC300"</td>
243       <td>SecurCore SC300</td>
244       <td>Contains \b Include and \b Source template files configured for the SecurCore SC300 processor.
245               The device name is ARMSC300 and the name of the \ref device_h_pg is <ARMSC300.h>.
246           </td>
247     </tr>
248 </table>
249
250 \section using_ARM_Lib_sec Create generic Libraries with CMSIS
251
252 The CMSIS Processor and Core Peripheral files allow also to create generic libraries. 
253 The <a href="../../DSP/html/index.html">\b CMSIS-DSP </a> Libraries are an example for such a generic library.
254
255 To build a generic Library set the define \b __CMSIS_GENERIC and include the relevant <b>core_<cpu>.h</b> CMSIS CPU & Core Access header file for the processor.
256 The define <b>__CMSIS_GENERIC</b> disables device-dependent features such as the <b>SysTick</b> timer and the <b>Interrupt System</b>.
257 Refer to \ref core_config_sect for a list of the available <b>core_<cpu>.h</b> header files. 
258
259 \b Example:
260
261 The following code section shows the usage of the <b>core_&lt;cpu&gt;.h</b> header files to build a generic library for Cortex-M0, Cortex-M3, Cortex-M4, or Cortex-M7.  To
262 select the processor, the source code uses the define \b CORTEX_M7, \b CORTEX_M4, \b CORTEX_M3, \b CORTEX_M0, or \b CORTEX_M0PLUS. By using this header file, the source code can access 
263 the functions for \ref Core_Register_gr, \ref intrinsic_CPU_gr, \ref intrinsic_SIMD_gr, and \ref ITM_Debug_gr.
264
265
266 \code
267 #define __CMSIS_GENERIC              /* disable NVIC and Systick functions */
268
269 #if defined (CORTEX_M7)
270   #include "core_cm7.h"
271 #if defined (CORTEX_M4)
272   #include "core_cm4.h"
273 #elif defined (CORTEX_M3)
274   #include "core_cm3.h"
275 #elif defined (CORTEX_M0)
276   #include "core_cm0.h"
277 #elif defined (CORTEX_M0PLUS)
278   #include "core_cm0plus.h"
279 #else
280   #error "Processor not specified or unsupported."
281 #endif
282
283 \endcode
284
285
286 */