From 31c8c4612c37f1653c5d499388c34db7ebab6187 Mon Sep 17 00:00:00 2001 From: Wiktoria Kuna Date: Tue, 17 Dec 2024 16:26:22 +0100 Subject: [PATCH] test_bitbang: Make I2C bitbang calculation common 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 --- test/test_bitbang.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/test/test_bitbang.py b/test/test_bitbang.py index 68c5096b9c..b6ebf5b681 100644 --- a/test/test_bitbang.py +++ b/test/test_bitbang.py @@ -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() @@ -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) @@ -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 @@ -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)