crt1.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /**
  14. * @file ARMCMx/compilers/GCC/crt1.c
  15. * @brief Startup stub functions.
  16. *
  17. * @addtogroup ARMCMx_GCC_STARTUP
  18. * @{
  19. */
  20. #include <stdint.h>
  21. #include <stdbool.h>
  22. #include "cmparams.h"
  23. /*===========================================================================*/
  24. /* Module local definitions. */
  25. /*===========================================================================*/
  26. #if !defined(CRT1_AREAS_NUMBER) || defined(__DOXYGEN__)
  27. #define CRT1_AREAS_NUMBER 8
  28. #endif
  29. #if (CRT1_AREAS_NUMBER < 0) || (CRT1_AREAS_NUMBER > 8)
  30. #error "CRT1_AREAS_NUMBER must be within 0 and 8"
  31. #endif
  32. /*===========================================================================*/
  33. /* Module exported variables. */
  34. /*===========================================================================*/
  35. /*===========================================================================*/
  36. /* Module local types. */
  37. /*===========================================================================*/
  38. /**
  39. * @brief Type of an area to be initialized.
  40. */
  41. typedef struct {
  42. uint32_t *init_text_area;
  43. uint32_t *init_area;
  44. uint32_t *clear_area;
  45. uint32_t *no_init_area;
  46. } ram_init_area_t;
  47. /*===========================================================================*/
  48. /* Module local variables. */
  49. /*===========================================================================*/
  50. #if (CRT1_AREAS_NUMBER > 0) || defined(__DOXYGEN__)
  51. extern uint32_t __ram0_init_text__, __ram0_init__, __ram0_clear__, __ram0_noinit__;
  52. #endif
  53. #if (CRT1_AREAS_NUMBER > 1) || defined(__DOXYGEN__)
  54. extern uint32_t __ram1_init_text__, __ram1_init__, __ram1_clear__, __ram1_noinit__;
  55. #endif
  56. #if (CRT1_AREAS_NUMBER > 2) || defined(__DOXYGEN__)
  57. extern uint32_t __ram2_init_text__, __ram2_init__, __ram2_clear__, __ram2_noinit__;
  58. #endif
  59. #if (CRT1_AREAS_NUMBER > 3) || defined(__DOXYGEN__)
  60. extern uint32_t __ram3_init_text__, __ram3_init__, __ram3_clear__, __ram3_noinit__;
  61. #endif
  62. #if (CRT1_AREAS_NUMBER > 4) || defined(__DOXYGEN__)
  63. extern uint32_t __ram4_init_text__, __ram4_init__, __ram4_clear__, __ram4_noinit__;
  64. #endif
  65. #if (CRT1_AREAS_NUMBER > 5) || defined(__DOXYGEN__)
  66. extern uint32_t __ram5_init_text__, __ram5_init__, __ram5_clear__, __ram5_noinit__;
  67. #endif
  68. #if (CRT1_AREAS_NUMBER > 6) || defined(__DOXYGEN__)
  69. extern uint32_t __ram6_init_text__, __ram6_init__, __ram6_clear__, __ram6_noinit__;
  70. #endif
  71. #if (CRT1_AREAS_NUMBER > 7) || defined(__DOXYGEN__)
  72. extern uint32_t __ram7_init_text__, __ram7_init__, __ram7_clear__, __ram7_noinit__;
  73. #endif
  74. /**
  75. * @brief Static table of areas to be initialized.
  76. */
  77. #if (CRT1_AREAS_NUMBER > 0) || defined(__DOXYGEN__)
  78. static const ram_init_area_t ram_areas[CRT1_AREAS_NUMBER] = {
  79. {&__ram0_init_text__, &__ram0_init__, &__ram0_clear__, &__ram0_noinit__},
  80. #if (CRT1_AREAS_NUMBER > 1) || defined(__DOXYGEN__)
  81. {&__ram1_init_text__, &__ram1_init__, &__ram1_clear__, &__ram1_noinit__},
  82. #endif
  83. #if (CRT1_AREAS_NUMBER > 2) || defined(__DOXYGEN__)
  84. {&__ram2_init_text__, &__ram2_init__, &__ram2_clear__, &__ram2_noinit__},
  85. #endif
  86. #if (CRT1_AREAS_NUMBER > 3) || defined(__DOXYGEN__)
  87. {&__ram3_init_text__, &__ram3_init__, &__ram3_clear__, &__ram3_noinit__},
  88. #endif
  89. #if (CRT1_AREAS_NUMBER > 4) || defined(__DOXYGEN__)
  90. {&__ram4_init_text__, &__ram4_init__, &__ram4_clear__, &__ram4_noinit__},
  91. #endif
  92. #if (CRT1_AREAS_NUMBER > 5) || defined(__DOXYGEN__)
  93. {&__ram5_init_text__, &__ram5_init__, &__ram5_clear__, &__ram5_noinit__},
  94. #endif
  95. #if (CRT1_AREAS_NUMBER > 6) || defined(__DOXYGEN__)
  96. {&__ram6_init_text__, &__ram6_init__, &__ram6_clear__, &__ram6_noinit__},
  97. #endif
  98. #if (CRT1_AREAS_NUMBER > 7) || defined(__DOXYGEN__)
  99. {&__ram7_init_text__, &__ram7_init__, &__ram7_clear__, &__ram7_noinit__},
  100. #endif
  101. };
  102. #endif
  103. /*===========================================================================*/
  104. /* Module local functions. */
  105. /*===========================================================================*/
  106. /*===========================================================================*/
  107. /* Module exported functions. */
  108. /*===========================================================================*/
  109. /**
  110. * @brief Architecture-dependent core initialization.
  111. * @details This hook is invoked immediately after the stack initialization
  112. * and before the DATA and BSS segments initialization.
  113. * @note This function is a weak symbol.
  114. */
  115. #if !defined(__DOXYGEN__)
  116. __attribute__((weak))
  117. #endif
  118. /*lint -save -e9075 [8.4] All symbols are invoked from asm context.*/
  119. void __core_init(void) {
  120. #if CORTEX_MODEL == 7
  121. SCB_EnableICache();
  122. SCB_EnableDCache();
  123. #endif
  124. }
  125. /**
  126. * @brief Early initialization.
  127. * @details This hook is invoked immediately after the stack and core
  128. * initialization and before the DATA and BSS segments
  129. * initialization.
  130. * @note This function is a weak symbol.
  131. */
  132. #if !defined(__DOXYGEN__)
  133. __attribute__((weak))
  134. #endif
  135. /*lint -save -e9075 [8.4] All symbols are invoked from asm context.*/
  136. #ifndef _ARDUPILOT_
  137. void __early_init(void) {}
  138. #endif
  139. /*lint -restore*/
  140. /**
  141. * @brief Late initialization.
  142. * @details This hook is invoked after the DATA and BSS segments
  143. * initialization and before any static constructor. The
  144. * default behavior is to do nothing.
  145. * @note This function is a weak symbol.
  146. */
  147. #if !defined(__DOXYGEN__)
  148. __attribute__((weak))
  149. #endif
  150. /*lint -save -e9075 [8.4] All symbols are invoked from asm context.*/
  151. #ifndef _ARDUPILOT_
  152. void __late_init(void) {}
  153. #endif
  154. /*lint -restore*/
  155. /**
  156. * @brief Default @p main() function exit handler.
  157. * @details This handler is invoked or the @p main() function exit. The
  158. * default behavior is to enter an infinite loop.
  159. * @note This function is a weak symbol.
  160. */
  161. #if !defined(__DOXYGEN__)
  162. __attribute__((noreturn, weak))
  163. #endif
  164. /*lint -save -e9075 [8.4] All symbols are invoked from asm context.*/
  165. void __default_exit(void) {
  166. /*lint -restore*/
  167. while (true) {
  168. }
  169. }
  170. /**
  171. * @brief Performs the initialization of the various RAM areas.
  172. */
  173. void __init_ram_areas(void) {
  174. #if CRT1_AREAS_NUMBER > 0
  175. const ram_init_area_t *rap = ram_areas;
  176. do {
  177. uint32_t *tp = rap->init_text_area;
  178. uint32_t *p = rap->init_area;
  179. /* Copying initialization data.*/
  180. while (p < rap->clear_area) {
  181. *p = *tp;
  182. p++;
  183. tp++;
  184. }
  185. /* Zeroing clear area.*/
  186. while (p < rap->no_init_area) {
  187. *p = 0;
  188. p++;
  189. }
  190. rap++;
  191. }
  192. while (rap < &ram_areas[CRT1_AREAS_NUMBER]);
  193. #endif
  194. }
  195. /** @} */