Ignore:
Timestamp:
May 24, 2023, 1:47:09 PM (2 years ago)
Author:
f.jahn
Message:

12V variant is working good.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/firmware/CubeMX/Src/main.c

    r4 r7  
    193193void ExternalRedLED2ShortOnThen2LongOnThenLongPauseBlinking(void);
    194194void RS485DisableButtonManagement(uint32_t new_time);
     195static void BatteryLowVoltageProtection(/*uint32_t current_time*/);
    195196
    196197/* USER CODE END PFP */
     
    278279  SEGGER_RTT_printf(0, "Free space for statistics in fake EEPROM: %u bytes\n", FEEPROM_StatFreeBytes());
    279280  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);
    280282  SEGGER_RTT_printf(0, "CPU Freq: %u Hz\n", HAL_RCC_GetSysClockFreq());
    281283
     
    374376  while (1)                                                                                                                                             // Main loop
    375377  {
     378          TP4_GPIO_Port->BSRR = TP4_Pin;
     379
    376380          ABVoltageDropCalculation();
     381
     382          BatteryLowVoltageProtection();
     383
     384          TP4_GPIO_Port->BRR = TP4_Pin;
    377385
    378386          //TIM2->CNT = 0;
     
    448456                  case SWITCH_AUTO:
    449457                          // 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)
    451477                          {
    452478                                  if (!restartAutoMode)
     
    457483#endif
    458484                                          HAL_NVIC_DisableIRQ(ADC_DMA_IRQ);
    459                                           MOSFETS_Management = &ADC_Open_Both_MOSFETs;
     485                                          MOSFETS_Management = &DoNothing;
     486                                          OpenBothMOSFETSVeryFast();
    460487                                          sys_data.s.relay_status = RELAY_IS_OPENED;
    461488                                          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;
    464490                                          restartAutoMode = 1;
    465491                                  }
     
    722748  sys_data.s.command = 0;
    723749  sys_data.s.device_status = 0;
     750  sys_data.s.ubsenseb_voltage = 1;
    724751}
    725752
     
    21802207static inline __attribute__((always_inline)) void CalculatingAndAveragingVoltageOnContactB(void)
    21812208{
    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;
    21832214
    21842215        // Calculating and averaging battery side voltage
    2185         int32_t temp = ADC_values[U_BAT_CHANNEL];                                                                       // Converting ADC value into int32_t type
     2216        int32_t temp = ADC_values[U_BAT_CHANNEL];                                                                       
    21862217        temp = (temp*ADC_VREF)>>ADC_RESOLUTION;                                                                         // Getting voltage value on ADC input [0:3000]mV
    21872218#ifdef VARIANT_24V
     
    21932224        if (ubsenseb_voltage_accum < 0) ubsenseb_voltage_accum = 0;
    21942225        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
     2231static 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
    22022236        // 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)
    22042238        {
    22052239                if (low_bat_shutdown_is_active == 0)
    22062240                {
     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
    22072251                        low_bat_shutdown_is_active = 1;
    22082252                        sys_data.s.device_status |= (1 << LOWBAT_ERROR);
    22092253                        sys_data.s.lowbat_error_cnt++;
    22102254                        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)
    22142274        {
    22152275                low_bat_shutdown_is_active = 0;
    22162276                sys_data.s.device_status &= ~(1 << LOWBAT_ERROR);
    2217         }
     2277        }*/
    22182278}
    22192279
     
    23092369//------------------------------------------------------------------------------
    23102370
     2371static 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
    23112388void HeavyCalculations(uint32_t current_time)
    23122389{
     
    23322409                CalculatingMinMaxVoltagesForContactA();
    23332410
    2334                 CalculatingAndAveragingVoltageOnContactB();
    2335 
    2336                 TestingForLowVoltageShutdown();
     2411                //CalculatingAndAveragingVoltageOnContactB();
     2412
     2413                //TestingForLowVoltageShutdown();
    23372414
    23382415                CalculatingMinMaxVoltagesForContactB();
Note: See TracChangeset for help on using the changeset viewer.