1# Copyright (c) 2011 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"""A Python library to interact with TTCI module for TPM testing.
6
7Background
8 - TTCI stands for TPM Test Controller Interface
9 - TTCI is a custom-designed hardware board that can be used to test TPM module
10 - TTCI board contains two modules: PCA9555 and INA219. This library provides
11   methods to interact with these modules programmatically
12
13Dependency
14 - This library depends on a new C shared library called "libsmogcheck.so".
15 - In order to run test cases built using this API, one needs a TTCI board
16
17Notes:
18 - An exception is raised if it doesn't make logical sense to continue program
19   flow (e.g. I/O error prevents test case from executing)
20 - An exception is caught and then converted to an error code if the caller
21   expects to check for error code per API definition
22"""
23
24import logging
25from autotest_lib.client.common_lib import smogcheck_ina219, smogcheck_pca9555
26
27
28# I2C slave addresses of INA219 module
29INA219_BPWR_SLV = 0x40  # Backup Power
30INA219_MPWR_SLV = 0x44  # Main Power
31
32
33class TtciError(Exception):
34    """Base class for all errors in this module."""
35
36
37class TtciController(object):
38    """Object to control TTCI board used for TPM module testing."""
39
40    def __init__(self):
41        """Constructor.
42
43        Mandatory params:
44          err: error string.
45          ina_backup_obj: an instance of InaController (for Backup Power port
46                          of INA219 module).
47          ina_main_obj: an instance of InaController (for Main Power port
48                        of INA219 module).
49          pca_obj: an instance of PcaController.
50
51        Raises:
52          TtciError: if error initializing TTCI controller.
53        """
54        self.err = None
55        try:
56            # Initialize PCA9555 module.
57            self.pca_obj = smogcheck_pca9555.PcaController()
58
59            # Initialize INA219 module.
60            self.ina_main_obj = smogcheck_ina219.InaController(
61                slave_addr=INA219_MPWR_SLV)
62            self.ina_backup_obj = smogcheck_ina219.InaController(
63                slave_addr=INA219_BPWR_SLV)
64        except smogcheck_pca9555.PcaError, e:
65            raise TtciError('Error initialize PCA9555 module: %s' % e)
66        except smogcheck_ina219.InaError, e:
67            raise TtciError('Error initialize INA219 module: %s' % e)
68
69    def TTCI_Get_Main_Power_Metrics(self):
70        """Gets voltage and current measurements from INA219 Main Power.
71
72        See docstring of getPowerMetrics() in smogcheck_ina219.py.
73        """
74        return self.ina_main_obj.getPowerMetrics()
75
76    def TTCI_Get_Backup_Power_Metrics(self):
77        """Gets voltage and current measurements from INA219 Backup Power.
78
79        See docstring of getPowerMetrics() in smogcheck_ina219.py.
80        """
81        return self.ina_backup_obj.getPowerMetrics()
82
83    def TTCI_Set_Main_Power_Control(self, turn_on):
84        """De/activated TPM Main Power.
85
86        Args:
87          turn_on: a boolean, on (true) = set bit to 1.
88
89        See docstring of setPCAcontrol() in smogcheck_pca9555.py.
90        """
91        return self.pca_obj.setPCAcontrol('main_power', turn_on=turn_on)
92
93    def TTCI_Set_Backup_Power_Control(self, turn_on):
94        """De/activated TPM Backup Power.
95
96        Args:
97          turn_on: a boolean, on (true) = set bit to 1.
98
99        See docstring of setPCAcontrol() in smogcheck_pca9555.py.
100        """
101        return self.pca_obj.setPCAcontrol('backup_power', turn_on=turn_on)
102
103    def TTCI_Set_Reset_Control(self, turn_on):
104        """De/activated TPM Reset.
105
106        Exception note:
107          for TPM Reset, true means setting bit value to 0 (not 1).
108
109        Args:
110          turn_on: a boolean, on (true) = set bit to 0.
111
112        See docstring of setPCAcontrol() in smogcheck_pca9555.py.
113        """
114        return self.pca_obj.setPCAcontrol('reset', turn_on=not(turn_on))
115
116    def TTCI_Set_PP_Control(self, turn_on):
117        """De/activated TPM Physical Presence.
118
119        Args:
120          turn_on: a boolean, on (true) = set bit to 1.
121
122        See docstring of setPCAcontrol() in smogcheck_pca9555.py.
123        """
124        return self.pca_obj.setPCAcontrol('pp', turn_on=turn_on)
125
126    def TTCI_Set_TPM_I2C_Control(self, turn_on):
127        """Enable/Disable I2C communication with TPM.
128
129        Args:
130          turn_on: a boolean, on (true) = set bit to 1.
131
132        See docstring of setPCAcontrol() in smogcheck_pca9555.py.
133        """
134        return self.pca_obj.setPCAcontrol('tpm_i2c', turn_on=turn_on)
135
136    def TTCI_Get_Main_Power_Status(self):
137        """Checks bit value of Main Power.
138
139        See docstring of getPCAbitStatus() in smogcheck_pca9555.py.
140        """
141        return self.pca_obj.getPCAbitStatus('main_power')
142
143    def TTCI_Get_Backup_Power_Status(self):
144        """Checks bit value of Backup Power.
145
146        See docstring of getPCAbitStatus() in smogcheck_pca9555.py.
147        """
148        return self.pca_obj.getPCAbitStatus('backup_power')
149
150    def TTCI_Get_PP_Status(self):
151        """Checks bit value of Physical Presence.
152
153        See docstring of getPCAbitStatus() in smogcheck_pca9555.py.
154        """
155        return self.pca_obj.getPCAbitStatus('pp')
156
157    def TTCI_Get_TPM_I2C_Status(self):
158        """Checks bit value of TPM I2C.
159
160        See docstring of getPCAbitStatus() in smogcheck_pca9555.py.
161        """
162        return self.pca_obj.getPCAbitStatus('tpm_i2c')
163
164    def TTCI_Set_LEDs(self, bit_value, failure, warning):
165        """De/activates PCA9555 LEDs.
166
167        See docstring of setLEDs() in smogcheck_pca9555.py.
168        """
169        return self.pca_obj.setLEDs(bit_value, failure, warning)
170
171    def TTCI_Get_Switch_Status(self):
172        """Checks status of DIP Switches (2-bit).
173
174        See docstring of getSwitchStatus() in smogcheck_pca9555.py.
175        """
176        return self.pca_obj.getSwitchStatus()
177
178    def TTCI_Get_LED_Status(self):
179        """Checks LED status.
180
181        See docstring of getLEDstatus() in smogcheck_pca9555.py.
182        """
183        return self.pca_obj.getLEDstatus()
184
185
186def computeTimeElapsed(end, start):
187    """Computes time difference in microseconds.
188
189    Args:
190      end: a datetime.datetime() object, end timestamp.
191      start: a datetime.datetime() object, start timestamp.
192
193    Returns:
194      usec: an integer.
195    """
196    t = end - start
197    usec = 1000000 * t.seconds + t.microseconds
198    logging.info('Elapsed time = %d usec', usec)
199    return usec
200