fsl_common.h 23 KB

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