Changeset 7 for trunk/firmware/CubeMX


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

12V variant is working good.

Location:
trunk/firmware/CubeMX
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/firmware/CubeMX/Inc/main.h

    r4 r7  
    5959/* USER CODE BEGIN EFP */
    6060
     61#ifdef VARIANT_24V
    6162#define DISABLE_SHORTCUT_DETECTION_DURING_SWITCH_OFF
     63#endif
    6264
    6365#ifdef DISABLE_SHORTCUT_DETECTION_DURING_SWITCH_OFF
     
    141143/* USER CODE BEGIN Private defines */
    142144
    143 #define INVERTER_CAP_PRECHARGE
    144 
    145145#ifdef DEBUG
    146146        #define DEVICE_TYPE_ID                                          201
     
    148148        #ifdef VARIANT_24V
    149149                #define DEVICE_TYPE_ID                                  202
     150                #define INVERTER_CAP_PRECHARGE
    150151        #else
    151152                #define DEVICE_TYPE_ID                                  201
     
    183184#ifdef VARIANT_24V
    184185        #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 sequence
    186         #define INRUSH_CURRENT_mV                                       1100    // [mV] Some big value to pass test sequence
    187         #define COPPER_V_DROP_AT_CONTROL_CURRENT_mV     100             // [mV] - must be measured on the board
     186        #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
    188189#else
    189190        #define CONTROL_CURRENT_A                                       500             // [A] long-term current limit of the device.
     
    202203#define CURRENT_INTEGRAL_FREQ                                   320000U         // (7.5cycles + 12.5cycles)*1channels + (3.5cycles + 12.5cycles)*5channels = 20 + 80 = 100cycles @ 32MHz = 320 kHz
    203204#ifdef VARIANT_24V
    204         #define CURRENT_INTEGRATION_PERIOD                      575                     // µs
     205        #define CURRENT_INTEGRATION_PERIOD                      1500            // µs
    205206#else
    206207        #define CURRENT_INTEGRATION_PERIOD                      1700            // µs
     
    286287#define COMMAND_TURN_OVERLOAD_DETECTION_ON              30405
    287288
    288 #define U_BAT_CALC_PERIOD                                       5       // ms
     289//#define U_BAT_CALC_PERIOD                                     5       // ms
    289290#ifdef VARIANT_24V
    290291#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)
    291292#define U_BAT_RECOVERY_VOLTAGE                          24000 // mV     (If voltage on battery is higher than 12V, then it is fully recovered from possible previous discharge)
    292293#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)))
    295297#endif
    296298
  • trunk/firmware/CubeMX/Src/gpio.c

    r1 r7  
    7474  /*Configure GPIO pin : PtPin */
    7575  GPIO_InitStruct.Pin = TP4_Pin;
    76   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
     76  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    7777  GPIO_InitStruct.Pull = GPIO_NOPULL;
     78  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    7879  HAL_GPIO_Init(TP4_GPIO_Port, &GPIO_InitStruct);
    7980
  • 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();
  • trunk/firmware/CubeMX/Src/stm32g0xx_it.c

    r4 r7  
    100100//extern uint32_t overload_shutdown_time;
    101101extern void (*InrushCurrentManagement)(void);
     102extern int low_bat_shutdown_is_active;
    102103
    103104//extern uint16_t i_samples[I_RMS_SAMPLES_COUNT];
     
    219220
    220221  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  }*/
    221235
    222236  MOSFETS_Management();
Note: See TracChangeset for help on using the changeset viewer.