fsl_common.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. #if defined(__ICCARM__)
  37. #include <stddef.h>
  38. #endif
  39. #include "fsl_device_registers.h"
  40. /*!
  41. * @addtogroup ksdk_common
  42. * @{
  43. */
  44. /*******************************************************************************
  45. * Definitions
  46. ******************************************************************************/
  47. /*! @brief Construct a status code value from a group and code number. */
  48. #define MAKE_STATUS(group, code) ((((group)*100) + (code)))
  49. /*! @brief Construct the version number for drivers. */
  50. #define MAKE_VERSION(major, minor, bugfix) (((major) << 16) | ((minor) << 8) | (bugfix))
  51. /* Debug console type definition. */
  52. #define DEBUG_CONSOLE_DEVICE_TYPE_NONE 0U /*!< No debug console. */
  53. #define DEBUG_CONSOLE_DEVICE_TYPE_UART 1U /*!< Debug console base on UART. */
  54. #define DEBUG_CONSOLE_DEVICE_TYPE_LPUART 2U /*!< Debug console base on LPUART. */
  55. #define DEBUG_CONSOLE_DEVICE_TYPE_LPSCI 3U /*!< Debug console base on LPSCI. */
  56. #define DEBUG_CONSOLE_DEVICE_TYPE_USBCDC 4U /*!< Debug console base on USBCDC. */
  57. #define DEBUG_CONSOLE_DEVICE_TYPE_FLEXCOMM 5U /*!< Debug console base on USBCDC. */
  58. #define DEBUG_CONSOLE_DEVICE_TYPE_IUART 6U /*!< Debug console base on i.MX UART. */
  59. /*! @brief Status group numbers. */
  60. enum _status_groups
  61. {
  62. kStatusGroup_Generic = 0, /*!< Group number for generic status codes. */
  63. kStatusGroup_FLASH = 1, /*!< Group number for FLASH status codes. */
  64. kStatusGroup_LPSPI = 4, /*!< Group number for LPSPI status codes. */
  65. kStatusGroup_FLEXIO_SPI = 5, /*!< Group number for FLEXIO SPI status codes. */
  66. kStatusGroup_DSPI = 6, /*!< Group number for DSPI status codes. */
  67. kStatusGroup_FLEXIO_UART = 7, /*!< Group number for FLEXIO UART status codes. */
  68. kStatusGroup_FLEXIO_I2C = 8, /*!< Group number for FLEXIO I2C status codes. */
  69. kStatusGroup_LPI2C = 9, /*!< Group number for LPI2C status codes. */
  70. kStatusGroup_UART = 10, /*!< Group number for UART status codes. */
  71. kStatusGroup_I2C = 11, /*!< Group number for UART status codes. */
  72. kStatusGroup_LPSCI = 12, /*!< Group number for LPSCI status codes. */
  73. kStatusGroup_LPUART = 13, /*!< Group number for LPUART status codes. */
  74. kStatusGroup_SPI = 14, /*!< Group number for SPI status code.*/
  75. kStatusGroup_XRDC = 15, /*!< Group number for XRDC status code.*/
  76. kStatusGroup_SEMA42 = 16, /*!< Group number for SEMA42 status code.*/
  77. kStatusGroup_SDHC = 17, /*!< Group number for SDHC status code */
  78. kStatusGroup_SDMMC = 18, /*!< Group number for SDMMC status code */
  79. kStatusGroup_SAI = 19, /*!< Group number for SAI status code */
  80. kStatusGroup_MCG = 20, /*!< Group number for MCG status codes. */
  81. kStatusGroup_SCG = 21, /*!< Group number for SCG status codes. */
  82. kStatusGroup_SDSPI = 22, /*!< Group number for SDSPI status codes. */
  83. kStatusGroup_FLEXIO_I2S = 23, /*!< Group number for FLEXIO I2S status codes */
  84. kStatusGroup_FLEXIO_MCULCD = 24, /*!< Group number for FLEXIO LCD status codes */
  85. kStatusGroup_FLASHIAP = 25, /*!< Group number for FLASHIAP status codes */
  86. kStatusGroup_FLEXCOMM_I2C = 26, /*!< Group number for FLEXCOMM I2C status codes */
  87. kStatusGroup_I2S = 27, /*!< Group number for I2S status codes */
  88. kStatusGroup_IUART = 28, /*!< Group number for IUART status codes */
  89. kStatusGroup_SDRAMC = 35, /*!< Group number for SDRAMC status codes. */
  90. kStatusGroup_POWER = 39, /*!< Group number for POWER status codes. */
  91. kStatusGroup_ENET = 40, /*!< Group number for ENET status codes. */
  92. kStatusGroup_PHY = 41, /*!< Group number for PHY status codes. */
  93. kStatusGroup_TRGMUX = 42, /*!< Group number for TRGMUX status codes. */
  94. kStatusGroup_SMARTCARD = 43, /*!< Group number for SMARTCARD status codes. */
  95. kStatusGroup_LMEM = 44, /*!< Group number for LMEM status codes. */
  96. kStatusGroup_QSPI = 45, /*!< Group number for QSPI status codes. */
  97. kStatusGroup_DMA = 50, /*!< Group number for DMA status codes. */
  98. kStatusGroup_EDMA = 51, /*!< Group number for EDMA status codes. */
  99. kStatusGroup_DMAMGR = 52, /*!< Group number for DMAMGR status codes. */
  100. kStatusGroup_FLEXCAN = 53, /*!< Group number for FlexCAN status codes. */
  101. kStatusGroup_LTC = 54, /*!< Group number for LTC status codes. */
  102. kStatusGroup_FLEXIO_CAMERA = 55, /*!< Group number for FLEXIO CAMERA status codes. */
  103. kStatusGroup_LPC_SPI = 56, /*!< Group number for LPC_SPI status codes. */
  104. kStatusGroup_LPC_USART = 57, /*!< Group number for LPC_USART status codes. */
  105. kStatusGroup_DMIC = 58, /*!< Group number for DMIC status codes. */
  106. kStatusGroup_SDIF = 59, /*!< Group number for SDIF status codes.*/
  107. kStatusGroup_SPIFI = 60, /*!< Group number for SPIFI status codes. */
  108. kStatusGroup_OTP = 61, /*!< Group number for OTP status codes. */
  109. kStatusGroup_MCAN = 62, /*!< Group number for MCAN status codes. */
  110. kStatusGroup_CAAM = 63, /*!< Group number for CAAM status codes. */
  111. kStatusGroup_ECSPI = 64, /*!< Group number for ECSPI status codes. */
  112. kStatusGroup_USDHC = 65, /*!< Group number for USDHC status codes.*/
  113. kStatusGroup_ESAI = 69, /*!< Group number for ESAI status codes. */
  114. kStatusGroup_FLEXSPI = 70, /*!< Group number for FLEXSPI status codes. */
  115. kStatusGroup_NOTIFIER = 98, /*!< Group number for NOTIFIER status codes. */
  116. kStatusGroup_DebugConsole = 99, /*!< Group number for debug console status codes. */
  117. kStatusGroup_ApplicationRangeStart = 100, /*!< Starting number for application groups. */
  118. };
  119. /*! @brief Generic status return codes. */
  120. enum _generic_status
  121. {
  122. kStatus_Success = MAKE_STATUS(kStatusGroup_Generic, 0),
  123. kStatus_Fail = MAKE_STATUS(kStatusGroup_Generic, 1),
  124. kStatus_ReadOnly = MAKE_STATUS(kStatusGroup_Generic, 2),
  125. kStatus_OutOfRange = MAKE_STATUS(kStatusGroup_Generic, 3),
  126. kStatus_InvalidArgument = MAKE_STATUS(kStatusGroup_Generic, 4),
  127. kStatus_Timeout = MAKE_STATUS(kStatusGroup_Generic, 5),
  128. kStatus_NoTransferInProgress = MAKE_STATUS(kStatusGroup_Generic, 6),
  129. };
  130. /*! @brief Type used for all status and error return values. */
  131. typedef int32_t status_t;
  132. /*
  133. * The fsl_clock.h is included here because it needs MAKE_VERSION/MAKE_STATUS/status_t
  134. * defined in previous of this file.
  135. */
  136. #include "fsl_clock.h"
  137. /*
  138. * Chip level peripheral reset API, for MCUs that implement peripheral reset control external to a peripheral
  139. */
  140. #if ((defined(FSL_FEATURE_SOC_SYSCON_COUNT) && (FSL_FEATURE_SOC_SYSCON_COUNT > 0)) || \
  141. (defined(FSL_FEATURE_SOC_ASYNC_SYSCON_COUNT) && (FSL_FEATURE_SOC_ASYNC_SYSCON_COUNT > 0)))
  142. #include "fsl_reset.h"
  143. #endif
  144. /*! @name Min/max macros */
  145. /* @{ */
  146. #if !defined(MIN)
  147. #define MIN(a, b) ((a) < (b) ? (a) : (b))
  148. #endif
  149. #if !defined(MAX)
  150. #define MAX(a, b) ((a) > (b) ? (a) : (b))
  151. #endif
  152. /* @} */
  153. /*! @brief Computes the number of elements in an array. */
  154. #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
  155. /*! @name UINT16_MAX/UINT32_MAX value */
  156. /* @{ */
  157. #if !defined(UINT16_MAX)
  158. #define UINT16_MAX ((uint16_t)-1)
  159. #endif
  160. #if !defined(UINT32_MAX)
  161. #define UINT32_MAX ((uint32_t)-1)
  162. #endif
  163. /* @} */
  164. /*! @name Timer utilities */
  165. /* @{ */
  166. /*! Macro to convert a microsecond period to raw count value */
  167. #define USEC_TO_COUNT(us, clockFreqInHz) (uint64_t)((uint64_t)us * clockFreqInHz / 1000000U)
  168. /*! Macro to convert a raw count value to microsecond */
  169. #define COUNT_TO_USEC(count, clockFreqInHz) (uint64_t)((uint64_t)count * 1000000U / clockFreqInHz)
  170. /*! Macro to convert a millisecond period to raw count value */
  171. #define MSEC_TO_COUNT(ms, clockFreqInHz) (uint64_t)((uint64_t)ms * clockFreqInHz / 1000U)
  172. /*! Macro to convert a raw count value to millisecond */
  173. #define COUNT_TO_MSEC(count, clockFreqInHz) (uint64_t)((uint64_t)count * 1000U / clockFreqInHz)
  174. /* @} */
  175. /*******************************************************************************
  176. * API
  177. ******************************************************************************/
  178. #if defined(__cplusplus)
  179. extern "C" {
  180. #endif
  181. /*!
  182. * @brief Enable specific interrupt.
  183. *
  184. * Enable the interrupt not routed from intmux.
  185. *
  186. * @param interrupt The IRQ number.
  187. */
  188. static inline void EnableIRQ(IRQn_Type interrupt)
  189. {
  190. if (NotAvail_IRQn == interrupt)
  191. {
  192. return;
  193. }
  194. #if defined(FSL_FEATURE_SOC_INTMUX_COUNT) && (FSL_FEATURE_SOC_INTMUX_COUNT > 0)
  195. if (interrupt < FSL_FEATURE_INTMUX_IRQ_START_INDEX)
  196. #endif
  197. {
  198. #if defined(__GIC_PRIO_BITS)
  199. GIC_EnableIRQ(interrupt);
  200. #else
  201. NVIC_EnableIRQ(interrupt);
  202. #endif
  203. }
  204. }
  205. /*!
  206. * @brief Disable specific interrupt.
  207. *
  208. * Disable the interrupt not routed from intmux.
  209. *
  210. * @param interrupt The IRQ number.
  211. */
  212. static inline void DisableIRQ(IRQn_Type interrupt)
  213. {
  214. if (NotAvail_IRQn == interrupt)
  215. {
  216. return;
  217. }
  218. #if defined(FSL_FEATURE_SOC_INTMUX_COUNT) && (FSL_FEATURE_SOC_INTMUX_COUNT > 0)
  219. if (interrupt < FSL_FEATURE_INTMUX_IRQ_START_INDEX)
  220. #endif
  221. {
  222. #if defined(__GIC_PRIO_BITS)
  223. GIC_DisableIRQ(interrupt);
  224. #else
  225. NVIC_DisableIRQ(interrupt);
  226. #endif
  227. }
  228. }
  229. /*!
  230. * @brief Disable the global IRQ
  231. *
  232. * Disable the global interrupt and return the current primask register. User is required to provided the primask
  233. * register for the EnableGlobalIRQ().
  234. *
  235. * @return Current primask value.
  236. */
  237. static inline uint32_t DisableGlobalIRQ(void)
  238. {
  239. #if defined(CPSR_I_Msk)
  240. uint32_t cpsr = __get_CPSR() & CPSR_I_Msk;
  241. __disable_irq();
  242. return cpsr;
  243. #else
  244. uint32_t regPrimask = __get_PRIMASK();
  245. __disable_irq();
  246. return regPrimask;
  247. #endif
  248. }
  249. /*!
  250. * @brief Enaable the global IRQ
  251. *
  252. * Set the primask register with the provided primask value but not just enable the primask. The idea is for the
  253. * convinience of integration of RTOS. some RTOS get its own management mechanism of primask. User is required to
  254. * use the EnableGlobalIRQ() and DisableGlobalIRQ() in pair.
  255. *
  256. * @param primask value of primask register to be restored. The primask value is supposed to be provided by the
  257. * DisableGlobalIRQ().
  258. */
  259. static inline void EnableGlobalIRQ(uint32_t primask)
  260. {
  261. #if defined(CPSR_I_Msk)
  262. __set_CPSR((__get_CPSR() & ~CPSR_I_Msk) | primask);
  263. #else
  264. __set_PRIMASK(primask);
  265. #endif
  266. }
  267. /*!
  268. * @brief install IRQ handler
  269. *
  270. * @param irq IRQ number
  271. * @param irqHandler IRQ handler address
  272. * @return The old IRQ handler address
  273. */
  274. uint32_t InstallIRQHandler(IRQn_Type irq, uint32_t irqHandler);
  275. #if (defined(FSL_FEATURE_SOC_SYSCON_COUNT) && (FSL_FEATURE_SOC_SYSCON_COUNT > 0))
  276. /*!
  277. * @brief Enable specific interrupt for wake-up from deep-sleep mode.
  278. *
  279. * Enable the interrupt for wake-up from deep sleep mode.
  280. * Some interrupts are typically used in sleep mode only and will not occur during
  281. * deep-sleep mode because relevant clocks are stopped. However, it is possible to enable
  282. * those clocks (significantly increasing power consumption in the reduced power mode),
  283. * making these wake-ups possible.
  284. *
  285. * @note This function also enables the interrupt in the NVIC (EnableIRQ() is called internally).
  286. *
  287. * @param interrupt The IRQ number.
  288. */
  289. void EnableDeepSleepIRQ(IRQn_Type interrupt);
  290. /*!
  291. * @brief Disable specific interrupt for wake-up from deep-sleep mode.
  292. *
  293. * Disable the interrupt for wake-up from deep sleep mode.
  294. * Some interrupts are typically used in sleep mode only and will not occur during
  295. * deep-sleep mode because relevant clocks are stopped. However, it is possible to enable
  296. * those clocks (significantly increasing power consumption in the reduced power mode),
  297. * making these wake-ups possible.
  298. *
  299. * @note This function also disables the interrupt in the NVIC (DisableIRQ() is called internally).
  300. *
  301. * @param interrupt The IRQ number.
  302. */
  303. void DisableDeepSleepIRQ(IRQn_Type interrupt);
  304. #endif /* FSL_FEATURE_SOC_SYSCON_COUNT */
  305. #if defined(__cplusplus)
  306. }
  307. #endif
  308. /*! @} */
  309. #endif /* _FSL_COMMON_H_ */