Robobo
core_cm0.h
Go to the documentation of this file.
1 /**************************************************************************/
23 #if defined ( __ICCARM__ )
24  #pragma system_include /* treat file as system include file for MISRA check */
25 #endif
26 
27 #ifdef __cplusplus
28  extern "C" {
29 #endif
30 
31 #ifndef __CORE_CM0_H_GENERIC
32 #define __CORE_CM0_H_GENERIC
33 
34 
63 /*******************************************************************************
64  * CMSIS definitions
65  ******************************************************************************/
74 /* CMSIS CM0 definitions */
75 #define __CM0_CMSIS_VERSION_MAIN (0x02)
76 #define __CM0_CMSIS_VERSION_SUB (0x10)
77 #define __CM0_CMSIS_VERSION ((__CM0_CMSIS_VERSION_MAIN << 16) | __CM0_CMSIS_VERSION_SUB)
79 #define __CORTEX_M (0x00)
82 #if defined ( __CC_ARM )
83  #define __ASM __asm
84  #define __INLINE __inline
86 #elif defined ( __ICCARM__ )
87  #define __ASM __asm
88  #define __INLINE inline
90 #elif defined ( __GNUC__ )
91  #define __ASM __asm
92  #define __INLINE inline
94 #elif defined ( __TASKING__ )
95  #define __ASM __asm
96  #define __INLINE inline
98 #endif
99 
101 #define __FPU_USED 0
102 
103 #if defined ( __CC_ARM )
104  #if defined __TARGET_FPU_VFP
105  #warning "Compiler generates FPU instructions for a device without an FPU (check __FPU_PRESENT)"
106  #endif
107 #elif defined ( __ICCARM__ )
108  #if defined __ARMVFP__
109  #warning "Compiler generates FPU instructions for a device without an FPU (check __FPU_PRESENT)"
110  #endif
111 
112 #elif defined ( __GNUC__ )
113  #if defined (__VFP_FP__) && !defined(__SOFTFP__)
114  #warning "Compiler generates FPU instructions for a device without an FPU (check __FPU_PRESENT)"
115  #endif
116 
117 #elif defined ( __TASKING__ )
118  /* add preprocessor checks */
119 #endif
120 
121 #include <stdint.h>
122 #include "core_cmInstr.h"
123 #include "core_cmFunc.h"
125 #endif /* __CORE_CM0_H_GENERIC */
126 
127 #ifndef __CMSIS_GENERIC
128 
129 #ifndef __CORE_CM0_H_DEPENDANT
130 #define __CORE_CM0_H_DEPENDANT
131 
132 /* check device defines and use defaults */
133 #if defined __CHECK_DEVICE_DEFINES
134  #ifndef __CM0_REV
135  #define __CM0_REV 0x0000
136  #warning "__CM0_REV not defined in device header file; using default!"
137  #endif
138 
139  #ifndef __NVIC_PRIO_BITS
140  #define __NVIC_PRIO_BITS 2
141  #warning "__NVIC_PRIO_BITS not defined in device header file; using default!"
142  #endif
143 
144  #ifndef __Vendor_SysTickConfig
145  #define __Vendor_SysTickConfig 0
146  #warning "__Vendor_SysTickConfig not defined in device header file; using default!"
147  #endif
148 #endif
149 
150 /* IO definitions (access restrictions to peripheral registers) */
151 #ifdef __cplusplus
152  #define __I volatile
153 #else
154  #define __I volatile const
155 #endif
156 #define __O volatile
157 #define __IO volatile
159 
163 /*******************************************************************************
164  * Register Abstraction
165  ******************************************************************************/
166 
182 typedef union
183 {
184  struct
185  {
186 #if (__CORTEX_M != 0x04)
187  uint32_t _reserved0:27;
188 #else
189  uint32_t _reserved0:16;
190  uint32_t GE:4;
191  uint32_t _reserved1:7;
192 #endif
193  uint32_t Q:1;
194  uint32_t V:1;
195  uint32_t C:1;
196  uint32_t Z:1;
197  uint32_t N:1;
198  } b;
199  uint32_t w;
200 } APSR_Type;
201 
202 
205 typedef union
206 {
207  struct
208  {
209  uint32_t ISR:9;
210  uint32_t _reserved0:23;
211  } b;
212  uint32_t w;
213 } IPSR_Type;
214 
215 
218 typedef union
219 {
220  struct
221  {
222  uint32_t ISR:9;
223 #if (__CORTEX_M != 0x04)
224  uint32_t _reserved0:15;
225 #else
226  uint32_t _reserved0:7;
227  uint32_t GE:4;
228  uint32_t _reserved1:4;
229 #endif
230  uint32_t T:1;
231  uint32_t IT:2;
232  uint32_t Q:1;
233  uint32_t V:1;
234  uint32_t C:1;
235  uint32_t Z:1;
236  uint32_t N:1;
237  } b;
238  uint32_t w;
239 } xPSR_Type;
240 
241 
244 typedef union
245 {
246  struct
247  {
248  uint32_t nPRIV:1;
249  uint32_t SPSEL:1;
250  uint32_t FPCA:1;
251  uint32_t _reserved0:29;
252  } b;
253  uint32_t w;
254 } CONTROL_Type;
255 
267 typedef struct
268 {
269  __IO uint32_t ISER[1];
270  uint32_t RESERVED0[31];
271  __IO uint32_t ICER[1];
272  uint32_t RSERVED1[31];
273  __IO uint32_t ISPR[1];
274  uint32_t RESERVED2[31];
275  __IO uint32_t ICPR[1];
276  uint32_t RESERVED3[31];
277  uint32_t RESERVED4[64];
278  __IO uint32_t IP[8];
279 } NVIC_Type;
280 
292 typedef struct
293 {
294  __I uint32_t CPUID;
295  __IO uint32_t ICSR;
296  uint32_t RESERVED0;
297  __IO uint32_t AIRCR;
298  __IO uint32_t SCR;
299  __IO uint32_t CCR;
300  uint32_t RESERVED1;
301  __IO uint32_t SHP[2];
302  __IO uint32_t SHCSR;
303 } SCB_Type;
304 
305 /* SCB CPUID Register Definitions */
306 #define SCB_CPUID_IMPLEMENTER_Pos 24
307 #define SCB_CPUID_IMPLEMENTER_Msk (0xFFUL << SCB_CPUID_IMPLEMENTER_Pos)
309 #define SCB_CPUID_VARIANT_Pos 20
310 #define SCB_CPUID_VARIANT_Msk (0xFUL << SCB_CPUID_VARIANT_Pos)
312 #define SCB_CPUID_ARCHITECTURE_Pos 16
313 #define SCB_CPUID_ARCHITECTURE_Msk (0xFUL << SCB_CPUID_ARCHITECTURE_Pos)
315 #define SCB_CPUID_PARTNO_Pos 4
316 #define SCB_CPUID_PARTNO_Msk (0xFFFUL << SCB_CPUID_PARTNO_Pos)
318 #define SCB_CPUID_REVISION_Pos 0
319 #define SCB_CPUID_REVISION_Msk (0xFUL << SCB_CPUID_REVISION_Pos)
321 /* SCB Interrupt Control State Register Definitions */
322 #define SCB_ICSR_NMIPENDSET_Pos 31
323 #define SCB_ICSR_NMIPENDSET_Msk (1UL << SCB_ICSR_NMIPENDSET_Pos)
325 #define SCB_ICSR_PENDSVSET_Pos 28
326 #define SCB_ICSR_PENDSVSET_Msk (1UL << SCB_ICSR_PENDSVSET_Pos)
328 #define SCB_ICSR_PENDSVCLR_Pos 27
329 #define SCB_ICSR_PENDSVCLR_Msk (1UL << SCB_ICSR_PENDSVCLR_Pos)
331 #define SCB_ICSR_PENDSTSET_Pos 26
332 #define SCB_ICSR_PENDSTSET_Msk (1UL << SCB_ICSR_PENDSTSET_Pos)
334 #define SCB_ICSR_PENDSTCLR_Pos 25
335 #define SCB_ICSR_PENDSTCLR_Msk (1UL << SCB_ICSR_PENDSTCLR_Pos)
337 #define SCB_ICSR_ISRPREEMPT_Pos 23
338 #define SCB_ICSR_ISRPREEMPT_Msk (1UL << SCB_ICSR_ISRPREEMPT_Pos)
340 #define SCB_ICSR_ISRPENDING_Pos 22
341 #define SCB_ICSR_ISRPENDING_Msk (1UL << SCB_ICSR_ISRPENDING_Pos)
343 #define SCB_ICSR_VECTPENDING_Pos 12
344 #define SCB_ICSR_VECTPENDING_Msk (0x1FFUL << SCB_ICSR_VECTPENDING_Pos)
346 #define SCB_ICSR_VECTACTIVE_Pos 0
347 #define SCB_ICSR_VECTACTIVE_Msk (0x1FFUL << SCB_ICSR_VECTACTIVE_Pos)
349 /* SCB Application Interrupt and Reset Control Register Definitions */
350 #define SCB_AIRCR_VECTKEY_Pos 16
351 #define SCB_AIRCR_VECTKEY_Msk (0xFFFFUL << SCB_AIRCR_VECTKEY_Pos)
353 #define SCB_AIRCR_VECTKEYSTAT_Pos 16
354 #define SCB_AIRCR_VECTKEYSTAT_Msk (0xFFFFUL << SCB_AIRCR_VECTKEYSTAT_Pos)
356 #define SCB_AIRCR_ENDIANESS_Pos 15
357 #define SCB_AIRCR_ENDIANESS_Msk (1UL << SCB_AIRCR_ENDIANESS_Pos)
359 #define SCB_AIRCR_SYSRESETREQ_Pos 2
360 #define SCB_AIRCR_SYSRESETREQ_Msk (1UL << SCB_AIRCR_SYSRESETREQ_Pos)
362 #define SCB_AIRCR_VECTCLRACTIVE_Pos 1
363 #define SCB_AIRCR_VECTCLRACTIVE_Msk (1UL << SCB_AIRCR_VECTCLRACTIVE_Pos)
365 /* SCB System Control Register Definitions */
366 #define SCB_SCR_SEVONPEND_Pos 4
367 #define SCB_SCR_SEVONPEND_Msk (1UL << SCB_SCR_SEVONPEND_Pos)
369 #define SCB_SCR_SLEEPDEEP_Pos 2
370 #define SCB_SCR_SLEEPDEEP_Msk (1UL << SCB_SCR_SLEEPDEEP_Pos)
372 #define SCB_SCR_SLEEPONEXIT_Pos 1
373 #define SCB_SCR_SLEEPONEXIT_Msk (1UL << SCB_SCR_SLEEPONEXIT_Pos)
375 /* SCB Configuration Control Register Definitions */
376 #define SCB_CCR_STKALIGN_Pos 9
377 #define SCB_CCR_STKALIGN_Msk (1UL << SCB_CCR_STKALIGN_Pos)
379 #define SCB_CCR_UNALIGN_TRP_Pos 3
380 #define SCB_CCR_UNALIGN_TRP_Msk (1UL << SCB_CCR_UNALIGN_TRP_Pos)
382 /* SCB System Handler Control and State Register Definitions */
383 #define SCB_SHCSR_SVCALLPENDED_Pos 15
384 #define SCB_SHCSR_SVCALLPENDED_Msk (1UL << SCB_SHCSR_SVCALLPENDED_Pos)
386 
397 typedef struct
398 {
399  __IO uint32_t CTRL;
400  __IO uint32_t LOAD;
401  __IO uint32_t VAL;
402  __I uint32_t CALIB;
403 } SysTick_Type;
404 
405 /* SysTick Control / Status Register Definitions */
406 #define SysTick_CTRL_COUNTFLAG_Pos 16
407 #define SysTick_CTRL_COUNTFLAG_Msk (1UL << SysTick_CTRL_COUNTFLAG_Pos)
409 #define SysTick_CTRL_CLKSOURCE_Pos 2
410 #define SysTick_CTRL_CLKSOURCE_Msk (1UL << SysTick_CTRL_CLKSOURCE_Pos)
412 #define SysTick_CTRL_TICKINT_Pos 1
413 #define SysTick_CTRL_TICKINT_Msk (1UL << SysTick_CTRL_TICKINT_Pos)
415 #define SysTick_CTRL_ENABLE_Pos 0
416 #define SysTick_CTRL_ENABLE_Msk (1UL << SysTick_CTRL_ENABLE_Pos)
418 /* SysTick Reload Register Definitions */
419 #define SysTick_LOAD_RELOAD_Pos 0
420 #define SysTick_LOAD_RELOAD_Msk (0xFFFFFFUL << SysTick_LOAD_RELOAD_Pos)
422 /* SysTick Current Register Definitions */
423 #define SysTick_VAL_CURRENT_Pos 0
424 #define SysTick_VAL_CURRENT_Msk (0xFFFFFFUL << SysTick_VAL_CURRENT_Pos)
426 /* SysTick Calibration Register Definitions */
427 #define SysTick_CALIB_NOREF_Pos 31
428 #define SysTick_CALIB_NOREF_Msk (1UL << SysTick_CALIB_NOREF_Pos)
430 #define SysTick_CALIB_SKEW_Pos 30
431 #define SysTick_CALIB_SKEW_Msk (1UL << SysTick_CALIB_SKEW_Pos)
433 #define SysTick_CALIB_TENMS_Pos 0
434 #define SysTick_CALIB_TENMS_Msk (0xFFFFFFUL << SysTick_VAL_CURRENT_Pos)
436 
445 
452 /* Memory mapping of Cortex-M0 Hardware */
453 #define SCS_BASE (0xE000E000UL)
454 #define CoreDebug_BASE (0xE000EDF0UL)
455 #define SysTick_BASE (SCS_BASE + 0x0010UL)
456 #define NVIC_BASE (SCS_BASE + 0x0100UL)
457 #define SCB_BASE (SCS_BASE + 0x0D00UL)
459 #define SCB ((SCB_Type *) SCB_BASE )
460 #define SysTick ((SysTick_Type *) SysTick_BASE )
461 #define NVIC ((NVIC_Type *) NVIC_BASE )
464 
468 /*******************************************************************************
469  * Hardware Abstraction Layer
470  ******************************************************************************/
471 
480 /* ########################## NVIC functions #################################### */
486 /* Interrupt Priorities are WORD accessible only under ARMv6M */
487 /* The following MACROS handle generation of the register offset and byte masks */
488 #define _BIT_SHIFT(IRQn) ( (((uint32_t)(IRQn) ) & 0x03) * 8 )
489 #define _SHP_IDX(IRQn) ( ((((uint32_t)(IRQn) & 0x0F)-8) >> 2) )
490 #define _IP_IDX(IRQn) ( ((uint32_t)(IRQn) >> 2) )
491 
492 
500 static __INLINE void NVIC_EnableIRQ(IRQn_Type IRQn)
501 {
502  NVIC->ISER[0] = (1 << ((uint32_t)(IRQn) & 0x1F));
503 }
504 
505 
513 static __INLINE void NVIC_DisableIRQ(IRQn_Type IRQn)
514 {
515  NVIC->ICER[0] = (1 << ((uint32_t)(IRQn) & 0x1F));
516 }
517 
518 
528 static __INLINE uint32_t NVIC_GetPendingIRQ(IRQn_Type IRQn)
529 {
530  return((uint32_t) ((NVIC->ISPR[0] & (1 << ((uint32_t)(IRQn) & 0x1F)))?1:0));
531 }
532 
533 
541 static __INLINE void NVIC_SetPendingIRQ(IRQn_Type IRQn)
542 {
543  NVIC->ISPR[0] = (1 << ((uint32_t)(IRQn) & 0x1F));
544 }
545 
546 
554 static __INLINE void NVIC_ClearPendingIRQ(IRQn_Type IRQn)
555 {
556  NVIC->ICPR[0] = (1 << ((uint32_t)(IRQn) & 0x1F)); /* Clear pending interrupt */
557 }
558 
559 
571 static __INLINE void NVIC_SetPriority(IRQn_Type IRQn, uint32_t priority)
572 {
573  if(IRQn < 0) {
574  SCB->SHP[_SHP_IDX(IRQn)] = (SCB->SHP[_SHP_IDX(IRQn)] & ~(0xFF << _BIT_SHIFT(IRQn))) |
575  (((priority << (8 - __NVIC_PRIO_BITS)) & 0xFF) << _BIT_SHIFT(IRQn)); }
576  else {
577  NVIC->IP[_IP_IDX(IRQn)] = (NVIC->IP[_IP_IDX(IRQn)] & ~(0xFF << _BIT_SHIFT(IRQn))) |
578  (((priority << (8 - __NVIC_PRIO_BITS)) & 0xFF) << _BIT_SHIFT(IRQn)); }
579 }
580 
581 
594 static __INLINE uint32_t NVIC_GetPriority(IRQn_Type IRQn)
595 {
596 
597  if(IRQn < 0) {
598  return((uint32_t)((SCB->SHP[_SHP_IDX(IRQn)] >> _BIT_SHIFT(IRQn) ) >> (8 - __NVIC_PRIO_BITS))); } /* get priority for Cortex-M0 system interrupts */
599  else {
600  return((uint32_t)((NVIC->IP[ _IP_IDX(IRQn)] >> _BIT_SHIFT(IRQn) ) >> (8 - __NVIC_PRIO_BITS))); } /* get priority for device specific interrupts */
601 }
602 
603 
608 static __INLINE void NVIC_SystemReset(void)
609 {
610  __DSB(); /* Ensure all outstanding memory accesses included
611  buffered write are completed before reset */
612  SCB->AIRCR = ((0x5FA << SCB_AIRCR_VECTKEY_Pos) |
614  __DSB(); /* Ensure completion of memory access */
615  while(1); /* wait until reset */
616 }
617 
622 /* ################################## SysTick function ############################################ */
628 #if (__Vendor_SysTickConfig == 0)
629 
639 static __INLINE uint32_t SysTick_Config(uint32_t ticks)
640 {
641  if (ticks > SysTick_LOAD_RELOAD_Msk) return (1); /* Reload value impossible */
642 
643  SysTick->LOAD = (ticks & SysTick_LOAD_RELOAD_Msk) - 1; /* set reload register */
644  NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1); /* set Priority for Cortex-M0 System Interrupts */
645  SysTick->VAL = 0; /* Load the SysTick Counter Value */
648  SysTick_CTRL_ENABLE_Msk; /* Enable SysTick IRQ and SysTick Timer */
649  return (0); /* Function successful */
650 }
651 
652 #endif
653 
659 #endif /* __CORE_CM0_H_DEPENDANT */
660 
661 #endif /* __CMSIS_GENERIC */
662 
663 #ifdef __cplusplus
664 }
665 #endif
__I uint32_t CALIB
Definition: core_cm0.h:402
__IO uint32_t ICSR
Definition: core_cm0.h:295
#define ISR(func)
Define service routine.
Definition: interrupt_sam_nvic.h:65
#define SysTick_CTRL_TICKINT_Msk
Definition: core_cm0.h:413
Union type to access the Application Program Status Register (APSR).
Definition: core_cm0.h:182
IRQn
Definition: ARMCM0.h:35
Definition: ARMCM0.h:46
uint32_t w
Definition: core_cm0.h:253
Structure type to access the System Timer (SysTick).
Definition: core_cm0.h:397
#define NVIC
Definition: core_cm0.h:461
__IO uint32_t AIRCR
Definition: core_cm0.h:297
Structure type to access the System Control Block (SCB).
Definition: core_cm0.h:292
#define SysTick
Definition: core_cm0.h:460
#define SCB
Definition: core_cm0.h:459
__IO uint32_t CTRL
Definition: core_cm0.h:399
uint32_t w
Definition: core_cm0.h:212
__IO uint32_t SCR
Definition: core_cm0.h:298
__IO uint32_t VAL
Definition: core_cm0.h:401
__IO uint32_t SHCSR
Definition: core_cm0.h:302
#define SCB_AIRCR_SYSRESETREQ_Msk
Definition: core_cm0.h:360
Union type to access the Control Registers (CONTROL).
Definition: core_cm0.h:244
#define SysTick_CTRL_ENABLE_Msk
Definition: core_cm0.h:416
__IO uint32_t LOAD
Definition: core_cm0.h:400
#define SysTick_CTRL_CLKSOURCE_Msk
Definition: core_cm0.h:410
#define __IO
Definition: core_cm0.h:157
Union type to access the Interrupt Program Status Register (IPSR).
Definition: core_cm0.h:205
#define __I
Definition: core_cm0.h:154
uint32_t w
Definition: core_cm0.h:199
__I uint32_t CPUID
Definition: core_cm0.h:294
#define __NVIC_PRIO_BITS
Definition: ARMCM0.h:62
Structure type to access the Nested Vectored Interrupt Controller (NVIC).
Definition: core_cm0.h:267
CMSIS Cortex-M Core Instruction Access Header File.
#define SCB_AIRCR_VECTKEY_Pos
Definition: core_cm0.h:350
#define SysTick_LOAD_RELOAD_Msk
Definition: core_cm0.h:420
Union type to access the Special-Purpose Program Status Registers (xPSR).
Definition: core_cm0.h:218
uint32_t w
Definition: core_cm0.h:238
CMSIS Cortex-M Core Function Access Header File.
__IO uint32_t CCR
Definition: core_cm0.h:299