@@ -39,6 +39,9 @@ class I2CPrimitive:
39
39
created.
40
40
"""
41
41
42
+ _MIN_BRGVAL = 2
43
+ _MAX_BRGVAL = 511
44
+
42
45
_ACK = 0
43
46
_READ = 1
44
47
_WRITE = 0
@@ -48,6 +51,28 @@ def __init__(self, device: SerialHandler = None):
48
51
self ._running = False
49
52
self ._mode = None
50
53
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
+
51
76
def _start (self , address : int , mode : int ) -> int :
52
77
"""Initiate I2C transfer.
53
78
@@ -365,16 +390,12 @@ class I2CMaster(I2CPrimitive):
365
390
created.
366
391
"""
367
392
368
- _MIN_BRGVAL = 2
369
- _MAX_BRGVAL = 511
370
393
# Specs say typical delay is 110 ns to 130 ns; 150 ns from testing.
371
394
_SCL_DELAY = 150e-9
372
395
373
396
def __init__ (self , device : SerialHandler = None ):
374
397
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 ()
378
399
self .configure (125e3 ) # 125 kHz is as low as the PSLab can go.
379
400
380
401
def configure (self , frequency : float ):
@@ -385,21 +406,23 @@ def configure(self, frequency: float):
385
406
frequency : float
386
407
Frequency of SCL in Hz.
387
408
"""
388
- brgval = int (( 1 / frequency - self ._SCL_DELAY ) * CP . CLOCK_RATE - 2 )
409
+ brgval = self ._get_i2c_brgval ( frequency )
389
410
390
411
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 )
395
413
else :
396
414
min_frequency = self ._get_i2c_frequency (self ._MAX_BRGVAL )
397
415
max_frequency = self ._get_i2c_frequency (self ._MIN_BRGVAL )
398
416
e = f"Frequency must be between { min_frequency } and { max_frequency } Hz."
399
417
raise ValueError (e )
400
418
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 )
403
426
404
427
def scan (self ) -> List [int ]:
405
428
"""Scan I2C port for connected devices.
0 commit comments