exceptions.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. while(TRUE){};
  86. }
  87. void MemManage_Handler(void)
  88. {
  89. while(TRUE){};
  90. }
  91. void BusFault_Handler(void)
  92. {
  93. while(TRUE){};
  94. }
  95. void UsageFault_Handler(void)
  96. {
  97. while(TRUE){};
  98. }
  99. #ifndef MCAL_ENABLE_USER_MODE_SUPPORT
  100. void SVC_Handler(void) __attribute__ ((weak)); /* SVCall Handler */
  101. void SVC_Handler(void)
  102. {
  103. while(TRUE){};
  104. }
  105. #else
  106. void SVCHandler_main(uint32 * svc_args)
  107. {
  108. uint32 svc_number; /* Stack contains: * r0, r1, r2, r3, r12, r14, the return address and xPSR */
  109. /* First argument (r0) is svc_args[0] */
  110. /* svc_args[6] = SP + 0x18 PC(r15) */
  111. /* ((char *)svc_args[6])[-2]; = first two bytes, lsb, of the instruction which caused the SVC */
  112. /* this will nto work if optimization compiler options are changed*/
  113. svc_number = ((uint8 *)svc_args[6])[-2];
  114. switch(svc_number)
  115. {
  116. case 1:
  117. /* Handle SVC 01*/
  118. ASM_KEYWORD("mov r0, #0x1"); /* Set User mode for Thread mode */
  119. ASM_KEYWORD("msr CONTROL, r0");
  120. break;
  121. case 0:
  122. /* Handle SVC 00*/
  123. ASM_KEYWORD("mov r0, #0x0"); /* Set Supervisor mode for Thread mode */
  124. ASM_KEYWORD("msr CONTROL, r0");
  125. break;
  126. case 2:
  127. /* Handle SVC 02*/
  128. Resume_Interrupts();
  129. break;
  130. case 3:
  131. /* Handle SVC 03*/
  132. Suspend_Interrupts();
  133. break;
  134. default:
  135. /* Unknown SVC*/
  136. break;
  137. }
  138. }
  139. void Suspend_Interrupts(void)
  140. {
  141. ASM_KEYWORD("push {r0}");
  142. ASM_KEYWORD("mov r0, #0x10");
  143. ASM_KEYWORD(" msr BASEPRI, r0");
  144. ASM_KEYWORD("pop {r0}");
  145. }
  146. void Resume_Interrupts(void)
  147. {
  148. ASM_KEYWORD("push {r0}");
  149. ASM_KEYWORD("mov r0, #0x0");
  150. ASM_KEYWORD("msr BASEPRI, r0");
  151. ASM_KEYWORD("pop {r0}");
  152. }
  153. #endif
  154. void DebugMon_Handler(void)
  155. {
  156. while(TRUE){};
  157. }
  158. void PendSV_Handler(void)
  159. {
  160. while(TRUE){};
  161. }
  162. void SysTick_Handler(void)
  163. {
  164. while(TRUE){};
  165. }
  166. void undefined_handler(void)
  167. {
  168. while(TRUE){};
  169. }
  170. #ifdef __ICCARM__
  171. #pragma default_function_attributes =
  172. #endif
  173. #ifdef __cplusplus
  174. }
  175. #endif