1 /****************************************************************************//**
3 * @brief : CMSIS Cortex-M3 Core Peripheral Access Layer Header File
4 * @version : V1.20 simplified for GCC only
5 * @date : 31. March 2010
6 *----------------------------------------------------------------------------
8 * Copyright (C) 2009 ARM Limited. All rights reserved.
10 * ARM Limited (ARM) is supplying this software for use with Cortex-Mx
11 * processor based microcontrollers. This file can be freely distributed
12 * within development tools that are supporting such ARM based processors.
14 * THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
15 * OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
16 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
17 * ARM SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
18 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
20 ******************************************************************************/
22 #ifndef __CM3_CORE_H__
23 #define __CM3_CORE_H__
29 #define __CM3_CMSIS_VERSION_MAIN (0x01) /*!< [31:16] CMSIS HAL main version */
30 #define __CM3_CMSIS_VERSION_SUB (0x20) /*!< [15:0] CMSIS HAL sub version */
31 #define __CM3_CMSIS_VERSION ((__CM3_CMSIS_VERSION_MAIN << 16) | __CM3_CMSIS_VERSION_SUB) /*!< CMSIS HAL version number */
33 #define __CORTEX_M (0x03) /*!< Cortex core */
36 * Lint configuration \n
37 * ----------------------- \n
39 * The following Lint messages will be suppressed and not shown: \n
41 * --- Error 10: --- \n
42 * register uint32_t __regBasePri __asm("basepri"); \n
43 * Error 10: Expecting ';' \n
45 * --- Error 530: --- \n
46 * return(__regBasePri); \n
47 * Warning 530: Symbol '__regBasePri' (line 264) not initialized \n
49 * --- Error 550: --- \n
50 * __regBasePri = (basePri & 0x1ff); \n
52 * Warning 550: Symbol '__regBasePri' (line 271) not accessed \n
54 * --- Error 754: --- \n
55 * uint32_t RESERVED0[24]; \n
56 * Info 754: local structure member '<some, not used in the HAL>' (line 109, file ./cm3_core.h) not referenced \n
58 * --- Error 750: --- \n
59 * #define __CM3_CORE_H__ \n
60 * Info 750: local macro '__CM3_CORE_H__' (line 43, file./cm3_core.h) not referenced \n
62 * --- Error 528: --- \n
63 * static __INLINE void NVIC_DisableIRQ(uint32_t IRQn) \n
64 * Warning 528: Symbol 'NVIC_DisableIRQ(unsigned int)' (line 419, file ./cm3_core.h) not referenced \n
66 * --- Error 751: --- \n
67 * } InterruptType_Type; \n
68 * Info 751: local typedef 'InterruptType_Type' (line 170, file ./cm3_core.h) not referenced \n
71 * Note: To re-enable a Message, insert a space before 'lint' * \n
85 #include <stdint.h> /* Include standard types */
87 #ifndef __NVIC_PRIO_BITS
88 #define __NVIC_PRIO_BITS 4 /*!< standard definition for NVIC Priority Bits */
97 * define access restrictions to peripheral registers
101 #define __I volatile /*!< defines 'read only' permissions */
103 #define __I volatile const /*!< defines 'read only' permissions */
105 #define __O volatile /*!< defines 'write only' permissions */
106 #define __IO volatile /*!< defines 'read / write' permissions */
110 /*******************************************************************************
111 * Register Abstraction
112 ******************************************************************************/
116 #define NVIC_VECTRESET 0 /*!< Vector Reset Bit */
117 #define NVIC_SYSRESETREQ 2 /*!< System Reset Request */
118 #define NVIC_AIRCR_VECTKEY (0x5FA << 16) /*!< AIRCR Key for write access */
119 #define NVIC_AIRCR_ENDIANESS 15 /*!< Endianess */
122 #define CoreDebug_DEMCR_TRCENA (1 << 24) /*!< DEMCR TRCENA enable */
123 #define ITM_TCR_ITMENA 1 /*!< ITM enable */
128 /* memory mapping struct for Nested Vectored Interrupt Controller (NVIC) */
131 __IO uint32_t ISER[8]; /*!< Interrupt Set Enable Register */
132 uint32_t RESERVED0[24];
133 __IO uint32_t ICER[8]; /*!< Interrupt Clear Enable Register */
134 uint32_t RSERVED1[24];
135 __IO uint32_t ISPR[8]; /*!< Interrupt Set Pending Register */
136 uint32_t RESERVED2[24];
137 __IO uint32_t ICPR[8]; /*!< Interrupt Clear Pending Register */
138 uint32_t RESERVED3[24];
139 __IO uint32_t IABR[8]; /*!< Interrupt Active bit Register */
140 uint32_t RESERVED4[56];
141 __IO uint8_t IP[240]; /*!< Interrupt Priority Register, 8Bit wide */
142 uint32_t RESERVED5[644];
143 __O uint32_t STIR; /*!< Software Trigger Interrupt Register */
147 /* memory mapping struct for System Control Block */
150 __I uint32_t CPUID; /*!< CPU ID Base Register */
151 __IO uint32_t ICSR; /*!< Interrupt Control State Register */
152 __IO uint32_t VTOR; /*!< Vector Table Offset Register */
153 __IO uint32_t AIRCR; /*!< Application Interrupt / Reset Control Register */
154 __IO uint32_t SCR; /*!< System Control Register */
155 __IO uint32_t CCR; /*!< Configuration Control Register */
156 __IO uint8_t SHP[12]; /*!< System Handlers Priority Registers (4-7, 8-11, 12-15) */
157 __IO uint32_t SHCSR; /*!< System Handler Control and State Register */
158 __IO uint32_t CFSR; /*!< Configurable Fault Status Register */
159 __IO uint32_t HFSR; /*!< Hard Fault Status Register */
160 __IO uint32_t DFSR; /*!< Debug Fault Status Register */
161 __IO uint32_t MMFAR; /*!< Mem Manage Address Register */
162 __IO uint32_t BFAR; /*!< Bus Fault Address Register */
163 __IO uint32_t AFSR; /*!< Auxiliary Fault Status Register */
164 __I uint32_t PFR[2]; /*!< Processor Feature Register */
165 __I uint32_t DFR; /*!< Debug Feature Register */
166 __I uint32_t ADR; /*!< Auxiliary Feature Register */
167 __I uint32_t MMFR[4]; /*!< Memory Model Feature Register */
168 __I uint32_t ISAR[5]; /*!< ISA Feature Register */
172 /* memory mapping struct for SysTick */
175 __IO uint32_t CTRL; /*!< SysTick Control and Status Register */
176 __IO uint32_t LOAD; /*!< SysTick Reload Value Register */
177 __IO uint32_t VAL; /*!< SysTick Current Value Register */
178 __I uint32_t CALIB; /*!< SysTick Calibration Register */
182 /* memory mapping structur for ITM */
187 __O uint8_t u8; /*!< ITM Stimulus Port 8-bit */
188 __O uint16_t u16; /*!< ITM Stimulus Port 16-bit */
189 __O uint32_t u32; /*!< ITM Stimulus Port 32-bit */
190 } PORT [32]; /*!< ITM Stimulus Port Registers */
191 uint32_t RESERVED0[864];
192 __IO uint32_t TER; /*!< ITM Trace Enable Register */
193 uint32_t RESERVED1[15];
194 __IO uint32_t TPR; /*!< ITM Trace Privilege Register */
195 uint32_t RESERVED2[15];
196 __IO uint32_t TCR; /*!< ITM Trace Control Register */
197 uint32_t RESERVED3[29];
198 __IO uint32_t IWR; /*!< ITM Integration Write Register */
199 __IO uint32_t IRR; /*!< ITM Integration Read Register */
200 __IO uint32_t IMCR; /*!< ITM Integration Mode Control Register */
201 uint32_t RESERVED4[43];
202 __IO uint32_t LAR; /*!< ITM Lock Access Register */
203 __IO uint32_t LSR; /*!< ITM Lock Status Register */
204 uint32_t RESERVED5[6];
205 __I uint32_t PID4; /*!< ITM Product ID Registers */
220 /* memory mapped struct for Interrupt Type */
224 __I uint32_t ICTR; /*!< Interrupt Control Type Register */
225 #if ((defined __CM3_REV) && (__CM3_REV >= 0x200))
226 __IO uint32_t ACTLR; /*!< Auxiliary Control Register */
230 } InterruptType_Type;
233 /* Memory Protection Unit */
234 #if defined (__MPU_PRESENT) && (__MPU_PRESENT == 1)
237 __I uint32_t TYPE; /*!< MPU Type Register */
238 __IO uint32_t CTRL; /*!< MPU Control Register */
239 __IO uint32_t RNR; /*!< MPU Region RNRber Register */
240 __IO uint32_t RBAR; /*!< MPU Region Base Address Register */
241 __IO uint32_t RASR; /*!< MPU Region Attribute and Size Register */
242 __IO uint32_t RBAR_A1; /*!< MPU Alias 1 Region Base Address Register */
243 __IO uint32_t RASR_A1; /*!< MPU Alias 1 Region Attribute and Size Register */
244 __IO uint32_t RBAR_A2; /*!< MPU Alias 2 Region Base Address Register */
245 __IO uint32_t RASR_A2; /*!< MPU Alias 2 Region Attribute and Size Register */
246 __IO uint32_t RBAR_A3; /*!< MPU Alias 3 Region Base Address Register */
247 __IO uint32_t RASR_A3; /*!< MPU Alias 3 Region Attribute and Size Register */
252 /* Core Debug Register */
255 __IO uint32_t DHCSR; /*!< Debug Halting Control and Status Register */
256 __O uint32_t DCRSR; /*!< Debug Core Register Selector Register */
257 __IO uint32_t DCRDR; /*!< Debug Core Register Data Register */
258 __IO uint32_t DEMCR; /*!< Debug Exception and Monitor Control Register */
262 /* Memory mapping of Cortex-M3 Hardware */
263 #define SCS_BASE (0xE000E000) /*!< System Control Space Base Address */
264 #define ITM_BASE (0xE0000000) /*!< ITM Base Address */
265 #define CoreDebug_BASE (0xE000EDF0) /*!< Core Debug Base Address */
266 #define SysTick_BASE (SCS_BASE + 0x0010) /*!< SysTick Base Address */
267 #define NVIC_BASE (SCS_BASE + 0x0100) /*!< NVIC Base Address */
268 #define SCB_BASE (SCS_BASE + 0x0D00) /*!< System Control Block Base Address */
270 #define InterruptType ((InterruptType_Type *) SCS_BASE) /*!< Interrupt Type Register */
271 #define SCB ((SCB_Type *) SCB_BASE) /*!< SCB configuration struct */
272 #define SysTick ((SysTick_Type *) SysTick_BASE) /*!< SysTick configuration struct */
273 #define NVIC ((NVIC_Type *) NVIC_BASE) /*!< NVIC configuration struct */
274 #define ITM ((ITM_Type *) ITM_BASE) /*!< ITM configuration struct */
275 #define CoreDebug ((CoreDebug_Type *) CoreDebug_BASE) /*!< Core Debug configuration struct */
277 #if defined (__MPU_PRESENT) && (__MPU_PRESENT == 1)
278 #define MPU_BASE (SCS_BASE + 0x0D90) /*!< Memory Protection Unit */
279 #define MPU ((MPU_Type*) MPU_BASE) /*!< Memory Protection Unit */
284 /*******************************************************************************
285 * Hardware Abstraction Layer
286 ******************************************************************************/
289 #define __ASM __asm /*!< asm keyword for GNU Compiler */
290 #define __INLINE inline /*!< inline keyword for GNU Compiler */
292 /* ################### Compiler specific Intrinsics ########################### */
294 /* GNU gcc specific functions */
296 static __INLINE void __enable_irq() { __ASM volatile ("cpsie i"); }
297 static __INLINE void __disable_irq() { __ASM volatile ("cpsid i"); }
299 static __INLINE void __enable_fault_irq() { __ASM volatile ("cpsie f"); }
300 static __INLINE void __disable_fault_irq() { __ASM volatile ("cpsid f"); }
302 static __INLINE void __NOP() { __ASM volatile ("nop"); }
303 static __INLINE void __WFI() { __ASM volatile ("wfi"); }
304 static __INLINE void __WFE() { __ASM volatile ("wfe"); }
305 static __INLINE void __SEV() { __ASM volatile ("sev"); }
306 static __INLINE void __ISB() { __ASM volatile ("isb"); }
307 static __INLINE void __DSB() { __ASM volatile ("dsb"); }
308 static __INLINE void __DMB() { __ASM volatile ("dmb"); }
309 static __INLINE void __CLREX() { __ASM volatile ("clrex"); }
313 * @brief Return the Process Stack Pointer
316 * @return uint32_t ProcessStackPointer
318 * Return the actual process stack pointer
320 static __INLINE uint32_t __get_PSP(void)
324 __ASM volatile ("MRS %0, psp" : "=r" (result) );
329 * @brief Set the Process Stack Pointer
331 * @param uint32_t Process Stack Pointer
334 * Assign the value ProcessStackPointer to the MSP
335 * (process stack pointer) Cortex processor register
337 static __INLINE void __set_PSP(uint32_t topOfProcStack)
339 __ASM volatile ("MSR psp, %0" : : "r" (topOfProcStack) );
343 * @brief Return the Main Stack Pointer
346 * @return uint32_t Main Stack Pointer
348 * Return the current value of the MSP (main stack pointer)
349 * Cortex processor register
351 static __INLINE uint32_t __get_MSP(void)
355 __ASM volatile ("MRS %0, msp" : "=r" (result) );
360 * @brief Set the Main Stack Pointer
362 * @param uint32_t Main Stack Pointer
365 * Assign the value mainStackPointer to the MSP
366 * (main stack pointer) Cortex processor register
368 static __INLINE void __set_MSP(uint32_t topOfMainStack)
370 __ASM volatile ("MSR msp, %0" : : "r" (topOfMainStack) );
374 * @brief Return the Base Priority value
377 * @return uint32_t BasePriority
379 * Return the content of the base priority register
381 static __INLINE uint32_t __get_BASEPRI(void)
385 __ASM volatile ("MRS %0, basepri_max" : "=r" (result) );
391 * @brief Set the Base Priority value
393 * @param uint32_t BasePriority
396 * Set the base priority register
398 static __INLINE void __set_BASEPRI(uint32_t value)
400 __ASM volatile ("MSR basepri, %0" : : "r" (value) );
404 * @brief Return the Priority Mask value
407 * @return uint32_t PriMask
409 * Return the state of the priority mask bit from the priority mask
412 static __INLINE uint32_t __get_PRIMASK(void)
416 __ASM volatile ("MRS %0, primask" : "=r" (result) );
422 * @brief Set the Priority Mask value
424 * @param uint32_t PriMask
427 * Set the priority mask bit in the priority mask register
429 static __INLINE void __set_PRIMASK(uint32_t priMask)
431 __ASM volatile ("MSR primask, %0" : : "r" (priMask) );
436 * @brief Return the Fault Mask value
439 * @return uint32_t FaultMask
441 * Return the content of the fault mask register
443 static __INLINE uint32_t __get_FAULTMASK(void)
447 __ASM volatile ("MRS %0, faultmask" : "=r" (result) );
453 * @brief Set the Fault Mask value
455 * @param uint32_t faultMask value
458 * Set the fault mask register
460 static __INLINE void __set_FAULTMASK(uint32_t faultMask)
462 __ASM volatile ("MSR faultmask, %0" : : "r" (faultMask) );
466 * @brief Return the Control Register value
469 * @return uint32_t Control value
471 * Return the content of the control register
473 static __INLINE uint32_t __get_CONTROL(void)
477 __ASM volatile ("MRS %0, control" : "=r" (result) );
482 * @brief Set the Control Register value
484 * @param uint32_t Control value
487 * Set the control register
489 static __INLINE void __set_CONTROL(uint32_t control)
491 __ASM volatile ("MSR control, %0" : : "r" (control) );
495 * @brief Reverse byte order in integer value
497 * @param uint32_t value to reverse
498 * @return uint32_t reversed value
500 * Reverse byte order in integer value
502 static __INLINE uint32_t __REV(uint32_t value)
506 __ASM volatile ("rev %0, %1" : "=r" (result) : "r" (value) );
511 * @brief Reverse byte order in unsigned short value
513 * @param uint16_t value to reverse
514 * @return uint32_t reversed value
516 * Reverse byte order in unsigned short value
518 static __INLINE uint32_t __REV16(uint16_t value)
522 __ASM volatile ("rev16 %0, %1" : "=r" (result) : "r" (value) );
527 * Reverse byte order in signed short value with sign extension to integer
529 * @param int16_t value to reverse
530 * @return int32_t reversed value
532 * @brief Reverse byte order in signed short value with sign extension to integer
534 static __INLINE int32_t __REVSH(int16_t value)
538 __ASM volatile ("revsh %0, %1" : "=r" (result) : "r" (value) );
543 * @brief Reverse bit order of value
545 * @param uint32_t value to reverse
546 * @return uint32_t reversed value
548 * Reverse bit order of value
550 static __INLINE uint32_t __RBIT(uint32_t value)
554 __ASM volatile ("rbit %0, %1" : "=r" (result) : "r" (value) );
559 * @brief LDR Exclusive
561 * @param uint8_t* address
562 * @return uint8_t value of (*address)
564 * Exclusive LDR command
566 static __INLINE uint8_t __LDREXB(uint8_t *addr)
570 __ASM volatile ("ldrexb %0, [%1]" : "=r" (result) : "r" (addr) );
575 * @brief LDR Exclusive
577 * @param uint16_t* address
578 * @return uint16_t value of (*address)
580 * Exclusive LDR command
582 static __INLINE uint16_t __LDREXH(uint16_t *addr)
586 __ASM volatile ("ldrexh %0, [%1]" : "=r" (result) : "r" (addr) );
591 * @brief LDR Exclusive
593 * @param uint32_t* address
594 * @return uint32_t value of (*address)
596 * Exclusive LDR command
598 static __INLINE uint32_t __LDREXW(uint32_t *addr)
602 __ASM volatile ("ldrex %0, [%1]" : "=r" (result) : "r" (addr) );
607 * @brief STR Exclusive
609 * @param uint8_t *address
610 * @param uint8_t value to store
611 * @return uint32_t successful / failed
613 * Exclusive STR command
615 static __INLINE uint32_t __STREXB(uint8_t value, uint8_t *addr)
619 __ASM volatile ("strexb %0, %2, [%1]" : "=r" (result) : "r" (addr), "r" (value) );
624 * @brief STR Exclusive
626 * @param uint16_t *address
627 * @param uint16_t value to store
628 * @return uint32_t successful / failed
630 * Exclusive STR command
632 static __INLINE uint32_t __STREXH(uint16_t value, uint16_t *addr)
636 __ASM volatile ("strexh %0, %2, [%1]" : "=r" (result) : "r" (addr), "r" (value) );
641 * @brief STR Exclusive
643 * @param uint32_t *address
644 * @param uint32_t value to store
645 * @return uint32_t successful / failed
647 * Exclusive STR command
649 static __INLINE uint32_t __STREXW(uint32_t value, uint32_t *addr)
653 __ASM volatile ("strex %0, %2, [%1]" : "=r" (result) : "r" (addr), "r" (value) );
658 /* ########################## NVIC functions #################################### */
662 * @brief Set the Priority Grouping in NVIC Interrupt Controller
664 * @param uint32_t priority_grouping is priority grouping field
667 * Set the priority grouping field using the required unlock sequence.
668 * The parameter priority_grouping is assigned to the field
669 * SCB->AIRCR [10:8] PRIGROUP field. Only values from 0..7 are used.
670 * In case of a conflict between priority grouping and available
671 * priority bits (__NVIC_PRIO_BITS) the smallest possible priority group is set.
673 static __INLINE void NVIC_SetPriorityGrouping(uint32_t PriorityGroup)
676 uint32_t PriorityGroupTmp = (PriorityGroup & 0x07); /* only values 0..7 are used */
678 reg_value = SCB->AIRCR; /* read old register configuration */
679 reg_value &= ~((0xFFFFU << 16) | (0x0F << 8)); /* clear bits to change */
680 reg_value = ((reg_value | NVIC_AIRCR_VECTKEY | (PriorityGroupTmp << 8))); /* Insert write key and priorty group */
681 SCB->AIRCR = reg_value;
685 * @brief Get the Priority Grouping from NVIC Interrupt Controller
688 * @return uint32_t priority grouping field
690 * Get the priority grouping from NVIC Interrupt Controller.
691 * priority grouping is SCB->AIRCR [10:8] PRIGROUP field.
693 static __INLINE uint32_t NVIC_GetPriorityGrouping(void)
695 return ((SCB->AIRCR >> 8) & 0x07); /* read priority grouping field */
699 * @brief Enable Interrupt in NVIC Interrupt Controller
701 * @param IRQn_Type IRQn specifies the interrupt number
704 * Enable a device specific interupt in the NVIC interrupt controller.
705 * The interrupt number cannot be a negative value.
707 static __INLINE void NVIC_EnableIRQ(IRQn_Type IRQn)
709 NVIC->ISER[((uint32_t)(IRQn) >> 5)] = (1 << ((uint32_t)(IRQn) & 0x1F)); /* enable interrupt */
713 * @brief Disable the interrupt line for external interrupt specified
715 * @param IRQn_Type IRQn is the positive number of the external interrupt
718 * Disable a device specific interupt in the NVIC interrupt controller.
719 * The interrupt number cannot be a negative value.
721 static __INLINE void NVIC_DisableIRQ(IRQn_Type IRQn)
723 NVIC->ICER[((uint32_t)(IRQn) >> 5)] = (1 << ((uint32_t)(IRQn) & 0x1F)); /* disable interrupt */
727 * @brief Read the interrupt pending bit for a device specific interrupt source
729 * @param IRQn_Type IRQn is the number of the device specifc interrupt
730 * @return uint32_t 1 if pending interrupt else 0
732 * Read the pending register in NVIC and return 1 if its status is pending,
733 * otherwise it returns 0
735 static __INLINE uint32_t NVIC_GetPendingIRQ(IRQn_Type IRQn)
737 return((uint32_t) ((NVIC->ISPR[(uint32_t)(IRQn) >> 5] & (1 << ((uint32_t)(IRQn) & 0x1F)))?1:0)); /* Return 1 if pending else 0 */
741 * @brief Set the pending bit for an external interrupt
743 * @param IRQn_Type IRQn is the Number of the interrupt
746 * Set the pending bit for the specified interrupt.
747 * The interrupt number cannot be a negative value.
749 static __INLINE void NVIC_SetPendingIRQ(IRQn_Type IRQn)
751 NVIC->ISPR[((uint32_t)(IRQn) >> 5)] = (1 << ((uint32_t)(IRQn) & 0x1F)); /* set interrupt pending */
755 * @brief Clear the pending bit for an external interrupt
757 * @param IRQn_Type IRQn is the Number of the interrupt
760 * Clear the pending bit for the specified interrupt.
761 * The interrupt number cannot be a negative value.
763 static __INLINE void NVIC_ClearPendingIRQ(IRQn_Type IRQn)
765 NVIC->ICPR[((uint32_t)(IRQn) >> 5)] = (1 << ((uint32_t)(IRQn) & 0x1F)); /* Clear pending interrupt */
769 * @brief Read the active bit for an external interrupt
771 * @param IRQn_Type IRQn is the Number of the interrupt
772 * @return uint32_t 1 if active else 0
774 * Read the active register in NVIC and returns 1 if its status is active,
775 * otherwise it returns 0.
777 static __INLINE uint32_t NVIC_GetActive(IRQn_Type IRQn)
779 return((uint32_t)((NVIC->IABR[(uint32_t)(IRQn) >> 5] & (1 << ((uint32_t)(IRQn) & 0x1F)))?1:0)); /* Return 1 if active else 0 */
783 * @brief Set the priority for an interrupt
785 * @param IRQn_Type IRQn is the Number of the interrupt
786 * @param priority is the priority for the interrupt
789 * Set the priority for the specified interrupt. The interrupt
790 * number can be positive to specify an external (device specific)
791 * interrupt, or negative to specify an internal (core) interrupt. \n
793 * Note: The priority cannot be set for every core interrupt.
795 static __INLINE void NVIC_SetPriority(IRQn_Type IRQn, uint32_t priority)
798 SCB->SHP[((uint32_t)(IRQn) & 0xF)-4] = ((priority << (8 - __NVIC_PRIO_BITS)) & 0xff); } /* set Priority for Cortex-M3 System Interrupts */
800 NVIC->IP[(uint32_t)(IRQn)] = ((priority << (8 - __NVIC_PRIO_BITS)) & 0xff); } /* set Priority for device specific Interrupts */
804 * @brief Read the priority for an interrupt
806 * @param IRQn_Type IRQn is the Number of the interrupt
807 * @return uint32_t priority is the priority for the interrupt
809 * Read the priority for the specified interrupt. The interrupt
810 * number can be positive to specify an external (device specific)
811 * interrupt, or negative to specify an internal (core) interrupt.
813 * The returned priority value is automatically aligned to the implemented
814 * priority bits of the microcontroller.
816 * Note: The priority cannot be set for every core interrupt.
818 static __INLINE uint32_t NVIC_GetPriority(IRQn_Type IRQn)
822 return((uint32_t)(SCB->SHP[((uint32_t)(IRQn) & 0xF)-4] >> (8 - __NVIC_PRIO_BITS))); } /* get priority for Cortex-M3 system interrupts */
824 return((uint32_t)(NVIC->IP[(uint32_t)(IRQn)] >> (8 - __NVIC_PRIO_BITS))); } /* get priority for device specific interrupts */
829 * @brief Encode the priority for an interrupt
831 * @param uint32_t PriorityGroup is the used priority group
832 * @param uint32_t PreemptPriority is the preemptive priority value (starting from 0)
833 * @param uint32_t SubPriority is the sub priority value (starting from 0)
834 * @return uint32_t the priority for the interrupt
836 * Encode the priority for an interrupt with the given priority group,
837 * preemptive priority value and sub priority value.
838 * In case of a conflict between priority grouping and available
839 * priority bits (__NVIC_PRIO_BITS) the samllest possible priority group is set.
841 * The returned priority value can be used for NVIC_SetPriority(...) function
843 static __INLINE uint32_t NVIC_EncodePriority (uint32_t PriorityGroup, uint32_t PreemptPriority, uint32_t SubPriority)
845 uint32_t PriorityGroupTmp = (PriorityGroup & 0x07); /* only values 0..7 are used */
846 uint32_t PreemptPriorityBits;
847 uint32_t SubPriorityBits;
849 PreemptPriorityBits = ((7 - PriorityGroupTmp) > __NVIC_PRIO_BITS) ? __NVIC_PRIO_BITS : 7 - PriorityGroupTmp;
850 SubPriorityBits = ((PriorityGroupTmp + __NVIC_PRIO_BITS) < 7) ? 0 : PriorityGroupTmp - 7 + __NVIC_PRIO_BITS;
853 ((PreemptPriority & ((1 << (PreemptPriorityBits)) - 1)) << SubPriorityBits) |
854 ((SubPriority & ((1 << (SubPriorityBits )) - 1)))
860 * @brief Decode the priority of an interrupt
862 * @param uint32_t Priority the priority for the interrupt
863 * @param uint32_t PrioGroup is the used priority group
864 * @param uint32_t* pPreemptPrio is the preemptive priority value (starting from 0)
865 * @param uint32_t* pSubPrio is the sub priority value (starting from 0)
868 * Decode an interrupt priority value with the given priority group to
869 * preemptive priority value and sub priority value.
870 * In case of a conflict between priority grouping and available
871 * priority bits (__NVIC_PRIO_BITS) the samllest possible priority group is set.
873 * The priority value can be retrieved with NVIC_GetPriority(...) function
875 static __INLINE void NVIC_DecodePriority (uint32_t Priority, uint32_t PriorityGroup, uint32_t* pPreemptPriority, uint32_t* pSubPriority)
877 uint32_t PriorityGroupTmp = (PriorityGroup & 0x07); /* only values 0..7 are used */
878 uint32_t PreemptPriorityBits;
879 uint32_t SubPriorityBits;
881 PreemptPriorityBits = ((7 - PriorityGroupTmp) > __NVIC_PRIO_BITS) ? __NVIC_PRIO_BITS : 7 - PriorityGroupTmp;
882 SubPriorityBits = ((PriorityGroupTmp + __NVIC_PRIO_BITS) < 7) ? 0 : PriorityGroupTmp - 7 + __NVIC_PRIO_BITS;
884 *pPreemptPriority = (Priority >> SubPriorityBits) & ((1 << (PreemptPriorityBits)) - 1);
885 *pSubPriority = (Priority ) & ((1 << (SubPriorityBits )) - 1);
890 /* ################################## SysTick function ############################################ */
892 #if (!defined (__Vendor_SysTickConfig)) || (__Vendor_SysTickConfig == 0)
894 /* SysTick constants */
895 #define SYSTICK_ENABLE 0 /* Config-Bit to start or stop the SysTick Timer */
896 #define SYSTICK_TICKINT 1 /* Config-Bit to enable or disable the SysTick interrupt */
897 #define SYSTICK_CLKSOURCE 2 /* Clocksource has the offset 2 in SysTick Control and Status Register */
898 #define SYSTICK_MAXCOUNT ((1<<24) -1) /* SysTick MaxCount */
901 * @brief Initialize and start the SysTick counter and its interrupt.
903 * @param uint32_t ticks is the number of ticks between two interrupts
906 * Initialise the system tick timer and its interrupt and start the
907 * system tick timer / counter in free running mode to generate
908 * periodical interrupts.
910 static __INLINE uint32_t SysTick_Config(uint32_t ticks)
912 if (ticks > SYSTICK_MAXCOUNT) return (1); /* Reload value impossible */
914 SysTick->LOAD = (ticks & SYSTICK_MAXCOUNT) - 1; /* set reload register */
915 NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1); /* set Priority for Cortex-M0 System Interrupts */
916 SysTick->VAL = (0x00); /* Load the SysTick Counter Value */
917 SysTick->CTRL = (1 << SYSTICK_CLKSOURCE) | (1<<SYSTICK_ENABLE) | (1<<SYSTICK_TICKINT); /* Enable SysTick IRQ and SysTick Timer */
918 return (0); /* Function successful */
927 /* ################################## Reset function ############################################ */
930 * @brief Initiate a system reset request.
935 * Initialize a system reset request to reset the MCU
937 static __INLINE void NVIC_SystemReset(void)
939 SCB->AIRCR = (NVIC_AIRCR_VECTKEY | (SCB->AIRCR & (0x700)) | (1<<NVIC_SYSRESETREQ)); /* Keep priority group unchanged */
940 __DSB(); /* Ensure completion of memory access */
941 while(1); /* wait until reset */
945 /* ################################## Debug Output function ############################################ */
949 * @brief Outputs a character via the ITM channel 0
951 * @param uint32_t character to output
952 * @return uint32_t input character
954 * The function outputs a character via the ITM channel 0.
955 * The function returns when no debugger is connected that has booked the output.
956 * It is blocking when a debugger is connected, but the previous character send is not transmitted.
958 static __INLINE uint32_t ITM_SendChar (uint32_t ch)
960 if (ch == '\n') ITM_SendChar('\r');
962 if ((CoreDebug->DEMCR & CoreDebug_DEMCR_TRCENA) &&
963 (ITM->TCR & ITM_TCR_ITMENA) &&
964 (ITM->TER & (1UL << 0)) )
966 while (ITM->PORT[0].u32 == 0);
967 ITM->PORT[0].u8 = (uint8_t) ch;
976 #endif /* __CM3_CORE_H__ */