fsl_common.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  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. /* Debug console type definition. */
  53. #define DEBUG_CONSOLE_DEVICE_TYPE_NONE 0U /*!< No debug console. */
  54. #define DEBUG_CONSOLE_DEVICE_TYPE_UART 1U /*!< Debug console base on UART. */
  55. #define DEBUG_CONSOLE_DEVICE_TYPE_LPUART 2U /*!< Debug console base on LPUART. */
  56. #define DEBUG_CONSOLE_DEVICE_TYPE_LPSCI 3U /*!< Debug console base on LPSCI. */
  57. #define DEBUG_CONSOLE_DEVICE_TYPE_USBCDC 4U /*!< Debug console base on USBCDC. */
  58. #define DEBUG_CONSOLE_DEVICE_TYPE_FLEXCOMM 5U /*!< Debug console base on USBCDC. */
  59. #define DEBUG_CONSOLE_DEVICE_TYPE_IUART 6U /*!< Debug console base on i.MX UART. */
  60. #define DEBUG_CONSOLE_DEVICE_TYPE_VUSART 7U /*!< Debug console base on LPC_USART. */
  61. /*! @brief Status group numbers. */
  62. enum _status_groups
  63. {
  64. kStatusGroup_Generic = 0, /*!< Group number for generic status codes. */
  65. kStatusGroup_FLASH = 1, /*!< Group number for FLASH status codes. */
  66. kStatusGroup_LPSPI = 4, /*!< Group number for LPSPI status codes. */
  67. kStatusGroup_FLEXIO_SPI = 5, /*!< Group number for FLEXIO SPI status codes. */
  68. kStatusGroup_DSPI = 6, /*!< Group number for DSPI status codes. */
  69. kStatusGroup_FLEXIO_UART = 7, /*!< Group number for FLEXIO UART status codes. */
  70. kStatusGroup_FLEXIO_I2C = 8, /*!< Group number for FLEXIO I2C status codes. */
  71. kStatusGroup_LPI2C = 9, /*!< Group number for LPI2C status codes. */
  72. kStatusGroup_UART = 10, /*!< Group number for UART status codes. */
  73. kStatusGroup_I2C = 11, /*!< Group number for UART status codes. */
  74. kStatusGroup_LPSCI = 12, /*!< Group number for LPSCI status codes. */
  75. kStatusGroup_LPUART = 13, /*!< Group number for LPUART status codes. */
  76. kStatusGroup_SPI = 14, /*!< Group number for SPI status code.*/
  77. kStatusGroup_XRDC = 15, /*!< Group number for XRDC status code.*/
  78. kStatusGroup_SEMA42 = 16, /*!< Group number for SEMA42 status code.*/
  79. kStatusGroup_SDHC = 17, /*!< Group number for SDHC status code */
  80. kStatusGroup_SDMMC = 18, /*!< Group number for SDMMC status code */
  81. kStatusGroup_SAI = 19, /*!< Group number for SAI status code */
  82. kStatusGroup_MCG = 20, /*!< Group number for MCG status codes. */
  83. kStatusGroup_SCG = 21, /*!< Group number for SCG status codes. */
  84. kStatusGroup_SDSPI = 22, /*!< Group number for SDSPI status codes. */
  85. kStatusGroup_FLEXIO_I2S = 23, /*!< Group number for FLEXIO I2S status codes */
  86. kStatusGroup_FLEXIO_MCULCD = 24, /*!< Group number for FLEXIO LCD status codes */
  87. kStatusGroup_FLASHIAP = 25, /*!< Group number for FLASHIAP status codes */
  88. kStatusGroup_FLEXCOMM_I2C = 26, /*!< Group number for FLEXCOMM I2C status codes */
  89. kStatusGroup_I2S = 27, /*!< Group number for I2S status codes */
  90. kStatusGroup_IUART = 28, /*!< Group number for IUART status codes */
  91. kStatusGroup_CSI = 29, /*!< Group number for CSI status codes */
  92. kStatusGroup_SDRAMC = 35, /*!< Group number for SDRAMC status codes. */
  93. kStatusGroup_POWER = 39, /*!< Group number for POWER status codes. */
  94. kStatusGroup_ENET = 40, /*!< Group number for ENET status codes. */
  95. kStatusGroup_PHY = 41, /*!< Group number for PHY status codes. */
  96. kStatusGroup_TRGMUX = 42, /*!< Group number for TRGMUX status codes. */
  97. kStatusGroup_SMARTCARD = 43, /*!< Group number for SMARTCARD status codes. */
  98. kStatusGroup_LMEM = 44, /*!< Group number for LMEM status codes. */
  99. kStatusGroup_QSPI = 45, /*!< Group number for QSPI status codes. */
  100. kStatusGroup_DMA = 50, /*!< Group number for DMA status codes. */
  101. kStatusGroup_EDMA = 51, /*!< Group number for EDMA status codes. */
  102. kStatusGroup_DMAMGR = 52, /*!< Group number for DMAMGR status codes. */
  103. kStatusGroup_FLEXCAN = 53, /*!< Group number for FlexCAN status codes. */
  104. kStatusGroup_LTC = 54, /*!< Group number for LTC status codes. */
  105. kStatusGroup_FLEXIO_CAMERA = 55, /*!< Group number for FLEXIO CAMERA status codes. */
  106. kStatusGroup_LPC_SPI = 56, /*!< Group number for LPC_SPI status codes. */
  107. kStatusGroup_LPC_USART = 57, /*!< Group number for LPC_USART status codes. */
  108. kStatusGroup_DMIC = 58, /*!< Group number for DMIC status codes. */
  109. kStatusGroup_SDIF = 59, /*!< Group number for SDIF status codes.*/
  110. kStatusGroup_SPIFI = 60, /*!< Group number for SPIFI status codes. */
  111. kStatusGroup_OTP = 61, /*!< Group number for OTP status codes. */
  112. kStatusGroup_MCAN = 62, /*!< Group number for MCAN status codes. */
  113. kStatusGroup_CAAM = 63, /*!< Group number for CAAM status codes. */
  114. kStatusGroup_ECSPI = 64, /*!< Group number for ECSPI status codes. */
  115. kStatusGroup_USDHC = 65, /*!< Group number for USDHC status codes.*/
  116. kStatusGroup_LPC_I2C = 66, /*!< Group number for LPC_I2C status codes.*/
  117. kStatusGroup_ESAI = 69, /*!< Group number for ESAI status codes. */
  118. kStatusGroup_FLEXSPI = 70, /*!< Group number for FLEXSPI status codes. */
  119. kStatusGroup_MMDC = 71, /*!< Group number for MMDC status codes. */
  120. kStatusGroup_MICFIL = 72, /*!< Group number for MIC status codes. */
  121. kStatusGroup_SDMA = 73, /*!< Group number for SDMA status codes. */
  122. kStatusGroup_NOTIFIER = 98, /*!< Group number for NOTIFIER status codes. */
  123. kStatusGroup_DebugConsole = 99, /*!< Group number for debug console status codes. */
  124. kStatusGroup_ApplicationRangeStart = 100, /*!< Starting number for application groups. */
  125. };
  126. /*! @brief Generic status return codes. */
  127. enum _generic_status
  128. {
  129. kStatus_Success = MAKE_STATUS(kStatusGroup_Generic, 0),
  130. kStatus_Fail = MAKE_STATUS(kStatusGroup_Generic, 1),
  131. kStatus_ReadOnly = MAKE_STATUS(kStatusGroup_Generic, 2),
  132. kStatus_OutOfRange = MAKE_STATUS(kStatusGroup_Generic, 3),
  133. kStatus_InvalidArgument = MAKE_STATUS(kStatusGroup_Generic, 4),
  134. kStatus_Timeout = MAKE_STATUS(kStatusGroup_Generic, 5),
  135. kStatus_NoTransferInProgress = MAKE_STATUS(kStatusGroup_Generic, 6),
  136. };
  137. /*! @brief Type used for all status and error return values. */
  138. typedef int32_t status_t;
  139. /*
  140. * The fsl_clock.h is included here because it needs MAKE_VERSION/MAKE_STATUS/status_t
  141. * defined in previous of this file.
  142. */
  143. #include "fsl_clock.h"
  144. /*
  145. * Chip level peripheral reset API, for MCUs that implement peripheral reset control external to a peripheral
  146. */
  147. #if ((defined(FSL_FEATURE_SOC_SYSCON_COUNT) && (FSL_FEATURE_SOC_SYSCON_COUNT > 0)) || \
  148. (defined(FSL_FEATURE_SOC_ASYNC_SYSCON_COUNT) && (FSL_FEATURE_SOC_ASYNC_SYSCON_COUNT > 0)))
  149. #include "fsl_reset.h"
  150. #endif
  151. /*! @name Min/max macros */
  152. /* @{ */
  153. #if !defined(MIN)
  154. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  155. #endif
  156. #if !defined(MAX)
  157. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  158. #endif
  159. /* @} */
  160. /*! @brief Computes the number of elements in an array. */
  161. #if !defined(ARRAY_SIZE)
  162. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  163. #endif
  164. /*! @name UINT16_MAX/UINT32_MAX value */
  165. /* @{ */
  166. #if !defined(UINT16_MAX)
  167. #define UINT16_MAX ((uint16_t)-1)
  168. #endif
  169. #if !defined(UINT32_MAX)
  170. #define UINT32_MAX ((uint32_t)-1)
  171. #endif
  172. /* @} */
  173. /*! @name Timer utilities */
  174. /* @{ */
  175. /*! Macro to convert a microsecond period to raw count value */
  176. #define USEC_TO_COUNT(us, clockFreqInHz) (uint64_t)((uint64_t)us * clockFreqInHz / 1000000U)
  177. /*! Macro to convert a raw count value to microsecond */
  178. #define COUNT_TO_USEC(count, clockFreqInHz) (uint64_t)((uint64_t)count * 1000000U / clockFreqInHz)
  179. /*! Macro to convert a millisecond period to raw count value */
  180. #define MSEC_TO_COUNT(ms, clockFreqInHz) (uint64_t)((uint64_t)ms * clockFreqInHz / 1000U)
  181. /*! Macro to convert a raw count value to millisecond */
  182. #define COUNT_TO_MSEC(count, clockFreqInHz) (uint64_t)((uint64_t)count * 1000U / clockFreqInHz)
  183. /* @} */
  184. /*! @name Alignment variable definition macros */
  185. /* @{ */
  186. #if (defined(__ICCARM__))
  187. /**
  188. * Workaround to disable MISRA C message suppress warnings for IAR compiler.
  189. * http://supp.iar.com/Support/?note=24725
  190. */
  191. _Pragma("diag_suppress=Pm120")
  192. #define SDK_PRAGMA(x) _Pragma(#x)
  193. _Pragma("diag_error=Pm120")
  194. /*! Macro to define a variable with alignbytes alignment */
  195. #define SDK_ALIGN(var, alignbytes) SDK_PRAGMA(data_alignment = alignbytes) var
  196. /*! Macro to define a variable with L1 d-cache line size alignment */
  197. #if defined(FSL_FEATURE_L1DCACHE_LINESIZE_BYTE)
  198. #define SDK_L1DCACHE_ALIGN(var) SDK_PRAGMA(data_alignment = FSL_FEATURE_L1DCACHE_LINESIZE_BYTE) var
  199. #endif
  200. /*! Macro to define a variable with L2 cache line size alignment */
  201. #if defined(FSL_FEATURE_L2CACHE_LINESIZE_BYTE)
  202. #define SDK_L2CACHE_ALIGN(var) SDK_PRAGMA(data_alignment = FSL_FEATURE_L2CACHE_LINESIZE_BYTE) var
  203. #endif
  204. #elif defined(__CC_ARM)
  205. /*! Macro to define a variable with alignbytes alignment */
  206. #define SDK_ALIGN(var, alignbytes) __align(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) __align(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) __align(FSL_FEATURE_L2CACHE_LINESIZE_BYTE) var
  214. #endif
  215. #elif defined(__GNUC__)
  216. /*! Macro to define a variable with alignbytes alignment */
  217. #define SDK_ALIGN(var, alignbytes) var __attribute__((aligned(alignbytes)))
  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) var __attribute__((aligned(FSL_FEATURE_L1DCACHE_LINESIZE_BYTE)))
  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) var __attribute__((aligned(FSL_FEATURE_L2CACHE_LINESIZE_BYTE)))
  225. #endif
  226. #else
  227. #error Toolchain not supported
  228. #define SDK_ALIGN(var, alignbytes) var
  229. #if defined(FSL_FEATURE_L1DCACHE_LINESIZE_BYTE)
  230. #define SDK_L1DCACHE_ALIGN(var) var
  231. #endif
  232. #if defined(FSL_FEATURE_L2CACHE_LINESIZE_BYTE)
  233. #define SDK_L2CACHE_ALIGN(var) var
  234. #endif
  235. #endif
  236. /*! Macro to change a value to a given size aligned value */
  237. #define SDK_SIZEALIGN(var, alignbytes) \
  238. ((unsigned int)((var) + ((alignbytes)-1)) & (unsigned int)(~(unsigned int)((alignbytes)-1)))
  239. /* @} */
  240. /*! Function to allocate/free L1 cache aligned memory using the malloc/free. */
  241. void *SDK_Malloc(size_t size, size_t alignbytes);
  242. void SDK_Free(void *ptr);
  243. /* @} */
  244. /*! @name Non-cacheable region definition macros */
  245. /* @{ */
  246. #if (defined(__ICCARM__))
  247. #if defined(FSL_FEATURE_L1ICACHE_LINESIZE_BYTE)
  248. #define AT_NONCACHEABLE_SECTION(var) var @"NonCacheable"
  249. #define AT_NONCACHEABLE_SECTION_ALIGN(var, alignbytes) SDK_PRAGMA(data_alignment = alignbytes) var @"NonCacheable"
  250. #else
  251. #define AT_NONCACHEABLE_SECTION(var) var
  252. #define AT_NONCACHEABLE_SECTION_ALIGN(var, alignbytes) SDK_PRAGMA(data_alignment = alignbytes) var
  253. #endif
  254. #elif(defined(__CC_ARM))
  255. #if defined(FSL_FEATURE_L1ICACHE_LINESIZE_BYTE)
  256. #define AT_NONCACHEABLE_SECTION(var) __attribute__((section("NonCacheable"), zero_init)) var
  257. #define AT_NONCACHEABLE_SECTION_ALIGN(var, alignbytes) \
  258. __attribute__((section("NonCacheable"), zero_init)) __align(alignbytes) var
  259. #else
  260. #define AT_NONCACHEABLE_SECTION(var) var
  261. #define AT_NONCACHEABLE_SECTION_ALIGN(var, alignbytes) __align(alignbytes) var
  262. #endif
  263. #elif(defined(__GNUC__))
  264. /* For GCC, when the non-cacheable section is required, please define "__STARTUP_INITIALIZE_NONCACHEDATA"
  265. * in your projects to make sure the non-cacheable section variables will be initialized in system startup.
  266. */
  267. #if defined(FSL_FEATURE_L1ICACHE_LINESIZE_BYTE)
  268. #define AT_NONCACHEABLE_SECTION(var) __attribute__((section("NonCacheable"))) var
  269. #define AT_NONCACHEABLE_SECTION_ALIGN(var, alignbytes) \
  270. __attribute__((section("NonCacheable"))) var __attribute__((aligned(alignbytes)))
  271. #else
  272. #define AT_NONCACHEABLE_SECTION(var) var
  273. #define AT_NONCACHEABLE_SECTION_ALIGN(var, alignbytes) var __attribute__((aligned(alignbytes)))
  274. #endif
  275. #else
  276. #error Toolchain not supported.
  277. #define AT_NONCACHEABLE_SECTION(var) var
  278. #define AT_NONCACHEABLE_SECTION_ALIGN(var, alignbytes) var
  279. #endif
  280. /* @} */
  281. /*******************************************************************************
  282. * API
  283. ******************************************************************************/
  284. #if defined(__cplusplus)
  285. extern "C"
  286. {
  287. #endif
  288. /*!
  289. * @brief Enable specific interrupt.
  290. *
  291. * Enable LEVEL1 interrupt. For some devices, there might be multiple interrupt
  292. * levels. For example, there are NVIC and intmux. Here the interrupts connected
  293. * to NVIC are the LEVEL1 interrupts, because they are routed to the core directly.
  294. * The interrupts connected to intmux are the LEVEL2 interrupts, they are routed
  295. * to NVIC first then routed to core.
  296. *
  297. * This function only enables the LEVEL1 interrupts. The number of LEVEL1 interrupts
  298. * is indicated by the feature macro FSL_FEATURE_NUMBER_OF_LEVEL1_INT_VECTORS.
  299. *
  300. * @param interrupt The IRQ number.
  301. * @retval kStatus_Success Interrupt enabled successfully
  302. * @retval kStatus_Fail Failed to enable the interrupt
  303. */
  304. static inline status_t EnableIRQ(IRQn_Type interrupt)
  305. {
  306. if (NotAvail_IRQn == interrupt)
  307. {
  308. return kStatus_Fail;
  309. }
  310. #if defined(FSL_FEATURE_NUMBER_OF_LEVEL1_INT_VECTORS) && (FSL_FEATURE_NUMBER_OF_LEVEL1_INT_VECTORS > 0)
  311. if (interrupt >= FSL_FEATURE_NUMBER_OF_LEVEL1_INT_VECTORS)
  312. {
  313. return kStatus_Fail;
  314. }
  315. #endif
  316. #if defined(__GIC_PRIO_BITS)
  317. extern void rt_hw_interrupt_umask(int vector);
  318. rt_hw_interrupt_umask(interrupt);
  319. #else
  320. NVIC_EnableIRQ(interrupt);
  321. #endif
  322. return kStatus_Success;
  323. }
  324. /*!
  325. * @brief Disable specific interrupt.
  326. *
  327. * Disable LEVEL1 interrupt. For some devices, there might be multiple interrupt
  328. * levels. For example, there are NVIC and intmux. Here the interrupts connected
  329. * to NVIC are the LEVEL1 interrupts, because they are routed to the core directly.
  330. * The interrupts connected to intmux are the LEVEL2 interrupts, they are routed
  331. * to NVIC first then routed to core.
  332. *
  333. * This function only disables the LEVEL1 interrupts. The number of LEVEL1 interrupts
  334. * is indicated by the feature macro FSL_FEATURE_NUMBER_OF_LEVEL1_INT_VECTORS.
  335. *
  336. * @param interrupt The IRQ number.
  337. * @retval kStatus_Success Interrupt disabled successfully
  338. * @retval kStatus_Fail Failed to disable the interrupt
  339. */
  340. static inline status_t DisableIRQ(IRQn_Type interrupt)
  341. {
  342. if (NotAvail_IRQn == interrupt)
  343. {
  344. return kStatus_Fail;
  345. }
  346. #if defined(FSL_FEATURE_NUMBER_OF_LEVEL1_INT_VECTORS) && (FSL_FEATURE_NUMBER_OF_LEVEL1_INT_VECTORS > 0)
  347. if (interrupt >= FSL_FEATURE_NUMBER_OF_LEVEL1_INT_VECTORS)
  348. {
  349. return kStatus_Fail;
  350. }
  351. #endif
  352. #if defined(__GIC_PRIO_BITS)
  353. extern void rt_hw_interrupt_mask(int vector);
  354. rt_hw_interrupt_mask(interrupt);
  355. #else
  356. NVIC_DisableIRQ(interrupt);
  357. #endif
  358. return kStatus_Success;
  359. }
  360. /*!
  361. * @brief Disable the global IRQ
  362. *
  363. * Disable the global interrupt and return the current primask register. User is required to provided the primask
  364. * register for the EnableGlobalIRQ().
  365. *
  366. * @return Current primask value.
  367. */
  368. static inline uint32_t DisableGlobalIRQ(void)
  369. {
  370. #if defined(CPSR_I_Msk)
  371. uint32_t cpsr = __get_CPSR() & CPSR_I_Msk;
  372. __disable_irq();
  373. return cpsr;
  374. #else
  375. uint32_t regPrimask = __get_PRIMASK();
  376. __disable_irq();
  377. return regPrimask;
  378. #endif
  379. }
  380. /*!
  381. * @brief Enaable the global IRQ
  382. *
  383. * Set the primask register with the provided primask value but not just enable the primask. The idea is for the
  384. * convinience of integration of RTOS. some RTOS get its own management mechanism of primask. User is required to
  385. * use the EnableGlobalIRQ() and DisableGlobalIRQ() in pair.
  386. *
  387. * @param primask value of primask register to be restored. The primask value is supposed to be provided by the
  388. * DisableGlobalIRQ().
  389. */
  390. static inline void EnableGlobalIRQ(uint32_t primask)
  391. {
  392. #if defined(CPSR_I_Msk)
  393. __set_CPSR((__get_CPSR() & ~CPSR_I_Msk) | primask);
  394. #else
  395. __set_PRIMASK(primask);
  396. #endif
  397. }
  398. /*!
  399. * @brief install IRQ handler
  400. *
  401. * @param irq IRQ number
  402. * @param irqHandler IRQ handler address
  403. * @return The old IRQ handler address
  404. */
  405. uint32_t InstallIRQHandler(IRQn_Type irq, uint32_t irqHandler);
  406. #if (defined(FSL_FEATURE_SOC_SYSCON_COUNT) && (FSL_FEATURE_SOC_SYSCON_COUNT > 0))
  407. /*!
  408. * @brief Enable specific interrupt for wake-up from deep-sleep mode.
  409. *
  410. * Enable the interrupt for wake-up from deep sleep mode.
  411. * Some interrupts are typically used in sleep mode only and will not occur during
  412. * deep-sleep mode because relevant clocks are stopped. However, it is possible to enable
  413. * those clocks (significantly increasing power consumption in the reduced power mode),
  414. * making these wake-ups possible.
  415. *
  416. * @note This function also enables the interrupt in the NVIC (EnableIRQ() is called internally).
  417. *
  418. * @param interrupt The IRQ number.
  419. */
  420. void EnableDeepSleepIRQ(IRQn_Type interrupt);
  421. /*!
  422. * @brief Disable specific interrupt for wake-up from deep-sleep mode.
  423. *
  424. * Disable the interrupt for wake-up from deep sleep mode.
  425. * Some interrupts are typically used in sleep mode only and will not occur during
  426. * deep-sleep mode because relevant clocks are stopped. However, it is possible to enable
  427. * those clocks (significantly increasing power consumption in the reduced power mode),
  428. * making these wake-ups possible.
  429. *
  430. * @note This function also disables the interrupt in the NVIC (DisableIRQ() is called internally).
  431. *
  432. * @param interrupt The IRQ number.
  433. */
  434. void DisableDeepSleepIRQ(IRQn_Type interrupt);
  435. #endif /* FSL_FEATURE_SOC_SYSCON_COUNT */
  436. #if defined(__cplusplus)
  437. }
  438. #endif
  439. /*! @} */
  440. #endif /* _FSL_COMMON_H_ */