Kinetis Bootloader  2.0.0
Common bootloader for Kinetis devices
fsl_common.h
1 /*
2  * Copyright (c) 2015, Freescale Semiconductor, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * o Redistributions of source code must retain the above copyright notice, this list
9  * of conditions and the following disclaimer.
10  *
11  * o Redistributions in binary form must reproduce the above copyright notice, this
12  * list of conditions and the following disclaimer in the documentation and/or
13  * other materials provided with the distribution.
14  *
15  * o Neither the name of Freescale Semiconductor, Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from this
17  * software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #ifndef _FSL_COMMON_H_
32 #define _FSL_COMMON_H_
33 
34 #include <assert.h>
35 #include <stdbool.h>
36 #include <stdint.h>
37 #include <string.h>
38 #if !defined(BOOTLOADER_HOST)
39 #include "fsl_device_registers.h"
40 #endif // BOOTLOADER_HOST
41 
47 /*******************************************************************************
48  * Definitions
49  ******************************************************************************/
50 
52 #define MAKE_STATUS(group, code) ((((group)*100) + (code)))
53 
55 #define MAKE_VERSION(major, minor, bugfix) (((major) << 16) | ((minor) << 8) | (bugfix))
56 
57 /* Debug console type definition. */
58 #define DEBUG_CONSOLE_DEVICE_TYPE_NONE 0U
59 #define DEBUG_CONSOLE_DEVICE_TYPE_UART 1U
60 #define DEBUG_CONSOLE_DEVICE_TYPE_LPUART 2U
61 #define DEBUG_CONSOLE_DEVICE_TYPE_LPSCI 3U
62 #define DEBUG_CONSOLE_DEVICE_TYPE_USBCDC 4U
65 enum _status_groups
66 {
106 };
107 
110 {
111  kStatus_Success = MAKE_STATUS(kStatusGroup_Generic, 0),
112  kStatus_Fail = MAKE_STATUS(kStatusGroup_Generic, 1),
113  kStatus_ReadOnly = MAKE_STATUS(kStatusGroup_Generic, 2),
114  kStatus_OutOfRange = MAKE_STATUS(kStatusGroup_Generic, 3),
115  kStatus_InvalidArgument = MAKE_STATUS(kStatusGroup_Generic, 4),
116  kStatus_Timeout = MAKE_STATUS(kStatusGroup_Generic, 5),
117  kStatus_NoTransferInProgress = MAKE_STATUS(kStatusGroup_Generic, 6),
118 };
119 
121 typedef int32_t status_t;
122 
123 #if !defined(BOOTLOADER_HOST)
124 #include "fsl_clock.h"
125 #endif // BOOTLOADER_HOST
126 
128 /* @{ */
129 #if !defined(MIN)
130 #define MIN(a, b) ((a) < (b) ? (a) : (b))
131 #endif
132 
133 #if !defined(MAX)
134 #define MAX(a, b) ((a) > (b) ? (a) : (b))
135 #endif
136 /* @} */
137 
139 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
140 
142 /* @{ */
144 #define USEC_TO_COUNT(us, clockFreqInHz) (uint64_t)((uint64_t)us * clockFreqInHz / 1000000U)
145 
146 #define COUNT_TO_USEC(count, clockFreqInHz) (uint64_t)((uint64_t)count * 1000000U / clockFreqInHz)
147 
149 #define MSEC_TO_COUNT(ms, clockFreqInHz) (uint64_t)((uint64_t)ms * clockFreqInHz / 1000U)
150 
151 #define COUNT_TO_MSEC(count, clockFreqInHz) (uint64_t)((uint64_t)count * 1000U / clockFreqInHz)
152 /* @} */
153 
154 /*******************************************************************************
155  * API
156  ******************************************************************************/
157 
158 #if !defined(BOOTLOADER_HOST)
159 
160 #if defined(__cplusplus)
161 extern "C" {
162 #endif
163 
171 static inline void EnableIRQ(IRQn_Type interrupt)
172 {
173 #if defined(FSL_FEATURE_SOC_INTMUX_COUNT) && (FSL_FEATURE_SOC_INTMUX_COUNT > 0)
174  if (interrupt < FSL_FEATURE_INTMUX_IRQ_START_INDEX)
175 #endif
176  {
177  NVIC_EnableIRQ(interrupt);
178  }
179 }
180 
188 static inline void DisableIRQ(IRQn_Type interrupt)
189 {
190 #if defined(FSL_FEATURE_SOC_INTMUX_COUNT) && (FSL_FEATURE_SOC_INTMUX_COUNT > 0)
191  if (interrupt < FSL_FEATURE_INTMUX_IRQ_START_INDEX)
192 #endif
193  {
194  NVIC_DisableIRQ(interrupt);
195  }
196 }
197 
206 static inline uint32_t DisableGlobalIRQ(void)
207 {
208  uint32_t regPrimask = __get_PRIMASK();
209 
210  __disable_irq();
211 
212  return regPrimask;
213 }
214 
225 static inline void EnableGlobalIRQ(uint32_t primask)
226 {
227  __set_PRIMASK(primask);
228 }
229 
236 void InstallIRQHandler(IRQn_Type irq, uint32_t irqHandler);
237 
238 #if defined(__cplusplus)
239 }
240 #endif
241 
242 #endif // BOOTLOADER_HOST
243 
246 #endif /* _FSL_COMMON_H_ */
Definition: fsl_common.h:71
Definition: fsl_common.h:94
Definition: fsl_common.h:105
Definition: fsl_common.h:78
static uint32_t DisableGlobalIRQ(void)
Disable the global IRQ.
Definition: fsl_common.h:206
Definition: fsl_common.h:85
Definition: fsl_common.h:76
Definition: fsl_common.h:80
Definition: fsl_common.h:79
Definition: fsl_common.h:101
Definition: fsl_common.h:97
Definition: fsl_common.h:100
static void EnableGlobalIRQ(uint32_t primask)
Enaable the global IRQ.
Definition: fsl_common.h:225
Definition: fsl_common.h:86
_generic_status
Generic status return codes.
Definition: fsl_common.h:109
Definition: fsl_common.h:82
Definition: fsl_common.h:77
Definition: fsl_common.h:88
Definition: fsl_common.h:90
Definition: fsl_common.h:103
Definition: fsl_common.h:95
Definition: fsl_common.h:102
Definition: fsl_common.h:91
Definition: fsl_common.h:89
Definition: fsl_common.h:92
Definition: fsl_common.h:72
Definition: fsl_common.h:93
Definition: fsl_common.h:73
static void DisableIRQ(IRQn_Type interrupt)
Disable specific interrupt.
Definition: fsl_common.h:188
Definition: fsl_common.h:83
Definition: fsl_common.h:81
Definition: fsl_common.h:75
void InstallIRQHandler(IRQn_Type irq, uint32_t irqHandler)
install IRQ handler
Definition: fsl_common.c:56
Definition: fsl_common.h:87
Definition: fsl_common.h:68
Definition: fsl_common.h:99
Definition: fsl_common.h:104
Definition: fsl_common.h:96
Definition: fsl_common.h:84
Definition: fsl_common.h:67
Definition: fsl_common.h:98
Definition: fsl_common.h:70
int32_t status_t
Type used for all status and error return values.
Definition: fsl_common.h:121
Definition: fsl_common.h:69
static void EnableIRQ(IRQn_Type interrupt)
Enable specific interrupt.
Definition: fsl_common.h:171
Definition: fsl_common.h:74