stm32f1xx_it.c 11 KB

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