fsl_common.h 26 KB

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