Changeset 7 for trunk/firmware/CubeMX
- Timestamp:
- May 24, 2023, 1:47:09 PM (2 years ago)
- Location:
- trunk/firmware/CubeMX
- Files:
-
- 4 edited
-
Inc/main.h (modified) (6 diffs)
-
Src/gpio.c (modified) (1 diff)
-
Src/main.c (modified) (10 diffs)
-
Src/stm32g0xx_it.c (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/firmware/CubeMX/Inc/main.h
r4 r7 59 59 /* USER CODE BEGIN EFP */ 60 60 61 #ifdef VARIANT_24V 61 62 #define DISABLE_SHORTCUT_DETECTION_DURING_SWITCH_OFF 63 #endif 62 64 63 65 #ifdef DISABLE_SHORTCUT_DETECTION_DURING_SWITCH_OFF … … 141 143 /* USER CODE BEGIN Private defines */ 142 144 143 #define INVERTER_CAP_PRECHARGE144 145 145 #ifdef DEBUG 146 146 #define DEVICE_TYPE_ID 201 … … 148 148 #ifdef VARIANT_24V 149 149 #define DEVICE_TYPE_ID 202 150 #define INVERTER_CAP_PRECHARGE 150 151 #else 151 152 #define DEVICE_TYPE_ID 201 … … 183 184 #ifdef VARIANT_24V 184 185 #define CONTROL_CURRENT_A 380 // [A] long-term current limit of the device. 185 #define SHORTCUT_CURRENT_mV 2000 // [mV] Some big value to pass test sequence186 #define INRUSH_CURRENT_mV 1100// [mV] Some big value to pass test sequence187 #define COPPER_V_DROP_AT_CONTROL_CURRENT_mV 100 // [mV] - must be measured on the board186 #define SHORTCUT_CURRENT_mV 1100 // [mV] Some big value to pass test sequence 187 #define INRUSH_CURRENT_mV 550 // [mV] Some big value to pass test sequence 188 #define COPPER_V_DROP_AT_CONTROL_CURRENT_mV 200 // [mV] - must be measured on the board 188 189 #else 189 190 #define CONTROL_CURRENT_A 500 // [A] long-term current limit of the device. … … 202 203 #define CURRENT_INTEGRAL_FREQ 320000U // (7.5cycles + 12.5cycles)*1channels + (3.5cycles + 12.5cycles)*5channels = 20 + 80 = 100cycles @ 32MHz = 320 kHz 203 204 #ifdef VARIANT_24V 204 #define CURRENT_INTEGRATION_PERIOD 575// µs205 #define CURRENT_INTEGRATION_PERIOD 1500 // µs 205 206 #else 206 207 #define CURRENT_INTEGRATION_PERIOD 1700 // µs … … 286 287 #define COMMAND_TURN_OVERLOAD_DETECTION_ON 30405 287 288 288 #define U_BAT_CALC_PERIOD 5 // ms289 //#define U_BAT_CALC_PERIOD 5 // ms 289 290 #ifdef VARIANT_24V 290 291 #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 292 #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 293 #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) 294 #define U_BAT_CRITICAL_VOLTAGE_mV 10000 // mV (If voltage on battery is lower than 10V, then there is a probability that MOSFETs will not open/close properly) 295 #define U_BAT_RECOVERY_VOLTAGE_mV 12000 // mV (If voltage on battery is higher than 12V, then it is fully recovered from possible previous discharge) 296 #define ADC_BAT_CRITICAL_VOLTAGE ((((U_BAT_CRITICAL_VOLTAGE_mV / 1000) * R22) * (ADC_MAX_VALUE + 1)) / ((R23 + R22) * (ADC_VREF / 1000))) 295 297 #endif 296 298 -
trunk/firmware/CubeMX/Src/gpio.c
r1 r7 74 74 /*Configure GPIO pin : PtPin */ 75 75 GPIO_InitStruct.Pin = TP4_Pin; 76 GPIO_InitStruct.Mode = GPIO_MODE_ INPUT;76 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 77 77 GPIO_InitStruct.Pull = GPIO_NOPULL; 78 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; 78 79 HAL_GPIO_Init(TP4_GPIO_Port, &GPIO_InitStruct); 79 80 -
trunk/firmware/CubeMX/Src/main.c
r4 r7 193 193 void ExternalRedLED2ShortOnThen2LongOnThenLongPauseBlinking(void); 194 194 void RS485DisableButtonManagement(uint32_t new_time); 195 static void BatteryLowVoltageProtection(/*uint32_t current_time*/); 195 196 196 197 /* USER CODE END PFP */ … … 278 279 SEGGER_RTT_printf(0, "Free space for statistics in fake EEPROM: %u bytes\n", FEEPROM_StatFreeBytes()); 279 280 SEGGER_RTT_printf(0, "MAX_POSSIBLE_DIFF_TO_MEASURE: %u\n", MAX_POSSIBLE_DIFF_TO_MEASURE); 281 SEGGER_RTT_printf(0, "ADC_BAT_CRITICAL_VOLTAGE: %u\n", ADC_BAT_CRITICAL_VOLTAGE); 280 282 SEGGER_RTT_printf(0, "CPU Freq: %u Hz\n", HAL_RCC_GetSysClockFreq()); 281 283 … … 374 376 while (1) // Main loop 375 377 { 378 TP4_GPIO_Port->BSRR = TP4_Pin; 379 376 380 ABVoltageDropCalculation(); 381 382 BatteryLowVoltageProtection(); 383 384 TP4_GPIO_Port->BRR = TP4_Pin; 377 385 378 386 //TIM2->CNT = 0; … … 448 456 case SWITCH_AUTO: 449 457 // Checking whether temperature, current and battery voltage are within limits 450 if ((temperature_shutdown_is_active == 1) || (low_bat_shutdown_is_active == 1)) 458 if (temperature_shutdown_is_active == 1) 459 { 460 if (!restartAutoMode) 461 { 462 // If so, opening both MOSFETs (i.e. disconnecting load/charger from battery) 463 /* 464 #ifdef DISABLE_SHORTCUT_DETECTION_DURING_SWITCH_OFF 465 DisableShortCutDetection(); 466 #endif 467 HAL_NVIC_DisableIRQ(ADC_DMA_IRQ); 468 MOSFETS_Management = &ADC_Open_Both_MOSFETs; 469 sys_data.s.relay_status = RELAY_IS_OPENED; 470 HAL_NVIC_EnableIRQ(ADC_DMA_IRQ); 471 ExternalRedLED_Management = &ExternalRedLED1ShortOnThenLongPauseBlinking; 472 */ 473 restartAutoMode = 1; 474 } 475 } 476 else if (low_bat_shutdown_is_active == 1) 451 477 { 452 478 if (!restartAutoMode) … … 457 483 #endif 458 484 HAL_NVIC_DisableIRQ(ADC_DMA_IRQ); 459 MOSFETS_Management = &ADC_Open_Both_MOSFETs; 485 MOSFETS_Management = &DoNothing; 486 OpenBothMOSFETSVeryFast(); 460 487 sys_data.s.relay_status = RELAY_IS_OPENED; 461 488 HAL_NVIC_EnableIRQ(ADC_DMA_IRQ); 462 if (temperature_shutdown_is_active == 1) ExternalRedLED_Management = &ExternalRedLED1ShortOnThenLongPauseBlinking; 463 if (low_bat_shutdown_is_active == 1) ExternalRedLED_Management = &ExternalRedLED5ShortOnThenLongPauseBlinking; 489 ExternalRedLED_Management = &ExternalRedLED5ShortOnThenLongPauseBlinking; 464 490 restartAutoMode = 1; 465 491 } … … 722 748 sys_data.s.command = 0; 723 749 sys_data.s.device_status = 0; 750 sys_data.s.ubsenseb_voltage = 1; 724 751 } 725 752 … … 2180 2207 static inline __attribute__((always_inline)) void CalculatingAndAveragingVoltageOnContactB(void) 2181 2208 { 2182 static int32_t ubsenseb_voltage_accum = U_BAT_RECOVERY_VOLTAGE * 32; // << ubsenseb_voltage_accum_avg; 2209 const uint32_t AVG_VALUE = 64U; 2210 static int32_t ubsenseb_voltage_accum = U_BAT_RECOVERY_VOLTAGE_mV * AVG_VALUE; // << ubsenseb_voltage_accum_avg; 2211 2212 // Special case for Battery undervoltage condition. During normal operation ubsenseb_voltage will never be zero 2213 //if (sys_data.s.ubsenseb_voltage == 0) ubsenseb_voltage_accum = 0; 2183 2214 2184 2215 // Calculating and averaging battery side voltage 2185 int32_t temp = ADC_values[U_BAT_CHANNEL]; // Converting ADC value into int32_t type2216 int32_t temp = ADC_values[U_BAT_CHANNEL]; 2186 2217 temp = (temp*ADC_VREF)>>ADC_RESOLUTION; // Getting voltage value on ADC input [0:3000]mV 2187 2218 #ifdef VARIANT_24V … … 2193 2224 if (ubsenseb_voltage_accum < 0) ubsenseb_voltage_accum = 0; 2194 2225 ubsenseb_voltage_accum += temp; 2195 sys_data.s.ubsenseb_voltage = ubsenseb_voltage_accum / 32; //>> ubsenseb_voltage_accum_avg; 2196 } 2197 2198 //------------------------------------------------------------------------------ 2199 2200 inline __attribute__((always_inline)) void TestingForLowVoltageShutdown(void) 2201 { 2226 sys_data.s.ubsenseb_voltage = ubsenseb_voltage_accum / AVG_VALUE; //>> ubsenseb_voltage_accum_avg; 2227 } 2228 2229 //------------------------------------------------------------------------------ 2230 2231 static inline __attribute__((always_inline)) void TestingForLowVoltageShutdown(void) 2232 { 2233 const uint32_t REACTIVATION_DELAY_ms = 10000; 2234 static uint32_t low_bat_event_last_time; 2235 2202 2236 // If voltage on contact B is lower than critical one, then we must initiate "low voltage shutdown" 2203 if (sys_data.s.ubsenseb_voltage < U_BAT_CRITICAL_VOLTAGE )2237 if (sys_data.s.ubsenseb_voltage < U_BAT_CRITICAL_VOLTAGE_mV) 2204 2238 { 2205 2239 if (low_bat_shutdown_is_active == 0) 2206 2240 { 2241 #ifdef DISABLE_SHORTCUT_DETECTION_DURING_SWITCH_OFF 2242 DisableShortCutDetection(); 2243 #endif 2244 HAL_NVIC_DisableIRQ(ADC_DMA_IRQ); 2245 MOSFETS_Management = &DoNothing; 2246 OpenBothMOSFETSVeryFast(); 2247 sys_data.s.relay_status = RELAY_IS_OPENED; 2248 HAL_NVIC_EnableIRQ(ADC_DMA_IRQ); 2249 ExternalRedLED_Management = &ExternalRedLED5ShortOnThenLongPauseBlinking; 2250 2207 2251 low_bat_shutdown_is_active = 1; 2208 2252 sys_data.s.device_status |= (1 << LOWBAT_ERROR); 2209 2253 sys_data.s.lowbat_error_cnt++; 2210 2254 statDataChanged = 1; 2211 } 2212 } 2213 else if (sys_data.s.ubsenseb_voltage > U_BAT_RECOVERY_VOLTAGE) 2255 2256 low_bat_event_last_time = HAL_GetTick(); 2257 } 2258 } 2259 else if (sys_data.s.ubsenseb_voltage > U_BAT_RECOVERY_VOLTAGE_mV) 2260 { 2261 if (low_bat_event_last_time + REACTIVATION_DELAY_ms < HAL_GetTick()) 2262 { 2263 low_bat_shutdown_is_active = 0; 2264 sys_data.s.device_status &= ~(1 << LOWBAT_ERROR); 2265 } 2266 } 2267 2268 /*if (low_bat_shutdown_is_active == 1) 2269 { 2270 2271 } 2272 2273 if (sys_data.s.ubsenseb_voltage > U_BAT_RECOVERY_VOLTAGE_mV) 2214 2274 { 2215 2275 low_bat_shutdown_is_active = 0; 2216 2276 sys_data.s.device_status &= ~(1 << LOWBAT_ERROR); 2217 } 2277 }*/ 2218 2278 } 2219 2279 … … 2309 2369 //------------------------------------------------------------------------------ 2310 2370 2371 static inline __attribute__((always_inline)) void BatteryLowVoltageProtection(/*uint32_t current_time*/) 2372 { 2373 //static uint32_t last_time = 0; 2374 2375 // During first start, somtimes we read 0s from ADC, so we need to wait for a while before calculating Min & Max values, especially Min values 2376 //if (current_time - last_time >= 0U) 2377 { 2378 //last_time = current_time; 2379 2380 CalculatingAndAveragingVoltageOnContactB(); 2381 2382 TestingForLowVoltageShutdown(); 2383 } 2384 } 2385 2386 //------------------------------------------------------------------------------ 2387 2311 2388 void HeavyCalculations(uint32_t current_time) 2312 2389 { … … 2332 2409 CalculatingMinMaxVoltagesForContactA(); 2333 2410 2334 CalculatingAndAveragingVoltageOnContactB();2335 2336 TestingForLowVoltageShutdown();2411 //CalculatingAndAveragingVoltageOnContactB(); 2412 2413 //TestingForLowVoltageShutdown(); 2337 2414 2338 2415 CalculatingMinMaxVoltagesForContactB(); -
trunk/firmware/CubeMX/Src/stm32g0xx_it.c
r4 r7 100 100 //extern uint32_t overload_shutdown_time; 101 101 extern void (*InrushCurrentManagement)(void); 102 extern int low_bat_shutdown_is_active; 102 103 103 104 //extern uint16_t i_samples[I_RMS_SAMPLES_COUNT]; … … 219 220 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 }*/ 221 235 222 236 MOSFETS_Management();
Note:
See TracChangeset
for help on using the changeset viewer.
