Low-Level API internals
Communication protocol specification
The whole goal of the Low-Level API is to bridge the chip's protocol to arrays of bytes that can be sent through the USB. The protocol is specified using Julia's type system. This means that the API defines a set of commands that correspond to the chip's (sub-)commands[subcommands]. The conversion to Vector{UInt8}
is done by MCP2221Driver.asarray
. It initializes a 64-bytes long vector and populates the two first items using MCP2221Driver.commandcode
and MCP2221Driver.subcommandcode
. The array is then mutated by MCP2221Driver.initarray!
to correspond to the chip's specification. This allows overwriting the sub-command item for commands that do not support sub-commands (for example, MCP2221Driver.I2CWriteDataCommand
).
The specification of the protocol through the type-system gives the type-stability of MCP2221Driver.query
. Indeed, since MCP2221Driver.expectsresponse
and MCP2221Driver.responsetype
are defined for each command types, the compiler knows what to expect as a response (nothing
when expectsresponse(T)
is false
, responsetype(T)
otherwise).
Low-Level API internals Reference
Commands and Responses
MCP2221Driver.AbstractCommand
— TypeAll mid-level commands inherit from AbstractCommand
. Commands should satisfy the mid-level command interface.
MCP2221Driver.AbstractResponse
— TypeAll mid-level responses inherit from AbstractResponse
. Responses should be able to build from an array of 64 UInt8
.
Command interface
MCP2221Driver.asarray
— Functionasarray(command)
Build a 64-long Vector{UInt8}
from the given command
.
MCP2221Driver.commandcode
— Functioncommandcode(::T) where {T<:AbstractCommand} ::UInt8
Return the first byte of a command sequence, identifying the command.
MCP2221Driver.subcommandcode
— Functionsubcommandcode(::T) where {T<:AbstractCommand} ::UInt8
Return the second byte of a command sequence, identifying the sub-command. Defaults to 0.
MCP2221Driver.responsetype
— Functionresponsetype(::Type{T}) where {T<:AbstractCommand}::Type{<:AbstractResponse}
Return the expected type of the command. It must me defined if expectsresponse
returns true for type T
.
MCP2221Driver.expectsresponse
— Functionexpectsresponse(::Type{T}) where {T<:AbstractCommand}::Bool
If true
, read a response of type responsetype(T)
from the device. Defaults to true
.
MCP2221Driver.initarray!
— Functioninitarray!(command, v)
Initialize the array v
given a command
. The command does not have to handle the two first bytes. It assumes v
elements are all zero.
Utilities
MCP2221Driver.ByteStringIterator
— TypeProduces bytes associated to a string, with at least L
bytes per character. If it is strict
, then each character will be exactly L
bytes, regardless of correctness.
MCP2221Driver.CommandSummary
— TypeInternal type used to make DocStringExtensions.jl
document the link between commands and reponses. Use with the COMMANDSUMMARY
abbreviation.
MCP2221Driver.writeaddress
— FunctionFormat an address for a write command according to MCP2221's specification.
MCP2221Driver.readaddress
— FunctionFormat an address for a read command according to MCP2221's specification.
Index
MCP2221Driver.AbstractCommand
MCP2221Driver.AbstractResponse
MCP2221Driver.ByteStringIterator
MCP2221Driver.CommandSummary
MCP2221Driver.asarray
MCP2221Driver.commandcode
MCP2221Driver.expectsresponse
MCP2221Driver.initarray!
MCP2221Driver.readaddress
MCP2221Driver.responsetype
MCP2221Driver.subcommandcode
MCP2221Driver.writeaddress
- subcommandsThe chip's flash memory access commands define a set of sub-commands. For simplicity, the Low-Level API maps those as normal commands and handles internally the conversion to sub-commands. Some commands have also been grouped, such as the I²C writing commands.