2017-04-21 01:32:38 +08:00
|
|
|
package tinkerboard
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2017-05-06 21:12:10 +08:00
|
|
|
"sync"
|
2017-04-21 01:32:38 +08:00
|
|
|
|
|
|
|
multierror "github.com/hashicorp/go-multierror"
|
2023-11-16 03:51:52 +08:00
|
|
|
|
2023-05-20 20:25:21 +08:00
|
|
|
"gobot.io/x/gobot/v2"
|
|
|
|
"gobot.io/x/gobot/v2/platforms/adaptors"
|
|
|
|
"gobot.io/x/gobot/v2/system"
|
2017-04-21 01:32:38 +08:00
|
|
|
)
|
|
|
|
|
2022-05-01 22:12:39 +08:00
|
|
|
const (
|
2022-12-17 18:56:11 +08:00
|
|
|
defaultI2cBusNumber = 1
|
|
|
|
|
|
|
|
defaultSpiBusNumber = 0
|
|
|
|
defaultSpiChipNumber = 0
|
|
|
|
defaultSpiMode = 0
|
|
|
|
defaultSpiBitsNumber = 8
|
|
|
|
defaultSpiMaxSpeed = 500000
|
2022-05-01 22:12:39 +08:00
|
|
|
)
|
|
|
|
|
2023-11-27 23:42:42 +08:00
|
|
|
type cdevPin struct {
|
|
|
|
chip uint8
|
|
|
|
line uint8
|
|
|
|
}
|
|
|
|
|
|
|
|
type gpioPinDefinition struct {
|
|
|
|
sysfs int
|
|
|
|
cdev cdevPin
|
|
|
|
}
|
|
|
|
|
|
|
|
type analogPinDefinition struct {
|
|
|
|
path string
|
|
|
|
r bool // readable
|
|
|
|
w bool // writable
|
|
|
|
bufLen uint16
|
|
|
|
}
|
|
|
|
|
2022-05-01 22:12:39 +08:00
|
|
|
type pwmPinDefinition struct {
|
|
|
|
dir string
|
|
|
|
dirRegexp string
|
2023-11-27 23:42:42 +08:00
|
|
|
channel int
|
2017-04-22 21:03:07 +08:00
|
|
|
}
|
|
|
|
|
2017-04-21 01:51:31 +08:00
|
|
|
// Adaptor represents a Gobot Adaptor for the ASUS Tinker Board
|
2017-04-21 01:32:38 +08:00
|
|
|
type Adaptor struct {
|
2022-12-02 00:33:33 +08:00
|
|
|
name string
|
|
|
|
sys *system.Accesser
|
2023-12-06 03:26:28 +08:00
|
|
|
mutex *sync.Mutex
|
2023-11-27 23:42:42 +08:00
|
|
|
*adaptors.AnalogPinsAdaptor
|
2022-12-02 00:33:33 +08:00
|
|
|
*adaptors.DigitalPinsAdaptor
|
2022-12-06 01:28:57 +08:00
|
|
|
*adaptors.PWMPinsAdaptor
|
2022-12-09 02:29:51 +08:00
|
|
|
*adaptors.I2cBusAdaptor
|
2022-12-17 18:56:11 +08:00
|
|
|
*adaptors.SpiBusAdaptor
|
2017-04-21 01:32:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewAdaptor creates a Tinkerboard Adaptor
|
2023-01-06 02:04:32 +08:00
|
|
|
//
|
|
|
|
// Optional parameters:
|
2023-05-20 20:25:21 +08:00
|
|
|
//
|
2023-11-27 23:42:42 +08:00
|
|
|
// adaptors.WithGpiodAccess(): use character device gpiod driver instead of sysfs (still used by default)
|
|
|
|
// adaptors.WithSpiGpioAccess(sclk, nss, mosi, miso): use GPIO's instead of /dev/spidev#.#
|
|
|
|
// adaptors.WithGpiosActiveLow(pin's): invert the pin behavior
|
|
|
|
// adaptors.WithGpiosPullUp/Down(pin's): sets the internal pull resistor
|
2023-05-20 20:25:21 +08:00
|
|
|
//
|
2023-12-06 03:26:28 +08:00
|
|
|
// Optional parameters for PWM, see [adaptors.NewPWMPinsAdaptor]
|
|
|
|
//
|
2023-01-28 19:22:32 +08:00
|
|
|
// note from RK3288 datasheet: "The pull direction (pullup or pulldown) for all of GPIOs are software-programmable", but
|
|
|
|
// the latter is not working for any pin (armbian 22.08.7)
|
2023-12-06 03:26:28 +08:00
|
|
|
func NewAdaptor(opts ...interface{}) *Adaptor {
|
2023-01-06 02:04:32 +08:00
|
|
|
sys := system.NewAccesser(system.WithDigitalPinGpiodAccess())
|
2023-12-06 03:26:28 +08:00
|
|
|
a := &Adaptor{
|
|
|
|
name: gobot.DefaultName("Tinker Board"),
|
|
|
|
sys: sys,
|
|
|
|
mutex: &sync.Mutex{},
|
|
|
|
}
|
|
|
|
|
|
|
|
var digitalPinsOpts []func(adaptors.DigitalPinsOptioner)
|
2023-12-12 02:09:00 +08:00
|
|
|
var pwmPinsOpts []adaptors.PwmPinsOptionApplier
|
2023-12-06 03:26:28 +08:00
|
|
|
for _, opt := range opts {
|
|
|
|
switch o := opt.(type) {
|
|
|
|
case func(adaptors.DigitalPinsOptioner):
|
|
|
|
digitalPinsOpts = append(digitalPinsOpts, o)
|
|
|
|
case adaptors.PwmPinsOptionApplier:
|
|
|
|
pwmPinsOpts = append(pwmPinsOpts, o)
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("'%s' can not be applied on adaptor '%s'", opt, a.name))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
a.AnalogPinsAdaptor = adaptors.NewAnalogPinsAdaptor(sys, a.translateAnalogPin)
|
|
|
|
a.DigitalPinsAdaptor = adaptors.NewDigitalPinsAdaptor(sys, a.translateDigitalPin, digitalPinsOpts...)
|
|
|
|
a.PWMPinsAdaptor = adaptors.NewPWMPinsAdaptor(sys, a.translatePWMPin, pwmPinsOpts...)
|
|
|
|
a.I2cBusAdaptor = adaptors.NewI2cBusAdaptor(sys, a.validateI2cBusNumber, defaultI2cBusNumber)
|
|
|
|
a.SpiBusAdaptor = adaptors.NewSpiBusAdaptor(sys, a.validateSpiBusNumber, defaultSpiBusNumber, defaultSpiChipNumber,
|
2022-12-17 18:56:11 +08:00
|
|
|
defaultSpiMode, defaultSpiBitsNumber, defaultSpiMaxSpeed)
|
2023-12-06 03:26:28 +08:00
|
|
|
return a
|
2017-04-21 01:32:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the name of the Adaptor
|
2023-12-06 03:26:28 +08:00
|
|
|
func (a *Adaptor) Name() string { return a.name }
|
2017-04-21 01:32:38 +08:00
|
|
|
|
|
|
|
// SetName sets the name of the Adaptor
|
2023-12-06 03:26:28 +08:00
|
|
|
func (a *Adaptor) SetName(n string) { a.name = n }
|
2017-04-21 01:32:38 +08:00
|
|
|
|
2022-12-02 00:33:33 +08:00
|
|
|
// Connect create new connection to board and pins.
|
2023-12-06 03:26:28 +08:00
|
|
|
func (a *Adaptor) Connect() error {
|
|
|
|
a.mutex.Lock()
|
|
|
|
defer a.mutex.Unlock()
|
2022-12-02 00:33:33 +08:00
|
|
|
|
2023-12-06 03:26:28 +08:00
|
|
|
if err := a.SpiBusAdaptor.Connect(); err != nil {
|
2022-12-17 18:56:11 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-12-06 03:26:28 +08:00
|
|
|
if err := a.I2cBusAdaptor.Connect(); err != nil {
|
2022-12-09 02:29:51 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-12-06 03:26:28 +08:00
|
|
|
if err := a.AnalogPinsAdaptor.Connect(); err != nil {
|
2023-11-27 23:42:42 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-12-06 03:26:28 +08:00
|
|
|
if err := a.PWMPinsAdaptor.Connect(); err != nil {
|
2022-12-06 01:28:57 +08:00
|
|
|
return err
|
|
|
|
}
|
2023-12-06 03:26:28 +08:00
|
|
|
return a.DigitalPinsAdaptor.Connect()
|
2022-12-02 00:33:33 +08:00
|
|
|
}
|
2017-04-21 01:32:38 +08:00
|
|
|
|
2022-12-06 01:28:57 +08:00
|
|
|
// Finalize closes connection to board, pins and bus
|
2023-12-06 03:26:28 +08:00
|
|
|
func (a *Adaptor) Finalize() error {
|
|
|
|
a.mutex.Lock()
|
|
|
|
defer a.mutex.Unlock()
|
2017-05-06 21:12:10 +08:00
|
|
|
|
2023-12-06 03:26:28 +08:00
|
|
|
err := a.DigitalPinsAdaptor.Finalize()
|
2022-12-02 00:33:33 +08:00
|
|
|
|
2023-12-06 03:26:28 +08:00
|
|
|
if e := a.PWMPinsAdaptor.Finalize(); e != nil {
|
2022-12-06 01:28:57 +08:00
|
|
|
err = multierror.Append(err, e)
|
2017-04-22 21:03:07 +08:00
|
|
|
}
|
2022-12-06 01:28:57 +08:00
|
|
|
|
2023-12-06 03:26:28 +08:00
|
|
|
if e := a.AnalogPinsAdaptor.Finalize(); e != nil {
|
2023-11-27 23:42:42 +08:00
|
|
|
err = multierror.Append(err, e)
|
|
|
|
}
|
|
|
|
|
2023-12-06 03:26:28 +08:00
|
|
|
if e := a.I2cBusAdaptor.Finalize(); e != nil {
|
2022-12-09 02:29:51 +08:00
|
|
|
err = multierror.Append(err, e)
|
2017-04-21 01:32:38 +08:00
|
|
|
}
|
2022-12-09 02:29:51 +08:00
|
|
|
|
2023-12-06 03:26:28 +08:00
|
|
|
if e := a.SpiBusAdaptor.Finalize(); e != nil {
|
2022-12-17 18:56:11 +08:00
|
|
|
err = multierror.Append(err, e)
|
|
|
|
}
|
2022-12-02 00:33:33 +08:00
|
|
|
return err
|
2017-04-22 21:03:07 +08:00
|
|
|
}
|
|
|
|
|
2023-12-06 03:26:28 +08:00
|
|
|
func (a *Adaptor) validateSpiBusNumber(busNr int) error {
|
2022-12-17 18:56:11 +08:00
|
|
|
// Valid bus numbers are [0,2] which corresponds to /dev/spidev0.x, /dev/spidev2.x
|
|
|
|
// x is the chip number <255
|
|
|
|
if (busNr != 0) && (busNr != 2) {
|
|
|
|
return fmt.Errorf("Bus number %d out of range", busNr)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-06 03:26:28 +08:00
|
|
|
func (a *Adaptor) validateI2cBusNumber(busNr int) error {
|
2022-12-09 02:29:51 +08:00
|
|
|
// Valid bus number is [0..4] which corresponds to /dev/i2c-0 through /dev/i2c-4.
|
|
|
|
// We don't support "/dev/i2c-6 DesignWare HDMI".
|
|
|
|
if (busNr < 0) || (busNr > 4) {
|
|
|
|
return fmt.Errorf("Bus number %d out of range", busNr)
|
2022-05-01 22:12:39 +08:00
|
|
|
}
|
2022-12-09 02:29:51 +08:00
|
|
|
return nil
|
2022-05-01 22:12:39 +08:00
|
|
|
}
|
|
|
|
|
2023-12-06 03:26:28 +08:00
|
|
|
func (a *Adaptor) translateAnalogPin(id string) (string, bool, bool, uint16, error) {
|
2023-11-27 23:42:42 +08:00
|
|
|
pinInfo, ok := analogPinDefinitions[id]
|
|
|
|
if !ok {
|
|
|
|
return "", false, false, 0, fmt.Errorf("'%s' is not a valid id for a analog pin", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
path := pinInfo.path
|
2023-12-06 03:26:28 +08:00
|
|
|
info, err := a.sys.Stat(path)
|
2023-11-27 23:42:42 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", false, false, 0, fmt.Errorf("Error (%v) on access '%s'", err, path)
|
|
|
|
}
|
|
|
|
if info.IsDir() {
|
|
|
|
return "", false, false, 0, fmt.Errorf("The item '%s' is a directory, which is not expected", path)
|
|
|
|
}
|
|
|
|
|
|
|
|
return path, pinInfo.r, pinInfo.w, pinInfo.bufLen, nil
|
|
|
|
}
|
|
|
|
|
2023-12-06 03:26:28 +08:00
|
|
|
func (a *Adaptor) translateDigitalPin(id string) (string, int, error) {
|
2022-12-02 00:33:33 +08:00
|
|
|
pindef, ok := gpioPinDefinitions[id]
|
2022-05-01 22:12:39 +08:00
|
|
|
if !ok {
|
2022-12-02 00:33:33 +08:00
|
|
|
return "", -1, fmt.Errorf("'%s' is not a valid id for a digital pin", id)
|
2022-05-01 22:12:39 +08:00
|
|
|
}
|
2023-12-06 03:26:28 +08:00
|
|
|
if a.sys.IsSysfsDigitalPinAccess() {
|
2022-11-27 23:06:09 +08:00
|
|
|
return "", pindef.sysfs, nil
|
|
|
|
}
|
2022-12-02 00:33:33 +08:00
|
|
|
chip := fmt.Sprintf("gpiochip%d", pindef.cdev.chip)
|
|
|
|
line := int(pindef.cdev.line)
|
2022-11-27 23:06:09 +08:00
|
|
|
return chip, line, nil
|
2017-04-29 16:26:38 +08:00
|
|
|
}
|
|
|
|
|
2023-12-06 03:26:28 +08:00
|
|
|
func (a *Adaptor) translatePWMPin(id string) (string, int, error) {
|
2022-12-06 01:28:57 +08:00
|
|
|
pinInfo, ok := pwmPinDefinitions[id]
|
|
|
|
if !ok {
|
|
|
|
return "", -1, fmt.Errorf("'%s' is not a valid id for a PWM pin", id)
|
2017-04-29 16:26:38 +08:00
|
|
|
}
|
2023-12-06 03:26:28 +08:00
|
|
|
path, err := pinInfo.findPWMDir(a.sys)
|
2022-12-06 01:28:57 +08:00
|
|
|
if err != nil {
|
|
|
|
return "", -1, err
|
|
|
|
}
|
|
|
|
return path, pinInfo.channel, nil
|
2017-04-29 16:26:38 +08:00
|
|
|
}
|
|
|
|
|
2023-11-16 03:51:52 +08:00
|
|
|
func (p pwmPinDefinition) findPWMDir(sys *system.Accesser) (string, error) {
|
2022-11-21 02:22:26 +08:00
|
|
|
items, _ := sys.Find(p.dir, p.dirRegexp)
|
2023-05-19 20:16:22 +08:00
|
|
|
if len(items) == 0 {
|
2022-12-09 02:29:51 +08:00
|
|
|
return "", fmt.Errorf("No path found for PWM directory pattern, '%s' in path '%s'. See README.md for activation",
|
|
|
|
p.dirRegexp, p.dir)
|
2017-04-29 16:26:38 +08:00
|
|
|
}
|
2022-05-01 22:12:39 +08:00
|
|
|
|
2023-11-16 03:51:52 +08:00
|
|
|
dir := items[0]
|
2022-11-21 02:22:26 +08:00
|
|
|
info, err := sys.Stat(dir)
|
2022-05-01 22:12:39 +08:00
|
|
|
if err != nil {
|
2022-11-05 14:42:28 +08:00
|
|
|
return "", fmt.Errorf("Error (%v) on access '%s'", err, dir)
|
2022-05-01 22:12:39 +08:00
|
|
|
}
|
|
|
|
if !info.IsDir() {
|
2022-11-05 14:42:28 +08:00
|
|
|
return "", fmt.Errorf("The item '%s' is not a directory, which is not expected", dir)
|
2022-05-01 22:12:39 +08:00
|
|
|
}
|
|
|
|
|
2023-11-16 03:51:52 +08:00
|
|
|
return dir, nil
|
2017-04-29 16:26:38 +08:00
|
|
|
}
|