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 ¶
- Constants
- func BlessFlag()
- type AddrSpace
- type ByteOrder
- type CPin
- type Chip
- type DPin
- type Dir
- type FT232H
- func New() (*FT232H, error)
- func OpenDesc(desc string) (*FT232H, error)
- func OpenFlag(arg []string, fatal bool) (*FT232H, error)
- func OpenIndex(index int) (*FT232H, error)
- func OpenMask(mask *Mask) (*FT232H, error)
- func OpenSerial(serial string) (*FT232H, error)
- func OpenVIDPID(vid uint16, pid uint16) (*FT232H, error)
- type Flag
- type GPIO
- func (gpio *GPIO) Chdir(pin CPin, dir Dir) error
- func (gpio *GPIO) Config(cfg *GPIOConfig) error
- func (gpio *GPIO) ConfigPin(pin CPin, dir Dir, val bool) error
- func (gpio *GPIO) Get(pin CPin) (bool, error)
- func (gpio *GPIO) Init() error
- func (gpio *GPIO) Read() (uint8, error)
- func (gpio *GPIO) Set(pin CPin, val bool) error
- func (gpio *GPIO) String() string
- func (gpio *GPIO) Write(val uint8) error
- type GPIOConfig
- type Handle
- type I2C
- func (i2c *I2C) Close() error
- func (i2c *I2C) Config(cfg *I2CConfig) error
- func (i2c *I2C) GetConfig() *I2CConfig
- func (i2c *I2C) Init() error
- func (i2c *I2C) Option(opt *I2COption) error
- func (i2c *I2C) Read(slave uint, count uint, start bool, stop bool) ([]uint8, error)
- func (i2c *I2C) Reg(slave uint, addr uint, space AddrSpace, order ByteOrder) *I2CReg
- func (i2c *I2C) String() string
- func (i2c *I2C) Write(slave uint, data []uint8, start bool, stop bool) (uint, error)
- type I2CClockRate
- type I2CConfig
- type I2COption
- type I2CReg
- type I2CRegReader
- type Mask
- type Mode
- type Pin
- type SPI
- func (spi *SPI) Change(cs Pin) error
- func (spi *SPI) Close() error
- func (spi *SPI) Config(cfg *SPIConfig) error
- func (spi *SPI) GetConfig() *SPIConfig
- func (spi *SPI) Init() error
- func (spi *SPI) Option(opt *SPIOption) error
- func (spi *SPI) Read(count uint, start bool, stop bool) ([]uint8, error)
- func (spi *SPI) ReadFrom(cs Pin, count uint, start bool, stop bool) ([]uint8, error)
- func (spi *SPI) String() string
- func (spi *SPI) Swap(data []uint8, start bool, stop bool) ([]uint8, error)
- func (spi *SPI) SwapWith(cs Pin, data []uint8, start bool, stop bool) ([]uint8, error)
- func (spi *SPI) Write(data []uint8, start bool, stop bool) (uint, error)
- func (spi *SPI) WriteTo(cs Pin, data []uint8, start bool, stop bool) (uint, error)
- type SPIConfig
- type SPIOption
- type Status
Examples ¶
Constants ¶
const ( I2CSlaveAddressMin = 0x08 I2CSlaveAddressMax = 0x77 )
Constants defining legal 7-bit I²C slave addresses
const ( I2CClockMaximum I2CClockRate = I2CClockHighSpeedMode I2CClockDefault I2CClockRate = I2CClockFastMode I2CLatencyDefault byte = 2 )
Constants related to I²C interface initialization.
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
const ( SPIClockMaximum uint32 = 30000000 SPIClockDefault uint32 = SPIClockMaximum SPILatencyDefault byte = 2 )
Constants related to SPI interface initialization.
Variables ¶
This section is empty.
Functions ¶
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.
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 ¶
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) Uint ¶
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 ¶
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 ¶
Equals is true if the given pin is on port "C" and has the same bitmask, otherwise false.
type Chip ¶
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.
type DPin ¶
type DPin uint8 // pin bitmask on MPSSE low-byte lines (port "D" of FT232H)
Types representing individual port pins.
func D ¶
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 ¶
Equals is true if the given pin is on port "D" and has the same bitmask, otherwise false.
type FT232H ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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.
type Flag ¶
Flag contains the attributes used to distinguish which FT232H device to open from a command-line-style string slice.
func NewFlag ¶
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 ¶
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.
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 ¶
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 ¶
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) Init ¶
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 ¶
Read returns the current value of all GPIO pins, returning 0 and a non-nil error if unsuccessful.
type GPIOConfig ¶
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 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) Config ¶
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) Init ¶
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 ¶
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 ¶
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 ¶
Reg constructs a new I2CReg for conveniently reading and writing data in I²C slave device registers.
func (*I2C) Write ¶
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 ¶
I2CRegReader represents a method for reading I²C slave device registers.
type Mask ¶
Mask contains strings for each of the supported attributes used to distinguish which FTDI device to open. See OpenMask for semantics.
type Mode ¶
Type aliases for the native types needed by the C libraries.
Constants defining the legacy protocols supported by MPSSE.
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 ¶
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) Config ¶
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) Init ¶
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 ¶
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 ¶
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 ¶
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) Swap ¶
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 ¶
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 ¶
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.
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 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
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
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. |