ft232h

package module
v0.0.0-...-b952502 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 7, 2020 License: MIT Imports: 11 Imported by: 4

README

ft232h

Go module for FTDI FT232H USB to GPIO/SPI/I²C/JTAG/UART protocol converter

GoDoc Travis CI Go Report Card

API features

This software is a work-in-progress (WIP) and not ready for use. The following features have been implemented, but their interfaces maywill change.
  • Documented and integration tested
  • Multi-platform support (see: build matrix)
    • go1.11,gp1.12,go1.13,go1.14,go1.15,go-master
      • Linux: amd64,386,arm64,arm
      • macOS: amd64
  • GPIO - read/write
    • 8 dedicated pins available in any mode
    • 8-bit parallel, and 1-bit serial read/write operations
  • SPI - read/write
    • SPI modes 0 and 2 only, i.e. CPHA=1
    • configurable clock rate up to 30 MHz
    • chip/slave-select CS on both ports (pins D3—D7, C0—C7), including:
      • automatic assert-on-write/read with configurable polarity
      • multi-slave support with independent clocks SCLK, SPI modes, CPOL, etc.
    • unlimited effective transfer time/size
      • USB uses 64 KiB packets internally
  • I2C - read/write
    • configurable clock rate up to high speed mode (3.4 Mb/s)
    • internal or external SDA pullup option
    • unlimited effective transfer time/size
      • USB uses 64 KiB packets internally
  • JTAG - not yet implementented
  • UART - not yet implementented
  • TBD (WIP)

Installation

If you are not using Go modules for your application (or are unsure), use the built-in go package manager:

go get -u -v github.com/ardnew/ft232h

Otherwise, you are using Go modules, either use the same command above (sans -u), or simply add the import statement to your source code and the module will be installed automatically:

import (
  // ... other imports ...
  "github.com/ardnew/ft232h"
)

No other files or configuration to your build process are necessary.

Linux

Many Linux distributions ship with the FTDI Virtual COM Port (VCP) driver pre-installed (as a kernel module, usually ftdi_sio). However, according to FTDI:

For Linux, Mac OS X (10.4 and later) and Windows CE (4.2 and later) the D2XX driver and VCP driver are mutually exclusive options as only one driver type may be installed at a given time for a given device ID.

There are a lot of ways to resolve the issue, including fancy udev rules to swap out modules when (un)plugging devices, but I don't personally use the VCP driver.

On Ubuntu, you can simply prevent the VCP module from being auto-loaded at bootup by blacklisting the module. For example, create a new file /etc/modprobe.d/blacklist-ftdi.conf with a single directive:

# the official FTDI driver D2XX is incompatible with the VCP driver,
# preventing communication with FT232H breakouts
blacklist ftdi_sio

Be sure to unload the module if it was already loaded:

sudo rmmod ftdi_sio
macOS

Despite FTDI's own quote from the D2XX Programmer's Guide above, I've found that the current versions of macOS (10.13 and later, personal experience) have no problem co-existing with the D2XX driver included with this ft232h Go module. It Just Works and no configuration is necessary.

Documentation

Markdown godoc
Primary API reference github.com/ardnew/ft232h GoDoc
Supported peripheral devices github.com/ardnew/ft232h/drv GoDoc
Native FTDI drivers github.com/ardnew/ft232h/native GoDoc

Examples

Demo applications using this module and its device drivers can be found in examples/.

Usage examples for the API can be found in the godoc package documentation.

Notes

Where to get one

Adafruit sells a very nice breakout with a bunch of extras:

  • USB-C and Stemma QT/Qwiic I²C connectors (with a little switch to short the chip's two awkward SDA pins!)
  • On-board EEPROM (for storing chip configuration)
  • 5V (VBUS) and 3.3V (on-board regulator, up to 500mA draw) outputs

Documentation

Overview

High-level driver for the FTDI FT232H USB to GPIO/SPI/I²C/JTAG/UART protocol converter.

Example
package main

import (
	"log"

	"github.com/ardnew/ft232h"
)

func doStuff(ft *ft232h.FT232H) {
	// At this point, you can call Init() or Config() on one of the interface
	// fields GPIO, SPI, I2C, ...
	log.Printf("using: %s", ft) // FT232H implements String() descriptively
}

func main() {
	// Call New() to open an FT232H device from a command line-oriented
	// application to help select which FTDI device to use (by parsing predefined
	// command line flags) if more than one is connected to the system.
	//
	// If no flags are provided, the first MPSSE-capable USB device found is used.
	// Use -h to see all available flags.
	//
	// See the New() godoc for other semantics related to the flag package.
	//
	// To open a specific device without using command line flags, use one of the
	// functions of form Open*(). In particular, OpenMask(nil) will open the first
	// compatible device found.

	// Open first device that matches all command line flags (if any provided)
	ft, err := ft232h.New()
	if nil != err {
		log.Fatalf("New(): %s", err)
	}
	defer ft.Close() // be sure to close device

	doStuff(ft)
}
Output:

Index

Examples

Constants

View Source
const (
	I2CSlaveAddressMin = 0x08
	I2CSlaveAddressMax = 0x77
)

Constants defining legal 7-bit I²C slave addresses

View Source
const (
	I2CClockMaximum   I2CClockRate = I2CClockHighSpeedMode
	I2CClockDefault   I2CClockRate = I2CClockFastMode
	I2CLatencyDefault byte         = 2
)

Constants related to I²C interface initialization.

View Source
const (
	PinLO byte = 0 // pin value clear
	PinHI byte = 1 // pin value set
	PinIN byte = 0 // pin direction input
	PinOT byte = 1 // pin direction output

	NumDPins = 8 // number of MPSSE low-byte line pins, port "D"
	NumCPins = 8 // number of MPSSE high-byte line pins, port "C"
)

Constants related to GPIO pin configuration

View Source
const (
	SPIClockMaximum   uint32 = 30000000
	SPIClockDefault   uint32 = SPIClockMaximum
	SPILatencyDefault byte   = 2
)

Constants related to SPI interface initialization.

Variables

This section is empty.

Functions

func BlessFlag

func BlessFlag()

BlessFlag registers the flags in the flag package's default, top-level FlagSet var flag.CommandLine. This lets external packages (e.g. `go test`) inherit these flags and not call os.Exit() when these otherwise unexpected flags are received.

Types

type AddrSpace

type AddrSpace uint8

AddrSpace represents the address space of a pointer. Intended to be used when specifying e.g. I²C register addresses and the like.

const (
	Addr8Bit  AddrSpace = 1 << iota // 8-bit addresses
	Addr16Bit                       // 16-bit addresses
	Addr32Bit                       // 32-bit addresses
	Addr64Bit                       // 64-bit addresses
)

Constants defining various address spaces.

func (AddrSpace) Bits

func (s AddrSpace) Bits() uint

Bits returns the number of usable bits in an address space.

func (AddrSpace) Bytes

func (s AddrSpace) Bytes() uint

Bytes returns the number of usable bytes in an address space.

func (AddrSpace) String

func (s AddrSpace) String() string

String returns a string representation of the address space.

type ByteOrder

type ByteOrder uint8

ByteOrder represents the byte order of a sequence of bytes.

const (
	MSB ByteOrder = iota // most significant byte first (big endian)
	LSB                  // least significant byte first (little endian)
)

Constants defining the supported byte orderings.

func (ByteOrder) Bytes

func (o ByteOrder) Bytes(count uint, value uint64) []uint8

Bytes converts the given value to an ordered slice of bytes. The receiver value determines ordering, and count (≤ 8) defines slice length (in bytes).

func (ByteOrder) String

func (o ByteOrder) String() string

String returns a string representation of the byte order.

func (ByteOrder) Uint

func (o ByteOrder) Uint(count uint, bytes []uint8) uint64

Uint converts a given slice of bytes to an unsigned integer. The receiver value determines ordering, and count (≤ 8) defines the number of bytes (starting from the beginning of the slice) to use for conversion. Use count < 8 and cast the value returned if a narrower type is required. If count > length of bytes slice, the slize is padded with zeros.

type CPin

type CPin uint8 // pin bitmask on MPSSE high-byte lines (port "C" of FT232H)

Types representing individual port pins.

func C

func C(pin uint) CPin

C returns a CPin bitmask with only the given bit at position pin set. If the given pin position is greater than 7, the invalid bitmask (0) is returned.

func (CPin) Equals

func (p CPin) Equals(q Pin) bool

Equals is true if the given pin is on port "C" and has the same bitmask, otherwise false.

func (CPin) IsMPSSE

func (p CPin) IsMPSSE() bool

IsMPSSE is false for pins on FT232H port "C".

func (CPin) Mask

func (p CPin) Mask() uint8

Mask is the bitmask used to address the pin on port "C".

func (CPin) Pos

func (p CPin) Pos() uint

Pos is the ordinal pin number (0-7) on port "C".

func (CPin) String

func (p CPin) String() string

String is the string representation "C#" of the pin, with # equal to Pos.

func (CPin) Valid

func (p CPin) Valid() bool

Valid is true if the pin bitmask has exactly one bit set, otherwise false.

type Chip

type Chip C.FT_DEVICE

Type aliases for the native types needed by the C libraries.

const (
	CFTBM      Chip = C.FT_DEVICE_BM
	CFTAM      Chip = C.FT_DEVICE_AM
	CFT100AX   Chip = C.FT_DEVICE_100AX
	CFTUnknown Chip = C.FT_DEVICE_UNKNOWN
	CFT2232C   Chip = C.FT_DEVICE_2232C
	CFT232R    Chip = C.FT_DEVICE_232R
	CFT2232H   Chip = C.FT_DEVICE_2232H
	CFT4232H   Chip = C.FT_DEVICE_4232H
	CFT232H    Chip = C.FT_DEVICE_232H
	CFTX       Chip = C.FT_DEVICE_X_SERIES
	CFT4222H0  Chip = C.FT_DEVICE_4222H_0
	CFT4222H12 Chip = C.FT_DEVICE_4222H_1_2
	CFT4222H3  Chip = C.FT_DEVICE_4222H_3
	CFT4222P   Chip = C.FT_DEVICE_4222_PROG
	CFT900     Chip = C.FT_DEVICE_900
	CFT930     Chip = C.FT_DEVICE_930
	CUMFTPD3A  Chip = C.FT_DEVICE_UMFTPD3A
)

Constants defining the FTDI chip identifiers specified by FTDI.

func (Chip) String

func (c Chip) String() string

String returns the descriptive string representation of an FTDI chip. Returns the string "invalid chip" if the chip is not defined.

type DPin

type DPin uint8 // pin bitmask on MPSSE low-byte lines (port "D" of FT232H)

Types representing individual port pins.

func D

func D(pin uint) DPin

D returns a DPin bitmask with only the given bit at position pin set. If the given pin position is greater than 7, the invalid bitmask (0) is returned.

func (DPin) Equals

func (p DPin) Equals(q Pin) bool

Equals is true if the given pin is on port "D" and has the same bitmask, otherwise false.

func (DPin) IsMPSSE

func (p DPin) IsMPSSE() bool

IsMPSSE is true for pins on FT232H port "D".

func (DPin) Mask

func (p DPin) Mask() uint8

Mask is the bitmask used to address the pin on port "D".

func (DPin) Pos

func (p DPin) Pos() uint

Pos is the ordinal pin number (0-7) on port "D".

func (DPin) String

func (p DPin) String() string

String is the string representation "D#" of the pin, with # equal to Pos.

func (DPin) Valid

func (p DPin) Valid() bool

Valid is true if the pin bitmask has exactly one bit set, otherwise false.

type Dir

type Dir bool

Dir represents the direction of a GPIO pin

const (
	Input  Dir = false // GPIO input pins (bit clear)
	Output Dir = true  // GPIO output pins (bit set)
)

Constants of GPIO pin direction type Dir

type FT232H

type FT232H struct {
	I2C  *I2C
	SPI  *SPI
	GPIO *GPIO
	// contains filtered or unexported fields
}

FT232H is the primary type for interacting with the device, holding the USB device file descriptor configuration/status and individual communication interfaces. Open a connection with an FT232H by calling the New() constructor. If more than one FTDI device (any FTDI device, not just FT232H) is present on the system, there are several constructor variations of form Open*() to help distinguish which device to open. The default constructor New() will attempt to parse command line flags to select a specific device. The only interface that is initialized by default is GPIO. You must call an initialization method of one of the other interfaces before using it.

func New

func New() (*FT232H, error)

New attempts to open a connection with the first MPSSE-capable USB device matching flags given at the command line. Use -h to see all of the supported flags.

func OpenDesc

func OpenDesc(desc string) (*FT232H, error)

OpenDesc attempts to open a connection with the first MPSSE-capable USB device with given description. Returns a non-nil error if unsuccessful. An empty string matches any description.

func OpenFlag

func OpenFlag(arg []string, fatal bool) (*FT232H, error)

OpenFlag attempts to open a connection with the first MPSSE-capable USB device matching flags given in a command-line-style string slice. See type Flag and func NewFlag() for details.

func OpenIndex

func OpenIndex(index int) (*FT232H, error)

OpenIndex attempts to open a connection with the MPSSE-capable USB device enumerated at index (starting at 0). Returns non-nil error if unsuccessful. A negative index is equivalent to 0.

func OpenMask

func OpenMask(mask *Mask) (*FT232H, error)

OpenMask attempts to open a connection with the first MPSSE-capable USB device matching all of the given attributes. Returns a non-nil error if unsuccessful. Uses the first device found if mask is nil or all attributes are empty strings.

The attributes are each specified as strings, including the integers, so that any attribute not given (i.e. empty string) will never exclude a device. The integer attributes can be expressed in any base recognized by the Go grammar for numeric literals (e.g., "13", "0b1101", "0xD", and "D" are all valid and equivalent).

func OpenSerial

func OpenSerial(serial string) (*FT232H, error)

OpenSerial attempts to open a connection with the first MPSSE-capable USB device with given serial no. Returns a non-nil error if unsuccessful. An empty string matches any serial number.

func OpenVIDPID

func OpenVIDPID(vid uint16, pid uint16) (*FT232H, error)

OpenVIDPID attempts to open a connection with the first MPSSE-capable USB device with given vendor ID vid and product ID pid. Returns a non-nil error if unsuccessful.

func (*FT232H) Close

func (m *FT232H) Close() error

Close closes the USB connection with an FT232H. Returns a non-nil error if unsuccessful.

func (*FT232H) String

func (m *FT232H) String() string

String constructs a string representation of an FT232H device.

type Flag

type Flag struct {
	*flag.FlagSet
	// contains filtered or unexported fields
}

Flag contains the attributes used to distinguish which FT232H device to open from a command-line-style string slice.

func NewFlag

func NewFlag(fatal bool) *Flag

NewFlag constructs a new FlagSet with fields to describe an FT232H. If fatal is true, the program will call os.Exit() if the flag parser fails on malformed input, unrecognized flags are provided, or the default help flag -h is received.

func (*Flag) Mask

func (f *Flag) Mask() *Mask

Mask constructs an Mask using the parsed flags explicitly provided. If the Flag has not yet been parsed, a zero Mask is returned that matches all devices.

func (*Flag) Parse

func (f *Flag) Parse(arg []string) error

Parse parses flags from the given slice of strings arg into the fields of its receiver, and silently ignores any unexpected flags.

func (*Flag) String

func (f *Flag) String() string

String returns a descriptive string of all flags successfully parsed.

type GPIO

type GPIO struct {
	// contains filtered or unexported fields
}

GPIO stores interface configuration settings for the GPIO ("C" port) and provides methods for reading and writing to GPIO pins. The GPIO interface is always initialized and available in any mode.

func (*GPIO) Chdir

func (gpio *GPIO) Chdir(pin CPin, dir Dir) error

Chdir changes the GPIO direction of the given pin. Use ConfigPin() to change both direction and value, or Config() to change all pin directions (and values).

func (*GPIO) Config

func (gpio *GPIO) Config(cfg *GPIOConfig) error

Config configures all GPIO pin directions and values to the settings defined in the given cfg, returning a non-nil error if unsuccessful.

func (*GPIO) ConfigPin

func (gpio *GPIO) ConfigPin(pin CPin, dir Dir, val bool) error

ConfigPin configures the given GPIO pin direction and value. The direction and value of all other pins is set based on the most recently read or written configuration determined prior to this call, and are all updated during this call. If you need more fine-grained control, use Read()/Write() directly.

func (*GPIO) Get

func (gpio *GPIO) Get(pin CPin) (bool, error)

Get reads the current value of the given pin.

func (*GPIO) Init

func (gpio *GPIO) Init() error

Init resets all GPIO pin directions and values using the most recently read or written configuration, returning a non-nil error if unsuccessful.

func (*GPIO) Read

func (gpio *GPIO) Read() (uint8, error)

Read returns the current value of all GPIO pins, returning 0 and a non-nil error if unsuccessful.

func (*GPIO) Set

func (gpio *GPIO) Set(pin CPin, val bool) error

Set sets the given pin to output with the given val. See ConfigPin() for other semantics.

func (*GPIO) String

func (gpio *GPIO) String() string

func (*GPIO) Write

func (gpio *GPIO) Write(val uint8) error

Write sets the value of all output pins at once using the given bitmask val, returning a non-nil error if unsuccessful.

type GPIOConfig

type GPIOConfig struct {
	Dir uint8
	Val uint8
}

GPIOConfig stores the most-recently read/written pin levels and directions.

func GPIOConfigDefault

func GPIOConfigDefault() *GPIOConfig

GPIOConfigDefault returns the default pin levels and directions for the GPIO interface. All pins are configured as inputs at logic level LOW by default.

func (*GPIOConfig) Set

func (cfg *GPIOConfig) Set(pin CPin, dir Dir, val bool) error

Set changes the pin direction and value configuration. This does not transfer any changes to the GPIO interface.

func (*GPIOConfig) String

func (c *GPIOConfig) String() string

func (*GPIOConfig) Write

func (cfg *GPIOConfig) Write(dir uint8, val uint8)

Write changes all pin direction and value configurations. This does not transfer any changes to the GPIO interface.

type Handle

type Handle C.FT_HANDLE

Type aliases for the native types needed by the C libraries.

type I2C

type I2C struct {
	// contains filtered or unexported fields
}

I2C stores interface configuration settings for an I²C master and provides methods for reading and writing to I²C slave devices. The interface must be initialized by calling either Init or Config (not both) before use.

func (*I2C) Close

func (i2c *I2C) Close() error

Close closes both the I²C interface and the connection to the FT232H device.

func (*I2C) Config

func (i2c *I2C) Config(cfg *I2CConfig) error

Config initializes the I²C interface with the given configuration to a state ready for read/write. If the given configuration is nil, the default configuration is used (see I2CConfigDefault). It is not necessary to call Init after calling Config. See documentation of Init for other semantics.

func (*I2C) GetConfig

func (i2c *I2C) GetConfig() *I2CConfig

I2CConfig returns the current configuration settings of the I2C receiver.

func (*I2C) Init

func (i2c *I2C) Init() error

Init initializes the I²C interface to a state ready for read/write. If Config has not been called, the default configuration is used (see I2CConfigDefault). If the interface is already initialized, it is first closed before initializing the interface.

func (*I2C) Option

func (i2c *I2C) Option(opt *I2COption) error

Option changes the dynamic configuration parameters of the I²C interface. It can be called while the I²C interface is open without having to first close and reopen the device.

func (*I2C) Read

func (i2c *I2C) Read(slave uint, count uint, start bool, stop bool) ([]uint8, error)

Read reads the given count number of bytes from the I²C interface. The given slave is the unshifted 7-bit I²C slave address to read from. There is no maximum length for the number of bytes to read. If start is true, an I²C start condition is generated before transfer. If stop is true, an I²C stop condition is generated after transfer. Returns the slice of bytes successfully read and a non-nil error if there was an error.

func (*I2C) Reg

func (i2c *I2C) Reg(slave uint, addr uint, space AddrSpace, order ByteOrder) *I2CReg

Reg constructs a new I2CReg for conveniently reading and writing data in I²C slave device registers.

func (*I2C) String

func (i2c *I2C) String() string

String returns a descriptive string of an I²C interface.

func (*I2C) Write

func (i2c *I2C) Write(slave uint, data []uint8, start bool, stop bool) (uint, error)

Write writes the given byte slice data to the I²C interface. The given slave is the unshifted 7-bit I²C slave address to write to. There is no maximum length for the data slice. If start is true, an I²C start condition is generated before transfer. If stop is true, an I²C stop condition is generated after transfer. Returns the slice of bytes successfully written and a non-nil error if there was an error.

type I2CClockRate

type I2CClockRate uint32

I2CClockRate holds one of the supported I²C clock rate constants.

const (
	I2CClockStandardMode  I2CClockRate = 100000  // 100 kb/sec
	I2CClockFastMode      I2CClockRate = 400000  // 400 kb/sec
	I2CClockFastModePlus  I2CClockRate = 1000000 // 1000 kb/sec
	I2CClockHighSpeedMode I2CClockRate = 3400000 // 3.4 Mb/sec
)

Constants defining the supported I²C clock rates.%d ms

func (I2CClockRate) String

func (c I2CClockRate) String() string

String returns a descriptive string of an I2CClockRate.

type I2CConfig

type I2CConfig struct {
	*I2COption
	Clock        I2CClockRate // 100000 (100 kb/s) - 3400000 (3.4 Mb/s)
	Latency      byte         // 1-255 USB HiSpeed, 2-255 USB FullSpeed
	Clock3Phase  bool         // I²C 3-phase clocking enabled=true/disabled=false
	LowDriveOnly bool         // float HIGH (pullup) if true, drive HIGH if false
}

I2CConfig holds all of the configuration settings for initializing an I²C interface.

func I2CConfigDefault

func I2CConfigDefault() *I2CConfig

I2CConfigDefault returns the default configuration settings for an I²C interface.

type I2COption

type I2COption struct {
	BreakOnNACK  bool // do not continue reading/writing stream on slave NACK
	LastReadNACK bool // send NACK after last byte read from I²C slave
	NoUSBDelay   bool // pack all I²C data into the fewest number of USB packets
}

I2COption holds all of the dynamic configuration settings that can be changed while an I²C interface is open.

type I2CReg

type I2CReg struct {
	// contains filtered or unexported fields
}

I2CReg represents a read-write register of an I²C slave device.

func (*I2CReg) Reader

func (reg *I2CReg) Reader(size uint) (I2CRegReader, error)

Reader returns a closure that can be used to repeatedly read from a register. The size argument defines the number of bytes to read, i.e. the size of the data read from the register, often 2 (16-bit); it is also used along with the I2CReg byte order to format the value returned by the closure.

The returned closure accepts a single argument rewrite that if true will reposition the register pointer to the receiver's register address, which is necessary when reading/writing multiple registers; otherwise, false, reads will occur much faster without having to reposition every time. The byte order given to the Reg() constructor is used to format the value returned when calling the closure.

type I2CRegReader

type I2CRegReader func(rewrite bool) (uint64, error)

I2CRegReader represents a method for reading I²C slave device registers.

type Mask

type Mask struct {
	Index  string
	VID    string
	PID    string
	Serial string
	Desc   string
}

Mask contains strings for each of the supported attributes used to distinguish which FTDI device to open. See OpenMask for semantics.

type Mode

type Mode C.int

Type aliases for the native types needed by the C libraries.

const (
	ModeNone Mode = 0
	ModeSPI  Mode = 1
	ModeI2C  Mode = 2
)

Constants defining the legacy protocols supported by MPSSE.

func (Mode) String

func (m Mode) String() string

String returns a string describing the legacy protocol supported by MPSSE. Returns the string "unknown" if the mode is invalid.

type Pin

type Pin interface {
	IsMPSSE() bool     // true if DPin (port "D"), false if CPin (GPIO/port "C")
	Mask() uint8       // the bitmask used to address the pin, equal to 1<<Pos()
	Pos() uint         // the ordinal pin number (0-7), equal to log2(Mask())
	String() string    // the string representation "D#" or "C#", with # = Pos()
	Valid() bool       // true IFF bitmask has exactly one bit set
	Equals(q Pin) bool // true IFF p and q have equal port and bitmask
}

Pin defines the methods required for representing an FT232H port pin.

type SPI

type SPI struct {
	// contains filtered or unexported fields
}

SPI stores interface configuration settings for an SPI master and provides methods for reading and writing to SPI slave devices. The interface must be initialized by calling either Init or Config (not both) before use.

func (*SPI) Change

func (spi *SPI) Change(cs Pin) error

Change changes the currently configured CS pin. It can be called while the SPI interface is open without having to first close and reopen the device. The CS pin can be on either port, "D" or "C" (GPIO) pin, see the godoc on Write for details.

func (*SPI) Close

func (spi *SPI) Close() error

Close closes both the SPI interface and the connection to the FT232H device.

func (*SPI) Config

func (spi *SPI) Config(cfg *SPIConfig) error

Config initializes the SPI interface with the given configuration to a state ready for read/write. If the given configuration is nil, the default configuration is used (see SPIConfigDefault). It is not necessary to call Init after calling Config. See documentation of Init for other semantics.

func (*SPI) GetConfig

func (spi *SPI) GetConfig() *SPIConfig

SPIConfig returns the current configuration settings of the SPI receiver.

func (*SPI) Init

func (spi *SPI) Init() error

Init initializes the SPI interface to a state ready for read/write. If Config has not been called, the default configuration is used (see SPIConfigDefault). If the interface is already initialized, it is first closed before initializing the interface.

func (*SPI) Option

func (spi *SPI) Option(opt *SPIOption) error

Option changes the dynamic configuration parameters of the SPI interface. It can be called while the SPI interface is open without having to first close and reopen the device.

func (*SPI) Read

func (spi *SPI) Read(count uint, start bool, stop bool) ([]uint8, error)

Read reads the given count number of bytes from the SPI interface. There is no maximum length for the number of bytes to read. If start is true, the CS line is asserted before transfer. If stop is true, the CS line is de-asserted after transfer. Returns the slice of bytes successfully read and a non-nil error if there was an error.

func (*SPI) ReadFrom

func (spi *SPI) ReadFrom(cs Pin, count uint, start bool, stop bool) ([]uint8, error)

ReadFrom returns the result of Read after configuring the active CS line. If the given CS pin is not the same as the currently configured CS pin, the CS configuration is changed and persists after reading.

func (*SPI) String

func (spi *SPI) String() string

String returns a descriptive string of an SPI interface.

func (*SPI) Swap

func (spi *SPI) Swap(data []uint8, start bool, stop bool) ([]uint8, error)

Swap simultaneously reads and writes data on the SPI interface. Simultaneous read+write means that "one bit is clocked in and one bit is clocked out during every clock cycle." There is no maximum length for the number of bytes to swap. If start is true, the CS line is asserted before transfer. If stop is true, the CS line is de-asserted after transfer. Returns the slice of bytes successfully read and a non-nil error if there was an error.

func (*SPI) SwapWith

func (spi *SPI) SwapWith(cs Pin, data []uint8, start bool, stop bool) ([]uint8, error)

SwapWith returns the result of Swap after configuring the active CS line. If the given CS pin is not the same as the currently configured CS pin, the CS configuration is changed and persists after swapping.

func (*SPI) Write

func (spi *SPI) Write(data []uint8, start bool, stop bool) (uint, error)

Write writes the given byte slice data to the SPI interface. There is no maximum length for the data slice. If start is true, the CS line is asserted before transfer. If stop is true, the CS line is de-asserted after transfer. Returns the slice of bytes successfully written and a non-nil error if there was an error.

func (*SPI) WriteTo

func (spi *SPI) WriteTo(cs Pin, data []uint8, start bool, stop bool) (uint, error)

WriteTo returns the result of Write after configuring the active CS line. If the given CS pin is not the same as the currently configured CS pin, the CS configuration is changed and persists after writing.

type SPIConfig

type SPIConfig struct {
	*SPIOption
	Clock   uint32 // valid range: 0-30000000 (30 MHz)
	Latency byte   // 1-255 USB HiSpeed, 2-255 USB FullSpeed
}

SPIConfig holds all of the configuration settings for initializing an SPI interface.

func SPIConfigDefault

func SPIConfigDefault() *SPIConfig

SPIConfigDefault returns the default configuration settings for an SPI interface.

type SPIOption

type SPIOption struct {
	CS        Pin  // CS pin to assert when writing (can be DPin or CPin (GPIO))
	ActiveLow bool // CS asserted "active" by driving pin LOW or HIGH
	Mode      byte // SPI operating mode (mode 0 and 2 support only)
}

SPIOption holds all of the dynamic configuration settings that can be changed while an SPI interface is open.

The CS pin may be either a DPin or CPin (GPIO). If it is a DPin, then the MPSSE engine automatically handles CS assertion before and after transfer, depending on the given flags start and stop. If it is a CPin, then the GPIO pin is automatically set and cleared depending on the given flags start and stop. In both cases, the current value of the ActiveLow flag determines if the CS line driven LOW (ActiveLow true, DEFAULT) or HIGH (ActiveLow false) when asserting and then de-asserting.

type Status

type Status C.FT_STATUS

Type aliases for the native types needed by the C libraries.

const (
	SOK                      Status = C.FT_OK
	SInvalidHandle           Status = C.FT_INVALID_HANDLE
	SDeviceNotFound          Status = C.FT_DEVICE_NOT_FOUND
	SDeviceNotOpened         Status = C.FT_DEVICE_NOT_OPENED
	SIOError                 Status = C.FT_IO_ERROR
	SInsufficientResources   Status = C.FT_INSUFFICIENT_RESOURCES
	SInvalidParameter        Status = C.FT_INVALID_PARAMETER
	SInvalidBaudRate         Status = C.FT_INVALID_BAUD_RATE
	SDeviceNotOpenedForErase Status = C.FT_DEVICE_NOT_OPENED_FOR_ERASE
	SDeviceNotOpenedForWrite Status = C.FT_DEVICE_NOT_OPENED_FOR_WRITE
	SFailedToWriteDevice     Status = C.FT_FAILED_TO_WRITE_DEVICE
	SEEPROMReadFailed        Status = C.FT_EEPROM_READ_FAILED
	SEEPROMWriteFailed       Status = C.FT_EEPROM_WRITE_FAILED
	SEEPROMEraseFailed       Status = C.FT_EEPROM_ERASE_FAILED
	SEEPROMNotPresent        Status = C.FT_EEPROM_NOT_PRESENT
	SEEPROMNotProgrammed     Status = C.FT_EEPROM_NOT_PROGRAMMED
	SInvalidArgs             Status = C.FT_INVALID_ARGS
	SNotSupported            Status = C.FT_NOT_SUPPORTED
	SOtherError              Status = C.FT_OTHER_ERROR
	SDeviceListNotReady      Status = C.FT_DEVICE_LIST_NOT_READY
)

Constants related to device status

func (Status) Error

func (s Status) Error() string

Error implements the error interface. Returns the string "unknown error" if the status is invalid.

func (Status) OK

func (s Status) OK() bool

OK returns true if the status equals SOK, otherwise false.

Directories

Path Synopsis
drv
Peripheral device driver packages based on github.com/ardnew/ft232h.
Peripheral device driver packages based on github.com/ardnew/ft232h.
examples
Native FTDI drivers to service low-level USB communication.
Native FTDI drivers to service low-level USB communication.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL