NanoKVMGo.Expansion (nano_kvm_go v0.4.0)

Copy Markdown View Source

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:

ModeLine 0Line 1
:offinput, pulled downinput, pulled down
:uartUART TXD ("ttyS3")UART RXD
:gpioGPIOGPIO
:pwmPWM outputPWM output
:pwm_gpioPWM outputGPIO
:gpio_pwmGPIOPWM output
:i2cI2C 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

Summary

Types

A message to subscribers, see the module documentation.

Functions

Returns a specification to start this module under a supervisor.

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.

Reads a GPIO line, making it an input.

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}}.

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.

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

Stops a PWM line and gives its channel back.

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

Stops sending the caller edges.

Drives a GPIO line, making it an output.

Types

info()

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

line()

@type line() :: 0 | 1

line_info()

@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()

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

A message to subscribers, see the module documentation.

mode()

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

option()

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

role()

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

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

info(server \\ __MODULE__)

@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(server \\ __MODULE__, line)

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

Reads a GPIO line, making it an input.

set_mode(server \\ __MODULE__, mode, opts \\ [])

@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(server \\ __MODULE__, line, period, duty_cycle)

@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(opts \\ [])

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

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

stop_pwm(server \\ __MODULE__, line)

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

Stops a PWM line and gives its channel back.

subscribe(server \\ __MODULE__)

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

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

unsubscribe(server \\ __MODULE__)

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

Stops sending the caller edges.

write(server \\ __MODULE__, line, value)

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

Drives a GPIO line, making it an output.