stm32f1xx_it.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file stm32f1xx_it.c
  5. * @brief Interrupt Service Routines.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22. #include "stm32f1xx_it.h"
  23. /* Private includes ----------------------------------------------------------*/
  24. /* USER CODE BEGIN Includes */
  25. #include "usart.h"
  26. #include "buffer.h"
  27. #include <rtthread.h>
  28. #include "flash.h"
  29. /* USER CODE END Includes */
  30. /* Private typedef -----------------------------------------------------------*/
  31. /* USER CODE BEGIN TD */
  32. /* USER CODE END TD */
  33. /* Private define ------------------------------------------------------------*/
  34. /* USER CODE BEGIN PD */
  35. /* USER CODE END PD */
  36. /* Private macro -------------------------------------------------------------*/
  37. /* USER CODE BEGIN PM */
  38. /* USER CODE END PM */
  39. /* Private variables ---------------------------------------------------------*/
  40. /* USER CODE BEGIN PV */
  41. /* USER CODE END PV */
  42. /* Private function prototypes -----------------------------------------------*/
  43. /* USER CODE BEGIN PFP */
  44. /* USER CODE END PFP */
  45. /* Private user code ---------------------------------------------------------*/
  46. /* USER CODE BEGIN 0 */
  47. /* USER CODE END 0 */
  48. /* External variables --------------------------------------------------------*/
  49. extern DMA_HandleTypeDef hdma_uart4_rx;
  50. extern DMA_HandleTypeDef hdma_uart4_tx;
  51. extern DMA_HandleTypeDef hdma_usart1_rx;
  52. extern DMA_HandleTypeDef hdma_usart1_tx;
  53. extern DMA_HandleTypeDef hdma_usart2_rx;
  54. extern DMA_HandleTypeDef hdma_usart2_tx;
  55. extern DMA_HandleTypeDef hdma_usart3_rx;
  56. extern DMA_HandleTypeDef hdma_usart3_tx;
  57. extern UART_HandleTypeDef huart4;
  58. extern UART_HandleTypeDef huart5;
  59. extern UART_HandleTypeDef huart1;
  60. extern UART_HandleTypeDef huart2;
  61. extern UART_HandleTypeDef huart3;
  62. /* USER CODE BEGIN EV */
  63. extern void USART_IRQHandler(UART_HandleTypeDef *huart);
  64. extern void USART_IRQHandler2(UART_HandleTypeDef *huart);
  65. /* USER CODE END EV */
  66. /******************************************************************************/
  67. /* Cortex-M3 Processor Interruption and Exception Handlers */
  68. /******************************************************************************/
  69. /**
  70. * @brief This function handles Non maskable interrupt.
  71. */
  72. void NMI_Handler(void)
  73. {
  74. /* USER CODE BEGIN NonMaskableInt_IRQn 0 */
  75. /* USER CODE END NonMaskableInt_IRQn 0 */
  76. /* USER CODE BEGIN NonMaskableInt_IRQn 1 */
  77. /* USER CODE END NonMaskableInt_IRQn 1 */
  78. }
  79. /**
  80. * @brief This function handles Memory management fault.
  81. */
  82. void MemManage_Handler(void)
  83. {
  84. /* USER CODE BEGIN MemoryManagement_IRQn 0 */
  85. /* USER CODE END MemoryManagement_IRQn 0 */
  86. while (1)
  87. {
  88. /* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */
  89. /* USER CODE END W1_MemoryManagement_IRQn 0 */
  90. }
  91. }
  92. /**
  93. * @brief This function handles Prefetch fault, memory access fault.
  94. */
  95. void BusFault_Handler(void)
  96. {
  97. /* USER CODE BEGIN BusFault_IRQn 0 */
  98. /* USER CODE END BusFault_IRQn 0 */
  99. while (1)
  100. {
  101. /* USER CODE BEGIN W1_BusFault_IRQn 0 */
  102. /* USER CODE END W1_BusFault_IRQn 0 */
  103. }
  104. }
  105. /**
  106. * @brief This function handles Undefined instruction or illegal state.
  107. */
  108. void UsageFault_Handler(void)
  109. {
  110. /* USER CODE BEGIN UsageFault_IRQn 0 */
  111. /* USER CODE END UsageFault_IRQn 0 */
  112. while (1)
  113. {
  114. /* USER CODE BEGIN W1_UsageFault_IRQn 0 */
  115. /* USER CODE END W1_UsageFault_IRQn 0 */
  116. }
  117. }
  118. /**
  119. * @brief This function handles System service call via SWI instruction.
  120. */
  121. void SVC_Handler(void)
  122. {
  123. /* USER CODE BEGIN SVCall_IRQn 0 */
  124. /* USER CODE END SVCall_IRQn 0 */
  125. /* USER CODE BEGIN SVCall_IRQn 1 */
  126. /* USER CODE END SVCall_IRQn 1 */
  127. }
  128. /**
  129. * @brief This function handles Debug monitor.
  130. */
  131. void DebugMon_Handler(void)
  132. {
  133. /* USER CODE BEGIN DebugMonitor_IRQn 0 */
  134. /* USER CODE END DebugMonitor_IRQn 0 */
  135. /* USER CODE BEGIN DebugMonitor_IRQn 1 */
  136. /* USER CODE END DebugMonitor_IRQn 1 */
  137. }
  138. /******************************************************************************/
  139. /* STM32F1xx Peripheral Interrupt Handlers */
  140. /* Add here the Interrupt Handlers for the used peripherals. */
  141. /* For the available peripheral interrupt handler names, */
  142. /* please refer to the startup file (startup_stm32f1xx.s). */
  143. /******************************************************************************/
  144. /**
  145. * @brief This function handles DMA1 channel2 global interrupt.
  146. */
  147. void DMA1_Channel2_IRQHandler(void)
  148. {
  149. /* USER CODE BEGIN DMA1_Channel2_IRQn 0 */
  150. /* USER CODE END DMA1_Channel2_IRQn 0 */
  151. HAL_DMA_IRQHandler(&hdma_usart3_tx);
  152. /* USER CODE BEGIN DMA1_Channel2_IRQn 1 */
  153. /* USER CODE END DMA1_Channel2_IRQn 1 */
  154. }
  155. /**
  156. * @brief This function handles DMA1 channel3 global interrupt.
  157. */
  158. void DMA1_Channel3_IRQHandler(void)
  159. {
  160. /* USER CODE BEGIN DMA1_Channel3_IRQn 0 */
  161. /* USER CODE END DMA1_Channel3_IRQn 0 */
  162. HAL_DMA_IRQHandler(&hdma_usart3_rx);
  163. /* USER CODE BEGIN DMA1_Channel3_IRQn 1 */
  164. /* USER CODE END DMA1_Channel3_IRQn 1 */
  165. }
  166. /**
  167. * @brief This function handles DMA1 channel4 global interrupt.
  168. */
  169. void DMA1_Channel4_IRQHandler(void)
  170. {
  171. /* USER CODE BEGIN DMA1_Channel4_IRQn 0 */
  172. /* USER CODE END DMA1_Channel4_IRQn 0 */
  173. HAL_DMA_IRQHandler(&hdma_usart1_tx);
  174. /* USER CODE BEGIN DMA1_Channel4_IRQn 1 */
  175. /* USER CODE END DMA1_Channel4_IRQn 1 */
  176. }
  177. /**
  178. * @brief This function handles DMA1 channel5 global interrupt.
  179. */
  180. void DMA1_Channel5_IRQHandler(void)
  181. {
  182. /* USER CODE BEGIN DMA1_Channel5_IRQn 0 */
  183. /* USER CODE END DMA1_Channel5_IRQn 0 */
  184. HAL_DMA_IRQHandler(&hdma_usart1_rx);
  185. /* USER CODE BEGIN DMA1_Channel5_IRQn 1 */
  186. /* USER CODE END DMA1_Channel5_IRQn 1 */
  187. }
  188. /**
  189. * @brief This function handles DMA1 channel6 global interrupt.
  190. */
  191. void DMA1_Channel6_IRQHandler(void)
  192. {
  193. /* USER CODE BEGIN DMA1_Channel6_IRQn 0 */
  194. /* USER CODE END DMA1_Channel6_IRQn 0 */
  195. HAL_DMA_IRQHandler(&hdma_usart2_rx);
  196. /* USER CODE BEGIN DMA1_Channel6_IRQn 1 */
  197. /* USER CODE END DMA1_Channel6_IRQn 1 */
  198. }
  199. /**
  200. * @brief This function handles DMA1 channel7 global interrupt.
  201. */
  202. void DMA1_Channel7_IRQHandler(void)
  203. {
  204. /* USER CODE BEGIN DMA1_Channel7_IRQn 0 */
  205. /* USER CODE END DMA1_Channel7_IRQn 0 */
  206. HAL_DMA_IRQHandler(&hdma_usart2_tx);
  207. /* USER CODE BEGIN DMA1_Channel7_IRQn 1 */
  208. /* USER CODE END DMA1_Channel7_IRQn 1 */
  209. }
  210. /**
  211. * @brief This function handles USART1 global interrupt.
  212. */
  213. void USART1_IRQHandler(void)
  214. {
  215. /* USER CODE BEGIN USART1_IRQn 0 */
  216. /* USER CODE END USART1_IRQn 0 */
  217. HAL_UART_IRQHandler(&huart1);
  218. /* USER CODE BEGIN USART1_IRQn 1 */
  219. USART_IRQHandler(&huart1);
  220. /* USER CODE END USART1_IRQn 1 */
  221. }
  222. /**
  223. * @brief This function handles USART2 global interrupt.
  224. */
  225. void USART2_IRQHandler(void)
  226. {
  227. /* USER CODE BEGIN USART2_IRQn 0 */
  228. /* USER CODE END USART2_IRQn 0 */
  229. HAL_UART_IRQHandler(&huart2);
  230. /* USER CODE BEGIN USART2_IRQn 1 */
  231. USART_IRQHandler(&huart2);
  232. /* USER CODE END USART2_IRQn 1 */
  233. }
  234. /**
  235. * @brief This function handles USART3 global interrupt.
  236. */
  237. void USART3_IRQHandler(void)
  238. {
  239. /* USER CODE BEGIN USART3_IRQn 0 */
  240. /* USER CODE END USART3_IRQn 0 */
  241. HAL_UART_IRQHandler(&huart3);
  242. /* USER CODE BEGIN USART3_IRQn 1 */
  243. USART_IRQHandler(&huart3);
  244. /* USER CODE END USART3_IRQn 1 */
  245. }
  246. /**
  247. * @brief This function handles UART4 global interrupt.
  248. */
  249. void UART4_IRQHandler(void)
  250. {
  251. /* USER CODE BEGIN UART4_IRQn 0 */
  252. /* USER CODE END UART4_IRQn 0 */
  253. HAL_UART_IRQHandler(&huart4);
  254. /* USER CODE BEGIN UART4_IRQn 1 */
  255. USART_IRQHandler(&huart4);
  256. /* USER CODE END UART4_IRQn 1 */
  257. }
  258. /**
  259. * @brief This function handles UART5 global interrupt.
  260. */
  261. void UART5_IRQHandler(void)
  262. {
  263. /* USER CODE BEGIN UART5_IRQn 0 */
  264. /* USER CODE END UART5_IRQn 0 */
  265. HAL_UART_IRQHandler(&huart5);
  266. /* USER CODE BEGIN UART5_IRQn 1 */
  267. /* USER CODE END UART5_IRQn 1 */
  268. }
  269. /**
  270. * @brief This function handles DMA2 channel3 global interrupt.
  271. */
  272. void DMA2_Channel3_IRQHandler(void)
  273. {
  274. /* USER CODE BEGIN DMA2_Channel3_IRQn 0 */
  275. /* USER CODE END DMA2_Channel3_IRQn 0 */
  276. HAL_DMA_IRQHandler(&hdma_uart4_rx);
  277. /* USER CODE BEGIN DMA2_Channel3_IRQn 1 */
  278. /* USER CODE END DMA2_Channel3_IRQn 1 */
  279. }
  280. /**
  281. * @brief This function handles DMA2 channel4 and channel5 global interrupts.
  282. */
  283. void DMA2_Channel4_5_IRQHandler(void)
  284. {
  285. /* USER CODE BEGIN DMA2_Channel4_5_IRQn 0 */
  286. /* USER CODE END DMA2_Channel4_5_IRQn 0 */
  287. HAL_DMA_IRQHandler(&hdma_uart4_tx);
  288. /* USER CODE BEGIN DMA2_Channel4_5_IRQn 1 */
  289. /* USER CODE END DMA2_Channel4_5_IRQn 1 */
  290. }
  291. /* USER CODE BEGIN 1 */
  292. void USART_IRQHandler(UART_HandleTypeDef *huart)
  293. {
  294. uint32_t tmp_flag = 0;
  295. uint32_t temp;
  296. extern unsigned char inMenu;
  297. tmp_flag =__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE); //获取IDLE标志位
  298. if((tmp_flag != RESET))//idle标志被置位
  299. {
  300. __HAL_UART_CLEAR_IDLEFLAG(huart);//清除标志位
  301. HAL_UART_DMAStop(huart); //
  302. rx_len = 0;
  303. if (huart->Instance == USART1){
  304. temp = __HAL_DMA_GET_COUNTER(&hdma_usart1_rx);// 获取DMA中未传输的数据个数
  305. rx_len = BUFFER_SIZE - temp; //总计数减去未传输的数据个数,得到已经接收的数据个数
  306. for (int i = 0; i < rx_len; i++)
  307. USART_Push(1, rx_buffer1[i]);
  308. rt_memset(rx_buffer1, 0, BUFFER_SIZE);
  309. HAL_UART_Receive_DMA(huart, rx_buffer1, BUFFER_SIZE);
  310. }
  311. else if (huart->Instance == USART2){
  312. temp = __HAL_DMA_GET_COUNTER(&hdma_usart2_rx);// 获取DMA中未传输的数据个数
  313. rx_len = BUFFER_SIZE - temp; //总计数减去未传输的数据个数,得到已经接收的数据个数
  314. if (inMenu==1) {
  315. if (rx_buffer2[0]==0x4F && rx_buffer2[1]==0x4B && rx_buffer2[2]==0x0D)
  316. rt_sem_release(uart2_lock);
  317. } else {
  318. for (int i = 0; i < rx_len; i++)
  319. USART_Push(2, rx_buffer2[i]);
  320. }
  321. rt_memset(rx_buffer2, 0, BUFFER_SIZE);
  322. HAL_UART_Receive_DMA(huart, rx_buffer2, BUFFER_SIZE);
  323. }
  324. else if (huart->Instance == USART3){
  325. temp = __HAL_DMA_GET_COUNTER(&hdma_usart3_rx);// 获取DMA中未传输的数据个数
  326. rx_len = BUFFER_SIZE - temp; //总计数减去未传输的数据个数,得到已经接收的数据个数
  327. if (inMenu==1) {
  328. if (rx_buffer3[0]==0x4F && rx_buffer3[1]==0x4B && rx_buffer3[2]==0x0D)
  329. rt_sem_release(uart3_lock);
  330. } else{
  331. for (int i = 0; i < rx_len; i++)
  332. USART_Push(3, rx_buffer3[i]);
  333. }
  334. rt_memset(rx_buffer3, 0, BUFFER_SIZE);
  335. HAL_UART_Receive_DMA(huart, rx_buffer3, BUFFER_SIZE);
  336. }
  337. else if (huart->Instance == UART4){
  338. temp = __HAL_DMA_GET_COUNTER(&hdma_uart4_rx);// 获取DMA中未传输的数据个数
  339. rx_len = BUFFER_SIZE - temp; //总计数减去未传输的数据个数,得到已经接收的数据个数
  340. for (int i = 0; i < rx_len; i++)
  341. USART_Push(4, rx_buffer4[i]);
  342. rt_memset(rx_buffer4, 0, BUFFER_SIZE);
  343. HAL_UART_Receive_DMA(huart, rx_buffer4, BUFFER_SIZE);
  344. }
  345. if (rx_len>0) rt_sem_release(thread_EmptyBuffer_sem);
  346. }
  347. }
  348. /* USER CODE END 1 */
  349. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/