Kinetis Bootloader  2.0.0
Common bootloader for Kinetis devices
bootloader_common.h
1 /*
2  * Copyright (c) 2013-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 #ifndef __BOOTLOADER_COMMON_H__
31 #define __BOOTLOADER_COMMON_H__
32 
33 #include <stdio.h>
34 #include <stdarg.h>
35 #include <stdint.h>
36 #if !defined(WIN32)
37 #include <stdbool.h>
38 #endif
39 #include "bootloader_common.h"
40 #include "fsl_common.h"
41 
42 #if defined(BOOTLOADER_HOST)
43 #include "blfwk/bootloader_config.h"
44 #elif defined(BUSPAL)
45 #include "../src/buspal_config.h"
46 #else
47 #include "bootloader_config.h"
48 #include "target_config.h"
49 #endif
50 
52 // Definitions
54 
55 #ifndef NULL
56 #define NULL 0
57 #endif
58 
59 // The following macros are to be used when trying to save code size for specific peripheral configurations
60 // that will only be using one peripheral instance. most of the peripheral driver code can use multiple instances but by
61 // just using one
62 // we can save space
63 #define USE_ONLY_UART(instance) (defined(BL_FEATURE_UART_OPTIMIZE) && (BL_UART_USED_INSTANCE == instance))
64 #define USE_ONLY_SPI(instance) (defined(BL_FEATURE_SPI_OPTIMIZE) && (BL_SPI_USED_INSTANCE == instance))
65 #define USE_ONLY_I2C(instance) (defined(BL_FEATURE_I2C_OPTIMIZE) && (BL_I2C_USED_INSTANCE == instance))
66 
68 
69 #if !defined(MIN)
70 #define MIN(a, b) ((a) < (b) ? (a) : (b))
71 #endif
72 
73 #if !defined(MAX)
74 #define MAX(a, b) ((a) > (b) ? (a) : (b))
75 #endif
76 
77 
79 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
80 
82 
83 #define BSWAP_16(x) (uint16_t)((((x)&0xFF00) >> 0x8) | (((x)&0xFF) << 0x8))
84 #define BSWAP_32(val) \
85  (uint32_t)((BSWAP_16((uint32_t)(val) & (uint32_t)0xFFFF) << 0x10) | (BSWAP_16((uint32_t)((val) >> 0x10))))
86 
87 
89 
90 #ifndef ALIGN_DOWN
91 #define ALIGN_DOWN(x, a) ((x) & -(a))
92 #endif
93 #ifndef ALIGN_UP
94 #define ALIGN_UP(x, a) (-(-(x) & -(a)))
95 #endif
96 
97 
103 #define FOUR_CHAR_CODE(a, b, c, d) (((d) << 24) | ((c) << 16) | ((b) << 8) | ((a)))
104 
105 #if (defined(DEBUG) || defined(_DEBUG)) && !defined(DEBUG_PRINT_DISABLE)
106 static inline void debug_printf(const char *format, ...);
107 
111 static inline void debug_printf(const char *format, ...)
112 {
113  va_list args;
114  va_start(args, format);
115  vprintf(format, args);
116 // Temporarily disable MISRA rule 14.2
117 #if defined(__ICCARM__)
118 #pragma diag_suppress = Pm049
119 #endif
120  va_end(args);
121 #if defined(__ICCARM__)
122 #pragma diag_default = Pm049
123 #endif
124 }
125 #else // (DEBUG || _DEBUG) && !DEBUG_PRINT_DISABLE
126 // Empty macro to cause debug_printf() calls to disappear.
127 #define debug_printf(x, ...) \
128  do \
129  { \
130  } while (false)
131 #endif // (DEBUG || _DEBUG) && !DEBUG_PRINT_DISABLE
132 
136 typedef void (*pin_irq_callback_t)(uint32_t instance);
137 
142 {
150 };
151 
156 {
159 };
160 
161 #if defined(__CC_ARM)
162 #pragma anon_unions
163 #endif
164 
168 typedef union StandardVersion
169 {
170  struct
171  {
172  uint8_t bugfix;
173  uint8_t minor;
174  uint8_t major;
175  char name;
176  };
177  uint32_t version;
178 
179 #if defined(__cplusplus)
180  StandardVersion()
181  : version(0)
182  {
183  }
184  StandardVersion(uint32_t version)
185  : version(version)
186  {
187  }
188 #endif
190 
191 // #define MAKE_VERSION(bugfix, minor, major, name) (((name) << 24) | ((major) << 16) | ((minor) << 8 ) | (bugfix))
192 
197 {
198  kExternalMemId_QuadSPI0 = 1,
199 };
200 
202 typedef enum _bootloader_clock_option
203 {
204  kClockOption_EnterBootloader = 0,
205  kClockOption_ExitBootloader = 1,
206 } bootloader_clock_option_t;
207 
209 // Prototypes
211 
214 
216 void init_hardware(void);
217 
219 void deinit_hardware(void);
220 
223 
225 // uint32_t read_autobaud_pin(uint32_t instance);
226 
228 void configure_clocks(bootloader_clock_option_t option);
229 
231 uint32_t get_available_lirc_clock(void);
232 
234 uint32_t get_bus_clock(void);
235 
237 uint32_t get_system_core_clock(void);
238 
240 bool usb_clock_init(void);
241 
243 uint32_t get_uart_clock(uint32_t instance);
244 
246 bool is_boot_pin_asserted(void);
247 
249 void enable_autobaud_pin_irq(uint32_t instance, pin_irq_callback_t func);
250 
252 void disable_autobaud_pin_irq(uint32_t instance);
253 
255 void Reset_Handler(void);
256 
258 void bootloader_watchdog_init(void);
259 
261 void bootloader_watchdog_service(void);
262 
264 void bootloader_watchdog_deinit(void);
265 
267 bool qspi_need_configure(void);
268 
272 
274 bool is_qspi_present(void);
275 
277 bool is_otfad_present(void);
278 
280 bool is_ltc_present(void);
281 
284 
286 void update_qspi_otfad_init_status(status_t initStatus);
287 
290 
292 bool is_in_execute_only_region(uint32_t start, uint32_t lengthInBytes);
293 
295 bool is_second_core_present(void);
296 
298 
299 #endif // __BOOTLOADER_COMMON_H__
300 // EOF
status_t get_qspi_otfad_init_status(void)
Return status for intializing qspi and otfad modules.
Definition: bl_misc.c:236
Memory interface status group number (102).
Definition: bootloader_common.h:145
uint32_t version
combined version numbers
Definition: bootloader_common.h:177
Definition: fsl_common.h:105
bool is_in_execute_only_region(uint32_t start, uint32_t lengthInBytes)
Check if data to be accessed is in execute-only region.
Definition: bl_misc.c:249
uint32_t get_available_lirc_clock(void)
Returns the available lirc clock frequency in Hertz.
void bootloader_watchdog_init(void)
Initialize watchdog.
void Reset_Handler(void)
Declaration for the reset handler, which is defined in assembler.
uint32_t get_bus_clock(void)
Returns the current bus clock frequency in Hertz.
Definition: MK65F18/src/bus_pal_hardware.c:858
bool usb_clock_init(void)
Configure usb clock.
Definition: MK65F18/src/bus_pal_hardware.c:285
Bootloader status group number (100).
Definition: bootloader_common.h:143
void configure_clocks(bootloader_clock_option_t option)
Returns the logic level of the board specific GPIO pin used for autobaud.
void disable_autobaud_pin_irq(uint32_t instance)
Disables the autobaud pin IRQ for the instance passed.
_bl_status_groups
Bootloader status group numbers.
Definition: bootloader_common.h:141
uint32_t get_system_core_clock(void)
Returns the current core clock frequency in Hertz.
void init_hardware(void)
Initialize the hardware such as pinmux.
Definition: hardware_init_MK80F25615.c:49
_bl_driver_status_groups
Driver status group numbers.
Definition: bootloader_common.h:155
QSPI driver status group number.
Definition: bootloader_common.h:157
Property store status group number (103).
Definition: bootloader_common.h:146
void(* pin_irq_callback_t)(uint32_t instance)
Callback function invoked for a pin change interrupt.
Definition: bootloader_common.h:136
SB loader status group number (101).
Definition: bootloader_common.h:144
_external_mem_identifiers
External memory identifiers.
Definition: bootloader_common.h:196
uint32_t get_uart_clock(uint32_t instance)
Returns the value in MHz of the UART clock based on the instance.
Definition: host_hardware.c:303
bool qspi_need_configure(void)
Determine if QSPI module to be configured.
Definition: bl_misc.c:97
bool is_second_core_present(void)
Check if second core is present.
status_t otfad_init_as_needed(void)
Initialize QSPI and OTFAD module.
Definition: bl_misc.c:108
Structure of version property.
Definition: bootloader_common.h:168
void update_available_peripherals(void)
Update available peripherals based on specific chips.
void bootloader_watchdog_service(void)
Service watchdog.
void bootloader_watchdog_deinit(void)
De-initialize watchdog.
Application crc check status group number (104).
Definition: bootloader_common.h:147
void enable_autobaud_pin_irq(uint32_t instance, pin_irq_callback_t func)
Enables the autobaud pin IRQ for the specific instance passed.
bool is_qspi_present(void)
Determine if QSPI memory is present or not.
Definition: bl_misc.c:152
OTFAD driver status group number.
Definition: bootloader_common.h:158
bool is_ltc_present(void)
Determine if LTC module is present or not.
Definition: bl_misc.c:175
bool is_boot_pin_asserted(void)
Returns true if reset BOOTROM mode is selected.
void deinit_hardware(void)
DeInitialize the hardware such as disabling port clock gate.
bool is_otfad_present(void)
Determine if OTFAD module is present or not.
Definition: bl_misc.c:162
Packetizer status group number (105).
Definition: bootloader_common.h:148
int32_t status_t
Type used for all status and error return values.
Definition: fsl_common.h:121
Reliable Update status groupt number (106).
Definition: bootloader_common.h:149
void update_qspi_otfad_init_status(status_t initStatus)
Update status for intializing qspi and otfad modules
Definition: bl_misc.c:242
bool is_secondary_i2c_slave_address_enabled(void)
Determine is the Secondary I2C slave address is enabled.