2017-02-23 18:12:17 +08:00
|
|
|
package dragonboard
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2017-05-06 21:11:31 +08:00
|
|
|
"sync"
|
2017-02-23 18:12:17 +08:00
|
|
|
|
|
|
|
multierror "github.com/hashicorp/go-multierror"
|
|
|
|
"gobot.io/x/gobot"
|
|
|
|
"gobot.io/x/gobot/drivers/i2c"
|
|
|
|
"gobot.io/x/gobot/sysfs"
|
|
|
|
)
|
|
|
|
|
2017-02-23 23:54:13 +08:00
|
|
|
// Adaptor represents a Gobot Adaptor for a DragonBoard 410c
|
2017-02-23 18:12:17 +08:00
|
|
|
type Adaptor struct {
|
|
|
|
name string
|
2022-11-05 14:42:28 +08:00
|
|
|
sysfs *sysfs.Accesser
|
|
|
|
mutex *sync.Mutex
|
2017-04-28 18:52:22 +08:00
|
|
|
digitalPins map[int]*sysfs.DigitalPin
|
2017-02-23 18:12:17 +08:00
|
|
|
pinMap map[string]int
|
2017-07-12 23:00:35 +08:00
|
|
|
i2cBuses [3]i2c.I2cDevice
|
2017-02-23 18:12:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var fixedPins = map[string]int{
|
|
|
|
"GPIO_A": 36,
|
|
|
|
"GPIO_B": 12,
|
|
|
|
"GPIO_C": 13,
|
|
|
|
"GPIO_D": 69,
|
|
|
|
"GPIO_E": 115,
|
|
|
|
"GPIO_F": 507,
|
|
|
|
"GPIO_G": 24,
|
|
|
|
"GPIO_H": 25,
|
|
|
|
"GPIO_I": 35,
|
|
|
|
"GPIO_J": 34,
|
|
|
|
"GPIO_K": 28,
|
|
|
|
"GPIO_L": 33,
|
|
|
|
|
|
|
|
"LED_1": 21,
|
|
|
|
"LED_2": 120,
|
|
|
|
}
|
|
|
|
|
2017-02-23 23:54:13 +08:00
|
|
|
// NewAdaptor creates a DragonBoard 410c Adaptor
|
2017-02-23 18:12:17 +08:00
|
|
|
func NewAdaptor() *Adaptor {
|
|
|
|
c := &Adaptor{
|
2017-05-06 21:11:31 +08:00
|
|
|
name: gobot.DefaultName("DragonBoard"),
|
2022-11-05 14:42:28 +08:00
|
|
|
sysfs: sysfs.NewAccesser(),
|
2017-05-06 21:11:31 +08:00
|
|
|
mutex: &sync.Mutex{},
|
2017-02-23 18:12:17 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
c.setPins()
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the name of the Adaptor
|
|
|
|
func (c *Adaptor) Name() string { return c.name }
|
|
|
|
|
|
|
|
// SetName sets the name of the Adaptor
|
|
|
|
func (c *Adaptor) SetName(n string) { c.name = n }
|
|
|
|
|
|
|
|
// Connect initializes the board
|
|
|
|
func (c *Adaptor) Connect() (err error) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finalize closes connection to board and pins
|
|
|
|
func (c *Adaptor) Finalize() (err error) {
|
2017-05-06 21:11:31 +08:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
2017-02-23 18:12:17 +08:00
|
|
|
for _, pin := range c.digitalPins {
|
|
|
|
if pin != nil {
|
|
|
|
if e := pin.Unexport(); e != nil {
|
|
|
|
err = multierror.Append(err, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, bus := range c.i2cBuses {
|
|
|
|
if bus != nil {
|
|
|
|
if e := bus.Close(); e != nil {
|
|
|
|
err = multierror.Append(err, e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-04-28 18:52:22 +08:00
|
|
|
// DigitalPin returns matched digitalPin for specified values
|
|
|
|
func (c *Adaptor) DigitalPin(pin string, dir string) (sysfsPin *sysfs.DigitalPin, err error) {
|
2017-05-06 21:11:31 +08:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
2017-02-23 18:12:17 +08:00
|
|
|
i, err := c.translatePin(pin)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.digitalPins[i] == nil {
|
2022-11-05 14:42:28 +08:00
|
|
|
c.digitalPins[i] = c.sysfs.NewDigitalPin(i)
|
2017-02-23 18:12:17 +08:00
|
|
|
if err = c.digitalPins[i].Export(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = c.digitalPins[i].Direction(dir); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.digitalPins[i], nil
|
|
|
|
}
|
|
|
|
|
2017-02-23 23:54:13 +08:00
|
|
|
// DigitalRead reads digital value to the specified pin.
|
|
|
|
// Valids pins are the GPIO_A through GPIO_L pins from the
|
|
|
|
// extender (pins 23-34 on header J8), as well as the SoC pins
|
|
|
|
// aka all the other pins, APQ GPIO_0-GPIO_122 and PM_MPP_0-4.
|
2017-02-23 18:12:17 +08:00
|
|
|
func (c *Adaptor) DigitalRead(pin string) (val int, err error) {
|
2017-04-28 18:52:22 +08:00
|
|
|
sysfsPin, err := c.DigitalPin(pin, sysfs.IN)
|
2017-02-23 18:12:17 +08:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return sysfsPin.Read()
|
|
|
|
}
|
|
|
|
|
|
|
|
// DigitalWrite writes digital value to the specified pin.
|
2017-02-23 23:54:13 +08:00
|
|
|
// Valids pins are the GPIO_A through GPIO_L pins from the
|
|
|
|
// extender (pins 23-34 on header J8), as well as the SoC pins
|
|
|
|
// aka all the other pins, APQ GPIO_0-GPIO_122 and PM_MPP_0-4.
|
2017-02-23 18:12:17 +08:00
|
|
|
func (c *Adaptor) DigitalWrite(pin string, val byte) (err error) {
|
2017-04-28 18:52:22 +08:00
|
|
|
sysfsPin, err := c.DigitalPin(pin, sysfs.OUT)
|
2017-02-23 18:12:17 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return sysfsPin.Write(int(val))
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetConnection returns a connection to a device on a specified bus.
|
2017-02-23 23:54:13 +08:00
|
|
|
// Valid bus number is [0..1] which corresponds to /dev/i2c-0 through /dev/i2c-1.
|
2017-02-23 18:12:17 +08:00
|
|
|
func (c *Adaptor) GetConnection(address int, bus int) (connection i2c.Connection, err error) {
|
2017-05-06 21:11:31 +08:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
|
|
|
|
2017-02-23 23:54:13 +08:00
|
|
|
if (bus < 0) || (bus > 1) {
|
2017-02-23 18:12:17 +08:00
|
|
|
return nil, fmt.Errorf("Bus number %d out of range", bus)
|
|
|
|
}
|
|
|
|
if c.i2cBuses[bus] == nil {
|
2022-11-05 14:42:28 +08:00
|
|
|
c.i2cBuses[bus], err = c.sysfs.NewI2cDevice(fmt.Sprintf("/dev/i2c-%d", bus))
|
2017-02-23 18:12:17 +08:00
|
|
|
}
|
|
|
|
return i2c.NewConnection(c.i2cBuses[bus], address), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDefaultBus returns the default i2c bus for this platform
|
|
|
|
func (c *Adaptor) GetDefaultBus() int {
|
|
|
|
return 0
|
|
|
|
}
|
2017-04-29 18:43:55 +08:00
|
|
|
|
|
|
|
func (c *Adaptor) setPins() {
|
|
|
|
c.digitalPins = make(map[int]*sysfs.DigitalPin)
|
|
|
|
c.pinMap = fixedPins
|
|
|
|
for i := 0; i < 122; i++ {
|
|
|
|
pin := fmt.Sprintf("GPIO_%d", i)
|
|
|
|
c.pinMap[pin] = i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Adaptor) translatePin(pin string) (i int, err error) {
|
|
|
|
if val, ok := c.pinMap[pin]; ok {
|
|
|
|
i = val
|
|
|
|
} else {
|
|
|
|
err = errors.New("Not a valid pin")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|