Contiki 3.x
startup.c
1 /*
2  * Copyright (c) 2013 - 2014, 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 #include "startup.h"
32 #include "derivative.h"
33 
34 #if (defined(__ICCARM__))
35  #pragma section = ".data"
36  #pragma section = ".data_init"
37  #pragma section = ".bss"
38 #endif
39 
40 /*******************************************************************************
41  * Code
42  ******************************************************************************/
43 
44 /*FUNCTION**********************************************************************
45  *
46  * Function Name : init_data_bss
47  * Description : Make necessary initializations for RAM.
48  * - Copy initialized data from ROM to RAM.
49  * - Clear the zero-initialized data section.
50  * - Copy the vector table from ROM to RAM. This could be an option.
51  *
52  * Tool Chians:
53  * __GNUC__ : GCC
54  * __CC_ARM : KEIL
55  * __ICCARM__ : IAR
56  *
57  *END**************************************************************************/
58 void init_data_bss(void)
59 {
60  uint32_t n;
61 
62  /* Addresses for VECTOR_TABLE and VECTOR_RAM come from the linker file */
63 #if defined(__CC_ARM)
64  extern uint32_t Image$$VECTOR_ROM$$Base[];
65  extern uint32_t Image$$VECTOR_RAM$$Base[];
66  extern uint32_t Image$$RW_m_data$$Base[];
67 
68  #define __VECTOR_TABLE Image$$VECTOR_ROM$$Base
69  #define __VECTOR_RAM Image$$VECTOR_RAM$$Base
70  #define __RAM_VECTOR_TABLE_SIZE (((uint32_t)Image$$RW_m_data$$Base - (uint32_t)Image$$VECTOR_RAM$$Base))
71 #elif defined(__ICCARM__)
72  extern uint32_t __RAM_VECTOR_TABLE_SIZE[];
73  extern uint32_t __VECTOR_TABLE[];
74  extern uint32_t __VECTOR_RAM[];
75 #elif defined(__GNUC__)
76  extern uint32_t __VECTOR_TABLE[];
77  extern uint32_t __VECTOR_RAM[];
78  extern uint32_t __RAM_VECTOR_TABLE_SIZE_BYTES[];
79  uint32_t __RAM_VECTOR_TABLE_SIZE = (uint32_t)(__RAM_VECTOR_TABLE_SIZE_BYTES);
80 #endif
81 
82  if (__VECTOR_RAM != __VECTOR_TABLE)
83  {
84  /* Copy the vector table from ROM to RAM */
85  for (n = 0; n < ((uint32_t)__RAM_VECTOR_TABLE_SIZE)/sizeof(uint32_t); n++)
86  {
87  __VECTOR_RAM[n] = __VECTOR_TABLE[n];
88  }
89  /* Point the VTOR to the position of vector table */
90  SCB->VTOR = (uint32_t)__VECTOR_RAM;
91  }
92  else
93  {
94  /* Point the VTOR to the position of vector table */
95  SCB->VTOR = (uint32_t)__VECTOR_TABLE;
96  }
97 
98 #if !defined(__CC_ARM) && !defined(__ICCARM__)
99 
100  /* Declare pointers for various data sections. These pointers
101  * are initialized using values pulled in from the linker file */
102  uint8_t * data_ram, * data_rom, * data_rom_end;
103  uint8_t * bss_start, * bss_end;
104 
105  /* Get the addresses for the .data section (initialized data section) */
106 #if defined(__GNUC__)
107  extern uint32_t __DATA_ROM[];
108  extern uint32_t __DATA_RAM[];
109  extern char __DATA_END[];
110  data_ram = (uint8_t *)__DATA_RAM;
111  data_rom = (uint8_t *)__DATA_ROM;
112  data_rom_end = (uint8_t *)__DATA_END;
113  n = data_rom_end - data_rom;
114 #endif
115 
116  /* Copy initialized data from ROM to RAM */
117  while (n--)
118  {
119  *data_ram++ = *data_rom++;
120  }
121 
122  /* Get the addresses for the .bss section (zero-initialized data) */
123 #if defined(__GNUC__)
124  extern char __START_BSS[];
125  extern char __END_BSS[];
126  bss_start = (uint8_t *)__START_BSS;
127  bss_end = (uint8_t *)__END_BSS;
128 #endif
129 
130  /* Clear the zero-initialized data section */
131  n = bss_end - bss_start;
132  while(n--)
133  {
134  *bss_start++ = 0;
135  }
136 #endif /* !__CC_ARM && !__ICCARM__*/
137 }
138 
139 /*******************************************************************************
140  * EOF
141  ******************************************************************************/
142 
#define SCB
Definition: core_cm0.h:494