Using.txt 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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_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_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_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_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) are device specific. In addition, the \ref startup_s_pg is also compiler vendor specific.
  26. The various compiler vendor tool chains may provide folders that contain the CMSIS files for each supported device.
  27. For example, the following files are provided in MDK to support the STM32F10x Connectivity Line device variants:
  28. <table class="cmtable">
  29. <tr>
  30. <th>File</th>
  31. <th>Description</th>
  32. </tr>
  33. <tr>
  34. <td>".\ARM\Startup\ST\STM32F10x\startup_stm32f10x_cl.s"</td>
  35. <td>\ref startup_s_pg for the STM32F10x Connectivity Line device variants.</td>
  36. </tr>
  37. <tr>
  38. <td>".\ARM\Startup\ST\STM32F10x\system_stmf10x.c"</td>
  39. <td>\ref system_c_pg for the STM32F10x device families.</td>
  40. </tr>
  41. <tr>
  42. <td>".\ARM\INC\ST\STM32F10x\stm32f10x.h"</td>
  43. <td>\ref device_h_pg for the STM32F10x device families.</td>
  44. </tr>
  45. <tr>
  46. <td>".\ARM\INC\ST\STM32F10x\system_stm32f10x.h"</td>
  47. <td>\ref system_Device_h_sec for the STM32F10x device families.</td>
  48. </tr>
  49. </table>
  50. \note The silicon vendors create these device-specific CMSIS-Core (Cortex-M) files based on \ref templates_pg provide by Arm.
  51. Thereafter, the functions described under <a href="Modules.html">\b Reference </a> can be used in the application.
  52. \b Examples
  53. - \subpage using_CMSIS is a simple example that shows the usage of the CMSIS layer.
  54. - \subpage using_VTOR_pg shows how to remap the interrupt vector table.
  55. - \subpage using_ARM_pg explains how to use CMSIS-Core (Cortex-M) for Arm processors.
  56. \page using_CMSIS Basic CMSIS Example
  57. A typical example for using the CMSIS layer is provided below. The example is based on a STM32F10x Device.
  58. \code
  59. #include <stm32f10x.h> // File name depends on device used
  60. uint32_t volatile msTicks; // Counter for millisecond Interval
  61. void SysTick_Handler (void) { // SysTick Interrupt Handler
  62. msTicks++; // Increment Counter
  63. }
  64. void WaitForTick (void) {
  65. uint32_t curTicks;
  66. curTicks = msTicks; // Save Current SysTick Value
  67. while (msTicks == curTicks) { // Wait for next SysTick Interrupt
  68. __WFE (); // Power-Down until next Event/Interrupt
  69. }
  70. }
  71. void TIM1_UP_IRQHandler (void) { // Timer Interrupt Handler
  72. ; // Add user code here
  73. }
  74. void timer1_init(int frequency) { // Set up Timer (device specific)
  75. NVIC_SetPriority (TIM1_UP_IRQn, 1); // Set Timer priority
  76. NVIC_EnableIRQ (TIM1_UP_IRQn); // Enable Timer Interrupt
  77. }
  78. void Device_Initialization (void) { // Configure & Initialize MCU
  79. if (SysTick_Config (SystemCoreClock / 1000)) { // SysTick 1mSec
  80. : // Handle Error
  81. }
  82. timer1_init (); // setup device-specific timer
  83. }
  84. // The processor clock is initialized by CMSIS startup + system file
  85. void main (void) { // user application starts here
  86. Device_Initialization (); // Configure & Initialize MCU
  87. while (1) { // Endless Loop (the Super-Loop)
  88. __disable_irq (); // Disable all interrupts
  89. Get_InputValues (); // Read Values
  90. __enable_irq (); // Enable all interrupts
  91. Calculation_Response (); // Calculate Results
  92. Output_Response (); // Output Results
  93. WaitForTick (); // Synchronize to SysTick Timer
  94. }
  95. }
  96. \endcode
  97. 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
  98. of a project. This allows you to generate generic software components that use the device selected in a project.
  99. \code
  100. #include "RTE_Components.h" // include information about project configuration
  101. #include CMSIS_device_header // include <device>.h file
  102. \endcode
  103. \page using_VTOR_pg Using Interrupt Vector Remap
  104. Most Cortex-M processors provide VTOR register for remapping interrupt vectors. The following example shows
  105. a typical use case where the interrupt vectors are copied to RAM and the SysTick_Handler is replaced.
  106. \code
  107. #include "ARMCM3.h" // Device header
  108. /* externals from startup_ARMCM3.s */
  109. extern uint32_t __Vectors[]; /* vector table ROM */
  110. #define VECTORTABLE_SIZE (256) /* size Cortex-M3 vector table */
  111. #define VECTORTABLE_ALIGNMENT (0x100ul) /* 16 Cortex + 32 ARMCM3 = 48 words */
  112. /* next power of 2 = 256 */
  113. /* new vector table in RAM */
  114. uint32_t vectorTable_RAM[VECTORTABLE_SIZE] __attribute__(( aligned (VECTORTABLE_ALIGNMENT) ));
  115. /*----------------------------------------------------------------------------
  116. SysTick_Handler
  117. *----------------------------------------------------------------------------*/
  118. volatile uint32_t msTicks = 0; /* counts 1ms timeTicks */
  119. void SysTick_Handler(void) {
  120. msTicks++; /* increment counter */
  121. }
  122. /*----------------------------------------------------------------------------
  123. SysTick_Handler (RAM)
  124. *----------------------------------------------------------------------------*/
  125. volatile uint32_t msTicks_RAM = 0; /* counts 1ms timeTicks */
  126. void SysTick_Handler_RAM(void) {
  127. msTicks_RAM++; /* increment counter */
  128. }
  129. /*----------------------------------------------------------------------------
  130. MAIN function
  131. *----------------------------------------------------------------------------*/
  132. int main (void) {
  133. uint32_t i;
  134. for (i = 0; i < VECTORTABLE_SIZE; i++) {
  135. vectorTable_RAM[i] = __Vectors[i]; /* copy vector table to RAM */
  136. }
  137. /* replace SysTick Handler */
  138. vectorTable_RAM[SysTick_IRQn + 16] = (uint32_t)SysTick_Handler_RAM;
  139. /* relocate vector table */
  140. __disable_irq();
  141. SCB->VTOR = (uint32_t)&vectorTable_RAM;
  142. __DSB();
  143. __enable_irq();
  144. SystemCoreClockUpdate(); /* Get Core Clock Frequency */
  145. SysTick_Config(SystemCoreClock / 1000ul); /* Setup SysTick Timer for 1 msec */
  146. while(1);
  147. }
  148. \endcode
  149. \page using_ARM_pg Using CMSIS with generic Arm Processors
  150. Arm provides CMSIS-Core (Cortex-M) files for the supported Arm Processors and for various compiler vendors.
  151. These files can be used when standard Arm processors should be used in a project.
  152. The table below lists the folder and device names of the Arm processors.
  153. <table class="cmtable">
  154. <tr>
  155. <th>Folder</th>
  156. <th>Processor</th>
  157. <th>Description</th>
  158. </tr>
  159. <tr>
  160. <td>".\Device\ARM\ARMCM0"</td>
  161. <td>Cortex-M0</td>
  162. <td>Contains \b Include and \b Source template files configured for the Cortex-M0 processor.
  163. The device name is ARMCM0 and the name of the \ref device_h_pg is <ARMCM0.h>.
  164. </td>
  165. </tr>
  166. <tr>
  167. <td>".\Device\ARM\ARMCM0plus"</td>
  168. <td>Cortex-M0+</td>
  169. <td>Contains \b Include and \b Source template files configured for the Cortex-M0+ processor.
  170. The device name is ARMCM0plus and the name of the \ref device_h_pg is <ARMCM0plus.h>.
  171. </td>
  172. </tr>
  173. <tr>
  174. <td>".\Device\ARM\ARMCM3"</td>
  175. <td>Cortex-M3</td>
  176. <td>Contains \b Include and \b Source template files configured for the Cortex-M3 processor.
  177. The device name is ARMCM3 and the name of the \ref device_h_pg is <ARMCM3.h>.
  178. </td>
  179. </tr>
  180. <tr>
  181. <td>".\Device\ARM\ARMCM4"</td>
  182. <td>Cortex-M4</td>
  183. <td>Contains \b Include and \b Source template files configured for the Cortex-M4 processor.
  184. The device name is ARMCM4 and the name of the \ref device_h_pg is <ARMCM4.h>.
  185. </td>
  186. </tr>
  187. <tr>
  188. <td>".\Device\ARM\ARMCM7"</td>
  189. <td>Cortex-M7</td>
  190. <td>Contains \b Include and \b Source template files configured for the Cortex-M7 processor.
  191. The device name is ARMCM7 and the name of the \ref device_h_pg is <ARMCM7.h>.
  192. </td>
  193. </tr>
  194. \if ARMSC
  195. <tr>
  196. <td>".\Device\ARM\ARMSC000"</td>
  197. <td>SecurCore SC000</td>
  198. <td>Contains \b Include and \b Source template files configured for the SecurCore SC000 processor.
  199. The device name is ARMSC000 and the name of the \ref device_h_pg is <ARMSC000.h>.
  200. </td>
  201. </tr>
  202. <tr>
  203. <td>".\Device\ARM\ARMSC300"</td>
  204. <td>SecurCore SC300</td>
  205. <td>Contains \b Include and \b Source template files configured for the SecurCore SC300 processor.
  206. The device name is ARMSC300 and the name of the \ref device_h_pg is <ARMSC300.h>.
  207. </td>
  208. </tr>
  209. \endif
  210. </table>
  211. \note
  212. 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
  213. of a project. This allows you to generate generic software components that adjust to the device settings.
  214. \section using_ARM_Lib_sec Create generic Libraries with CMSIS
  215. The CMSIS Processor and Core Peripheral files allow also to create generic libraries.
  216. The <a href="../../DSP/html/index.html">\b CMSIS-DSP </a> Libraries are an example for such a generic library.
  217. 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.
  218. The define <b>__CMSIS_GENERIC</b> disables device-dependent features such as the <b>SysTick</b> timer and the <b>Interrupt System</b>.
  219. Refer to \ref core_config_sect for a list of the available <b>core_<cpu>.h</b> header files.
  220. \b Example:
  221. 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
  222. 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
  223. 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,
  224. and \ref ITM_Debug_gr.
  225. \code
  226. #define __CMSIS_GENERIC /* disable NVIC and Systick functions */
  227. #if defined (CORTEX_M7)
  228. #include "core_cm7.h"
  229. #elif defined (CORTEX_M4)
  230. #include "core_cm4.h"
  231. #elif defined (CORTEX_M3)
  232. #include "core_cm3.h"
  233. #elif defined (CORTEX_M0)
  234. #include "core_cm0.h"
  235. #elif defined (CORTEX_M0PLUS)
  236. #include "core_cm0plus.h"
  237. #else
  238. #error "Processor not specified or unsupported."
  239. #endif
  240. \endcode
  241. */