24
24
====================================================
25
25
Performs random writes and reads to I2C EEPROM.
26
26
27
- Run this script as its own main.py to individually run the test, or compile
27
+ Run this script as its own main.py to individually run the test, or compile
28
28
with mpy-cross and call from separate test script.
29
29
30
30
* Author(s): Shawn Hymel for Adafruit Industries
34
34
35
35
**Hardware:**
36
36
37
- * `Microchip AT24HC04B I2C EEPROM <https://www.digikey.com/product-detail/en/microchip-technology/AT24HC04B-PU/AT24HC04B-PU-ND/1886137>`_
37
+ * `Microchip AT24HC04B I2C EEPROM <https://www.digikey.com/product-detail/en/\
38
+ microchip-technology/AT24HC04B-PU/AT24HC04B-PU-ND/1886137>`_
38
39
39
40
**Software and Dependencies:**
40
41
43
44
44
45
"""
45
46
46
- import board
47
- import busio
48
47
import random
49
48
import time
50
49
50
+ import board
51
+ import busio
52
+
51
53
__version__ = "0.0.0-auto.0"
52
54
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_BoardTest.git"
53
55
66
68
NA = "N/A"
67
69
68
70
# Open comms to I2C EEPROM by trying a write to memory address
69
- def _eeprom_i2c_wait (i2c , i2c_addr , mem_addr , timeout = 1.0 ):
70
-
71
+ def _eeprom_i2c_wait (i2c , i2c_addr , mem_addr , timeout = 1.0 ):
72
+
71
73
# Try to access the I2C EEPROM (it becomes unresonsive during a write)
72
74
timestamp = time .monotonic ()
73
75
while time .monotonic () < timestamp + timeout :
74
76
try :
75
77
i2c .writeto (i2c_addr , bytearray ([mem_addr ]), end = 1 , stop = False )
76
78
return True
77
- except :
79
+ except OSError :
78
80
pass
79
-
81
+
80
82
return False
81
83
82
84
# Write to address. Returns status (True for successful write, False otherwise)
83
- def _eeprom_i2c_write_byte (i2c , i2c_addr , mem_addr , mem_data , timeout = 1.0 ):
84
-
85
+ def _eeprom_i2c_write_byte (i2c , i2c_addr , mem_addr , mem_data ):
86
+
85
87
# Make sure address is only one byte:
86
88
if mem_addr > 255 :
87
89
return False
88
-
90
+
89
91
# Make sure data is only one byte:
90
92
if mem_data > 255 :
91
93
return False
92
-
94
+
93
95
# Write data to memory at given address
94
96
try :
95
97
i2c .writeto (i2c_addr , bytearray ([mem_addr , mem_data ]))
96
- except :
98
+ except OSError :
97
99
return False
98
-
100
+
99
101
return True
100
102
101
103
# Read from address. Returns tuple [status, result]
102
- def _eeprom_i2c_read_byte (i2c , i2c_addr , mem_addr , timeout = 1.0 ):
103
-
104
+ def _eeprom_i2c_read_byte (i2c , i2c_addr , mem_addr , timeout = 1.0 ):
105
+
104
106
# Make sure address is only one byte:
105
107
if mem_addr > 255 :
106
108
return False , bytearray ()
107
-
109
+
108
110
# Try writing to address (EEPROM is unresponsive while writing)
109
- if _eeprom_i2c_wait (i2c , i2c_addr , mem_addr , timeout ) == False :
111
+ if not _eeprom_i2c_wait (i2c , i2c_addr , mem_addr , timeout ):
110
112
return False , bytearray ()
111
-
113
+
112
114
# Finish the read
113
115
buf = bytearray (1 )
114
116
i2c .readfrom_into (i2c_addr , buf )
115
-
117
+
116
118
return True , buf
117
119
118
120
def run_test (pins , sda_pin = SDA_PIN_NAME , scl_pin = SCL_PIN_NAME ):
119
-
121
+
120
122
"""
121
123
Performs random writes and reads to I2C EEPROM.
122
-
124
+
123
125
:param list[str] pins: list of pins to run the test on
124
126
:param str sda_pin: pin name of I2C SDA
125
127
:param str scl_pin: pin name of I2C SCL
@@ -131,7 +133,7 @@ def run_test(pins, sda_pin=SDA_PIN_NAME, scl_pin=SCL_PIN_NAME):
131
133
132
134
# Tell user to connect EEPROM chip
133
135
print ("Connect a Microchip AT24HC04B EEPROM I2C chip. " +
134
- "Press enter to continue." )
136
+ "Press enter to continue." )
135
137
input ()
136
138
137
139
# Set up I2C
@@ -143,7 +145,7 @@ def run_test(pins, sda_pin=SDA_PIN_NAME, scl_pin=SCL_PIN_NAME):
143
145
144
146
# Pick a random address, write to it, read from it, and see if they match
145
147
pass_test = True
146
- for i in range (NUM_I2C_TESTS ):
148
+ for _ in range (NUM_I2C_TESTS ):
147
149
148
150
# Randomly pick an address and a data value (one byte)
149
151
mem_addr = random .randint (0 , EEPROM_I2C_MAX_ADDR )
@@ -153,7 +155,7 @@ def run_test(pins, sda_pin=SDA_PIN_NAME, scl_pin=SCL_PIN_NAME):
153
155
154
156
# Try writing this random value to the random address
155
157
result = _eeprom_i2c_write_byte (i2c , EEPROM_I2C_ADDR , mem_addr , mem_data )
156
- if result == False :
158
+ if not result :
157
159
print ("FAIL: I2C could not communicate" )
158
160
pass_test = False
159
161
break
@@ -162,7 +164,7 @@ def run_test(pins, sda_pin=SDA_PIN_NAME, scl_pin=SCL_PIN_NAME):
162
164
result = _eeprom_i2c_read_byte (i2c , EEPROM_I2C_ADDR , mem_addr )
163
165
print ("Read:\t \t " + hex (result [1 ][0 ]))
164
166
print ()
165
- if result [0 ] == False :
167
+ if not result [0 ]:
166
168
print ("FAIL: I2C could not communicate" )
167
169
pass_test = False
168
170
break
@@ -179,23 +181,23 @@ def run_test(pins, sda_pin=SDA_PIN_NAME, scl_pin=SCL_PIN_NAME):
179
181
# Store results
180
182
if pass_test :
181
183
return PASS , [sda_pin , scl_pin ]
182
- else :
183
- return FAIL , [sda_pin , scl_pin ]
184
-
185
- else :
186
- print ("No I2C pins found" )
187
- return NA , []
184
+
185
+ return FAIL , [sda_pin , scl_pin ]
186
+
187
+ # Else (no pins found)
188
+ print ("No I2C pins found" )
189
+ return NA , []
188
190
189
191
def _main ():
190
-
192
+
191
193
# List out all the pins available to us
192
194
pins = [p for p in dir (board )]
193
195
print ()
194
196
print ("All pins found:" , end = ' ' )
195
-
197
+
196
198
# Print pins
197
- for p in pins :
198
- print (p , end = ' ' )
199
+ for pin in pins :
200
+ print (pin , end = ' ' )
199
201
print ('\n ' )
200
202
201
203
# Run test
0 commit comments