arm_startup.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /* arm_startup.c
  2. *
  3. * Copyright (C) 2006-2022 wolfSSL Inc.
  4. *
  5. * This file is part of wolfSSL.
  6. *
  7. * wolfSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * wolfSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
  20. */
  21. #include "hw.h"
  22. #include <stdio.h>
  23. // Memory initialization
  24. extern uint32_t __data_load_start__[];
  25. extern uint32_t __data_start__[];
  26. extern uint32_t __data_end__[];
  27. extern uint32_t __bss_start__[];
  28. extern uint32_t __bss_end__[];
  29. extern uint32_t __fast_load_start__[];
  30. extern uint32_t __fast_start__[];
  31. extern uint32_t __fast_end__[];
  32. extern uint32_t __stack_process_end__[];
  33. extern uint32_t __heap_start__[];
  34. extern uint32_t __heap_end__[];
  35. // Copy memory: src=Source, dst_beg=Destination Begin, dst_end=Destination End
  36. void memcpy32(uint32_t* src, uint32_t* dst_beg, uint32_t* dst_end)
  37. {
  38. while (dst_beg < dst_end) {
  39. *dst_beg++ = *src++;
  40. }
  41. }
  42. // Zero address in range
  43. void meminit32(uint32_t* start, uint32_t* end)
  44. {
  45. while (start < end) {
  46. *start++ = 0;
  47. }
  48. }
  49. // Entry Point
  50. void reset_handler(void)
  51. {
  52. // Disable Watchdog
  53. hw_watchdog_disable();
  54. // Init sections
  55. memcpy32(__data_load_start__, __data_start__, __data_end__);
  56. meminit32(__bss_start__, __bss_end__);
  57. memcpy32(__fast_load_start__, __fast_start__, __fast_end__);
  58. // Init heap
  59. __heap_start__[0] = 0;
  60. __heap_start__[1] = ((uint32_t)__heap_end__ - (uint32_t)__heap_start__);
  61. // Init hardware
  62. hw_init();
  63. // Start main
  64. extern void main(void);
  65. main();
  66. // Application has ended, so busy wait
  67. while(1);
  68. }
  69. // Vector Exception/Interrupt Handlers
  70. static void Default_Handler(void)
  71. {
  72. /* If we get here then need to implement real IRQ handler */
  73. while(1);
  74. }
  75. void HardFault_HandlerC( uint32_t *hardfault_args )
  76. {
  77. /* These are volatile to try and prevent the compiler/linker optimizing them
  78. away as the variables never actually get used. If the debugger won't show the
  79. values of the variables, make them global my moving their declaration outside
  80. of this function. */
  81. volatile uint32_t stacked_r0;
  82. volatile uint32_t stacked_r1;
  83. volatile uint32_t stacked_r2;
  84. volatile uint32_t stacked_r3;
  85. volatile uint32_t stacked_r12;
  86. volatile uint32_t stacked_lr;
  87. volatile uint32_t stacked_pc;
  88. volatile uint32_t stacked_psr;
  89. volatile uint32_t _CFSR;
  90. volatile uint32_t _HFSR;
  91. volatile uint32_t _DFSR;
  92. volatile uint32_t _AFSR;
  93. volatile uint32_t _BFAR;
  94. volatile uint32_t _MMAR;
  95. stacked_r0 = ((uint32_t)hardfault_args[0]);
  96. stacked_r1 = ((uint32_t)hardfault_args[1]);
  97. stacked_r2 = ((uint32_t)hardfault_args[2]);
  98. stacked_r3 = ((uint32_t)hardfault_args[3]);
  99. stacked_r12 = ((uint32_t)hardfault_args[4]);
  100. stacked_lr = ((uint32_t)hardfault_args[5]);
  101. stacked_pc = ((uint32_t)hardfault_args[6]);
  102. stacked_psr = ((uint32_t)hardfault_args[7]);
  103. // Configurable Fault Status Register
  104. // Consists of MMSR, BFSR and UFSR
  105. _CFSR = (*((volatile uint32_t *)(0xE000ED28)));
  106. // Hard Fault Status Register
  107. _HFSR = (*((volatile uint32_t *)(0xE000ED2C)));
  108. // Debug Fault Status Register
  109. _DFSR = (*((volatile uint32_t *)(0xE000ED30)));
  110. // Auxiliary Fault Status Register
  111. _AFSR = (*((volatile uint32_t *)(0xE000ED3C)));
  112. // Read the Fault Address Registers. These may not contain valid values.
  113. // Check BFARVALID/MMARVALID to see if they are valid values
  114. // MemManage Fault Address Register
  115. _MMAR = (*((volatile uint32_t *)(0xE000ED34)));
  116. // Bus Fault Address Register
  117. _BFAR = (*((volatile uint32_t *)(0xE000ED38)));
  118. printf ("\n\nHard fault handler (all numbers in hex):\n");
  119. printf ("R0 = %x\n", stacked_r0);
  120. printf ("R1 = %x\n", stacked_r1);
  121. printf ("R2 = %x\n", stacked_r2);
  122. printf ("R3 = %x\n", stacked_r3);
  123. printf ("R12 = %x\n", stacked_r12);
  124. printf ("LR [R14] = %x subroutine call return address\n", stacked_lr);
  125. printf ("PC [R15] = %x program counter\n", stacked_pc);
  126. printf ("PSR = %x\n", stacked_psr);
  127. printf ("CFSR = %x\n", _CFSR);
  128. printf ("HFSR = %x\n", _HFSR);
  129. printf ("DFSR = %x\n", _DFSR);
  130. printf ("AFSR = %x\n", _AFSR);
  131. printf ("MMAR = %x\n", _MMAR);
  132. printf ("BFAR = %x\n", _BFAR);
  133. // Break into the debugger
  134. __asm("BKPT #0\n");
  135. }
  136. __attribute__( ( naked ) )
  137. void HardFault_Handler(void)
  138. {
  139. __asm volatile
  140. (
  141. " tst lr, #4 \n"
  142. " ite eq \n"
  143. " mrseq r0, msp \n"
  144. " mrsne r0, psp \n"
  145. " ldr r1, [r0, #24] \n"
  146. " ldr r2, handler2_address_const \n"
  147. " bx r2 \n"
  148. " handler2_address_const: .word HardFault_HandlerC \n"
  149. );
  150. }
  151. // Vectors
  152. typedef void (*vector_entry)(void);
  153. const vector_entry vectors[] __attribute__ ((section(".vectors"),used)) =
  154. {
  155. /* Interrupt Vector Table Function Pointers */
  156. // Address Vector IRQ Source module Source description
  157. (vector_entry)__stack_process_end__, // ARM core Initial Supervisor SP
  158. reset_handler, // 0x0000_0004 1 - ARM core Initial Program Counter
  159. Default_Handler, // 0x0000_0008 2 - ARM core Non-maskable Interrupt (NMI)
  160. HardFault_Handler, // 0x0000_000C 3 - ARM core Hard Fault
  161. Default_Handler, // 0x0000_0010 4 -
  162. HardFault_Handler, // 0x0000_0014 5 - ARM core Bus Fault
  163. HardFault_Handler, // 0x0000_0018 6 - ARM core Usage Fault
  164. Default_Handler, // 0x0000_001C 7 -
  165. Default_Handler, // 0x0000_0020 8 -
  166. Default_Handler, // 0x0000_0024 9 -
  167. Default_Handler, // 0x0000_0028 10 -
  168. Default_Handler, // 0x0000_002C 11 - ARM core Supervisor call (SVCall)
  169. Default_Handler, // 0x0000_0030 12 - ARM core Debug Monitor
  170. Default_Handler, // 0x0000_0034 13 -
  171. Default_Handler, // 0x0000_0038 14 - ARM core Pendable request for system service (PendableSrvReq)
  172. Default_Handler, // 0x0000_003C 15 - ARM core System tick timer (SysTick)
  173. #ifdef CPU_MK82FN256VLL15
  174. // Add specific driver interrupt handlers below
  175. Default_Handler, /* DMA0_DMA16_IRQn = 0, /**< DMA channel 0,16 transfer complete */
  176. Default_Handler, /* DMA1_DMA17_IRQn = 1, /**< DMA channel 1,17 transfer complete */
  177. Default_Handler, /* DMA2_DMA18_IRQn = 2, /**< DMA channel 2,18 transfer complete */
  178. Default_Handler, /* DMA3_DMA19_IRQn = 3, /**< DMA channel 3,19 transfer complete */
  179. Default_Handler, /* DMA4_DMA20_IRQn = 4, /**< DMA channel 4,20 transfer complete */
  180. Default_Handler, /* DMA5_DMA21_IRQn = 5, /**< DMA channel 5,21 transfer complete */
  181. Default_Handler, /* DMA6_DMA22_IRQn = 6, /**< DMA channel 6,22 transfer complete */
  182. Default_Handler, /* DMA7_DMA23_IRQn = 7, /**< DMA channel 7,23 transfer complete */
  183. Default_Handler, /* DMA8_DMA24_IRQn = 8, /**< DMA channel 8,24 transfer complete */
  184. Default_Handler, /* DMA9_DMA25_IRQn = 9, /**< DMA channel 9,25 transfer complete */
  185. Default_Handler, /* DMA10_DMA26_IRQn = 10, /**< DMA channel 10,26 transfer complete */
  186. Default_Handler, /* DMA11_DMA27_IRQn = 11, /**< DMA channel 11,27 transfer complete */
  187. Default_Handler, /* DMA12_DMA28_IRQn = 12, /**< DMA channel 12,28 transfer complete */
  188. Default_Handler, /* DMA13_DMA29_IRQn = 13, /**< DMA channel 13,29 transfer complete */
  189. Default_Handler, /* DMA14_DMA30_IRQn = 14, /**< DMA channel 14,30 transfer complete */
  190. Default_Handler, /* DMA15_DMA31_IRQn = 15, /**< DMA channel 15,31 transfer complete */
  191. Default_Handler, /* DMA_Error_IRQn = 16, /**< DMA channel 0 - 31 error */
  192. Default_Handler, /* MCM_IRQn = 17, /**< MCM normal interrupt */
  193. Default_Handler, /* FTFA_IRQn = 18, /**< FTFA command complete */
  194. Default_Handler, /* Read_Collision_IRQn = 19, /**< FTFA read collision */
  195. Default_Handler, /* LVD_LVW_IRQn = 20, /**< PMC controller low-voltage detect, low-voltage warning */
  196. Default_Handler, /* LLWU_IRQn = 21, /**< Low leakage wakeup unit */
  197. Default_Handler, /* WDOG_EWM_IRQn = 22, /**< Single interrupt vector for WDOG and EWM */
  198. Default_Handler, /* TRNG0_IRQn = 23, /**< True randon number generator */
  199. Default_Handler, /* I2C0_IRQn = 24, /**< Inter-integrated circuit 0 */
  200. Default_Handler, /* I2C1_IRQn = 25, /**< Inter-integrated circuit 1 */
  201. Default_Handler, /* SPI0_IRQn = 26, /**< Serial peripheral Interface 0 */
  202. Default_Handler, /* SPI1_IRQn = 27, /**< Serial peripheral Interface 1 */
  203. Default_Handler, /* I2S0_Tx_IRQn = 28, /**< Integrated interchip sound 0 transmit interrupt */
  204. Default_Handler, /* I2S0_Rx_IRQn = 29, /**< Integrated interchip sound 0 receive interrupt */
  205. Default_Handler, /* LPUART0_IRQn = 30, /**< LPUART0 receive/transmit/error interrupt */
  206. Default_Handler, /* LPUART1_IRQn = 31, /**< LPUART1 receive/transmit/error interrupt */
  207. Default_Handler, /* LPUART2_IRQn = 32, /**< LPUART2 receive/transmit/error interrupt */
  208. Default_Handler, /* LPUART3_IRQn = 33, /**< LPUART3 receive/transmit/error interrupt */
  209. Default_Handler, /* LPUART4_IRQn = 34, /**< LPUART4 receive/transmit/error interrupt */
  210. Default_Handler, /* Reserved51_IRQn = 35, /**< Reserved interrupt */
  211. Default_Handler, /* Reserved52_IRQn = 36, /**< Reserved interrupt */
  212. Default_Handler, /* EMVSIM0_IRQn = 37, /**< EMVSIM0 common interrupt */
  213. Default_Handler, /* EMVSIM1_IRQn = 38, /**< EMVSIM1 common interrupt */
  214. Default_Handler, /* ADC0_IRQn = 39, /**< Analog-to-digital converter 0 */
  215. Default_Handler, /* CMP0_IRQn = 40, /**< Comparator 0 */
  216. Default_Handler, /* CMP1_IRQn = 41, /**< Comparator 1 */
  217. Default_Handler, /* FTM0_IRQn = 42, /**< FlexTimer module 0 fault, overflow and channels interrupt */
  218. Default_Handler, /* FTM1_IRQn = 43, /**< FlexTimer module 1 fault, overflow and channels interrupt */
  219. Default_Handler, /* FTM2_IRQn = 44, /**< FlexTimer module 2 fault, overflow and channels interrupt */
  220. Default_Handler, /* CMT_IRQn = 45, /**< Carrier modulator transmitter */
  221. Default_Handler, /* RTC_IRQn = 46, /**< Real time clock */
  222. Default_Handler, /* RTC_Seconds_IRQn = 47, /**< Real time clock seconds */
  223. Default_Handler, /* PIT0CH0_IRQn = 48, /**< Periodic interrupt timer 0 channel 0 */
  224. Default_Handler, /* PIT0CH1_IRQn = 49, /**< Periodic interrupt timer 0 channel 1 */
  225. Default_Handler, /* PIT0CH2_IRQn = 50, /**< Periodic interrupt timer 0 channel 2 */
  226. Default_Handler, /* PIT0CH3_IRQn = 51, /**< Periodic interrupt timer 0 channel 3 */
  227. Default_Handler, /* PDB0_IRQn = 52, /**< Programmable delay block */
  228. Default_Handler, /* USB0_IRQn = 53, /**< USB OTG interrupt */
  229. Default_Handler, /* USBDCD_IRQn = 54, /**< USB charger detect */
  230. Default_Handler, /* Reserved71_IRQn = 55, /**< Reserved interrupt */
  231. Default_Handler, /* DAC0_IRQn = 56, /**< Digital-to-analog converter 0 */
  232. Default_Handler, /* MCG_IRQn = 57, /**< Multipurpose clock generator */
  233. Default_Handler, /* LPTMR0_LPTMR1_IRQn = 58, /**< Single interrupt vector for Low Power Timer 0 and 1 */
  234. Default_Handler, /* PORTA_IRQn = 59, /**< Port A pin detect interrupt */
  235. Default_Handler, /* PORTB_IRQn = 60, /**< Port B pin detect interrupt */
  236. Default_Handler, /* PORTC_IRQn = 61, /**< Port C pin detect interrupt */
  237. Default_Handler, /* PORTD_IRQn = 62, /**< Port D pin detect interrupt */
  238. Default_Handler, /* PORTE_IRQn = 63, /**< Port E pin detect interrupt */
  239. Default_Handler, /* SWI_IRQn = 64, /**< Software interrupt */
  240. Default_Handler, /* SPI2_IRQn = 65, /**< Serial peripheral Interface 2 */
  241. Default_Handler, /* Reserved82_IRQn = 66, /**< Reserved interrupt */
  242. Default_Handler, /* Reserved83_IRQn = 67, /**< Reserved interrupt */
  243. Default_Handler, /* Reserved84_IRQn = 68, /**< Reserved interrupt */
  244. Default_Handler, /* Reserved85_IRQn = 69, /**< Reserved interrupt */
  245. Default_Handler, /* FLEXIO0_IRQn = 70, /**< FLEXIO0 */
  246. Default_Handler, /* FTM3_IRQn = 71, /**< FlexTimer module 3 fault, overflow and channels interrupt */
  247. Default_Handler, /* Reserved88_IRQn = 72, /**< Reserved interrupt */
  248. Default_Handler, /* Reserved89_IRQn = 73, /**< Reserved interrupt */
  249. Default_Handler, /* I2C2_IRQn = 74, /**< Inter-integrated circuit 2 */
  250. Default_Handler, /* Reserved91_IRQn = 75, /**< Reserved interrupt */
  251. Default_Handler, /* Reserved92_IRQn = 76, /**< Reserved interrupt */
  252. Default_Handler, /* Reserved93_IRQn = 77, /**< Reserved interrupt */
  253. Default_Handler, /* Reserved94_IRQn = 78, /**< Reserved interrupt */
  254. Default_Handler, /* Reserved95_IRQn = 79, /**< Reserved interrupt */
  255. Default_Handler, /* Reserved96_IRQn = 80, /**< Reserved interrupt */
  256. Default_Handler, /* SDHC_IRQn = 81, /**< Secured digital host controller */
  257. Default_Handler, /* Reserved98_IRQn = 82, /**< Reserved interrupt */
  258. Default_Handler, /* Reserved99_IRQn = 83, /**< Reserved interrupt */
  259. Default_Handler, /* Reserved100_IRQn = 84, /**< Reserved interrupt */
  260. Default_Handler, /* Reserved101_IRQn = 85, /**< Reserved interrupt */
  261. Default_Handler, /* Reserved102_IRQn = 86, /**< Reserved interrupt */
  262. Default_Handler, /* TSI0_IRQn = 87, /**< Touch Sensing Input */
  263. Default_Handler, /* TPM1_IRQn = 88, /**< TPM1 single interrupt vector for all sources */
  264. Default_Handler, /* TPM2_IRQn = 89, /**< TPM2 single interrupt vector for all sources */
  265. Default_Handler, /* Reserved106_IRQn = 90, /**< Reserved interrupt */
  266. Default_Handler, /* I2C3_IRQn = 91, /**< Inter-integrated circuit 3 */
  267. Default_Handler, /* Reserved108_IRQn = 92, /**< Reserved interrupt */
  268. Default_Handler, /* Reserved109_IRQn = 93, /**< Reserved interrupt */
  269. Default_Handler, /* Reserved110_IRQn = 94, /**< Reserved interrupt */
  270. Default_Handler, /* Reserved111_IRQn = 95, /**< Reserved interrupt */
  271. Default_Handler, /* Reserved112_IRQn = 96, /**< Reserved interrupt */
  272. Default_Handler, /* Reserved113_IRQn = 97, /**< Reserved interrupt */
  273. Default_Handler, /* Reserved114_IRQn = 98, /**< Reserved interrupt */
  274. Default_Handler, /* Reserved115_IRQn = 99, /**< Reserved interrupt */
  275. Default_Handler, /* QuadSPI0_IRQn = 100, /**< qspi */
  276. Default_Handler, /* Reserved117_IRQn = 101, /**< Reserved interrupt */
  277. Default_Handler, /* Reserved118_IRQn = 102, /**< Reserved interrupt */
  278. Default_Handler, /* Reserved119_IRQn = 103, /**< Reserved interrupt */
  279. Default_Handler, /* LTC0_IRQn = 104, /**< LP Trusted Cryptography */
  280. Default_Handler, /* Reserved121_IRQn = 105, /**< Reserved interrupt */
  281. Default_Handler, /* Reserved122_IRQn = 106 /**< Reserved interrupt */
  282. #endif /* CPU_MK82FN256VLL15 */
  283. };