startup_ARMCA7.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /******************************************************************************
  2. * @file startup_ARMCA7.c
  3. * @brief CMSIS Device System Source File for Arm Cortex-A7 Device Series
  4. * @version V1.0.1
  5. * @date 10. January 2021
  6. ******************************************************************************/
  7. /*
  8. * Copyright (c) 2009-2021 Arm Limited. All rights reserved.
  9. *
  10. * SPDX-License-Identifier: Apache-2.0
  11. *
  12. * Licensed under the Apache License, Version 2.0 (the License); you may
  13. * not use this file except in compliance with the License.
  14. * You may obtain a copy of the License at
  15. *
  16. * www.apache.org/licenses/LICENSE-2.0
  17. *
  18. * Unless required by applicable law or agreed to in writing, software
  19. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  20. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  21. * See the License for the specific language governing permissions and
  22. * limitations under the License.
  23. */
  24. #include <ARMCA7.h>
  25. /*----------------------------------------------------------------------------
  26. Definitions
  27. *----------------------------------------------------------------------------*/
  28. #define USR_MODE 0x10 // User mode
  29. #define FIQ_MODE 0x11 // Fast Interrupt Request mode
  30. #define IRQ_MODE 0x12 // Interrupt Request mode
  31. #define SVC_MODE 0x13 // Supervisor mode
  32. #define ABT_MODE 0x17 // Abort mode
  33. #define UND_MODE 0x1B // Undefined Instruction mode
  34. #define SYS_MODE 0x1F // System mode
  35. /*----------------------------------------------------------------------------
  36. Internal References
  37. *----------------------------------------------------------------------------*/
  38. void Vectors (void) __attribute__ ((naked, section("RESET")));
  39. void Reset_Handler (void) __attribute__ ((naked));
  40. void Default_Handler(void) __attribute__ ((noreturn));
  41. /*----------------------------------------------------------------------------
  42. Exception / Interrupt Handler
  43. *----------------------------------------------------------------------------*/
  44. void Undef_Handler (void) __attribute__ ((weak, alias("Default_Handler")));
  45. void SVC_Handler (void) __attribute__ ((weak, alias("Default_Handler")));
  46. void PAbt_Handler (void) __attribute__ ((weak, alias("Default_Handler")));
  47. void DAbt_Handler (void) __attribute__ ((weak, alias("Default_Handler")));
  48. void IRQ_Handler (void) __attribute__ ((weak, alias("Default_Handler")));
  49. void FIQ_Handler (void) __attribute__ ((weak, alias("Default_Handler")));
  50. /*----------------------------------------------------------------------------
  51. Exception / Interrupt Vector Table
  52. *----------------------------------------------------------------------------*/
  53. void Vectors(void) {
  54. __ASM volatile(
  55. "LDR PC, =Reset_Handler \n"
  56. "LDR PC, =Undef_Handler \n"
  57. "LDR PC, =SVC_Handler \n"
  58. "LDR PC, =PAbt_Handler \n"
  59. "LDR PC, =DAbt_Handler \n"
  60. "NOP \n"
  61. "LDR PC, =IRQ_Handler \n"
  62. "LDR PC, =FIQ_Handler \n"
  63. );
  64. }
  65. /*----------------------------------------------------------------------------
  66. Reset Handler called on controller reset
  67. *----------------------------------------------------------------------------*/
  68. void Reset_Handler(void) {
  69. __ASM volatile(
  70. // Mask interrupts
  71. "CPSID if \n"
  72. // Put any cores other than 0 to sleep
  73. "MRC p15, 0, R0, c0, c0, 5 \n" // Read MPIDR
  74. "ANDS R0, R0, #3 \n"
  75. "goToSleep: \n"
  76. "WFINE \n"
  77. "BNE goToSleep \n"
  78. // Reset SCTLR Settings
  79. "MRC p15, 0, R0, c1, c0, 0 \n" // Read CP15 System Control register
  80. "BIC R0, R0, #(0x1 << 12) \n" // Clear I bit 12 to disable I Cache
  81. "BIC R0, R0, #(0x1 << 2) \n" // Clear C bit 2 to disable D Cache
  82. "BIC R0, R0, #0x1 \n" // Clear M bit 0 to disable MMU
  83. "BIC R0, R0, #(0x1 << 11) \n" // Clear Z bit 11 to disable branch prediction
  84. "BIC R0, R0, #(0x1 << 13) \n" // Clear V bit 13 to disable hivecs
  85. "MCR p15, 0, R0, c1, c0, 0 \n" // Write value back to CP15 System Control register
  86. "ISB \n"
  87. // Configure ACTLR
  88. "MRC p15, 0, r0, c1, c0, 1 \n" // Read CP15 Auxiliary Control Register
  89. "ORR r0, r0, #(1 << 1) \n" // Enable L2 prefetch hint (UNK/WI since r4p1)
  90. "MCR p15, 0, r0, c1, c0, 1 \n" // Write CP15 Auxiliary Control Register
  91. // Set Vector Base Address Register (VBAR) to point to this application's vector table
  92. "LDR R0, =Vectors \n"
  93. "MCR p15, 0, R0, c12, c0, 0 \n"
  94. // Setup Stack for each exceptional mode
  95. "CPS #0x11 \n"
  96. "LDR SP, =Image$$FIQ_STACK$$ZI$$Limit \n"
  97. "CPS #0x12 \n"
  98. "LDR SP, =Image$$IRQ_STACK$$ZI$$Limit \n"
  99. "CPS #0x13 \n"
  100. "LDR SP, =Image$$SVC_STACK$$ZI$$Limit \n"
  101. "CPS #0x17 \n"
  102. "LDR SP, =Image$$ABT_STACK$$ZI$$Limit \n"
  103. "CPS #0x1B \n"
  104. "LDR SP, =Image$$UND_STACK$$ZI$$Limit \n"
  105. "CPS #0x1F \n"
  106. "LDR SP, =Image$$ARM_LIB_STACK$$ZI$$Limit \n"
  107. // Call SystemInit
  108. "BL SystemInit \n"
  109. // Unmask interrupts
  110. "CPSIE if \n"
  111. // Call __main
  112. "BL __main \n"
  113. );
  114. }
  115. /*----------------------------------------------------------------------------
  116. Default Handler for Exceptions / Interrupts
  117. *----------------------------------------------------------------------------*/
  118. void Default_Handler(void) {
  119. while(1);
  120. }