stm32f1xx_it.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. /* 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 DMA_HandleTypeDef hdma_uart4_rx;
  49. extern DMA_HandleTypeDef hdma_uart4_tx;
  50. extern DMA_HandleTypeDef hdma_usart1_rx;
  51. extern DMA_HandleTypeDef hdma_usart1_tx;
  52. extern DMA_HandleTypeDef hdma_usart2_rx;
  53. extern DMA_HandleTypeDef hdma_usart2_tx;
  54. extern DMA_HandleTypeDef hdma_usart3_rx;
  55. extern DMA_HandleTypeDef hdma_usart3_tx;
  56. extern UART_HandleTypeDef huart4;
  57. extern UART_HandleTypeDef huart1;
  58. extern UART_HandleTypeDef huart2;
  59. extern UART_HandleTypeDef huart3;
  60. /* USER CODE BEGIN EV */
  61. extern void USART_IRQHandler(UART_HandleTypeDef *huart);
  62. extern void USART_IRQHandler2(UART_HandleTypeDef *huart);
  63. /* USER CODE END EV */
  64. /******************************************************************************/
  65. /* Cortex-M3 Processor Interruption and Exception Handlers */
  66. /******************************************************************************/
  67. /**
  68. * @brief This function handles Non maskable interrupt.
  69. */
  70. void NMI_Handler(void)
  71. {
  72. /* USER CODE BEGIN NonMaskableInt_IRQn 0 */
  73. /* USER CODE END NonMaskableInt_IRQn 0 */
  74. /* USER CODE BEGIN NonMaskableInt_IRQn 1 */
  75. /* USER CODE END NonMaskableInt_IRQn 1 */
  76. }
  77. /**
  78. * @brief This function handles Memory management fault.
  79. */
  80. void MemManage_Handler(void)
  81. {
  82. /* USER CODE BEGIN MemoryManagement_IRQn 0 */
  83. /* USER CODE END MemoryManagement_IRQn 0 */
  84. while (1)
  85. {
  86. /* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */
  87. /* USER CODE END W1_MemoryManagement_IRQn 0 */
  88. }
  89. }
  90. /**
  91. * @brief This function handles Prefetch fault, memory access fault.
  92. */
  93. void BusFault_Handler(void)
  94. {
  95. /* USER CODE BEGIN BusFault_IRQn 0 */
  96. /* USER CODE END BusFault_IRQn 0 */
  97. while (1)
  98. {
  99. /* USER CODE BEGIN W1_BusFault_IRQn 0 */
  100. /* USER CODE END W1_BusFault_IRQn 0 */
  101. }
  102. }
  103. /**
  104. * @brief This function handles Undefined instruction or illegal state.
  105. */
  106. void UsageFault_Handler(void)
  107. {
  108. /* USER CODE BEGIN UsageFault_IRQn 0 */
  109. /* USER CODE END UsageFault_IRQn 0 */
  110. while (1)
  111. {
  112. /* USER CODE BEGIN W1_UsageFault_IRQn 0 */
  113. /* USER CODE END W1_UsageFault_IRQn 0 */
  114. }
  115. }
  116. /**
  117. * @brief This function handles System service call via SWI instruction.
  118. */
  119. void SVC_Handler(void)
  120. {
  121. /* USER CODE BEGIN SVCall_IRQn 0 */
  122. /* USER CODE END SVCall_IRQn 0 */
  123. /* USER CODE BEGIN SVCall_IRQn 1 */
  124. /* USER CODE END SVCall_IRQn 1 */
  125. }
  126. /**
  127. * @brief This function handles Debug monitor.
  128. */
  129. void DebugMon_Handler(void)
  130. {
  131. /* USER CODE BEGIN DebugMonitor_IRQn 0 */
  132. /* USER CODE END DebugMonitor_IRQn 0 */
  133. /* USER CODE BEGIN DebugMonitor_IRQn 1 */
  134. /* USER CODE END DebugMonitor_IRQn 1 */
  135. }
  136. /******************************************************************************/
  137. /* STM32F1xx Peripheral Interrupt Handlers */
  138. /* Add here the Interrupt Handlers for the used peripherals. */
  139. /* For the available peripheral interrupt handler names, */
  140. /* please refer to the startup file (startup_stm32f1xx.s). */
  141. /******************************************************************************/
  142. /**
  143. * @brief This function handles DMA1 channel2 global interrupt.
  144. */
  145. void DMA1_Channel2_IRQHandler(void)
  146. {
  147. /* USER CODE BEGIN DMA1_Channel2_IRQn 0 */
  148. /* USER CODE END DMA1_Channel2_IRQn 0 */
  149. HAL_DMA_IRQHandler(&hdma_usart3_tx);
  150. /* USER CODE BEGIN DMA1_Channel2_IRQn 1 */
  151. /* USER CODE END DMA1_Channel2_IRQn 1 */
  152. }
  153. /**
  154. * @brief This function handles DMA1 channel3 global interrupt.
  155. */
  156. void DMA1_Channel3_IRQHandler(void)
  157. {
  158. /* USER CODE BEGIN DMA1_Channel3_IRQn 0 */
  159. /* USER CODE END DMA1_Channel3_IRQn 0 */
  160. HAL_DMA_IRQHandler(&hdma_usart3_rx);
  161. /* USER CODE BEGIN DMA1_Channel3_IRQn 1 */
  162. /* USER CODE END DMA1_Channel3_IRQn 1 */
  163. }
  164. /**
  165. * @brief This function handles DMA1 channel4 global interrupt.
  166. */
  167. void DMA1_Channel4_IRQHandler(void)
  168. {
  169. /* USER CODE BEGIN DMA1_Channel4_IRQn 0 */
  170. /* USER CODE END DMA1_Channel4_IRQn 0 */
  171. HAL_DMA_IRQHandler(&hdma_usart1_tx);
  172. /* USER CODE BEGIN DMA1_Channel4_IRQn 1 */
  173. /* USER CODE END DMA1_Channel4_IRQn 1 */
  174. }
  175. /**
  176. * @brief This function handles DMA1 channel5 global interrupt.
  177. */
  178. void DMA1_Channel5_IRQHandler(void)
  179. {
  180. /* USER CODE BEGIN DMA1_Channel5_IRQn 0 */
  181. /* USER CODE END DMA1_Channel5_IRQn 0 */
  182. HAL_DMA_IRQHandler(&hdma_usart1_rx);
  183. /* USER CODE BEGIN DMA1_Channel5_IRQn 1 */
  184. /* USER CODE END DMA1_Channel5_IRQn 1 */
  185. }
  186. /**
  187. * @brief This function handles DMA1 channel6 global interrupt.
  188. */
  189. void DMA1_Channel6_IRQHandler(void)
  190. {
  191. /* USER CODE BEGIN DMA1_Channel6_IRQn 0 */
  192. /* USER CODE END DMA1_Channel6_IRQn 0 */
  193. HAL_DMA_IRQHandler(&hdma_usart2_rx);
  194. /* USER CODE BEGIN DMA1_Channel6_IRQn 1 */
  195. /* USER CODE END DMA1_Channel6_IRQn 1 */
  196. }
  197. /**
  198. * @brief This function handles DMA1 channel7 global interrupt.
  199. */
  200. void DMA1_Channel7_IRQHandler(void)
  201. {
  202. /* USER CODE BEGIN DMA1_Channel7_IRQn 0 */
  203. /* USER CODE END DMA1_Channel7_IRQn 0 */
  204. HAL_DMA_IRQHandler(&hdma_usart2_tx);
  205. /* USER CODE BEGIN DMA1_Channel7_IRQn 1 */
  206. /* USER CODE END DMA1_Channel7_IRQn 1 */
  207. }
  208. /**
  209. * @brief This function handles USART1 global interrupt.
  210. */
  211. void USART1_IRQHandler(void)
  212. {
  213. /* USER CODE BEGIN USART1_IRQn 0 */
  214. /* USER CODE END USART1_IRQn 0 */
  215. HAL_UART_IRQHandler(&huart1);
  216. /* USER CODE BEGIN USART1_IRQn 1 */
  217. USART_IRQHandler(&huart1);
  218. /* USER CODE END USART1_IRQn 1 */
  219. }
  220. /**
  221. * @brief This function handles USART2 global interrupt.
  222. */
  223. void USART2_IRQHandler(void)
  224. {
  225. /* USER CODE BEGIN USART2_IRQn 0 */
  226. /* USER CODE END USART2_IRQn 0 */
  227. HAL_UART_IRQHandler(&huart2);
  228. /* USER CODE BEGIN USART2_IRQn 1 */
  229. USART_IRQHandler(&huart2);
  230. /* USER CODE END USART2_IRQn 1 */
  231. }
  232. /**
  233. * @brief This function handles USART3 global interrupt.
  234. */
  235. void USART3_IRQHandler(void)
  236. {
  237. /* USER CODE BEGIN USART3_IRQn 0 */
  238. /* USER CODE END USART3_IRQn 0 */
  239. HAL_UART_IRQHandler(&huart3);
  240. /* USER CODE BEGIN USART3_IRQn 1 */
  241. USART_IRQHandler(&huart3);
  242. /* USER CODE END USART3_IRQn 1 */
  243. }
  244. /**
  245. * @brief This function handles UART4 global interrupt.
  246. */
  247. void UART4_IRQHandler(void)
  248. {
  249. /* USER CODE BEGIN UART4_IRQn 0 */
  250. /* USER CODE END UART4_IRQn 0 */
  251. HAL_UART_IRQHandler(&huart4);
  252. /* USER CODE BEGIN UART4_IRQn 1 */
  253. USART_IRQHandler(&huart4);
  254. /* USER CODE END UART4_IRQn 1 */
  255. }
  256. /**
  257. * @brief This function handles DMA2 channel3 global interrupt.
  258. */
  259. void DMA2_Channel3_IRQHandler(void)
  260. {
  261. /* USER CODE BEGIN DMA2_Channel3_IRQn 0 */
  262. /* USER CODE END DMA2_Channel3_IRQn 0 */
  263. HAL_DMA_IRQHandler(&hdma_uart4_rx);
  264. /* USER CODE BEGIN DMA2_Channel3_IRQn 1 */
  265. /* USER CODE END DMA2_Channel3_IRQn 1 */
  266. }
  267. /**
  268. * @brief This function handles DMA2 channel4 and channel5 global interrupts.
  269. */
  270. void DMA2_Channel4_5_IRQHandler(void)
  271. {
  272. /* USER CODE BEGIN DMA2_Channel4_5_IRQn 0 */
  273. /* USER CODE END DMA2_Channel4_5_IRQn 0 */
  274. HAL_DMA_IRQHandler(&hdma_uart4_tx);
  275. /* USER CODE BEGIN DMA2_Channel4_5_IRQn 1 */
  276. /* USER CODE END DMA2_Channel4_5_IRQn 1 */
  277. }
  278. /* USER CODE BEGIN 1 */
  279. void USART_IRQHandler(UART_HandleTypeDef *huart)
  280. {
  281. uint32_t tmp_flag = 0;
  282. uint32_t temp;
  283. extern unsigned char inMenu;
  284. tmp_flag =__HAL_UART_GET_FLAG(huart, UART_FLAG_IDLE); //获取IDLE标志位
  285. if((tmp_flag != RESET))//idle标志被置位
  286. {
  287. __HAL_UART_CLEAR_IDLEFLAG(huart);//清除标志位
  288. HAL_UART_DMAStop(huart); //
  289. rx_len = 0;
  290. if (huart->Instance == USART1){
  291. temp = __HAL_DMA_GET_COUNTER(&hdma_usart1_rx);// 获取DMA中未传输的数据个数
  292. rx_len = BUFFER_SIZE - temp; //总计数减去未传输的数据个数,得到已经接收的数据个数
  293. for (int i = 0; i < rx_len; i++)
  294. USART_Push(1, rx_buffer1[i]);
  295. rt_memset(rx_buffer1, 0, BUFFER_SIZE);
  296. HAL_UART_Receive_DMA(huart, rx_buffer1, BUFFER_SIZE);
  297. }
  298. else if (huart->Instance == USART2){
  299. temp = __HAL_DMA_GET_COUNTER(&hdma_usart2_rx);// 获取DMA中未传输的数据个数
  300. rx_len = BUFFER_SIZE - temp; //总计数减去未传输的数据个数,得到已经接收的数据个数
  301. if (inMenu==1) {
  302. if (rx_buffer2[0]==0x4F && rx_buffer2[1]==0x4B && rx_buffer2[2]==0x0D)
  303. rt_sem_release(uart2_lock);
  304. } else {
  305. for (int i = 0; i < rx_len; i++)
  306. USART_Push(2, rx_buffer2[i]);
  307. }
  308. rt_memset(rx_buffer2, 0, BUFFER_SIZE);
  309. HAL_UART_Receive_DMA(huart, rx_buffer2, BUFFER_SIZE);
  310. }
  311. else if (huart->Instance == USART3){
  312. temp = __HAL_DMA_GET_COUNTER(&hdma_usart3_rx);// 获取DMA中未传输的数据个数
  313. rx_len = BUFFER_SIZE - temp; //总计数减去未传输的数据个数,得到已经接收的数据个数
  314. if (inMenu==1) {
  315. if (rx_buffer3[0]==0x4F && rx_buffer3[1]==0x4B && rx_buffer3[2]==0x0D)
  316. rt_sem_release(uart3_lock);
  317. } else{
  318. for (int i = 0; i < rx_len; i++)
  319. USART_Push(3, rx_buffer3[i]);
  320. }
  321. rt_memset(rx_buffer3, 0, BUFFER_SIZE);
  322. HAL_UART_Receive_DMA(huart, rx_buffer3, BUFFER_SIZE);
  323. }
  324. else if (huart->Instance == UART4){
  325. temp = __HAL_DMA_GET_COUNTER(&hdma_uart4_rx);// 获取DMA中未传输的数据个数
  326. rx_len = BUFFER_SIZE - temp; //总计数减去未传输的数据个数,得到已经接收的数据个数
  327. for (int i = 0; i < rx_len; i++)
  328. USART_Push(4, rx_buffer4[i]);
  329. rt_memset(rx_buffer4, 0, BUFFER_SIZE);
  330. HAL_UART_Receive_DMA(huart, rx_buffer4, BUFFER_SIZE);
  331. }
  332. if (rx_len>0) rt_sem_release(thread_EmptyBuffer_sem);
  333. }
  334. }
  335. /* USER CODE END 1 */
  336. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/