Contiki 3.x
startup-k60.c
1 #include <stdint.h>
2 #include <stddef.h>
3 #include "K60.h"
4 #include "core-clocks.h"
5 #include "config-board.h"
6 
7 extern int main(void); /* the reset handler will invoke main() after hardware startup. */
8 
9 /* The reset handler does not need any function prologue/epilogue since it is
10  * only called after a reset */
11 void reset_handler(void) __attribute__((naked));
12 
13 /* Start of .data section in RAM */
14 extern uint32_t _data_start[];
15 /* End of .data section in RAM */
16 extern uint32_t _data_end[];
17 /* Start of .data section in flash */
18 extern uint32_t _data_load[];
19 
20 /*
21  * Copy all initialized variables in .data from flash to RAM.
22  * .data must be 4-byte aligned!
23  */
24 static void
25 copy_initialized(void)
26 {
27  uint32_t *ram = _data_start;
28  uint32_t *rom = _data_load;
29  while(ram < _data_end) {
30  *(ram++) = *(rom++);
31  }
32 }
33 /* Start of .bss section in RAM */
34 extern uint32_t __bss_start[];
35 /* End of .bss section in RAM */
36 extern uint32_t __bss_end[];
37 
38 /*
39  * Clear out .bss section.
40  * .bss must be 4-byte aligned!
41  */
42 static void
43 clear_bss(void)
44 {
45  uint32_t *p = __bss_start;
46  while(p < __bss_end) {
47  *p = 0x0ul;
48  ++p;
49  }
50 }
51 /* Start of .ramcode section in RAM */
52 extern uint32_t _ramcode_start[];
53 /* End of .ramcode section in RAM */
54 extern uint32_t _ramcode_end[];
55 /* Start of .ramcode section in flash */
56 extern uint32_t _ramcode_load[];
57 /*
58  * Copy the ramcode section to RAM.
59  */
60 static void
61 copy_ramcode(void)
62 {
63  uint32_t *ram = _ramcode_start;
64  uint32_t *rom = _ramcode_load;
65  while(ram < _ramcode_end) {
66  *(ram++) = *(rom++);
67  }
68 }
69 /* Initialize all data used by the C runtime. */
70 static void __attribute__((unused))
71 init_data(void)
72 {
73  copy_initialized();
74 
75  clear_bss();
76 
77  copy_ramcode();
78 }
79 /* newlib's initialization function */
80 extern void __libc_init_array(void);
81 
82 /* our local copy of newlib init */
83 void call_init_array(void);
84 
85 /* Stack pointer will be set to _stack_start by the hardware at reset/power on */
86 void
87 reset_handler(void)
88 {
89 #if DISABLE_WDOG
90  /* Disable watchdog to allow single stepping through the startup code. */
91  /*
92  * The following unlock sequence must be completed within 256 bus cycles or
93  * the watchdog will reset the system. The watchdog is enabled by default at
94  * power on.
95  *
96  * The sequence is:
97  * 1. Write 0xC520 to the unlock register
98  * 2. Write 0xD928 to the unlock register
99  *
100  * Watchdog is now unlocked to allow us to change its settings
101  *
102  * 3. Clear the WDOGEN bit of the WDOG_STCTRLH register to completely disable
103  * the watchdog.
104  *
105  * It is now possible to single step through the code without the watchdog
106  * resetting the system.
107  */
108  WDOG->UNLOCK = 0xC520;
109  WDOG->UNLOCK = 0xD928;
110  WDOG->STCTRLH &= ~WDOG_STCTRLH_WDOGEN_MASK;
111 
112  /*
113  * The line below this comment is the earliest possible location for a
114  * breakpoint when debugging the startup code.
115  */
116 #endif /* DISABLE_WDOG */
117 
118  call_init_array(); /* or __libc_init_array() as provided by newlib or other libc */
119 
120  main();
121  /* main should never return, but just in case... */
122  while(1);
123 }
124 /* Initialize static C++ objects etc. */
125 
126 /* The implementation of call_constructors is based on newlib's
127  * __libc_init_array() copyright CodeSourcery */
128 
129 /* The below copyright notice applies only to the function call_constructors,
130  * copied from newlib 2.0 */
131 /*
132  * Copyright (C) 2004 CodeSourcery, LLC
133  *
134  * Permission to use, copy, modify, and distribute this file
135  * for any purpose is hereby granted without fee, provided that
136  * the above copyright notice and this notice appears in all
137  * copies.
138  *
139  * This file is distributed WITHOUT ANY WARRANTY; without even the implied
140  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
141  */
142 
143 extern void(*__preinit_array_start[]) (void) __attribute__((weak));
144 extern void(*__preinit_array_end[]) (void) __attribute__((weak));
145 extern void(*__init_array_start[]) (void) __attribute__((weak));
146 extern void(*__init_array_end[]) (void) __attribute__((weak));
147 
148 /* By default, initialize all C runtime data after preinit */
149 void _init(void) __attribute__((weak, alias("init_data")));
150 
151 void
152 call_init_array(void)
153 {
154  size_t count;
155  size_t i;
156 
157  count = __preinit_array_end - __preinit_array_start;
158  for(i = 0; i < count; i++) {
159  __preinit_array_start[i]();
160  }
161 
162  _init();
163 
164  count = __init_array_end - __init_array_start;
165  for(i = 0; i < count; i++) {
166  __init_array_start[i]();
167  }
168 }
void __attribute__((interrupt))
This ISR handles most of the business interacting with the 1-wire bus.
Definition: onewire.c:174
int main(void)
This is main...
Definition: ethconfig.c:49
#define WDOG
Peripheral WDOG base pointer.
Definition: MK60D10.h:9298
Board configuration defines for Mulle platform.
K60 hardware register header wrapper.
K60 clock configuration functions.