stm32f1xx_it(250).c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 "main.h"
  21. #include "stm32f1xx_it.h"
  22. /* Private includes ----------------------------------------------------------*/
  23. /* USER CODE BEGIN Includes */
  24. /* USER CODE END Includes */
  25. /* Private typedef -----------------------------------------------------------*/
  26. /* USER CODE BEGIN TD */
  27. /* USER CODE END TD */
  28. /* Private define ------------------------------------------------------------*/
  29. /* USER CODE BEGIN PD */
  30. /* USER CODE END PD */
  31. /* Private macro -------------------------------------------------------------*/
  32. /* USER CODE BEGIN PM */
  33. /* USER CODE END PM */
  34. /* Private variables ---------------------------------------------------------*/
  35. /* USER CODE BEGIN PV */
  36. /* USER CODE END PV */
  37. /* Private function prototypes -----------------------------------------------*/
  38. /* USER CODE BEGIN PFP */
  39. /* USER CODE END PFP */
  40. /* Private user code ---------------------------------------------------------*/
  41. /* USER CODE BEGIN 0 */
  42. /* USER CODE END 0 */
  43. /* External variables --------------------------------------------------------*/
  44. extern TIM_HandleTypeDef htim2;
  45. extern TIM_HandleTypeDef htim4;
  46. extern DMA_HandleTypeDef hdma_uart4_rx;
  47. extern DMA_HandleTypeDef hdma_usart3_rx;
  48. extern UART_HandleTypeDef huart4;
  49. extern UART_HandleTypeDef huart1;
  50. extern UART_HandleTypeDef huart3;
  51. extern TIM_HandleTypeDef htim1;
  52. /* USER CODE BEGIN EV */
  53. /* USER CODE END EV */
  54. /******************************************************************************/
  55. /* Cortex-M3 Processor Interruption and Exception Handlers */
  56. /******************************************************************************/
  57. /**
  58. * @brief This function handles Non maskable interrupt.
  59. */
  60. void NMI_Handler(void)
  61. {
  62. /* USER CODE BEGIN NonMaskableInt_IRQn 0 */
  63. /* USER CODE END NonMaskableInt_IRQn 0 */
  64. /* USER CODE BEGIN NonMaskableInt_IRQn 1 */
  65. while (1)
  66. {
  67. }
  68. /* USER CODE END NonMaskableInt_IRQn 1 */
  69. }
  70. /**
  71. * @brief This function handles Hard fault interrupt.
  72. */
  73. void HardFault_Handler(void)
  74. {
  75. /* USER CODE BEGIN HardFault_IRQn 0 */
  76. /* USER CODE END HardFault_IRQn 0 */
  77. while (1)
  78. {
  79. /* USER CODE BEGIN W1_HardFault_IRQn 0 */
  80. /* USER CODE END W1_HardFault_IRQn 0 */
  81. }
  82. }
  83. /**
  84. * @brief This function handles Memory management fault.
  85. */
  86. void MemManage_Handler(void)
  87. {
  88. /* USER CODE BEGIN MemoryManagement_IRQn 0 */
  89. /* USER CODE END MemoryManagement_IRQn 0 */
  90. while (1)
  91. {
  92. /* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */
  93. /* USER CODE END W1_MemoryManagement_IRQn 0 */
  94. }
  95. }
  96. /**
  97. * @brief This function handles Prefetch fault, memory access fault.
  98. */
  99. void BusFault_Handler(void)
  100. {
  101. /* USER CODE BEGIN BusFault_IRQn 0 */
  102. /* USER CODE END BusFault_IRQn 0 */
  103. while (1)
  104. {
  105. /* USER CODE BEGIN W1_BusFault_IRQn 0 */
  106. /* USER CODE END W1_BusFault_IRQn 0 */
  107. }
  108. }
  109. /**
  110. * @brief This function handles Undefined instruction or illegal state.
  111. */
  112. void UsageFault_Handler(void)
  113. {
  114. /* USER CODE BEGIN UsageFault_IRQn 0 */
  115. /* USER CODE END UsageFault_IRQn 0 */
  116. while (1)
  117. {
  118. /* USER CODE BEGIN W1_UsageFault_IRQn 0 */
  119. /* USER CODE END W1_UsageFault_IRQn 0 */
  120. }
  121. }
  122. /**
  123. * @brief This function handles Debug monitor.
  124. */
  125. void DebugMon_Handler(void)
  126. {
  127. /* USER CODE BEGIN DebugMonitor_IRQn 0 */
  128. /* USER CODE END DebugMonitor_IRQn 0 */
  129. /* USER CODE BEGIN DebugMonitor_IRQn 1 */
  130. /* USER CODE END DebugMonitor_IRQn 1 */
  131. }
  132. /******************************************************************************/
  133. /* STM32F1xx Peripheral Interrupt Handlers */
  134. /* Add here the Interrupt Handlers for the used peripherals. */
  135. /* For the available peripheral interrupt handler names, */
  136. /* please refer to the startup file (startup_stm32f1xx.s). */
  137. /******************************************************************************/
  138. /**
  139. * @brief This function handles DMA1 channel3 global interrupt.
  140. */
  141. void DMA1_Channel3_IRQHandler(void)
  142. {
  143. /* USER CODE BEGIN DMA1_Channel3_IRQn 0 */
  144. /* USER CODE END DMA1_Channel3_IRQn 0 */
  145. HAL_DMA_IRQHandler(&hdma_usart3_rx);
  146. /* USER CODE BEGIN DMA1_Channel3_IRQn 1 */
  147. /* USER CODE END DMA1_Channel3_IRQn 1 */
  148. }
  149. /**
  150. * @brief This function handles TIM1 update interrupt.
  151. */
  152. void TIM1_UP_IRQHandler(void)
  153. {
  154. /* USER CODE BEGIN TIM1_UP_IRQn 0 */
  155. /* USER CODE END TIM1_UP_IRQn 0 */
  156. HAL_TIM_IRQHandler(&htim1);
  157. /* USER CODE BEGIN TIM1_UP_IRQn 1 */
  158. /* USER CODE END TIM1_UP_IRQn 1 */
  159. }
  160. /**
  161. * @brief This function handles TIM2 global interrupt.
  162. */
  163. void TIM2_IRQHandler(void)
  164. {
  165. /* USER CODE BEGIN TIM2_IRQn 0 */
  166. /* USER CODE END TIM2_IRQn 0 */
  167. HAL_TIM_IRQHandler(&htim2);
  168. /* USER CODE BEGIN TIM2_IRQn 1 */
  169. /* USER CODE END TIM2_IRQn 1 */
  170. }
  171. /**
  172. * @brief This function handles TIM4 global interrupt.
  173. */
  174. void TIM4_IRQHandler(void)
  175. {
  176. /* USER CODE BEGIN TIM4_IRQn 0 */
  177. /* USER CODE END TIM4_IRQn 0 */
  178. HAL_TIM_IRQHandler(&htim4);
  179. /* USER CODE BEGIN TIM4_IRQn 1 */
  180. /* USER CODE END TIM4_IRQn 1 */
  181. }
  182. /**
  183. * @brief This function handles USART1 global interrupt.
  184. */
  185. void USART1_IRQHandler(void)
  186. {
  187. /* USER CODE BEGIN USART1_IRQn 0 */
  188. /* USER CODE END USART1_IRQn 0 */
  189. HAL_UART_IRQHandler(&huart1);
  190. /* USER CODE BEGIN USART1_IRQn 1 */
  191. /* USER CODE END USART1_IRQn 1 */
  192. }
  193. /**
  194. * @brief This function handles USART3 global interrupt.
  195. */
  196. void USART3_IRQHandler(void)
  197. {
  198. /* USER CODE BEGIN USART3_IRQn 0 */
  199. // uint8_t temp = 0;
  200. /* USER CODE END USART3_IRQn 0 */
  201. HAL_UART_IRQHandler(&huart3);
  202. /* USER CODE BEGIN USART3_IRQn 1 */
  203. if(__HAL_UART_GET_FLAG(&huart3, UART_FLAG_IDLE) != RESET){
  204. g_usart3_rx_sta++;
  205. timeoutCnt_485 = 0; // 清除超时计数值
  206. // 清除空闲中断标志(否则,中断将不断重新触发)
  207. __HAL_UART_CLEAR_IDLEFLAG(&huart3);
  208. // 停止DMA接收(这将更新DMA传输计数器)
  209. HAL_UART_DMAStop(&huart3);
  210. // 计算实际接收到的数据长度
  211. uint16_t dataSize = USART3_REC_LEN - __HAL_DMA_GET_COUNTER(huart3.hdmarx);
  212. // 处理接收到的数据...
  213. parseModbusResponse((uint8_t *)g_usart3_rx_buf , dataSize);
  214. // 如果需要继续接收,可以再次启动DMA接收
  215. HAL_UART_Receive_DMA(&huart3, (uint8_t*)g_usart3_rx_buf, USART3_REC_LEN);
  216. }
  217. /* USER CODE END USART3_IRQn 1 */
  218. }
  219. /**
  220. * @brief This function handles UART4 global interrupt.
  221. */
  222. void UART4_IRQHandler(void)
  223. {
  224. /* USER CODE BEGIN UART4_IRQn 0 */
  225. /* USER CODE END UART4_IRQn 0 */
  226. HAL_UART_IRQHandler(&huart4);
  227. /* USER CODE BEGIN UART4_IRQn 1 */
  228. if(__HAL_UART_GET_FLAG(&huart4, UART_FLAG_IDLE) != RESET){
  229. g_usart4_rx_sta++;
  230. // 清除空闲中断标志(否则,中断将不断重新触发)
  231. __HAL_UART_CLEAR_IDLEFLAG(&huart4);
  232. // 停止DMA接收(这将更新DMA传输计数器)
  233. // HAL_UART_DMAStop(&huart4);
  234. // 计算实际接收到的数据长度
  235. // uint16_t dataSize = USART4_REC_LEN - __HAL_DMA_GET_COUNTER(huart4.hdmarx);
  236. //
  237. // 处理接收到的数据...
  238. // 如果需要继续接收,可以再次启动DMA接收
  239. HAL_UART_Receive_DMA(&huart4, (uint8_t*)g_usart4_rx_buf, USART4_REC_LEN);
  240. }
  241. /* USER CODE END UART4_IRQn 1 */
  242. }
  243. /**
  244. * @brief This function handles DMA2 channel3 global interrupt.
  245. */
  246. void DMA2_Channel3_IRQHandler(void)
  247. {
  248. /* USER CODE BEGIN DMA2_Channel3_IRQn 0 */
  249. /* USER CODE END DMA2_Channel3_IRQn 0 */
  250. HAL_DMA_IRQHandler(&hdma_uart4_rx);
  251. /* USER CODE BEGIN DMA2_Channel3_IRQn 1 */
  252. /* USER CODE END DMA2_Channel3_IRQn 1 */
  253. }
  254. /* USER CODE BEGIN 1 */
  255. /* USER CODE END 1 */