1 /* 2 * Copyright (c) 2015-2016, Freescale Semiconductor, Inc. 3 * Copyright 2016-2022 NXP 4 * All rights reserved. 5 * 6 * SPDX-License-Identifier: BSD-3-Clause 7 */ 8 9 #ifndef FSL_COMMON_H_ 10 #define FSL_COMMON_H_ 11 12 #include <assert.h> 13 #include <stdbool.h> 14 #include <stdint.h> 15 #include <string.h> 16 #include <stdlib.h> 17 18 #if defined(__ICCARM__) || (defined(__CC_ARM) || defined(__ARMCC_VERSION)) || defined(__GNUC__) 19 #include <stddef.h> 20 #endif 21 22 #include "fsl_device_registers.h" 23 24 /*! 25 * @addtogroup ksdk_common 26 * @{ 27 */ 28 29 /******************************************************************************* 30 * Configurations 31 ******************************************************************************/ 32 33 /*! @brief Macro to use the default weak IRQ handler in drivers. */ 34 #ifndef FSL_DRIVER_TRANSFER_DOUBLE_WEAK_IRQ 35 #define FSL_DRIVER_TRANSFER_DOUBLE_WEAK_IRQ 1 36 #endif 37 38 /******************************************************************************* 39 * Definitions 40 ******************************************************************************/ 41 42 /*! @brief Construct a status code value from a group and code number. */ 43 #define MAKE_STATUS(group, code) ((((group)*100L) + (code))) 44 45 /*! @brief Construct the version number for drivers. 46 * 47 * The driver version is a 32-bit number, for both 32-bit platforms(such as Cortex M) 48 * and 16-bit platforms(such as DSC). 49 * 50 * @verbatim 51 52 | Unused || Major Version || Minor Version || Bug Fix | 53 31 25 24 17 16 9 8 0 54 55 @endverbatim 56 */ 57 #define MAKE_VERSION(major, minor, bugfix) (((major)*65536L) + ((minor)*256L) + (bugfix)) 58 59 /*! @name Driver version */ 60 /*! @{ */ 61 /*! @brief common driver version. */ 62 #define FSL_COMMON_DRIVER_VERSION (MAKE_VERSION(2, 4, 0)) 63 /*! @} */ 64 65 /*! @name Debug console type definition. */ 66 /*! @{ */ 67 #define DEBUG_CONSOLE_DEVICE_TYPE_NONE 0U /*!< No debug console. */ 68 #define DEBUG_CONSOLE_DEVICE_TYPE_UART 1U /*!< Debug console based on UART. */ 69 #define DEBUG_CONSOLE_DEVICE_TYPE_LPUART 2U /*!< Debug console based on LPUART. */ 70 #define DEBUG_CONSOLE_DEVICE_TYPE_LPSCI 3U /*!< Debug console based on LPSCI. */ 71 #define DEBUG_CONSOLE_DEVICE_TYPE_USBCDC 4U /*!< Debug console based on USBCDC. */ 72 #define DEBUG_CONSOLE_DEVICE_TYPE_FLEXCOMM 5U /*!< Debug console based on FLEXCOMM. */ 73 #define DEBUG_CONSOLE_DEVICE_TYPE_IUART 6U /*!< Debug console based on i.MX UART. */ 74 #define DEBUG_CONSOLE_DEVICE_TYPE_VUSART 7U /*!< Debug console based on LPC_VUSART. */ 75 #define DEBUG_CONSOLE_DEVICE_TYPE_MINI_USART 8U /*!< Debug console based on LPC_USART. */ 76 #define DEBUG_CONSOLE_DEVICE_TYPE_SWO 9U /*!< Debug console based on SWO. */ 77 #define DEBUG_CONSOLE_DEVICE_TYPE_QSCI 10U /*!< Debug console based on QSCI. */ 78 /*! @} */ 79 80 /*! @brief Status group numbers. */ 81 enum _status_groups 82 { 83 kStatusGroup_Generic = 0, /*!< Group number for generic status codes. */ 84 kStatusGroup_FLASH = 1, /*!< Group number for FLASH status codes. */ 85 kStatusGroup_LPSPI = 4, /*!< Group number for LPSPI status codes. */ 86 kStatusGroup_FLEXIO_SPI = 5, /*!< Group number for FLEXIO SPI status codes. */ 87 kStatusGroup_DSPI = 6, /*!< Group number for DSPI status codes. */ 88 kStatusGroup_FLEXIO_UART = 7, /*!< Group number for FLEXIO UART status codes. */ 89 kStatusGroup_FLEXIO_I2C = 8, /*!< Group number for FLEXIO I2C status codes. */ 90 kStatusGroup_LPI2C = 9, /*!< Group number for LPI2C status codes. */ 91 kStatusGroup_UART = 10, /*!< Group number for UART status codes. */ 92 kStatusGroup_I2C = 11, /*!< Group number for UART status codes. */ 93 kStatusGroup_LPSCI = 12, /*!< Group number for LPSCI status codes. */ 94 kStatusGroup_LPUART = 13, /*!< Group number for LPUART status codes. */ 95 kStatusGroup_SPI = 14, /*!< Group number for SPI status code.*/ 96 kStatusGroup_XRDC = 15, /*!< Group number for XRDC status code.*/ 97 kStatusGroup_SEMA42 = 16, /*!< Group number for SEMA42 status code.*/ 98 kStatusGroup_SDHC = 17, /*!< Group number for SDHC status code */ 99 kStatusGroup_SDMMC = 18, /*!< Group number for SDMMC status code */ 100 kStatusGroup_SAI = 19, /*!< Group number for SAI status code */ 101 kStatusGroup_MCG = 20, /*!< Group number for MCG status codes. */ 102 kStatusGroup_SCG = 21, /*!< Group number for SCG status codes. */ 103 kStatusGroup_SDSPI = 22, /*!< Group number for SDSPI status codes. */ 104 kStatusGroup_FLEXIO_I2S = 23, /*!< Group number for FLEXIO I2S status codes */ 105 kStatusGroup_FLEXIO_MCULCD = 24, /*!< Group number for FLEXIO LCD status codes */ 106 kStatusGroup_FLASHIAP = 25, /*!< Group number for FLASHIAP status codes */ 107 kStatusGroup_FLEXCOMM_I2C = 26, /*!< Group number for FLEXCOMM I2C status codes */ 108 kStatusGroup_I2S = 27, /*!< Group number for I2S status codes */ 109 kStatusGroup_IUART = 28, /*!< Group number for IUART status codes */ 110 kStatusGroup_CSI = 29, /*!< Group number for CSI status codes */ 111 kStatusGroup_MIPI_DSI = 30, /*!< Group number for MIPI DSI status codes */ 112 kStatusGroup_SDRAMC = 35, /*!< Group number for SDRAMC status codes. */ 113 kStatusGroup_POWER = 39, /*!< Group number for POWER status codes. */ 114 kStatusGroup_ENET = 40, /*!< Group number for ENET status codes. */ 115 kStatusGroup_PHY = 41, /*!< Group number for PHY status codes. */ 116 kStatusGroup_TRGMUX = 42, /*!< Group number for TRGMUX status codes. */ 117 kStatusGroup_SMARTCARD = 43, /*!< Group number for SMARTCARD status codes. */ 118 kStatusGroup_LMEM = 44, /*!< Group number for LMEM status codes. */ 119 kStatusGroup_QSPI = 45, /*!< Group number for QSPI status codes. */ 120 kStatusGroup_DMA = 50, /*!< Group number for DMA status codes. */ 121 kStatusGroup_EDMA = 51, /*!< Group number for EDMA status codes. */ 122 kStatusGroup_DMAMGR = 52, /*!< Group number for DMAMGR status codes. */ 123 kStatusGroup_FLEXCAN = 53, /*!< Group number for FlexCAN status codes. */ 124 kStatusGroup_LTC = 54, /*!< Group number for LTC status codes. */ 125 kStatusGroup_FLEXIO_CAMERA = 55, /*!< Group number for FLEXIO CAMERA status codes. */ 126 kStatusGroup_LPC_SPI = 56, /*!< Group number for LPC_SPI status codes. */ 127 kStatusGroup_LPC_USART = 57, /*!< Group number for LPC_USART status codes. */ 128 kStatusGroup_DMIC = 58, /*!< Group number for DMIC status codes. */ 129 kStatusGroup_SDIF = 59, /*!< Group number for SDIF status codes.*/ 130 kStatusGroup_SPIFI = 60, /*!< Group number for SPIFI status codes. */ 131 kStatusGroup_OTP = 61, /*!< Group number for OTP status codes. */ 132 kStatusGroup_MCAN = 62, /*!< Group number for MCAN status codes. */ 133 kStatusGroup_CAAM = 63, /*!< Group number for CAAM status codes. */ 134 kStatusGroup_ECSPI = 64, /*!< Group number for ECSPI status codes. */ 135 kStatusGroup_USDHC = 65, /*!< Group number for USDHC status codes.*/ 136 kStatusGroup_LPC_I2C = 66, /*!< Group number for LPC_I2C status codes.*/ 137 kStatusGroup_DCP = 67, /*!< Group number for DCP status codes.*/ 138 kStatusGroup_MSCAN = 68, /*!< Group number for MSCAN status codes.*/ 139 kStatusGroup_ESAI = 69, /*!< Group number for ESAI status codes. */ 140 kStatusGroup_FLEXSPI = 70, /*!< Group number for FLEXSPI status codes. */ 141 kStatusGroup_MMDC = 71, /*!< Group number for MMDC status codes. */ 142 kStatusGroup_PDM = 72, /*!< Group number for MIC status codes. */ 143 kStatusGroup_SDMA = 73, /*!< Group number for SDMA status codes. */ 144 kStatusGroup_ICS = 74, /*!< Group number for ICS status codes. */ 145 kStatusGroup_SPDIF = 75, /*!< Group number for SPDIF status codes. */ 146 kStatusGroup_LPC_MINISPI = 76, /*!< Group number for LPC_MINISPI status codes. */ 147 kStatusGroup_HASHCRYPT = 77, /*!< Group number for Hashcrypt status codes */ 148 kStatusGroup_LPC_SPI_SSP = 78, /*!< Group number for LPC_SPI_SSP status codes. */ 149 kStatusGroup_I3C = 79, /*!< Group number for I3C status codes */ 150 kStatusGroup_LPC_I2C_1 = 97, /*!< Group number for LPC_I2C_1 status codes. */ 151 kStatusGroup_NOTIFIER = 98, /*!< Group number for NOTIFIER status codes. */ 152 kStatusGroup_DebugConsole = 99, /*!< Group number for debug console status codes. */ 153 kStatusGroup_SEMC = 100, /*!< Group number for SEMC status codes. */ 154 kStatusGroup_ApplicationRangeStart = 101, /*!< Starting number for application groups. */ 155 kStatusGroup_IAP = 102, /*!< Group number for IAP status codes */ 156 kStatusGroup_SFA = 103, /*!< Group number for SFA status codes*/ 157 kStatusGroup_SPC = 104, /*!< Group number for SPC status codes. */ 158 kStatusGroup_PUF = 105, /*!< Group number for PUF status codes. */ 159 kStatusGroup_TOUCH_PANEL = 106, /*!< Group number for touch panel status codes */ 160 kStatusGroup_VBAT = 107, /*!< Group number for VBAT status codes */ 161 162 kStatusGroup_HAL_GPIO = 121, /*!< Group number for HAL GPIO status codes. */ 163 kStatusGroup_HAL_UART = 122, /*!< Group number for HAL UART status codes. */ 164 kStatusGroup_HAL_TIMER = 123, /*!< Group number for HAL TIMER status codes. */ 165 kStatusGroup_HAL_SPI = 124, /*!< Group number for HAL SPI status codes. */ 166 kStatusGroup_HAL_I2C = 125, /*!< Group number for HAL I2C status codes. */ 167 kStatusGroup_HAL_FLASH = 126, /*!< Group number for HAL FLASH status codes. */ 168 kStatusGroup_HAL_PWM = 127, /*!< Group number for HAL PWM status codes. */ 169 kStatusGroup_HAL_RNG = 128, /*!< Group number for HAL RNG status codes. */ 170 kStatusGroup_HAL_I2S = 129, /*!< Group number for HAL I2S status codes. */ 171 kStatusGroup_HAL_ADC_SENSOR = 130, /*!< Group number for HAL ADC SENSOR status codes. */ 172 kStatusGroup_TIMERMANAGER = 135, /*!< Group number for TiMER MANAGER status codes. */ 173 kStatusGroup_SERIALMANAGER = 136, /*!< Group number for SERIAL MANAGER status codes. */ 174 kStatusGroup_LED = 137, /*!< Group number for LED status codes. */ 175 kStatusGroup_BUTTON = 138, /*!< Group number for BUTTON status codes. */ 176 kStatusGroup_EXTERN_EEPROM = 139, /*!< Group number for EXTERN EEPROM status codes. */ 177 kStatusGroup_SHELL = 140, /*!< Group number for SHELL status codes. */ 178 kStatusGroup_MEM_MANAGER = 141, /*!< Group number for MEM MANAGER status codes. */ 179 kStatusGroup_LIST = 142, /*!< Group number for List status codes. */ 180 kStatusGroup_OSA = 143, /*!< Group number for OSA status codes. */ 181 kStatusGroup_COMMON_TASK = 144, /*!< Group number for Common task status codes. */ 182 kStatusGroup_MSG = 145, /*!< Group number for messaging status codes. */ 183 kStatusGroup_SDK_OCOTP = 146, /*!< Group number for OCOTP status codes. */ 184 kStatusGroup_SDK_FLEXSPINOR = 147, /*!< Group number for FLEXSPINOR status codes.*/ 185 kStatusGroup_CODEC = 148, /*!< Group number for codec status codes. */ 186 kStatusGroup_ASRC = 149, /*!< Group number for codec status ASRC. */ 187 kStatusGroup_OTFAD = 150, /*!< Group number for codec status codes. */ 188 kStatusGroup_SDIOSLV = 151, /*!< Group number for SDIOSLV status codes. */ 189 kStatusGroup_MECC = 152, /*!< Group number for MECC status codes. */ 190 kStatusGroup_ENET_QOS = 153, /*!< Group number for ENET_QOS status codes. */ 191 kStatusGroup_LOG = 154, /*!< Group number for LOG status codes. */ 192 kStatusGroup_I3CBUS = 155, /*!< Group number for I3CBUS status codes. */ 193 kStatusGroup_QSCI = 156, /*!< Group number for QSCI status codes. */ 194 kStatusGroup_ELEMU = 157, /*!< Group number for ELEMU status codes. */ 195 kStatusGroup_QUEUEDSPI = 158, /*!< Group number for QSPI status codes. */ 196 kStatusGroup_POWER_MANAGER = 159, /*!< Group number for POWER_MANAGER status codes. */ 197 kStatusGroup_IPED = 160, /*!< Group number for IPED status codes. */ 198 kStatusGroup_ELS_PKC = 161, /*!< Group number for ELS PKC status codes. */ 199 kStatusGroup_CSS_PKC = 162, /*!< Group number for CSS PKC status codes. */ 200 kStatusGroup_HOSTIF = 163, /*!< Group number for HOSTIF status codes. */ 201 kStatusGroup_CLIF = 164, /*!< Group number for CLIF status codes. */ 202 kStatusGroup_BMA = 165, /*!< Group number for BMA status codes. */ 203 kStatusGroup_NETC = 166, /*!< Group number for NETC status codes. */ 204 kStatusGroup_ELE = 167, /*!< Group number for ELE status codes. */ 205 }; 206 207 /*! \public 208 * @brief Generic status return codes. 209 */ 210 enum 211 { 212 kStatus_Success = MAKE_STATUS(kStatusGroup_Generic, 0), /*!< Generic status for Success. */ 213 kStatus_Fail = MAKE_STATUS(kStatusGroup_Generic, 1), /*!< Generic status for Fail. */ 214 kStatus_ReadOnly = MAKE_STATUS(kStatusGroup_Generic, 2), /*!< Generic status for read only failure. */ 215 kStatus_OutOfRange = MAKE_STATUS(kStatusGroup_Generic, 3), /*!< Generic status for out of range access. */ 216 kStatus_InvalidArgument = MAKE_STATUS(kStatusGroup_Generic, 4), /*!< Generic status for invalid argument check. */ 217 kStatus_Timeout = MAKE_STATUS(kStatusGroup_Generic, 5), /*!< Generic status for timeout. */ 218 kStatus_NoTransferInProgress = 219 MAKE_STATUS(kStatusGroup_Generic, 6), /*!< Generic status for no transfer in progress. */ 220 kStatus_Busy = MAKE_STATUS(kStatusGroup_Generic, 7), /*!< Generic status for module is busy. */ 221 kStatus_NoData = 222 MAKE_STATUS(kStatusGroup_Generic, 8), /*!< Generic status for no data is found for the operation. */ 223 }; 224 225 /*! @brief Type used for all status and error return values. */ 226 typedef int32_t status_t; 227 228 /*! 229 * @name Min/max macros 230 * @{ 231 */ 232 #if !defined(MIN) 233 /*! Computes the minimum of \a a and \a b. */ 234 #define MIN(a, b) (((a) < (b)) ? (a) : (b)) 235 #endif 236 237 #if !defined(MAX) 238 /*! Computes the maximum of \a a and \a b. */ 239 #define MAX(a, b) (((a) > (b)) ? (a) : (b)) 240 #endif 241 /*! @} */ 242 243 /*! @brief Computes the number of elements in an array. */ 244 #if !defined(ARRAY_SIZE) 245 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) 246 #endif 247 248 /*! @name UINT16_MAX/UINT32_MAX value */ 249 /*! @{ */ 250 #if !defined(UINT16_MAX) 251 /*! Max value of uint16_t type. */ 252 #define UINT16_MAX ((uint16_t)-1) 253 #endif 254 255 #if !defined(UINT32_MAX) 256 /*! Max value of uint32_t type. */ 257 #define UINT32_MAX ((uint32_t)-1) 258 #endif 259 /*! @} */ 260 261 /*! 262 * @def SUPPRESS_FALL_THROUGH_WARNING() 263 * 264 * For switch case code block, if case section ends without "break;" statement, there wil be 265 * fallthrough warning with compiler flag -Wextra or -Wimplicit-fallthrough=n when using armgcc. 266 * To suppress this warning, "SUPPRESS_FALL_THROUGH_WARNING();" need to be added at the end of each 267 * case section which misses "break;"statement. 268 */ 269 #if defined(__GNUC__) && !defined(__ARMCC_VERSION) 270 #define SUPPRESS_FALL_THROUGH_WARNING() __attribute__((fallthrough)) 271 #else 272 #define SUPPRESS_FALL_THROUGH_WARNING() 273 #endif 274 275 /******************************************************************************* 276 * API 277 ******************************************************************************/ 278 279 #if defined(__cplusplus) 280 extern "C" { 281 #endif 282 283 #if !((defined(__DSC__) && defined(__CW__))) 284 /*! 285 * @brief Allocate memory with given alignment and aligned size. 286 * 287 * This is provided to support the dynamically allocated memory 288 * used in cache-able region. 289 * @param size The length required to malloc. 290 * @param alignbytes The alignment size. 291 * @retval The allocated memory. 292 */ 293 void *SDK_Malloc(size_t size, size_t alignbytes); 294 295 /*! 296 * @brief Free memory. 297 * 298 * @param ptr The memory to be release. 299 */ 300 void SDK_Free(void *ptr); 301 #endif 302 303 /*! 304 * @brief Delay at least for some time. 305 * Please note that, this API uses while loop for delay, different run-time environments make the time not precise, 306 * if precise delay count was needed, please implement a new delay function with hardware timer. 307 * 308 * @param delayTime_us Delay time in unit of microsecond. 309 * @param coreClock_Hz Core clock frequency with Hz. 310 */ 311 void SDK_DelayAtLeastUs(uint32_t delayTime_us, uint32_t coreClock_Hz); 312 313 #if defined(__cplusplus) 314 } 315 #endif 316 317 /*! @} */ 318 319 #if (defined(__DSC__) && defined(__CW__)) 320 #include "fsl_common_dsc.h" 321 #elif defined(__XTENSA__) 322 #include "fsl_common_dsp.h" 323 #else 324 #include "fsl_common_arm.h" 325 #endif 326 327 #endif /* FSL_COMMON_H_ */ 328