blob: ce60b091f17c168812b9071dcc359a43c67d2398 [file] [log] [blame]
Christina Quastb0a05702014-11-28 10:27:32 +01001/* ----------------------------------------------------------------------------
2 * ATMEL Microcontroller Software Support
3 * ----------------------------------------------------------------------------
4 * Copyright (c) 2010, Atmel Corporation
5 *
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * - Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the disclaimer below.
13 *
14 * Atmel's name may not be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
20 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
25 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
26 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 * ----------------------------------------------------------------------------
28 */
29
30/*----------------------------------------------------------------------------
31 * Headers
32 *----------------------------------------------------------------------------*/
33
34#include "board.h"
35#include "board_lowlevel.h"
36
37/*----------------------------------------------------------------------------
38 * Exported variables
39 *----------------------------------------------------------------------------*/
40
41/* Stack Configuration */
42#define STACK_SIZE 0x900 /** Stack size (in DWords) */
43__attribute__ ((aligned(8),section(".stack")))
44uint32_t pdwStack[STACK_SIZE] ;
45
46/* Initialize segments */
47extern uint32_t _sfixed;
48extern uint32_t _efixed;
49extern uint32_t _etext;
50extern uint32_t _srelocate;
51extern uint32_t _erelocate;
52extern uint32_t _szero;
53extern uint32_t _ezero;
54
55
56/*----------------------------------------------------------------------------
57 * ProtoTypes
58 *----------------------------------------------------------------------------*/
Christina Quast8be71e42014-12-02 13:06:01 +010059
60/** \cond DOXYGEN_SHOULD_SKIP_THIS */
Christina Quastb0a05702014-11-28 10:27:32 +010061extern int main( void ) ;
Christina Quast8be71e42014-12-02 13:06:01 +010062/** \endcond */
Christina Quastb0a05702014-11-28 10:27:32 +010063void ResetException( void ) ;
Christina Quastb0a05702014-11-28 10:27:32 +010064
65/*------------------------------------------------------------------------------
66 * Exception Table
67 *------------------------------------------------------------------------------*/
68
69__attribute__((section(".vectors")))
70IntFunc exception_table[] = {
71
72 /* Configure Initial Stack Pointer, using linker-generated symbols */
73 (IntFunc)(&pdwStack[STACK_SIZE-1]),
74 ResetException,
75
76 NMI_Handler,
77 HardFault_Handler,
78 MemManage_Handler,
79 BusFault_Handler,
80 UsageFault_Handler,
81 0, 0, 0, 0, /* Reserved */
82 SVC_Handler,
83 DebugMon_Handler,
84 0, /* Reserved */
85 PendSV_Handler,
86 SysTick_Handler,
87
88 /* Configurable interrupts */
89 SUPC_IrqHandler, /* 0 Supply Controller */
90 RSTC_IrqHandler, /* 1 Reset Controller */
91 RTC_IrqHandler, /* 2 Real Time Clock */
92 RTT_IrqHandler, /* 3 Real Time Timer */
93 WDT_IrqHandler, /* 4 Watchdog Timer */
94 PMC_IrqHandler, /* 5 PMC */
95 EEFC_IrqHandler, /* 6 EEFC */
96 IrqHandlerNotUsed, /* 7 Reserved */
97 UART0_IrqHandler, /* 8 UART0 */
98 UART1_IrqHandler, /* 9 UART1 */
99 SMC_IrqHandler, /* 10 SMC */
100 PIOA_IrqHandler, /* 11 Parallel IO Controller A */
101 PIOB_IrqHandler, /* 12 Parallel IO Controller B */
102 PIOC_IrqHandler, /* 13 Parallel IO Controller C */
103 USART0_IrqHandler, /* 14 USART 0 */
104 USART1_IrqHandler, /* 15 USART 1 */
105 IrqHandlerNotUsed, /* 16 Reserved */
106 IrqHandlerNotUsed, /* 17 Reserved */
107 MCI_IrqHandler, /* 18 MCI */
108 TWI0_IrqHandler, /* 19 TWI 0 */
109 TWI1_IrqHandler, /* 20 TWI 1 */
110 SPI_IrqHandler, /* 21 SPI */
111 SSC_IrqHandler, /* 22 SSC */
112 TC0_IrqHandler, /* 23 Timer Counter 0 */
113 TC1_IrqHandler, /* 24 Timer Counter 1 */
114 TC2_IrqHandler, /* 25 Timer Counter 2 */
115 TC3_IrqHandler, /* 26 Timer Counter 3 */
116 TC4_IrqHandler, /* 27 Timer Counter 4 */
117 TC5_IrqHandler, /* 28 Timer Counter 5 */
118 ADC_IrqHandler, /* 29 ADC controller */
119 DAC_IrqHandler, /* 30 DAC controller */
120 PWM_IrqHandler, /* 31 PWM */
121 CRCCU_IrqHandler, /* 32 CRC Calculation Unit */
122 ACC_IrqHandler, /* 33 Analog Comparator */
123 USBD_IrqHandler, /* 34 USB Device Port */
124 IrqHandlerNotUsed /* 35 not used */
125};
126
Harald Welte0af49482017-03-02 23:18:02 +0100127#if defined(BOARD_USB_DFU) && defined(APPLICATION_dfu)
Harald Welted1e96342017-03-03 00:34:17 +0100128#include "usb/device/dfu/dfu.h"
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100129static void BootIntoApp(void)
130{
131 unsigned int *pSrc;
132 void (*appReset)(void);
133
134 pSrc = (unsigned int *) ((unsigned char *)IFLASH_ADDR + BOARD_DFU_BOOT_SIZE);
135 SCB->VTOR = ((unsigned int)(pSrc)) | (0x0 << 7);
136 appReset = pSrc[1];
Harald Weltecab66412017-03-02 19:22:50 +0100137
Harald Welted1e96342017-03-03 00:34:17 +0100138 g_dfu->state = DFU_STATE_appIDLE;
Harald Weltecab66412017-03-02 19:22:50 +0100139
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100140 appReset();
141}
142#endif
143
Christina Quastb0a05702014-11-28 10:27:32 +0100144/**
145 * \brief This is the code that gets called on processor reset.
146 * To initialize the device, and call the main() routine.
147 */
148void ResetException( void )
149{
150 uint32_t *pSrc, *pDest ;
151
152 /* Low level Initialize */
153 LowLevelInit() ;
154
Harald Welte0af49482017-03-02 23:18:02 +0100155
156#if defined(BOARD_USB_DFU) && defined(APPLICATION_dfu)
Harald Welte91fc4022017-03-03 01:05:22 +0100157 /* we are before the text segment has been relocated, so g_dfu is
158 * not initialized yet */
159 g_dfu = &_g_dfu;
Harald Welte14051002017-03-04 19:17:27 +0100160 if ((g_dfu->magic != USB_DFU_MAGIC) && !USBDFU_OverrideEnterDFU()) {
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100161 BootIntoApp();
Harald Welted1e96342017-03-03 00:34:17 +0100162 /* Infinite loop */
163 while ( 1 ) ;
164 }
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100165#endif
166
Christina Quastb0a05702014-11-28 10:27:32 +0100167 /* Initialize the relocate segment */
168 pSrc = &_etext ;
169 pDest = &_srelocate ;
170
171 if ( pSrc != pDest )
172 {
173 for ( ; pDest < &_erelocate ; )
174 {
175 *pDest++ = *pSrc++ ;
176 }
177 }
178
179 /* Clear the zero segment */
180 for ( pDest = &_szero ; pDest < &_ezero ; )
181 {
182 *pDest++ = 0;
183 }
184
185 /* Set the vector table base address */
186 pSrc = (uint32_t *)&_sfixed;
187 SCB->VTOR = ( (uint32_t)pSrc & SCB_VTOR_TBLOFF_Msk ) ;
188
189 if ( ((uint32_t)pSrc >= IRAM_ADDR) && ((uint32_t)pSrc < IRAM_ADDR+IRAM_SIZE) )
190 {
191 SCB->VTOR |= 1 << SCB_VTOR_TBLBASE_Pos ;
192 }
193
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100194 /* App should have disabled interrupts during the transition */
195 __enable_irq();
196
Harald Weltedb17e832017-03-02 23:19:13 +0100197 /* Branch to main function */
198 main() ;
199
Christina Quastb0a05702014-11-28 10:27:32 +0100200 /* Infinite loop */
201 while ( 1 ) ;
202}
203