Skip to content

Reimplement wakeup() #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 15, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 15 additions & 35 deletions adafruit_atecc/adafruit_atecc.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,48 +113,28 @@ def __init__(self, i2c_bus, address=_REG_ATECC_DEVICE_ADDR, debug=False):
"""
self._debug = debug
self._i2cbuf = bytearray(12)
self._i2c_bus = i2c_bus
self._i2c_device = None
self.wakeup()
if not self._i2c_device:
self._i2c_device = I2CDevice(self._i2c_bus, address)
self.idle()
# don't probe, the device will NACK until woken up
self._wake_device = I2CDevice(i2c_bus, 0x00, probe=False)
self._i2c_device = I2CDevice(i2c_bus, address, probe=False)
if (self.version() >> 8) not in (_ATECC_508_VER, _ATECC_608_VER):
raise RuntimeError(
"Failed to find 608 or 508 chip. Please check your wiring."
)

def wakeup(self):
"""Wakes up THE ATECC608A from sleep or idle modes.
Returns True if device woke up from sleep/idle mode.
"""
while not self._i2c_bus.try_lock():
pass
# check if it exists, first
if 0x60 in self._i2c_bus.scan():
self._i2c_bus.unlock()
return
zero_bits = bytearray(2)
"""Wakes up THE ATECC608A from sleep or idle modes."""
# This is a hack to generate the ATECC Wake condition, which is SDA
# held low for t > 60us (twlo). For an I2C clock freq of 100kHz, 8
# clock cycles will be 80us. This signal is generated by trying to
# address something at 0x00. It will fail, but the pattern should
# wake up the ATECC.
# pylint: disable=bare-except
try:
self._i2c_bus.writeto(0x0, zero_bits)
except OSError:
pass # this may fail, that's ok - its just to wake up the chip!
time.sleep(_TWLO_TIME)
data = self._i2c_bus.scan() # check for an i2c device

try:
if data[0] != 96:
raise TypeError("ATECCx08 not found - please check your wiring!")
except IndexError as err:
raise IndexError("ATECCx08 not found - please check your wiring!") from err
self._i2c_bus.unlock()
if not self._i2c_device:
self._i2c_device = I2CDevice(self._i2c_bus, _REG_ATECC_DEVICE_ADDR)
# check if we are ready to read from
r = bytearray(1)
self._get_response(r)
if r[0] != 0x11:
raise RuntimeError("Failed to wakeup")
with self._wake_device as i2c:
i2c.write(bytes([0x00]))
except:
pass
time.sleep(0.001)

def idle(self):
"""Puts the chip into idle mode
Expand Down