Using.txt 14 KB

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