Skip to content

Commit

Permalink
Poll the ATECC508A for early command completion
Browse files Browse the repository at this point in the history
The command timings are pessimistic and most complete sooner than
their worst case. This implements a heuristic of waiting half the time
and then polling for the rest of the wait period.
  • Loading branch information
fhunleth committed Feb 7, 2019
1 parent 0eb9477 commit ff677c4
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/atecc508a/transport/i2c_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ defmodule ATECC508A.Transport.I2CServer do
# 1.5 ms in the datasheet
@atecc508a_wake_delay_ms 2
@atecc508a_signature <<0x04, 0x11, 0x33, 0x43>>
@atecc508a_poll_interval_ms 2

@spec start_link(keyword()) :: :ignore | {:error, any()} | {:ok, pid()}
def start_link([bus_name, address, process_name]) do
Expand Down Expand Up @@ -162,4 +163,24 @@ defmodule ATECC508A.Transport.I2CServer do
# See ATECC508A 6.2 for the sleep sequence.
Circuits.I2C.write(i2c, address, <<0x01>>)
end

defp poll_read(i2c, address, response_len, timeout, max_timeout) do
case Circuits.I2C.read(i2c, address, response_len) do
{:ok, _} = response ->
response

{:error, :i2c_nak} ->
new_timeout = timeout + @atecc508a_poll_interval_ms

if new_timeout < max_timeout do
Process.sleep(@atecc508a_poll_interval_ms)
poll_read(i2c, address, response_len, new_timeout, max_timeout)
else
{:error, :timeout}
end

other_error ->
other_error
end
end
end

0 comments on commit ff677c4

Please sign in to comment.