1 # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. 2 # Use of this source code is governed by a BSD-style license that can be 3 # found in the LICENSE file. 4 5 import logging 6 7 from autotest_lib.client.common_lib import error 8 from autotest_lib.server.cros.faft.firmware_test import FirmwareTest 9 10 class firmware_ECCharging(FirmwareTest): 11 """ 12 Servo based EC charging control test. 13 """ 14 version = 1 15 16 # Threshold of trickle charging current in mA 17 TRICKLE_CHARGE_THRESHOLD = 100 18 19 def initialize(self, host, cmdline_args): 20 super(firmware_ECCharging, self).initialize(host, cmdline_args) 21 # Only run in normal mode 22 self.switcher.setup_mode('normal') 23 self.ec.send_command("chan 0") 24 25 26 def cleanup(self): 27 self.ec.send_command("chan 0xffffffff") 28 super(firmware_ECCharging, self).cleanup() 29 30 31 def _get_battery_desired_voltage(self): 32 """Get battery desired voltage value.""" 33 voltage = int(self.ec.send_command_get_output("battery", 34 ["V-desired:\s+0x[0-9a-f]*\s+=\s+(\d+)\s+mV"])[0][1]) 35 logging.info("Battery desired voltage = %d mV", voltage) 36 return voltage 37 38 39 def _get_battery_desired_current(self): 40 """Get battery desired current value.""" 41 current = int(self.ec.send_command_get_output("battery", 42 ["I-desired:\s+0x[0-9a-f]*\s+=\s+(\d+)\s+mA"])[0][1]) 43 logging.info("Battery desired current = %d mA", current) 44 return current 45 46 47 def _get_battery_actual_voltage(self): 48 """Get the actual voltage from charger to battery.""" 49 voltage = int(self.ec.send_command_get_output("battery", 50 ["V:\s+0x[0-9a-f]*\s+=\s+(\d+)\s+mV"])[0][1]) 51 logging.info("Battery actual voltage = %d mV", voltage) 52 return voltage 53 54 55 def _get_battery_actual_current(self): 56 """Get the actual current from charger to battery.""" 57 current = int(self.ec.send_command_get_output("battery", 58 ["I:\s+0x[0-9a-f]*\s+=\s+([0-9-]+)\s+mA"])[0][1]) 59 logging.info("Battery actual current = %d mA", current) 60 return current 61 62 63 def _get_battery_charge(self): 64 """Get battery charge state.""" 65 charge = int(self.ec.send_command_get_output("battery", 66 ["Charge:\s+(\d+)\s+"])[0][1]) 67 logging.info("Battery charge = %d %%", charge) 68 return charge 69 70 71 def _get_charger_target_voltage(self): 72 """Get target charging voltage set in charger.""" 73 voltage = int(self.ec.send_command_get_output("charger", 74 ["V_batt:\s+(\d+)\s"])[0][1]) 75 logging.info("Charger target voltage = %d mV", voltage) 76 return voltage 77 78 79 def _get_charger_target_current(self): 80 """Get target charging current set in charger.""" 81 current = int(self.ec.send_command_get_output("charger", 82 ["I_batt:\s+(\d+)\s"])[0][1]) 83 logging.info("Charger target current = %d mA", current) 84 return current 85 86 87 def _get_trickle_charging(self): 88 """Check if we are trickle charging battery.""" 89 return (self._get_battery_desired_current() < 90 self.TRICKLE_CHARGE_THRESHOLD) 91 92 93 def _check_target_value(self): 94 """Check charger target values are correct. 95 96 Raise: 97 error.TestFail: Raised when check fails. 98 """ 99 if (self._get_charger_target_voltage() >= 100 1.05 * self._get_battery_desired_voltage()): 101 raise error.TestFail("Charger target voltage is too high.") 102 if (self._get_charger_target_current() >= 103 1.05 * self._get_battery_desired_current()): 104 raise error.TestFail("Charger target current is too high.") 105 106 107 def _check_actual_value(self): 108 """Check actual voltage/current values are correct. 109 110 Raise: 111 error.TestFail: Raised when check fails. 112 """ 113 if (self._get_battery_actual_voltage() >= 114 1.05 * self._get_charger_target_voltage()): 115 raise error.TestFail("Battery actual voltage is too high.") 116 if (self._get_battery_actual_current() >= 117 1.05 * self._get_charger_target_current()): 118 raise error.TestFail("Battery actual current is too high.") 119 120 121 def run_once(self): 122 if not self.check_ec_capability(['battery', 'charging']): 123 raise error.TestNAError("Nothing needs to be tested on this device") 124 if self._get_battery_charge() == 100: 125 logging.info("Battery is full. Unable to test.") 126 return 127 if self._get_trickle_charging(): 128 logging.info("Trickling charging battery. Unable to test.") 129 return 130 if self._get_battery_actual_current() < 0: 131 raise error.TestFail("This test must be run with AC power.") 132 133 logging.info("Checking charger target values...") 134 self._check_target_value() 135 136 logging.info("Checking battery actual values...") 137 self._check_actual_value() 138