Skip to content

Commit 671838c

Browse files
nkpro2000srbessman
authored andcommitted
Refactor I2C
1 parent 113aa0c commit 671838c

File tree

1 file changed

+35
-12
lines changed

1 file changed

+35
-12
lines changed

pslab/bus/i2c.py

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ class I2CPrimitive:
3939
created.
4040
"""
4141

42+
_MIN_BRGVAL = 2
43+
_MAX_BRGVAL = 511
44+
4245
_ACK = 0
4346
_READ = 1
4447
_WRITE = 0
@@ -48,6 +51,28 @@ def __init__(self, device: SerialHandler = None):
4851
self._running = False
4952
self._mode = None
5053

54+
def _init(self):
55+
self._device.send_byte(CP.I2C_HEADER)
56+
self._device.send_byte(CP.I2C_INIT)
57+
self._device.get_ack()
58+
59+
def _configure(self, brgval: int):
60+
"""Configure bus brgval.
61+
62+
Parameters
63+
----------
64+
brgval : int
65+
Brgval of SCL in Hz.
66+
"""
67+
if self._MIN_BRGVAL <= brgval <= self._MAX_BRGVAL:
68+
self._device.send_byte(CP.I2C_HEADER)
69+
self._device.send_byte(CP.I2C_CONFIG)
70+
self._device.send_int(brgval)
71+
self._device.get_ack()
72+
else:
73+
e = f"Brgval must be between {self._MIN_BRGVAL} and {self._MAX_BRGVAL}."
74+
raise ValueError(e)
75+
5176
def _start(self, address: int, mode: int) -> int:
5277
"""Initiate I2C transfer.
5378
@@ -365,16 +390,12 @@ class I2CMaster(I2CPrimitive):
365390
created.
366391
"""
367392

368-
_MIN_BRGVAL = 2
369-
_MAX_BRGVAL = 511
370393
# Specs say typical delay is 110 ns to 130 ns; 150 ns from testing.
371394
_SCL_DELAY = 150e-9
372395

373396
def __init__(self, device: SerialHandler = None):
374397
super().__init__(device)
375-
self._device.send_byte(CP.I2C_HEADER)
376-
self._device.send_byte(CP.I2C_INIT)
377-
self._device.get_ack()
398+
self._init()
378399
self.configure(125e3) # 125 kHz is as low as the PSLab can go.
379400

380401
def configure(self, frequency: float):
@@ -385,21 +406,23 @@ def configure(self, frequency: float):
385406
frequency : float
386407
Frequency of SCL in Hz.
387408
"""
388-
brgval = int((1 / frequency - self._SCL_DELAY) * CP.CLOCK_RATE - 2)
409+
brgval = self._get_i2c_brgval(frequency)
389410

390411
if self._MIN_BRGVAL <= brgval <= self._MAX_BRGVAL:
391-
self._device.send_byte(CP.I2C_HEADER)
392-
self._device.send_byte(CP.I2C_CONFIG)
393-
self._device.send_int(brgval)
394-
self._device.get_ack()
412+
self._configure(brgval)
395413
else:
396414
min_frequency = self._get_i2c_frequency(self._MAX_BRGVAL)
397415
max_frequency = self._get_i2c_frequency(self._MIN_BRGVAL)
398416
e = f"Frequency must be between {min_frequency} and {max_frequency} Hz."
399417
raise ValueError(e)
400418

401-
def _get_i2c_frequency(self, brgval: int) -> float:
402-
return 1 / ((brgval + 2) / CP.CLOCK_RATE + self._SCL_DELAY)
419+
@classmethod
420+
def _get_i2c_brgval(cls, frequency: float) -> int:
421+
return int((1 / frequency - cls._SCL_DELAY) * CP.CLOCK_RATE - 2)
422+
423+
@classmethod
424+
def _get_i2c_frequency(cls, brgval: int) -> float:
425+
return 1 / ((brgval + 2) / CP.CLOCK_RATE + cls._SCL_DELAY)
403426

404427
def scan(self) -> List[int]:
405428
"""Scan I2C port for connected devices.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy