fsl_common.h 27 KB

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