gd32f30x_it.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*!
  2. \file gd32f3x0_it.c
  3. \brief interrupt service routines
  4. */
  5. /*
  6. Copyright (C) 2022 GigaDevice
  7. 2022-12-01, V1.0.0, firmware for GD32F3x0
  8. */
  9. /******************************
  10. * Include File
  11. ******************************/
  12. #include "gd32f30x_it.h"
  13. #include "user.h"
  14. #include "TimeTask_Event.h"
  15. #include "can.h"
  16. #include "FSM_1st.h"
  17. #include "api_rt.h"
  18. #include "sys_ctrl.h"
  19. #include "enviolo_can.h"
  20. #include "string.h"
  21. /******************************
  22. * Parameter
  23. ******************************/
  24. extern uint8_t data;
  25. /******************************
  26. * Function
  27. ******************************/
  28. /*!
  29. \brief this function handles NMI exception
  30. \param[in] none
  31. \param[out] none
  32. \retval none
  33. */
  34. void NMI_Handler(void)
  35. {
  36. }
  37. /*!
  38. \brief this function handles HardFault exception
  39. \param[in] none
  40. \param[out] none
  41. \retval none
  42. */
  43. void HardFault_Handler(void)
  44. {
  45. sysctrl_stPwmState.blPwmOnflg = TRUE;/* HardFault_Handler无法被屏蔽,hw_blPWMOnFlg可能被RAMtest修改 */
  46. sysctrl_voPwmOff();
  47. /* if Hard Fault exception occurs, go to infinite loop */
  48. while (1)
  49. {
  50. //do nothing
  51. }
  52. }
  53. /*!
  54. \brief this function handles MemManage exception
  55. \param[in] none
  56. \param[out] none
  57. \retval none
  58. */
  59. void MemManage_Handler(void)
  60. {
  61. /* if Memory Manage exception occurs, go to infinite loop */
  62. while (1)
  63. {
  64. //do nothing
  65. }
  66. }
  67. /*!
  68. \brief this function handles BusFault exception
  69. \param[in] none
  70. \param[out] none
  71. \retval none
  72. */
  73. void BusFault_Handler(void)
  74. {
  75. /* if Bus Fault exception occurs, go to infinite loop */
  76. while (1)
  77. {
  78. //do nothing
  79. }
  80. }
  81. /*!
  82. \brief this function handles UsageFault exception
  83. \param[in] none
  84. \param[out] none
  85. \retval none
  86. */
  87. void UsageFault_Handler(void)
  88. {
  89. /* if Usage Fault exception occurs, go to infinite loop */
  90. while (1)
  91. {
  92. //do nothing
  93. }
  94. }
  95. /*!
  96. \brief this function handles SVC exception
  97. \param[in] none
  98. \param[out] none
  99. \retval none
  100. */
  101. void SVC_Handler(void)
  102. {
  103. }
  104. /*!
  105. \brief this function handles DebugMon exception
  106. \param[in] none
  107. \param[out] none
  108. \retval none
  109. */
  110. void DebugMon_Handler(void)
  111. {
  112. }
  113. /*!
  114. \brief this function handles PendSV exception
  115. \param[in] none
  116. \param[out] none
  117. \retval none
  118. */
  119. void PendSV_Handler(void)
  120. {
  121. }
  122. /*!
  123. \brief this function handles SysTick exception
  124. \param[in] none
  125. \param[out] none
  126. \retval none
  127. */
  128. void SysTick_Handler(void)
  129. {
  130. iRtTimer_Isr(HW_SYSTICK_TIMER);
  131. }
  132. /*!
  133. \brief
  134. \param[in] none
  135. \param[out] none
  136. \retval none
  137. */
  138. void ADC0_1_IRQHandler(void)
  139. {
  140. /* MCU self check count */
  141. clasB_uwADCCnt++;
  142. iRtAdc_CompleteIsr(0);
  143. }
  144. /*!
  145. \brief
  146. \param[in] none
  147. \param[out] none
  148. \retval none
  149. */
  150. void TIMER0_UP_TIMER9_IRQHandler(void)
  151. {
  152. /* MCU self check count */
  153. clasB_uwTIM0Cnt ++;
  154. iRtPwm_UpdateIsr(0);
  155. }
  156. /*!
  157. \brief
  158. \param[in] none
  159. \param[out] none
  160. \retval none
  161. */
  162. void TIMER1_IRQHandler(void)
  163. {
  164. /* MCU self check count */
  165. clasB_uwTIM1Cnt++;
  166. iRtCap_Isr(0);
  167. // UWORD uwIntSource = 0;
  168. // if (timer_interrupt_flag_get(TIMER1, TIMER_INT_FLAG_UP) != 0)
  169. // {
  170. // if(switch_flg.SysCoef_Flag == TRUE)
  171. // {
  172. // uwIntSource = 1;
  173. // cadence_voCadenceCal(uwIntSource);
  174. // bikespeed_voBikeSpeedCal(uwIntSource);
  175. // }
  176. // timer_interrupt_flag_clear(TIMER1, TIMER_INT_FLAG_UP);
  177. // }
  178. // else if (timer_interrupt_flag_get(TIMER1, TIMER_INT_FLAG_CH2) != 0)
  179. // {
  180. // if(switch_flg.SysCoef_Flag == TRUE)
  181. // {
  182. // uwIntSource = 2;
  183. // cadence_voCadenceCal(uwIntSource);
  184. // /* Select rising or falling edge trigger */
  185. // if(gpio_input_bit_get(GPIOB, GPIO_PIN_10) != 0)
  186. // {
  187. // /* reset the CH2P and CH2NP bits */
  188. // TIMER_CHCTL2(TIMER1) &= ~(uint32_t)(TIMER_CHCTL2_CH2P|TIMER_CHCTL2_CH2NP);
  189. // TIMER_CHCTL2(TIMER1) |= (uint32_t)((uint32_t)(TIMER_IC_POLARITY_FALLING) << 8U);
  190. // }
  191. // else
  192. // {
  193. // /* reset the CH2P and CH2NP bits */
  194. // TIMER_CHCTL2(TIMER1) &= ~(uint32_t)(TIMER_CHCTL2_CH2P|TIMER_CHCTL2_CH2NP);
  195. // TIMER_CHCTL2(TIMER1) |= (uint32_t)((uint32_t)(TIMER_IC_POLARITY_RISING) << 8U);
  196. // }
  197. // }
  198. // timer_interrupt_flag_clear(TIMER1, TIMER_INT_FLAG_CH2);
  199. // }
  200. // else if (timer_interrupt_flag_get(TIMER1, TIMER_INT_FLAG_CH3) != 0)
  201. // {
  202. // if(switch_flg.SysCoef_Flag == TRUE)
  203. // {
  204. // uwIntSource = 3;
  205. // bikespeed_voBikeSpeedCal(uwIntSource);
  206. // }
  207. // timer_interrupt_flag_clear(TIMER1, TIMER_INT_FLAG_CH3);
  208. // }
  209. // else
  210. // {
  211. // //do nothing
  212. // }
  213. }
  214. /*!
  215. \brief
  216. \param[in] none
  217. \param[out] none
  218. \retval none
  219. */
  220. void TIMER3_IRQHandler(void)
  221. {
  222. /* MCU self check count */
  223. clasB_uwTIM3Cnt++;
  224. iRtTimer_Isr(HW_TBS_TIMER);
  225. }
  226. /*!
  227. \brief
  228. \param[in] none
  229. \param[out] none
  230. \retval none
  231. */
  232. void TIMER5_IRQHandler(void)
  233. {
  234. /* MCU self check count */
  235. clasB_uwTIM5Cnt++;
  236. iRtTimer_Isr(HW_EVENT1MS_TIMER);
  237. }
  238. /*!
  239. \brief
  240. \param[in] none
  241. \param[out] none
  242. \retval none
  243. */
  244. void DMA1_Channel2_IRQHandler(void)
  245. {
  246. iRtUart_RxIsr(0);
  247. // /* Read PC Conmand */
  248. // if (dma_flag_get(DMA1, DMA_CH2, DMA_INT_FLAG_FTF) != 0)
  249. // {
  250. // UART_voCBDoneRead(UART_ERR_OK, 22);
  251. // DMA_CH2CTL(DMA1) &= ~DMA_CHXCTL_CHEN;
  252. // //dma_flag_clear(DMA1, DMA_CH2, DMA_INT_FLAG_FTF);
  253. // DMA_INTC(DMA1) |= DMA_FLAG_ADD(DMA_INT_FLAG_FTF, DMA_CH2);
  254. // uwTempCount = 22 - DMA_CH2CNT(DMA1);
  255. // DMA_CH2CNT(DMA1) = uwTempCount;
  256. // DMA_CH2CTL(DMA1) |= DMA_CHXCTL_CHEN;
  257. // }
  258. // /* RX error */
  259. // if (dma_flag_get(DMA1, DMA_CH2, DMA_FLAG_ERR) != 0)
  260. // {
  261. // DMA_CH2CTL(DMA1) &= ~DMA_CHXCTL_CHEN;
  262. // //dma_flag_clear(DMA1, DMA_CH2, DMA_FLAG_ERR);
  263. // DMA_INTC(DMA1) |= DMA_FLAG_ADD(DMA_FLAG_ERR, DMA_CH2);
  264. // DMA_CH2CNT(DMA1) = 22;
  265. // DMA_CH2CTL(DMA1) |= DMA_CHXCTL_CHEN;
  266. // }
  267. }
  268. /*!
  269. \brief
  270. \param[in] none
  271. \param[out] none
  272. \retval none
  273. */
  274. void DMA1_Channel4_IRQHandler(void)
  275. {
  276. /* USER CODE BEGIN DMA1_Channel2_3_IRQn 0 */
  277. if (dma_flag_get(DMA1, DMA_CH4, DMA_INT_FLAG_FTF) != 0)
  278. {
  279. iRtUart_AsyncWriteCompleteIsr(0);
  280. DMA_INTC(DMA1) |= DMA_FLAG_ADD(DMA_INT_FLAG_FTF, DMA_CH4);
  281. }
  282. // TX error
  283. if (dma_flag_get(DMA1, DMA_CH4, DMA_FLAG_ERR) != 0)
  284. {
  285. iRtUart_AsyncWriteErrorIsr(0);
  286. DMA_INTC(DMA1) |= DMA_FLAG_ADD(DMA_FLAG_ERR, DMA_CH4);
  287. }
  288. // if (dma_flag_get(DMA1, DMA_CH4, DMA_INT_FLAG_FTF) != 0)
  289. // {
  290. // if (UART_stParaStatus.bParaStart)
  291. // {
  292. // UART_bInsertPendTx = FALSE; // clear insertBuffer pending
  293. // UART_stParaStatus.bParaStart = FALSE; // clear parameter status
  294. // }
  295. // else
  296. // {
  297. // // do nothing
  298. // }
  299. // DMA_CH4CTL(DMA1) &= ~DMA_CHXCTL_CHEN;
  300. // //dma_flag_clear(DMA1, DMA_CH4, DMA_INT_FLAG_FTF);
  301. // DMA_INTC(DMA1) |= DMA_FLAG_ADD(DMA_INT_FLAG_FTF, DMA_CH4);
  302. // UART_stParaStatus.bWriteBusy = FALSE;
  303. // }
  304. // /* TX error */
  305. // if (dma_flag_get(DMA1, DMA_CH4, DMA_FLAG_ERR) != 0)
  306. // {
  307. // if (UART_stParaStatus.bParaStart)
  308. // {
  309. // UART_bInsertPendTx = FALSE; // clear insertBuffer pending
  310. // UART_stParaStatus.bParaStart = FALSE; // clear parameter status
  311. // }
  312. // DMA_CH4CTL(DMA1) &= ~DMA_CHXCTL_CHEN;
  313. // //dma_flag_clear(DMA1, DMA_CH4, DMA_FLAG_ERR);
  314. // DMA_INTC(DMA1) |= DMA_FLAG_ADD(DMA_FLAG_ERR, DMA_CH4);
  315. // UART_stParaStatus.bWriteBusy = FALSE;
  316. // }
  317. }
  318. /*!
  319. \brief
  320. \param[in] none
  321. \param[out] none
  322. \retval none
  323. */
  324. void CAN0_RX0_IRQHandler(void)
  325. {
  326. can_message_receive(CAN0, CAN_FIFO0, pRxMsg);
  327. if((pRxMsg->rx_ff != CAN_FF_STANDARD) || (pRxMsg->rx_dlen == 0))
  328. {
  329. can_interrupt_enable(CAN0, CAN_INT_RFF0);
  330. return;
  331. }
  332. switch (pRxMsg->rx_sfid)
  333. {
  334. case ID_PBU_BC:
  335. case ID_PBU_TO_MC: //接收PBU数据
  336. {
  337. CAN_RxBuf_Struct_PBU.ucBufID = (UWORD)pRxMsg->rx_sfid;
  338. CAN_Rx_ISR(&CAN_RxBuf_Struct_PBU, pRxMsg->rx_dlen);
  339. break;
  340. }
  341. case ID_BMS_BC:
  342. case ID_BMS_TO_MC: //接收BMS数据
  343. {
  344. CAN_RxBuf_Struct_BMS.ucBufID = (UWORD)pRxMsg->rx_sfid;
  345. CAN_Rx_ISR(&CAN_RxBuf_Struct_BMS, pRxMsg->rx_dlen);
  346. break;
  347. }
  348. case ID_HMI_BC:
  349. case ID_HMI_TO_MC: //接收HMI数据
  350. {
  351. CAN_RxBuf_Struct_HMI.ucBufID = (UWORD)pRxMsg->rx_sfid;
  352. CAN_Rx_ISR(&CAN_RxBuf_Struct_HMI, pRxMsg->rx_dlen);
  353. break;
  354. }
  355. case ID_CDL_BC:
  356. case ID_CDL_TO_MC: // case ID_CDL_TO_MC_TE://接收CDL数据
  357. {
  358. CAN_RxBuf_Struct_CDL.ucBufID = (UWORD)pRxMsg->rx_sfid;
  359. CAN_Rx_ISR(&CAN_RxBuf_Struct_CDL, pRxMsg->rx_dlen);
  360. break;
  361. }
  362. default:
  363. break;
  364. }
  365. can_interrupt_enable(CAN0, CAN_INT_RFF0);
  366. }
  367. /*!
  368. \brief
  369. \param[in] none
  370. \param[out] none
  371. \retval none
  372. */
  373. void CAN0_RX1_IRQHandler(void)
  374. {
  375. can_message_receive(CAN0, CAN_FIFO1, pRxMsg2);
  376. if((pRxMsg2->rx_ff != CAN_FF_STANDARD) || (pRxMsg2->rx_dlen == 0))
  377. {
  378. can_interrupt_enable(CAN0, CAN_INT_RFF1);
  379. return;
  380. }
  381. switch (pRxMsg2->rx_sfid)
  382. {
  383. case ID_ENVIOLO_TO_MC_1:
  384. case ID_ENVIOLO_TO_MC_2:
  385. {
  386. if(pRxMsg2->rx_dlen == 8)//由于没有帧头帧尾和CRC,故保证数据的准确性必须为8字节才解析
  387. {
  388. GearBox_CanData.RefreshFlag = TRUE;
  389. GearBox_CanData.ID = pRxMsg2->rx_sfid;
  390. memcpy((uint8_t*)GearBox_CanData.Data, (uint8_t*)pRxMsg2->rx_data, 8);
  391. }
  392. break;
  393. }
  394. default:
  395. break;
  396. }
  397. can_interrupt_enable(CAN0, CAN_INT_RFF1);
  398. }