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     kStatusGroup_XSPI                  = 108, /*!< Group number for XSPI status codes */
162     kStatusGroup_PNGDEC                = 109, /*!< Group number for PNGDEC status codes */
163     kStatusGroup_JPEGDEC               = 110, /*!< Group number for JPEGDEC status codes */
164 
165     kStatusGroup_HAL_GPIO       = 121, /*!< Group number for HAL GPIO status codes. */
166     kStatusGroup_HAL_UART       = 122, /*!< Group number for HAL UART status codes. */
167     kStatusGroup_HAL_TIMER      = 123, /*!< Group number for HAL TIMER status codes. */
168     kStatusGroup_HAL_SPI        = 124, /*!< Group number for HAL SPI status codes. */
169     kStatusGroup_HAL_I2C        = 125, /*!< Group number for HAL I2C status codes. */
170     kStatusGroup_HAL_FLASH      = 126, /*!< Group number for HAL FLASH status codes. */
171     kStatusGroup_HAL_PWM        = 127, /*!< Group number for HAL PWM status codes. */
172     kStatusGroup_HAL_RNG        = 128, /*!< Group number for HAL RNG status codes. */
173     kStatusGroup_HAL_I2S        = 129, /*!< Group number for HAL I2S status codes. */
174     kStatusGroup_HAL_ADC_SENSOR = 130, /*!< Group number for HAL ADC SENSOR status codes. */
175     kStatusGroup_TIMERMANAGER   = 135, /*!< Group number for TiMER MANAGER status codes. */
176     kStatusGroup_SERIALMANAGER  = 136, /*!< Group number for SERIAL MANAGER status codes. */
177     kStatusGroup_LED            = 137, /*!< Group number for LED status codes. */
178     kStatusGroup_BUTTON         = 138, /*!< Group number for BUTTON status codes. */
179     kStatusGroup_EXTERN_EEPROM  = 139, /*!< Group number for EXTERN EEPROM status codes. */
180     kStatusGroup_SHELL          = 140, /*!< Group number for SHELL status codes. */
181     kStatusGroup_MEM_MANAGER    = 141, /*!< Group number for MEM MANAGER status codes. */
182     kStatusGroup_LIST           = 142, /*!< Group number for List status codes. */
183     kStatusGroup_OSA            = 143, /*!< Group number for OSA status codes. */
184     kStatusGroup_COMMON_TASK    = 144, /*!< Group number for Common task status codes. */
185     kStatusGroup_MSG            = 145, /*!< Group number for messaging status codes. */
186     kStatusGroup_SDK_OCOTP      = 146, /*!< Group number for OCOTP status codes. */
187     kStatusGroup_SDK_FLEXSPINOR = 147, /*!< Group number for FLEXSPINOR status codes.*/
188     kStatusGroup_CODEC          = 148, /*!< Group number for codec status codes. */
189     kStatusGroup_ASRC           = 149, /*!< Group number for codec status ASRC. */
190     kStatusGroup_OTFAD          = 150, /*!< Group number for codec status codes. */
191     kStatusGroup_SDIOSLV        = 151, /*!< Group number for SDIOSLV status codes. */
192     kStatusGroup_MECC           = 152, /*!< Group number for MECC status codes. */
193     kStatusGroup_ENET_QOS       = 153, /*!< Group number for ENET_QOS status codes. */
194     kStatusGroup_LOG            = 154, /*!< Group number for LOG status codes. */
195     kStatusGroup_I3CBUS         = 155, /*!< Group number for I3CBUS status codes. */
196     kStatusGroup_QSCI           = 156, /*!< Group number for QSCI status codes. */
197     kStatusGroup_ELEMU          = 157, /*!< Group number for ELEMU status codes. */
198     kStatusGroup_QUEUEDSPI      = 158, /*!< Group number for QSPI status codes. */
199     kStatusGroup_POWER_MANAGER  = 159, /*!< Group number for POWER_MANAGER status codes. */
200     kStatusGroup_IPED           = 160, /*!< Group number for IPED status codes. */
201     kStatusGroup_ELS_PKC        = 161, /*!< Group number for ELS PKC status codes. */
202     kStatusGroup_CSS_PKC        = 162, /*!< Group number for CSS PKC status codes. */
203     kStatusGroup_HOSTIF         = 163, /*!< Group number for HOSTIF status codes. */
204     kStatusGroup_CLIF           = 164, /*!< Group number for CLIF status codes. */
205     kStatusGroup_BMA            = 165, /*!< Group number for BMA status codes. */
206     kStatusGroup_NETC           = 166, /*!< Group number for NETC status codes. */
207     kStatusGroup_ELE            = 167, /*!< Group number for ELE status codes. */
208     kStatusGroup_GLIKEY         = 168, /*!< Group number for GLIKEY status codes. */
209 };
210 
211 /*! \public
212  * @brief Generic status return codes.
213  */
214 enum
215 {
216     kStatus_Success         = MAKE_STATUS(kStatusGroup_Generic, 0), /*!< Generic status for Success. */
217     kStatus_Fail            = MAKE_STATUS(kStatusGroup_Generic, 1), /*!< Generic status for Fail. */
218     kStatus_ReadOnly        = MAKE_STATUS(kStatusGroup_Generic, 2), /*!< Generic status for read only failure. */
219     kStatus_OutOfRange      = MAKE_STATUS(kStatusGroup_Generic, 3), /*!< Generic status for out of range access. */
220     kStatus_InvalidArgument = MAKE_STATUS(kStatusGroup_Generic, 4), /*!< Generic status for invalid argument check. */
221     kStatus_Timeout         = MAKE_STATUS(kStatusGroup_Generic, 5), /*!< Generic status for timeout. */
222     kStatus_NoTransferInProgress =
223         MAKE_STATUS(kStatusGroup_Generic, 6),            /*!< Generic status for no transfer in progress. */
224     kStatus_Busy = MAKE_STATUS(kStatusGroup_Generic, 7), /*!< Generic status for module is busy. */
225     kStatus_NoData =
226         MAKE_STATUS(kStatusGroup_Generic, 8), /*!< Generic status for no data is found for the operation. */
227 };
228 
229 /*! @brief Type used for all status and error return values. */
230 typedef int32_t status_t;
231 
232 #ifdef __ZEPHYR__
233 #include <zephyr/sys/util.h>
234 #else
235 /*!
236  * @name Min/max macros
237  * @{
238  */
239 #if !defined(MIN)
240 /*! Computes the minimum of \a a and \a b. */
241 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
242 #endif
243 
244 #if !defined(MAX)
245 /*! Computes the maximum of \a a and \a b. */
246 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
247 #endif
248 /*! @} */
249 
250 /*! @brief Computes the number of elements in an array. */
251 #if !defined(ARRAY_SIZE)
252 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
253 #endif
254 #endif /* __ZEPHYR__ */
255 
256 /*! @name UINT16_MAX/UINT32_MAX value */
257 /*! @{ */
258 #if !defined(UINT16_MAX)
259 /*! Max value of uint16_t type. */
260 #define UINT16_MAX ((uint16_t)-1)
261 #endif
262 
263 #if !defined(UINT32_MAX)
264 /*! Max value of uint32_t type. */
265 #define UINT32_MAX ((uint32_t)-1)
266 #endif
267 /*! @} */
268 
269 /*! @name UINTPTR_SIZE value */
270 /* @{ */
271 #if !defined(UINTPTR_SIZE)
272 #if UINTPTR_MAX > UINT32_MAX
273   #define UINTPTR_SIZE 8 /* 64-bit processor */
274 #elif UINTPTR_MAX > UINT16_MAX
275   #define UINTPTR_SIZE 4 /* 32-bit processor */
276 #else
277   #error "UINTPTR_SIZE is unknown!"
278 #endif
279 #endif
280 /* @} */
281 
282 /*! Macro to get upper 32 bits of a 64-bit value */
283 #if !defined(UINT64_H)
284 #define UINT64_H(X)        ((uint32_t)((((uint64_t) (X)) >> 32U) & 0x0FFFFFFFFULL))
285 #endif
286 
287 /*! Macro to get lower 32 bits of a 64-bit value */
288 #if !defined(UINT64_L)
289 #define UINT64_L(X)        ((uint32_t)(((uint64_t) (X)) & 0x0FFFFFFFFULL))
290 #endif
291 
292 /*!
293  * @def SUPPRESS_FALL_THROUGH_WARNING()
294  *
295  * For switch case code block, if case section ends without "break;" statement, there wil be
296  * fallthrough warning with compiler flag -Wextra or -Wimplicit-fallthrough=n when using armgcc.
297  * To suppress this warning, "SUPPRESS_FALL_THROUGH_WARNING();" need to be added at the end of each
298  * case section which misses "break;"statement.
299  */
300 #if defined(__GNUC__) && !defined(__ARMCC_VERSION)
301 #define SUPPRESS_FALL_THROUGH_WARNING() __attribute__((fallthrough))
302 #else
303 #define SUPPRESS_FALL_THROUGH_WARNING()
304 #endif
305 
306 /*******************************************************************************
307  * API
308  ******************************************************************************/
309 
310 #if defined(__cplusplus)
311 extern "C" {
312 #endif
313 
314 #if !((defined(__DSC__) && defined(__CW__)))
315 /*!
316  * @brief Allocate memory with given alignment and aligned size.
317  *
318  * This is provided to support the dynamically allocated memory
319  * used in cache-able region.
320  * @param size The length required to malloc.
321  * @param alignbytes The alignment size.
322  * @retval The allocated memory.
323  */
324 void *SDK_Malloc(size_t size, size_t alignbytes);
325 
326 /*!
327  * @brief Free memory.
328  *
329  * @param ptr The memory to be release.
330  */
331 void SDK_Free(void *ptr);
332 #endif
333 
334 /*!
335  * @brief Delay at least for some time.
336  *  Please note that, this API uses while loop for delay, different run-time environments make the time not precise,
337  *  if precise delay count was needed, please implement a new delay function with hardware timer.
338  *
339  * @param delayTime_us  Delay time in unit of microsecond.
340  * @param coreClock_Hz  Core clock frequency with Hz.
341  */
342 void SDK_DelayAtLeastUs(uint32_t delayTime_us, uint32_t coreClock_Hz);
343 
344 #if defined(__cplusplus)
345 }
346 #endif
347 
348 /*! @} */
349 
350 #if (defined(__DSC__) && defined(__CW__))
351 #include "fsl_common_dsc.h"
352 #elif defined(__XTENSA__)
353 #include "fsl_common_dsp.h"
354 #else
355 #include "fsl_common_arm.h"
356 #endif
357 
358 #endif /* FSL_COMMON_H_ */
359