# `NanoKVMGo.Expansion`
[🔗](https://github.com/cocoa-xu/nano_kvm_go/blob/v0.4.0/lib/nano_kvm_go/expansion.ex#L1)

The two lines of the auxiliary USB-C port, the one with the lightning
icon. They are the SoC's UART3 pads, which the NanoKVM-Go system's kernel
makes a UART, GPIOs, PWM outputs or an I2C bus; Sipeed's Finger Robot is
a servo on them. Both are inputs pulled down until a mode is set:

| Mode        | Line 0                | Line 1                |
| ----------- | --------------------- | --------------------- |
| `:off`      | input, pulled down    | input, pulled down    |
| `:uart`     | UART TXD (`"ttyS3"`)  | UART RXD              |
| `:gpio`     | GPIO                  | GPIO                  |
| `:pwm`      | PWM output            | PWM output            |
| `:pwm_gpio` | PWM output            | GPIO                  |
| `:gpio_pwm` | GPIO                  | PWM output            |
| `:i2c`      | I2C SDA (`"i2c-2"`)   | I2C SCL               |

    NanoKVMGo.Expansion.set_mode(:pwm_gpio)
    NanoKVMGo.Expansion.set_pwm(0, 20_000_000, 1_500_000)
    NanoKVMGo.Expansion.subscribe()
    NanoKVMGo.Expansion.read(1)
    #=> {:ok, 0}

Subscribers get `{NanoKVMGo.Expansion, {:edge, line, value, timestamp}}`
for each edge of a GPIO input, the timestamp in nanoseconds of the
monotonic clock. A line becomes an output with `write/3` and an input
again with `read/2` or `subscribe/1`.

The mode persists in the U-Boot environment, `nanokvm_expansion`, and is
set again when this process starts; with `persist: false` it lasts until
the next boot only. The GPIOs and PWM channels this process opened are given back
before the mode changes, and the kernel refuses a mode change with
`{:error, :busy}` while other code holds them. Use `Circuits.UART` on
`"ttyS3"` in the `:uart` mode and `Circuits.I2C` on `"i2c-2"` in the
`:i2c` mode; the I2C bus is bit-banged at about 50 kHz and needs pull-ups
on the other side.

Which USB-C contacts carry the lines, SBU1/SBU2 or D+/D-, and at what
voltage, most likely 3.3 V, is not known yet. Measure before connecting
anything but the Finger Robot. On SBU, flipping the plug swaps the lines.

## Options

  * `:name` - the name to register, `NanoKVMGo.Expansion` by default
  * `:device` - the kernel driver's sysfs directory,
    `/sys/devices/platform/expansion` by default
  * `:pwm_chip` - the PWM chip's sysfs directory, by default the one of
    the SoC's PWM1 (`6061000.pwm`)
  * `:kv` - the U-Boot environment, where the mode persists,
    `Nerves.Runtime.KV` (of `nerves_runtime`) by default
  * `:gpio` - the GPIO library, `Circuits.GPIO` by default

# `info`

```elixir
@type info() :: %{
  mode: mode(),
  modes: [mode()],
  lines: [line_info()],
  uart: String.t(),
  i2c: String.t()
}
```

# `line`

```elixir
@type line() :: 0 | 1
```

# `line_info`

```elixir
@type line_info() :: %{
  line: line(),
  role: role(),
  gpio: %{direction: :input | :output, value: 0 | 1} | nil,
  pwm: %{period: pos_integer(), duty_cycle: non_neg_integer()} | nil
}
```

# `message`

```elixir
@type message() :: {NanoKVMGo.Expansion, {:edge, line(), 0 | 1, integer()}}
```

A message to subscribers, see the module documentation.

# `mode`

```elixir
@type mode() :: :off | :uart | :gpio | :pwm | :pwm_gpio | :gpio_pwm | :i2c
```

# `option`

```elixir
@type option() ::
  {:name, GenServer.name()}
  | {:device, Path.t()}
  | {:pwm_chip, Path.t()}
  | {:kv, module()}
  | {:gpio, module()}
```

# `role`

```elixir
@type role() :: :off | :uart_txd | :uart_rxd | :gpio | :pwm | :i2c_sda | :i2c_scl
```

# `child_spec`

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `info`

```elixir
@spec info(GenServer.server()) :: {:ok, info()} | {:error, term()}
```

The mode, the modes the kernel offers and what each line is: a GPIO's
direction and value, a PWM's period and duty cycle in nanoseconds, while
this process has them.

# `read`

```elixir
@spec read(GenServer.server(), line()) :: {:ok, 0 | 1} | {:error, term()}
```

Reads a GPIO line, making it an input.

# `set_mode`

```elixir
@spec set_mode(GenServer.server(), mode(), [{:persist, boolean()}]) ::
  :ok | {:error, term()}
```

Sets the mode, persisted for the next boots unless `persist: false`:
`set_mode(:uart)`, `set_mode(server, :uart)` or
`set_mode(server, :uart, persist: false)`. A mode the kernel does not
offer gives `{:error, {:unknown_mode, mode}}`, and one set but not
persisted `{:error, {:not_kept, reason}}`.

# `set_pwm`

```elixir
@spec set_pwm(GenServer.server(), line(), pos_integer(), non_neg_integer()) ::
  :ok | {:error, term()}
```

Runs a PWM line: high for `duty_cycle` of every `period` nanoseconds. The
timer counts 24 MHz, in steps of 41.7 ns; a change starts a new period.

# `start_link`

```elixir
@spec start_link([option()]) :: GenServer.on_start()
```

Starts the process and sets the persisted mode. See the module documentation
for the options.

# `stop_pwm`

```elixir
@spec stop_pwm(GenServer.server(), line()) :: :ok | {:error, term()}
```

Stops a PWM line and gives its channel back.

# `subscribe`

```elixir
@spec subscribe(GenServer.server()) :: :ok
```

Sends the caller the edges of the GPIO inputs, now and after mode changes.

# `unsubscribe`

```elixir
@spec unsubscribe(GenServer.server()) :: :ok
```

Stops sending the caller edges.

# `write`

```elixir
@spec write(GenServer.server(), line(), 0 | 1) :: :ok | {:error, term()}
```

Drives a GPIO line, making it an output.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
