25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

673 lines
31 KiB

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