gcc_startup_lpc5410x.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. //*****************************************************************************
  2. //
  3. // Startup code for use with GNU tools.
  4. //
  5. //*****************************************************************************
  6. //*****************************************************************************
  7. //
  8. // Forward declaration of the default fault handlers.
  9. //
  10. //*****************************************************************************
  11. static void Reset_Handler(void);
  12. static void Default_Handler(void);
  13. //*****************************************************************************
  14. //
  15. // External declaration for the interrupt handler used by the application.
  16. //
  17. //*****************************************************************************
  18. void NMI_Handler(void) __attribute__((weak, alias("Default_Handler")));
  19. void HardFault_Handler(void) __attribute__((weak, alias("Default_Handler")));
  20. void MemManage_Handler(void) __attribute__((weak, alias("Default_Handler")));
  21. void BusFault_Handler(void) __attribute__((weak, alias("Default_Handler")));
  22. void UsageFault_Handler(void) __attribute__((weak, alias("Default_Handler")));
  23. void SVC_Handler(void) __attribute__((weak, alias("Default_Handler")));
  24. void DebugMon_Handler(void) __attribute__((weak, alias("Default_Handler")));
  25. void PendSV_Handler(void) __attribute__((weak, alias("Default_Handler")));
  26. void SysTick_Handler(void) __attribute__((weak, alias("Default_Handler")));
  27. void WDT_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  28. void BOD_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  29. void Reserved_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  30. void DMA_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  31. void GINT0_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  32. void PIN_INT0_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  33. void PIN_INT1_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  34. void PIN_INT2_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  35. void PIN_INT3_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  36. void UTICK_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  37. void MRT_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  38. void CT32B0_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  39. void CT32B1_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  40. void CT32B2_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  41. void CT32B3_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  42. void CT32B4_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  43. void SCT0_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  44. void UART0_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  45. void UART1_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  46. void UART2_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  47. void UART3_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  48. void I2C0_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  49. void I2C1_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  50. void I2C2_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  51. void SPI0_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  52. void SPI1_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  53. void ADC_SEQA_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  54. void ADC_SEQB_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  55. void ADC_THCMP_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  56. void RTC_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  57. //void Reserved_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  58. void MAILBOX_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  59. void GINT1_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  60. void PIN_INT4_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  61. void PIN_INT5_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  62. void PIN_INT6_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  63. void PIN_INT7_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  64. //void Reserved_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  65. //void Reserved_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  66. //void Reserved_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  67. void RIT_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  68. void Reserved41_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  69. void Reserved42_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  70. void Reserved43_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  71. void Reserved44_IRQHandler(void) __attribute__((weak, alias("Default_Handler")));
  72. //*****************************************************************************
  73. //
  74. // The entry point for the application.
  75. //
  76. //*****************************************************************************
  77. extern int main(void);
  78. //*****************************************************************************
  79. //
  80. // Reserve space for the system stack.
  81. //
  82. //*****************************************************************************
  83. static unsigned long pulStack[512];
  84. //*****************************************************************************
  85. //
  86. // The vector table. Note that the proper constructs must be placed on this to
  87. // ensure that it ends up at physical address 0x0000.0000.
  88. //
  89. //*****************************************************************************
  90. __attribute__ ((section(".isr_vector")))
  91. void (* const g_pfnVectors[])(void) =
  92. {
  93. (void (*)(void))((unsigned long)pulStack + sizeof(pulStack)),
  94. // The initial stack pointer
  95. Reset_Handler, // Reset Handler
  96. NMI_Handler, // NMI Handler
  97. HardFault_Handler, // Hard Fault Handler
  98. MemManage_Handler, // MPU Fault Handler
  99. BusFault_Handler, // Bus Fault Handler
  100. UsageFault_Handler, // Usage Fault Handler
  101. 0, // Reserved
  102. 0, // Reserved
  103. 0, // Reserved
  104. 0, // Reserved
  105. SVC_Handler, // SVCall Handler
  106. DebugMon_Handler, // Debug Monitor Handler
  107. 0, // Reserved
  108. PendSV_Handler, // PendSV Handler
  109. SysTick_Handler, // SysTick Handler
  110. // External Interrupts
  111. WDT_IRQHandler,
  112. BOD_IRQHandler,
  113. Reserved_IRQHandler,
  114. DMA_IRQHandler,
  115. GINT0_IRQHandler,
  116. PIN_INT0_IRQHandler,
  117. PIN_INT1_IRQHandler,
  118. PIN_INT2_IRQHandler,
  119. PIN_INT3_IRQHandler,
  120. UTICK_IRQHandler,
  121. MRT_IRQHandler,
  122. CT32B0_IRQHandler,
  123. CT32B1_IRQHandler,
  124. CT32B2_IRQHandler,
  125. CT32B3_IRQHandler,
  126. CT32B4_IRQHandler,
  127. SCT0_IRQHandler,
  128. UART0_IRQHandler,
  129. UART1_IRQHandler,
  130. UART2_IRQHandler,
  131. UART3_IRQHandler,
  132. I2C0_IRQHandler,
  133. I2C1_IRQHandler,
  134. I2C2_IRQHandler,
  135. SPI0_IRQHandler,
  136. SPI1_IRQHandler,
  137. ADC_SEQA_IRQHandler,
  138. ADC_SEQB_IRQHandler,
  139. ADC_THCMP_IRQHandler,
  140. RTC_IRQHandler,
  141. Reserved_IRQHandler,
  142. MAILBOX_IRQHandler,
  143. GINT1_IRQHandler,
  144. PIN_INT4_IRQHandler,
  145. PIN_INT5_IRQHandler,
  146. PIN_INT6_IRQHandler,
  147. PIN_INT7_IRQHandler,
  148. Reserved_IRQHandler,
  149. Reserved_IRQHandler,
  150. Reserved_IRQHandler,
  151. RIT_IRQHandler,
  152. Reserved41_IRQHandler,
  153. Reserved42_IRQHandler,
  154. Reserved43_IRQHandler,
  155. Reserved44_IRQHandler,
  156. };
  157. //**RIT_IRQHandler ***************************************************************************
  158. // Reserved41_IRQHandler
  159. // TReserved42_IRQHandler he following are constructs created by the linker, indicating where the
  160. // tReserved43_IRQHandler he "data" and "bss" segments reside in memory. The initializers for the
  161. // fReserved44_IRQHandler or the "data" segment resides immediately following the "text" segment.
  162. //
  163. //*****************************************************************************
  164. extern unsigned long _etext;
  165. extern unsigned long _data;
  166. extern unsigned long _edata;
  167. extern unsigned long _bss;
  168. extern unsigned long _ebss;
  169. //*****************************************************************************
  170. //
  171. // This is the code that gets called when the processor first starts execution
  172. // following a reset event. Only the absolutely necessary set is performed,
  173. // after which the application supplied entry() routine is called. Any fancy
  174. // actions (such as making decisions based on the reset cause register, and
  175. // resetting the bits in that register) are left solely in the hands of the
  176. // application.
  177. //
  178. //*****************************************************************************
  179. static void Reset_Handler(void)
  180. {
  181. unsigned long *pulSrc, *pulDest;
  182. //
  183. // Copy the data segment initializers from flash to SRAM.
  184. //
  185. pulSrc = &_etext;
  186. for(pulDest = &_data; pulDest < &_edata; )
  187. {
  188. *pulDest++ = *pulSrc++;
  189. }
  190. #if !defined (__USE_LPCOPEN)
  191. // LPCOpen init code deals with FP and VTOR initialisation
  192. #if defined (__VFP_FP__) && !defined (__SOFTFP__)
  193. /*
  194. * Code to enable the Cortex-M4 FPU only included
  195. * if appropriate build options have been selected.
  196. * Code taken from Section 7.1, Cortex-M4 TRM (DDI0439C)
  197. */
  198. // CPACR is located at address 0xE000ED88
  199. asm("LDR.W R0, =0xE000ED88");
  200. // Read CPACR
  201. asm("LDR R1, [R0]");
  202. // Set bits 20-23 to enable CP10 and CP11 coprocessors
  203. asm(" ORR R1, R1, #(0xF << 20)");
  204. // Write back the modified value to the CPACR
  205. asm("STR R1, [R0]");
  206. #endif // (__VFP_FP__) && !(__SOFTFP__)
  207. // ******************************
  208. // Check to see if we are running the code from a non-zero
  209. // address (eg RAM, external flash), in which case we need
  210. // to modify the VTOR register to tell the CPU that the
  211. // vector table is located at a non-0x0 address.
  212. // Note that we do not use the CMSIS register access mechanism,
  213. // as there is no guarantee that the project has been configured
  214. // to use CMSIS.
  215. unsigned int * pSCB_VTOR = (unsigned int *) 0xE000ED08;
  216. if ((unsigned int *) g_pfnVectors != (unsigned int *) 0x00000000) {
  217. // CMSIS : SCB->VTOR = <address of vector table>
  218. *pSCB_VTOR = (unsigned int) g_pfnVectors;
  219. }
  220. #endif
  221. //
  222. // Zero fill the bss segment.
  223. //
  224. __asm(" ldr r0, =_bss\n"
  225. " ldr r1, =_ebss\n"
  226. " mov r2, #0\n"
  227. " .thumb_func\n"
  228. "zero_loop:\n"
  229. " cmp r0, r1\n"
  230. " it lt\n"
  231. " strlt r2, [r0], #4\n"
  232. " blt zero_loop");
  233. // call system init.
  234. SystemInit();
  235. //
  236. // Call the application's entry point.
  237. //
  238. main();
  239. }
  240. //*****************************************************************************
  241. //
  242. // This is the code that gets called when the processor receives an unexpected
  243. // interrupt. This simply enters an infinite loop, preserving the system state
  244. // for examination by a debugger.
  245. //
  246. //*****************************************************************************
  247. static void Default_Handler(void)
  248. {
  249. //
  250. // Go into an infinite loop.
  251. //
  252. while(1)
  253. {
  254. }
  255. }