Documentation
¶
Overview ¶
Package ch347 provides methods to access UART and SPI+I2C+GPIO interfaces of the High-speed USB converter chip CH347 in HIDAPI mode (Mode 2).
The ch347 package was built by examining USB packets of the official demonstration library.
github.com/sstallion/go-hid can be used as HIDAPI interface.
Index ¶
- Variables
- type HIDDev
- type I2CMode
- type IO
- func (c *IO) I2C(addr uint16, w, r []byte) error
- func (c *IO) ReadPin(pin Pin) (bool, error)
- func (c *IO) SPI(w, r []byte) error
- func (c *IO) SetCS(enable bool) error
- func (c *IO) SetCS1(enable bool) error
- func (c *IO) SetI2C(mode I2CMode) error
- func (c *IO) SetSPI(mode SPIMode, clock SPIClock, byteOrder SPIByteOrder) error
- func (c *IO) WritePin(pin Pin, output bool, level bool) error
- type Pin
- type SPIByteOrder
- type SPIClock
- type SPIMode
- type UART
- type UARTDataBits
- type UARTParity
- type UARTStopBit
Constants ¶
This section is empty.
Variables ¶
var ( ErrI2CRead = errors.New("i2c read failed") ErrI2CWrite = errors.New("i2c write failed") )
var (
ErrInvalidResponse = errors.New("invalid response")
)
Functions ¶
This section is empty.
Types ¶
type HIDDev ¶
type HIDDev interface { io.ReadWriter SendFeatureReport(p []byte) (int, error) }
Note: ¶
It's advised to handle read timeouts and "Interrupted system call" errors. Otherwise, operations might error "invalid response" once an interrupt has occurred or block indefinitely.
Example with the Read method override for github.com/sstallion/go-hid:
type HIDWithTimeout struct { *hid.Device } // Read overridden with ReadWithTimeout and with "Interrupted system call" error handling. func (d *HIDWithTimeout) Read(p []byte) (n int, err error) { for { n, err = d.Device.ReadWithTimeout(p, 1*time.Second) if err == nil || err.Error() != "Interrupted system call" { return } } } func main() { dev, _ := hid.OpenPath("/dev/hidraw6") c = &ch347.IO{Dev: &HIDWithTimeout{dev}} }
type IO ¶
type IO struct { Dev HIDDev // contains filtered or unexported fields }
IO implements methods to access CH347 SPI+I2C+GPIO.
Pass second hidraw device to Dev.
func (*IO) I2C ¶
I2C performs write and read operations with device on given address.
Example:
// Read all 4096 bytes from 24C32B chip w := []byte{0x00, 0x00} // "Random read". Write page address first. r := make([]byte, 4096) // Allocate buffer for 4096 bytes to be read. err = c.I2C(0x57, w, r) if err != nil { return err } // Print result as a string fmt.Println(string(r))
func (*IO) ReadPin ¶
ReadPin returns given pin level.
For output pin "true" means there is +3.3V on this pin.
For input pin "true" means this pin is shorted to GND.
func (*IO) SetI2C ¶
SetI2C configures the interface with a specified mode.
- I2CMode0 - Low rate 20KHz.
- I2CMode1 - Standart rate 100KHz.
- I2CMode2 - Fast rate 400KHz.
- I2CMode3 - High rate 750KHz.
func (*IO) SetSPI ¶
func (c *IO) SetSPI(mode SPIMode, clock SPIClock, byteOrder SPIByteOrder) error
SetSPI configures the interface with a specified mode, clock, and byte order.
- SPIClock0 - 60 MHz.
- SPIClock1 - 30 MHz.
- SPIClock2 - 15 MHz.
- SPIClock3 - 7.5 MHz.
- SPIClock4 - 3.75 Mhz.
- SPIClock5 - 1.875 MHz.
- SPIClock6 - 937.5 KHz.
- SPIClock7 - 468.75 KHz.
Note: ¶
If you want to initialize both I2C and SPI, then I2C should be initialized first.
type SPIByteOrder ¶
type SPIByteOrder uint8
const ( SPIByteOrderMSB SPIByteOrder = iota SPIByteOrderLSB )
type UART ¶
type UART struct {
Dev HIDDev
}
UART implements ReadWriter interface to access CH347 UART.
Pass first hidraw device.
func (*UART) Set ¶
func (c *UART) Set(baudRate uint32, dataBits UARTDataBits, parity UARTParity, stop UARTStopBit) error
type UARTDataBits ¶
type UARTDataBits uint8
const ( UARTDataBits5 UARTDataBits = iota UARTDataBits6 UARTDataBits7 UARTDataBits8 UARTDataBits16 )
type UARTParity ¶
type UARTParity uint8
const ( UARTParityNone UARTParity = iota UARTParityOdd UARTParityEven UARTParityMark UARTParitySpace )
type UARTStopBit ¶
type UARTStopBit uint8
const ( UARTStopBitOne UARTStopBit = iota UARTStopBitOneHalf UartStopBitTwo )
Directories
¶
Path | Synopsis |
---|---|
examples
|
|
i2c-aht2x
Module
|
|
spi-flash
Module
|
|
spi-ssd1306-bad-apple
Module
|
|
uart-echo
Module
|
|
uart-loopback
Module
|
|
uart-pzem-004t
Module
|