stm32f1xx_it.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file stm32f1xx_it.c
  5. * @brief Interrupt Service Routines.
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2022 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. #include "uart.h"
  25. /* USER CODE END Includes */
  26. /* Private typedef -----------------------------------------------------------*/
  27. /* USER CODE BEGIN TD */
  28. /* USER CODE END TD */
  29. /* Private define ------------------------------------------------------------*/
  30. /* USER CODE BEGIN PD */
  31. /* USER CODE END PD */
  32. /* Private macro -------------------------------------------------------------*/
  33. /* USER CODE BEGIN PM */
  34. /* USER CODE END PM */
  35. /* Private variables ---------------------------------------------------------*/
  36. /* USER CODE BEGIN PV */
  37. uint32_t adcCounter=0;
  38. int maxWaveDiff = 0; // Stores the latest measured ADC Wave data (MAX-MIN)
  39. /* USER CODE END PV */
  40. /* Private function prototypes -----------------------------------------------*/
  41. /* USER CODE BEGIN PFP */
  42. /* USER CODE END PFP */
  43. /* Private user code ---------------------------------------------------------*/
  44. /* USER CODE BEGIN 0 */
  45. /* USER CODE END 0 */
  46. /* External variables --------------------------------------------------------*/
  47. extern ADC_HandleTypeDef hadc1;
  48. extern UART_HandleTypeDef huart2;
  49. /* USER CODE BEGIN EV */
  50. /* USER CODE END EV */
  51. /******************************************************************************/
  52. /* Cortex-M3 Processor Interruption and Exception Handlers */
  53. /******************************************************************************/
  54. /**
  55. * @brief This function handles Non maskable interrupt.
  56. */
  57. void NMI_Handler(void)
  58. {
  59. /* USER CODE BEGIN NonMaskableInt_IRQn 0 */
  60. /* USER CODE END NonMaskableInt_IRQn 0 */
  61. /* USER CODE BEGIN NonMaskableInt_IRQn 1 */
  62. while (1)
  63. {
  64. }
  65. /* USER CODE END NonMaskableInt_IRQn 1 */
  66. }
  67. /**
  68. * @brief This function handles Hard fault interrupt.
  69. */
  70. void HardFault_Handler(void)
  71. {
  72. /* USER CODE BEGIN HardFault_IRQn 0 */
  73. /* USER CODE END HardFault_IRQn 0 */
  74. while (1)
  75. {
  76. /* USER CODE BEGIN W1_HardFault_IRQn 0 */
  77. /* USER CODE END W1_HardFault_IRQn 0 */
  78. }
  79. }
  80. /**
  81. * @brief This function handles Memory management fault.
  82. */
  83. void MemManage_Handler(void)
  84. {
  85. /* USER CODE BEGIN MemoryManagement_IRQn 0 */
  86. /* USER CODE END MemoryManagement_IRQn 0 */
  87. while (1)
  88. {
  89. /* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */
  90. /* USER CODE END W1_MemoryManagement_IRQn 0 */
  91. }
  92. }
  93. /**
  94. * @brief This function handles Prefetch fault, memory access fault.
  95. */
  96. void BusFault_Handler(void)
  97. {
  98. /* USER CODE BEGIN BusFault_IRQn 0 */
  99. /* USER CODE END BusFault_IRQn 0 */
  100. while (1)
  101. {
  102. /* USER CODE BEGIN W1_BusFault_IRQn 0 */
  103. /* USER CODE END W1_BusFault_IRQn 0 */
  104. }
  105. }
  106. /**
  107. * @brief This function handles Undefined instruction or illegal state.
  108. */
  109. void UsageFault_Handler(void)
  110. {
  111. /* USER CODE BEGIN UsageFault_IRQn 0 */
  112. /* USER CODE END UsageFault_IRQn 0 */
  113. while (1)
  114. {
  115. /* USER CODE BEGIN W1_UsageFault_IRQn 0 */
  116. /* USER CODE END W1_UsageFault_IRQn 0 */
  117. }
  118. }
  119. /**
  120. * @brief This function handles System service call via SWI instruction.
  121. */
  122. void SVC_Handler(void)
  123. {
  124. /* USER CODE BEGIN SVCall_IRQn 0 */
  125. /* USER CODE END SVCall_IRQn 0 */
  126. /* USER CODE BEGIN SVCall_IRQn 1 */
  127. /* USER CODE END SVCall_IRQn 1 */
  128. }
  129. /**
  130. * @brief This function handles Debug monitor.
  131. */
  132. void DebugMon_Handler(void)
  133. {
  134. /* USER CODE BEGIN DebugMonitor_IRQn 0 */
  135. /* USER CODE END DebugMonitor_IRQn 0 */
  136. /* USER CODE BEGIN DebugMonitor_IRQn 1 */
  137. /* USER CODE END DebugMonitor_IRQn 1 */
  138. }
  139. /**
  140. * @brief This function handles Pendable request for system service.
  141. */
  142. void PendSV_Handler(void)
  143. {
  144. /* USER CODE BEGIN PendSV_IRQn 0 */
  145. /* USER CODE END PendSV_IRQn 0 */
  146. /* USER CODE BEGIN PendSV_IRQn 1 */
  147. /* USER CODE END PendSV_IRQn 1 */
  148. }
  149. /**
  150. * @brief This function handles System tick timer.
  151. */
  152. void SysTick_Handler(void)
  153. {
  154. /* USER CODE BEGIN SysTick_IRQn 0 */
  155. /* USER CODE END SysTick_IRQn 0 */
  156. HAL_IncTick();
  157. /* USER CODE BEGIN SysTick_IRQn 1 */
  158. /* USER CODE END SysTick_IRQn 1 */
  159. }
  160. /******************************************************************************/
  161. /* STM32F1xx Peripheral Interrupt Handlers */
  162. /* Add here the Interrupt Handlers for the used peripherals. */
  163. /* For the available peripheral interrupt handler names, */
  164. /* please refer to the startup file (startup_stm32f1xx.s). */
  165. /******************************************************************************/
  166. /**
  167. * @brief This function handles ADC1 and ADC2 global interrupts.
  168. */
  169. void ADC1_2_IRQHandler(void)
  170. {
  171. /* USER CODE BEGIN ADC1_2_IRQn 0 */
  172. /**
  173. * Test with osc wave out 50Hz Sine 2160mV (according to Osc measurement)
  174. *
  175. * ADC is 0-3.3V on 12 bits = 4096 bits = 0,8056640625mV / bit
  176. *
  177. * max-min measured in ADC of 2605 = 2098,7mV so a small diff of 61mV
  178. */
  179. static uint16_t adcData = 0;
  180. static uint16_t minValue = 0xFFF;
  181. static uint16_t maxValue = 0;
  182. static uint16_t minCounter = 0;
  183. static uint32_t cnt = 10000; // 205 882,35Hz = 4 117,6 samples/wave. 10000 samples = 48mS = 2.4 Waves
  184. adcData = ADC1->DR;
  185. if( cnt > 0 ) {
  186. if( adcData > maxValue ) {
  187. maxValue = adcData;
  188. }
  189. if( adcData < minValue ) {
  190. minValue = adcData;
  191. minCounter++;
  192. }
  193. cnt--;
  194. }
  195. else {
  196. cnt = 10000;
  197. maxWaveDiff = (int)maxValue - (int)minValue;
  198. maxValue = 0;
  199. minValue = 0xFFF;
  200. minCounter = 0;
  201. adcCounter++;
  202. }
  203. /* USER CODE END ADC1_2_IRQn 0 */
  204. /* USER CODE BEGIN ADC1_2_IRQn 1 */
  205. /* USER CODE END ADC1_2_IRQn 1 */
  206. }
  207. /**
  208. * @brief This function handles USART2 global interrupt.
  209. */
  210. void USART2_IRQHandler(void)
  211. {
  212. /* USER CODE BEGIN USART2_IRQn 0 */
  213. usartInterrupt();
  214. /* USER CODE END USART2_IRQn 0 */
  215. /* USER CODE BEGIN USART2_IRQn 1 */
  216. /* USER CODE END USART2_IRQn 1 */
  217. }
  218. /* USER CODE BEGIN 1 */
  219. /* USER CODE END 1 */