blob: c46d42ff63b2bda0a98434140eb0fae6baa78b31 [file] [log] [blame]
Christina Quastb0a05702014-11-28 10:27:32 +01001/* ----------------------------------------------------------------------------
2 * ATMEL Microcontroller Software Support
3 * ----------------------------------------------------------------------------
4 * Copyright (c) 2010, Atmel Corporation
Kévin Redon9a12d682018-07-08 13:21:16 +02005 * Copyright (c) 2017, Harald Welte <laforge@gnumonks.org>
6 * Copyright (c) 2018, sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de>
Christina Quastb0a05702014-11-28 10:27:32 +01007 *
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 *
13 * - Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the disclaimer below.
15 *
16 * Atmel's name may not be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
22 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
25 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 * ----------------------------------------------------------------------------
30 */
31
32/*----------------------------------------------------------------------------
33 * Headers
34 *----------------------------------------------------------------------------*/
35
36#include "board.h"
37#include "board_lowlevel.h"
38
39/*----------------------------------------------------------------------------
40 * Exported variables
41 *----------------------------------------------------------------------------*/
42
43/* Stack Configuration */
44#define STACK_SIZE 0x900 /** Stack size (in DWords) */
45__attribute__ ((aligned(8),section(".stack")))
46uint32_t pdwStack[STACK_SIZE] ;
47
48/* Initialize segments */
49extern uint32_t _sfixed;
50extern uint32_t _efixed;
51extern uint32_t _etext;
52extern uint32_t _srelocate;
53extern uint32_t _erelocate;
54extern uint32_t _szero;
55extern uint32_t _ezero;
56
57
58/*----------------------------------------------------------------------------
59 * ProtoTypes
60 *----------------------------------------------------------------------------*/
Christina Quast8be71e42014-12-02 13:06:01 +010061
62/** \cond DOXYGEN_SHOULD_SKIP_THIS */
Christina Quastb0a05702014-11-28 10:27:32 +010063extern int main( void ) ;
Christina Quast8be71e42014-12-02 13:06:01 +010064/** \endcond */
Christina Quastb0a05702014-11-28 10:27:32 +010065void ResetException( void ) ;
Christina Quastb0a05702014-11-28 10:27:32 +010066
67/*------------------------------------------------------------------------------
68 * Exception Table
69 *------------------------------------------------------------------------------*/
70
71__attribute__((section(".vectors")))
72IntFunc exception_table[] = {
73
74 /* Configure Initial Stack Pointer, using linker-generated symbols */
75 (IntFunc)(&pdwStack[STACK_SIZE-1]),
76 ResetException,
77
78 NMI_Handler,
79 HardFault_Handler,
80 MemManage_Handler,
81 BusFault_Handler,
82 UsageFault_Handler,
83 0, 0, 0, 0, /* Reserved */
84 SVC_Handler,
85 DebugMon_Handler,
86 0, /* Reserved */
87 PendSV_Handler,
88 SysTick_Handler,
89
90 /* Configurable interrupts */
91 SUPC_IrqHandler, /* 0 Supply Controller */
92 RSTC_IrqHandler, /* 1 Reset Controller */
93 RTC_IrqHandler, /* 2 Real Time Clock */
94 RTT_IrqHandler, /* 3 Real Time Timer */
95 WDT_IrqHandler, /* 4 Watchdog Timer */
96 PMC_IrqHandler, /* 5 PMC */
97 EEFC_IrqHandler, /* 6 EEFC */
98 IrqHandlerNotUsed, /* 7 Reserved */
99 UART0_IrqHandler, /* 8 UART0 */
100 UART1_IrqHandler, /* 9 UART1 */
101 SMC_IrqHandler, /* 10 SMC */
102 PIOA_IrqHandler, /* 11 Parallel IO Controller A */
103 PIOB_IrqHandler, /* 12 Parallel IO Controller B */
104 PIOC_IrqHandler, /* 13 Parallel IO Controller C */
105 USART0_IrqHandler, /* 14 USART 0 */
106 USART1_IrqHandler, /* 15 USART 1 */
107 IrqHandlerNotUsed, /* 16 Reserved */
108 IrqHandlerNotUsed, /* 17 Reserved */
109 MCI_IrqHandler, /* 18 MCI */
110 TWI0_IrqHandler, /* 19 TWI 0 */
111 TWI1_IrqHandler, /* 20 TWI 1 */
112 SPI_IrqHandler, /* 21 SPI */
113 SSC_IrqHandler, /* 22 SSC */
114 TC0_IrqHandler, /* 23 Timer Counter 0 */
115 TC1_IrqHandler, /* 24 Timer Counter 1 */
116 TC2_IrqHandler, /* 25 Timer Counter 2 */
117 TC3_IrqHandler, /* 26 Timer Counter 3 */
118 TC4_IrqHandler, /* 27 Timer Counter 4 */
119 TC5_IrqHandler, /* 28 Timer Counter 5 */
120 ADC_IrqHandler, /* 29 ADC controller */
121 DAC_IrqHandler, /* 30 DAC controller */
122 PWM_IrqHandler, /* 31 PWM */
123 CRCCU_IrqHandler, /* 32 CRC Calculation Unit */
124 ACC_IrqHandler, /* 33 Analog Comparator */
125 USBD_IrqHandler, /* 34 USB Device Port */
126 IrqHandlerNotUsed /* 35 not used */
127};
128
Harald Welte0af49482017-03-02 23:18:02 +0100129#if defined(BOARD_USB_DFU) && defined(APPLICATION_dfu)
Harald Welted1e96342017-03-03 00:34:17 +0100130#include "usb/device/dfu/dfu.h"
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100131static void BootIntoApp(void)
132{
133 unsigned int *pSrc;
134 void (*appReset)(void);
135
136 pSrc = (unsigned int *) ((unsigned char *)IFLASH_ADDR + BOARD_DFU_BOOT_SIZE);
Kévin Redon9e29a3e2018-07-07 13:48:44 +0200137 /* set vector table to application vector table (store at the beginning of the application) */
138 SCB->VTOR = (unsigned int)(pSrc);
139 /* set stack pointer to address provided in the beginning of the application (loaded into a register first) */
140 __asm__ volatile ("MSR msp,%0" : :"r"(*pSrc));
141 /* start application (by jumping to the reset function which address is stored as second entry of the vector table) */
Kévin Redon76be7c82018-05-21 19:33:50 +0200142 appReset = (void(*)(void))pSrc[1];
Harald Weltecab66412017-03-02 19:22:50 +0100143
Harald Welted1e96342017-03-03 00:34:17 +0100144 g_dfu->state = DFU_STATE_appIDLE;
Harald Weltecab66412017-03-02 19:22:50 +0100145
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100146 appReset();
147}
148#endif
149
Christina Quastb0a05702014-11-28 10:27:32 +0100150/**
151 * \brief This is the code that gets called on processor reset.
152 * To initialize the device, and call the main() routine.
153 */
154void ResetException( void )
155{
156 uint32_t *pSrc, *pDest ;
157
158 /* Low level Initialize */
159 LowLevelInit() ;
160
Harald Welte0af49482017-03-02 23:18:02 +0100161
162#if defined(BOARD_USB_DFU) && defined(APPLICATION_dfu)
Kévin Redon9918c282018-07-07 15:22:16 +0200163 if (!USBDFU_OverrideEnterDFU()) {
164 UART_Exit();
165 __disable_irq();
166 BootIntoApp();
167 /* Infinite loop */
168 while ( 1 ) ;
Harald Welted1e96342017-03-03 00:34:17 +0100169 }
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100170#endif
171
Christina Quastb0a05702014-11-28 10:27:32 +0100172 /* Initialize the relocate segment */
173 pSrc = &_etext ;
174 pDest = &_srelocate ;
175
176 if ( pSrc != pDest )
177 {
178 for ( ; pDest < &_erelocate ; )
179 {
180 *pDest++ = *pSrc++ ;
181 }
182 }
183
184 /* Clear the zero segment */
185 for ( pDest = &_szero ; pDest < &_ezero ; )
186 {
187 *pDest++ = 0;
188 }
189
190 /* Set the vector table base address */
191 pSrc = (uint32_t *)&_sfixed;
192 SCB->VTOR = ( (uint32_t)pSrc & SCB_VTOR_TBLOFF_Msk ) ;
193
194 if ( ((uint32_t)pSrc >= IRAM_ADDR) && ((uint32_t)pSrc < IRAM_ADDR+IRAM_SIZE) )
195 {
196 SCB->VTOR |= 1 << SCB_VTOR_TBLBASE_Pos ;
197 }
198
Harald Welte7ed6f3b2017-02-26 17:00:43 +0100199 /* App should have disabled interrupts during the transition */
200 __enable_irq();
201
Harald Weltedb17e832017-03-02 23:19:13 +0100202 /* Branch to main function */
203 main() ;
204
Christina Quastb0a05702014-11-28 10:27:32 +0100205 /* Infinite loop */
206 while ( 1 ) ;
207}
208