exc.S 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* *****************************************************************************
  2. * The MIT License
  3. *
  4. * Copyright (c) 2010 Perry Hung.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. * ****************************************************************************/
  24. # On an exception, push a fake stack thread mode stack frame and redirect
  25. # thread execution to a thread mode error handler
  26. # From RM008:
  27. # The SP is decremented by eight words by the completion of the stack push.
  28. # Figure 5-1 shows the contents of the stack after an exception pre-empts the
  29. # current program flow.
  30. #
  31. # Old SP--> <previous>
  32. # xPSR
  33. # PC
  34. # LR
  35. # r12
  36. # r3
  37. # r2
  38. # r1
  39. # SP--> r0
  40. .text
  41. .globl __exc_nmi
  42. .weak __exc_nmi
  43. .globl __exc_hardfault
  44. .weak __exc_hardfault
  45. .globl __exc_memmanage
  46. .weak __exc_memmanage
  47. .globl __exc_busfault
  48. .weak __exc_busfault
  49. .globl __exc_usagefault
  50. .weak __exc_usagefault
  51. .code 16
  52. .thumb_func
  53. __exc_nmi:
  54. mov r0, #1
  55. b __default_exc
  56. .thumb_func
  57. __exc_hardfault:
  58. mov r0, #2
  59. b __default_exc
  60. .thumb_func
  61. __exc_memmanage:
  62. mov r0, #3
  63. b __default_exc
  64. .thumb_func
  65. __exc_busfault:
  66. mov r0, #4
  67. b __default_exc
  68. .thumb_func
  69. __exc_usagefault:
  70. mov r0, #5
  71. b __default_exc
  72. .thumb_func
  73. __default_exc:
  74. ldr r2, NVIC_CCR @ Enable returning to thread mode even if there are
  75. mov r1 ,#1 @ pending exceptions. See flag NONEBASETHRDENA.
  76. str r1, [r2]
  77. cpsid i @ Disable global interrupts
  78. ldr r2, SYSTICK_CSR @ Disable systick handler
  79. mov r1, #0
  80. str r1, [r2]
  81. ldr r1, CPSR_MASK @ Set default CPSR
  82. push {r1}
  83. ldr r1, TARGET_PC @ Set target pc
  84. push {r1}
  85. sub sp, sp, #24 @ Don't care
  86. ldr r1, EXC_RETURN @ Return to thread mode
  87. mov lr, r1
  88. bx lr @ Exception exit
  89. .align 4
  90. CPSR_MASK: .word 0x61000000
  91. EXC_RETURN: .word 0xFFFFFFF9
  92. TARGET_PC: .word __error
  93. NVIC_CCR: .word 0xE000ED14 @ NVIC configuration control register
  94. SYSTICK_CSR: .word 0xE000E010 @ Systick control register