Interrupt.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #include <Myproject.h>
  2. /**
  3. @function LVW_TSD_INT
  4. @brief 芯片过温 芯片欠压故障中断
  5. @date 2025-11-01
  6. */
  7. void LVW_TSD_INT(void) interrupt 0
  8. {
  9. if (ReadBit(LVSR, LVWIF))
  10. {
  11. if (ReadBit(LVSR, LVWF))
  12. {
  13. FaultProcess();
  14. ClrBit(LVSR, LVWF);
  15. }
  16. ClrBit(LVSR, LVWIF);
  17. }
  18. if (TSDIF)
  19. { TSDIF = 0; }
  20. }
  21. /**
  22. @function EXTERN_INT0
  23. @brief 外部中断0 用于预驱故障检测 中断优先级最高
  24. @date 2025-11-01
  25. */
  26. void EXTERN_INT0(void) interrupt 1
  27. {
  28. if (IF0)
  29. {
  30. FaultProcess();
  31. mcFaultSource = FaultHardOVCurrent;
  32. IF0 = 0;
  33. }
  34. }
  35. /* -------------------------------------------------------------------------------------------------
  36. Function Name : void CMP3_INT(void)
  37. Description : CMP3:硬件比较器过流保护,关断输出,中断优先级最高
  38. Input : 无
  39. Output : 无
  40. -------------------------------------------------------------------------------------------------*/
  41. void CMP3_INT(void) interrupt 12
  42. {
  43. if (ReadBit(CMP_SR, CMP3IF))
  44. {
  45. FaultProcess(); // 关闭输出
  46. mcFaultSource = FaultHardOVCurrent; // 硬件过流保护
  47. ClrBit(CMP_SR, CMP3IF);
  48. }
  49. }
  50. /**
  51. @function DRV_ISR
  52. @brief FOC中断(Drv中断),每个载波周期执行一次,用于处理响应较高的程序,中断优先级第二。DCEN开了就会产生中断。
  53. @date 2025-11-01
  54. */
  55. void DRV_ISR(void) interrupt 3
  56. {
  57. if (ReadBit(DRV_SR, DCIF)) // 比较中断
  58. {
  59. Fault_Overcurrent(); //软件过流保护
  60. DRV_SR = (DRV_SR | SYSTIF) & (~DCIF);
  61. }
  62. }
  63. /**
  64. @function SYStick_INT
  65. @brief 定时器中断(SYS TICK中断)
  66. @date 2025-11-01
  67. */
  68. void SYStick_INT(void) interrupt 10
  69. {
  70. if (ReadBit(DRV_SR, SYSTIF)) // SYS TICK中断
  71. {
  72. IsTick = true;
  73. if (mcFocCtrl.State_Count)
  74. { mcFocCtrl.State_Count--; }
  75. DRV_SR = (DRV_SR | DCIF) & (~SYSTIF);// 清零标志位
  76. }
  77. }
  78. /**
  79. @function COM1_INT_Handler
  80. @brief I2C/UART1 共用此中断
  81. @date 2025-11-05
  82. */
  83. void COM1_INT_Handler(void) interrupt 13
  84. {
  85. static uint8_t recv_index = 0;
  86. uint8_t byte = 0x00, expected_len = 0;
  87. uint16_t dataLen = 0x00;
  88. if (TI)
  89. { TI = 0; }
  90. if (RI)
  91. {
  92. byte = UT_DR;
  93. RI = 0;
  94. #if (CONTROL_MODE == UARTMODE)
  95. {
  96. // 安全写入检查
  97. if (recv_index >= MAX_FRAME_LEN)
  98. {
  99. recv_index = 0;
  100. return;
  101. }
  102. // 状态机处理
  103. // 阶段1:等待帧头
  104. if (recv_index == 0)
  105. {
  106. if (byte == FRAME_HEADER)
  107. { RecvBuffer[recv_index++] = byte; }
  108. return;
  109. }
  110. // 阶段2:接收功能码和长度
  111. if (recv_index < DATA_OFFSET)
  112. {
  113. RecvBuffer[recv_index++] = byte;
  114. // 校验长度字段有效性
  115. if (recv_index == DATA_OFFSET)
  116. {
  117. dataLen = ((uint16_t)RecvBuffer[5] << 8) | RecvBuffer[4];
  118. // 检查长度字节
  119. if (dataLen > (MAX_FRAME_LEN - DATA_OFFSET - CRC_OFFSET))
  120. { recv_index = 0; }
  121. }
  122. return;
  123. }
  124. // 阶段3:接收数据区
  125. dataLen = ((uint16_t)RecvBuffer[5] << 8) | RecvBuffer[4];
  126. expected_len = DATA_OFFSET + dataLen + CRC_OFFSET;
  127. if (recv_index < expected_len)
  128. {
  129. RecvBuffer[recv_index++] = byte;
  130. // 完整帧接收完成
  131. if (recv_index == expected_len)
  132. {
  133. RecMessageFalg = true;
  134. recv_index = 0;
  135. }
  136. return;
  137. }
  138. // 异常情况复位
  139. recv_index = 0;
  140. }
  141. #endif
  142. }
  143. }
  144. /**
  145. @function COM_INT_Handler
  146. @brief SPI/UART2/LIN 共用此中断
  147. @date 2025-11-05
  148. */
  149. void COM2_INT_Handler(void) interrupt 14
  150. {
  151. if (UT2TI)
  152. { UT2TI = 0; }
  153. if (UT2RI)
  154. { UT2RI = 0; }
  155. }
  156. /**
  157. @function DAM_INT_Handler
  158. @brief DMA中断函数
  159. @date 2025-11-05
  160. */
  161. void DAM_INT_Handler(void) interrupt 15
  162. {
  163. if (ReadBit(DMA0_CR0, DMAIF))
  164. {
  165. memset(&DebugDat, 0, MAX_FRAME_LEN);
  166. ClrBit(DMA0_CR0, DMAIF);
  167. }
  168. if (ReadBit(DMA1_CR0, DMAIF))
  169. {
  170. ClrBit(DMA1_CR0, DMAIF);
  171. }
  172. }