1 # Copyright 2015 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 """This is a USB type C charging test using Plankton board.""" 6 7 import logging 8 import math 9 import time 10 11 from autotest_lib.client.common_lib import error 12 from autotest_lib.server.cros.faft.firmware_test import FirmwareTest 13 from autotest_lib.server.cros.servo import pd_console 14 15 class firmware_TypeCCharging(FirmwareTest): 16 """USB type C charging test.""" 17 version = 1 18 19 USBC_SINK_VOLTAGE = 5 20 VBUS_TOLERANCE = 0.12 21 VBUS_5V_CURRENT_RANGE = (2, 3.4) 22 VBUS_CHANGE_DELAY = 4 23 24 25 def run_once(self): 26 """Compares VBUS voltage and current with charging setting. 27 28 When charging voltage == 0, Plankton will act as a power sink and draws 29 5 volts from DUT. Other charging voltage should be seen on USB type C 30 VBUS INA meter in a 12% range. 31 32 When charging voltage == 5, Plankton INA current should be seen around 33 3 Amps (we set the range among 2 ~ 3.4 Amps just as in factory testing). 34 Other positive charging votage should not be less than 0 Amp. 35 36 @raise TestFail: If VBUS voltage or current is not in range. 37 """ 38 self.pd_console_utils = pd_console.PDConsoleUtils(self.plankton) 39 40 for charging_voltage in self.plankton.get_charging_voltages(): 41 self.plankton.charge(charging_voltage) 42 time.sleep(self.VBUS_CHANGE_DELAY) 43 pd_state = self.pd_console_utils.get_pd_state(0) 44 if charging_voltage > 0: 45 expected_state = self.pd_console_utils.SRC_CONNECT 46 else: 47 expected_state = self.pd_console_utils.SNK_CONNECT 48 logging.info('Plankton state = %s', pd_state) 49 if pd_state != expected_state: 50 raise error.TestFail('PD state != expected state, (%s != %s)' % 51 (pd_state, expected_state)) 52 expected_vbus_voltage = float( 53 charging_voltage if charging_voltage > 0 else 54 self.USBC_SINK_VOLTAGE) 55 tolerance = self.VBUS_TOLERANCE * expected_vbus_voltage 56 vbus_voltage = self.plankton.vbus_voltage 57 vbus_current = self.plankton.vbus_current 58 logging.info('Charging %dV: VBUS V=%f I=%f', charging_voltage, 59 vbus_voltage, vbus_current) 60 61 if math.fabs(expected_vbus_voltage - vbus_voltage) > tolerance: 62 raise error.TestFail( 63 'VBUS voltage out of range: %f (%f, delta %f)' % 64 (vbus_voltage, expected_vbus_voltage, tolerance)) 65 66 if charging_voltage == 0 and vbus_current > 0: 67 raise error.TestFail('Failed to consume power from DUT') 68 69 if charging_voltage > 0 and vbus_current <= 0: 70 raise error.Testfail( 71 'VBUS current less than 0 in %d volt: %f' % 72 (charging_voltage, vbus_current)) 73 74 if (charging_voltage == 5 and 75 (vbus_current < self.VBUS_5V_CURRENT_RANGE[0] or 76 vbus_current > self.VBUS_5V_CURRENT_RANGE[1])): 77 raise error.TestFail( 78 'VBUS current out of range in 5 volt: %f %r' % 79 (vbus_current, self.VBUS_5V_CURRENT_RANGE)) 80