Skip to content

Commit

Permalink
test_bitbang: Make I2C bitbang calculation common
Browse files Browse the repository at this point in the history
Use a common function to calculate the values for I2C master's outputs
and name the return values more explicitly.

Internal-tag: [#69926]
Signed-off-by: Wiktoria Kuna <[email protected]>
  • Loading branch information
wkkuna committed Dec 17, 2024
1 parent 365219e commit 31c8c46
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions test/test_bitbang.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@ def get_i2c_pads():
("sda", [("o", 1), ("oe", 1), ("i", 1)])])


class TestBitBangI2C(unittest.TestCase):
def i2c_master_output_bitbang(val):
scl_o = 0
scl_oe = (val ^ 1) & 1
sda_o = 0
sda_oe = ((val >> 1) & ~(val >> 2)) & 1
return (scl_o, scl_oe, sda_o, sda_oe)


def _master_output_bitbang(self, val):
return (0, (val ^ 1) & 1, 0, ((val >> 1) & ~(val >> 2)) & 1)
class TestBitBangI2C(unittest.TestCase):

def test_i2c_master_syntax(self):
i2c_master = I2CMaster()
Expand All @@ -54,7 +59,7 @@ def generator(i2c):
self.assertEqual((yield i2c._r.fields.sda), 0)
for i in range(8):
yield from i2c._w.write(i)
scl_o, scl_oe, sda_o, sda_oe = self._master_output_bitbang(i)
scl_o, scl_oe, sda_o, sda_oe = i2c_master_output_bitbang(i)
self.assertEqual((yield i2c.pads.scl.o), scl_o)
self.assertEqual((yield i2c.pads.scl.oe), scl_oe)
self.assertEqual((yield i2c.pads.sda.o), sda_o)
Expand All @@ -67,9 +72,6 @@ def generator(i2c):

class TestI2C(unittest.TestCase):

def _master_output_bitbang(self, val):
return (0, (val ^ 1) & 1, 0, ((val >> 1) & ~(val >> 2)) & 1)

@passive
def check_start(self, pads):
self.detect_start = False
Expand Down Expand Up @@ -191,7 +193,7 @@ def generator(i2c):
self.assertEqual((yield i2c._r.fields.sda), 0)
for i in range(8):
yield from i2c._w.write(i)
scl_o, scl_oe, sda_o, sda_oe = self._master_output_bitbang(i)
scl_o, scl_oe, sda_o, sda_oe = i2c_master_output_bitbang(i)
self.assertEqual((yield i2c.pads.scl.o), scl_o)
self.assertEqual((yield i2c.pads.scl.oe), scl_oe)
self.assertEqual((yield i2c.pads.sda.o), sda_o)
Expand Down

0 comments on commit 31c8c46

Please sign in to comment.