exceptions.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*==================================================================================================
  2. * Project : RTD AUTOSAR 4.4
  3. * Platform : CORTEXM
  4. * Peripheral :
  5. * Dependencies : none
  6. *
  7. * Autosar Version : 4.4.0
  8. * Autosar Revision : ASR_REL_4_4_REV_0000
  9. * Autosar Conf.Variant :
  10. * SW Version : 1.0.0
  11. * Build Version : S32K1_RTD_1_0_0_HF01_D2109_ASR_REL_4_4_REV_0000_20210907
  12. *
  13. * (c) Copyright 2020-2021 NXP Semiconductors
  14. * All Rights Reserved.
  15. *
  16. * NXP Confidential. This software is owned or controlled by NXP and may only be
  17. * used strictly in accordance with the applicable license terms. By expressly
  18. * accepting such terms or by downloading, installing, activating and/or otherwise
  19. * using the software, you are agreeing that you have read, and that you agree to
  20. * comply with and are bound by, such license terms. If you do not agree to be
  21. * bound by the applicable license terms, then you may not retain, install,
  22. * activate or otherwise use the software.
  23. ==================================================================================================*/
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. #include "Platform_Types.h"
  28. #include "Mcal.h"
  29. #ifdef __ICCARM__
  30. #pragma default_function_attributes = @ ".systeminit"
  31. #else
  32. __attribute__ ((section (".systeminit")))
  33. #endif
  34. #ifdef __ICCARM__
  35. #pragma default_function_attributes = @ ".systeminit"
  36. #else
  37. __attribute__ ((section (".systeminit")))
  38. #endif
  39. void NMI_Handler(void) __attribute__ ((weak)); /* NMI Handler */
  40. void HardFault_Handler(void) __attribute__ ((weak)); /* Hard Fault Handler */
  41. void MemManage_Handler(void) __attribute__ ((weak)); /* Reserved */
  42. void BusFault_Handler(void) __attribute__ ((weak)); /* Bus Fault Handler */
  43. void UsageFault_Handler(void) __attribute__ ((weak)); /* Usage Fault Handler */
  44. void DebugMon_Handler(void) __attribute__ ((weak)); /* Debug Monitor Handler */
  45. void PendSV_Handler(void) __attribute__ ((weak)); /* PendSV Handler */
  46. void SysTick_Handler(void) __attribute__ ((weak)); /* SysTick Handler */
  47. void undefined_handler(void); /* Undefined Handler */
  48. #ifdef MCAL_ENABLE_USER_MODE_SUPPORT
  49. void SVCHandler_main(uint32 * svc_args);
  50. void Suspend_Interrupts(void);
  51. void Resume_Interrupts(void);
  52. #endif
  53. #ifdef MCAL_ENABLE_USER_MODE_SUPPORT
  54. #ifndef __ICCARM__
  55. /* Define the SVC handler in assembly, to ensure there is no extra PUSH instruction at the beginning of the C handler.*/
  56. ASM_KEYWORD(".globl SVC_Handler");
  57. ASM_KEYWORD(".weak SVC_Handler");
  58. ASM_KEYWORD("SVC_Handler:");
  59. /*Load in R0 the stack pointer (depneding on context from which SVC is called)*/
  60. ASM_KEYWORD("tst lr, #4");
  61. ASM_KEYWORD("ite eq");
  62. ASM_KEYWORD("mrseq r0, MSP");
  63. ASM_KEYWORD("mrsne r0, PSP");
  64. /* stack pointer is passed to SVCHandler_main, it will be used to extract the parameter given to svc call*/
  65. ASM_KEYWORD("b SVCHandler_main");
  66. #else
  67. void SVC_Handler(void) __attribute__((naked, weak));
  68. void SVC_Handler(void)
  69. {
  70. ASM_KEYWORD("tst lr, #4\n"\
  71. "ite eq\n" \
  72. "mrseq r0, MSP\n" \
  73. "mrsne r0, PSP\n");
  74. /* stack pointer is passed to SVCHandler_main, it will be used to extract the parameter given to svc call*/
  75. ASM_KEYWORD("b SVCHandler_main");
  76. }
  77. #endif
  78. #endif
  79. void NMI_Handler(void)
  80. {
  81. while(TRUE){};
  82. }
  83. void HardFault_Handler(void)
  84. {
  85. ASM_KEYWORD("bx lr");
  86. while(TRUE){};
  87. }
  88. void MemManage_Handler(void)
  89. {
  90. while(TRUE){};
  91. }
  92. void BusFault_Handler(void)
  93. {
  94. while(TRUE){};
  95. }
  96. void UsageFault_Handler(void)
  97. {
  98. while(TRUE){};
  99. }
  100. #ifndef MCAL_ENABLE_USER_MODE_SUPPORT
  101. void SVC_Handler(void) __attribute__ ((weak)); /* SVCall Handler */
  102. void SVC_Handler(void)
  103. {
  104. while(TRUE){};
  105. }
  106. #else
  107. void SVCHandler_main(uint32 * svc_args)
  108. {
  109. uint32 svc_number; /* Stack contains: * r0, r1, r2, r3, r12, r14, the return address and xPSR */
  110. /* First argument (r0) is svc_args[0] */
  111. /* svc_args[6] = SP + 0x18 PC(r15) */
  112. /* ((char *)svc_args[6])[-2]; = first two bytes, lsb, of the instruction which caused the SVC */
  113. /* this will nto work if optimization compiler options are changed*/
  114. svc_number = ((uint8 *)svc_args[6])[-2];
  115. switch(svc_number)
  116. {
  117. case 1:
  118. /* Handle SVC 01*/
  119. ASM_KEYWORD("mov r0, #0x1"); /* Set User mode for Thread mode */
  120. ASM_KEYWORD("msr CONTROL, r0");
  121. break;
  122. case 0:
  123. /* Handle SVC 00*/
  124. ASM_KEYWORD("mov r0, #0x0"); /* Set Supervisor mode for Thread mode */
  125. ASM_KEYWORD("msr CONTROL, r0");
  126. break;
  127. case 2:
  128. /* Handle SVC 02*/
  129. Resume_Interrupts();
  130. break;
  131. case 3:
  132. /* Handle SVC 03*/
  133. Suspend_Interrupts();
  134. break;
  135. default:
  136. /* Unknown SVC*/
  137. break;
  138. }
  139. }
  140. void Suspend_Interrupts(void)
  141. {
  142. ASM_KEYWORD("push {r0}");
  143. ASM_KEYWORD("mov r0, #0x10");
  144. ASM_KEYWORD(" msr BASEPRI, r0");
  145. ASM_KEYWORD("pop {r0}");
  146. }
  147. void Resume_Interrupts(void)
  148. {
  149. ASM_KEYWORD("push {r0}");
  150. ASM_KEYWORD("mov r0, #0x0");
  151. ASM_KEYWORD("msr BASEPRI, r0");
  152. ASM_KEYWORD("pop {r0}");
  153. }
  154. #endif
  155. void DebugMon_Handler(void)
  156. {
  157. while(TRUE){};
  158. }
  159. void PendSV_Handler(void)
  160. {
  161. while(TRUE){};
  162. }
  163. void SysTick_Handler(void)
  164. {
  165. while(TRUE){};
  166. }
  167. void undefined_handler(void)
  168. {
  169. while(TRUE){};
  170. }
  171. #ifdef __ICCARM__
  172. #pragma default_function_attributes =
  173. #endif
  174. #ifdef __cplusplus
  175. }
  176. #endif