ch347

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2023 License: MIT Imports: 4 Imported by: 7

README

CH347 HIDAPI Go Package

Go package to access UART and SPI+I2C+GPIO interfaces of the High-speed USB converter chip CH347 in HIDAPI mode (Mode 2).

https://github.com/wuxx/USB-HS-Bridge/

Check documentation on pkg.go.dev or provided examples for usage.

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

Constants

This section is empty.

Variables

View Source
var (
	ErrI2CRead  = errors.New("i2c read failed")
	ErrI2CWrite = errors.New("i2c write failed")
)
View Source
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 I2CMode

type I2CMode uint8
const (
	I2CMode0 I2CMode = iota // Low rate 20KHz.
	I2CMode1                // Standart rate 100KHz.
	I2CMode2                // Fast rate 400KHz.
	I2CMode3                // High rate 750KHz.
)

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

func (c *IO) I2C(addr uint16, w, r []byte) error

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

func (c *IO) ReadPin(pin Pin) (bool, error)

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) SPI

func (c *IO) SPI(w, r []byte) error

SPI performs write and read operations.

func (*IO) SetCS

func (c *IO) SetCS(enable bool) error

SetCS asserts CS0 pin.

func (*IO) SetCS1

func (c *IO) SetCS1(enable bool) error

SetCS1 asserts CS1 pin.

func (*IO) SetI2C

func (c *IO) SetI2C(mode I2CMode) error

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.

func (*IO) WritePin

func (c *IO) WritePin(pin Pin, output bool, level bool) error

WritePin sets given pin operation mode.

Example:

// Blink ACT led (GPIO4).
st := false
for {
	err := WritePin(GPIO4, true, st)

	if err != nil {
		fmt.Println(err)
		break;
	}

	if st {
		st = false
	} else {
		st = true
	}

	time.Sleep(100*time.Miliseconds)
}

type Pin

type Pin uint8

Pin represents available pins for GPIO operations.

const (
	// CTS0/SCK/TCK
	GPIO0 Pin = iota

	// RTS0/MSIO/TDO
	GPIO1

	// DSR0/SCS0/TMS
	GPIO2

	// SCL
	GPIO3

	// ACT
	GPIO4

	// DTR0/TNOW0/SCS1/TRST
	GPIO5

	// CTS1
	GPIO6

	// RTS1
	GPIO7
)

type SPIByteOrder

type SPIByteOrder uint8
const (
	SPIByteOrderMSB SPIByteOrder = iota
	SPIByteOrderLSB
)

type SPIClock

type SPIClock uint8
const (
	SPIClock0 SPIClock = iota // 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
)

type SPIMode

type SPIMode uint8
const (
	SPIMode0 SPIMode = iota
	SPIMode1
	SPIMode2
	SPIMode3
)

type UART

type UART struct {
	Dev HIDDev
}

UART implements ReadWriter interface to access CH347 UART.

Pass first hidraw device.

func (*UART) Read

func (c *UART) Read(b []byte) (int, error)

Read implementes reader interface.

func (*UART) Set

func (c *UART) Set(baudRate uint32, dataBits UARTDataBits, parity UARTParity, stop UARTStopBit) error

func (*UART) Write

func (c *UART) Write(b []byte) (int, error)

Write implementes writer interface.

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
uart-echo Module
uart-loopback Module

Jump to

Keyboard shortcuts

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