source: trunk/firmware/CubeMX/Inc/main.h@ 3

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

Precharge pulses are made as short as possible.

File size: 14.8 KB
Line 
1/* USER CODE BEGIN Header */
2/**
3 ******************************************************************************
4 * @file : main.h
5 * @brief : Header for main.c file.
6 * This file contains the common defines of the application.
7 ******************************************************************************
8 * @attention
9 *
10 * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
11 * All rights reserved.</center></h2>
12 *
13 * This software component is licensed by ST under BSD 3-Clause license,
14 * the "License"; You may not use this file except in compliance with the
15 * License. You may obtain a copy of the License at:
16 * opensource.org/licenses/BSD-3-Clause
17 *
18 ******************************************************************************
19 */
20/* USER CODE END Header */
21
22/* Define to prevent recursive inclusion -------------------------------------*/
23#ifndef __MAIN_H
24#define __MAIN_H
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30/* Includes ------------------------------------------------------------------*/
31#include "stm32g0xx_hal.h"
32
33/* Private includes ----------------------------------------------------------*/
34/* USER CODE BEGIN Includes */
35
36/* USER CODE END Includes */
37
38/* Exported types ------------------------------------------------------------*/
39/* USER CODE BEGIN ET */
40
41typedef enum SWITCH_MODE {SWITCH_OFF = 0, SWITCH_ON, SWITCH_AUTO} switch_mode_t;
42typedef enum RELAY_STATE {RELAY_IS_OPENED = 0, RELAY_IS_CLOSED, ONLY_BA_OPENED, ONLY_AB_OPENED} relay_state_t;
43
44/* USER CODE END ET */
45
46/* Exported constants --------------------------------------------------------*/
47/* USER CODE BEGIN EC */
48
49/* USER CODE END EC */
50
51/* Exported macro ------------------------------------------------------------*/
52/* USER CODE BEGIN EM */
53
54/* USER CODE END EM */
55
56/* Exported functions prototypes ---------------------------------------------*/
57void Error_Handler(void);
58
59/* USER CODE BEGIN EFP */
60
61//#define DISABLE_SHORTCUT_DETECTION_DURING_SWITCH_OFF
62
63#ifdef DISABLE_SHORTCUT_DETECTION_DURING_SWITCH_OFF
64void DisableShortCutDetection(void);
65void EnableShortCutDetection(void);
66#endif
67
68/* USER CODE END EFP */
69
70/* Private defines -----------------------------------------------------------*/
71#define DISABLE_VBOOST_Pin GPIO_PIN_13
72#define DISABLE_VBOOST_GPIO_Port GPIOC
73#define TP2_Pin GPIO_PIN_14
74#define TP2_GPIO_Port GPIOC
75#define TP4_Pin GPIO_PIN_15
76#define TP4_GPIO_Port GPIOC
77#define SENSE_TEMP_Pin GPIO_PIN_0
78#define SENSE_TEMP_GPIO_Port GPIOA
79#define U_BAT_SENSE_Pin GPIO_PIN_1
80#define U_BAT_SENSE_GPIO_Port GPIOA
81#define I_PLUS_SENSE_Pin GPIO_PIN_2
82#define I_PLUS_SENSE_GPIO_Port GPIOA
83#define U_SW_SENSE_Pin GPIO_PIN_3
84#define U_SW_SENSE_GPIO_Port GPIOA
85#define DAC_A_Pin GPIO_PIN_4
86#define DAC_A_GPIO_Port GPIOA
87#define DAC_B_Pin GPIO_PIN_5
88#define DAC_B_GPIO_Port GPIOA
89#define I_MINUS_SENSE_Pin GPIO_PIN_6
90#define I_MINUS_SENSE_GPIO_Port GPIOA
91#define AB_SENSE_1_Pin GPIO_PIN_7
92#define AB_SENSE_1_GPIO_Port GPIOA
93#define LED_FUNCTION_Pin GPIO_PIN_0
94#define LED_FUNCTION_GPIO_Port GPIOB
95#define LED_ERROR_Pin GPIO_PIN_1
96#define LED_ERROR_GPIO_Port GPIOB
97#define LED_STATE_Pin GPIO_PIN_2
98#define LED_STATE_GPIO_Port GPIOB
99#define DIP2_Pin GPIO_PIN_10
100#define DIP2_GPIO_Port GPIOB
101#define DIP3_Pin GPIO_PIN_11
102#define DIP3_GPIO_Port GPIOB
103#define DIP4_Pin GPIO_PIN_12
104#define DIP4_GPIO_Port GPIOB
105#define DIP5_Pin GPIO_PIN_13
106#define DIP5_GPIO_Port GPIOB
107#define DIP6_Pin GPIO_PIN_14
108#define DIP6_GPIO_Port GPIOB
109#define DIP7_Pin GPIO_PIN_15
110#define DIP7_GPIO_Port GPIOB
111#define OUT_CS_Pin GPIO_PIN_8
112#define OUT_CS_GPIO_Port GPIOA
113#define RS485_D_Pin GPIO_PIN_9
114#define RS485_D_GPIO_Port GPIOA
115#define OVP_IN_Pin GPIO_PIN_6
116#define OVP_IN_GPIO_Port GPIOC
117#define LVP_IN_Pin GPIO_PIN_7
118#define LVP_IN_GPIO_Port GPIOC
119#define RS485_R_Pin GPIO_PIN_10
120#define RS485_R_GPIO_Port GPIOA
121#define R2_Pin GPIO_PIN_11
122#define R2_GPIO_Port GPIOA
123#define RS485_DE_Pin GPIO_PIN_12
124#define RS485_DE_GPIO_Port GPIOA
125#define BTN1_Pin GPIO_PIN_15
126#define BTN1_GPIO_Port GPIOA
127#define OUT_CTRL_Pin GPIO_PIN_0
128#define OUT_CTRL_GPIO_Port GPIOD
129#define SW_OFF_Pin GPIO_PIN_2
130#define SW_OFF_GPIO_Port GPIOD
131#define SW_ON_Pin GPIO_PIN_3
132#define SW_ON_GPIO_Port GPIOD
133#define LED_SW_ERROR_Pin GPIO_PIN_3
134#define LED_SW_ERROR_GPIO_Port GPIOB
135#define LED_SW_STATE_Pin GPIO_PIN_4
136#define LED_SW_STATE_GPIO_Port GPIOB
137#define DIP0_Pin GPIO_PIN_8
138#define DIP0_GPIO_Port GPIOB
139#define DIP1_Pin GPIO_PIN_9
140#define DIP1_GPIO_Port GPIOB
141/* USER CODE BEGIN Private defines */
142
143#define INVERTER_CAP_PRECHARGE
144
145#ifdef DEBUG
146 #define DEVICE_TYPE_ID 201
147#else
148 #ifdef VARIANT_24V
149 #define DEVICE_TYPE_ID 202
150 #else
151 #define DEVICE_TYPE_ID 201
152 #endif
153#endif
154
155#define FW_VERSION_MAJOR 1
156#define FW_VERSION_MINOR 0
157#define FW_VERSION_REVISION 0
158#ifdef VARIANT_24V
159 #define DEVICE_SN 0xEC565524 // ECSGSS24 = ECS Green Smart Switch, 24V version (Must be substituted with real serial number)
160#else
161 #define DEVICE_SN 0xEC565512 // ECSGSS12 = ECS Green Smart Switch, 12V version (Must be substituted with real serial number)
162#endif
163
164#define MB_BAUDRATE_DEFAULT 19200 // bit pro sekunde
165#define MB_PARITY_MODE_DEFAULT 'e' // EVEN - GERADE
166#define MB_STOPBITS_DEFAULT 1
167#define MB_SLAVE_ADRESS_DEFAULT 1
168
169#define ADC_CHANNELS 6
170#define TEMP_CHANNEL 0
171#define U_BAT_CHANNEL 1
172#define I_PLUS_CHANNEL 2
173#define U_SW_CHANNEL 3
174#define MOSFETS_VDROP_CHANNEL 4
175#define I_MINUS_CHANNEL 5
176
177#define ADC_RESOLUTION 12 // 12 bits
178#define ADC_MAX_VALUE 0xFFF // 4095
179#define ADC_VREF 3000 // mV
180
181//#define INA240_MAX_DIFF_2_MEASURE 75 // mV
182
183#ifdef VARIANT_24V
184 #define CONTROL_CURRENT_A 380 // [A] long-term current limit of the device.
185 #define SHORTCUT_CURRENT_mV 800 // [mV] Some big value to pass test sequence
186 #define INRUSH_CURRENT_mV 1100 // [mV] Some big value to pass test sequence
187 #define COPPER_V_DROP_AT_CONTROL_CURRENT_mV 56 // [mV] - must be measured on the board
188#else
189 #define CONTROL_CURRENT_A 500 // [A] long-term current limit of the device.
190 #define SHORTCUT_CURRENT_mV 1500 // [mV] Some big value to pass test sequence
191 #define INRUSH_CURRENT_mV 616 // [mV] Some big value to pass test sequence
192 #define COPPER_V_DROP_AT_CONTROL_CURRENT_mV 150 // [mV] - must be measured on the board
193#endif
194//#define COPPER_V_DROP_AT_INRUSH_CURRENT ((INRUSH_CURRENT_A * COPPER_V_DROP_AT_CONTROL_CURRENT_A)/CONTROL_CURRENT_A) // ~391mV
195//#define COPPER_V_DROP_AT_SHORTCUT_CURRENT ((SHORTCUT_CURRENT_A * COPPER_V_DROP_AT_CONTROL_CURRENT_A)/CONTROL_CURRENT_A) // ~1176mV
196//#if COPPER_V_DROP_AT_INRUSH_CURRENT > 3000 || COPPER_V_DROP_AT_SHORTCUT_CURRENT > 3000 || COPPER_V_DROP_AT_CONTROL_CURRENT_A > 3000
197//#error Voltage drop cannot be greater than 1.5V. See INA281B2 datasheet.
198//#endif
199#define ADC_VALUE_AT_INRUSH_CURRENT ((ADC_MAX_VALUE * INRUSH_CURRENT_mV)/ADC_VREF)
200//#define ADC_VALUE_DELTA_AT_SHORTCUT_CURRENT ((ADC_MAX_VALUE * COPPER_V_DROP_AT_SHORTCUT_CURRENT)/ADC_VREF) // 1605
201#define ADC_VALUE_AT_CONTROL_CURRENT ((ADC_MAX_VALUE * COPPER_V_DROP_AT_CONTROL_CURRENT_mV)/ADC_VREF) // 140
202#define CURRENT_INTEGRAL_FREQ 320000U // (7.5cycles + 12.5cycles)*1channels + (3.5cycles + 12.5cycles)*5channels = 20 + 80 = 100cycles @ 32MHz = 320 kHz
203#ifdef VARIANT_24V
204 #define CURRENT_INTEGRATION_PERIOD 575 // µs
205#else
206 #define CURRENT_INTEGRATION_PERIOD 1700 // µs
207#endif
208#if CURRENT_INTEGRATION_PERIOD < 31
209#error CURRENT_INTEGRATION_PERIOD cannot be less then 31 µs!
210#endif
211#define CURRENT_INTEGRATION_STEPS ((CURRENT_INTEGRAL_FREQ * CURRENT_INTEGRATION_PERIOD) / (1000*1000)) // 640
212//#define COPPER_TEMP_COEFFICIENT 0.00393f
213
214#define DIP_IS_OFF GPIO_PIN_SET
215#define DIP_IS_ON GPIO_PIN_RESET
216
217#define OVP_PRESENT GPIO_PIN_SET
218#define OVP_NOT_PRESENT GPIO_PIN_RESET
219
220#define LVP_PRESENT GPIO_PIN_SET
221#define LVP_NOT_PRESENT GPIO_PIN_RESET
222
223#define OUT_CTRL_ACTIVATE GPIO_PIN_SET
224#define OUT_CTRL_DEACTIVATE GPIO_PIN_RESET
225
226#ifdef VARIANT_24V
227 #define R19 30000 // Ohm. See schematic page 6. Revision 62
228 #define R20 30000 // Ohm. See schematic page 6. Revision 62
229 #define R27 30000 // Ohm. See schematic page 6. Revision 62
230 #define R23 30000 // Ohm. See schematic page 6. Revision 62
231 #define R24 30000 // Ohm. See schematic page 6. Revision 62
232 #define R25 30000 // Ohm. See schematic page 6. Revision 62
233 #define R26 10000 // Ohm. See schematic page 6. Revision 62
234 #define R33 10000 // Ohm. See schematic page 6. Revision 62
235 #define R35 1000 // Ohm. See schematic page 6. Revision 62
236 #define R37 100000 // Ohm. See schematic page 6. Revision 62
237 #define R43 100000 // Ohm. See schematic page 6. Revision 62
238 #define R42 1000 // Ohm. See schematic page 6. Revision 62
239 #if (R23 + R24 + R25 + R33 != R43) || R43 != R37 || R33 != R26 || (R19 + R20 + R27 + R26 != R43) || R35 != R42
240 #error Registers values are not correct!
241 #endif
242 #define MAX_POSSIBLE_DIFF_TO_MEASURE ((ADC_VREF * R35 /* ((R23 + R24 + R25) + R33)*/)/(2 /* R43 */* R33)) // 150mV
243#else
244 #define R31 10000 // Ohm. See schematic page 6. Revision 62
245 #define R37 10000 // Ohm. See schematic page 6. Revision 62
246 #define R23 47000 // Ohm. See schematic page 6. Revision 62
247 #define R25 47000 // Ohm. See schematic page 6. Revision 62
248 #define R22 10000 // Ohm. See schematic page 6. Revision 62
249 #define R24 10000 // Ohm. See schematic page 6. Revision 62
250 #define R33 470000 // Ohm. See schematic page 6. Revision 62
251 #define R38 470000 // Ohm. See schematic page 6. Revision 62
252 #if R31 != R37 || R23 != R25 || R22 != R24 || R33 != R38 || R31 != R22
253 #error Values of these two registers must be the same!
254 #endif
255 #define MAX_POSSIBLE_DIFF_TO_MEASURE ((ADC_VREF * /*R31*/ (R23 + R22))/(2 * R38 /*R22*/)) // 181mV
256#endif
257#define MAX_ALLOWED_MOSFETS_V_DROP 30 // mV
258#define MAX_ALLOWED_MOSFETS_V_DROP_DELTA 30 // mV
259#define MAX_ALLOWED_MOSFETS_V_DROP_ADC ((MAX_ALLOWED_MOSFETS_V_DROP * (ADC_MAX_VALUE/2)) / MAX_POSSIBLE_DIFF_TO_MEASURE) // 409
260#define POS_ALLOWED_MOSFETS_V_DROP_ADC (MAX_ALLOWED_MOSFETS_V_DROP_ADC + (ADC_MAX_VALUE/2)) // 2456
261#define NEG_ALLOWED_MOSFETS_V_DROP_ADC ((ADC_MAX_VALUE/2) - MAX_ALLOWED_MOSFETS_V_DROP_ADC) // 1638
262
263#define MOSFET_CHANNEL_A DAC_CHANNEL_1 //TIM_CHANNEL_1
264#define MOSFET_CHANNEL_B DAC_CHANNEL_2 //TIM_CHANNEL_2
265#define DAC_3V 0xFFF
266#define DAC_0V 0x00
267#define DAC_STEP 15
268#if DAC_3V % DAC_STEP
269 #error DAC_STEP cannot be that value!
270#endif
271
272// MODBUS commands
273#define COMMAND_SAVE_CONFIG 1
274#define COMMAND_RESTART_MODBUS 2
275#define COMMAND_RESTORE_DEFAULTS 3
276#define COMMAND_SAVE_LOCK_KEY 5
277#define COMMAND_ON_SWITCH 10
278#define COMMAND_OFF_SWITCH 20
279#define COMMAND_AUTO_SWITCH 30
280#define COMMAND_RESTART 500
281#define COMMAND_SAVE_CONFIG_WITH_SN 50403
282#define COMMAND_OFFSET_CALIBRATION 40302
283#define COMMAND_CURRENT_CALIBRATION 30201
284#define COMMAND_CURRENT_OFFSET_CALIBRATION 60504
285#define COMMAND_TURN_OVERLOAD_DETECTION_OFF 20304
286#define COMMAND_TURN_OVERLOAD_DETECTION_ON 30405
287
288#define U_BAT_CALC_PERIOD 5 // ms
289#ifdef VARIANT_24V
290#define U_BAT_CRITICAL_VOLTAGE 20000 // mV (If voltage on battery is lower than 10V, then there is a probability that MOSFETs will not open/close properly)
291#define U_BAT_RECOVERY_VOLTAGE 24000 // mV (If voltage on battery is higher than 12V, then it is fully recovered from possible previous discharge)
292#else
293#define U_BAT_CRITICAL_VOLTAGE 10000 // mV (If voltage on battery is lower than 10V, then there is a probability that MOSFETs will not open/close properly)
294#define U_BAT_RECOVERY_VOLTAGE 12000 // mV (If voltage on battery is higher than 12V, then it is fully recovered from possible previous discharge)
295#endif
296
297#define BAT_CHARGE_SIGN_V 2 // mV If voltage on side A greater than voltage on side B by this value, then charging is going on
298
299//#define VOLTAGE_RATIO 20 // When we have ±20mV on INA240A1, the input voltage is ±1mV
300
301#define TEMP_SENSOR_mV_AT_MINUS30 300 // in mV (See LM61 datasheet)
302#define TEMP_SENSOR_mV_AT_PLUS100 1600 // in mV (See LM61 datasheet)
303#define TEMP_SENSOR_ADC_AT_MINUS30 ((TEMP_SENSOR_mV_AT_MINUS30 * ADC_MAX_VALUE) / ADC_VREF)
304#define TEMP_SENSOR_ADC_AT_PLUS100 ((TEMP_SENSOR_mV_AT_PLUS100 * ADC_MAX_VALUE) / ADC_VREF)
305#define TEMP_SENSOR_0_OFFSET 600 // in mV
306#define MIN_TEMP (-300) // Celsium degrees (See LM61 datasheet)
307#define MAX_TEMP (1000) // Celsium degrees (See LM61 datasheet)
308#define TEMP_RECOVER_PERCENT 25 // When temperature drops to (critical - 25%) then we remove overtemperature flag
309
310#define BTN_IS_PRESSED GPIO_PIN_RESET
311
312#define LVP_OVP_EVENT_NUM 4
313
314#define SWCLK_Port GPIOA
315#define SWCLK_Pin GPIO_PIN_14
316#define SWDIO_Port GPIOA
317#define SWDIO_Pin GPIO_PIN_13
318
319#define OVERTEMP_ERROR 0
320#define OVERCURRENT_ERROR 1
321#define OVERLOAD_ERROR 2
322#define LOWBAT_ERROR 3
323#define HEATER_IS_ON 4
324#define ABBA_VOLTAGE_ERROR 5
325//#define OFFSET_CALLIB_ERROR 4
326//#define OFFSET_CALLIB_IS_ON 5
327//#define OFFSET_CALLIB_SEQ_ERROR 6
328//#define EEPROM_SAVE_ERROR 7
329//#define CURRENT_SENSOR_CALLIB_IS_ON 8
330//#define CURRENT_SENSOR_CALLIB_TEMP_ERROR 9
331//#define CURRENT_SENSOR_CALLIB_ERROR 10
332//#define CURRENT_SENSOR_CALLIB_SEQ_ERROR 11
333//#define CURRENT_SENSOR_OFFSET_CALLIB_IS_ON 14
334//#define CURRENT_SENSOR_OFFSET_CALLIB_ERROR 15
335
336#define VBOOST_OFF_TIMER htim14
337#define VBOOST_ON_TIMER htim7
338#define VBOOST_ENABLE GPIO_PIN_SET
339#define VBOOST_DISABLE GPIO_PIN_RESET
340
341#define ON_MODE_AUTO_OFF_TIMER htim16
342
343#define I_RMS_SAMPLES_COUNT 4096
344#define I_RMS_SAMPLES_SUM_COUNT 32
345
346#define FC_WRITE_SINGLE_REGISTER 0x06
347#define FC_WRITE_MULTIPLE_REGISTER 0x10
348
349#define HOUR_TIME_INTERVALL (2UL * 60000UL) // 2 min for DEBUG
350
351#define DAC_HANDLE hdac1
352#define DAC_CH_A DOR1
353#define DAC_CH_B DOR2
354#define DAC_CH_A_ALIGNMENT DAC_DHR12R1_ALIGNMENT
355#define DAC_CH_B_ALIGNMENT DAC_DHR12R2_ALIGNMENT
356
357/* USER CODE END Private defines */
358
359#ifdef __cplusplus
360}
361#endif
362
363#endif /* __MAIN_H */
Note: See TracBrowser for help on using the repository browser.