stm32f1xx_it.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file stm32f1xx_it.c
  5. * @brief Interrupt Service Routines.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2024 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "stm32f1xx_it.h"
  21. #include "main.h"
  22. /* Private includes ----------------------------------------------------------*/
  23. /* USER CODE BEGIN Includes */
  24. #include "schedule.h"
  25. #include "stm32f103xe.h"
  26. #include "stm32f1xx_ll_usart.h"
  27. #include "uart_interface.h"
  28. /* USER CODE END Includes */
  29. /* Private typedef -----------------------------------------------------------*/
  30. /* USER CODE BEGIN TD */
  31. /* USER CODE END TD */
  32. /* Private define ------------------------------------------------------------*/
  33. /* USER CODE BEGIN PD */
  34. /* USER CODE END PD */
  35. /* Private macro -------------------------------------------------------------*/
  36. /* USER CODE BEGIN PM */
  37. /* USER CODE END PM */
  38. /* Private variables ---------------------------------------------------------*/
  39. /* USER CODE BEGIN PV */
  40. /* USER CODE END PV */
  41. /* Private function prototypes -----------------------------------------------*/
  42. /* USER CODE BEGIN PFP */
  43. /* USER CODE END PFP */
  44. /* Private user code ---------------------------------------------------------*/
  45. /* USER CODE BEGIN 0 */
  46. /* USER CODE END 0 */
  47. /* External variables --------------------------------------------------------*/
  48. extern PCD_HandleTypeDef hpcd_USB_FS;
  49. extern CAN_HandleTypeDef hcan;
  50. extern UART_HandleTypeDef huart3;
  51. /* USER CODE BEGIN EV */
  52. /* USER CODE END EV */
  53. /******************************************************************************/
  54. /* Cortex-M3 Processor Interruption and Exception Handlers */
  55. /******************************************************************************/
  56. /**
  57. * @brief This function handles Non maskable interrupt.
  58. */
  59. void NMI_Handler(void)
  60. {
  61. /* USER CODE BEGIN NonMaskableInt_IRQn 0 */
  62. /* USER CODE END NonMaskableInt_IRQn 0 */
  63. /* USER CODE BEGIN NonMaskableInt_IRQn 1 */
  64. while (1)
  65. {
  66. }
  67. /* USER CODE END NonMaskableInt_IRQn 1 */
  68. }
  69. /**
  70. * @brief This function handles Hard fault interrupt.
  71. */
  72. void HardFault_Handler(void)
  73. {
  74. /* USER CODE BEGIN HardFault_IRQn 0 */
  75. /* USER CODE END HardFault_IRQn 0 */
  76. while (1)
  77. {
  78. /* USER CODE BEGIN W1_HardFault_IRQn 0 */
  79. /* USER CODE END W1_HardFault_IRQn 0 */
  80. }
  81. }
  82. /**
  83. * @brief This function handles Memory management fault.
  84. */
  85. void MemManage_Handler(void)
  86. {
  87. /* USER CODE BEGIN MemoryManagement_IRQn 0 */
  88. /* USER CODE END MemoryManagement_IRQn 0 */
  89. while (1)
  90. {
  91. /* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */
  92. /* USER CODE END W1_MemoryManagement_IRQn 0 */
  93. }
  94. }
  95. /**
  96. * @brief This function handles Prefetch fault, memory access fault.
  97. */
  98. void BusFault_Handler(void)
  99. {
  100. /* USER CODE BEGIN BusFault_IRQn 0 */
  101. /* USER CODE END BusFault_IRQn 0 */
  102. while (1)
  103. {
  104. /* USER CODE BEGIN W1_BusFault_IRQn 0 */
  105. /* USER CODE END W1_BusFault_IRQn 0 */
  106. }
  107. }
  108. /**
  109. * @brief This function handles Undefined instruction or illegal state.
  110. */
  111. void UsageFault_Handler(void)
  112. {
  113. /* USER CODE BEGIN UsageFault_IRQn 0 */
  114. /* USER CODE END UsageFault_IRQn 0 */
  115. while (1)
  116. {
  117. /* USER CODE BEGIN W1_UsageFault_IRQn 0 */
  118. /* USER CODE END W1_UsageFault_IRQn 0 */
  119. }
  120. }
  121. /**
  122. * @brief This function handles System service call via SWI instruction.
  123. */
  124. void SVC_Handler(void)
  125. {
  126. /* USER CODE BEGIN SVCall_IRQn 0 */
  127. /* USER CODE END SVCall_IRQn 0 */
  128. /* USER CODE BEGIN SVCall_IRQn 1 */
  129. /* USER CODE END SVCall_IRQn 1 */
  130. }
  131. /**
  132. * @brief This function handles Debug monitor.
  133. */
  134. void DebugMon_Handler(void)
  135. {
  136. /* USER CODE BEGIN DebugMonitor_IRQn 0 */
  137. /* USER CODE END DebugMonitor_IRQn 0 */
  138. /* USER CODE BEGIN DebugMonitor_IRQn 1 */
  139. /* USER CODE END DebugMonitor_IRQn 1 */
  140. }
  141. /**
  142. * @brief This function handles Pendable request for system service.
  143. */
  144. void PendSV_Handler(void)
  145. {
  146. /* USER CODE BEGIN PendSV_IRQn 0 */
  147. /* USER CODE END PendSV_IRQn 0 */
  148. /* USER CODE BEGIN PendSV_IRQn 1 */
  149. /* USER CODE END PendSV_IRQn 1 */
  150. }
  151. /**
  152. * @brief This function handles System tick timer.
  153. */
  154. void SysTick_Handler(void)
  155. {
  156. /* USER CODE BEGIN SysTick_IRQn 0 */
  157. /* USER CODE END SysTick_IRQn 0 */
  158. HAL_IncTick();
  159. /* USER CODE BEGIN SysTick_IRQn 1 */
  160. schedule_clock();
  161. uart_rx_ticks();
  162. /* USER CODE END SysTick_IRQn 1 */
  163. }
  164. /******************************************************************************/
  165. /* STM32F1xx Peripheral Interrupt Handlers */
  166. /* Add here the Interrupt Handlers for the used peripherals. */
  167. /* For the available peripheral interrupt handler names, */
  168. /* please refer to the startup file (startup_stm32f1xx.s). */
  169. /******************************************************************************/
  170. /**
  171. * @brief This function handles USB high priority or CAN TX interrupts.
  172. */
  173. void USB_HP_CAN1_TX_IRQHandler(void)
  174. {
  175. /* USER CODE BEGIN USB_HP_CAN1_TX_IRQn 0 */
  176. /* USER CODE END USB_HP_CAN1_TX_IRQn 0 */
  177. HAL_CAN_IRQHandler(&hcan);
  178. HAL_PCD_IRQHandler(&hpcd_USB_FS);
  179. /* USER CODE BEGIN USB_HP_CAN1_TX_IRQn 1 */
  180. /* USER CODE END USB_HP_CAN1_TX_IRQn 1 */
  181. }
  182. /**
  183. * @brief This function handles USB low priority or CAN RX0 interrupts.
  184. */
  185. void USB_LP_CAN1_RX0_IRQHandler(void)
  186. {
  187. /* USER CODE BEGIN USB_LP_CAN1_RX0_IRQn 0 */
  188. /* USER CODE END USB_LP_CAN1_RX0_IRQn 0 */
  189. HAL_CAN_IRQHandler(&hcan);
  190. HAL_PCD_IRQHandler(&hpcd_USB_FS);
  191. /* USER CODE BEGIN USB_LP_CAN1_RX0_IRQn 1 */
  192. /* USER CODE END USB_LP_CAN1_RX0_IRQn 1 */
  193. }
  194. /**
  195. * @brief This function handles USART1 global interrupt.
  196. */
  197. void USART1_IRQHandler(void)
  198. {
  199. /* USER CODE BEGIN USART1_IRQn 0 */
  200. uint32_t isrflags = READ_REG(USART1->SR);
  201. uint32_t cr1its = READ_REG(USART1->CR1);
  202. // 接收中断处理
  203. if ((isrflags & USART_SR_RXNE) != RESET)
  204. {
  205. CLEAR_BIT(USART1->SR, USART_SR_RXNE);
  206. uart_msg.rx_over_time_count = 0;
  207. uart_msg.disconnect_count = 0;
  208. uart_msg.disconnect_flg = 0;
  209. if (uart_msg.rx_count_u8 >= RX_TX_BUF_LEN)
  210. {
  211. uart_msg.rx_count_u8 = 0;
  212. }
  213. uart_msg.rx[uart_msg.rx_count_u8++] = LL_USART_ReceiveData8(USART1);
  214. }
  215. // 发送中断处理
  216. if (((isrflags & USART_SR_TXE) != RESET) && (cr1its & USART_CR1_TXEIE) != RESET)
  217. {
  218. CLEAR_BIT(USART1->SR, USART_SR_TXE);
  219. LL_USART_TransmitData8(USART1, uart_msg.tx[uart_msg.tx_count_u8++]);
  220. if (uart_msg.tx_count_u8 >= uart_msg.tx_len)
  221. {
  222. CLEAR_BIT(USART1->CR1, USART_CR1_TXEIE);
  223. /* 使能数据发送完毕中断 */
  224. SET_BIT(USART1->CR1, USART_CR1_TCIE);
  225. }
  226. }
  227. // 发送完成中断
  228. if (((isrflags & USART_SR_TC) != RESET) && ((cr1its & USART_CR1_TCIE) != RESET))
  229. {
  230. CLEAR_BIT(USART1->CR1, USART_CR1_TCIE);
  231. SET_BIT(USART1->CR1, USART_CR1_RXNEIE);
  232. }
  233. // 发送错误
  234. if ((isrflags & USART_SR_ORE) != RESET)
  235. CLEAR_BIT(USART1->SR, USART_SR_ORE);
  236. /* USER CODE END USART1_IRQn 0 */
  237. /* USER CODE BEGIN USART1_IRQn 1 */
  238. /* USER CODE END USART1_IRQn 1 */
  239. }
  240. /**
  241. * @brief This function handles USART2 global interrupt.
  242. */
  243. void USART2_IRQHandler(void)
  244. {
  245. /* USER CODE BEGIN USART2_IRQn 0 */
  246. /* USER CODE END USART2_IRQn 0 */
  247. /* USER CODE BEGIN USART2_IRQn 1 */
  248. /* USER CODE END USART2_IRQn 1 */
  249. }
  250. /**
  251. * @brief This function handles USART3 global interrupt.
  252. */
  253. void USART3_IRQHandler(void)
  254. {
  255. /* USER CODE BEGIN USART3_IRQn 0 */
  256. /* USER CODE END USART3_IRQn 0 */
  257. HAL_UART_IRQHandler(&huart3);
  258. /* USER CODE BEGIN USART3_IRQn 1 */
  259. /* USER CODE END USART3_IRQn 1 */
  260. }
  261. /* USER CODE BEGIN 1 */
  262. /* USER CODE END 1 */