diff --git a/.circleci/config.yml b/.circleci/config.yml index a4065c2..ea8c3c2 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -16,7 +16,7 @@ jobs: steps: - run: name: Install system dependencies - command: apk add --no-cache build-base linux-headers + command: apk add --no-cache build-base linux-headers git libftdi1-dev - checkout - run: name: Install hex and rebar diff --git a/.credo.exs b/.credo.exs index 8b607c7..c524b18 100644 --- a/.credo.exs +++ b/.credo.exs @@ -4,7 +4,7 @@ %{ name: "default", files: %{ - included: ["lib/"], + included: ["lib/", "examples/"], excluded: ["lib/i2c/i2c_nif.ex"] }, strict: true, diff --git a/.formatter.exs b/.formatter.exs index 5a0d2db..c98e9ff 100644 --- a/.formatter.exs +++ b/.formatter.exs @@ -1,4 +1,4 @@ # Used by "mix format" [ - inputs: ["{mix,.formatter,.credo}.exs", "{config,lib,test}/**/*.{ex,exs}"] + inputs: ["{mix,.formatter,.credo}.exs", "{config,examples,lib,test}/**/*.{ex,exs}"] ] diff --git a/examples/backends/grisp.ex b/examples/backends/grisp.ex new file mode 100644 index 0000000..f38fd7f --- /dev/null +++ b/examples/backends/grisp.ex @@ -0,0 +1,78 @@ +defmodule Circuits.I2C.GRiSP do + @moduledoc """ + Circuits.I2C backend for GRiSP + + This is completely untested. + """ + @behaviour Circuits.I2C.Backend + + alias Circuits.I2C.Backend + alias Circuits.I2C.Bus + + defstruct [:ref] + + @doc """ + Return the I2C bus names on this system + + No supported options + """ + @impl Backend + def bus_names(_options) do + :grisp_i2c.buses() |> Map.keys() |> Enum.map(&Atom.to_string/1) + end + + @doc """ + Open an I2C bus + """ + @impl Backend + def open(bus_name, _options) do + ref = :grisp_i2c.open(String.to_atom(bus_name)) + {:ok, %__MODULE__{ref: ref}} + end + + @doc """ + Return information about this backend + """ + @impl Backend + def info() do + %{backend: __MODULE__} + end + + defimpl Bus do + @impl Bus + def read(%Circuits.I2C.GRiSP{ref: ref}, address, count, _options) do + with [result] <- :grisp_i2c.transfer(ref, [{:read, address, 0, count}]) do + {:ok, result} + end + end + + @impl Bus + def write(%Circuits.I2C.GRiSP{ref: ref}, address, data, _options) do + with [:ok] <- :grisp_i2c.transfer(ref, [{:write, address, 0, data}]) do + :ok + end + end + + @impl Bus + def write_read( + %Circuits.I2C.GRiSP{ref: ref}, + address, + write_data, + read_count, + options + ) do + with [:ok, result] <- + :grisp_i2c.transfer(ref, [ + {:write, address, 0, write_data}, + {:read, address, 0, count} + ]) do + {:ok, result} + end + end + + @impl Bus + def close(%Circuits.I2C.GRiSP{ref: ref}) do + :ok + end + end +end diff --git a/examples/backends/mpsse.ex b/examples/backends/mpsse.ex new file mode 100644 index 0000000..f34492e --- /dev/null +++ b/examples/backends/mpsse.ex @@ -0,0 +1,114 @@ +defmodule Circuits.I2C.MPSSE do + @moduledoc """ + Circuits.I2C backend for USB devices that use the FTDI MPSSE protocol + + Devices that speak MPSSE: + + * [Adafruit FT232H Breakout](https://www.adafruit.com/product/2264) + + Example use: + + ```elixir + iex> {:ok, i2c} = Circuits.I2C.MPSSE.open("anything", []) + {:ok, %Circuits.I2C.MPSSE{mpsse: #Reference<0.3204948360.1742602257.8675>}} + iex> Circuits.I2C.detect_devices(i2c) + 'P' + # This is also [0x50]. 0x50 is the address of an I2C EEPROM. Read the beginning. + iex> Circuits.I2C.write_read(i2c, 0x50, <<0>>, 16) + {:ok, <<34, 51, 68, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255>>} + iex> Circuits.I2C.close(i2c) + :ok + ``` + """ + @behaviour Circuits.I2C.Backend + + alias Circuits.I2C.Backend + alias Circuits.I2C.Bus + + defstruct [:mpsse] + + @doc """ + Return the I2C bus names on this system + + No supported options + """ + @impl Backend + def bus_names(_options) do + # TODO + ["i2c-1"] + end + + @doc """ + Open an I2C bus + """ + @impl Backend + def open(_bus_name, _options) do + with {:ok, mpsse} <- MPSSE.find_and_open(:i2c) do + {:ok, %__MODULE__{mpsse: mpsse}} + end + end + + @doc """ + Return information about this backend + """ + @impl Backend + def info() do + %{backend: __MODULE__} + end + + defimpl Bus do + @impl Bus + def read(%Circuits.I2C.MPSSE{mpsse: mpsse}, address, count, _options) do + address_rd = Bitwise.bsl(address, 1) + 1 + + with :ok <- MPSSE.start(mpsse), + :ok <- MPSSE.write(mpsse, <>), + :ack <- MPSSE.get_ack(mpsse), + {:ok, result} <- MPSSE.read(mpsse, count) do + MPSSE.stop(mpsse) + {:ok, result} + end + end + + @impl Bus + def write(%Circuits.I2C.MPSSE{mpsse: mpsse}, address, data, _options) do + address_wr = Bitwise.bsl(address, 1) + + with :ok <- MPSSE.start(mpsse), + :ok <- MPSSE.write(mpsse, [address_wr, data]), + :ack <- MPSSE.get_ack(mpsse) do + MPSSE.stop(mpsse) + :ok + end + end + + @impl Bus + def write_read( + %Circuits.I2C.MPSSE{mpsse: mpsse}, + address, + write_data, + read_count, + _options + ) do + address_wr = Bitwise.bsl(address, 1) + address_rd = address_wr + 1 + + with :ok <- MPSSE.start(mpsse), + :ok <- MPSSE.write(mpsse, [address_wr, write_data]), + :ack <- MPSSE.get_ack(mpsse), + :ok <- MPSSE.start(mpsse), + :ok <- MPSSE.write(mpsse, <>), + :ack <- MPSSE.get_ack(mpsse), + {:ok, result} <- MPSSE.read(mpsse, read_count) do + MPSSE.stop(mpsse) + {:ok, result} + end + end + + @impl Bus + def close(%Circuits.I2C.MPSSE{mpsse: mpsse}) do + MPSSE.close(mpsse) + :ok + end + end +end diff --git a/mix.exs b/mix.exs index 4763658..daf9312 100644 --- a/mix.exs +++ b/mix.exs @@ -32,7 +32,9 @@ defmodule Circuits.I2C.MixProject do ] end - defp elixirc_paths(env) when env in [:test, :dev], do: ["lib", "examples"] + defp elixirc_paths(:mpsse), + do: ["lib", "examples/backends/mpsse_backend.ex"] + defp elixirc_paths(_env), do: ["lib"] def application do @@ -66,7 +68,8 @@ defmodule Circuits.I2C.MixProject do {:credo, "~> 1.6", only: :dev, runtime: false}, {:credo_binary_patterns, "~> 0.2.2", only: :dev, runtime: false}, {:dialyxir, "~> 1.2", only: :dev, runtime: false}, - {:elixir_make, "~> 0.6", runtime: false} + {:elixir_make, "~> 0.6", runtime: false}, + {:mpsse, github: "fhunleth/mpsse", submodules: true, only: :mpsse} ] end diff --git a/mix.lock b/mix.lock index e8118fc..f4bcd63 100644 --- a/mix.lock +++ b/mix.lock @@ -1,16 +1,17 @@ %{ "bunt": {:hex, :bunt, "1.0.0", "081c2c665f086849e6d57900292b3a161727ab40431219529f13c4ddcf3e7a44", [:mix], [], "hexpm", "dc5f86aa08a5f6fa6b8096f0735c4e76d54ae5c9fa2c143e5a1fc7c1cd9bb6b5"}, - "credo": {:hex, :credo, "1.7.3", "05bb11eaf2f2b8db370ecaa6a6bda2ec49b2acd5e0418bc106b73b07128c0436", [:mix], [{:bunt, "~> 0.2.1 or ~> 1.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2 or ~> 1.0", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "35ea675a094c934c22fb1dca3696f3c31f2728ae6ef5a53b5d648c11180a4535"}, + "credo": {:hex, :credo, "1.7.5", "643213503b1c766ec0496d828c90c424471ea54da77c8a168c725686377b9545", [:mix], [{:bunt, "~> 0.2.1 or ~> 1.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2 or ~> 1.0", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "f799e9b5cd1891577d8c773d245668aa74a2fcd15eb277f51a0131690ebfb3fd"}, "credo_binary_patterns": {:hex, :credo_binary_patterns, "0.2.3", "0dabadbe3cfd8db14b69ff346c112bfadde9bf65dc7aea19c39743c8d2ed07fa", [:mix], [{:credo, "~> 1.6", [hex: :credo, repo: "hexpm", optional: false]}], "hexpm", "3c333a564ed3e27f5c9f69985a921b88ef90f131bf722d085957cc4b25b7a085"}, "dialyxir": {:hex, :dialyxir, "1.4.3", "edd0124f358f0b9e95bfe53a9fcf806d615d8f838e2202a9f430d59566b6b53b", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "bf2cfb75cd5c5006bec30141b131663299c661a864ec7fbbc72dfa557487a986"}, "earmark_parser": {:hex, :earmark_parser, "1.4.39", "424642f8335b05bb9eb611aa1564c148a8ee35c9c8a8bba6e129d51a3e3c6769", [:mix], [], "hexpm", "06553a88d1f1846da9ef066b87b57c6f605552cfbe40d20bd8d59cc6bde41944"}, - "elixir_make": {:hex, :elixir_make, "0.7.8", "505026f266552ee5aabca0b9f9c229cbb496c689537c9f922f3eb5431157efc7", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:certifi, "~> 2.0", [hex: :certifi, repo: "hexpm", optional: true]}], "hexpm", "7a71945b913d37ea89b06966e1342c85cfe549b15e6d6d081e8081c493062c07"}, + "elixir_make": {:hex, :elixir_make, "0.8.3", "d38d7ee1578d722d89b4d452a3e36bcfdc644c618f0d063b874661876e708683", [:mix], [{:castore, "~> 0.1 or ~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:certifi, "~> 2.0", [hex: :certifi, repo: "hexpm", optional: true]}], "hexpm", "5c99a18571a756d4af7a4d89ca75c28ac899e6103af6f223982f09ce44942cc9"}, "erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"}, - "ex_doc": {:hex, :ex_doc, "0.31.1", "8a2355ac42b1cc7b2379da9e40243f2670143721dd50748bf6c3b1184dae2089", [:mix], [{:earmark_parser, "~> 1.4.39", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.1", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "3178c3a407c557d8343479e1ff117a96fd31bafe52a039079593fb0524ef61b0"}, + "ex_doc": {:hex, :ex_doc, "0.32.0", "896afb57b1e00030f6ec8b2e19d3ca99a197afb23858d49d94aea673dc222f12", [:mix], [{:earmark_parser, "~> 1.4.39", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.1", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "ed2c3e42c558f49bda3ff37e05713432006e1719a6c4a3320c7e4735787374e7"}, "file_system": {:hex, :file_system, "1.0.0", "b689cc7dcee665f774de94b5a832e578bd7963c8e637ef940cd44327db7de2cd", [:mix], [], "hexpm", "6752092d66aec5a10e662aefeed8ddb9531d79db0bc145bb8c40325ca1d8536d"}, "jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"}, "makeup": {:hex, :makeup, "1.1.1", "fa0bc768698053b2b3869fa8a62616501ff9d11a562f3ce39580d60860c3a55e", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "5dc62fbdd0de44de194898b6710692490be74baa02d9d108bc29f007783b0b48"}, - "makeup_elixir": {:hex, :makeup_elixir, "0.16.1", "cc9e3ca312f1cfeccc572b37a09980287e243648108384b97ff2b76e505c3555", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "e127a341ad1b209bd80f7bd1620a15693a9908ed780c3b763bccf7d200c767c6"}, - "makeup_erlang": {:hex, :makeup_erlang, "0.1.3", "d684f4bac8690e70b06eb52dad65d26de2eefa44cd19d64a8095e1417df7c8fd", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "b78dc853d2e670ff6390b605d807263bf606da3c82be37f9d7f68635bd886fc9"}, + "makeup_elixir": {:hex, :makeup_elixir, "0.16.2", "627e84b8e8bf22e60a2579dad15067c755531fea049ae26ef1020cad58fe9578", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "41193978704763f6bbe6cc2758b84909e62984c7752b3784bd3c218bb341706b"}, + "makeup_erlang": {:hex, :makeup_erlang, "0.1.5", "e0ff5a7c708dda34311f7522a8758e23bfcd7d8d8068dc312b5eb41c6fd76eba", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "94d2e986428585a21516d7d7149781480013c56e30c6a233534bedf38867a59a"}, + "mpsse": {:git, "https://github.com/fhunleth/mpsse.git", "f49977a4d2a691fd8a1b0ab023f8ca276726bf9c", [submodules: true]}, "nimble_parsec": {:hex, :nimble_parsec, "1.4.0", "51f9b613ea62cfa97b25ccc2c1b4216e81df970acd8e16e8d1bdc58fef21370d", [:mix], [], "hexpm", "9c565862810fb383e9838c1dd2d7d2c437b3d13b267414ba6af33e50d2d1cf28"}, }