]> begriffs open source - cmsis/blob - CMSIS/Core_A/Include/core_ca.h
CMSIS-Core(M): Initial contribution for generic MPU functions.
[cmsis] / CMSIS / Core_A / Include / core_ca.h
1 /**************************************************************************//**
2  * @file     core_ca.h
3  * @brief    CMSIS Cortex-A Core Peripheral Access Layer Header File
4  * @version  V1.00
5  * @date     22. Feb 2017
6  ******************************************************************************/
7 /*
8  * Copyright (c) 2009-2017 ARM Limited. All rights reserved.
9  *
10  * SPDX-License-Identifier: Apache-2.0
11  *
12  * Licensed under the Apache License, Version 2.0 (the License); you may
13  * not use this file except in compliance with the License.
14  * You may obtain a copy of the License at
15  *
16  * www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
20  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  */
24
25 #if defined ( __ICCARM__ )
26  #pragma system_include  /* treat file as system include file for MISRA check */
27 #endif
28
29 #ifdef __cplusplus
30  extern "C" {
31 #endif
32
33 #ifndef __CORE_CA_H_GENERIC
34 #define __CORE_CA_H_GENERIC
35
36
37 /*******************************************************************************
38  *                 CMSIS definitions
39  ******************************************************************************/
40
41 /*  CMSIS CA definitions */
42 #define __CA_CMSIS_VERSION_MAIN  (1U)                                      /*!< \brief [31:16] CMSIS HAL main version   */
43 #define __CA_CMSIS_VERSION_SUB   (0U)                                      /*!< \brief [15:0]  CMSIS HAL sub version    */
44 #define __CA_CMSIS_VERSION       ((__CA_CMSIS_VERSION_MAIN << 16U) | \
45                                    __CA_CMSIS_VERSION_SUB          )       /*!< \brief CMSIS HAL version number         */
46
47 #if defined ( __CC_ARM )
48   #if defined __TARGET_FPU_VFP
49     #if (__FPU_PRESENT == 1)
50       #define __FPU_USED       1U
51     #else
52       #warning "Compiler generates FPU instructions for a device without an FPU (check __FPU_PRESENT)"
53       #define __FPU_USED       0U
54     #endif
55   #else
56     #define __FPU_USED         0U
57   #endif
58
59 #elif defined ( __ICCARM__ )
60   #if defined __ARMVFP__
61     #if (__FPU_PRESENT == 1)
62       #define __FPU_USED       1U
63     #else
64       #warning "Compiler generates FPU instructions for a device without an FPU (check __FPU_PRESENT)"
65       #define __FPU_USED       0U
66     #endif
67   #else
68     #define __FPU_USED         0U
69   #endif
70
71 #elif defined ( __TMS470__ )
72   #if defined __TI_VFP_SUPPORT__
73     #if (__FPU_PRESENT == 1)
74       #define __FPU_USED       1U
75     #else
76       #warning "Compiler generates FPU instructions for a device without an FPU (check __FPU_PRESENT)"
77       #define __FPU_USED       0U
78     #endif
79   #else
80     #define __FPU_USED         0U
81   #endif
82
83 #elif defined ( __GNUC__ )
84   #if defined (__VFP_FP__) && !defined(__SOFTFP__)
85     #if (__FPU_PRESENT == 1)
86       #define __FPU_USED       1U
87     #else
88       #warning "Compiler generates FPU instructions for a device without an FPU (check __FPU_PRESENT)"
89       #define __FPU_USED       0U
90     #endif
91   #else
92     #define __FPU_USED         0U
93   #endif
94
95 #elif defined ( __TASKING__ )
96   #if defined __FPU_VFP__
97     #if (__FPU_PRESENT == 1)
98       #define __FPU_USED       1U
99     #else
100       #error "Compiler generates FPU instructions for a device without an FPU (check __FPU_PRESENT)"
101       #define __FPU_USED       0U
102     #endif
103   #else
104     #define __FPU_USED         0U
105   #endif
106 #endif
107
108 #include "cmsis_compiler.h"               /* CMSIS compiler specific defines */
109
110 #ifdef __cplusplus
111 }
112 #endif
113
114 #endif /* __CORE_CA_H_GENERIC */
115
116 #ifndef __CMSIS_GENERIC
117
118 #ifndef __CORE_CA_H_DEPENDANT
119 #define __CORE_CA_H_DEPENDANT
120
121 #ifdef __cplusplus
122  extern "C" {
123 #endif
124
125  /* check device defines and use defaults */
126 #if defined __CHECK_DEVICE_DEFINES
127   #ifndef __CA_REV
128     #define __CA_REV              0x0000U
129     #warning "__CA_REV not defined in device header file; using default!"
130   #endif
131   
132   #ifndef __FPU_PRESENT
133     #define __FPU_PRESENT             0U
134     #warning "__FPU_PRESENT not defined in device header file; using default!"
135   #endif
136     
137   #ifndef __GIC_PRESENT
138     #define __GIC_PRESENT             1U
139     #warning "__GIC_PRESENT not defined in device header file; using default!"
140   #endif
141   
142   #ifndef __TIM_PRESENT
143     #define __TIM_PRESENT             1U
144     #warning "__TIM_PRESENT not defined in device header file; using default!"
145   #endif
146   
147   #ifndef __L2C_PRESENT
148     #define __L2C_PRESENT             0U
149     #warning "__L2C_PRESENT not defined in device header file; using default!"
150   #endif
151 #endif
152
153 /* IO definitions (access restrictions to peripheral registers) */
154 #ifdef __cplusplus
155   #define   __I     volatile             /*!< \brief Defines 'read only' permissions */
156 #else
157   #define   __I     volatile const       /*!< \brief Defines 'read only' permissions */
158 #endif
159 #define     __O     volatile             /*!< \brief Defines 'write only' permissions */
160 #define     __IO    volatile             /*!< \brief Defines 'read / write' permissions */
161
162 /* following defines should be used for structure members */
163 #define     __IM     volatile const      /*!< \brief Defines 'read only' structure member permissions */
164 #define     __OM     volatile            /*!< \brief Defines 'write only' structure member permissions */
165 #define     __IOM    volatile            /*!< \brief Defines 'read / write' structure member permissions */
166
167
168  /*******************************************************************************
169   *                 Register Abstraction
170    Core Register contain:
171    - CPSR
172    - CP15 Registers
173    - L2C-310 Cache Controller
174    - Generic Interrupt Controller Distributor
175    - Generic Interrupt Controller Interface
176   ******************************************************************************/
177
178 /* Core Register CPSR */
179 typedef union
180 {
181   struct
182   {
183     uint32_t M:5;                        /*!< \brief bit:  0.. 4  Mode field */
184     uint32_t T:1;                        /*!< \brief bit:      5  Thumb execution state bit */
185     uint32_t F:1;                        /*!< \brief bit:      6  FIQ mask bit */
186     uint32_t I:1;                        /*!< \brief bit:      7  IRQ mask bit */
187     uint32_t A:1;                        /*!< \brief bit:      8  Asynchronous abort mask bit */
188     uint32_t E:1;                        /*!< \brief bit:      9  Endianness execution state bit */
189     uint32_t IT1:6;                      /*!< \brief bit: 10..15  If-Then execution state bits 2-7 */
190     uint32_t GE:4;                       /*!< \brief bit: 16..19  Greater than or Equal flags */
191     uint32_t _reserved0:4;               /*!< \brief bit: 20..23  Reserved */
192     uint32_t J:1;                        /*!< \brief bit:     24  Jazelle bit */
193     uint32_t IT0:2;                      /*!< \brief bit: 25..26  If-Then execution state bits 0-1 */
194     uint32_t Q:1;                        /*!< \brief bit:     27  Saturation condition flag */
195     uint32_t V:1;                        /*!< \brief bit:     28  Overflow condition code flag */
196     uint32_t C:1;                        /*!< \brief bit:     29  Carry condition code flag */
197     uint32_t Z:1;                        /*!< \brief bit:     30  Zero condition code flag */
198     uint32_t N:1;                        /*!< \brief bit:     31  Negative condition code flag */
199   } b;                                   /*!< \brief Structure used for bit  access */
200   uint32_t w;                            /*!< \brief Type      used for word access */
201 } CPSR_Type;
202
203 /* CPSR Register Definitions */
204 #define CPSR_N_Pos                       31U                                    /*!< \brief CPSR: N Position */
205 #define CPSR_N_Msk                       (1UL << CPSR_N_Pos)                    /*!< \brief CPSR: N Mask */
206
207 #define CPSR_Z_Pos                       30U                                    /*!< \brief CPSR: Z Position */
208 #define CPSR_Z_Msk                       (1UL << CPSR_Z_Pos)                    /*!< \brief CPSR: Z Mask */
209
210 #define CPSR_C_Pos                       29U                                    /*!< \brief CPSR: C Position */
211 #define CPSR_C_Msk                       (1UL << CPSR_C_Pos)                    /*!< \brief CPSR: C Mask */
212
213 #define CPSR_V_Pos                       28U                                    /*!< \brief CPSR: V Position */
214 #define CPSR_V_Msk                       (1UL << CPSR_V_Pos)                    /*!< \brief CPSR: V Mask */
215
216 #define CPSR_Q_Pos                       27U                                    /*!< \brief CPSR: Q Position */
217 #define CPSR_Q_Msk                       (1UL << CPSR_Q_Pos)                    /*!< \brief CPSR: Q Mask */
218
219 #define CPSR_IT0_Pos                     25U                                    /*!< \brief CPSR: IT0 Position */
220 #define CPSR_IT0_Msk                     (3UL << CPSR_IT0_Pos)                  /*!< \brief CPSR: IT0 Mask */
221
222 #define CPSR_J_Pos                       24U                                    /*!< \brief CPSR: J Position */
223 #define CPSR_J_Msk                       (1UL << CPSR_J_Pos)                    /*!< \brief CPSR: J Mask */
224
225 #define CPSR_GE_Pos                      16U                                    /*!< \brief CPSR: GE Position */
226 #define CPSR_GE_Msk                      (0xFUL << CPSR_GE_Pos)                 /*!< \brief CPSR: GE Mask */
227
228 #define CPSR_IT1_Pos                     10U                                    /*!< \brief CPSR: IT1 Position */
229 #define CPSR_IT1_Msk                     (0x3FUL << CPSR_IT1_Pos)               /*!< \brief CPSR: IT1 Mask */
230
231 #define CPSR_E_Pos                       9U                                     /*!< \brief CPSR: E Position */
232 #define CPSR_E_Msk                       (1UL << CPSR_E_Pos)                    /*!< \brief CPSR: E Mask */
233
234 #define CPSR_A_Pos                       8U                                     /*!< \brief CPSR: A Position */
235 #define CPSR_A_Msk                       (1UL << CPSR_A_Pos)                    /*!< \brief CPSR: A Mask */
236
237 #define CPSR_I_Pos                       7U                                     /*!< \brief CPSR: I Position */
238 #define CPSR_I_Msk                       (1UL << CPSR_I_Pos)                    /*!< \brief CPSR: I Mask */
239
240 #define CPSR_F_Pos                       6U                                     /*!< \brief CPSR: F Position */
241 #define CPSR_F_Msk                       (1UL << CPSR_F_Pos)                    /*!< \brief CPSR: F Mask */
242
243 #define CPSR_T_Pos                       5U                                     /*!< \brief CPSR: T Position */
244 #define CPSR_T_Msk                       (1UL << CPSR_T_Pos)                    /*!< \brief CPSR: T Mask */
245
246 #define CPSR_M_Pos                       0U                                     /*!< \brief CPSR: M Position */
247 #define CPSR_M_Msk                       (0x1FUL << CPSR_M_Pos)                 /*!< \brief CPSR: M Mask */
248
249 /* CP15 Register SCTLR */
250 typedef union
251 {
252   struct
253   {
254     uint32_t M:1;                        /*!< \brief bit:     0  MMU enable */
255     uint32_t A:1;                        /*!< \brief bit:     1  Alignment check enable */
256     uint32_t C:1;                        /*!< \brief bit:     2  Cache enable */
257     uint32_t _reserved0:2;               /*!< \brief bit: 3.. 4  Reserved */
258     uint32_t CP15BEN:1;                  /*!< \brief bit:     5  CP15 barrier enable */
259     uint32_t _reserved1:1;               /*!< \brief bit:     6  Reserved */
260     uint32_t B:1;                        /*!< \brief bit:     7  Endianness model */
261     uint32_t _reserved2:2;               /*!< \brief bit: 8.. 9  Reserved */
262     uint32_t SW:1;                       /*!< \brief bit:    10  SWP and SWPB enable */
263     uint32_t Z:1;                        /*!< \brief bit:    11  Branch prediction enable */
264     uint32_t I:1;                        /*!< \brief bit:    12  Instruction cache enable */
265     uint32_t V:1;                        /*!< \brief bit:    13  Vectors bit */
266     uint32_t RR:1;                       /*!< \brief bit:    14  Round Robin select */
267     uint32_t _reserved3:2;               /*!< \brief bit:15..16  Reserved */
268     uint32_t HA:1;                       /*!< \brief bit:    17  Hardware Access flag enable */
269     uint32_t _reserved4:1;               /*!< \brief bit:    18  Reserved */
270     uint32_t WXN:1;                      /*!< \brief bit:    19  Write permission implies XN */
271     uint32_t UWXN:1;                     /*!< \brief bit:    20  Unprivileged write permission implies PL1 XN */
272     uint32_t FI:1;                       /*!< \brief bit:    21  Fast interrupts configuration enable */
273     uint32_t U:1;                        /*!< \brief bit:    22  Alignment model */
274     uint32_t _reserved5:1;               /*!< \brief bit:    23  Reserved */
275     uint32_t VE:1;                       /*!< \brief bit:    24  Interrupt Vectors Enable */
276     uint32_t EE:1;                       /*!< \brief bit:    25  Exception Endianness */
277     uint32_t _reserved6:1;               /*!< \brief bit:    26  Reserved */
278     uint32_t NMFI:1;                     /*!< \brief bit:    27  Non-maskable FIQ (NMFI) support */
279     uint32_t TRE:1;                      /*!< \brief bit:    28  TEX remap enable. */
280     uint32_t AFE:1;                      /*!< \brief bit:    29  Access flag enable */
281     uint32_t TE:1;                       /*!< \brief bit:    30  Thumb Exception enable */
282     uint32_t _reserved7:1;               /*!< \brief bit:    31  Reserved */
283   } b;                                   /*!< \brief Structure used for bit  access */
284   uint32_t w;                            /*!< \brief Type      used for word access */
285 } SCTLR_Type;
286
287 #define SCTLR_TE_Pos                     30U                                    /*!< \brief SCTLR: TE Position */
288 #define SCTLR_TE_Msk                     (1UL << SCTLR_TE_Pos)                  /*!< \brief SCTLR: TE Mask */
289
290 #define SCTLR_AFE_Pos                    29U                                    /*!< \brief SCTLR: AFE Position */
291 #define SCTLR_AFE_Msk                    (1UL << SCTLR_AFE_Pos)                 /*!< \brief SCTLR: AFE Mask */
292
293 #define SCTLR_TRE_Pos                    28U                                    /*!< \brief SCTLR: TRE Position */
294 #define SCTLR_TRE_Msk                    (1UL << SCTLR_TRE_Pos)                 /*!< \brief SCTLR: TRE Mask */
295
296 #define SCTLR_NMFI_Pos                   27U                                    /*!< \brief SCTLR: NMFI Position */
297 #define SCTLR_NMFI_Msk                   (1UL << SCTLR_NMFI_Pos)                /*!< \brief SCTLR: NMFI Mask */
298
299 #define SCTLR_EE_Pos                     25U                                    /*!< \brief SCTLR: EE Position */
300 #define SCTLR_EE_Msk                     (1UL << SCTLR_EE_Pos)                  /*!< \brief SCTLR: EE Mask */
301
302 #define SCTLR_VE_Pos                     24U                                    /*!< \brief SCTLR: VE Position */
303 #define SCTLR_VE_Msk                     (1UL << SCTLR_VE_Pos)                  /*!< \brief SCTLR: VE Mask */
304
305 #define SCTLR_U_Pos                      22U                                    /*!< \brief SCTLR: U Position */
306 #define SCTLR_U_Msk                      (1UL << SCTLR_U_Pos)                   /*!< \brief SCTLR: U Mask */
307
308 #define SCTLR_FI_Pos                     21U                                    /*!< \brief SCTLR: FI Position */
309 #define SCTLR_FI_Msk                     (1UL << SCTLR_FI_Pos)                  /*!< \brief SCTLR: FI Mask */
310
311 #define SCTLR_UWXN_Pos                   20U                                    /*!< \brief SCTLR: UWXN Position */
312 #define SCTLR_UWXN_Msk                   (1UL << SCTLR_UWXN_Pos)                /*!< \brief SCTLR: UWXN Mask */
313
314 #define SCTLR_WXN_Pos                    19U                                    /*!< \brief SCTLR: WXN Position */
315 #define SCTLR_WXN_Msk                    (1UL << SCTLR_WXN_Pos)                 /*!< \brief SCTLR: WXN Mask */
316
317 #define SCTLR_HA_Pos                     17U                                    /*!< \brief SCTLR: HA Position */
318 #define SCTLR_HA_Msk                     (1UL << SCTLR_HA_Pos)                  /*!< \brief SCTLR: HA Mask */
319
320 #define SCTLR_RR_Pos                     14U                                    /*!< \brief SCTLR: RR Position */
321 #define SCTLR_RR_Msk                     (1UL << SCTLR_RR_Pos)                  /*!< \brief SCTLR: RR Mask */
322
323 #define SCTLR_V_Pos                      13U                                    /*!< \brief SCTLR: V Position */
324 #define SCTLR_V_Msk                      (1UL << SCTLR_V_Pos)                   /*!< \brief SCTLR: V Mask */
325
326 #define SCTLR_I_Pos                      12U                                    /*!< \brief SCTLR: I Position */
327 #define SCTLR_I_Msk                      (1UL << SCTLR_I_Pos)                   /*!< \brief SCTLR: I Mask */
328
329 #define SCTLR_Z_Pos                      11U                                    /*!< \brief SCTLR: Z Position */
330 #define SCTLR_Z_Msk                      (1UL << SCTLR_Z_Pos)                   /*!< \brief SCTLR: Z Mask */
331
332 #define SCTLR_SW_Pos                     10U                                    /*!< \brief SCTLR: SW Position */
333 #define SCTLR_SW_Msk                     (1UL << SCTLR_SW_Pos)                  /*!< \brief SCTLR: SW Mask */
334
335 #define SCTLR_B_Pos                      7U                                     /*!< \brief SCTLR: B Position */
336 #define SCTLR_B_Msk                      (1UL << SCTLR_B_Pos)                   /*!< \brief SCTLR: B Mask */
337
338 #define SCTLR_CP15BEN_Pos                5U                                     /*!< \brief SCTLR: CP15BEN Position */
339 #define SCTLR_CP15BEN_Msk                (1UL << SCTLR_CP15BEN_Pos)             /*!< \brief SCTLR: CP15BEN Mask */
340
341 #define SCTLR_C_Pos                      2U                                     /*!< \brief SCTLR: C Position */
342 #define SCTLR_C_Msk                      (1UL << SCTLR_C_Pos)                   /*!< \brief SCTLR: C Mask */
343
344 #define SCTLR_A_Pos                      1U                                     /*!< \brief SCTLR: A Position */
345 #define SCTLR_A_Msk                      (1UL << SCTLR_A_Pos)                   /*!< \brief SCTLR: A Mask */
346
347 #define SCTLR_M_Pos                      0U                                     /*!< \brief SCTLR: M Position */
348 #define SCTLR_M_Msk                      (1UL << SCTLR_M_Pos)                   /*!< \brief SCTLR: M Mask */
349
350 /* CP15 Register ACTLR */
351 typedef union
352 {
353   struct
354   {
355     uint32_t _reserved0:6;               /*!< bit: 0.. 5  Reserved */
356     uint32_t SMP:1;                      /*!< bit:     6  Enables coherent requests to the processor */
357     uint32_t _reserved1:3;               /*!< bit: 7.. 9  Reserved */
358     uint32_t DODMBS:1;                   /*!< bit:    10  Disable optimized data memory barrier behavior */
359     uint32_t L2RADIS:1;                  /*!< bit:    11  L2 Data Cache read-allocate mode disable */
360     uint32_t L1RADIS:1;                  /*!< bit:    12  L1 Data Cache read-allocate mode disable */
361     uint32_t L1PCTL:2;                   /*!< bit:13..14  L1 Data prefetch control */
362     uint32_t DDVM:1;                     /*!< bit:    15  Disable Distributed Virtual Memory (DVM) transactions */
363     uint32_t _reserved3:12;              /*!< bit:16..27  Reserved */
364     uint32_t DDI:1;                      /*!< bit:    28  Disable dual issue */
365     uint32_t _reserved7:3;               /*!< bit:29..31  Reserved */
366   } b;                                   /*!< Structure used for bit  access */
367   uint32_t w;                            /*!< Type      used for word access */
368 } ACTLR_Type;
369
370 #define ACTLR_DDI_Pos                    28U                                     /*!< ACTLR: DDI Position */
371 #define ACTLR_DDI_Msk                    (1UL << ACTLR_DDI_Pos)                  /*!< ACTLR: DDI Mask */
372
373 #define ACTLR_DDVM_Pos                   15U                                     /*!< ACTLR: DDVM Position */
374 #define ACTLR_DDVM_Msk                   (1UL << ACTLR_DDVM_Pos)                 /*!< ACTLR: DDVM Mask */
375
376 #define ACTLR_L1PCTL_Pos                 13U                                     /*!< ACTLR: L1PCTL Position */
377 #define ACTLR_L1PCTL_Msk                 (3UL << ACTLR_L1PCTL_Pos)               /*!< ACTLR: L1PCTL Mask */
378
379 #define ACTLR_L1RADIS_Pos                12U                                     /*!< ACTLR: L1RADIS Position */
380 #define ACTLR_L1RADIS_Msk                (1UL << ACTLR_L1RADIS_Pos)              /*!< ACTLR: L1RADIS Mask */
381
382 #define ACTLR_L2RADIS_Pos                11U                                     /*!< ACTLR: L2RADIS Position */
383 #define ACTLR_L2RADIS_Msk                (1UL << ACTLR_L2RADIS_Pos)              /*!< ACTLR: L2RADIS Mask */
384
385 #define ACTLR_DODMBS_Pos                 10U                                     /*!< ACTLR: DODMBS Position */
386 #define ACTLR_DODMBS_Msk                 (1UL << ACTLR_DODMBS_Pos)               /*!< ACTLR: DODMBS Mask */
387
388 #define ACTLR_SMP_Pos                    6U                                      /*!< ACTLR: SMP Position */
389 #define ACTLR_SMP_Msk                    (1UL << ACTLR_SMP_Pos)                  /*!< ACTLR: SMP Mask */
390
391 /* CP15 Register CPACR */
392 typedef union
393 {
394   struct
395   {
396     uint32_t _reserved0:20;              /*!< \brief bit: 0..19  Reserved */
397     uint32_t cp10:2;                     /*!< \brief bit:20..21  Access rights for coprocessor 10 */
398     uint32_t cp11:2;                     /*!< \brief bit:22..23  Access rights for coprocessor 11 */
399     uint32_t _reserved1:6;               /*!< \brief bit:24..29  Reserved */
400     uint32_t D32DIS:1;                   /*!< \brief bit:    30  Disable use of registers D16-D31 of the VFP register file */
401     uint32_t ASEDIS:1;                   /*!< \brief bit:    31  Disable Advanced SIMD Functionality */
402   } b;                                   /*!< \brief Structure used for bit  access */
403   uint32_t w;                            /*!< \brief Type      used for word access */
404 } CPACR_Type;
405
406 #define CPACR_ASEDIS_Pos                 31U                                    /*!< \brief CPACR: ASEDIS Position */
407 #define CPACR_ASEDIS_Msk                 (1UL << CPACR_ASEDIS_Pos)              /*!< \brief CPACR: ASEDIS Mask */
408
409 #define CPACR_D32DIS_Pos                 30U                                    /*!< \brief CPACR: D32DIS Position */
410 #define CPACR_D32DIS_Msk                 (1UL << CPACR_D32DIS_Pos)              /*!< \brief CPACR: D32DIS Mask */
411
412 #define CPACR_cp11_Pos                   22U                                    /*!< \brief CPACR: cp11 Position */
413 #define CPACR_cp11_Msk                   (3UL << CPACR_cp11_Pos)                /*!< \brief CPACR: cp11 Mask */
414
415 #define CPACR_cp10_Pos                   20U                                    /*!< \brief CPACR: cp10 Position */
416 #define CPACR_cp10_Msk                   (3UL << CPACR_cp10_Pos)                /*!< \brief CPACR: cp10 Mask */
417
418 /* CP15 Register DFSR */
419 typedef union
420 {
421   struct
422   {
423     uint32_t FS0:4;                      /*!< \brief bit: 0.. 3  Fault Status bits bit 0-3 */
424     uint32_t Domain:4;                   /*!< \brief bit: 4.. 7  Fault on which domain */
425     uint32_t _reserved0:2;               /*!< \brief bit: 8.. 9  Reserved */
426     uint32_t FS1:1;                      /*!< \brief bit:    10  Fault Status bits bit 4 */
427     uint32_t WnR:1;                      /*!< \brief bit:    11  Write not Read bit */
428     uint32_t ExT:1;                      /*!< \brief bit:    12  External abort type */
429     uint32_t CM:1;                       /*!< \brief bit:    13  Cache maintenance fault */
430     uint32_t _reserved1:18;              /*!< \brief bit:14..31  Reserved */
431   } b;                                   /*!< \brief Structure used for bit  access */
432   uint32_t w;                            /*!< \brief Type      used for word access */
433 } DFSR_Type;
434
435 #define DFSR_CM_Pos                      13U                                    /*!< \brief DFSR: CM Position */
436 #define DFSR_CM_Msk                      (1UL << DFSR_CM_Pos)                   /*!< \brief DFSR: CM Mask */
437
438 #define DFSR_Ext_Pos                     12U                                    /*!< \brief DFSR: Ext Position */
439 #define DFSR_Ext_Msk                     (1UL << DFSR_Ext_Pos)                  /*!< \brief DFSR: Ext Mask */
440
441 #define DFSR_WnR_Pos                     11U                                    /*!< \brief DFSR: WnR Position */
442 #define DFSR_WnR_Msk                     (1UL << DFSR_WnR_Pos)                  /*!< \brief DFSR: WnR Mask */
443
444 #define DFSR_FS1_Pos                     10U                                    /*!< \brief DFSR: FS1 Position */
445 #define DFSR_FS1_Msk                     (1UL << DFSR_FS1_Pos)                  /*!< \brief DFSR: FS1 Mask */
446
447 #define DFSR_Domain_Pos                  4U                                     /*!< \brief DFSR: Domain Position */
448 #define DFSR_Domain_Msk                  (0xFUL << DFSR_Domain_Pos)             /*!< \brief DFSR: Domain Mask */
449
450 #define DFSR_FS0_Pos                     0U                                     /*!< \brief DFSR: FS0 Position */
451 #define DFSR_FS0_Msk                     (0xFUL << DFSR_FS0_Pos)                /*!< \brief DFSR: FS0 Mask */
452
453 /* CP15 Register IFSR */
454 typedef union
455 {
456   struct
457   {
458     uint32_t FS0:4;                      /*!< \brief bit: 0.. 3  Fault Status bits bit 0-3 */
459     uint32_t _reserved0:6;               /*!< \brief bit: 4.. 9  Reserved */
460     uint32_t FS1:1;                      /*!< \brief bit:    10  Fault Status bits bit 4 */
461     uint32_t _reserved1:1;               /*!< \brief bit:    11  Reserved */
462     uint32_t ExT:1;                      /*!< \brief bit:    12  External abort type */
463     uint32_t _reserved2:19;              /*!< \brief bit:13..31  Reserved */
464   } b;                                   /*!< \brief Structure used for bit  access */
465   uint32_t w;                            /*!< \brief Type      used for word access */
466 } IFSR_Type;
467
468 #define IFSR_ExT_Pos                     12U                                    /*!< \brief IFSR: ExT Position */
469 #define IFSR_ExT_Msk                     (1UL << IFSR_ExT_Pos)                  /*!< \brief IFSR: ExT Mask */
470
471 #define IFSR_FS1_Pos                     10U                                    /*!< \brief IFSR: FS1 Position */
472 #define IFSR_FS1_Msk                     (1UL << IFSR_FS1_Pos)                  /*!< \brief IFSR: FS1 Mask */
473
474 #define IFSR_FS0_Pos                     0U                                     /*!< \brief IFSR: FS0 Position */
475 #define IFSR_FS0_Msk                     (0xFUL << IFSR_FS0_Pos)                /*!< \brief IFSR: FS0 Mask */
476
477 /* CP15 Register ISR */
478 typedef union
479 {
480   struct
481   {
482     uint32_t _reserved0:6;               /*!< \brief bit: 0.. 5  Reserved */
483     uint32_t F:1;                        /*!< \brief bit:     6  FIQ pending bit */
484     uint32_t I:1;                        /*!< \brief bit:     7  IRQ pending bit */
485     uint32_t A:1;                        /*!< \brief bit:     8  External abort pending bit */
486     uint32_t _reserved1:23;              /*!< \brief bit:14..31  Reserved */
487   } b;                                   /*!< \brief Structure used for bit  access */
488   uint32_t w;                            /*!< \brief Type      used for word access */
489 } ISR_Type;
490
491 #define ISR_A_Pos                        13U                                    /*!< \brief ISR: A Position */
492 #define ISR_A_Msk                        (1UL << ISR_A_Pos)                     /*!< \brief ISR: A Mask */
493
494 #define ISR_I_Pos                        12U                                    /*!< \brief ISR: I Position */
495 #define ISR_I_Msk                        (1UL << ISR_I_Pos)                     /*!< \brief ISR: I Mask */
496
497 #define ISR_F_Pos                        11U                                    /*!< \brief ISR: F Position */
498 #define ISR_F_Msk                        (1UL << ISR_F_Pos)                     /*!< \brief ISR: F Mask */
499
500
501 /**
502   \brief     Mask and shift a bit field value for use in a register bit range.
503   \param[in] field  Name of the register bit field.
504   \param[in] value  Value of the bit field. This parameter is interpreted as an uint32_t type.
505   \return           Masked and shifted value.
506 */
507 #define _VAL2FLD(field, value)    (((uint32_t)(value) << field ## _Pos) & field ## _Msk)
508
509 /**
510   \brief     Mask and shift a register value to extract a bit filed value.
511   \param[in] field  Name of the register bit field.
512   \param[in] value  Value of register. This parameter is interpreted as an uint32_t type.
513   \return           Masked and shifted bit field value.
514 */
515 #define _FLD2VAL(field, value)    (((uint32_t)(value) & field ## _Msk) >> field ## _Pos)
516
517
518 /**
519  \brief  Union type to access the L2C_310 Cache Controller.
520 */
521 #if (__L2C_PRESENT == 1U) || defined(DOXYGEN)
522 typedef struct
523 {
524   __IM  uint32_t CACHE_ID;                   /*!< \brief Offset: 0x0000 (R/ ) Cache ID Register               */
525   __IM  uint32_t CACHE_TYPE;                 /*!< \brief Offset: 0x0004 (R/ ) Cache Type Register             */
526         uint32_t RESERVED0[0x3e];
527   __IOM uint32_t CONTROL;                    /*!< \brief Offset: 0x0100 (R/W) Control Register                */
528   __IOM uint32_t AUX_CNT;                    /*!< \brief Offset: 0x0104 (R/W) Auxiliary Control               */
529         uint32_t RESERVED1[0x3e];
530   __IOM uint32_t EVENT_CONTROL;              /*!< \brief Offset: 0x0200 (R/W) Event Counter Control           */
531   __IOM uint32_t EVENT_COUNTER1_CONF;        /*!< \brief Offset: 0x0204 (R/W) Event Counter 1 Configuration   */
532   __IOM uint32_t EVENT_COUNTER0_CONF;        /*!< \brief Offset: 0x0208 (R/W) Event Counter 1 Configuration   */
533         uint32_t RESERVED2[0x2];
534   __IOM uint32_t INTERRUPT_MASK;             /*!< \brief Offset: 0x0214 (R/W) Interrupt Mask                  */
535   __IM  uint32_t MASKED_INT_STATUS;          /*!< \brief Offset: 0x0218 (R/ ) Masked Interrupt Status         */
536   __IM  uint32_t RAW_INT_STATUS;             /*!< \brief Offset: 0x021c (R/ ) Raw Interrupt Status            */
537   __OM  uint32_t INTERRUPT_CLEAR;            /*!< \brief Offset: 0x0220 ( /W) Interrupt Clear                 */
538         uint32_t RESERVED3[0x143];
539   __IOM uint32_t CACHE_SYNC;                 /*!< \brief Offset: 0x0730 (R/W) Cache Sync                      */
540         uint32_t RESERVED4[0xf];
541   __IOM uint32_t INV_LINE_PA;                /*!< \brief Offset: 0x0770 (R/W) Invalidate Line By PA           */
542         uint32_t RESERVED6[2];
543   __IOM uint32_t INV_WAY;                    /*!< \brief Offset: 0x077c (R/W) Invalidate by Way               */
544         uint32_t RESERVED5[0xc];
545   __IOM uint32_t CLEAN_LINE_PA;              /*!< \brief Offset: 0x07b0 (R/W) Clean Line by PA                */
546         uint32_t RESERVED7[1];
547   __IOM uint32_t CLEAN_LINE_INDEX_WAY;       /*!< \brief Offset: 0x07b8 (R/W) Clean Line by Index/Way         */
548   __IOM uint32_t CLEAN_WAY;                  /*!< \brief Offset: 0x07bc (R/W) Clean by Way                    */
549         uint32_t RESERVED8[0xc];
550   __IOM uint32_t CLEAN_INV_LINE_PA;          /*!< \brief Offset: 0x07f0 (R/W) Clean and Invalidate Line by PA  */
551         uint32_t RESERVED9[1];
552   __IOM uint32_t CLEAN_INV_LINE_INDEX_WAY;   /*!< \brief Offset: 0x07f8 (R/W) Clean and Invalidate Line by Index/Way  */
553   __IOM uint32_t CLEAN_INV_WAY;              /*!< \brief Offset: 0x07fc (R/W) Clean and Invalidate by Way     */
554         uint32_t RESERVED10[0x40];
555   __IOM uint32_t DATA_LOCK_0_WAY;            /*!< \brief Offset: 0x0900 (R/W) Data Lockdown 0 by Way          */
556   __IOM uint32_t INST_LOCK_0_WAY;            /*!< \brief Offset: 0x0904 (R/W) Instruction Lockdown 0 by Way   */
557   __IOM uint32_t DATA_LOCK_1_WAY;            /*!< \brief Offset: 0x0908 (R/W) Data Lockdown 1 by Way          */
558   __IOM uint32_t INST_LOCK_1_WAY;            /*!< \brief Offset: 0x090c (R/W) Instruction Lockdown 1 by Way   */
559   __IOM uint32_t DATA_LOCK_2_WAY;            /*!< \brief Offset: 0x0910 (R/W) Data Lockdown 2 by Way          */
560   __IOM uint32_t INST_LOCK_2_WAY;            /*!< \brief Offset: 0x0914 (R/W) Instruction Lockdown 2 by Way   */
561   __IOM uint32_t DATA_LOCK_3_WAY;            /*!< \brief Offset: 0x0918 (R/W) Data Lockdown 3 by Way          */
562   __IOM uint32_t INST_LOCK_3_WAY;            /*!< \brief Offset: 0x091c (R/W) Instruction Lockdown 3 by Way   */
563   __IOM uint32_t DATA_LOCK_4_WAY;            /*!< \brief Offset: 0x0920 (R/W) Data Lockdown 4 by Way          */
564   __IOM uint32_t INST_LOCK_4_WAY;            /*!< \brief Offset: 0x0924 (R/W) Instruction Lockdown 4 by Way   */
565   __IOM uint32_t DATA_LOCK_5_WAY;            /*!< \brief Offset: 0x0928 (R/W) Data Lockdown 5 by Way          */
566   __IOM uint32_t INST_LOCK_5_WAY;            /*!< \brief Offset: 0x092c (R/W) Instruction Lockdown 5 by Way   */
567   __IOM uint32_t DATA_LOCK_6_WAY;            /*!< \brief Offset: 0x0930 (R/W) Data Lockdown 5 by Way          */
568   __IOM uint32_t INST_LOCK_6_WAY;            /*!< \brief Offset: 0x0934 (R/W) Instruction Lockdown 5 by Way   */
569   __IOM uint32_t DATA_LOCK_7_WAY;            /*!< \brief Offset: 0x0938 (R/W) Data Lockdown 6 by Way          */
570   __IOM uint32_t INST_LOCK_7_WAY;            /*!< \brief Offset: 0x093c (R/W) Instruction Lockdown 6 by Way   */
571         uint32_t RESERVED11[0x4];
572   __IOM uint32_t LOCK_LINE_EN;               /*!< \brief Offset: 0x0950 (R/W) Lockdown by Line Enable         */
573   __IOM uint32_t UNLOCK_ALL_BY_WAY;          /*!< \brief Offset: 0x0954 (R/W) Unlock All Lines by Way         */
574         uint32_t RESERVED12[0xaa];
575   __IOM uint32_t ADDRESS_FILTER_START;       /*!< \brief Offset: 0x0c00 (R/W) Address Filtering Start         */
576   __IOM uint32_t ADDRESS_FILTER_END;         /*!< \brief Offset: 0x0c04 (R/W) Address Filtering End           */
577         uint32_t RESERVED13[0xce];
578   __IOM uint32_t DEBUG_CONTROL;              /*!< \brief Offset: 0x0f40 (R/W) Debug Control Register          */
579 } L2C_310_TypeDef;
580
581 #define L2C_310           ((L2C_310_TypeDef *)L2C_310_BASE) /*!< \brief L2C_310 Declaration */
582 #endif
583
584 #if (__GIC_PRESENT == 1U) || defined(DOXYGEN)
585 /** \brief  Structure type to access the Generic Interrupt Controller Distributor (GICD)
586 */
587 typedef struct
588 {
589   __IOM uint32_t D_CTLR;                 /*!< \brief  Offset: 0x000 (R/W) Distributor Control Register */
590   __IM  uint32_t D_TYPER;                /*!< \brief  Offset: 0x004 (R/ ) Interrupt Controller Type Register */
591   __IM  uint32_t D_IIDR;                 /*!< \brief  Offset: 0x008 (R/ ) Distributor Implementer Identification Register */
592         uint32_t RESERVED0;
593   __IOM uint32_t D_STATUSR;              /*!< \brief  Offset: 0x010 (R/W) Error Reporting Status Register, optional */
594         uint32_t RESERVED1[11];
595   __OM  uint32_t D_SETSPI_NSR;           /*!< \brief  Offset: 0x040 ( /W) Set SPI Register */
596         uint32_t RESERVED2;
597   __OM  uint32_t D_CLRSPI_NSR;           /*!< \brief  Offset: 0x048 ( /W) Clear SPI Register */
598         uint32_t RESERVED3;
599   __OM  uint32_t D_SETSPI_SR;            /*!< \brief  Offset: 0x050 ( /W) Set SPI, Secure Register */
600         uint32_t RESERVED4;
601   __OM  uint32_t D_CLRSPI_SR;            /*!< \brief  Offset: 0x058 ( /W) Clear SPI, Secure Register */
602         uint32_t RESERVED5[9];
603   __IOM uint32_t D_IGROUPR[32];          /*!< \brief  Offset: 0x080 (R/W) Interrupt Group Registers */
604   __IOM uint32_t D_ISENABLER[32];        /*!< \brief  Offset: 0x100 (R/W) Interrupt Set-Enable Registers */
605   __IOM uint32_t D_ICENABLER[32];        /*!< \brief  Offset: 0x180 (R/W) Interrupt Clear-Enable Registers */
606   __IOM uint32_t D_ISPENDR[32];          /*!< \brief  Offset: 0x200 (R/W) Interrupt Set-Pending Registers */
607   __IOM uint32_t D_ICPENDR[32];          /*!< \brief  Offset: 0x280 (R/W) Interrupt Clear-Pending Registers */
608   __IOM uint32_t D_ISACTIVER[32];        /*!< \brief  Offset: 0x300 (R/W) Interrupt Set-Active Registers */
609   __IOM uint32_t D_ICACTIVER[32];        /*!< \brief  Offset: 0x380 (R/W) Interrupt Clear-Active Registers */
610   __IOM uint8_t  D_IPRIORITYR[1020];     /*!< \brief  Offset: 0x400 (R/W) Interrupt Priority Registers */
611         uint32_t RESERVED9[1];
612   __IOM uint8_t  D_ITARGETSR[1020];      /*!< \brief  Offset: 0x800 (R/W) Interrupt Targets Registers */
613         uint32_t RESERVED10[1];
614   __IOM uint32_t D_ICFGR[64];            /*!< \brief  Offset: 0xC00 (R/W) Interrupt Configuration Registers */
615   __IOM uint32_t D_IGRPMODR[32];         /*!< \brief  Offset: 0xD00 (R/W) Interrupt Group Modifier Registers */
616         uint32_t RESERVED11[32];
617   __IOM uint32_t D_NSACR[64];            /*!< \brief  Offset: 0xE00 (R/W) Non-secure Access Control Registers */
618   __OM  uint32_t D_SGIR;                 /*!< \brief  Offset: 0xF00 ( /W) Software Generated Interrupt Register */
619         uint32_t RESERVED13[3];
620   __IOM uint8_t  D_CPENDSGIR[16];        /*!< \brief  Offset: 0xF10 (R/W) SGI Clear-Pending Registers */
621   __IOM uint8_t  D_SPENDSGIR[16];        /*!< \brief  Offset: 0xF20 (R/W) SGI Set-Pending Registers */
622         uint32_t RESERVED14[5236];
623   __IOM uint64_t D_IROUTER[988];         /*!< \brief  Offset: 0x6100(R/W) Interrupt Routing Registers */
624 }  GICDistributor_Type;
625
626 #define GICDistributor      ((GICDistributor_Type      *)     GIC_DISTRIBUTOR_BASE ) /*!< GIC Distributor configuration struct */
627
628 /** \brief  Structure type to access the Generic Interrupt Controller Interface (GICC)
629 */
630 typedef struct
631 {
632   __IOM uint32_t C_CTLR;                 /*!< \brief  Offset: 0x000 (R/W) CPU Interface Control Register */
633   __IOM uint32_t C_PMR;                  /*!< \brief  Offset: 0x004 (R/W) Interrupt Priority Mask Register */
634   __IOM uint32_t C_BPR;                  /*!< \brief  Offset: 0x008 (R/W) Binary Point Register */
635   __IM  uint32_t C_IAR;                  /*!< \brief  Offset: 0x00C (R/ ) Interrupt Acknowledge Register */
636   __OM  uint32_t C_EOIR;                 /*!< \brief  Offset: 0x010 ( /W) End Of Interrupt Register */
637   __IM  uint32_t C_RPR;                  /*!< \brief  Offset: 0x014 (R/ ) Running Priority Register */
638   __IM  uint32_t C_HPPIR;                /*!< \brief  Offset: 0x018 (R/ ) Highest Priority Pending Interrupt Register */
639   __IOM uint32_t C_ABPR;                 /*!< \brief  Offset: 0x01C (R/W) Aliased Binary Point Register */
640   __IM  uint32_t C_AIAR;                 /*!< \brief  Offset: 0x020 (R/ ) Aliased Interrupt Acknowledge Register */
641   __OM  uint32_t C_AEOIR;                /*!< \brief  Offset: 0x024 ( /W) Aliased End Of Interrupt Register */
642   __IM  uint32_t C_AHPPIR;               /*!< \brief  Offset: 0x028 (R/ ) Aliased Highest Priority Pending Interrupt Register */
643   __IOM uint32_t C_STATUSR;              /*!< \brief  Offset: 0x02C (R/W) Error Reporting Status Register, optional */
644         uint32_t RESERVED15[40];
645   __IOM uint32_t C_APR[4];               /*!< \brief  Offset: 0x0D0 (R/W) Active Priority Register */
646   __IOM uint32_t C_NSAPR[4];             /*!< \brief  Offset: 0x0E0 (R/W) Non-secure Active Priority Register */
647         uint32_t RESERVED17[3];
648   __IM  uint32_t C_IIDR;                 /*!< \brief  Offset: 0x0FC (R/ ) CPU Interface Identification Register */
649         uint32_t RESERVED18[960];  
650   __OM  uint32_t C_DIR;                  /*!< \brief  Offset: 0x1000( /W) Deactivate Interrupt Register */
651 }  GICInterface_Type;
652
653 #define GICInterface        ((GICInterface_Type        *)     GIC_INTERFACE_BASE )   /*!< GIC Interface configuration struct */
654 #endif
655
656 #if (__TIM_PRESENT == 1U) || defined(DOXYGEN)
657 #if ((__CORTEX_A == 5U) || (__CORTEX_A == 9U)) || defined(DOXYGEN)
658 /** \brief Structure type to access the Private Timer
659 */
660 typedef struct
661 {
662   __IOM uint32_t LOAD;            //!< \brief  Offset: 0x000 (R/W) Private Timer Load Register
663   __IOM uint32_t COUNTER;         //!< \brief  Offset: 0x004 (R/W) Private Timer Counter Register
664   __IOM uint32_t CONTROL;         //!< \brief  Offset: 0x008 (R/W) Private Timer Control Register
665   __IOM uint32_t ISR;             //!< \brief  Offset: 0x00C (R/W) Private Timer Interrupt Status Register
666         uint32_t RESERVED[4];
667   __IOM uint32_t WLOAD;           //!< \brief  Offset: 0x020 (R/W) Watchdog Load Register
668   __IOM uint32_t WCOUNTER;        //!< \brief  Offset: 0x024 (R/W) Watchdog Counter Register
669   __IOM uint32_t WCONTROL;        //!< \brief  Offset: 0x028 (R/W) Watchdog Control Register
670   __IOM uint32_t WISR;            //!< \brief  Offset: 0x02C (R/W) Watchdog Interrupt Status Register
671   __IOM uint32_t WRESET;          //!< \brief  Offset: 0x030 (R/W) Watchdog Reset Status Register
672   __OM  uint32_t WDISABLE;        //!< \brief  Offset: 0x034 ( /W) Watchdog Disable Register
673 } Timer_Type;
674 #define PTIM ((Timer_Type *) TIMER_BASE )   /*!< \brief Timer configuration struct */
675 #endif
676 #endif
677
678  /*******************************************************************************
679   *                Hardware Abstraction Layer
680    Core Function Interface contains:
681    - L1 Cache Functions
682    - L2C-310 Cache Controller Functions 
683    - PL1 Timer Functions
684    - GIC Functions
685    - MMU Functions
686   ******************************************************************************/
687  
688 /* ##########################  L1 Cache functions  ################################# */
689
690 /** \brief Enable Caches by setting I and C bits in \ref CMSIS_SCTLR "SCTLR" register.
691 */
692 __STATIC_INLINE void L1C_EnableCaches(void) {
693   __set_SCTLR( __get_SCTLR() | (1u << SCTLR_I_Pos) | (1u << SCTLR_C_Pos));
694   __ISB();
695 }
696
697 /** \brief Disable Caches by clearing I and C bits in \ref CMSIS_SCTLR "SCTLR" register.
698 */
699 __STATIC_INLINE void L1C_DisableCaches(void) {
700   __set_SCTLR( __get_SCTLR() & ~(1u << SCTLR_I_Pos) & ~(1u << SCTLR_C_Pos));
701   __ISB();
702 }
703
704 /** \brief  Enable Branch Prediction by setting Z bit in \ref CMSIS_SCTLR "SCTLR" register.
705 */
706 __STATIC_INLINE void L1C_EnableBTAC(void) {
707   __set_SCTLR( __get_SCTLR() | (1u << SCTLR_Z_Pos));
708   __ISB();
709 }
710
711 /** \brief  Disable Branch Prediction by clearing Z bit in \ref CMSIS_SCTLR "SCTLR" register.
712 */
713 __STATIC_INLINE void L1C_DisableBTAC(void) {
714   __set_SCTLR( __get_SCTLR() & ~(1u << SCTLR_Z_Pos));
715   __ISB();
716 }
717
718 /** \brief  Invalidate entire branch predictor array
719 */
720 __STATIC_INLINE void L1C_InvalidateBTAC(void) {
721   __set_BPIALL(0);
722   __DSB();     //ensure completion of the invalidation
723   __ISB();     //ensure instruction fetch path sees new state
724 }
725
726 /** \brief  Invalidate the whole instruction cache
727 */
728 __STATIC_INLINE void L1C_InvalidateICacheAll(void) {
729   __set_ICIALLU(0);
730   __DSB();     //ensure completion of the invalidation
731   __ISB();     //ensure instruction fetch path sees new I cache state
732 }
733
734 /** \brief  Clean data cache line by address.
735 * \param va Pointer to data to clear the cache for.
736 */
737 __STATIC_INLINE void L1C_CleanDCacheMVA(void *va) {
738   __set_DCCMVAC((uint32_t)va);
739   __DMB();     //ensure the ordering of data cache maintenance operations and their effects
740 }
741
742 /** \brief  Invalidate data cache line by address.
743 * \param va Pointer to data to invalidate the cache for.
744 */
745 __STATIC_INLINE void L1C_InvalidateDCacheMVA(void *va) {
746   __set_DCIMVAC((uint32_t)va);
747   __DMB();     //ensure the ordering of data cache maintenance operations and their effects
748 }
749
750 /** \brief  Clean and Invalidate data cache by address.
751 * \param va Pointer to data to invalidate the cache for.
752 */
753 __STATIC_INLINE void L1C_CleanInvalidateDCacheMVA(void *va) {
754   __set_DCCIMVAC((uint32_t)va);
755   __DMB();     //ensure the ordering of data cache maintenance operations and their effects
756 }
757
758 /** \brief  Clean and Invalidate the entire data or unified cache
759 * \param op 0 - invalidate, 1 - clean, otherwise - invalidate and clean
760 * \see __L1C_CleanInvalidateCache
761 */
762 __STATIC_INLINE void L1C_CleanInvalidateCache(uint32_t op) {
763   __L1C_CleanInvalidateCache(op);
764 }
765
766 /** \brief  Invalidate the whole data cache.
767 */
768 __STATIC_INLINE void L1C_InvalidateDCacheAll(void) {
769   L1C_CleanInvalidateCache(0);
770 }
771
772 /** \brief  Clean the whole data cache.
773  */
774 __STATIC_INLINE void L1C_CleanDCacheAll(void) {
775   L1C_CleanInvalidateCache(1);
776 }
777
778 /** \brief  Clean and invalidate the whole data cache.
779  */
780 __STATIC_INLINE void L1C_CleanInvalidateDCacheAll(void) {
781   L1C_CleanInvalidateCache(2);
782 }
783
784
785 /* ##########################  L2 Cache functions  ################################# */
786 #if (__L2C_PRESENT == 1U) || defined(DOXYGEN)
787 /** \brief Cache Sync operation by writing \ref L2C_310_TypeDef::CACHE_SYNC "CACHE_SYNC" register.
788 */
789 __STATIC_INLINE void L2C_Sync(void)
790 {
791   L2C_310->CACHE_SYNC = 0x0;
792 }
793
794 /** \brief Read cache controller cache ID from \ref L2C_310_TypeDef::CACHE_ID "CACHE_ID" register.
795  * \return L2C_310_TypeDef::CACHE_ID
796  */
797 __STATIC_INLINE int L2C_GetID (void)
798 {
799   return L2C_310->CACHE_ID;
800 }
801
802 /** \brief Read cache controller cache type from \ref L2C_310_TypeDef::CACHE_TYPE "CACHE_TYPE" register.
803 *  \return L2C_310_TypeDef::CACHE_TYPE
804 */
805 __STATIC_INLINE int L2C_GetType (void)
806 {
807   return L2C_310->CACHE_TYPE;
808 }
809
810 /** \brief Invalidate all cache by way
811 */
812 __STATIC_INLINE void L2C_InvAllByWay (void)
813 {
814   unsigned int assoc;
815
816   if (L2C_310->AUX_CNT & (1u << 16u)) {
817     assoc = 16u;
818   } else {
819     assoc =  8u;
820   }
821   
822   L2C_310->INV_WAY = (1u << assoc) - 1u;
823   while(L2C_310->INV_WAY & ((1u << assoc) - 1u)); //poll invalidate
824
825   L2C_Sync();
826 }
827
828 /** \brief Clean and Invalidate all cache by way
829 */
830 __STATIC_INLINE void L2C_CleanInvAllByWay (void)
831 {
832   unsigned int assoc;
833
834   if (L2C_310->AUX_CNT & (1u << 16u)) {
835     assoc = 16u;
836   } else {
837     assoc =  8u;
838   }
839
840   L2C_310->CLEAN_INV_WAY = (1u << assoc) - 1u;
841   while(L2C_310->CLEAN_INV_WAY & ((1u << assoc) - 1u)); //poll invalidate
842
843   L2C_Sync();
844 }
845
846 /** \brief Enable Level 2 Cache
847 */
848 __STATIC_INLINE void L2C_Enable(void)
849 {
850   L2C_310->CONTROL = 0;
851   L2C_310->INTERRUPT_CLEAR = 0x000001FFuL;
852   L2C_310->DEBUG_CONTROL = 0;
853   L2C_310->DATA_LOCK_0_WAY = 0;
854   L2C_310->CACHE_SYNC = 0;
855   L2C_310->CONTROL = 0x01;
856   L2C_Sync();
857 }
858
859 /** \brief Disable Level 2 Cache
860 */
861 __STATIC_INLINE void L2C_Disable(void)
862 {
863   L2C_310->CONTROL = 0x00;
864   L2C_Sync();
865 }
866
867 /** \brief Invalidate cache by physical address
868 * \param pa Pointer to data to invalidate cache for.
869 */
870 __STATIC_INLINE void L2C_InvPa (void *pa)
871 {
872   L2C_310->INV_LINE_PA = (unsigned int)pa;
873   L2C_Sync();
874 }
875
876 /** \brief Clean cache by physical address
877 * \param pa Pointer to data to invalidate cache for.
878 */
879 __STATIC_INLINE void L2C_CleanPa (void *pa)
880 {
881   L2C_310->CLEAN_LINE_PA = (unsigned int)pa;
882   L2C_Sync();
883 }
884
885 /** \brief Clean and invalidate cache by physical address
886 * \param pa Pointer to data to invalidate cache for.
887 */
888 __STATIC_INLINE void L2C_CleanInvPa (void *pa)
889 {
890   L2C_310->CLEAN_INV_LINE_PA = (unsigned int)pa;
891   L2C_Sync();
892 }
893 #endif
894
895 /* ##########################  GIC functions  ###################################### */
896 #if (__GIC_PRESENT == 1U) || defined(DOXYGEN)
897   
898 /** Enable the interrupt distributor using the GIC's \ref GICDistributor_Type::D_CTLR "D_CTLR" register.
899 */
900 __STATIC_INLINE void GIC_EnableDistributor(void)
901 {
902   GICDistributor->D_CTLR |= 1;
903 }
904
905 /** Disable the interrupt distributor using the GIC's \ref GICDistributor_Type::D_CTLR "D_CTLR" register.
906 */
907 __STATIC_INLINE void GIC_DisableDistributor(void)
908 {
909   GICDistributor->D_CTLR &=~1;
910 }
911
912 /** Read the GIC's \ref GICDistributor_Type.D_TYPER "D_TYPER" register.
913 * \return GICDistributor_Type::D_TYPER
914 */
915 __STATIC_INLINE uint32_t GIC_DistributorInfo(void)
916 {
917   return (uint32_t)(GICDistributor->D_TYPER);
918 }
919
920 /** Reads the GIC's \ref GICDistributor_Type.D_IIDR "D_IIDR" register.
921 * \return GICDistributor_Type::D_IIDR
922 */
923 __STATIC_INLINE uint32_t GIC_DistributorImplementer(void)
924 {
925   return (uint32_t)(GICDistributor->D_IIDR);
926 }
927
928 /** Sets the GIC's \ref GICDistributor_Type.D_ITARGETSR "D_ITARGETSR" register for the given interrupt.
929 * \param IRQn Interrupt to be configured.
930 * \param cpu_target CPU interfaces to assign this interrupt to.
931 */
932 __STATIC_INLINE void GIC_SetTarget(IRQn_Type IRQn, uint32_t cpu_target)
933 {
934   GICDistributor->D_ITARGETSR[((uint32_t)(int32_t)IRQn)] = (uint8_t)(cpu_target & 0xff);
935 }
936
937 /** Read the GIC's \ref GICDistributor_Type.D_ITARGETSR "D_ITARGETSR" register.
938 * \param IRQn Interrupt to acquire the configuration for.
939 * \return GICDistributor_Type::D_ITARGETSR
940 */
941 __STATIC_INLINE uint32_t GIC_GetTarget(IRQn_Type IRQn)
942 {
943   return ((uint32_t) GICDistributor->D_ITARGETSR[((uint32_t)(int32_t)IRQn)] & 0xff);
944 }
945
946 /** Enable the CPUs interrupt interface.
947 */
948 __STATIC_INLINE void GIC_EnableInterface(void)
949 {
950   GICInterface->C_CTLR |= 1; //enable interface
951 }
952
953 /** Disable the CPUs interrupt interface.
954 */
955 __STATIC_INLINE void GIC_DisableInterface(void)
956 {
957   GICInterface->C_CTLR &=~1; //disable distributor
958 }
959
960 /** Read the CPU's \ref GICInterface_Type::C_IAR "C_IAR" register.
961 * \return GICInterface_Type::C_IAR
962 */
963 __STATIC_INLINE IRQn_Type GIC_AcknowledgePending(void)
964 {
965   return (IRQn_Type)(GICInterface->C_IAR);
966 }
967
968 /** Writes the given interrupt number to the CPU's \ref GICInterface_Type::C_EOIR "C_EOIR" register.
969 * \param IRQn The interrupt to be signaled as finished.
970 */
971 __STATIC_INLINE void GIC_EndInterrupt(IRQn_Type IRQn)
972 {
973   GICInterface->C_EOIR = IRQn;
974 }
975
976 /** Enables the given interrupt using GIC's \ref GICDistributor_Type::D_ISENABLER "D_ISENABLER" register.
977 * \param IRQn The interrupt to be enabled.
978 */
979 __STATIC_INLINE void GIC_EnableIRQ(IRQn_Type IRQn)
980 {
981   GICDistributor->D_ISENABLER[IRQn / 32] = 1 << (IRQn % 32);
982 }
983
984 /** Get interrupt enable status using GIC's \ref GICDistributor_Type::D_ISENABLER "D_ISENABLER" register.
985 * \param IRQn The interrupt to be queried.
986 * \return 0 - interrupt is not enabled, 1 - interrupt is enabled.
987 */
988 __STATIC_INLINE uint32_t GIC_GetEnableIRQ(IRQn_Type IRQn)
989 {
990   return (GICDistributor->D_ISENABLER[IRQn / 32] >> (IRQn % 32)) & 0x1UL;
991 }
992
993 /** Disables the given interrupt using GIC's \ref GICDistributor_Type::D_ICENABLER "D_ICENABLER" register.
994 * \param IRQn The interrupt to be disabled.
995 */
996 __STATIC_INLINE void GIC_DisableIRQ(IRQn_Type IRQn)
997 {
998   GICDistributor->D_ICENABLER[IRQn / 32] = 1 << (IRQn % 32);
999 }
1000
1001 /** Get interrupt pending status from GIC's \ref GICDistributor_Type::D_ISPENDR "D_ISPENDR" register.
1002 * \param IRQn The interrupt to be queried.
1003 * \return 0 - interrupt is not pending, 1 - interrupt is pendig.
1004 */
1005 __STATIC_INLINE uint32_t GIC_GetPendingIRQ(IRQn_Type IRQn)
1006 {
1007   uint32_t pend;
1008
1009   if (IRQn >= 16U) {
1010     pend = (GICDistributor->D_ISPENDR[IRQn / 32] >> (IRQn % 32)) & 0x1UL;
1011   } else {
1012     // INTID 0-15 Software Generated Interrupt
1013     pend = GICDistributor->D_SPENDSGIR[IRQn] & 0xff;
1014     // No CPU identification offered
1015     if (pend != 0U) {
1016       pend = 1U;
1017     } else {
1018       pend = 0U;
1019     }
1020   }
1021
1022   return (pend);
1023 }
1024
1025 /** Sets the given interrupt as pending using GIC's \ref GICDistributor_Type::D_ISPENDR "D_ISPENDR" register.
1026 * \param IRQn The interrupt to be enabled.
1027 */
1028 __STATIC_INLINE void GIC_SetPendingIRQ(IRQn_Type IRQn)
1029 {
1030   if (IRQn >= 16U) {
1031     GICDistributor->D_ISPENDR[IRQn / 32] = 1 << (IRQn % 32);
1032   } else {
1033     // INTID 0-15 Software Generated Interrupt
1034     GICDistributor->D_SPENDSGIR[IRQn] = 1U;
1035     // Forward the interrupt to the CPU interface that requested it
1036     GICDistributor->D_SGIR = (IRQn | 0x02000000U);
1037   }
1038 }
1039
1040 /** Clears the given interrupt from being pending using GIC's \ref GICDistributor_Type::D_ICPENDR "D_ICPENDR" register.
1041 * \param IRQn The interrupt to be enabled.
1042 */
1043 __STATIC_INLINE void GIC_ClearPendingIRQ(IRQn_Type IRQn)
1044 {
1045   if (IRQn >= 16U) {
1046     GICDistributor->D_ICPENDR[IRQn / 32] = 1 << (IRQn % 32);
1047   } else {
1048     // INTID 0-15 Software Generated Interrupt
1049     GICDistributor->D_CPENDSGIR[IRQn] = 1U;
1050   }
1051 }
1052
1053 /** Sets the interrupt configuration using GIC's GICDistributor_Type::D_ICFGR "D_ICFGR" register.
1054 * \param IRQn The interrupt to be configured.
1055 * \param int_config Int_config field value. Bit 0: Reserved (0 - N-N model, 1 - 1-N model for some GIC before v1)
1056 *                                           Bit 1: 0 - level sensitive, 1 - edge triggered
1057 */
1058 __STATIC_INLINE void GIC_SetConfiguration(IRQn_Type IRQn, uint32_t int_config)
1059 {
1060   uint32_t icfgr = GICDistributor->D_ICFGR[IRQn / 16];
1061   uint32_t shift = (IRQn % 16) << 1;
1062
1063   icfgr &= (    ~(0x03 << shift));
1064   icfgr |= (int_config << shift);
1065
1066   GICDistributor->D_ICFGR[IRQn / 16] = icfgr;
1067 }
1068
1069 /** Get the interrupt configuration from the GIC's GICDistributor_Type::D_ICFGR "D_ICFGR" register.
1070 * \param IRQn Interrupt to acquire the configuration for.
1071 * \return Int_config field value. Bit 0: Reserved (0 - N-N model, 1 - 1-N model for some GIC before v1)
1072 *                                 Bit 1: 0 - level sensitive, 1 - edge triggered
1073 */
1074 __STATIC_INLINE uint32_t GIC_GetConfiguration(IRQn_Type IRQn)
1075 {
1076   return (GICDistributor->D_ICFGR[IRQn / 16] >> ((IRQn % 16) >> 1));
1077 }
1078
1079 /** Set the priority for the given interrupt in the GIC's \ref GICDistributor_Type::D_IPRIORITYR "D_IPRIORITYR" register.
1080 * \param IRQn The interrupt to be configured.
1081 * \param priority The priority for the interrupt, lower values denote higher priorities.
1082 */
1083 __STATIC_INLINE void GIC_SetPriority(IRQn_Type IRQn, uint32_t priority)
1084 {
1085   GICDistributor->D_IPRIORITYR[((uint32_t)(int32_t)IRQn)] = (uint8_t)(priority);
1086 }
1087
1088 /** Read the current interrupt priority from GIC's \ref GICDistributor_Type::D_IPRIORITYR "D_IPRIORITYR" register.
1089 * \param IRQn The interrupt to be queried.
1090 */
1091 __STATIC_INLINE uint32_t GIC_GetPriority(IRQn_Type IRQn)
1092 {
1093   return((uint32_t)GICDistributor->D_IPRIORITYR[((uint32_t)(int32_t)IRQn)]);
1094 }
1095
1096 /** Set the interrupt priority mask using CPU's \ref GICInterface_Type::C_PMR "C_PMR" register.
1097 * \param priority Priority mask to be set.
1098 */
1099 __STATIC_INLINE void GIC_SetInterfacePriorityMask(uint32_t priority)
1100 {
1101   GICInterface->C_PMR = priority & 0xff; //set priority mask
1102 }
1103
1104 /** Read the current interrupt priority mask from CPU's \ref GICInterface_Type::C_PMR "C_PMR" register.
1105 * \result GICInterface_Type::C_PMR
1106 */
1107 __STATIC_INLINE uint32_t GIC_GetInterfacePriorityMask(void)
1108 {
1109   return (uint32_t)GICInterface->C_PMR;
1110 }
1111
1112 /** Configures the group priority and subpriority split point using CPU's \ref GICInterface_Type::C_BPR "C_BPR" register.
1113 * \param binary_point Amount of bits used as subpriority.
1114 */
1115 __STATIC_INLINE void GIC_SetBinaryPoint(uint32_t binary_point)
1116 {
1117   GICInterface->C_BPR = binary_point & 0x07; //set binary point
1118 }
1119
1120 /** Read the current group priority and subpriority split point from CPU's \ref GICInterface_Type::C_BPR "C_BPR" register.
1121 * \return GICInterface_Type::C_BPR
1122 */
1123 __STATIC_INLINE uint32_t GIC_GetBinaryPoint(void)
1124 {
1125   return (uint32_t)GICInterface->C_BPR;
1126 }
1127
1128 /** Get the status for a given interrupt.
1129 * \param IRQn The interrupt to get status for.
1130 * \return 0 - not pending/active, 1 - pending, 2 - active, 3 - pending and active
1131 */
1132 __STATIC_INLINE uint32_t GIC_GetIRQStatus(IRQn_Type IRQn)
1133 {
1134   uint32_t pending, active;
1135
1136   active = ((GICDistributor->D_ISACTIVER[IRQn / 32])  >> (IRQn % 32)) & 0x1;
1137   pending =((GICDistributor->D_ISPENDR[IRQn / 32]) >> (IRQn % 32)) & 0x1;
1138
1139   return ((active<<1) | pending);
1140 }
1141
1142 /** Generate a software interrupt using GIC's \ref GICDistributor_Type::D_SGIR "D_SGIR" register.
1143 * \param IRQn Software interrupt to be generated.
1144 * \param target_list List of CPUs the software interrupt should be forwarded to.
1145 * \param filter_list Filter to be applied to determine interrupt receivers.
1146 */
1147 __STATIC_INLINE void GIC_SendSGI(IRQn_Type IRQn, uint32_t target_list, uint32_t filter_list)
1148 {
1149   GICDistributor->D_SGIR = ((filter_list & 0x3) << 24) | ((target_list & 0xff) << 16) | (IRQn & 0xf);
1150 }
1151
1152 /** Get the interrupt number of the highest interrupt pending from CPU's \ref GICInterface_Type::C_HPPIR "C_HPPIR" register.
1153 * \return GICInterface_Type::C_HPPIR
1154 */
1155 __STATIC_INLINE uint32_t GIC_GetHighPendingIRQ(void) 
1156
1157   return GICInterface->C_HPPIR; 
1158 }
1159
1160 /** Provides information about the implementer and revision of the CPU interface.
1161 * \return GICInterface_Type::C_IIDR
1162 */
1163 __STATIC_INLINE uint32_t GIC_GetInterfaceId(void)
1164
1165   return GICInterface->C_IIDR; 
1166 }
1167
1168 /** Set the interrupt group from the GIC's GICDistributor_Type::D_IGROUPR "D_IGROUPR" register.
1169 * \param IRQn The interrupt to be queried.
1170 * \param group Interrupt group number: 0 - Group 0, 1 - Group 1
1171 */
1172 __STATIC_INLINE void GIC_SetGroup(IRQn_Type IRQn, uint32_t group)
1173 {
1174   uint32_t igroupr = GICDistributor->D_IGROUPR[IRQn / 32];
1175   uint32_t shift   = (IRQn % 32);
1176
1177   igroupr &= (~(0x01       << shift));
1178   igroupr |= ((group & 1)  << shift);
1179
1180   GICDistributor->D_IGROUPR[IRQn / 32] = igroupr;
1181 }
1182 #define GIC_SetSecurity         GIC_SetGroup
1183
1184 /** Get the interrupt group from the GIC's GICDistributor_Type::D_IGROUPR "D_IGROUPR" register.
1185 * \param IRQn The interrupt to be queried.
1186 * \return 0 - Group 0, 1 - Group 1
1187 */
1188 __STATIC_INLINE uint32_t GIC_GetGroup(IRQn_Type IRQn)
1189 {
1190   return (GICDistributor->D_IGROUPR[IRQn / 32] >> (IRQn % 32)) & 0x1;
1191 }
1192 #define GIC_GetSecurity         GIC_GetGroup
1193
1194 /** Initialize the interrupt distributor.
1195 */
1196 __STATIC_INLINE void GIC_DistInit(void)
1197 {
1198   IRQn_Type i;
1199   uint32_t num_irq = 0;
1200   uint32_t priority_field;
1201
1202   //A reset sets all bits in the D_IGROUPRs corresponding to the SPIs to 0,
1203   //configuring all of the interrupts as Secure.
1204
1205   //Disable interrupt forwarding
1206   GIC_DisableDistributor();
1207   //Get the maximum number of interrupts that the GIC supports
1208   num_irq = 32 * ((GIC_DistributorInfo() & 0x1f) + 1);
1209
1210   /* Priority level is implementation defined.
1211    To determine the number of priority bits implemented write 0xFF to an D_IPRIORITYR
1212    priority field and read back the value stored.*/
1213   GIC_SetPriority((IRQn_Type)0, 0xff);
1214   priority_field = GIC_GetPriority((IRQn_Type)0);
1215
1216   for (i = (IRQn_Type)32; i < num_irq; i++)
1217   {
1218       //Disable the SPI interrupt
1219       GIC_DisableIRQ(i);
1220       if (i > 15) {
1221         //Set level-sensitive (and N-N model)
1222         GIC_SetConfiguration(i, 0);
1223       }
1224       //Set priority
1225       GIC_SetPriority(i, priority_field/2);
1226       //Set target list to CPU0
1227       GIC_SetTarget(i, 1);
1228   }
1229   //Enable distributor
1230   GIC_EnableDistributor();
1231 }
1232
1233 /** Initialize the CPU's interrupt interface
1234 */
1235 __STATIC_INLINE void GIC_CPUInterfaceInit(void)
1236 {
1237   IRQn_Type i;
1238   uint32_t priority_field;
1239
1240   //A reset sets all bits in the D_IGROUPRs corresponding to the SPIs to 0,
1241   //configuring all of the interrupts as Secure.
1242
1243   //Disable interrupt forwarding
1244   GIC_DisableInterface();
1245
1246   /* Priority level is implementation defined.
1247    To determine the number of priority bits implemented write 0xFF to an D_IPRIORITYR
1248    priority field and read back the value stored.*/
1249   GIC_SetPriority((IRQn_Type)0, 0xff);
1250   priority_field = GIC_GetPriority((IRQn_Type)0);
1251
1252   //SGI and PPI
1253   for (i = (IRQn_Type)0; i < 32; i++)
1254   {
1255     if(i > 15) {
1256       //Set level-sensitive (and N-N model) for PPI
1257       GIC_SetConfiguration(i, 0U);
1258     }
1259     //Disable SGI and PPI interrupts
1260     GIC_DisableIRQ(i);
1261     //Set priority
1262     GIC_SetPriority(i, priority_field/2);
1263   }
1264   //Enable interface
1265   GIC_EnableInterface();
1266   //Set binary point to 0
1267   GIC_SetBinaryPoint(0);
1268   //Set priority mask
1269   GIC_SetInterfacePriorityMask(0xff);
1270 }
1271
1272 /** Initialize and enable the GIC
1273 */
1274 __STATIC_INLINE void GIC_Enable(void)
1275 {
1276   GIC_DistInit();
1277   GIC_CPUInterfaceInit(); //per CPU
1278 }
1279 #endif
1280
1281 /* ##########################  Generic Timer functions  ############################ */
1282 #if (__TIM_PRESENT == 1U) || defined(DOXYGEN)
1283   
1284 /* PL1 Physical Timer */
1285 #if (__CORTEX_A == 7U) || defined(DOXYGEN)
1286   
1287 /** \brief Physical Timer Control register */
1288 typedef union
1289 {
1290   struct
1291   {
1292     uint32_t ENABLE:1;      /*!< \brief bit: 0      Enables the timer. */
1293     uint32_t IMASK:1;       /*!< \brief bit: 1      Timer output signal mask bit. */
1294     uint32_t ISTATUS:1;     /*!< \brief bit: 2      The status of the timer. */
1295     uint32_t _reserved0:29; /*!< \brief bit: 3..31  Reserved */
1296   } b;                      /*!< \brief Structure used for bit  access */
1297   uint32_t w;               /*!< \brief Type      used for word access */
1298 } CNTP_CTL_Type;
1299
1300 /** Configures the frequency the timer shall run at.
1301 * \param value The timer frequency in Hz.
1302 */
1303 __STATIC_INLINE void PL1_SetCounterFrequency(uint32_t value) {
1304   __set_CNTFRQ(value);
1305   __ISB();
1306 }
1307
1308 /** Sets the reset value of the timer.
1309 * \param value The value the timer is loaded with.
1310 */
1311 __STATIC_INLINE void PL1_SetLoadValue(uint32_t value) {
1312   __set_CNTP_TVAL(value);
1313   __ISB();
1314 }
1315
1316 /** Get the current counter value.
1317 * \return Current counter value.
1318 */
1319 __STATIC_INLINE uint32_t PL1_GetCurrentValue() {
1320   return(__get_CNTP_TVAL());
1321 }
1322
1323 /** Configure the timer by setting the control value.
1324 * \param value New timer control value.
1325 */
1326 __STATIC_INLINE void PL1_SetControl(uint32_t value) {
1327   __set_CNTP_CTL(value);
1328   __ISB();
1329 }
1330
1331 /* Private Timer */
1332 #elif ((__CORTEX_A == 5U) || (__CORTEX_A == 9U)) || defined(DOXYGEN)
1333 /** Set the load value to timers \ref Timer_Type::LOAD "LOAD" register.
1334 * \param value The load value to be set.
1335 */
1336 __STATIC_INLINE void PTIM_SetLoadValue(uint32_t value) {
1337   PTIM->LOAD = value;
1338 }
1339
1340 /** Get the load value from timers \ref Timer_Type::LOAD "LOAD" register.
1341 * \return Timer_Type::LOAD
1342 */
1343 __STATIC_INLINE uint32_t PTIM_GetLoadValue() {
1344   return(PTIM->LOAD);
1345 }
1346
1347 /** Set current counter value from its \ref Timer_Type::COUNTER "COUNTER" register.
1348 */
1349 __STATIC_INLINE void PTIM_SetCurrentValue(uint32_t value) {
1350   PTIM->COUNTER = value;
1351 }
1352
1353 /** Get current counter value from timers \ref Timer_Type::COUNTER "COUNTER" register.
1354 * \result Timer_Type::COUNTER
1355 */
1356 __STATIC_INLINE uint32_t PTIM_GetCurrentValue() {
1357   return(PTIM->COUNTER);
1358 }
1359
1360 /** Configure the timer using its \ref Timer_Type::CONTROL "CONTROL" register.
1361 * \param value The new configuration value to be set.
1362 */
1363 __STATIC_INLINE void PTIM_SetControl(uint32_t value) {
1364   PTIM->CONTROL = value;
1365 }
1366
1367 /** Get the current timer configuration from its \ref Timer_Type::CONTROL "CONTROL" register.
1368 * \return Timer_Type::CONTROL
1369 */
1370 __STATIC_INLINE uint32_t PTIM_GetControl(void) {
1371   return(PTIM->CONTROL);
1372 }
1373
1374 /** Get the event flag in timers \ref Timer_Type::ISR "ISR" register.
1375 * \return 0 - flag is not set, 1- flag is set
1376 */
1377 __STATIC_INLINE uint32_t PTIM_GetEventFlag(void) {
1378   return (PTIM->ISR & 1UL);
1379 }
1380
1381 /** Clears the event flag in timers \ref Timer_Type::ISR "ISR" register.
1382 */
1383 __STATIC_INLINE void PTIM_ClearEventFlag(void) {
1384   PTIM->ISR = 1;
1385 }
1386 #endif
1387 #endif
1388
1389 /* ##########################  MMU functions  ###################################### */
1390
1391 #define SECTION_DESCRIPTOR      (0x2)
1392 #define SECTION_MASK            (0xFFFFFFFC)
1393
1394 #define SECTION_TEXCB_MASK      (0xFFFF8FF3)
1395 #define SECTION_B_SHIFT         (2)
1396 #define SECTION_C_SHIFT         (3)
1397 #define SECTION_TEX0_SHIFT      (12)
1398 #define SECTION_TEX1_SHIFT      (13)
1399 #define SECTION_TEX2_SHIFT      (14)
1400
1401 #define SECTION_XN_MASK         (0xFFFFFFEF)
1402 #define SECTION_XN_SHIFT        (4)
1403
1404 #define SECTION_DOMAIN_MASK     (0xFFFFFE1F)
1405 #define SECTION_DOMAIN_SHIFT    (5)
1406
1407 #define SECTION_P_MASK          (0xFFFFFDFF)
1408 #define SECTION_P_SHIFT         (9)
1409
1410 #define SECTION_AP_MASK         (0xFFFF73FF)
1411 #define SECTION_AP_SHIFT        (10)
1412 #define SECTION_AP2_SHIFT       (15)
1413
1414 #define SECTION_S_MASK          (0xFFFEFFFF)
1415 #define SECTION_S_SHIFT         (16)
1416
1417 #define SECTION_NG_MASK         (0xFFFDFFFF)
1418 #define SECTION_NG_SHIFT        (17)
1419
1420 #define SECTION_NS_MASK         (0xFFF7FFFF)
1421 #define SECTION_NS_SHIFT        (19)
1422
1423 #define PAGE_L1_DESCRIPTOR      (0x1)
1424 #define PAGE_L1_MASK            (0xFFFFFFFC)
1425
1426 #define PAGE_L2_4K_DESC         (0x2)
1427 #define PAGE_L2_4K_MASK         (0xFFFFFFFD)
1428
1429 #define PAGE_L2_64K_DESC        (0x1)
1430 #define PAGE_L2_64K_MASK        (0xFFFFFFFC)
1431
1432 #define PAGE_4K_TEXCB_MASK      (0xFFFFFE33)
1433 #define PAGE_4K_B_SHIFT         (2)
1434 #define PAGE_4K_C_SHIFT         (3)
1435 #define PAGE_4K_TEX0_SHIFT      (6)
1436 #define PAGE_4K_TEX1_SHIFT      (7)
1437 #define PAGE_4K_TEX2_SHIFT      (8)
1438
1439 #define PAGE_64K_TEXCB_MASK     (0xFFFF8FF3)
1440 #define PAGE_64K_B_SHIFT        (2)
1441 #define PAGE_64K_C_SHIFT        (3)
1442 #define PAGE_64K_TEX0_SHIFT     (12)
1443 #define PAGE_64K_TEX1_SHIFT     (13)
1444 #define PAGE_64K_TEX2_SHIFT     (14)
1445
1446 #define PAGE_TEXCB_MASK         (0xFFFF8FF3)
1447 #define PAGE_B_SHIFT            (2)
1448 #define PAGE_C_SHIFT            (3)
1449 #define PAGE_TEX_SHIFT          (12)
1450
1451 #define PAGE_XN_4K_MASK         (0xFFFFFFFE)
1452 #define PAGE_XN_4K_SHIFT        (0)
1453 #define PAGE_XN_64K_MASK        (0xFFFF7FFF)
1454 #define PAGE_XN_64K_SHIFT       (15)
1455
1456 #define PAGE_DOMAIN_MASK        (0xFFFFFE1F)
1457 #define PAGE_DOMAIN_SHIFT       (5)
1458
1459 #define PAGE_P_MASK             (0xFFFFFDFF)
1460 #define PAGE_P_SHIFT            (9)
1461
1462 #define PAGE_AP_MASK            (0xFFFFFDCF)
1463 #define PAGE_AP_SHIFT           (4)
1464 #define PAGE_AP2_SHIFT          (9)
1465
1466 #define PAGE_S_MASK             (0xFFFFFBFF)
1467 #define PAGE_S_SHIFT            (10)
1468
1469 #define PAGE_NG_MASK            (0xFFFFF7FF)
1470 #define PAGE_NG_SHIFT           (11)
1471
1472 #define PAGE_NS_MASK            (0xFFFFFFF7)
1473 #define PAGE_NS_SHIFT           (3)
1474
1475 #define OFFSET_1M               (0x00100000)
1476 #define OFFSET_64K              (0x00010000)
1477 #define OFFSET_4K               (0x00001000)
1478
1479 #define DESCRIPTOR_FAULT        (0x00000000)
1480
1481 /* Attributes enumerations */
1482
1483 /* Region size attributes */
1484 typedef enum
1485 {
1486    SECTION,
1487    PAGE_4k,
1488    PAGE_64k,
1489 } mmu_region_size_Type;
1490
1491 /* Region type attributes */
1492 typedef enum
1493 {
1494    NORMAL,
1495    DEVICE,
1496    SHARED_DEVICE,
1497    NON_SHARED_DEVICE,
1498    STRONGLY_ORDERED
1499 } mmu_memory_Type;
1500
1501 /* Region cacheability attributes */
1502 typedef enum
1503 {
1504    NON_CACHEABLE,
1505    WB_WA,
1506    WT,
1507    WB_NO_WA,
1508 } mmu_cacheability_Type;
1509
1510 /* Region parity check attributes */
1511 typedef enum
1512 {
1513    ECC_DISABLED,
1514    ECC_ENABLED,
1515 } mmu_ecc_check_Type;
1516
1517 /* Region execution attributes */
1518 typedef enum
1519 {
1520    EXECUTE,
1521    NON_EXECUTE,
1522 } mmu_execute_Type;
1523
1524 /* Region global attributes */
1525 typedef enum
1526 {
1527    GLOBAL,
1528    NON_GLOBAL,
1529 } mmu_global_Type;
1530
1531 /* Region shareability attributes */
1532 typedef enum
1533 {
1534    NON_SHARED,
1535    SHARED,
1536 } mmu_shared_Type;
1537
1538 /* Region security attributes */
1539 typedef enum
1540 {
1541    SECURE,
1542    NON_SECURE,
1543 } mmu_secure_Type;
1544
1545 /* Region access attributes */
1546 typedef enum
1547 {
1548    NO_ACCESS,
1549    RW,
1550    READ,
1551 } mmu_access_Type;
1552
1553 /* Memory Region definition */
1554 typedef struct RegionStruct {
1555     mmu_region_size_Type rg_t;
1556     mmu_memory_Type mem_t;
1557     uint8_t domain;
1558     mmu_cacheability_Type inner_norm_t;
1559     mmu_cacheability_Type outer_norm_t;
1560     mmu_ecc_check_Type e_t;
1561     mmu_execute_Type xn_t;
1562     mmu_global_Type g_t;
1563     mmu_secure_Type sec_t;
1564     mmu_access_Type priv_t;
1565     mmu_access_Type user_t;
1566     mmu_shared_Type sh_t;
1567
1568 } mmu_region_attributes_Type;
1569
1570 //Following macros define the descriptors and attributes
1571 //Sect_Normal. Outer & inner wb/wa, non-shareable, executable, rw, domain 0
1572 #define section_normal(descriptor_l1, region)     region.rg_t = SECTION; \
1573                                    region.domain = 0x0; \
1574                                    region.e_t = ECC_DISABLED; \
1575                                    region.g_t = GLOBAL; \
1576                                    region.inner_norm_t = WB_WA; \
1577                                    region.outer_norm_t = WB_WA; \
1578                                    region.mem_t = NORMAL; \
1579                                    region.sec_t = SECURE; \
1580                                    region.xn_t = EXECUTE; \
1581                                    region.priv_t = RW; \
1582                                    region.user_t = RW; \
1583                                    region.sh_t = NON_SHARED; \
1584                                    MMU_GetSectionDescriptor(&descriptor_l1, region);
1585
1586 //Sect_Normal_Cod. Outer & inner wb/wa, non-shareable, executable, ro, domain 0
1587 #define section_normal_cod(descriptor_l1, region) region.rg_t = SECTION; \
1588                                    region.domain = 0x0; \
1589                                    region.e_t = ECC_DISABLED; \
1590                                    region.g_t = GLOBAL; \
1591                                    region.inner_norm_t = WB_WA; \
1592                                    region.outer_norm_t = WB_WA; \
1593                                    region.mem_t = NORMAL; \
1594                                    region.sec_t = SECURE; \
1595                                    region.xn_t = EXECUTE; \
1596                                    region.priv_t = READ; \
1597                                    region.user_t = READ; \
1598                                    region.sh_t = NON_SHARED; \
1599                                    MMU_GetSectionDescriptor(&descriptor_l1, region);
1600
1601 //Sect_Normal_RO. Sect_Normal_Cod, but not executable
1602 #define section_normal_ro(descriptor_l1, region)  region.rg_t = SECTION; \
1603                                    region.domain = 0x0; \
1604                                    region.e_t = ECC_DISABLED; \
1605                                    region.g_t = GLOBAL; \
1606                                    region.inner_norm_t = WB_WA; \
1607                                    region.outer_norm_t = WB_WA; \
1608                                    region.mem_t = NORMAL; \
1609                                    region.sec_t = SECURE; \
1610                                    region.xn_t = NON_EXECUTE; \
1611                                    region.priv_t = READ; \
1612                                    region.user_t = READ; \
1613                                    region.sh_t = NON_SHARED; \
1614                                    MMU_GetSectionDescriptor(&descriptor_l1, region);
1615
1616 //Sect_Normal_RW. Sect_Normal_Cod, but writeable and not executable
1617 #define section_normal_rw(descriptor_l1, region) region.rg_t = SECTION; \
1618                                    region.domain = 0x0; \
1619                                    region.e_t = ECC_DISABLED; \
1620                                    region.g_t = GLOBAL; \
1621                                    region.inner_norm_t = WB_WA; \
1622                                    region.outer_norm_t = WB_WA; \
1623                                    region.mem_t = NORMAL; \
1624                                    region.sec_t = SECURE; \
1625                                    region.xn_t = NON_EXECUTE; \
1626                                    region.priv_t = RW; \
1627                                    region.user_t = RW; \
1628                                    region.sh_t = NON_SHARED; \
1629                                    MMU_GetSectionDescriptor(&descriptor_l1, region);
1630 //Sect_SO. Strongly-ordered (therefore shareable), not executable, rw, domain 0, base addr 0
1631 #define section_so(descriptor_l1, region) region.rg_t = SECTION; \
1632                                    region.domain = 0x0; \
1633                                    region.e_t = ECC_DISABLED; \
1634                                    region.g_t = GLOBAL; \
1635                                    region.inner_norm_t = NON_CACHEABLE; \
1636                                    region.outer_norm_t = NON_CACHEABLE; \
1637                                    region.mem_t = STRONGLY_ORDERED; \
1638                                    region.sec_t = SECURE; \
1639                                    region.xn_t = NON_EXECUTE; \
1640                                    region.priv_t = RW; \
1641                                    region.user_t = RW; \
1642                                    region.sh_t = NON_SHARED; \
1643                                    MMU_GetSectionDescriptor(&descriptor_l1, region);
1644
1645 //Sect_Device_RO. Device, non-shareable, non-executable, ro, domain 0, base addr 0
1646 #define section_device_ro(descriptor_l1, region) region.rg_t = SECTION; \
1647                                    region.domain = 0x0; \
1648                                    region.e_t = ECC_DISABLED; \
1649                                    region.g_t = GLOBAL; \
1650                                    region.inner_norm_t = NON_CACHEABLE; \
1651                                    region.outer_norm_t = NON_CACHEABLE; \
1652                                    region.mem_t = STRONGLY_ORDERED; \
1653                                    region.sec_t = SECURE; \
1654                                    region.xn_t = NON_EXECUTE; \
1655                                    region.priv_t = READ; \
1656                                    region.user_t = READ; \
1657                                    region.sh_t = NON_SHARED; \
1658                                    MMU_GetSectionDescriptor(&descriptor_l1, region);
1659
1660 //Sect_Device_RW. Sect_Device_RO, but writeable
1661 #define section_device_rw(descriptor_l1, region) region.rg_t = SECTION; \
1662                                    region.domain = 0x0; \
1663                                    region.e_t = ECC_DISABLED; \
1664                                    region.g_t = GLOBAL; \
1665                                    region.inner_norm_t = NON_CACHEABLE; \
1666                                    region.outer_norm_t = NON_CACHEABLE; \
1667                                    region.mem_t = STRONGLY_ORDERED; \
1668                                    region.sec_t = SECURE; \
1669                                    region.xn_t = NON_EXECUTE; \
1670                                    region.priv_t = RW; \
1671                                    region.user_t = RW; \
1672                                    region.sh_t = NON_SHARED; \
1673                                    MMU_GetSectionDescriptor(&descriptor_l1, region);
1674 //Page_4k_Device_RW.  Shared device, not executable, rw, domain 0
1675 #define page4k_device_rw(descriptor_l1, descriptor_l2, region) region.rg_t = PAGE_4k; \
1676                                    region.domain = 0x0; \
1677                                    region.e_t = ECC_DISABLED; \
1678                                    region.g_t = GLOBAL; \
1679                                    region.inner_norm_t = NON_CACHEABLE; \
1680                                    region.outer_norm_t = NON_CACHEABLE; \
1681                                    region.mem_t = SHARED_DEVICE; \
1682                                    region.sec_t = SECURE; \
1683                                    region.xn_t = NON_EXECUTE; \
1684                                    region.priv_t = RW; \
1685                                    region.user_t = RW; \
1686                                    region.sh_t = NON_SHARED; \
1687                                    MMU_GetPageDescriptor(&descriptor_l1, &descriptor_l2, region);
1688
1689 //Page_64k_Device_RW.  Shared device, not executable, rw, domain 0
1690 #define page64k_device_rw(descriptor_l1, descriptor_l2, region)  region.rg_t = PAGE_64k; \
1691                                    region.domain = 0x0; \
1692                                    region.e_t = ECC_DISABLED; \
1693                                    region.g_t = GLOBAL; \
1694                                    region.inner_norm_t = NON_CACHEABLE; \
1695                                    region.outer_norm_t = NON_CACHEABLE; \
1696                                    region.mem_t = SHARED_DEVICE; \
1697                                    region.sec_t = SECURE; \
1698                                    region.xn_t = NON_EXECUTE; \
1699                                    region.priv_t = RW; \
1700                                    region.user_t = RW; \
1701                                    region.sh_t = NON_SHARED; \
1702                                    MMU_GetPageDescriptor(&descriptor_l1, &descriptor_l2, region);
1703
1704 /** \brief  Set section execution-never attribute
1705
1706   \param [out]    descriptor_l1  L1 descriptor.
1707   \param [in]                xn  Section execution-never attribute : EXECUTE , NON_EXECUTE.
1708
1709   \return          0
1710 */
1711 __STATIC_INLINE int MMU_XNSection(uint32_t *descriptor_l1, mmu_execute_Type xn)
1712 {
1713   *descriptor_l1 &= SECTION_XN_MASK;
1714   *descriptor_l1 |= ((xn & 0x1) << SECTION_XN_SHIFT);
1715   return 0;
1716 }
1717
1718 /** \brief  Set section domain
1719
1720   \param [out]    descriptor_l1  L1 descriptor.
1721   \param [in]            domain  Section domain
1722
1723   \return          0
1724 */
1725 __STATIC_INLINE int MMU_DomainSection(uint32_t *descriptor_l1, uint8_t domain)
1726 {
1727   *descriptor_l1 &= SECTION_DOMAIN_MASK;
1728   *descriptor_l1 |= ((domain & 0xF) << SECTION_DOMAIN_SHIFT);
1729   return 0;
1730 }
1731
1732 /** \brief  Set section parity check
1733
1734   \param [out]    descriptor_l1  L1 descriptor.
1735   \param [in]              p_bit Parity check: ECC_DISABLED, ECC_ENABLED
1736
1737   \return          0
1738 */
1739 __STATIC_INLINE int MMU_PSection(uint32_t *descriptor_l1, mmu_ecc_check_Type p_bit)
1740 {
1741   *descriptor_l1 &= SECTION_P_MASK;
1742   *descriptor_l1 |= ((p_bit & 0x1) << SECTION_P_SHIFT);
1743   return 0;
1744 }
1745
1746 /** \brief  Set section access privileges
1747
1748   \param [out]    descriptor_l1  L1 descriptor.
1749   \param [in]              user  User Level Access: NO_ACCESS, RW, READ
1750   \param [in]              priv  Privilege Level Access: NO_ACCESS, RW, READ
1751   \param [in]               afe  Access flag enable
1752
1753   \return          0
1754 */
1755 __STATIC_INLINE int MMU_APSection(uint32_t *descriptor_l1, mmu_access_Type user, mmu_access_Type priv, uint32_t afe)
1756 {
1757   uint32_t ap = 0;
1758
1759   if (afe == 0) { //full access
1760     if ((priv == NO_ACCESS) && (user == NO_ACCESS)) { ap = 0x0; }
1761     else if ((priv == RW) && (user == NO_ACCESS))   { ap = 0x1; }
1762     else if ((priv == RW) && (user == READ))        { ap = 0x2; }
1763     else if ((priv == RW) && (user == RW))          { ap = 0x3; }
1764     else if ((priv == READ) && (user == NO_ACCESS)) { ap = 0x5; }
1765     else if ((priv == READ) && (user == READ))      { ap = 0x7; }
1766   }
1767
1768   else { //Simplified access
1769     if ((priv == RW) && (user == NO_ACCESS))        { ap = 0x1; }
1770     else if ((priv == RW) && (user == RW))          { ap = 0x3; }
1771     else if ((priv == READ) && (user == NO_ACCESS)) { ap = 0x5; }
1772     else if ((priv == READ) && (user == READ))      { ap = 0x7; }
1773   }
1774
1775   *descriptor_l1 &= SECTION_AP_MASK;
1776   *descriptor_l1 |= (ap & 0x3) << SECTION_AP_SHIFT;
1777   *descriptor_l1 |= ((ap & 0x4)>>2) << SECTION_AP2_SHIFT;
1778
1779   return 0;
1780 }
1781
1782 /** \brief  Set section shareability
1783
1784   \param [out]    descriptor_l1  L1 descriptor.
1785   \param [in]             s_bit  Section shareability: NON_SHARED, SHARED
1786
1787   \return          0
1788 */
1789 __STATIC_INLINE int MMU_SharedSection(uint32_t *descriptor_l1, mmu_shared_Type s_bit)
1790 {
1791   *descriptor_l1 &= SECTION_S_MASK;
1792   *descriptor_l1 |= ((s_bit & 0x1) << SECTION_S_SHIFT);
1793   return 0;
1794 }
1795
1796 /** \brief  Set section Global attribute
1797
1798   \param [out]    descriptor_l1  L1 descriptor.
1799   \param [in]             g_bit  Section attribute: GLOBAL, NON_GLOBAL
1800
1801   \return          0
1802 */
1803 __STATIC_INLINE int MMU_GlobalSection(uint32_t *descriptor_l1, mmu_global_Type g_bit)
1804 {
1805   *descriptor_l1 &= SECTION_NG_MASK;
1806   *descriptor_l1 |= ((g_bit & 0x1) << SECTION_NG_SHIFT);
1807   return 0;
1808 }
1809
1810 /** \brief  Set section Security attribute
1811
1812   \param [out]    descriptor_l1  L1 descriptor.
1813   \param [in]             s_bit  Section Security attribute: SECURE, NON_SECURE
1814
1815   \return          0
1816 */
1817 __STATIC_INLINE int MMU_SecureSection(uint32_t *descriptor_l1, mmu_secure_Type s_bit)
1818 {
1819   *descriptor_l1 &= SECTION_NS_MASK;
1820   *descriptor_l1 |= ((s_bit & 0x1) << SECTION_NS_SHIFT);
1821   return 0;
1822 }
1823
1824 /* Page 4k or 64k */
1825 /** \brief  Set 4k/64k page execution-never attribute
1826
1827   \param [out]    descriptor_l2  L2 descriptor.
1828   \param [in]                xn  Page execution-never attribute : EXECUTE , NON_EXECUTE.
1829   \param [in]              page  Page size: PAGE_4k, PAGE_64k,
1830
1831   \return          0
1832 */
1833 __STATIC_INLINE int MMU_XNPage(uint32_t *descriptor_l2, mmu_execute_Type xn, mmu_region_size_Type page)
1834 {
1835   if (page == PAGE_4k)
1836   {
1837       *descriptor_l2 &= PAGE_XN_4K_MASK;
1838       *descriptor_l2 |= ((xn & 0x1) << PAGE_XN_4K_SHIFT);
1839   }
1840   else
1841   {
1842       *descriptor_l2 &= PAGE_XN_64K_MASK;
1843       *descriptor_l2 |= ((xn & 0x1) << PAGE_XN_64K_SHIFT);
1844   }
1845   return 0;
1846 }
1847
1848 /** \brief  Set 4k/64k page domain
1849
1850   \param [out]    descriptor_l1  L1 descriptor.
1851   \param [in]            domain  Page domain
1852
1853   \return          0
1854 */
1855 __STATIC_INLINE int MMU_DomainPage(uint32_t *descriptor_l1, uint8_t domain)
1856 {
1857   *descriptor_l1 &= PAGE_DOMAIN_MASK;
1858   *descriptor_l1 |= ((domain & 0xf) << PAGE_DOMAIN_SHIFT);
1859   return 0;
1860 }
1861
1862 /** \brief  Set 4k/64k page parity check
1863
1864   \param [out]    descriptor_l1  L1 descriptor.
1865   \param [in]              p_bit Parity check: ECC_DISABLED, ECC_ENABLED
1866
1867   \return          0
1868 */
1869 __STATIC_INLINE int MMU_PPage(uint32_t *descriptor_l1, mmu_ecc_check_Type p_bit)
1870 {
1871   *descriptor_l1 &= SECTION_P_MASK;
1872   *descriptor_l1 |= ((p_bit & 0x1) << SECTION_P_SHIFT);
1873   return 0;
1874 }
1875
1876 /** \brief  Set 4k/64k page access privileges
1877
1878   \param [out]    descriptor_l2  L2 descriptor.
1879   \param [in]              user  User Level Access: NO_ACCESS, RW, READ
1880   \param [in]              priv  Privilege Level Access: NO_ACCESS, RW, READ
1881   \param [in]               afe  Access flag enable
1882
1883   \return          0
1884 */
1885 __STATIC_INLINE int MMU_APPage(uint32_t *descriptor_l2, mmu_access_Type user, mmu_access_Type priv, uint32_t afe)
1886 {
1887   uint32_t ap = 0;
1888
1889   if (afe == 0) { //full access
1890     if ((priv == NO_ACCESS) && (user == NO_ACCESS)) { ap = 0x0; }
1891     else if ((priv == RW) && (user == NO_ACCESS))   { ap = 0x1; }
1892     else if ((priv == RW) && (user == READ))        { ap = 0x2; }
1893     else if ((priv == RW) && (user == RW))          { ap = 0x3; }
1894     else if ((priv == READ) && (user == NO_ACCESS)) { ap = 0x5; }
1895     else if ((priv == READ) && (user == READ))      { ap = 0x6; }
1896   }
1897
1898   else { //Simplified access
1899     if ((priv == RW) && (user == NO_ACCESS))        { ap = 0x1; }
1900     else if ((priv == RW) && (user == RW))          { ap = 0x3; }
1901     else if ((priv == READ) && (user == NO_ACCESS)) { ap = 0x5; }
1902     else if ((priv == READ) && (user == READ))      { ap = 0x7; }
1903   }
1904
1905   *descriptor_l2 &= PAGE_AP_MASK;
1906   *descriptor_l2 |= (ap & 0x3) << PAGE_AP_SHIFT;
1907   *descriptor_l2 |= ((ap & 0x4)>>2) << PAGE_AP2_SHIFT;
1908
1909   return 0;
1910 }
1911
1912 /** \brief  Set 4k/64k page shareability
1913
1914   \param [out]    descriptor_l2  L2 descriptor.
1915   \param [in]             s_bit  4k/64k page shareability: NON_SHARED, SHARED
1916
1917   \return          0
1918 */
1919 __STATIC_INLINE int MMU_SharedPage(uint32_t *descriptor_l2, mmu_shared_Type s_bit)
1920 {
1921   *descriptor_l2 &= PAGE_S_MASK;
1922   *descriptor_l2 |= ((s_bit & 0x1) << PAGE_S_SHIFT);
1923   return 0;
1924 }
1925
1926 /** \brief  Set 4k/64k page Global attribute
1927
1928   \param [out]    descriptor_l2  L2 descriptor.
1929   \param [in]             g_bit  4k/64k page attribute: GLOBAL, NON_GLOBAL
1930
1931   \return          0
1932 */
1933 __STATIC_INLINE int MMU_GlobalPage(uint32_t *descriptor_l2, mmu_global_Type g_bit)
1934 {
1935   *descriptor_l2 &= PAGE_NG_MASK;
1936   *descriptor_l2 |= ((g_bit & 0x1) << PAGE_NG_SHIFT);
1937   return 0;
1938 }
1939
1940 /** \brief  Set 4k/64k page Security attribute
1941
1942   \param [out]    descriptor_l1  L1 descriptor.
1943   \param [in]             s_bit  4k/64k page Security attribute: SECURE, NON_SECURE
1944
1945   \return          0
1946 */
1947 __STATIC_INLINE int MMU_SecurePage(uint32_t *descriptor_l1, mmu_secure_Type s_bit)
1948 {
1949   *descriptor_l1 &= PAGE_NS_MASK;
1950   *descriptor_l1 |= ((s_bit & 0x1) << PAGE_NS_SHIFT);
1951   return 0;
1952 }
1953
1954 /** \brief  Set Section memory attributes
1955
1956   \param [out]    descriptor_l1  L1 descriptor.
1957   \param [in]               mem  Section memory type: NORMAL, DEVICE, SHARED_DEVICE, NON_SHARED_DEVICE, STRONGLY_ORDERED
1958   \param [in]             outer  Outer cacheability: NON_CACHEABLE, WB_WA, WT, WB_NO_WA,
1959   \param [in]             inner  Inner cacheability: NON_CACHEABLE, WB_WA, WT, WB_NO_WA,
1960
1961   \return          0
1962 */
1963 __STATIC_INLINE int MMU_MemorySection(uint32_t *descriptor_l1, mmu_memory_Type mem, mmu_cacheability_Type outer, mmu_cacheability_Type inner)
1964 {
1965   *descriptor_l1 &= SECTION_TEXCB_MASK;
1966
1967   if (STRONGLY_ORDERED == mem)
1968   {
1969     return 0;
1970   }
1971   else if (SHARED_DEVICE == mem)
1972   {
1973     *descriptor_l1 |= (1 << SECTION_B_SHIFT);
1974   }
1975   else if (NON_SHARED_DEVICE == mem)
1976   {
1977     *descriptor_l1 |= (1 << SECTION_TEX1_SHIFT);
1978   }
1979   else if (NORMAL == mem)
1980   {
1981    *descriptor_l1 |= 1 << SECTION_TEX2_SHIFT;
1982    switch(inner)
1983    {
1984       case NON_CACHEABLE:
1985         break;
1986       case WB_WA:
1987         *descriptor_l1 |= (1 << SECTION_B_SHIFT);
1988         break;
1989       case WT:
1990         *descriptor_l1 |= 1 << SECTION_C_SHIFT;
1991         break;
1992       case WB_NO_WA:
1993         *descriptor_l1 |= (1 << SECTION_B_SHIFT) | (1 << SECTION_C_SHIFT);
1994         break;
1995     }
1996     switch(outer)
1997     {
1998       case NON_CACHEABLE:
1999         break;
2000       case WB_WA:
2001         *descriptor_l1 |= (1 << SECTION_TEX0_SHIFT);
2002         break;
2003       case WT:
2004         *descriptor_l1 |= 1 << SECTION_TEX1_SHIFT;
2005         break;
2006       case WB_NO_WA:
2007         *descriptor_l1 |= (1 << SECTION_TEX0_SHIFT) | (1 << SECTION_TEX0_SHIFT);
2008         break;
2009     }
2010   }
2011   return 0;
2012 }
2013
2014 /** \brief  Set 4k/64k page memory attributes
2015
2016   \param [out]    descriptor_l2  L2 descriptor.
2017   \param [in]               mem  4k/64k page memory type: NORMAL, DEVICE, SHARED_DEVICE, NON_SHARED_DEVICE, STRONGLY_ORDERED
2018   \param [in]             outer  Outer cacheability: NON_CACHEABLE, WB_WA, WT, WB_NO_WA,
2019   \param [in]             inner  Inner cacheability: NON_CACHEABLE, WB_WA, WT, WB_NO_WA,
2020   \param [in]              page  Page size
2021
2022   \return          0
2023 */
2024 __STATIC_INLINE int MMU_MemoryPage(uint32_t *descriptor_l2, mmu_memory_Type mem, mmu_cacheability_Type outer, mmu_cacheability_Type inner, mmu_region_size_Type page)
2025 {
2026   *descriptor_l2 &= PAGE_4K_TEXCB_MASK;
2027
2028   if (page == PAGE_64k)
2029   {
2030     //same as section
2031     MMU_MemorySection(descriptor_l2, mem, outer, inner);
2032   }
2033   else
2034   {
2035     if (STRONGLY_ORDERED == mem)
2036     {
2037       return 0;
2038     }
2039     else if (SHARED_DEVICE == mem)
2040     {
2041       *descriptor_l2 |= (1 << PAGE_4K_B_SHIFT);
2042     }
2043     else if (NON_SHARED_DEVICE == mem)
2044     {
2045       *descriptor_l2 |= (1 << PAGE_4K_TEX1_SHIFT);
2046     }
2047     else if (NORMAL == mem)
2048     {
2049       *descriptor_l2 |= 1 << PAGE_4K_TEX2_SHIFT;
2050       switch(inner)
2051       {
2052         case NON_CACHEABLE:
2053           break;
2054         case WB_WA:
2055           *descriptor_l2 |= (1 << PAGE_4K_B_SHIFT);
2056           break;
2057         case WT:
2058           *descriptor_l2 |= 1 << PAGE_4K_C_SHIFT;
2059           break;
2060         case WB_NO_WA:
2061           *descriptor_l2 |= (1 << PAGE_4K_B_SHIFT) | (1 << PAGE_4K_C_SHIFT);
2062           break;
2063       }
2064       switch(outer)
2065       {
2066         case NON_CACHEABLE:
2067           break;
2068         case WB_WA:
2069           *descriptor_l2 |= (1 << PAGE_4K_TEX0_SHIFT);
2070           break;
2071         case WT:
2072           *descriptor_l2 |= 1 << PAGE_4K_TEX1_SHIFT;
2073           break;
2074         case WB_NO_WA:
2075           *descriptor_l2 |= (1 << PAGE_4K_TEX0_SHIFT) | (1 << PAGE_4K_TEX0_SHIFT);
2076           break;
2077       }
2078     }
2079   }
2080
2081   return 0;
2082 }
2083
2084 /** \brief  Create a L1 section descriptor
2085
2086   \param [out]     descriptor  L1 descriptor
2087   \param [in]      reg  Section attributes
2088   
2089   \return          0
2090 */
2091 __STATIC_INLINE int MMU_GetSectionDescriptor(uint32_t *descriptor, mmu_region_attributes_Type reg)
2092 {
2093   *descriptor  = 0;
2094
2095   MMU_MemorySection(descriptor, reg.mem_t, reg.outer_norm_t, reg.inner_norm_t);
2096   MMU_XNSection(descriptor,reg.xn_t);
2097   MMU_DomainSection(descriptor, reg.domain);
2098   MMU_PSection(descriptor, reg.e_t);
2099   MMU_APSection(descriptor, reg.priv_t, reg.user_t, 1);
2100   MMU_SharedSection(descriptor,reg.sh_t);
2101   MMU_GlobalSection(descriptor,reg.g_t);
2102   MMU_SecureSection(descriptor,reg.sec_t);
2103   *descriptor &= SECTION_MASK;
2104   *descriptor |= SECTION_DESCRIPTOR;
2105  
2106   return 0;
2107 }
2108
2109
2110 /** \brief  Create a L1 and L2 4k/64k page descriptor
2111
2112   \param [out]       descriptor  L1 descriptor
2113   \param [out]      descriptor2  L2 descriptor
2114   \param [in]               reg  4k/64k page attributes
2115
2116   \return          0
2117 */
2118 __STATIC_INLINE int MMU_GetPageDescriptor(uint32_t *descriptor, uint32_t *descriptor2, mmu_region_attributes_Type reg)
2119 {
2120   *descriptor  = 0;
2121   *descriptor2 = 0;
2122
2123   switch (reg.rg_t)
2124   {
2125     case PAGE_4k:
2126       MMU_MemoryPage(descriptor2, reg.mem_t, reg.outer_norm_t, reg.inner_norm_t, PAGE_4k);
2127       MMU_XNPage(descriptor2, reg.xn_t, PAGE_4k);
2128       MMU_DomainPage(descriptor, reg.domain);
2129       MMU_PPage(descriptor, reg.e_t);
2130       MMU_APPage(descriptor2, reg.priv_t, reg.user_t, 1);
2131       MMU_SharedPage(descriptor2,reg.sh_t);
2132       MMU_GlobalPage(descriptor2,reg.g_t);
2133       MMU_SecurePage(descriptor,reg.sec_t);
2134       *descriptor &= PAGE_L1_MASK;
2135       *descriptor |= PAGE_L1_DESCRIPTOR;
2136       *descriptor2 &= PAGE_L2_4K_MASK;
2137       *descriptor2 |= PAGE_L2_4K_DESC;
2138       break;
2139
2140     case PAGE_64k:
2141       MMU_MemoryPage(descriptor2, reg.mem_t, reg.outer_norm_t, reg.inner_norm_t, PAGE_64k);
2142       MMU_XNPage(descriptor2, reg.xn_t, PAGE_64k);
2143       MMU_DomainPage(descriptor, reg.domain);
2144       MMU_PPage(descriptor, reg.e_t);
2145       MMU_APPage(descriptor2, reg.priv_t, reg.user_t, 1);
2146       MMU_SharedPage(descriptor2,reg.sh_t);
2147       MMU_GlobalPage(descriptor2,reg.g_t);
2148       MMU_SecurePage(descriptor,reg.sec_t);
2149       *descriptor &= PAGE_L1_MASK;
2150       *descriptor |= PAGE_L1_DESCRIPTOR;
2151       *descriptor2 &= PAGE_L2_64K_MASK;
2152       *descriptor2 |= PAGE_L2_64K_DESC;
2153       break;
2154
2155     case SECTION:
2156       //error
2157       break;
2158   }
2159   
2160   return 0;
2161 }
2162
2163 /** \brief  Create a 1MB Section
2164
2165   \param [in]               ttb  Translation table base address
2166   \param [in]      base_address  Section base address
2167   \param [in]             count  Number of sections to create
2168   \param [in]     descriptor_l1  L1 descriptor (region attributes)
2169
2170 */
2171 __STATIC_INLINE void MMU_TTSection(uint32_t *ttb, uint32_t base_address, uint32_t count, uint32_t descriptor_l1)
2172 {
2173   uint32_t offset;
2174   uint32_t entry;
2175   uint32_t i;
2176
2177   offset = base_address >> 20;
2178   entry  = (base_address & 0xFFF00000) | descriptor_l1;
2179
2180   //4 bytes aligned
2181   ttb = ttb + offset;
2182
2183   for (i = 0; i < count; i++ )
2184   {
2185     //4 bytes aligned
2186     *ttb++ = entry;
2187     entry += OFFSET_1M;
2188   }
2189 }
2190
2191 /** \brief  Create a 4k page entry
2192
2193   \param [in]               ttb  L1 table base address
2194   \param [in]      base_address  4k base address
2195   \param [in]             count  Number of 4k pages to create
2196   \param [in]     descriptor_l1  L1 descriptor (region attributes)
2197   \param [in]            ttb_l2  L2 table base address
2198   \param [in]     descriptor_l2  L2 descriptor (region attributes)
2199
2200 */
2201 __STATIC_INLINE void MMU_TTPage4k(uint32_t *ttb, uint32_t base_address, uint32_t count, uint32_t descriptor_l1, uint32_t *ttb_l2, uint32_t descriptor_l2 )
2202 {
2203
2204   uint32_t offset, offset2;
2205   uint32_t entry, entry2;
2206   uint32_t i;
2207
2208   offset = base_address >> 20;
2209   entry  = ((int)ttb_l2 & 0xFFFFFC00) | descriptor_l1;
2210
2211   //4 bytes aligned
2212   ttb += offset;
2213   //create l1_entry
2214   *ttb = entry;
2215
2216   offset2 = (base_address & 0xff000) >> 12;
2217   ttb_l2 += offset2;
2218   entry2 = (base_address & 0xFFFFF000) | descriptor_l2;
2219   for (i = 0; i < count; i++ )
2220   {
2221     //4 bytes aligned
2222     *ttb_l2++ = entry2;
2223     entry2 += OFFSET_4K;
2224   }
2225 }
2226
2227 /** \brief  Create a 64k page entry
2228
2229   \param [in]               ttb  L1 table base address
2230   \param [in]      base_address  64k base address
2231   \param [in]             count  Number of 64k pages to create
2232   \param [in]     descriptor_l1  L1 descriptor (region attributes)
2233   \param [in]            ttb_l2  L2 table base address
2234   \param [in]     descriptor_l2  L2 descriptor (region attributes)
2235
2236 */
2237 __STATIC_INLINE void MMU_TTPage64k(uint32_t *ttb, uint32_t base_address, uint32_t count, uint32_t descriptor_l1, uint32_t *ttb_l2, uint32_t descriptor_l2 )
2238 {
2239   uint32_t offset, offset2;
2240   uint32_t entry, entry2;
2241   uint32_t i,j;
2242
2243
2244   offset = base_address >> 20;
2245   entry  = ((int)ttb_l2 & 0xFFFFFC00) | descriptor_l1;
2246
2247   //4 bytes aligned
2248   ttb += offset;
2249   //create l1_entry
2250   *ttb = entry;
2251
2252   offset2 = (base_address & 0xff000) >> 12;
2253   ttb_l2 += offset2;
2254   entry2 = (base_address & 0xFFFF0000) | descriptor_l2;
2255   for (i = 0; i < count; i++ )
2256   {
2257     //create 16 entries
2258     for (j = 0; j < 16; j++)
2259     {
2260       //4 bytes aligned
2261       *ttb_l2++ = entry2;
2262     }
2263     entry2 += OFFSET_64K;
2264   }
2265 }
2266
2267 /** \brief  Enable MMU
2268 */
2269 __STATIC_INLINE void MMU_Enable(void) {
2270   // Set M bit 0 to enable the MMU
2271   // Set AFE bit to enable simplified access permissions model
2272   // Clear TRE bit to disable TEX remap and A bit to disable strict alignment fault checking
2273   __set_SCTLR( (__get_SCTLR() & ~(1 << 28) & ~(1 << 1)) | 1 | (1 << 29));
2274   __ISB();
2275 }
2276
2277 /** \brief  Disable MMU
2278 */
2279 __STATIC_INLINE void MMU_Disable(void) {
2280   // Clear M bit 0 to disable the MMU
2281   __set_SCTLR( __get_SCTLR() & ~1);
2282   __ISB();
2283 }
2284
2285 /** \brief  Invalidate entire unified TLB
2286 */
2287
2288 __STATIC_INLINE void MMU_InvalidateTLB(void) {
2289   __set_TLBIALL(0);
2290   __DSB();     //ensure completion of the invalidation
2291   __ISB();     //ensure instruction fetch path sees new state
2292 }
2293
2294
2295 #ifdef __cplusplus
2296 }
2297 #endif
2298
2299 #endif /* __CORE_CA_H_DEPENDANT */
2300
2301 #endif /* __CMSIS_GENERIC */