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