a53dbc2713d14e0ecfe80a1e44f4cec82fd384b5
[gps-watch.git] / src / common / fsl_common.h
1 /*
2  * The Clear BSD License
3  * Copyright (c) 2015-2016, Freescale Semiconductor, Inc.
4  * Copyright 2016-2017 NXP
5  * All rights reserved.
6  * 
7  * Redistribution and use in source and binary forms, with or without modification,
8  * are permitted (subject to the limitations in the disclaimer below) provided
9  *  that the following conditions are met:
10  *
11  * o Redistributions of source code must retain the above copyright notice, this list
12  *   of conditions and the following disclaimer.
13  *
14  * o Redistributions in binary form must reproduce the above copyright notice, this
15  *   list of conditions and the following disclaimer in the documentation and/or
16  *   other materials provided with the distribution.
17  *
18  * o Neither the name of the copyright holder nor the names of its
19  *   contributors may be used to endorse or promote products derived from this
20  *   software without specific prior written permission.
21  *
22  * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
27  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34
35 #ifndef _FSL_COMMON_H_
36 #define _FSL_COMMON_H_
37
38 #include <assert.h>
39 #include <stdbool.h>
40 #include <stdint.h>
41 #include <string.h>
42 #include <stdlib.h>
43
44 #if defined(__ICCARM__)
45 #include <stddef.h>
46 #endif
47
48 #include "fsl_device_registers.h"
49
50 /*!
51  * @addtogroup ksdk_common
52  * @{
53  */
54
55 /*******************************************************************************
56  * Definitions
57  ******************************************************************************/
58
59 /*! @brief Construct a status code value from a group and code number. */
60 #define MAKE_STATUS(group, code) ((((group)*100) + (code)))
61
62 /*! @brief Construct the version number for drivers. */
63 #define MAKE_VERSION(major, minor, bugfix) (((major) << 16) | ((minor) << 8) | (bugfix))
64
65 /*! @name Driver version */
66 /*@{*/
67 /*! @brief common driver version 2.0.0. */
68 #define FSL_COMMON_DRIVER_VERSION (MAKE_VERSION(2, 0, 0))
69 /*@}*/
70
71 /* Debug console type definition. */
72 #define DEBUG_CONSOLE_DEVICE_TYPE_NONE          0U      /*!< No debug console.             */
73 #define DEBUG_CONSOLE_DEVICE_TYPE_UART          1U      /*!< Debug console base on UART.   */
74 #define DEBUG_CONSOLE_DEVICE_TYPE_LPUART        2U      /*!< Debug console base on LPUART. */
75 #define DEBUG_CONSOLE_DEVICE_TYPE_LPSCI         3U      /*!< Debug console base on LPSCI.  */
76 #define DEBUG_CONSOLE_DEVICE_TYPE_USBCDC        4U      /*!< Debug console base on USBCDC. */
77 #define DEBUG_CONSOLE_DEVICE_TYPE_FLEXCOMM      5U      /*!< Debug console base on USBCDC. */
78 #define DEBUG_CONSOLE_DEVICE_TYPE_IUART         6U      /*!< Debug console base on i.MX UART. */
79 #define DEBUG_CONSOLE_DEVICE_TYPE_VUSART        7U      /*!< Debug console base on LPC_USART. */
80 #define DEBUG_CONSOLE_DEVICE_TYPE_MINI_USART    8U      /*!< Debug console base on LPC_USART. */
81 #define DEBUG_CONSOLE_DEVICE_TYPE_SWO           9U      /*!< Debug console base on SWO. */
82
83 /*! @brief Status group numbers. */
84 enum _status_groups
85 {
86     kStatusGroup_Generic = 0,                 /*!< Group number for generic status codes. */
87     kStatusGroup_FLASH = 1,                   /*!< Group number for FLASH status codes. */
88     kStatusGroup_LPSPI = 4,                   /*!< Group number for LPSPI status codes. */
89     kStatusGroup_FLEXIO_SPI = 5,              /*!< Group number for FLEXIO SPI status codes. */
90     kStatusGroup_DSPI = 6,                    /*!< Group number for DSPI status codes. */
91     kStatusGroup_FLEXIO_UART = 7,             /*!< Group number for FLEXIO UART status codes. */
92     kStatusGroup_FLEXIO_I2C = 8,              /*!< Group number for FLEXIO I2C status codes. */
93     kStatusGroup_LPI2C = 9,                   /*!< Group number for LPI2C status codes. */
94     kStatusGroup_UART = 10,                   /*!< Group number for UART status codes. */
95     kStatusGroup_I2C = 11,                    /*!< Group number for UART status codes. */
96     kStatusGroup_LPSCI = 12,                  /*!< Group number for LPSCI status codes. */
97     kStatusGroup_LPUART = 13,                 /*!< Group number for LPUART status codes. */
98     kStatusGroup_SPI = 14,                    /*!< Group number for SPI status code.*/
99     kStatusGroup_XRDC = 15,                   /*!< Group number for XRDC status code.*/
100     kStatusGroup_SEMA42 = 16,                 /*!< Group number for SEMA42 status code.*/
101     kStatusGroup_SDHC = 17,                   /*!< Group number for SDHC status code */
102     kStatusGroup_SDMMC = 18,                  /*!< Group number for SDMMC status code */
103     kStatusGroup_SAI = 19,                    /*!< Group number for SAI status code */
104     kStatusGroup_MCG = 20,                    /*!< Group number for MCG status codes. */
105     kStatusGroup_SCG = 21,                    /*!< Group number for SCG status codes. */
106     kStatusGroup_SDSPI = 22,                  /*!< Group number for SDSPI status codes. */
107     kStatusGroup_FLEXIO_I2S = 23,             /*!< Group number for FLEXIO I2S status codes */
108     kStatusGroup_FLEXIO_MCULCD = 24,          /*!< Group number for FLEXIO LCD status codes */
109     kStatusGroup_FLASHIAP = 25,               /*!< Group number for FLASHIAP status codes */
110     kStatusGroup_FLEXCOMM_I2C = 26,           /*!< Group number for FLEXCOMM I2C status codes */
111     kStatusGroup_I2S = 27,                    /*!< Group number for I2S status codes */
112     kStatusGroup_IUART = 28,                  /*!< Group number for IUART status codes */
113     kStatusGroup_CSI = 29,                    /*!< Group number for CSI status codes */
114     kStatusGroup_MIPI_DSI = 30,               /*!< Group number for MIPI DSI status codes */
115     kStatusGroup_SDRAMC = 35,                 /*!< Group number for SDRAMC status codes. */
116     kStatusGroup_POWER = 39,                  /*!< Group number for POWER status codes. */
117     kStatusGroup_ENET = 40,                   /*!< Group number for ENET status codes. */
118     kStatusGroup_PHY = 41,                    /*!< Group number for PHY status codes. */
119     kStatusGroup_TRGMUX = 42,                 /*!< Group number for TRGMUX status codes. */
120     kStatusGroup_SMARTCARD = 43,              /*!< Group number for SMARTCARD status codes. */
121     kStatusGroup_LMEM = 44,                   /*!< Group number for LMEM status codes. */
122     kStatusGroup_QSPI = 45,                   /*!< Group number for QSPI status codes. */
123     kStatusGroup_DMA = 50,                    /*!< Group number for DMA status codes. */
124     kStatusGroup_EDMA = 51,                   /*!< Group number for EDMA status codes. */
125     kStatusGroup_DMAMGR = 52,                 /*!< Group number for DMAMGR status codes. */
126     kStatusGroup_FLEXCAN = 53,                /*!< Group number for FlexCAN status codes. */
127     kStatusGroup_LTC = 54,                    /*!< Group number for LTC status codes. */
128     kStatusGroup_FLEXIO_CAMERA = 55,          /*!< Group number for FLEXIO CAMERA status codes. */
129     kStatusGroup_LPC_SPI = 56,                /*!< Group number for LPC_SPI status codes. */
130     kStatusGroup_LPC_USART = 57,              /*!< Group number for LPC_USART status codes. */
131     kStatusGroup_DMIC = 58,                   /*!< Group number for DMIC status codes. */
132     kStatusGroup_SDIF = 59,                   /*!< Group number for SDIF status codes.*/
133     kStatusGroup_SPIFI = 60,                  /*!< Group number for SPIFI status codes. */
134     kStatusGroup_OTP = 61,                    /*!< Group number for OTP status codes. */
135     kStatusGroup_MCAN = 62,                   /*!< Group number for MCAN status codes. */
136     kStatusGroup_CAAM = 63,                   /*!< Group number for CAAM status codes. */
137     kStatusGroup_ECSPI = 64,                  /*!< Group number for ECSPI status codes. */
138     kStatusGroup_USDHC = 65,                  /*!< Group number for USDHC status codes.*/
139     kStatusGroup_LPC_I2C = 66,                /*!< Group number for LPC_I2C status codes.*/
140     kStatusGroup_DCP = 67,                    /*!< Group number for DCP status codes.*/
141     kStatusGroup_MSCAN = 68,                  /*!< Group number for MSCAN status codes.*/
142     kStatusGroup_ESAI = 69,                   /*!< Group number for ESAI status codes. */
143     kStatusGroup_FLEXSPI = 70,                /*!< Group number for FLEXSPI status codes. */
144     kStatusGroup_MMDC = 71,                   /*!< Group number for MMDC status codes. */
145     kStatusGroup_MICFIL = 72,                 /*!< Group number for MIC status codes. */
146     kStatusGroup_SDMA = 73,                   /*!< Group number for SDMA status codes. */
147     kStatusGroup_ICS = 74,                    /*!< Group number for ICS status codes. */
148     kStatusGroup_SPDIF = 75,                  /*!< Group number for SPDIF status codes. */
149     kStatusGroup_LPC_MINISPI = 76,            /*!< Group number for LPC_MINISPI status codes. */
150     kStatusGroup_NOTIFIER = 98,               /*!< Group number for NOTIFIER status codes. */
151     kStatusGroup_DebugConsole = 99,           /*!< Group number for debug console status codes. */
152     kStatusGroup_SEMC = 100,                   /*!< Group number for SEMC status codes. */    
153     kStatusGroup_ApplicationRangeStart = 101, /*!< Starting number for application groups. */
154 };
155
156 /*! @brief Generic status return codes. */
157 enum _generic_status
158 {
159     kStatus_Success = MAKE_STATUS(kStatusGroup_Generic, 0),
160     kStatus_Fail = MAKE_STATUS(kStatusGroup_Generic, 1),
161     kStatus_ReadOnly = MAKE_STATUS(kStatusGroup_Generic, 2),
162     kStatus_OutOfRange = MAKE_STATUS(kStatusGroup_Generic, 3),
163     kStatus_InvalidArgument = MAKE_STATUS(kStatusGroup_Generic, 4),
164     kStatus_Timeout = MAKE_STATUS(kStatusGroup_Generic, 5),
165     kStatus_NoTransferInProgress = MAKE_STATUS(kStatusGroup_Generic, 6),
166 };
167
168 /*! @brief Type used for all status and error return values. */
169 typedef int32_t status_t;
170
171 /*
172  * The fsl_clock.h is included here because it needs MAKE_VERSION/MAKE_STATUS/status_t
173  * defined in previous of this file.
174  */
175 #include "fsl_clock.h"
176
177 /*
178  * Chip level peripheral reset API, for MCUs that implement peripheral reset control external to a peripheral
179  */
180 #if ((defined(FSL_FEATURE_SOC_SYSCON_COUNT) && (FSL_FEATURE_SOC_SYSCON_COUNT > 0)) || \
181      (defined(FSL_FEATURE_SOC_ASYNC_SYSCON_COUNT) && (FSL_FEATURE_SOC_ASYNC_SYSCON_COUNT > 0)))
182 #include "fsl_reset.h"
183 #endif
184
185 /*
186  * Macro guard for whether to use default weak IRQ implementation in drivers
187  */
188 #ifndef FSL_DRIVER_TRANSFER_DOUBLE_WEAK_IRQ
189 #define FSL_DRIVER_TRANSFER_DOUBLE_WEAK_IRQ 1
190 #endif
191
192 /*! @name Min/max macros */
193 /* @{ */
194 #if !defined(MIN)
195 #define MIN(a, b) ((a) < (b) ? (a) : (b))
196 #endif
197
198 #if !defined(MAX)
199 #define MAX(a, b) ((a) > (b) ? (a) : (b))
200 #endif
201 /* @} */
202
203 /*! @brief Computes the number of elements in an array. */
204 #if !defined(ARRAY_SIZE)
205 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
206 #endif
207
208 /*! @name UINT16_MAX/UINT32_MAX value */
209 /* @{ */
210 #if !defined(UINT16_MAX)
211 #define UINT16_MAX ((uint16_t)-1)
212 #endif
213
214 #if !defined(UINT32_MAX)
215 #define UINT32_MAX ((uint32_t)-1)
216 #endif
217 /* @} */
218
219 /*! @name Timer utilities */
220 /* @{ */
221 /*! Macro to convert a microsecond period to raw count value */
222 #define USEC_TO_COUNT(us, clockFreqInHz) (uint64_t)((uint64_t)us * clockFreqInHz / 1000000U)
223 /*! Macro to convert a raw count value to microsecond */
224 #define COUNT_TO_USEC(count, clockFreqInHz) (uint64_t)((uint64_t)count * 1000000U / clockFreqInHz)
225
226 /*! Macro to convert a millisecond period to raw count value */
227 #define MSEC_TO_COUNT(ms, clockFreqInHz) (uint64_t)((uint64_t)ms * clockFreqInHz / 1000U)
228 /*! Macro to convert a raw count value to millisecond */
229 #define COUNT_TO_MSEC(count, clockFreqInHz) (uint64_t)((uint64_t)count * 1000U / clockFreqInHz)
230 /* @} */
231
232 /*! @name Alignment variable definition macros */
233 /* @{ */
234 #if (defined(__ICCARM__))
235 /**
236  * Workaround to disable MISRA C message suppress warnings for IAR compiler.
237  * http://supp.iar.com/Support/?note=24725
238  */
239 _Pragma("diag_suppress=Pm120")
240 #define SDK_PRAGMA(x) _Pragma(#x)
241     _Pragma("diag_error=Pm120")
242 /*! Macro to define a variable with alignbytes alignment */
243 #define SDK_ALIGN(var, alignbytes) SDK_PRAGMA(data_alignment = alignbytes) var
244 /*! Macro to define a variable with L1 d-cache line size alignment */
245 #if defined(FSL_FEATURE_L1DCACHE_LINESIZE_BYTE)
246 #define SDK_L1DCACHE_ALIGN(var) SDK_PRAGMA(data_alignment = FSL_FEATURE_L1DCACHE_LINESIZE_BYTE) var
247 #endif
248 /*! Macro to define a variable with L2 cache line size alignment */
249 #if defined(FSL_FEATURE_L2CACHE_LINESIZE_BYTE)
250 #define SDK_L2CACHE_ALIGN(var) SDK_PRAGMA(data_alignment = FSL_FEATURE_L2CACHE_LINESIZE_BYTE) var
251 #endif
252 #elif defined(__ARMCC_VERSION)
253 /*! Macro to define a variable with alignbytes alignment */
254 #define SDK_ALIGN(var, alignbytes) __align(alignbytes) var
255 /*! Macro to define a variable with L1 d-cache line size alignment */
256 #if defined(FSL_FEATURE_L1DCACHE_LINESIZE_BYTE)
257 #define SDK_L1DCACHE_ALIGN(var) __align(FSL_FEATURE_L1DCACHE_LINESIZE_BYTE) var
258 #endif
259 /*! Macro to define a variable with L2 cache line size alignment */
260 #if defined(FSL_FEATURE_L2CACHE_LINESIZE_BYTE)
261 #define SDK_L2CACHE_ALIGN(var) __align(FSL_FEATURE_L2CACHE_LINESIZE_BYTE) var
262 #endif
263 #elif defined(__GNUC__)
264 /*! Macro to define a variable with alignbytes alignment */
265 #define SDK_ALIGN(var, alignbytes) var __attribute__((aligned(alignbytes)))
266 /*! Macro to define a variable with L1 d-cache line size alignment */
267 #if defined(FSL_FEATURE_L1DCACHE_LINESIZE_BYTE)
268 #define SDK_L1DCACHE_ALIGN(var) var __attribute__((aligned(FSL_FEATURE_L1DCACHE_LINESIZE_BYTE)))
269 #endif
270 /*! Macro to define a variable with L2 cache line size alignment */
271 #if defined(FSL_FEATURE_L2CACHE_LINESIZE_BYTE)
272 #define SDK_L2CACHE_ALIGN(var) var __attribute__((aligned(FSL_FEATURE_L2CACHE_LINESIZE_BYTE)))
273 #endif
274 #else
275 #error Toolchain not supported
276 #define SDK_ALIGN(var, alignbytes) var
277 #if defined(FSL_FEATURE_L1DCACHE_LINESIZE_BYTE)
278 #define SDK_L1DCACHE_ALIGN(var) var
279 #endif
280 #if defined(FSL_FEATURE_L2CACHE_LINESIZE_BYTE)
281 #define SDK_L2CACHE_ALIGN(var) var
282 #endif
283 #endif
284
285 /*! Macro to change a value to a given size aligned value */
286 #define SDK_SIZEALIGN(var, alignbytes) \
287     ((unsigned int)((var) + ((alignbytes)-1)) & (unsigned int)(~(unsigned int)((alignbytes)-1)))
288 /* @} */
289
290 /*! @name Non-cacheable region definition macros */
291 /* For initialized non-zero non-cacheable variables, please using "AT_NONCACHEABLE_SECTION_INIT(var) ={xx};" or
292  * "AT_NONCACHEABLE_SECTION_ALIGN_INIT(var) ={xx};" in your projects to define them, for zero-inited non-cacheable variables,
293  * please using "AT_NONCACHEABLE_SECTION(var);" or "AT_NONCACHEABLE_SECTION_ALIGN(var);" to define them, these zero-inited variables
294  * will be initialized to zero in system startup.
295  */
296 /* @{ */
297 #if (defined(__ICCARM__))
298 #if defined(FSL_FEATURE_L1ICACHE_LINESIZE_BYTE)
299 #define AT_NONCACHEABLE_SECTION(var) var @"NonCacheable"
300 #define AT_NONCACHEABLE_SECTION_ALIGN(var, alignbytes) SDK_PRAGMA(data_alignment = alignbytes) var @"NonCacheable"
301 #define AT_NONCACHEABLE_SECTION_INIT(var) var @"NonCacheable.init"
302 #define AT_NONCACHEABLE_SECTION_ALIGN_INIT(var, alignbytes) SDK_PRAGMA(data_alignment = alignbytes) var @"NonCacheable.init"
303 #else
304 #define AT_NONCACHEABLE_SECTION(var) var
305 #define AT_NONCACHEABLE_SECTION_ALIGN(var, alignbytes) SDK_PRAGMA(data_alignment = alignbytes) var
306 #define AT_NONCACHEABLE_SECTION_INIT(var) var
307 #define AT_NONCACHEABLE_SECTION_ALIGN_INIT(var, alignbytes) SDK_PRAGMA(data_alignment = alignbytes) var
308 #endif
309 #elif(defined(__ARMCC_VERSION))
310 #if defined(FSL_FEATURE_L1ICACHE_LINESIZE_BYTE)
311 #define AT_NONCACHEABLE_SECTION(var) __attribute__((section("NonCacheable"), zero_init)) var
312 #define AT_NONCACHEABLE_SECTION_ALIGN(var, alignbytes) \
313     __attribute__((section("NonCacheable"), zero_init)) __align(alignbytes) var
314 #define AT_NONCACHEABLE_SECTION_INIT(var) __attribute__((section("NonCacheable.init"))) var
315 #define AT_NONCACHEABLE_SECTION_ALIGN_INIT(var, alignbytes) \
316     __attribute__((section("NonCacheable.init"))) __align(alignbytes) var
317 #else
318 #define AT_NONCACHEABLE_SECTION(var) var
319 #define AT_NONCACHEABLE_SECTION_ALIGN(var, alignbytes) __align(alignbytes) var
320 #define AT_NONCACHEABLE_SECTION_INIT(var) var
321 #define AT_NONCACHEABLE_SECTION_ALIGN_INIT(var, alignbytes) __align(alignbytes) var
322 #endif
323 #elif(defined(__GNUC__))
324 /* For GCC, when the non-cacheable section is required, please define "__STARTUP_INITIALIZE_NONCACHEDATA"
325  * in your projects to make sure the non-cacheable section variables will be initialized in system startup.
326  */
327 #if defined(FSL_FEATURE_L1ICACHE_LINESIZE_BYTE)
328 #define AT_NONCACHEABLE_SECTION_INIT(var) __attribute__((section("NonCacheable.init"))) var
329 #define AT_NONCACHEABLE_SECTION_ALIGN_INIT(var, alignbytes) \
330     __attribute__((section("NonCacheable.init"))) var __attribute__((aligned(alignbytes)))
331 #define AT_NONCACHEABLE_SECTION(var) __attribute__((section("NonCacheable,\"aw\",%nobits @"))) var
332 #define AT_NONCACHEABLE_SECTION_ALIGN(var, alignbytes) \
333     __attribute__((section("NonCacheable,\"aw\",%nobits @"))) var __attribute__((aligned(alignbytes)))
334 #else
335 #define AT_NONCACHEABLE_SECTION(var) var
336 #define AT_NONCACHEABLE_SECTION_ALIGN(var, alignbytes) var __attribute__((aligned(alignbytes)))
337 #define AT_NONCACHEABLE_SECTION_INIT(var) var
338 #define AT_NONCACHEABLE_SECTION_ALIGN_INIT(var, alignbytes) var __attribute__((aligned(alignbytes)))
339 #endif
340 #else
341 #error Toolchain not supported.
342 #define AT_NONCACHEABLE_SECTION(var) var
343 #define AT_NONCACHEABLE_SECTION_ALIGN(var, alignbytes) var
344 #define AT_NONCACHEABLE_SECTION_INIT(var) var
345 #define AT_NONCACHEABLE_SECTION_ALIGN_INIT(var, alignbytes) var
346 #endif
347 /* @} */
348
349 /*! @name Time sensitive region */
350 /* @{ */
351 #if defined(FSL_SDK_DRIVER_QUICK_ACCESS_ENABLE) && FSL_SDK_DRIVER_QUICK_ACCESS_ENABLE
352 #if (defined(__ICCARM__))
353 #define AT_QUICKACCESS_SECTION_CODE(func) func @"CodeQuickAccess"
354 #define AT_QUICKACCESS_SECTION_DATA(func) func @"DataQuickAccess"
355 #elif(defined(__ARMCC_VERSION))
356 #define AT_QUICKACCESS_SECTION_CODE(func) __attribute__((section("CodeQuickAccess"))) func
357 #define AT_QUICKACCESS_SECTION_DATA(func) __attribute__((section("DataQuickAccess"))) func
358 #elif(defined(__GNUC__))
359 #define AT_QUICKACCESS_SECTION_CODE(func) __attribute__((section("CodeQuickAccess"))) func
360 #define AT_QUICKACCESS_SECTION_DATA(func) __attribute__((section("DataQuickAccess"))) func
361 #else
362 #error Toolchain not supported.
363 #endif /* defined(__ICCARM__) */
364 #else
365 #if (defined(__ICCARM__))
366 #define AT_QUICKACCESS_SECTION_CODE(func) func
367 #define AT_QUICKACCESS_SECTION_DATA(func) func
368 #elif(defined(__ARMCC_VERSION))
369 #define AT_QUICKACCESS_SECTION_CODE(func) func
370 #define AT_QUICKACCESS_SECTION_DATA(func) func
371 #elif(defined(__GNUC__))
372 #define AT_QUICKACCESS_SECTION_CODE(func) func
373 #define AT_QUICKACCESS_SECTION_DATA(func) func
374 #else
375 #error Toolchain not supported.
376 #endif    
377 #endif /* __FSL_SDK_DRIVER_QUICK_ACCESS_ENABLE */
378 /* @} */
379
380 /*******************************************************************************
381  * API
382  ******************************************************************************/
383
384 #if defined(__cplusplus)
385         extern "C"
386 {
387 #endif
388
389     /*!
390      * @brief Enable specific interrupt.
391      *
392      * Enable LEVEL1 interrupt. For some devices, there might be multiple interrupt
393      * levels. For example, there are NVIC and intmux. Here the interrupts connected
394      * to NVIC are the LEVEL1 interrupts, because they are routed to the core directly.
395      * The interrupts connected to intmux are the LEVEL2 interrupts, they are routed
396      * to NVIC first then routed to core.
397      *
398      * This function only enables the LEVEL1 interrupts. The number of LEVEL1 interrupts
399      * is indicated by the feature macro FSL_FEATURE_NUMBER_OF_LEVEL1_INT_VECTORS.
400      *
401      * @param interrupt The IRQ number.
402      * @retval kStatus_Success Interrupt enabled successfully
403      * @retval kStatus_Fail Failed to enable the interrupt
404      */
405     static inline status_t EnableIRQ(IRQn_Type interrupt)
406     {
407         if (NotAvail_IRQn == interrupt)
408         {
409             return kStatus_Fail;
410         }
411
412 #if defined(FSL_FEATURE_NUMBER_OF_LEVEL1_INT_VECTORS) && (FSL_FEATURE_NUMBER_OF_LEVEL1_INT_VECTORS > 0)
413         if (interrupt >= FSL_FEATURE_NUMBER_OF_LEVEL1_INT_VECTORS)
414         {
415             return kStatus_Fail;
416         }
417 #endif
418
419 #if defined(__GIC_PRIO_BITS)
420         GIC_EnableIRQ(interrupt);
421 #else
422         NVIC_EnableIRQ(interrupt);
423 #endif
424         return kStatus_Success;
425     }
426
427     /*!
428      * @brief Disable specific interrupt.
429      *
430      * Disable LEVEL1 interrupt. For some devices, there might be multiple interrupt
431      * levels. For example, there are NVIC and intmux. Here the interrupts connected
432      * to NVIC are the LEVEL1 interrupts, because they are routed to the core directly.
433      * The interrupts connected to intmux are the LEVEL2 interrupts, they are routed
434      * to NVIC first then routed to core.
435      *
436      * This function only disables the LEVEL1 interrupts. The number of LEVEL1 interrupts
437      * is indicated by the feature macro FSL_FEATURE_NUMBER_OF_LEVEL1_INT_VECTORS.
438      *
439      * @param interrupt The IRQ number.
440      * @retval kStatus_Success Interrupt disabled successfully
441      * @retval kStatus_Fail Failed to disable the interrupt
442      */
443     static inline status_t DisableIRQ(IRQn_Type interrupt)
444     {
445         if (NotAvail_IRQn == interrupt)
446         {
447             return kStatus_Fail;
448         }
449
450 #if defined(FSL_FEATURE_NUMBER_OF_LEVEL1_INT_VECTORS) && (FSL_FEATURE_NUMBER_OF_LEVEL1_INT_VECTORS > 0)
451         if (interrupt >= FSL_FEATURE_NUMBER_OF_LEVEL1_INT_VECTORS)
452         {
453             return kStatus_Fail;
454         }
455 #endif
456
457 #if defined(__GIC_PRIO_BITS)
458         GIC_DisableIRQ(interrupt);
459 #else
460     NVIC_DisableIRQ(interrupt);
461 #endif
462         return kStatus_Success;
463     }
464
465     /*!
466      * @brief Disable the global IRQ
467      *
468      * Disable the global interrupt and return the current primask register. User is required to provided the primask
469      * register for the EnableGlobalIRQ().
470      *
471      * @return Current primask value.
472      */
473     static inline uint32_t DisableGlobalIRQ(void)
474     {
475 #if defined(CPSR_I_Msk)
476         uint32_t cpsr = __get_CPSR() & CPSR_I_Msk;
477
478         __disable_irq();
479
480         return cpsr;
481 #else
482     uint32_t regPrimask = __get_PRIMASK();
483
484     __disable_irq();
485
486     return regPrimask;
487 #endif
488     }
489
490     /*!
491      * @brief Enaable the global IRQ
492      *
493      * Set the primask register with the provided primask value but not just enable the primask. The idea is for the
494      * convinience of integration of RTOS. some RTOS get its own management mechanism of primask. User is required to
495      * use the EnableGlobalIRQ() and DisableGlobalIRQ() in pair.
496      *
497      * @param primask value of primask register to be restored. The primask value is supposed to be provided by the
498      * DisableGlobalIRQ().
499      */
500     static inline void EnableGlobalIRQ(uint32_t primask)
501     {
502 #if defined(CPSR_I_Msk)
503         __set_CPSR((__get_CPSR() & ~CPSR_I_Msk) | primask);
504 #else
505     __set_PRIMASK(primask);
506 #endif
507     }
508
509 #if defined(ENABLE_RAM_VECTOR_TABLE)
510     /*!
511      * @brief install IRQ handler
512      *
513      * @param irq IRQ number
514      * @param irqHandler IRQ handler address
515      * @return The old IRQ handler address
516      */
517     uint32_t InstallIRQHandler(IRQn_Type irq, uint32_t irqHandler);
518 #endif /* ENABLE_RAM_VECTOR_TABLE. */
519
520 #if (defined(FSL_FEATURE_SOC_SYSCON_COUNT) && (FSL_FEATURE_SOC_SYSCON_COUNT > 0))
521     /*!
522      * @brief Enable specific interrupt for wake-up from deep-sleep mode.
523      *
524      * Enable the interrupt for wake-up from deep sleep mode.
525      * Some interrupts are typically used in sleep mode only and will not occur during
526      * deep-sleep mode because relevant clocks are stopped. However, it is possible to enable
527      * those clocks (significantly increasing power consumption in the reduced power mode),
528      * making these wake-ups possible.
529      *
530      * @note This function also enables the interrupt in the NVIC (EnableIRQ() is called internally).
531      *
532      * @param interrupt The IRQ number.
533      */
534     void EnableDeepSleepIRQ(IRQn_Type interrupt);
535
536     /*!
537      * @brief Disable specific interrupt for wake-up from deep-sleep mode.
538      *
539      * Disable the interrupt for wake-up from deep sleep mode.
540      * Some interrupts are typically used in sleep mode only and will not occur during
541      * deep-sleep mode because relevant clocks are stopped. However, it is possible to enable
542      * those clocks (significantly increasing power consumption in the reduced power mode),
543      * making these wake-ups possible.
544      *
545      * @note This function also disables the interrupt in the NVIC (DisableIRQ() is called internally).
546      *
547      * @param interrupt The IRQ number.
548      */
549     void DisableDeepSleepIRQ(IRQn_Type interrupt);
550 #endif /* FSL_FEATURE_SOC_SYSCON_COUNT */
551
552     /*!
553      * @brief Allocate memory with given alignment and aligned size.
554      *
555      * This is provided to support the dynamically allocated memory
556      * used in cache-able region.
557      * @param size The length required to malloc.
558      * @param alignbytes The alignment size.
559      * @retval The allocated memory.
560      */    
561     void *SDK_Malloc(size_t size, size_t alignbytes);
562     
563     /*!
564      * @brief Free memory.
565      *
566      * @param ptr The memory to be release.
567      */ 
568     void SDK_Free(void *ptr);    
569
570 #if defined(__cplusplus)
571 }
572 #endif
573
574 /*! @} */
575
576 #endif /* _FSL_COMMON_H_ */