Logo Search packages:      
Sourcecode: linux-fsl-imx51 version File versions  Download package

static ddi_bc_Status_t ddi_bc_Charging ( void   )  [static]

Charging state function.

Function

This function implements the Charging state.

Definition at line 632 of file ddi_bc_sm.c.

References DDI_BC_BROKEN_CHARGING_TIMEOUT, ddi_bc_hwExpressibleCurrent(), ddi_bc_hwGetBatteryVoltage(), ddi_bc_hwGetChargeStatus(), ddi_bc_hwGetMaxCurrent(), ddi_bc_hwPowerSupplyIsPresent(), ddi_bc_hwSetCurrentThreshold(), ddi_bc_RampGetBatteryTempAlarm(), ddi_bc_RampGetDieTempAlarm(), ddi_bc_RampGetLimit(), ddi_bc_RampGetTarget(), ddi_bc_RampStep(), ddi_bc_RampUpdateAlarms(), DDI_BC_STATUS_BROKEN, DDI_BC_STATUS_SUCCESS, g_ddi_bc_Configuration, TransitionToBroken(), TransitionToToppingOff(), TransitionToWaitingToCharge(), _ddi_bc_Cfg::u32ChargingTimeout, and _ddi_bc_Cfg::u32StateMachinePeriod.

{

      //--------------------------------------------------------------------------
      // This variable counts the number of times we've seen the charging status
      // bit cleared.
      //--------------------------------------------------------------------------

      static int iStatusCount = 0;
      //--------------------------------------------------------------------------
      // The first order of business is to update alarms.
      //--------------------------------------------------------------------------

      ddi_bc_RampUpdateAlarms();

      //--------------------------------------------------------------------------
      // If we're not under an alarm, increment the state timer.
      //--------------------------------------------------------------------------

      if (!ddi_bc_RampGetDieTempAlarm() && !ddi_bc_RampGetBatteryTempAlarm()) {
            g_ddi_bc_u32StateTimer +=
                g_ddi_bc_Configuration.u32StateMachinePeriod;
      }
      /* Check if the power supply is still around. */


      if (!ddi_bc_hwPowerSupplyIsPresent()) {

            //----------------------------------------------------------------------
            // If control arrives here, the power supply has been removed. Go back
            // and wait.
            //----------------------------------------------------------------------

            TransitionToWaitingToCharge();

            //----------------------------------------------------------------------
            // Return success.
            //----------------------------------------------------------------------

            return (DDI_BC_STATUS_SUCCESS);

      }
      //--------------------------------------------------------------------------
      // If control arrives here, we're still connected to a power supply. We need
      // to decide now if the battery is still charging, or if it's nearly full.
      // If it's still charging, we'll stay in this state. Otherwise, we'll move
      // to the Topping Off state.
      //
      // Most of the time, we decide that the battery is still charging simply by
      // checking if the the actual current flow is above the charging threshold
      // current (as indicated by the charge status bit). However, if we're
      // still ramping up to full charging current, the hardware may still be set
      // to deliver an amount that's less than the threshold. In that case, the
      // charging status bit would *definitely* show a low charging current, but
      // that doesn't mean the battery is ready for topping off.
      //
      // So, in summary, we will move to the Topping Off state if both of the
      // following are true:
      //
      //  1) The maximum current set in the hardware is greater than the charging
      //     threshold.
      //  -AND-
      //  2) The actual current flow is also higher than the threshold (as
      //     indicated by the charge status bit).
      //
      //--------------------------------------------------------------------------



            ddi_bc_hwSetCurrentThreshold(g_ddi_bc_Configuration.
                                   u16ChargingThresholdCurrent);


      {
            uint16_t u16ActualProgrammedCurrent = ddi_bc_hwGetMaxCurrent();

            //----------------------------------------------------------------------
            // Get the Maximum current that we will ramp to.
            //----------------------------------------------------------------------

            //----------------------------------------------------------------------
            // Not all possible values are expressible by the BATTCHRG_I bitfield.
            // The following coverts the max current value into the the closest hardware
            // expressible bitmask equivalent.  Then, it converts this back to the actual
            // decimal current value that this bitmask represents.
            //----------------------------------------------------------------------

            uint16_t u16CurrentRampTarget = ddi_bc_RampGetTarget();

            if (u16CurrentRampTarget > ddi_bc_RampGetLimit())
                  u16CurrentRampTarget = ddi_bc_RampGetLimit();

            //----------------------------------------------------------------------
            // Not all possible values are expressible by the BATTCHRG_I bitfield.
            // The following coverts the max current value into the the closest hardware
            // expressible bitmask equivalent.  Then, it converts this back to the actual
            // decimal current value that this bitmask represents.
            //----------------------------------------------------------------------

            u16CurrentRampTarget =
                ddi_bc_hwExpressibleCurrent(u16CurrentRampTarget);

            //----------------------------------------------------------------------
            // We want to wait before we check the charge status bit until the ramping
            // up is complete.  Because the charge status bit is noisy, we want to
            // disregard it until the programmed charge currint in BATTCHRG_I is well
            // beyond the STOP_ILIMIT value.
            //----------------------------------------------------------------------
            if ((u16ActualProgrammedCurrent >= u16CurrentRampTarget) &&
                !ddi_bc_hwGetChargeStatus()) {
                  uint8_t u8IlimitThresholdLimit;
                  //----------------------------------------------------------------------
                  // If control arrives here, the hardware flag is telling us that the
                  // charging current has fallen below the threshold. We need to see this
                  // happen twice consecutively before we believe it. Increment the count.
                  //----------------------------------------------------------------------

                  iStatusCount++;


                  u8IlimitThresholdLimit = 10;

                  //----------------------------------------------------------------------
                  // How many times in a row have we seen this status bit low?
                  //----------------------------------------------------------------------

                  if (iStatusCount >= u8IlimitThresholdLimit) {

                        /*
                         * If control arrives here, we've seen the
                         * CHRGSTS bit low too many times. This means
                         * it's time to move to the Topping Off state.
                         * First, reset the status count for the next
                         * time we're in this state.
                         */

                        iStatusCount = 0;

#ifdef CONFIG_POWER_SUPPLY_DEBUG
                        u16ExternalBatteryPowerVoltageCheck =
                            ddi_bc_hwGetBatteryVoltage();
#endif



                        /* Move to the Topping Off state */


                        TransitionToToppingOff();

                        //------------------------------------------------------------------
                        // Return success.
                        //------------------------------------------------------------------

                        return (DDI_BC_STATUS_SUCCESS);

                  }

            } else {

                  //----------------------------------------------------------------------
                  // If control arrives here, the battery is still charging. Clear the
                  // status count.
                  //----------------------------------------------------------------------

                  iStatusCount = 0;

            }

      }

      //--------------------------------------------------------------------------
      // Have we been in this state too long?
      //--------------------------------------------------------------------------

      if (g_ddi_bc_u32StateTimer >= g_ddi_bc_Configuration.u32ChargingTimeout) {

            //----------------------------------------------------------------------
            // If control arrives here, we've been here too long.
            //----------------------------------------------------------------------

            ddi_bc_gBrokenReason = DDI_BC_BROKEN_CHARGING_TIMEOUT;

            TransitionToBroken();

            //----------------------------------------------------------------------
            // Tell our caller the battery appears to be broken.
            //----------------------------------------------------------------------

            return (DDI_BC_STATUS_BROKEN);

      }
      //--------------------------------------------------------------------------
      // If control arrives here, we're staying in this state. Step the current
      // ramp.
      //--------------------------------------------------------------------------

      ddi_bc_RampStep(g_ddi_bc_Configuration.u32StateMachinePeriod);

      //--------------------------------------------------------------------------
      // Return success.
      //--------------------------------------------------------------------------

      return (DDI_BC_STATUS_SUCCESS);

}


Generated by  Doxygen 1.6.0   Back to index