Skip to content

Commit d5bde7f

Browse files
committed
reimplement wakeup
1 parent 0d855b1 commit d5bde7f

File tree

1 file changed

+14
-33
lines changed

1 file changed

+14
-33
lines changed

adafruit_atecc/adafruit_atecc.py

Lines changed: 14 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -129,48 +129,29 @@ def __init__(self, i2c_bus, address=_REG_ATECC_DEVICE_ADDR, debug=False):
129129
"""
130130
self._debug = debug
131131
self._i2cbuf = bytearray(12)
132-
self._i2c_bus = i2c_bus
133-
self._i2c_device = None
134-
self.wakeup()
135-
if not self._i2c_device:
136-
self._i2c_device = I2CDevice(self._i2c_bus, address)
137-
self.idle()
132+
# don't probe, the device will NACK until woken up
133+
self._wake_device = I2CDevice(i2c_bus, 0x00, probe=False)
134+
self._i2c_device = I2CDevice(i2c_bus, address, probe=False)
138135
if (self.version() >> 8) not in (_ATECC_508_VER, _ATECC_608_VER):
139136
raise RuntimeError(
140137
"Failed to find 608 or 508 chip. Please check your wiring."
141138
)
142139

143140
def wakeup(self):
144141
"""Wakes up THE ATECC608A from sleep or idle modes.
145-
Returns True if device woke up from sleep/idle mode.
146142
"""
147-
while not self._i2c_bus.try_lock():
148-
pass
149-
# check if it exists, first
150-
if 0x60 in self._i2c_bus.scan():
151-
self._i2c_bus.unlock()
152-
return
153-
zero_bits = bytearray(2)
143+
# This is a hack to generate the ATECC Wake condition, which is SDA
144+
# held low for t > 60us (twlo). For an I2C clock freq of 100kHz, 8
145+
# clock cycles will be 80us. This signal is generated by trying to
146+
# address something at 0x00. It will fail, but the pattern should
147+
# wake up the ATECC.
148+
# pylint: disable=bare-except
154149
try:
155-
self._i2c_bus.writeto(0x0, zero_bits)
156-
except OSError:
157-
pass # this may fail, that's ok - its just to wake up the chip!
158-
time.sleep(_TWLO_TIME)
159-
data = self._i2c_bus.scan() # check for an i2c device
160-
161-
try:
162-
if data[0] != 96:
163-
raise TypeError("ATECCx08 not found - please check your wiring!")
164-
except IndexError:
165-
raise IndexError("ATECCx08 not found - please check your wiring!")
166-
self._i2c_bus.unlock()
167-
if not self._i2c_device:
168-
self._i2c_device = I2CDevice(self._i2c_bus, _REG_ATECC_DEVICE_ADDR)
169-
# check if we are ready to read from
170-
r = bytearray(1)
171-
self._get_response(r)
172-
if r[0] != 0x11:
173-
raise RuntimeError("Failed to wakeup")
150+
with self._wake_device as i2c:
151+
i2c.write(bytes([0x00]))
152+
except:
153+
pass
154+
time.sleep(0.001)
174155

175156
def idle(self):
176157
"""Puts the chip into idle mode

0 commit comments

Comments
 (0)