Advertisement
Guest User

stm32f429_startup.d - Build time: 56 seconds.

a guest
May 1st, 2015
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.41 KB | None | 0 0
  1. module startup;
  2.  
  3. version(GNU)
  4. {
  5. static import gcc.attribute; // we need this to get the section, weak and alias attributes
  6. void wfi(){ version(ALLOW_WFI){ asm{ "wfi"; } } }
  7. }
  8. else version(LDC)
  9. {
  10. import ldc.llvmasm;
  11. void wfi(){ version(ALLOW_WFI){ __asm("wfi"); } }
  12. }
  13.  
  14. import core.stdc.config; // we need this for c_ulong, so we can get symbols from the linker-script
  15. import core.stdc.stdint; // these are the types normally used on microcontrollers
  16.  
  17. // Create convenience enums and aliases for the weak and alias attributes:
  18. enum isr_vector = gcc.attribute.attribute("section",".isr_vector.ro");
  19. enum naked = gcc.attribute.attribute("naked");
  20. enum weak = gcc.attribute.attribute("weak");
  21. alias Tuple(A...) = A;
  22. alias weakalias(string A) = Tuple!(weak, gcc.attribute.attribute("alias", A));
  23.  
  24. // The following symbols are provided by our linker-script:
  25. extern(C) extern __gshared c_ulong _stack; // initial stack address
  26. extern(C) extern __gshared c_ulong _siccmram; // pointer to read-only data that needs to be copied to CCMRAM
  27. extern(C) extern __gshared c_ulong _sccmram; // start address of .ccmram section (somewhere within CCMRAM)
  28. extern(C) extern __gshared c_ulong _eccmram; // end address of .ccmram section (somewhere within CCMRAM)
  29. extern(C) extern __gshared c_ulong _sirelocated; // pointer to read-only data that needs to be copied to normal SRAM
  30. extern(C) extern __gshared c_ulong _srelocated; // start address of .relocated section (somewhere within SRAM)
  31. extern(C) extern __gshared c_ulong _erelocated; // end address of .relocated section (somewhere within SRAM)
  32. extern(C) extern __gshared c_ulong _szeroed; // start address of .zeroed section (somewhere within SRAM or CCMRAM)
  33. extern(C) extern __gshared c_ulong _ezeroed; // end address of .zeroed section (somewhere within SRAM or CCMRAM)
  34.  
  35. // Create a convenience alias for our vector functions:
  36. //alias extern(C) const void function() VectorFunc; // currently, this won't work for me
  37. alias extern(C) const void *VectorFunc; // so I'm using a void* instead.
  38.  
  39. @property VectorFuncs(string dsl)() {
  40. static struct A {
  41. struct PTR { string n; @property s() {return `cast(VectorFunc)&`~n;} }
  42. struct PAD { string n; @property s() {return `cast(VectorFunc)null`;} }
  43. struct VAL { string n; @property s() {return `cast(VectorFunc)`~n;} }
  44. struct EXC { string n = `defaultExceptionHandler`; @property s() {return null;} }
  45. mixin(dsl);
  46. }
  47. string code;
  48.  
  49. foreach(I, M; A.init.tupleof)
  50. static if(is(typeof(M)==A.EXC))
  51. code ~= `@weakalias!"`~M.n~`" extern (C) void ` ~ __traits(identifier, A.tupleof[I]) ~ "();\n";
  52.  
  53. code ~= "\n@isr_vector VectorFunc[" ~ A.tupleof.length.stringof ~ "] g_pfnVectors = [\n";
  54. foreach(I, M; A.init.tupleof)
  55. code ~= " " ~ (M.s?M.s:"&"~__traits(identifier, A.tupleof[I])) ~ ",\n";
  56. code ~= "];\n";
  57.  
  58. return code;
  59. }
  60.  
  61. mixin(VectorFuncs!(q{
  62. PTR stack = {`_stack`}; /* -16 $0000 Initial Stack Pointer */
  63. EXC Reset_Handler = {`defaultResetHandler`}; /* -15 $0004 Reset Vector */
  64. EXC NMI_Handler; /* -14 $0008 Non Maskable Interrupt Vector */
  65. EXC HardFault_Handler; /* -13 $000c Hard Fault Vector */
  66. EXC MemManage_Handler; /* -12 $0010 Memory Protection Unit Fault Vector */
  67. EXC BusFault_Handler; /* -11 $0014 Bus Fault Vector */
  68. EXC UsageFault_Handler; /* -10 $0018 Usage Fault Vector */
  69. PAD pad01; /* -9 $001c Reserved */
  70. PAD pad02; /* -8 $0020 Reserved */
  71. PAD pad03; /* -7 $0024 Reserved */
  72. PAD pad04; /* -6 $0028 Reserved */
  73. EXC SVC_Handler; /* -5 $002c SuperVisor Call Vector */
  74. EXC DebugMon_Handler; /* -4 $0030 Debug Monitor Vector */
  75. PAD pad05; /* -3 $0034 Reserved */
  76. EXC PendSV_Handler; /* -2 $0038 Pending SuperVisor Vector */
  77. EXC SysTick_Handler; /* -1 $003c System Tick Vector */
  78.  
  79. EXC WWDG_IRQHandler; /* 0 $0040 Windowed WatchDog */
  80. EXC PVD_IRQHandler; /* 1 $0044 PVD through EXTI Line detection */
  81. EXC TAMP_STAMP_IRQHandler; /* 2 $0048 Tamper and TimeStamps through the EXTI line */
  82. EXC RTC_WKUP_IRQHandler; /* 3 $004c RTC Wakeup through the EXTI line */
  83. EXC FLASH_IRQHandler; /* 4 $0050 FLASH */
  84. EXC RCC_IRQHandler; /* 5 $0054 RCC */
  85. EXC EXTI0_IRQHandler; /* 6 $0058 EXTI Line0 */
  86. EXC EXTI1_IRQHandler; /* 7 $005c EXTI Line1 */
  87. EXC EXTI2_IRQHandler; /* 8 $0060 EXTI Line2 */
  88. EXC EXTI3_IRQHandler; /* 9 $0064 EXTI Line3 */
  89. EXC EXTI4_IRQHandler; /* 10 $0068 EXTI Line4 */
  90. EXC DMA1_Stream0_IRQHandler; /* 11 $006c DMA1 Stream 0 */
  91. EXC DMA1_Stream1_IRQHandler; /* 12 $0070 DMA1 Stream 1 */
  92. EXC DMA1_Stream2_IRQHandler; /* 13 $0074 DMA1 Stream 2 */
  93. EXC DMA1_Stream3_IRQHandler; /* 14 $0078 DMA1 Stream 3 */
  94. EXC DMA1_Stream4_IRQHandler; /* 15 $007c DMA1 Stream 4 */
  95. EXC DMA1_Stream5_IRQHandler; /* 16 $0080 DMA1 Stream 5 */
  96. EXC DMA1_Stream6_IRQHandler; /* 17 $0084 DMA1 Stream 6 */
  97. EXC ADC_IRQHandler; /* 18 $0088 ADC1, ADC2 and ADC3s */
  98. EXC CAN1_TX_IRQHandler; /* 19 $008c CAN1 TX */
  99. EXC CAN1_RX0_IRQHandler; /* 20 $0090 CAN1 RX0 */
  100. EXC CAN1_RX1_IRQHandler; /* 21 $0094 CAN1 RX1 */
  101. EXC CAN1_SCE_IRQHandler; /* 22 $0098 CAN1 SCE */
  102. EXC EXTI9_5_IRQHandler; /* 23 $009c External Line[9:5]s */
  103. EXC TIM1_BRK_TIM9_IRQHandler; /* 24 $00a0 TIM1 Break and TIM9 */
  104. EXC TIM1_UP_TIM10_IRQHandler; /* 25 $00a4 TIM1 Update and TIM10 */
  105. EXC TIM1_TRG_COM_TIM11_IRQHandler; /* 26 $00a8 TIM1 Trigger and Commutation and TIM11 */
  106. EXC TIM1_CC_IRQHandler; /* 27 $00ac TIM1 Capture Compare */
  107. EXC TIM2_IRQHandler; /* 28 $00b0 TIM2 */
  108. EXC TIM3_IRQHandler; /* 29 $00b4 TIM3 */
  109. EXC TIM4_IRQHandler; /* 30 $00b8 TIM4 */
  110. EXC I2C1_EV_IRQHandler; /* 31 $00bc I2C1 Event */
  111. EXC I2C1_ER_IRQHandler; /* 32 $00c0 I2C1 Error */
  112. EXC I2C2_EV_IRQHandler; /* 33 $00c4 I2C2 Event */
  113. EXC I2C2_ER_IRQHandler; /* 34 $00c8 I2C2 Error */
  114. EXC SPI1_IRQHandler; /* 35 $00cc SPI1 */
  115. EXC SPI2_IRQHandler; /* 36 $00d0 SPI2 */
  116. EXC USART1_IRQHandler; /* 37 $00d4 USART1 */
  117. EXC USART2_IRQHandler; /* 38 $00d8 USART2 */
  118. EXC USART3_IRQHandler; /* 39 $00dc USART3 */
  119. EXC EXTI15_10_IRQHandler; /* 40 $00e0 External Line[15:10]s */
  120. EXC RTC_Alarm_IRQHandler; /* 41 $00e4 RTC Alarm (A and B) through EXTI Line */
  121. EXC OTG_FS_WKUP_IRQHandler; /* 42 $00e8 USB OTG FS Wakeup through EXTI line */
  122. EXC TIM8_BRK_TIM12_IRQHandler; /* 43 $00ec TIM8 Break and TIM12 */
  123. EXC TIM8_UP_TIM13_IRQHandler; /* 44 $00f0 TIM8 Update and TIM13 */
  124. EXC TIM8_TRG_COM_TIM14_IRQHandler; /* 45 $00f4 TIM8 Trigger and Commutation and TIM14 */
  125. EXC TIM8_CC_IRQHandler; /* 46 $00f8 TIM8 Capture Compare */
  126. EXC DMA1_Stream7_IRQHandler; /* 47 $00fc DMA1 Stream7 */
  127. EXC FMC_IRQHandler; /* 48 $0100 FMC */
  128. EXC SDIO_IRQHandler; /* 49 $0104 SDIO */
  129. EXC TIM5_IRQHandler; /* 50 $0108 TIM5 */
  130. EXC SPI3_IRQHandler; /* 51 $010c SPI3 */
  131. EXC UART4_IRQHandler; /* 52 $0110 UART4 */
  132. EXC UART5_IRQHandler; /* 53 $0114 UART5 */
  133. EXC TIM6_DAC_IRQHandler; /* 54 $0118 TIM6 and DAC1&2 underrun errors */
  134. EXC TIM7_IRQHandler; /* 55 $011c TIM7 */
  135. EXC DMA2_Stream0_IRQHandler; /* 56 $0120 DMA2 Stream 0 */
  136. EXC DMA2_Stream1_IRQHandler; /* 57 $0124 DMA2 Stream 1 */
  137. EXC DMA2_Stream2_IRQHandler; /* 58 $0128 DMA2 Stream 2 */
  138. EXC DMA2_Stream3_IRQHandler; /* 59 $012c DMA2 Stream 3 */
  139. EXC DMA2_Stream4_IRQHandler; /* 60 $0130 DMA2 Stream 4 */
  140. EXC ETH_IRQHandler; /* 61 $0134 Ethernet */
  141. EXC ETH_WKUP_IRQHandler; /* 62 $0138 Ethernet Wakeup through EXTI line */
  142. EXC CAN2_TX_IRQHandler; /* 63 $013c CAN2 TX */
  143. EXC CAN2_RX0_IRQHandler; /* 64 $0140 CAN2 RX0 */
  144. EXC CAN2_RX1_IRQHandler; /* 65 $0144 CAN2 RX1 */
  145. EXC CAN2_SCE_IRQHandler; /* 66 $0148 CAN2 SCE */
  146. EXC OTG_FS_IRQHandler; /* 67 $014c USB OTG FS */
  147. EXC DMA2_Stream5_IRQHandler; /* 68 $0150 DMA2 Stream 5 */
  148. EXC DMA2_Stream6_IRQHandler; /* 69 $0154 DMA2 Stream 6 */
  149. EXC DMA2_Stream7_IRQHandler; /* 70 $0158 DMA2 Stream 7 */
  150. EXC USART6_IRQHandler; /* 71 $015c USART6 */
  151. EXC I2C3_EV_IRQHandler; /* 72 $0160 I2C3 event */
  152. EXC I2C3_ER_IRQHandler; /* 73 $0164 I2C3 error */
  153. EXC OTG_HS_EP1_OUT_IRQHandler; /* 74 $0168 USB OTG HS End Point 1 Out */
  154. EXC OTG_HS_EP1_IN_IRQHandler; /* 75 $016c USB OTG HS End Point 1 In */
  155. EXC OTG_HS_WKUP_IRQHandler; /* 76 $0170 USB OTG HS Wakeup through EXTI */
  156. EXC OTG_HS_IRQHandler; /* 77 $0174 USB OTG HS */
  157. EXC DCMI_IRQHandler; /* 78 $0178 DCMI */
  158. PAD pad06; /* 79 $017c Reserved */
  159. EXC HASH_RNG_IRQHandler; /* 80 $0180 Hash and Rng */
  160. EXC FPU_IRQHandler; /* 81 $0184 FPU */
  161. EXC UART7_IRQHandler; /* 82 $0188 UART7 */
  162. EXC UART8_IRQHandler; /* 83 $018c UART8 */
  163. EXC SPI4_IRQHandler; /* 84 $0190 SPI4 */
  164. EXC SPI5_IRQHandler; /* 85 $0194 SPI5 */
  165. EXC SPI6_IRQHandler; /* 86 $0198 SPI6 */
  166. EXC SAI1_IRQHandler; /* 87 $019c SAI1 */
  167. EXC LTDC_IRQHandler; /* 88 $01a0 LTDC */
  168. EXC LTDC_ER_IRQHandler; /* 89 $01a4 LTDC Error */
  169. EXC DMA2D_IRQHandler; /* 90 $01a8 DMA2D */
  170. }));
  171.  
  172. @weak extern(C) void LowLevelInit();
  173. @weak extern(C) void SystemInit();
  174. @weak extern(C) void __libc_init_array();
  175. @weak extern(C) extern __gshared c_ulong SystemCoreClock;
  176. extern(C) void main();
  177.  
  178. void copyBlock(const(void) *aSource, void *aDestination, void *aDestinationEnd)
  179. {
  180. const(uint32_t) *s = cast(const(uint32_t) *)aSource;
  181. uint32_t *d = cast(uint32_t *)aDestination;
  182. uint32_t *e = cast(uint32_t *)aDestinationEnd;
  183.  
  184. while(d < e)
  185. {
  186. *d++ = *s++;
  187. }
  188. }
  189.  
  190. void zeroBlock(void *aDestination, void *aDestinationEnd)
  191. {
  192. uint32_t *d = cast(uint32_t *)aDestination;
  193. uint32_t *e = cast(uint32_t *)aDestinationEnd;
  194.  
  195. while(d < e)
  196. {
  197. *d++ = 0;
  198. }
  199. }
  200.  
  201. @naked extern(C) void defaultResetHandler() /* we can mark this naked, as it never returns and should never save any registers on the stack */
  202. {
  203. uint32_t saveFreq;
  204.  
  205. LowLevelInit();
  206. SystemInit();
  207.  
  208. saveFreq = SystemCoreClock;
  209. copyBlock(&_siccmram, &_sccmram, &_eccmram);
  210. copyBlock(&_sirelocated, &_srelocated, &_erelocated);
  211. zeroBlock(&_szeroed, &_ezeroed);
  212. __libc_init_array();
  213. if(&SystemCoreClock) SystemCoreClock = saveFreq;
  214.  
  215. main();
  216. defaultExceptionHandler();
  217. }
  218.  
  219. @naked extern(C) void defaultExceptionHandler()
  220. {
  221. while(true)
  222. {
  223. wfi();
  224. }
  225. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement