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

Last change on this file since 8 was 7, checked in by f.jahn, 2 years ago

12V variant is working good.

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