]> rtime.felk.cvut.cz Git - lincan.git/blob - embedded/arch/arm/mach-lpc17xx/defines/core_cm3.h
Contributed support for LCP17xx devices and PiKRON's LMC1 board.
[lincan.git] / embedded / arch / arm / mach-lpc17xx / defines / core_cm3.h
1 /****************************************************************************//**
2  * @file :    core_cm3.h
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  *----------------------------------------------------------------------------
7  *
8  * Copyright (C) 2009 ARM Limited. All rights reserved.
9  *
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.
13  *
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.
19  *
20  ******************************************************************************/
21
22 #ifndef __CM3_CORE_H__
23 #define __CM3_CORE_H__
24
25 #ifdef __cplusplus
26  extern "C" {
27 #endif
28
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       */
32
33 #define __CORTEX_M                (0x03)                                                       /*!< Cortex core                    */
34
35 /**
36  *  Lint configuration \n
37  *  ----------------------- \n
38  *
39  *  The following Lint messages will be suppressed and not shown: \n
40  *  \n
41  *    --- Error 10: --- \n
42  *    register uint32_t __regBasePri         __asm("basepri"); \n
43  *    Error 10: Expecting ';' \n
44  *     \n
45  *    --- Error 530: --- \n
46  *    return(__regBasePri); \n
47  *    Warning 530: Symbol '__regBasePri' (line 264) not initialized \n
48  *     \n
49  *    --- Error 550: --- \n
50  *      __regBasePri = (basePri & 0x1ff); \n
51  *    } \n
52  *    Warning 550: Symbol '__regBasePri' (line 271) not accessed \n
53  *     \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
57  *     \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
61  *     \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
65  *     \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
69  * \n
70  * \n
71  *    Note:  To re-enable a Message, insert a space before 'lint' * \n
72  *
73  */
74
75 /*lint -save */
76 /*lint -e10  */
77 /*lint -e530 */
78 /*lint -e550 */
79 /*lint -e754 */
80 /*lint -e750 */
81 /*lint -e528 */
82 /*lint -e751 */
83
84
85 #include <stdint.h>                           /* Include standard types */
86
87 #ifndef __NVIC_PRIO_BITS
88   #define __NVIC_PRIO_BITS    4               /*!< standard definition for NVIC Priority Bits */
89 #endif
90
91
92
93
94 /**
95  * IO definitions
96  *
97  * define access restrictions to peripheral registers
98  */
99
100 #ifdef __cplusplus
101 #define     __I     volatile                  /*!< defines 'read only' permissions      */
102 #else
103 #define     __I     volatile const            /*!< defines 'read only' permissions      */
104 #endif
105 #define     __O     volatile                  /*!< defines 'write only' permissions     */
106 #define     __IO    volatile                  /*!< defines 'read / write' permissions   */
107
108
109
110 /*******************************************************************************
111  *                 Register Abstraction
112  ******************************************************************************/
113
114
115 /* System Reset */
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                    */
120
121 /* Core Debug */
122 #define CoreDebug_DEMCR_TRCENA (1 << 24)      /*!< DEMCR TRCENA enable          */
123 #define ITM_TCR_ITMENA              1         /*!< ITM enable                   */
124
125
126
127
128 /* memory mapping struct for Nested Vectored Interrupt Controller (NVIC) */
129 typedef struct
130 {
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      */
144 }  NVIC_Type;
145
146
147 /* memory mapping struct for System Control Block */
148 typedef struct
149 {
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                                     */
169 } SCB_Type;
170
171
172 /* memory mapping struct for SysTick */
173 typedef struct
174 {
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        */
179 } SysTick_Type;
180
181
182 /* memory mapping structur for ITM */
183 typedef struct
184 {
185   __O  union
186   {
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              */
206   __I  uint32_t PID5;
207   __I  uint32_t PID6;
208   __I  uint32_t PID7;
209   __I  uint32_t PID0;
210   __I  uint32_t PID1;
211   __I  uint32_t PID2;
212   __I  uint32_t PID3;
213   __I  uint32_t CID0;
214   __I  uint32_t CID1;
215   __I  uint32_t CID2;
216   __I  uint32_t CID3;
217 } ITM_Type;
218
219
220 /* memory mapped struct for Interrupt Type */
221 typedef struct
222 {
223        uint32_t RESERVED0;
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       */
227 #else
228        uint32_t RESERVED1;
229 #endif
230 } InterruptType_Type;
231
232
233 /* Memory Protection Unit */
234 #if defined (__MPU_PRESENT) && (__MPU_PRESENT == 1)
235 typedef struct
236 {
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  */
248 } MPU_Type;
249 #endif
250
251
252 /* Core Debug Register */
253 typedef struct
254 {
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    */
259 } CoreDebug_Type;
260
261
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    */
269
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      */
276
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               */
280 #endif
281
282
283
284 /*******************************************************************************
285  *                Hardware Abstraction Layer
286  ******************************************************************************/
287
288
289   #define __ASM            __asm                                      /*!< asm keyword for GNU Compiler          */
290   #define __INLINE         inline                                     /*!< inline keyword for GNU Compiler       */
291
292 /* ###################  Compiler specific Intrinsics  ########################### */
293
294 /* GNU gcc specific functions */
295
296 static __INLINE void __enable_irq()               { __ASM volatile ("cpsie i"); }
297 static __INLINE void __disable_irq()              { __ASM volatile ("cpsid i"); }
298
299 static __INLINE void __enable_fault_irq()         { __ASM volatile ("cpsie f"); }
300 static __INLINE void __disable_fault_irq()        { __ASM volatile ("cpsid f"); }
301
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"); }
310
311
312 /**
313  * @brief  Return the Process Stack Pointer
314  *
315  * @param  none
316  * @return uint32_t ProcessStackPointer
317  *
318  * Return the actual process stack pointer
319  */
320 static __INLINE uint32_t __get_PSP(void)
321 {
322   uint32_t result=0;
323
324   __ASM volatile ("MRS %0, psp"  : "=r" (result) );
325   return(result);
326 }
327
328 /**
329  * @brief  Set the Process Stack Pointer
330  *
331  * @param  uint32_t Process Stack Pointer
332  * @return none
333  *
334  * Assign the value ProcessStackPointer to the MSP
335  * (process stack pointer) Cortex processor register
336  */
337 static __INLINE void __set_PSP(uint32_t topOfProcStack)
338 {
339   __ASM volatile ("MSR psp, %0" : : "r" (topOfProcStack) );
340 }
341
342 /**
343  * @brief  Return the Main Stack Pointer
344  *
345  * @param  none
346  * @return uint32_t Main Stack Pointer
347  *
348  * Return the current value of the MSP (main stack pointer)
349  * Cortex processor register
350  */
351 static __INLINE uint32_t __get_MSP(void)
352 {
353   uint32_t result=0;
354
355   __ASM volatile ("MRS %0, msp"  : "=r" (result) );
356   return(result);
357 }
358
359 /**
360  * @brief  Set the Main Stack Pointer
361  *
362  * @param  uint32_t Main Stack Pointer
363  * @return none
364  *
365  * Assign the value mainStackPointer to the MSP
366  * (main stack pointer) Cortex processor register
367  */
368 static __INLINE void __set_MSP(uint32_t topOfMainStack)
369 {
370   __ASM volatile ("MSR msp, %0" : : "r" (topOfMainStack) );
371 }
372
373 /**
374  * @brief  Return the Base Priority value
375  *
376  * @param  none
377  * @return uint32_t BasePriority
378  *
379  * Return the content of the base priority register
380  */
381 static __INLINE uint32_t __get_BASEPRI(void)
382 {
383   uint32_t result=0;
384   
385   __ASM volatile ("MRS %0, basepri_max" : "=r" (result) );
386   return(result);
387 }
388
389
390 /**
391  * @brief  Set the Base Priority value
392  *
393  * @param  uint32_t BasePriority
394  * @return none
395  *
396  * Set the base priority register
397  */
398 static __INLINE void __set_BASEPRI(uint32_t value)
399 {
400   __ASM volatile ("MSR basepri, %0" : : "r" (value) );
401 }
402
403 /**
404  * @brief  Return the Priority Mask value
405  *
406  * @param  none
407  * @return uint32_t PriMask
408  *
409  * Return the state of the priority mask bit from the priority mask
410  * register
411  */
412 static __INLINE uint32_t __get_PRIMASK(void)
413 {
414   uint32_t result=0;
415
416   __ASM volatile ("MRS %0, primask" : "=r" (result) );
417   return(result);
418 }
419
420
421 /**
422  * @brief  Set the Priority Mask value
423  *
424  * @param  uint32_t PriMask
425  * @return none
426  *
427  * Set the priority mask bit in the priority mask register
428  */
429 static __INLINE void __set_PRIMASK(uint32_t priMask)
430 {
431   __ASM volatile ("MSR primask, %0" : : "r" (priMask) );
432 }
433
434
435 /**
436  * @brief  Return the Fault Mask value
437  *
438  * @param  none
439  * @return uint32_t FaultMask
440  *
441  * Return the content of the fault mask register
442  */
443 static __INLINE uint32_t __get_FAULTMASK(void)
444 {
445   uint32_t result=0;
446   
447   __ASM volatile ("MRS %0, faultmask" : "=r" (result) );
448   return(result);
449 }
450
451
452 /**
453  * @brief  Set the Fault Mask value
454  *
455  * @param  uint32_t faultMask value
456  * @return none
457  *
458  * Set the fault mask register
459  */
460 static __INLINE void __set_FAULTMASK(uint32_t faultMask)
461 {
462   __ASM volatile ("MSR faultmask, %0" : : "r" (faultMask) );
463 }
464
465 /**
466  * @brief  Return the Control Register value
467 *
468 *  @param  none
469 *  @return uint32_t Control value
470  *
471  * Return the content of the control register
472  */
473 static __INLINE uint32_t __get_CONTROL(void)
474 {
475   uint32_t result=0;
476
477   __ASM volatile ("MRS %0, control" : "=r" (result) );
478   return(result);
479 }
480
481 /**
482  * @brief  Set the Control Register value
483  *
484  * @param  uint32_t Control value
485  * @return none
486  *
487  * Set the control register
488  */
489 static __INLINE void __set_CONTROL(uint32_t control)
490 {
491   __ASM volatile ("MSR control, %0" : : "r" (control) );
492 }
493
494 /**
495  * @brief  Reverse byte order in integer value
496  *
497  * @param  uint32_t value to reverse
498  * @return uint32_t reversed value
499  *
500  * Reverse byte order in integer value
501  */
502 static __INLINE uint32_t __REV(uint32_t value)
503 {
504   uint32_t result=0;
505   
506   __ASM volatile ("rev %0, %1" : "=r" (result) : "r" (value) );
507   return(result);
508 }
509
510 /**
511  * @brief  Reverse byte order in unsigned short value
512  *
513  * @param  uint16_t value to reverse
514  * @return uint32_t reversed value
515  *
516  * Reverse byte order in unsigned short value
517  */
518 static __INLINE uint32_t __REV16(uint16_t value)
519 {
520   uint32_t result=0;
521   
522   __ASM volatile ("rev16 %0, %1" : "=r" (result) : "r" (value) );
523   return(result);
524 }
525
526 /**
527  * Reverse byte order in signed short value with sign extension to integer
528  *
529  * @param  int16_t value to reverse
530  * @return int32_t reversed value
531  *
532  * @brief  Reverse byte order in signed short value with sign extension to integer
533  */
534 static __INLINE int32_t __REVSH(int16_t value)
535 {
536   uint32_t result=0;
537   
538   __ASM volatile ("revsh %0, %1" : "=r" (result) : "r" (value) );
539   return(result);
540 }
541
542 /**
543  * @brief  Reverse bit order of value
544  *
545  * @param  uint32_t value to reverse
546  * @return uint32_t reversed value
547  *
548  * Reverse bit order of value
549  */
550 static __INLINE uint32_t __RBIT(uint32_t value)
551 {
552   uint32_t result=0;
553   
554    __ASM volatile ("rbit %0, %1" : "=r" (result) : "r" (value) );
555    return(result);
556 }
557
558 /**
559  * @brief  LDR Exclusive
560  *
561  * @param  uint8_t* address
562  * @return uint8_t value of (*address)
563  *
564  * Exclusive LDR command
565  */
566 static __INLINE uint8_t __LDREXB(uint8_t *addr)
567 {
568     uint8_t result=0;
569   
570    __ASM volatile ("ldrexb %0, [%1]" : "=r" (result) : "r" (addr) );
571    return(result);
572 }
573
574 /**
575  * @brief  LDR Exclusive
576  *
577  * @param  uint16_t* address
578  * @return uint16_t value of (*address)
579  *
580  * Exclusive LDR command
581  */
582 static __INLINE uint16_t __LDREXH(uint16_t *addr)
583 {
584     uint16_t result=0;
585   
586    __ASM volatile ("ldrexh %0, [%1]" : "=r" (result) : "r" (addr) );
587    return(result);
588 }
589
590 /**
591  * @brief  LDR Exclusive
592  *
593  * @param  uint32_t* address
594  * @return uint32_t value of (*address)
595  *
596  * Exclusive LDR command
597  */
598 static __INLINE uint32_t __LDREXW(uint32_t *addr)
599 {
600     uint32_t result=0;
601   
602    __ASM volatile ("ldrex %0, [%1]" : "=r" (result) : "r" (addr) );
603    return(result);
604 }
605
606 /**
607  * @brief  STR Exclusive
608  *
609  * @param  uint8_t *address
610  * @param  uint8_t value to store
611  * @return uint32_t successful / failed
612  *
613  * Exclusive STR command
614  */
615 static __INLINE uint32_t __STREXB(uint8_t value, uint8_t *addr)
616 {
617    uint32_t result=0;
618   
619    __ASM volatile ("strexb %0, %2, [%1]" : "=r" (result) : "r" (addr), "r" (value) );
620    return(result);
621 }
622
623 /**
624  * @brief  STR Exclusive
625  *
626  * @param  uint16_t *address
627  * @param  uint16_t value to store
628  * @return uint32_t successful / failed
629  *
630  * Exclusive STR command
631  */
632 static __INLINE uint32_t __STREXH(uint16_t value, uint16_t *addr)
633 {
634    uint32_t result=0;
635   
636    __ASM volatile ("strexh %0, %2, [%1]" : "=r" (result) : "r" (addr), "r" (value) );
637    return(result);
638 }
639
640 /**
641  * @brief  STR Exclusive
642  *
643  * @param  uint32_t *address
644  * @param  uint32_t value to store
645  * @return uint32_t successful / failed
646  *
647  * Exclusive STR command
648  */
649 static __INLINE uint32_t __STREXW(uint32_t value, uint32_t *addr)
650 {
651    uint32_t result=0;
652   
653    __ASM volatile ("strex %0, %2, [%1]" : "=r" (result) : "r" (addr), "r" (value) );
654    return(result);
655 }
656
657
658 /* ##########################   NVIC functions  #################################### */
659
660
661 /**
662  * @brief  Set the Priority Grouping in NVIC Interrupt Controller
663  *
664  * @param  uint32_t priority_grouping is priority grouping field
665  * @return none
666  *
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.
672  */
673 static __INLINE void NVIC_SetPriorityGrouping(uint32_t PriorityGroup)
674 {
675   uint32_t reg_value;
676   uint32_t PriorityGroupTmp = (PriorityGroup & 0x07);                         /* only values 0..7 are used          */
677
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;
682 }
683
684 /**
685  * @brief  Get the Priority Grouping from NVIC Interrupt Controller
686  *
687  * @param  none
688  * @return uint32_t   priority grouping field
689  *
690  * Get the priority grouping from NVIC Interrupt Controller.
691  * priority grouping is SCB->AIRCR [10:8] PRIGROUP field.
692  */
693 static __INLINE uint32_t NVIC_GetPriorityGrouping(void)
694 {
695   return ((SCB->AIRCR >> 8) & 0x07);                                          /* read priority grouping field */
696 }
697
698 /**
699  * @brief  Enable Interrupt in NVIC Interrupt Controller
700  *
701  * @param  IRQn_Type IRQn specifies the interrupt number
702  * @return none
703  *
704  * Enable a device specific interupt in the NVIC interrupt controller.
705  * The interrupt number cannot be a negative value.
706  */
707 static __INLINE void NVIC_EnableIRQ(IRQn_Type IRQn)
708 {
709   NVIC->ISER[((uint32_t)(IRQn) >> 5)] = (1 << ((uint32_t)(IRQn) & 0x1F)); /* enable interrupt */
710 }
711
712 /**
713  * @brief  Disable the interrupt line for external interrupt specified
714  *
715  * @param  IRQn_Type IRQn is the positive number of the external interrupt
716  * @return none
717  *
718  * Disable a device specific interupt in the NVIC interrupt controller.
719  * The interrupt number cannot be a negative value.
720  */
721 static __INLINE void NVIC_DisableIRQ(IRQn_Type IRQn)
722 {
723   NVIC->ICER[((uint32_t)(IRQn) >> 5)] = (1 << ((uint32_t)(IRQn) & 0x1F)); /* disable interrupt */
724 }
725
726 /**
727  * @brief  Read the interrupt pending bit for a device specific interrupt source
728  *
729  * @param  IRQn_Type IRQn is the number of the device specifc interrupt
730  * @return uint32_t 1 if pending interrupt else 0
731  *
732  * Read the pending register in NVIC and return 1 if its status is pending,
733  * otherwise it returns 0
734  */
735 static __INLINE uint32_t NVIC_GetPendingIRQ(IRQn_Type IRQn)
736 {
737   return((uint32_t) ((NVIC->ISPR[(uint32_t)(IRQn) >> 5] & (1 << ((uint32_t)(IRQn) & 0x1F)))?1:0)); /* Return 1 if pending else 0 */
738 }
739
740 /**
741  * @brief  Set the pending bit for an external interrupt
742  *
743  * @param  IRQn_Type IRQn is the Number of the interrupt
744  * @return none
745  *
746  * Set the pending bit for the specified interrupt.
747  * The interrupt number cannot be a negative value.
748  */
749 static __INLINE void NVIC_SetPendingIRQ(IRQn_Type IRQn)
750 {
751   NVIC->ISPR[((uint32_t)(IRQn) >> 5)] = (1 << ((uint32_t)(IRQn) & 0x1F)); /* set interrupt pending */
752 }
753
754 /**
755  * @brief  Clear the pending bit for an external interrupt
756  *
757  * @param  IRQn_Type IRQn is the Number of the interrupt
758  * @return none
759  *
760  * Clear the pending bit for the specified interrupt.
761  * The interrupt number cannot be a negative value.
762  */
763 static __INLINE void NVIC_ClearPendingIRQ(IRQn_Type IRQn)
764 {
765   NVIC->ICPR[((uint32_t)(IRQn) >> 5)] = (1 << ((uint32_t)(IRQn) & 0x1F)); /* Clear pending interrupt */
766 }
767
768 /**
769  * @brief  Read the active bit for an external interrupt
770  *
771  * @param  IRQn_Type  IRQn is the Number of the interrupt
772  * @return uint32_t   1 if active else 0
773  *
774  * Read the active register in NVIC and returns 1 if its status is active,
775  * otherwise it returns 0.
776  */
777 static __INLINE uint32_t NVIC_GetActive(IRQn_Type IRQn)
778 {
779   return((uint32_t)((NVIC->IABR[(uint32_t)(IRQn) >> 5] & (1 << ((uint32_t)(IRQn) & 0x1F)))?1:0)); /* Return 1 if active else 0 */
780 }
781
782 /**
783  * @brief  Set the priority for an interrupt
784  *
785  * @param  IRQn_Type IRQn is the Number of the interrupt
786  * @param  priority is the priority for the interrupt
787  * @return none
788  *
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
792  *
793  * Note: The priority cannot be set for every core interrupt.
794  */
795 static __INLINE void NVIC_SetPriority(IRQn_Type IRQn, uint32_t priority)
796 {
797   if(IRQn < 0) {
798     SCB->SHP[((uint32_t)(IRQn) & 0xF)-4] = ((priority << (8 - __NVIC_PRIO_BITS)) & 0xff); } /* set Priority for Cortex-M3 System Interrupts */
799   else {
800     NVIC->IP[(uint32_t)(IRQn)] = ((priority << (8 - __NVIC_PRIO_BITS)) & 0xff);    }        /* set Priority for device specific Interrupts      */
801 }
802
803 /**
804  * @brief  Read the priority for an interrupt
805  *
806  * @param  IRQn_Type IRQn is the Number of the interrupt
807  * @return uint32_t  priority is the priority for the interrupt
808  *
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.
812  *
813  * The returned priority value is automatically aligned to the implemented
814  * priority bits of the microcontroller.
815  *
816  * Note: The priority cannot be set for every core interrupt.
817  */
818 static __INLINE uint32_t NVIC_GetPriority(IRQn_Type IRQn)
819 {
820
821   if(IRQn < 0) {
822     return((uint32_t)(SCB->SHP[((uint32_t)(IRQn) & 0xF)-4] >> (8 - __NVIC_PRIO_BITS)));  } /* get priority for Cortex-M3 system interrupts */
823   else {
824     return((uint32_t)(NVIC->IP[(uint32_t)(IRQn)]           >> (8 - __NVIC_PRIO_BITS)));  } /* get priority for device specific interrupts  */
825 }
826
827
828 /**
829  * @brief  Encode the priority for an interrupt
830  *
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
835  *
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.
840  *
841  * The returned priority value can be used for NVIC_SetPriority(...) function
842  */
843 static __INLINE uint32_t NVIC_EncodePriority (uint32_t PriorityGroup, uint32_t PreemptPriority, uint32_t SubPriority)
844 {
845   uint32_t PriorityGroupTmp = (PriorityGroup & 0x07);                         /* only values 0..7 are used          */
846   uint32_t PreemptPriorityBits;
847   uint32_t SubPriorityBits;
848
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;
851
852   return (
853            ((PreemptPriority & ((1 << (PreemptPriorityBits)) - 1)) << SubPriorityBits) |
854            ((SubPriority     & ((1 << (SubPriorityBits    )) - 1)))
855          );
856 }
857
858
859 /**
860  * @brief  Decode the priority of an interrupt
861  *
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)
866  * @return none
867  *
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.
872  *
873  * The priority value can be retrieved with NVIC_GetPriority(...) function
874  */
875 static __INLINE void NVIC_DecodePriority (uint32_t Priority, uint32_t PriorityGroup, uint32_t* pPreemptPriority, uint32_t* pSubPriority)
876 {
877   uint32_t PriorityGroupTmp = (PriorityGroup & 0x07);                         /* only values 0..7 are used          */
878   uint32_t PreemptPriorityBits;
879   uint32_t SubPriorityBits;
880
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;
883
884   *pPreemptPriority = (Priority >> SubPriorityBits) & ((1 << (PreemptPriorityBits)) - 1);
885   *pSubPriority     = (Priority                   ) & ((1 << (SubPriorityBits    )) - 1);
886 }
887
888
889
890 /* ##################################    SysTick function  ############################################ */
891
892 #if (!defined (__Vendor_SysTickConfig)) || (__Vendor_SysTickConfig == 0)
893
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                                                      */
899
900 /**
901  * @brief  Initialize and start the SysTick counter and its interrupt.
902  *
903  * @param  uint32_t ticks is the number of ticks between two interrupts
904  * @return  none
905  *
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.
909  */
910 static __INLINE uint32_t SysTick_Config(uint32_t ticks)
911 {
912   if (ticks > SYSTICK_MAXCOUNT)  return (1);                                             /* Reload value impossible */
913
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 */
919 }
920
921 #endif
922
923
924
925
926
927 /* ##################################    Reset function  ############################################ */
928
929 /**
930  * @brief  Initiate a system reset request.
931  *
932  * @param   none
933  * @return  none
934  *
935  * Initialize a system reset request to reset the MCU
936  */
937 static __INLINE void NVIC_SystemReset(void)
938 {
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 */
942 }
943
944
945 /* ##################################    Debug Output  function  ############################################ */
946
947
948 /**
949  * @brief  Outputs a character via the ITM channel 0
950  *
951  * @param   uint32_t character to output
952  * @return  uint32_t input character
953  *
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.
957  */
958 static __INLINE uint32_t ITM_SendChar (uint32_t ch)
959 {
960   if (ch == '\n') ITM_SendChar('\r');
961
962   if ((CoreDebug->DEMCR & CoreDebug_DEMCR_TRCENA)  &&
963       (ITM->TCR & ITM_TCR_ITMENA)                  &&
964       (ITM->TER & (1UL << 0))  )
965   {
966     while (ITM->PORT[0].u32 == 0);
967     ITM->PORT[0].u8 = (uint8_t) ch;
968   }
969   return (ch);
970 }
971
972 #ifdef __cplusplus
973 }
974 #endif
975
976 #endif /* __CM3_CORE_H__ */
977
978 /*lint -restore */