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