startup.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. *
  3. * MIT License
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. extern unsigned int _stored_data;
  23. extern unsigned int _start_data;
  24. extern unsigned int _end_data;
  25. extern unsigned int _start_bss;
  26. extern unsigned int _end_bss;
  27. extern unsigned int _end_stack;
  28. extern unsigned int _start_heap;
  29. extern void isr_uart3(void);
  30. #define STACK_PAINTING
  31. static volatile unsigned int avail_mem = 0;
  32. static unsigned int sp;
  33. extern void main(void);
  34. void isr_reset(void) {
  35. register unsigned int *src, *dst;
  36. src = (unsigned int *) &_stored_data;
  37. dst = (unsigned int *) &_start_data;
  38. /* Copy the .data section from flash to RAM. */
  39. while (dst < (unsigned int *)&_end_data) {
  40. *dst = *src;
  41. dst++;
  42. src++;
  43. }
  44. /* Initialize the BSS section to 0 */
  45. dst = &_start_bss;
  46. while (dst < (unsigned int *)&_end_bss) {
  47. *dst = 0U;
  48. dst++;
  49. }
  50. /* Paint the stack. */
  51. avail_mem = &_end_stack - &_start_heap;
  52. #ifdef STACK_PAINTING
  53. {
  54. asm volatile("mrs %0, msp" : "=r"(sp));
  55. dst = ((unsigned int *)(&_end_stack)) - (8192 / sizeof(unsigned int)); ;
  56. while ((unsigned int)dst < sp) {
  57. *dst = 0xDEADC0DE;
  58. dst++;
  59. }
  60. }
  61. #endif
  62. /* Run the program! */
  63. main();
  64. }
  65. void isr_fault(void)
  66. {
  67. /* Panic. */
  68. while(1) ;;
  69. }
  70. void isr_memfault(void)
  71. {
  72. /* Panic. */
  73. while(1) ;;
  74. }
  75. void isr_busfault(void)
  76. {
  77. /* Panic. */
  78. while(1) ;;
  79. }
  80. void isr_usagefault(void)
  81. {
  82. /* Panic. */
  83. while(1) ;;
  84. }
  85. void isr_empty(void)
  86. {
  87. /* Ignore the event and continue */
  88. }
  89. __attribute__ ((section(".isr_vector")))
  90. void (* const IV[])(void) =
  91. {
  92. (void (*)(void))(&_end_stack),
  93. isr_reset, // Reset
  94. isr_fault, // NMI
  95. isr_fault, // HardFault
  96. isr_memfault, // MemFault
  97. isr_busfault, // BusFault
  98. isr_usagefault, // UsageFault
  99. 0, 0, 0, 0, // 4x reserved
  100. isr_empty, // SVC
  101. isr_empty, // DebugMonitor
  102. 0, // reserved
  103. isr_empty, // PendSV
  104. isr_empty, // SysTick
  105. isr_empty, // NVIC_WWDG_IRQ 0
  106. isr_empty, // PVD_IRQ 1
  107. isr_empty, // TAMP_STAMP_IRQ 2
  108. isr_empty, // RTC_WKUP_IRQ 3
  109. isr_empty, // FLASH_IRQ 4
  110. isr_empty, // RCC_IRQ 5
  111. isr_empty, // EXTI0_IRQ 6
  112. isr_empty, // EXTI1_IRQ 7
  113. isr_empty, // EXTI2_IRQ 8
  114. isr_empty, // EXTI3_IRQ 9
  115. isr_empty, // EXTI4_IRQ 10
  116. isr_empty, // DMA1_STREAM0_IRQ 11
  117. isr_empty, // DMA1_STREAM1_IRQ 12
  118. isr_empty, // DMA1_STREAM2_IRQ 13
  119. isr_empty, // DMA1_STREAM3_IRQ 14
  120. isr_empty, // DMA1_STREAM4_IRQ 15
  121. isr_empty, // DMA1_STREAM5_IRQ 16
  122. isr_empty, // DMA1_STREAM6_IRQ 17
  123. isr_empty, // ADC_IRQ 18
  124. isr_empty, // CAN1_TX_IRQ 19
  125. isr_empty, // CAN1_RX0_IRQ 20
  126. isr_empty, // CAN1_RX1_IRQ 21
  127. isr_empty, // CAN1_SCE_IRQ 22
  128. isr_empty, // EXTI9_5_IRQ 23
  129. isr_empty, // TIM1_BRK_TIM9_IRQ 24
  130. isr_empty, // TIM1_UP_TIM10_IRQ 25
  131. isr_empty, // TIM1_TRG_COM_TIM11_IRQ 26
  132. isr_empty, // TIM1_CC_IRQ 27
  133. isr_empty, // TIM2_IRQ 28
  134. isr_empty, // TIM3_IRQ 29
  135. isr_empty, // TIM4_IRQ 30
  136. isr_empty, // I2C1_EV_IRQ 31
  137. isr_empty, // I2C1_ER_IRQ 32
  138. isr_empty, // I2C2_EV_IRQ 33
  139. isr_empty, // I2C2_ER_IRQ 34
  140. isr_empty, // SPI1_IRQ 35
  141. isr_empty, // SPI2_IRQ 36
  142. isr_empty, // USART1_IRQ 37
  143. isr_empty, // USART2_IRQ 38
  144. isr_empty, // USART3_IRQ 39
  145. isr_empty, // EXTI15_10_IRQ 40
  146. isr_empty, // RTC_ALARM_IRQ 41
  147. isr_empty, // USB_FS_WKUP_IRQ 42
  148. isr_empty, // TIM8_BRK_TIM12_IRQ 43
  149. isr_empty, // TIM8_UP_TIM13_IRQ 44
  150. isr_empty, // TIM8_TRG_COM_TIM14_IRQ 45
  151. isr_empty, // TIM8_CC_IRQ 46
  152. isr_empty, // DMA1_STREAM7_IRQ 47
  153. };