source: trunk/firmware/CubeMX/Src/stm32g0xx_it.c@ 2

Last change on this file since 2 was 1, checked in by f.jahn, 3 years ago
File size: 13.6 KB
RevLine 
[1]1/* USER CODE BEGIN Header */
2/**
3 ******************************************************************************
4 * @file stm32g0xx_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
21/* Includes ------------------------------------------------------------------*/
22#include "main.h"
23#include "stm32g0xx_it.h"
24/* Private includes ----------------------------------------------------------*/
25/* USER CODE BEGIN Includes */
26
27#include "modbus.h"
28#include "SEGGER_RTT.h"
29#include "dac.h"
30#include "log.h"
31#include "precharge.h"
32
33/* USER CODE END Includes */
34
35/* Private typedef -----------------------------------------------------------*/
36/* USER CODE BEGIN TD */
37
38/* USER CODE END TD */
39
40/* Private define ------------------------------------------------------------*/
41/* USER CODE BEGIN PD */
42
43#define TAG "ISR "
44
45/* USER CODE END PD */
46
47/* Private macro -------------------------------------------------------------*/
48/* USER CODE BEGIN PM */
49
50/* USER CODE END PM */
51
52/* Private variables ---------------------------------------------------------*/
53/* USER CODE BEGIN PV */
54
55/* USER CODE END PV */
56
57/* Private function prototypes -----------------------------------------------*/
58/* USER CODE BEGIN PFP */
59
60/* USER CODE END PFP */
61
62/* Private user code ---------------------------------------------------------*/
63/* USER CODE BEGIN 0 */
64
65/* USER CODE END 0 */
66
67/* External variables --------------------------------------------------------*/
68extern DMA_HandleTypeDef hdma_adc1;
69extern ADC_HandleTypeDef hadc1;
70extern DAC_HandleTypeDef hdac1;
71extern TIM_HandleTypeDef htim6;
72extern TIM_HandleTypeDef htim7;
73extern TIM_HandleTypeDef htim14;
74extern TIM_HandleTypeDef htim15;
75extern TIM_HandleTypeDef htim16;
76extern TIM_HandleTypeDef htim17;
77extern UART_HandleTypeDef huart1;
78/* USER CODE BEGIN EV */
79
80extern uint16_t ADC_values[ADC_CHANNELS];
81extern uint16_t rawMOSFETsVoltageDrop;
82extern int32_t rawContactVoltageDropPlus;
83extern int32_t rawContactVoltageDropMinus;
84extern int command_parser_is_enabled;
85extern void DoNothing(void);
86extern void OpenBothMOSFETSVeryFast(void);
87extern void (*MOSFETS_Management)(void); // function pointer
88extern void (*ExternalGreenLED_Management)(void);
89extern sys_data_t sys_data;
90extern void ADC_Open_Both_MOSFETs(void);
91extern void ADC_Close_Both_MOSFETs(void);
92extern void TurnExternalGreenLEDOff(void);
93extern void TurnExternalGreenLEDOn(void);
94extern void (*AUTO_Mode)(uint32_t, int); // Function pointer that contains function that is executed when gSwitch is in AUTO mode (depends on DIP switches)
95extern void EnterPowerSavingMode(void);
96extern void ExternalGreenLEDShortBlinking(void);
97extern int overcurrent_shutdown_is_active;
98extern int overload_shutdown_is_active;
99extern uint32_t overcurrent_shutdown_time;
100//extern uint32_t overload_shutdown_time;
101extern void (*InrushCurrentManagement)(void);
102
103//extern uint16_t i_samples[I_RMS_SAMPLES_COUNT];
104//extern uint16_t d_samples[I_RMS_SAMPLES_COUNT];
105//extern uint16_t u_samples[I_RMS_SAMPLES_COUNT];
106//extern volatile int32_t i_samples_counter;
107extern int statDataChanged;
108extern volatile uint32_t maxIntegral;
109
110/* USER CODE END EV */
111
112/******************************************************************************/
113/* Cortex-M0+ Processor Interruption and Exception Handlers */
114/******************************************************************************/
115/**
116 * @brief This function handles Non maskable interrupt.
117 */
118void NMI_Handler(void)
119{
120 /* USER CODE BEGIN NonMaskableInt_IRQn 0 */
121
122 /* USER CODE END NonMaskableInt_IRQn 0 */
123 HAL_RCC_NMI_IRQHandler();
124 /* USER CODE BEGIN NonMaskableInt_IRQn 1 */
125
126 while (1)
127 {
128 //OpenBothMOSFETS();
129 HAL_GPIO_TogglePin(LED_ERROR_GPIO_Port, LED_ERROR_Pin);
130 SEGGER_RTT_WriteString(0, "NMI ERROR!!!\n");
131 }
132
133 /* USER CODE END NonMaskableInt_IRQn 1 */
134}
135
136/**
137 * @brief This function handles Hard fault interrupt.
138 */
139void HardFault_Handler(void)
140{
141 /* USER CODE BEGIN HardFault_IRQn 0 */
142 SEGGER_RTT_WriteString(0, RTT_CTRL_TEXT_BRIGHT_RED);
143 SEGGER_RTT_WriteString(0, "Hardfault ERROR!!!\n");
144 /* USER CODE END HardFault_IRQn 0 */
145 while (1)
146 {
147 /* USER CODE BEGIN W1_HardFault_IRQn 0 */
148 HAL_GPIO_TogglePin(LED_ERROR_GPIO_Port, LED_ERROR_Pin);
149 //OpenBothMOSFETS();
150
151 /* USER CODE END W1_HardFault_IRQn 0 */
152 }
153}
154
155/**
156 * @brief This function handles System service call via SWI instruction.
157 */
158void SVC_Handler(void)
159{
160 /* USER CODE BEGIN SVC_IRQn 0 */
161
162 /* USER CODE END SVC_IRQn 0 */
163 /* USER CODE BEGIN SVC_IRQn 1 */
164
165 /* USER CODE END SVC_IRQn 1 */
166}
167
168/**
169 * @brief This function handles Pendable request for system service.
170 */
171void PendSV_Handler(void)
172{
173 /* USER CODE BEGIN PendSV_IRQn 0 */
174
175 /* USER CODE END PendSV_IRQn 0 */
176 /* USER CODE BEGIN PendSV_IRQn 1 */
177
178 /* USER CODE END PendSV_IRQn 1 */
179}
180
181/**
182 * @brief This function handles System tick timer.
183 */
184#ifdef USE_RAM_FUNC
185__RAM_FUNC void SysTick_Handler(void)
186#else
187void SysTick_Handler(void)
188#endif
189{
190 /* USER CODE BEGIN SysTick_IRQn 0 */
191 //LED_ERROR_GPIO_Port->BSRR = LED_ERROR_Pin;
192 /* USER CODE END SysTick_IRQn 0 */
193 HAL_IncTick();
194 /* USER CODE BEGIN SysTick_IRQn 1 */
195 //LED_ERROR_GPIO_Port->BRR = LED_ERROR_Pin;
196 /* USER CODE END SysTick_IRQn 1 */
197}
198
199/******************************************************************************/
200/* STM32G0xx Peripheral Interrupt Handlers */
201/* Add here the Interrupt Handlers for the used peripherals. */
202/* For the available peripheral interrupt handler names, */
203/* please refer to the startup file (startup_stm32g0xx.s). */
204/******************************************************************************/
205
206/**
207 * @brief This function handles DMA1 channel 1 interrupt.
208 */
209#ifdef USE_RAM_FUNC
210__RAM_FUNC void DMA1_Channel1_IRQHandler(void)
211#else
212void DMA1_Channel1_IRQHandler(void)
213#endif
214{
215 /* USER CODE BEGIN DMA1_Channel1_IRQn 0 */
216 //R2_GPIO_Port->BSRR = R2_Pin;
217
218 static uint32_t current_integral = 0;
219
220 rawMOSFETsVoltageDrop = ADC_values[MOSFETS_VDROP_CHANNEL];
221
222 MOSFETS_Management();
223
224 rawContactVoltageDropPlus = ADC_values[I_PLUS_CHANNEL]; // Charge current sensor raw value
225 rawContactVoltageDropMinus = ADC_values[I_MINUS_CHANNEL]; // Discharge current sensor raw value
226
227 DMA1->IFCR |= DMA_FLAG_TC1;
228
229 uint32_t current_adc_value = (rawContactVoltageDropPlus >= rawContactVoltageDropMinus)? rawContactVoltageDropPlus: rawContactVoltageDropMinus;
230
231 if (current_adc_value >= sys_data.s.copper_v_drop_adc_limit)
232 {
233 current_integral += current_adc_value;
234 if (current_integral > maxIntegral) // maxIntegral updated in main loop
235 {
236 InrushCurrentManagement();
237 current_integral = 0;
238 }
239 }
240 else current_integral = 0;
241
242 //R2_GPIO_Port->BRR = R2_Pin;
243
244 return;
245 /* USER CODE END DMA1_Channel1_IRQn 0 */
246 HAL_DMA_IRQHandler(&hdma_adc1);
247 /* USER CODE BEGIN DMA1_Channel1_IRQn 1 */
248 /* USER CODE END DMA1_Channel1_IRQn 1 */
249}
250
251/**
252 * @brief This function handles ADC1, COMP1 and COMP2 interrupts (COMP interrupts through EXTI lines 17 and 18).
253 */
254#ifdef USE_RAM_FUNC
255__RAM_FUNC void ADC1_COMP_IRQHandler(void)
256#else
257void ADC1_COMP_IRQHandler(void)
258#endif
259{
260 /* USER CODE BEGIN ADC1_COMP_IRQn 0 */
261 //LED_STATE_GPIO_Port->BSRR = LED_STATE_Pin;
262 // DO NOT BREAKPOINT THIS !!! MOSFETs DIES!!!
263
264 // We open both MOSFETs arrays no matter which way overcurrent happened
265 OpenBothMOSFETSVeryFast();
266 HAL_NVIC_DisableIRQ(ADC1_COMP_IRQn);
267 //__HAL_ADC_CLEAR_FLAG(&hadc1, (ADC_FLAG_AWD3 | ADC_FLAG_AWD2));
268 MOSFETS_Management = &DoNothing;
269 if (overload_shutdown_is_active == 0)
270 {
271 overcurrent_shutdown_is_active = 1;
272 overcurrent_shutdown_time = uwTick;
273 sys_data.s.overcurrent_error_cnt++;
274 statDataChanged = 1;
275 }
276 //LED_STATE_GPIO_Port->BRR = LED_STATE_Pin;
277 return;
278 /* USER CODE END ADC1_COMP_IRQn 0 */
279 HAL_ADC_IRQHandler(&hadc1);
280 /* USER CODE BEGIN ADC1_COMP_IRQn 1 */
281 /* USER CODE END ADC1_COMP_IRQn 1 */
282}
283
284/**
285 * @brief This function handles TIM6, DAC1 and LPTIM1 interrupts (LPTIM1 interrupt through EXTI line 29).
286 */
287void TIM6_DAC_LPTIM1_IRQHandler(void)
288{
289 /* USER CODE BEGIN TIM6_DAC_LPTIM1_IRQn 0 */
290
291 HAL_TIM_IRQHandler(&htim6);
292 // We dont't use DAC ISR
293 return;
294
295 /* USER CODE END TIM6_DAC_LPTIM1_IRQn 0 */
296 HAL_TIM_IRQHandler(&htim6);
297 HAL_DAC_IRQHandler(&hdac1);
298 /* USER CODE BEGIN TIM6_DAC_LPTIM1_IRQn 1 */
299
300 /* USER CODE END TIM6_DAC_LPTIM1_IRQn 1 */
301}
302
303/**
304 * @brief This function handles TIM7 and LPTIM2 interrupts (LPTIM2 interrupt through EXTI line 30).
305 */
306void TIM7_LPTIM2_IRQHandler(void)
307{
308 /* USER CODE BEGIN TIM7_LPTIM2_IRQn 0 */
309
310 /* USER CODE END TIM7_LPTIM2_IRQn 0 */
311 HAL_TIM_IRQHandler(&htim7);
312 /* USER CODE BEGIN TIM7_LPTIM2_IRQn 1 */
313
314 /* USER CODE END TIM7_LPTIM2_IRQn 1 */
315}
316
317/**
318 * @brief This function handles TIM14 global interrupt.
319 */
320void TIM14_IRQHandler(void)
321{
322 /* USER CODE BEGIN TIM14_IRQn 0 */
323
324 /* USER CODE END TIM14_IRQn 0 */
325 HAL_TIM_IRQHandler(&htim14);
326 /* USER CODE BEGIN TIM14_IRQn 1 */
327
328 /* USER CODE END TIM14_IRQn 1 */
329}
330
331/**
332 * @brief This function handles TIM15 global interrupt.
333 */
334void TIM15_IRQHandler(void)
335{
336 /* USER CODE BEGIN TIM15_IRQn 0 */
337
338 /* USER CODE END TIM15_IRQn 0 */
339 HAL_TIM_IRQHandler(&htim15);
340 /* USER CODE BEGIN TIM15_IRQn 1 */
341
342 /* USER CODE END TIM15_IRQn 1 */
343}
344
345/**
346 * @brief This function handles TIM16 global interrupt.
347 */
348void TIM16_IRQHandler(void)
349{
350 /* USER CODE BEGIN TIM16_IRQn 0 */
351
352 /* USER CODE END TIM16_IRQn 0 */
353 HAL_TIM_IRQHandler(&htim16);
354 /* USER CODE BEGIN TIM16_IRQn 1 */
355
356 /* USER CODE END TIM16_IRQn 1 */
357}
358
359/**
360 * @brief This function handles TIM17 global interrupt.
361 */
362void TIM17_IRQHandler(void)
363{
364 /* USER CODE BEGIN TIM17_IRQn 0 */
365
366 /* USER CODE END TIM17_IRQn 0 */
367 HAL_TIM_IRQHandler(&htim17);
368 /* USER CODE BEGIN TIM17_IRQn 1 */
369
370 /* USER CODE END TIM17_IRQn 1 */
371}
372
373/**
374 * @brief This function handles USART1 global interrupt / USART1 wake-up interrupt through EXTI line 25.
375 */
376#ifdef USE_RAM_FUNC
377__RAM_FUNC void USART1_IRQHandler(void)
378#else
379void USART1_IRQHandler(void)
380#endif
381{
382 /* USER CODE BEGIN USART1_IRQn 0 */
383 //LED_ERROR_GPIO_Port->BSRR = LED_ERROR_Pin;
384
385 MODBUS_UART_IRQHandler(&huart1);
386
387 //LED_ERROR_GPIO_Port->BRR = LED_ERROR_Pin;
388 return;
389
390 /* USER CODE END USART1_IRQn 0 */
391 HAL_UART_IRQHandler(&huart1);
392 /* USER CODE BEGIN USART1_IRQn 1 */
393
394 /* USER CODE END USART1_IRQn 1 */
395}
396
397/* USER CODE BEGIN 1 */
398
399void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef * htim)
400{
401 // We don't have periodic timer interrupts, so we can disable any timer, which generated interrupt
402 HAL_TIM_Base_Stop_IT(htim);
403
404 if (htim->Instance == TIM6)
405 { // This timer is used to start MOSFETs ON mode only after 250ms delay after turn on of VBOOST voltage
406 sys_data.s.user_button_mode = SWITCH_ON;
407
408#ifdef INVERTER_CAP_PRECHARGE
409 SetReturnFunction(&ADC_Close_Both_MOSFETs);
410 MOSFETS_Management = &PreChargeStage;
411#else
412 MOSFETS_Management = &ADC_Close_Both_MOSFETs;
413#endif
414 //MOSFETS_Management = &ADC_Close_Both_MOSFETs;
415 sys_data.s.relay_status = RELAY_IS_CLOSED;
416 ExternalGreenLED_Management = &TurnExternalGreenLEDOn;
417 }
418 else if (htim->Instance == VBOOST_ON_TIMER.Instance)
419 { // This timer is used to start MOSFETs regulation only after 250ms delay after VBOOST voltage activation in AUTO mode
420 sys_data.s.user_button_mode = SWITCH_AUTO;
421 AUTO_Mode(0, 1); // Reinitializing AUTO mode
422 }
423 else if (htim->Instance == VBOOST_OFF_TIMER.Instance)
424 {
425 // This timer is used to turn off VBOOST voltage after 100ms after entering into OFF mode
426 //HAL_GPIO_WritePin(DISABLE_VBOOST_GPIO_Port, DISABLE_VBOOST_Pin, VBOOST_DISABLE);
427 }
428 else if (htim->Instance == TIM15)
429 {
430#ifdef DISABLE_SHORTCUT_DETECTION_DURING_SWITCH_OFF
431 EnableShortCutDetection();
432 HAL_TIM_Base_Stop_IT(htim);
433#endif
434 }
435 else if (htim->Instance == TIM16)
436 {
437#ifdef DISABLE_SHORTCUT_DETECTION_DURING_SWITCH_OFF
438 DisableShortCutDetection();
439#endif
440 // This timer is used to turn off switch after 60sec after entering into ON mode
441 sys_data.s.user_button_mode = SWITCH_OFF;
442 MOSFETS_Management = &ADC_Open_Both_MOSFETs;
443 sys_data.s.relay_status = RELAY_IS_OPENED;
444 ExternalGreenLED_Management = &TurnExternalGreenLEDOff;
445
446 // After 100ms we turn off VBOOST voltage for power saving
447 HAL_TIM_Base_Stop_IT(&VBOOST_OFF_TIMER);
448 __HAL_TIM_CLEAR_FLAG(&VBOOST_OFF_TIMER, TIM_FLAG_UPDATE);
449 __HAL_TIM_SET_COUNTER(&VBOOST_OFF_TIMER, 0);
450 HAL_TIM_Base_Start_IT(&VBOOST_OFF_TIMER);
451 }
452 else if (htim->Instance == TIM17)
453 {
454 // This timer is used to stop parsing Modbus commands for 10sec after illegal command is recieved
455 command_parser_is_enabled = 1;
456 }
457}
458
459//------------------------------------------------------------------------------
460
461/* USER CODE END 1 */
Note: See TracBrowser for help on using the repository browser.