Christina Quast | b0a0570 | 2014-11-28 10:27:32 +0100 | [diff] [blame] | 1 | /* ----------------------------------------------------------------------------
|
| 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")))
|
| 44 | uint32_t pdwStack[STACK_SIZE] ;
|
| 45 |
|
| 46 | /* Initialize segments */
|
| 47 | extern uint32_t _sfixed;
|
| 48 | extern uint32_t _efixed;
|
| 49 | extern uint32_t _etext;
|
| 50 | extern uint32_t _srelocate;
|
| 51 | extern uint32_t _erelocate;
|
| 52 | extern uint32_t _szero;
|
| 53 | extern uint32_t _ezero;
|
| 54 |
|
| 55 |
|
| 56 | /*----------------------------------------------------------------------------
|
| 57 | * ProtoTypes
|
| 58 | *----------------------------------------------------------------------------*/
|
Christina Quast | 8be71e4 | 2014-12-02 13:06:01 +0100 | [diff] [blame] | 59 |
|
| 60 | /** \cond DOXYGEN_SHOULD_SKIP_THIS */
|
Christina Quast | b0a0570 | 2014-11-28 10:27:32 +0100 | [diff] [blame] | 61 | extern int main( void ) ;
|
Christina Quast | 8be71e4 | 2014-12-02 13:06:01 +0100 | [diff] [blame] | 62 | /** \endcond */
|
Christina Quast | b0a0570 | 2014-11-28 10:27:32 +0100 | [diff] [blame] | 63 | void ResetException( void ) ;
|
Christina Quast | b0a0570 | 2014-11-28 10:27:32 +0100 | [diff] [blame] | 64 |
|
| 65 | /*------------------------------------------------------------------------------
|
| 66 | * Exception Table
|
| 67 | *------------------------------------------------------------------------------*/
|
| 68 |
|
| 69 | __attribute__((section(".vectors")))
|
| 70 | IntFunc 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 Welte | 0af4948 | 2017-03-02 23:18:02 +0100 | [diff] [blame] | 127 | #if defined(BOARD_USB_DFU) && defined(APPLICATION_dfu)
|
Harald Welte | d1e9634 | 2017-03-03 00:34:17 +0100 | [diff] [blame] | 128 | #include "usb/device/dfu/dfu.h"
|
Harald Welte | 7ed6f3b | 2017-02-26 17:00:43 +0100 | [diff] [blame] | 129 | static 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 Welte | cab6641 | 2017-03-02 19:22:50 +0100 | [diff] [blame] | 137 |
|
Harald Welte | d1e9634 | 2017-03-03 00:34:17 +0100 | [diff] [blame] | 138 | g_dfu->state = DFU_STATE_appIDLE;
|
Harald Welte | cab6641 | 2017-03-02 19:22:50 +0100 | [diff] [blame] | 139 |
|
Harald Welte | 7ed6f3b | 2017-02-26 17:00:43 +0100 | [diff] [blame] | 140 | appReset();
|
| 141 | }
|
| 142 | #endif
|
| 143 |
|
Christina Quast | b0a0570 | 2014-11-28 10:27:32 +0100 | [diff] [blame] | 144 | /**
|
| 145 | * \brief This is the code that gets called on processor reset.
|
| 146 | * To initialize the device, and call the main() routine.
|
| 147 | */
|
| 148 | void ResetException( void )
|
| 149 | {
|
| 150 | uint32_t *pSrc, *pDest ;
|
| 151 |
|
| 152 | /* Low level Initialize */
|
| 153 | LowLevelInit() ;
|
| 154 |
|
Harald Welte | 0af4948 | 2017-03-02 23:18:02 +0100 | [diff] [blame] | 155 |
|
| 156 | #if defined(BOARD_USB_DFU) && defined(APPLICATION_dfu)
|
Harald Welte | d1e9634 | 2017-03-03 00:34:17 +0100 | [diff] [blame] | 157 | if (g_dfu->magic != USB_DFU_MAGIC) {
|
Harald Welte | 7ed6f3b | 2017-02-26 17:00:43 +0100 | [diff] [blame] | 158 | BootIntoApp();
|
Harald Welte | d1e9634 | 2017-03-03 00:34:17 +0100 | [diff] [blame] | 159 | /* Infinite loop */
|
| 160 | while ( 1 ) ;
|
| 161 | }
|
Harald Welte | 7ed6f3b | 2017-02-26 17:00:43 +0100 | [diff] [blame] | 162 | #endif
|
| 163 |
|
Christina Quast | b0a0570 | 2014-11-28 10:27:32 +0100 | [diff] [blame] | 164 | /* Initialize the relocate segment */
|
| 165 | pSrc = &_etext ;
|
| 166 | pDest = &_srelocate ;
|
| 167 |
|
| 168 | if ( pSrc != pDest )
|
| 169 | {
|
| 170 | for ( ; pDest < &_erelocate ; )
|
| 171 | {
|
| 172 | *pDest++ = *pSrc++ ;
|
| 173 | }
|
| 174 | }
|
| 175 |
|
| 176 | /* Clear the zero segment */
|
| 177 | for ( pDest = &_szero ; pDest < &_ezero ; )
|
| 178 | {
|
| 179 | *pDest++ = 0;
|
| 180 | }
|
| 181 |
|
| 182 | /* Set the vector table base address */
|
| 183 | pSrc = (uint32_t *)&_sfixed;
|
| 184 | SCB->VTOR = ( (uint32_t)pSrc & SCB_VTOR_TBLOFF_Msk ) ;
|
| 185 |
|
| 186 | if ( ((uint32_t)pSrc >= IRAM_ADDR) && ((uint32_t)pSrc < IRAM_ADDR+IRAM_SIZE) )
|
| 187 | {
|
| 188 | SCB->VTOR |= 1 << SCB_VTOR_TBLBASE_Pos ;
|
| 189 | }
|
| 190 |
|
Harald Welte | 7ed6f3b | 2017-02-26 17:00:43 +0100 | [diff] [blame] | 191 | /* App should have disabled interrupts during the transition */
|
| 192 | __enable_irq();
|
| 193 |
|
Harald Welte | db17e83 | 2017-03-02 23:19:13 +0100 | [diff] [blame] | 194 | /* Branch to main function */
|
| 195 | main() ;
|
| 196 |
|
Christina Quast | b0a0570 | 2014-11-28 10:27:32 +0100 | [diff] [blame] | 197 | /* Infinite loop */
|
| 198 | while ( 1 ) ;
|
| 199 | }
|
| 200 |
|