Advertisement
Guest User

Cortexm3 startup

a guest
Oct 12th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.26 KB | None | 0 0
  1. /* ---------------------------------------------------------------------------- */
  2. /*                  Atmel Microcontroller Software Support                      */
  3. /*                       SAM Software Package License                           */
  4. /* ---------------------------------------------------------------------------- */
  5. /* Copyright (c) %copyright_year%, Atmel Corporation                                        */
  6. /*                                                                              */
  7. /* All rights reserved.                                                         */
  8. /*                                                                              */
  9. /* Redistribution and use in source and binary forms, with or without           */
  10. /* modification, are permitted provided that the following condition is met:    */
  11. /*                                                                              */
  12. /* - Redistributions of source code must retain the above copyright notice,     */
  13. /* this list of conditions and the disclaimer below.                            */
  14. /*                                                                              */
  15. /* Atmel's name may not be used to endorse or promote products derived from     */
  16. /* this software without specific prior written permission.                     */
  17. /*                                                                              */
  18. /* DISCLAIMER:  THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR   */
  19. /* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
  20. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE   */
  21. /* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,      */
  22. /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */
  23. /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,  */
  24. /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    */
  25. /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING         */
  26. /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, */
  27. /* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                           */
  28. /* ---------------------------------------------------------------------------- */
  29.  
  30. #include "sam3n.h"
  31.  
  32. /* Initialize segments */
  33. extern uint32_t _sfixed;
  34. extern uint32_t _efixed;
  35. extern uint32_t _etext;
  36. extern uint32_t _srelocate;
  37. extern uint32_t _erelocate;
  38. extern uint32_t _szero;
  39. extern uint32_t _ezero;
  40. extern uint32_t _sstack;
  41. extern uint32_t _estack;
  42.  
  43. /** \cond DOXYGEN_SHOULD_SKIP_THIS */
  44. int main(void);
  45. /** \endcond */
  46.  
  47. void __libc_init_array(void);
  48.  
  49. /* Default empty handler */
  50. void Dummy_Handler(void);
  51.  
  52. /* Cortex-M3 core handlers */
  53. void NMI_Handler        ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
  54. void HardFault_Handler  ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
  55. void MemManage_Handler  ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
  56. void BusFault_Handler   ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
  57. void UsageFault_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
  58. void SVC_Handler        ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
  59. void DebugMon_Handler   ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
  60. void PendSV_Handler     ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
  61. void SysTick_Handler    ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
  62.  
  63. /* Peripherals handlers */
  64. void SUPC_Handler   ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
  65. void RSTC_Handler   ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
  66. void RTC_Handler    ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
  67. void RTT_Handler    ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
  68. void WDT_Handler    ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
  69. void PMC_Handler    ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
  70. void EFC_Handler    ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
  71. void UART0_Handler  ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
  72. void UART1_Handler  ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
  73. void PIOA_Handler   ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
  74. void PIOB_Handler   ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
  75. #ifdef _SAM3N_PIOC_INSTANCE_
  76. void PIOC_Handler   ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
  77. #endif /* _SAM3N_PIOC_INSTANCE_ */
  78. void USART0_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
  79. #ifdef _SAM3N_USART1_INSTANCE_
  80. void USART1_Handler ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
  81. #endif /* _SAM3N_USART1_INSTANCE_ */
  82. void TWI0_Handler   ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
  83. void TWI1_Handler   ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
  84. void SPI_Handler    ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
  85. void TC0_Handler    ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
  86. void TC1_Handler    ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
  87. void TC2_Handler    ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
  88. #ifdef _SAM3N_TC1_INSTANCE_
  89. void TC3_Handler    ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
  90. #endif /* _SAM3N_TC1_INSTANCE_ */
  91. #ifdef _SAM3N_TC1_INSTANCE_
  92. void TC4_Handler    ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
  93. #endif /* _SAM3N_TC1_INSTANCE_ */
  94. #ifdef _SAM3N_TC1_INSTANCE_
  95. void TC5_Handler    ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
  96. #endif /* _SAM3N_TC1_INSTANCE_ */
  97. void ADC_Handler    ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
  98. void DACC_Handler   ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
  99. void PWM_Handler    ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
  100.  
  101. /* Exception Table */
  102. __attribute__ ((section(".vectors")))
  103. const DeviceVectors exception_table = {
  104.  
  105.         /* Configure Initial Stack Pointer, using linker-generated symbols */
  106.         .pvStack = (void*) (&_estack),
  107.  
  108.         .pfnReset_Handler      = (void*) Reset_Handler,
  109.         .pfnNMI_Handler        = (void*) NMI_Handler,
  110.         .pfnHardFault_Handler  = (void*) HardFault_Handler,
  111.         .pfnMemManage_Handler  = (void*) MemManage_Handler,
  112.         .pfnBusFault_Handler   = (void*) BusFault_Handler,
  113.         .pfnUsageFault_Handler = (void*) UsageFault_Handler,
  114.         .pfnReserved1_Handler  = (void*) (0UL),          /* Reserved */
  115.         .pfnReserved2_Handler  = (void*) (0UL),          /* Reserved */
  116.         .pfnReserved3_Handler  = (void*) (0UL),          /* Reserved */
  117.         .pfnReserved4_Handler  = (void*) (0UL),          /* Reserved */
  118.         .pfnSVC_Handler        = (void*) SVC_Handler,
  119.         .pfnDebugMon_Handler   = (void*) DebugMon_Handler,
  120.         .pfnReserved5_Handler  = (void*) (0UL),          /* Reserved */
  121.         .pfnPendSV_Handler     = (void*) PendSV_Handler,
  122.         .pfnSysTick_Handler    = (void*) SysTick_Handler,
  123.  
  124.         /* Configurable interrupts */
  125.         .pfnSUPC_Handler   = (void*) SUPC_Handler,   /* 0  Supply Controller */
  126.         .pfnRSTC_Handler   = (void*) RSTC_Handler,   /* 1  Reset Controller */
  127.         .pfnRTC_Handler    = (void*) RTC_Handler,    /* 2  Real Time Clock */
  128.         .pfnRTT_Handler    = (void*) RTT_Handler,    /* 3  Real Time Timer */
  129.         .pfnWDT_Handler    = (void*) WDT_Handler,    /* 4  Watchdog Timer */
  130.         .pfnPMC_Handler    = (void*) PMC_Handler,    /* 5  Power Management Controller */
  131.         .pfnEFC_Handler    = (void*) EFC_Handler,    /* 6  Enhanced Flash Controller */
  132.         .pvReserved7       = (void*) (0UL),          /* 7  Reserved */
  133.         .pfnUART0_Handler  = (void*) UART0_Handler,  /* 8  UART 0 */
  134.         .pfnUART1_Handler  = (void*) UART1_Handler,  /* 9  UART 1 */
  135.         .pvReserved10      = (void*) (0UL),          /* 10 Reserved */
  136.         .pfnPIOA_Handler   = (void*) PIOA_Handler,   /* 11 Parallel I/O Controller A */
  137.         .pfnPIOB_Handler   = (void*) PIOB_Handler,   /* 12 Parallel I/O Controller B */
  138. #ifdef _SAM3N_PIOC_INSTANCE_
  139.         .pfnPIOC_Handler   = (void*) PIOC_Handler,   /* 13 Parallel I/O Controller C */
  140. #else
  141.         .pvReserved13      = (void*) (0UL),          /* 13 Reserved */
  142. #endif /* _SAM3N_PIOC_INSTANCE_ */
  143.         .pfnUSART0_Handler = (void*) USART0_Handler, /* 14 USART 0 */
  144. #ifdef _SAM3N_USART1_INSTANCE_
  145.         .pfnUSART1_Handler = (void*) USART1_Handler, /* 15 USART 1 */
  146. #else
  147.         .pvReserved15      = (void*) (0UL),          /* 15 Reserved */
  148. #endif /* _SAM3N_USART1_INSTANCE_ */
  149.         .pvReserved16      = (void*) (0UL),          /* 16 Reserved */
  150.         .pvReserved17      = (void*) (0UL),          /* 17 Reserved */
  151.         .pvReserved18      = (void*) (0UL),          /* 18 Reserved */
  152.         .pfnTWI0_Handler   = (void*) TWI0_Handler,   /* 19 Two Wire Interface 0 */
  153.         .pfnTWI1_Handler   = (void*) TWI1_Handler,   /* 20 Two Wire Interface 1 */
  154.         .pfnSPI_Handler    = (void*) SPI_Handler,    /* 21 Serial Peripheral Interface */
  155.         .pvReserved22      = (void*) (0UL),          /* 22 Reserved */
  156.         .pfnTC0_Handler    = (void*) TC0_Handler,    /* 23 Timer/Counter 0 */
  157.         .pfnTC1_Handler    = (void*) TC1_Handler,    /* 24 Timer/Counter 1 */
  158.         .pfnTC2_Handler    = (void*) TC2_Handler,    /* 25 Timer/Counter 2 */
  159. #ifdef _SAM3N_TC1_INSTANCE_
  160.         .pfnTC3_Handler    = (void*) TC3_Handler,    /* 26 Timer/Counter 3 */
  161. #else
  162.         .pvReserved26      = (void*) (0UL),          /* 26 Reserved */
  163. #endif /* _SAM3N_TC1_INSTANCE_ */
  164. #ifdef _SAM3N_TC1_INSTANCE_
  165.         .pfnTC4_Handler    = (void*) TC4_Handler,    /* 27 Timer/Counter 4 */
  166. #else
  167.         .pvReserved27      = (void*) (0UL),          /* 27 Reserved */
  168. #endif /* _SAM3N_TC1_INSTANCE_ */
  169. #ifdef _SAM3N_TC1_INSTANCE_
  170.         .pfnTC5_Handler    = (void*) TC5_Handler,    /* 28 Timer/Counter 5 */
  171. #else
  172.         .pvReserved28      = (void*) (0UL),          /* 28 Reserved */
  173. #endif /* _SAM3N_TC1_INSTANCE_ */
  174.         .pfnADC_Handler    = (void*) ADC_Handler,    /* 29 Analog To Digital Converter */
  175.         .pfnDACC_Handler   = (void*) DACC_Handler,   /* 30 Digital To Analog Converter */
  176.         .pfnPWM_Handler    = (void*) PWM_Handler     /* 31 Pulse Width Modulation */
  177. };
  178.  
  179. /**
  180.  * \brief This is the code that gets called on processor reset.
  181.  * To initialize the device, and call the main() routine.
  182.  */
  183. void Reset_Handler(void)
  184. {
  185.         uint32_t *pSrc, *pDest;
  186.  
  187.         /* Initialize the relocate segment */
  188.         pSrc = &_etext;
  189.         pDest = &_srelocate;
  190.  
  191.         if (pSrc != pDest) {
  192.                 for (; pDest < &_erelocate;) {
  193.                         *pDest++ = *pSrc++;
  194.                 }
  195.         }
  196.  
  197.         /* Clear the zero segment */
  198.         for (pDest = &_szero; pDest < &_ezero;) {
  199.                 *pDest++ = 0;
  200.         }
  201.  
  202.         /* Set the vector table base address */
  203.         pSrc = (uint32_t *) & _sfixed;
  204.         SCB->VTOR = ((uint32_t) pSrc & SCB_VTOR_TBLOFF_Msk);
  205.  
  206.         /* Initialize the C library */
  207.         __libc_init_array();
  208.  
  209.         /* Branch to main function */
  210.         main();
  211.  
  212.         /* Infinite loop */
  213.         while (1);
  214. }
  215.  
  216. /**
  217.  * \brief Default interrupt handler for unused IRQs.
  218.  */
  219. void Dummy_Handler(void)
  220. {
  221.         while (1) {
  222.         }
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement