Changeset 7 for trunk/firmware/CubeMX/Src/main.c
- Timestamp:
- May 24, 2023, 1:47:09 PM (2 years ago)
- File:
-
- 1 edited
-
trunk/firmware/CubeMX/Src/main.c (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
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();
Note:
See TracChangeset
for help on using the changeset viewer.
