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