mirror of
https://github.com/kidoman/embd
synced 2024-12-21 12:20:05 +01:00
host specific drivers can now be loaded separately
this ensures cleaner abstractions/code and will ensure that the produced binary is as small as possible. a convenience package is provided to easily load all hosts easily: "github.com/kidoman/embd/host/all"
This commit is contained in:
parent
57328c979d
commit
c35deeb17c
507
bbb.go
507
bbb.go
@ -1,507 +0,0 @@
|
||||
// BeagleBone Black support.
|
||||
// The following features are supported on Linux kernel 3.8+
|
||||
//
|
||||
// GPIO (digital (rw), analog (ro), pwm)
|
||||
// I2C
|
||||
// LED
|
||||
|
||||
package embd
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/golang/glog"
|
||||
"github.com/kidoman/embd/util"
|
||||
)
|
||||
|
||||
var bbbPins = PinMap{
|
||||
&PinDesc{ID: "P8_07", Aliases: []string{"66", "GPIO_66", "Caps: TIMER4"}, Caps: CapDigital | CapGPMC, DigitalLogical: 66},
|
||||
&PinDesc{ID: "P8_08", Aliases: []string{"67", "GPIO_67", "TIMER7"}, Caps: CapDigital | CapGPMC, DigitalLogical: 67},
|
||||
&PinDesc{ID: "P8_09", Aliases: []string{"69", "GPIO_69", "TIMER5"}, Caps: CapDigital | CapGPMC, DigitalLogical: 69},
|
||||
&PinDesc{ID: "P8_10", Aliases: []string{"68", "GPIO_68", "TIMER6"}, Caps: CapDigital | CapGPMC, DigitalLogical: 68},
|
||||
&PinDesc{ID: "P8_11", Aliases: []string{"45", "GPIO_45"}, Caps: CapDigital | CapGPMC, DigitalLogical: 45},
|
||||
&PinDesc{ID: "P8_12", Aliases: []string{"44", "GPIO_44"}, Caps: CapDigital | CapGPMC, DigitalLogical: 44},
|
||||
&PinDesc{ID: "P8_13", Aliases: []string{"23", "GPIO_23", "EHRPWM2B"}, Caps: CapDigital | CapGPMC, DigitalLogical: 23},
|
||||
&PinDesc{ID: "P8_14", Aliases: []string{"26", "GPIO_26"}, Caps: CapDigital | CapGPMC, DigitalLogical: 26},
|
||||
&PinDesc{ID: "P8_15", Aliases: []string{"47", "GPIO_47"}, Caps: CapDigital | CapGPMC, DigitalLogical: 47},
|
||||
&PinDesc{ID: "P8_16", Aliases: []string{"46", "GPIO_46"}, Caps: CapDigital | CapGPMC, DigitalLogical: 46},
|
||||
&PinDesc{ID: "P8_17", Aliases: []string{"27", "GPIO_27"}, Caps: CapDigital | CapGPMC, DigitalLogical: 27},
|
||||
&PinDesc{ID: "P8_18", Aliases: []string{"65", "GPIO_65"}, Caps: CapDigital | CapGPMC, DigitalLogical: 65},
|
||||
&PinDesc{ID: "P8_19", Aliases: []string{"22", "GPIO_22", "EHRPWM2A"}, Caps: CapDigital | CapGPMC, DigitalLogical: 22},
|
||||
&PinDesc{ID: "P8_26", Aliases: []string{"61", "GPIO_61"}, Caps: CapDigital | CapGPMC, DigitalLogical: 61},
|
||||
&PinDesc{ID: "P8_27", Aliases: []string{"86", "GPIO_86"}, Caps: CapDigital | CapLCD, DigitalLogical: 86},
|
||||
&PinDesc{ID: "P8_28", Aliases: []string{"88", "GPIO_88"}, Caps: CapDigital | CapLCD, DigitalLogical: 88},
|
||||
&PinDesc{ID: "P8_29", Aliases: []string{"87", "GPIO_87"}, Caps: CapDigital | CapLCD, DigitalLogical: 87},
|
||||
&PinDesc{ID: "P8_30", Aliases: []string{"89", "GPIO_89"}, Caps: CapDigital | CapLCD, DigitalLogical: 89},
|
||||
&PinDesc{ID: "P8_31", Aliases: []string{"10", "GPIO_10", "UART5_CTSN"}, Caps: CapDigital | CapLCD, DigitalLogical: 10},
|
||||
&PinDesc{ID: "P8_32", Aliases: []string{"11", "GPIO_11", "UART5_RTSN"}, Caps: CapDigital | CapLCD, DigitalLogical: 11},
|
||||
&PinDesc{ID: "P8_33", Aliases: []string{"9", "GPIO_9 ", "UART4_RTSN"}, Caps: CapDigital | CapLCD, DigitalLogical: 9},
|
||||
&PinDesc{ID: "P8_34", Aliases: []string{"81", "GPIO_81", "UART3_RTSN"}, Caps: CapDigital | CapLCD, DigitalLogical: 81},
|
||||
&PinDesc{ID: "P8_35", Aliases: []string{"8", "GPIO_8 ", "UART4_CTSN"}, Caps: CapDigital | CapLCD, DigitalLogical: 8},
|
||||
&PinDesc{ID: "P8_36", Aliases: []string{"80", "GPIO_80", "UART3_CTSN"}, Caps: CapDigital | CapLCD, DigitalLogical: 80},
|
||||
&PinDesc{ID: "P8_37", Aliases: []string{"78", "GPIO_78", "UART5_TXD"}, Caps: CapDigital | CapLCD, DigitalLogical: 78},
|
||||
&PinDesc{ID: "P8_38", Aliases: []string{"79", "GPIO_79", "UART5_RXD"}, Caps: CapDigital | CapLCD, DigitalLogical: 79},
|
||||
&PinDesc{ID: "P8_39", Aliases: []string{"76", "GPIO_76"}, Caps: CapDigital | CapLCD, DigitalLogical: 76},
|
||||
&PinDesc{ID: "P8_40", Aliases: []string{"77", "GPIO_77"}, Caps: CapDigital | CapLCD, DigitalLogical: 77},
|
||||
&PinDesc{ID: "P8_41", Aliases: []string{"74", "GPIO_74"}, Caps: CapDigital | CapLCD, DigitalLogical: 74},
|
||||
&PinDesc{ID: "P8_42", Aliases: []string{"75", "GPIO_75"}, Caps: CapDigital | CapLCD, DigitalLogical: 75},
|
||||
&PinDesc{ID: "P8_43", Aliases: []string{"72", "GPIO_72"}, Caps: CapDigital | CapLCD, DigitalLogical: 72},
|
||||
&PinDesc{ID: "P8_44", Aliases: []string{"73", "GPIO_73"}, Caps: CapDigital | CapLCD, DigitalLogical: 73},
|
||||
&PinDesc{ID: "P8_45", Aliases: []string{"70", "GPIO_70"}, Caps: CapDigital | CapLCD, DigitalLogical: 70},
|
||||
&PinDesc{ID: "P8_46", Aliases: []string{"71", "GPIO_71"}, Caps: CapDigital | CapLCD, DigitalLogical: 71},
|
||||
|
||||
&PinDesc{ID: "P9_11", Aliases: []string{"30", "GPIO_30", "UART4_RXD"}, Caps: CapDigital | CapUART, DigitalLogical: 30},
|
||||
&PinDesc{ID: "P9_12", Aliases: []string{"60", "GPIO_60", "GPIO1_28"}, Caps: CapDigital, DigitalLogical: 60},
|
||||
&PinDesc{ID: "P9_13", Aliases: []string{"31", "GPIO_31", "UART4_TXD"}, Caps: CapDigital | CapUART, DigitalLogical: 31},
|
||||
&PinDesc{ID: "P9_14", Aliases: []string{"50", "GPIO_50", "EHRPWM1A"}, Caps: CapDigital | CapPWM, DigitalLogical: 50},
|
||||
&PinDesc{ID: "P9_15", Aliases: []string{"48", "GPIO_48", "GPIO1_16"}, Caps: CapDigital, DigitalLogical: 48},
|
||||
&PinDesc{ID: "P9_16", Aliases: []string{"51", "GPIO_51", "EHRPWM1B"}, Caps: CapDigital | CapPWM, DigitalLogical: 51},
|
||||
&PinDesc{ID: "P9_17", Aliases: []string{"5", "GPIO_5", "I2C1_SCL"}, Caps: CapDigital | CapI2C, DigitalLogical: 5},
|
||||
&PinDesc{ID: "P9_18", Aliases: []string{"4", "GPIO_4", "I2C1_SDA"}, Caps: CapDigital | CapI2C, DigitalLogical: 4},
|
||||
&PinDesc{ID: "P9_19", Aliases: []string{"13", "GPIO_13", "I2C2_SCL"}, Caps: CapDigital | CapI2C, DigitalLogical: 13},
|
||||
&PinDesc{ID: "P9_20", Aliases: []string{"12", "GPIO_12", "I2C2_SDA"}, Caps: CapDigital | CapI2C, DigitalLogical: 12},
|
||||
&PinDesc{ID: "P9_21", Aliases: []string{"3", "GPIO_3", "UART2_TXD"}, Caps: CapDigital | CapUART, DigitalLogical: 3},
|
||||
&PinDesc{ID: "P9_22", Aliases: []string{"2", "GPIO_2", "UART2_RXD"}, Caps: CapDigital | CapUART, DigitalLogical: 2},
|
||||
&PinDesc{ID: "P9_23", Aliases: []string{"49", "GPIO_49", "GPIO1_17"}, Caps: CapDigital, DigitalLogical: 49},
|
||||
&PinDesc{ID: "P9_24", Aliases: []string{"15", "GPIO_15", "UART1_TXD"}, Caps: CapDigital | CapUART, DigitalLogical: 15},
|
||||
&PinDesc{ID: "P9_25", Aliases: []string{"117", "GPIO_117", "GPIO3_21"}, Caps: CapDigital, DigitalLogical: 117},
|
||||
&PinDesc{ID: "P9_26", Aliases: []string{"14", "GPIO_14", "UART1_RXD"}, Caps: CapDigital | CapUART, DigitalLogical: 14},
|
||||
&PinDesc{ID: "P9_27", Aliases: []string{"115", "GPIO_115", "GPIO3_19"}, Caps: CapDigital, DigitalLogical: 115},
|
||||
&PinDesc{ID: "P9_28", Aliases: []string{"113", "GPIO_113", "SPI1_CS0"}, Caps: CapDigital | CapSPI, DigitalLogical: 113},
|
||||
&PinDesc{ID: "P9_29", Aliases: []string{"111", "GPIO_111", "SPI1_D0"}, Caps: CapDigital | CapSPI, DigitalLogical: 111},
|
||||
&PinDesc{ID: "P9_30", Aliases: []string{"112", "GPIO_112", "SPI1_D1"}, Caps: CapDigital | CapSPI, DigitalLogical: 112},
|
||||
&PinDesc{ID: "P9_31", Aliases: []string{"110", "GPIO_110", "SPI1_SCLK"}, Caps: CapDigital | CapSPI, DigitalLogical: 110},
|
||||
&PinDesc{ID: "P9_32", Aliases: []string{"VADC"}},
|
||||
&PinDesc{ID: "P9_33", Aliases: []string{"4", "AIN4"}, Caps: CapAnalog, AnalogLogical: 4},
|
||||
&PinDesc{ID: "P9_34", Aliases: []string{"AGND"}},
|
||||
&PinDesc{ID: "P9_35", Aliases: []string{"6", "AIN6"}, Caps: CapAnalog, AnalogLogical: 6},
|
||||
&PinDesc{ID: "P9_36", Aliases: []string{"5", "AIN5"}, Caps: CapAnalog, AnalogLogical: 5},
|
||||
&PinDesc{ID: "P9_37", Aliases: []string{"2", "AIN2"}, Caps: CapAnalog, AnalogLogical: 2},
|
||||
&PinDesc{ID: "P9_38", Aliases: []string{"3", "AIN3"}, Caps: CapAnalog, AnalogLogical: 3},
|
||||
&PinDesc{ID: "P9_39", Aliases: []string{"0", "AIN0"}, Caps: CapAnalog, AnalogLogical: 0},
|
||||
&PinDesc{ID: "P9_40", Aliases: []string{"1", "AIN1"}, Caps: CapAnalog, AnalogLogical: 1},
|
||||
}
|
||||
|
||||
var bbbLEDMap = LEDMap{
|
||||
"beaglebone:green:usr0": []string{"0", "USR0", "usr0"},
|
||||
"beaglebone:green:usr1": []string{"1", "USR1", "usr1"},
|
||||
"beaglebone:green:usr2": []string{"2", "USR2", "usr2"},
|
||||
"beaglebone:green:usr3": []string{"3", "USR3", "usr3"},
|
||||
}
|
||||
|
||||
func bbbEnsureFeatureEnabled(id string) error {
|
||||
pattern := "/sys/devices/bone_capemgr.*/slots"
|
||||
file, err := findFirstMatchingFile(pattern)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
bytes, err := ioutil.ReadFile(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
str := string(bytes)
|
||||
if strings.Contains(str, id) {
|
||||
return nil
|
||||
}
|
||||
slots, err := os.OpenFile(file, os.O_WRONLY, os.ModeExclusive)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer slots.Close()
|
||||
_, err = slots.WriteString(id)
|
||||
return err
|
||||
}
|
||||
|
||||
// This cannot be currently used to disable things like the
|
||||
// analog and pwm modules. Removing them from slots file can
|
||||
// potentially cause a kernel panic and unsettle things. So the
|
||||
// recommended thing to do is to simply reboot.
|
||||
func bbbEnsureFeatureDisabled(id string) error {
|
||||
pattern := "/sys/devices/bone_capemgr.*/slots"
|
||||
file, err := findFirstMatchingFile(pattern)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
slots, err := os.OpenFile(file, os.O_RDWR, os.ModeExclusive)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer slots.Close()
|
||||
scanner := bufio.NewScanner(slots)
|
||||
for scanner.Scan() {
|
||||
text := scanner.Text()
|
||||
if !strings.Contains(text, id) {
|
||||
continue
|
||||
}
|
||||
// Extract the id from the line
|
||||
idx := strings.Index(text, ":")
|
||||
if idx < 0 {
|
||||
// Something is off, bail
|
||||
continue
|
||||
}
|
||||
dis := strings.TrimSpace(text[:idx])
|
||||
slots.Seek(0, 0)
|
||||
_, err = slots.WriteString("-" + dis)
|
||||
return err
|
||||
}
|
||||
// Could not disable the feature
|
||||
return fmt.Errorf("embd: could not disable feature %q", id)
|
||||
}
|
||||
|
||||
type bbbAnalogPin struct {
|
||||
id string
|
||||
n int
|
||||
|
||||
drv GPIODriver
|
||||
|
||||
val *os.File
|
||||
|
||||
initialized bool
|
||||
}
|
||||
|
||||
func newBBBAnalogPin(pd *PinDesc, drv GPIODriver) AnalogPin {
|
||||
return &bbbAnalogPin{id: pd.ID, n: pd.AnalogLogical, drv: drv}
|
||||
}
|
||||
|
||||
func (p *bbbAnalogPin) N() int {
|
||||
return p.n
|
||||
}
|
||||
|
||||
func (p *bbbAnalogPin) init() error {
|
||||
if p.initialized {
|
||||
return nil
|
||||
}
|
||||
|
||||
var err error
|
||||
if err = p.ensureEnabled(); err != nil {
|
||||
return err
|
||||
}
|
||||
if p.val, err = p.valueFile(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p.initialized = true
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *bbbAnalogPin) ensureEnabled() error {
|
||||
return bbbEnsureFeatureEnabled("cape-bone-iio")
|
||||
}
|
||||
|
||||
func (p *bbbAnalogPin) valueFilePath() (string, error) {
|
||||
pattern := fmt.Sprintf("/sys/devices/ocp.*/helper.*/AIN%v", p.n)
|
||||
return findFirstMatchingFile(pattern)
|
||||
}
|
||||
|
||||
func (p *bbbAnalogPin) openFile(path string) (*os.File, error) {
|
||||
return os.OpenFile(path, os.O_RDONLY, os.ModeExclusive)
|
||||
}
|
||||
|
||||
func (p *bbbAnalogPin) valueFile() (*os.File, error) {
|
||||
path, err := p.valueFilePath()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return p.openFile(path)
|
||||
}
|
||||
|
||||
func (p *bbbAnalogPin) Read() (int, error) {
|
||||
if err := p.init(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
p.val.Seek(0, 0)
|
||||
bytes, err := ioutil.ReadAll(p.val)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
str := string(bytes)
|
||||
str = strings.TrimSpace(str)
|
||||
return strconv.Atoi(str)
|
||||
}
|
||||
|
||||
func (p *bbbAnalogPin) Close() error {
|
||||
if err := p.drv.Unregister(p.id); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !p.initialized {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := p.val.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p.initialized = false
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
const (
|
||||
// BBBPWMDefaultPolarity represents the default polarity (Positve or 1) for pwm.
|
||||
BBBPWMDefaultPolarity = Positive
|
||||
|
||||
// BBBPWMDefaultDuty represents the default duty (0ns) for pwm.
|
||||
BBBPWMDefaultDuty = 0
|
||||
|
||||
// BBBPWMDefaultPeriod represents the default period (500000ns) for pwm. Equals 2000 Hz.
|
||||
BBBPWMDefaultPeriod = 500000
|
||||
|
||||
// BBBPWMMaxPulseWidth represents the max period (1000000000ns) supported by pwm. Equals 1 Hz.
|
||||
BBBPWMMaxPulseWidth = 1000000000
|
||||
)
|
||||
|
||||
type bbbPWMPin struct {
|
||||
n string
|
||||
|
||||
drv GPIODriver
|
||||
|
||||
period int
|
||||
polarity Polarity
|
||||
|
||||
dutyf *os.File
|
||||
periodf *os.File
|
||||
polarityf *os.File
|
||||
|
||||
initialized bool
|
||||
}
|
||||
|
||||
func newBBBPWMPin(pd *PinDesc, drv GPIODriver) PWMPin {
|
||||
return &bbbPWMPin{n: pd.ID, drv: drv}
|
||||
}
|
||||
|
||||
func (p *bbbPWMPin) N() string {
|
||||
return p.n
|
||||
}
|
||||
|
||||
func (p *bbbPWMPin) id() string {
|
||||
return "bone_pwm_" + p.n
|
||||
}
|
||||
|
||||
func (p *bbbPWMPin) init() error {
|
||||
if p.initialized {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := p.ensurePWMEnabled(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := p.ensurePinEnabled(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
basePath, err := p.basePath()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := p.ensurePeriodFileExists(basePath, 500*time.Millisecond); err != nil {
|
||||
return err
|
||||
}
|
||||
if p.periodf, err = p.periodFile(basePath); err != nil {
|
||||
return err
|
||||
}
|
||||
if p.dutyf, err = p.dutyFile(basePath); err != nil {
|
||||
return err
|
||||
}
|
||||
if p.polarityf, err = p.polarityFile(basePath); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p.initialized = true
|
||||
|
||||
if err := p.reset(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *bbbPWMPin) ensurePWMEnabled() error {
|
||||
return bbbEnsureFeatureEnabled("am33xx_pwm")
|
||||
}
|
||||
|
||||
func (p *bbbPWMPin) ensurePinEnabled() error {
|
||||
return bbbEnsureFeatureEnabled(p.id())
|
||||
}
|
||||
|
||||
func (p *bbbPWMPin) ensurePinDisabled() error {
|
||||
return bbbEnsureFeatureDisabled(p.id())
|
||||
}
|
||||
|
||||
func (p *bbbPWMPin) basePath() (string, error) {
|
||||
pattern := "/sys/devices/ocp.*/pwm_test_" + p.n + ".*"
|
||||
return findFirstMatchingFile(pattern)
|
||||
}
|
||||
|
||||
func (p *bbbPWMPin) openFile(path string) (*os.File, error) {
|
||||
return os.OpenFile(path, os.O_WRONLY, os.ModeExclusive)
|
||||
}
|
||||
|
||||
func (p *bbbPWMPin) ensurePeriodFileExists(basePath string, d time.Duration) error {
|
||||
path := p.periodFilePath(basePath)
|
||||
timeout := time.After(d)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-timeout:
|
||||
return errors.New("embd: period file not found before timeout")
|
||||
default:
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// We are looping, wait a bit.
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *bbbPWMPin) periodFilePath(basePath string) string {
|
||||
return path.Join(basePath, "period")
|
||||
}
|
||||
|
||||
func (p *bbbPWMPin) periodFile(basePath string) (*os.File, error) {
|
||||
return p.openFile(p.periodFilePath(basePath))
|
||||
}
|
||||
|
||||
func (p *bbbPWMPin) dutyFile(basePath string) (*os.File, error) {
|
||||
return p.openFile(path.Join(basePath, "duty"))
|
||||
}
|
||||
|
||||
func (p *bbbPWMPin) polarityFile(basePath string) (*os.File, error) {
|
||||
return p.openFile(path.Join(basePath, "polarity"))
|
||||
}
|
||||
|
||||
func (p *bbbPWMPin) SetPeriod(ns int) error {
|
||||
if err := p.init(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if ns > BBBPWMMaxPulseWidth {
|
||||
return fmt.Errorf("embd: pwm period for %v is out of bounds (must be =< %vns)", p.n, BBBPWMMaxPulseWidth)
|
||||
}
|
||||
|
||||
_, err := p.periodf.WriteString(strconv.Itoa(ns))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p.period = ns
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *bbbPWMPin) SetDuty(ns int) error {
|
||||
if err := p.init(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if ns > BBBPWMMaxPulseWidth {
|
||||
return fmt.Errorf("embd: pwm duty %v for pin %v is out of bounds (must be =< %vns)", p.n, BBBPWMMaxPulseWidth)
|
||||
}
|
||||
|
||||
if ns > p.period {
|
||||
return fmt.Errorf("embd: pwm duty %v for pin %v is greater than the period %v", ns, p.n, p.period)
|
||||
}
|
||||
|
||||
_, err := p.dutyf.WriteString(strconv.Itoa(ns))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *bbbPWMPin) SetMicroseconds(us int) error {
|
||||
if err := p.init(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if p.period != 20000000 {
|
||||
glog.Warningf("embd: pwm pin %v has freq %v hz. recommended 50 hz for servo mode", 1000000000/p.period)
|
||||
}
|
||||
duty := us * 1000 // in nanoseconds
|
||||
if duty > p.period {
|
||||
return fmt.Errorf("embd: calculated pwm duty %vns for pin %v (servo mode) is greater than the period %vns", duty, p.n, p.period)
|
||||
}
|
||||
return p.SetDuty(duty)
|
||||
}
|
||||
|
||||
func (p *bbbPWMPin) SetAnalog(value byte) error {
|
||||
duty := util.Map(int64(value), 0, 255, 0, int64(p.period))
|
||||
return p.SetDuty(int(duty))
|
||||
}
|
||||
|
||||
func (p *bbbPWMPin) SetPolarity(pol Polarity) error {
|
||||
if err := p.init(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err := p.polarityf.WriteString(strconv.Itoa(int(pol)))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p.polarity = pol
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *bbbPWMPin) reset() error {
|
||||
if err := p.SetPolarity(Positive); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := p.SetDuty(BBBPWMDefaultDuty); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := p.SetPeriod(BBBPWMDefaultPeriod); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *bbbPWMPin) Close() error {
|
||||
if err := p.drv.Unregister(p.n); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !p.initialized {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := p.reset(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := p.ensurePinDisabled(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p.initialized = false
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
Register(HostBBB, func(rev int) *Descriptor {
|
||||
return &Descriptor{
|
||||
GPIODriver: func() GPIODriver {
|
||||
return newGPIODriver(bbbPins, newDigitalPin, newBBBAnalogPin, newBBBPWMPin)
|
||||
},
|
||||
I2CDriver: newI2CDriver,
|
||||
LEDDriver: func() LEDDriver {
|
||||
return newLEDDriver(bbbLEDMap)
|
||||
},
|
||||
}
|
||||
})
|
||||
}
|
41
bbb_test.go
41
bbb_test.go
@ -1,41 +0,0 @@
|
||||
package embd
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestBBBAnalogPinClose(t *testing.T) {
|
||||
pinMap := PinMap{
|
||||
&PinDesc{ID: "P1_1", Aliases: []string{"1"}, Caps: CapAnalog},
|
||||
}
|
||||
driver := newGPIODriver(pinMap, nil, newBBBAnalogPin, nil)
|
||||
pin, err := driver.AnalogPin(1)
|
||||
if err != nil {
|
||||
t.Fatalf("Looking up analog pin 1: got %v", err)
|
||||
}
|
||||
pin.Close()
|
||||
pin2, err := driver.AnalogPin(1)
|
||||
if err != nil {
|
||||
t.Fatalf("Looking up analog pin 1: got %v", err)
|
||||
}
|
||||
if pin == pin2 {
|
||||
t.Fatal("Looking up closed analog pin 1: but got the old instance")
|
||||
}
|
||||
}
|
||||
|
||||
func TestBBBPWMPinClose(t *testing.T) {
|
||||
pinMap := PinMap{
|
||||
&PinDesc{ID: "P1_1", Aliases: []string{"1"}, Caps: CapPWM},
|
||||
}
|
||||
driver := newGPIODriver(pinMap, nil, nil, newBBBPWMPin)
|
||||
pin, err := driver.PWMPin(1)
|
||||
if err != nil {
|
||||
t.Fatalf("Looking up pwm pin 1: got %v", err)
|
||||
}
|
||||
pin.Close()
|
||||
pin2, err := driver.PWMPin(1)
|
||||
if err != nil {
|
||||
t.Fatalf("Looking up pwm pin 1: got %v", err)
|
||||
}
|
||||
if pin == pin2 {
|
||||
t.Fatal("Looking up closed pwm pin 1: but got the old instance")
|
||||
}
|
||||
}
|
@ -25,7 +25,9 @@ type gpioDriver struct {
|
||||
initializedPins map[string]pin
|
||||
}
|
||||
|
||||
func newGPIODriver(pinMap PinMap, dpf digitalPinFactory, apf analogPinFactory, ppf pwmPinFactory) GPIODriver {
|
||||
// NewGPIODriver returns a GPIODriver interface which allows control
|
||||
// over the GPIO subsystem.
|
||||
func NewGPIODriver(pinMap PinMap, dpf digitalPinFactory, apf analogPinFactory, ppf pwmPinFactory) GPIODriver {
|
||||
return &gpioDriver{
|
||||
pinMap: pinMap,
|
||||
dpf: dpf,
|
||||
|
@ -62,7 +62,7 @@ func TestGpioDriverDigitalPin(t *testing.T) {
|
||||
pinMap := PinMap{
|
||||
&PinDesc{ID: "P1_1", Aliases: []string{"1"}, Caps: CapDigital, DigitalLogical: 1},
|
||||
}
|
||||
driver := newGPIODriver(pinMap, newFakeDigitalPin, nil, nil)
|
||||
driver := NewGPIODriver(pinMap, newFakeDigitalPin, nil, nil)
|
||||
for _, test := range tests {
|
||||
pin, err := driver.DigitalPin(test.key)
|
||||
if err != nil {
|
||||
@ -112,7 +112,7 @@ func TestGpioDriverAnalogPin(t *testing.T) {
|
||||
pinMap := PinMap{
|
||||
&PinDesc{ID: "P1_1", Aliases: []string{"1"}, Caps: CapAnalog, AnalogLogical: 1},
|
||||
}
|
||||
driver := newGPIODriver(pinMap, nil, newFakeAnalogPin, nil)
|
||||
driver := NewGPIODriver(pinMap, nil, newFakeAnalogPin, nil)
|
||||
for _, test := range tests {
|
||||
pin, err := driver.AnalogPin(test.key)
|
||||
if err != nil {
|
||||
@ -130,7 +130,7 @@ func TestGpioDriverUnavailablePinType(t *testing.T) {
|
||||
&PinDesc{ID: "P1_1", Aliases: []string{"1"}, Caps: CapDigital, DigitalLogical: 1},
|
||||
&PinDesc{ID: "P1_2", Aliases: []string{"1"}, Caps: CapAnalog, AnalogLogical: 1},
|
||||
}
|
||||
driver := newGPIODriver(pinMap, nil, nil, nil)
|
||||
driver := NewGPIODriver(pinMap, nil, nil, nil)
|
||||
_, err := driver.DigitalPin(1)
|
||||
if err == nil {
|
||||
t.Fatal("Looking up digital pin 1: did not get error")
|
||||
@ -153,7 +153,7 @@ func TestGpioPinCaching(t *testing.T) {
|
||||
pinMap := PinMap{
|
||||
&PinDesc{ID: "P1_1", Aliases: []string{"1"}, Caps: CapDigital},
|
||||
}
|
||||
driver := newGPIODriver(pinMap, newFakeDigitalPin, nil, nil)
|
||||
driver := NewGPIODriver(pinMap, newFakeDigitalPin, nil, nil)
|
||||
pin, err := driver.DigitalPin(1)
|
||||
if err != nil {
|
||||
t.Fatalf("Looking up digital pin 1: got %v", err)
|
||||
|
7
host/all/all.go
Normal file
7
host/all/all.go
Normal file
@ -0,0 +1,7 @@
|
||||
// Package all conviniently loads all the inbuilt/supported host drivers.
|
||||
package all
|
||||
|
||||
import (
|
||||
_ "github.com/kidoman/embd/host/bbb"
|
||||
_ "github.com/kidoman/embd/host/rpi"
|
||||
)
|
104
host/bbb/analogpin.go
Normal file
104
host/bbb/analogpin.go
Normal file
@ -0,0 +1,104 @@
|
||||
// Analog I/O support on the BBB.
|
||||
|
||||
package bbb
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/kidoman/embd"
|
||||
)
|
||||
|
||||
type analogPin struct {
|
||||
id string
|
||||
n int
|
||||
|
||||
drv embd.GPIODriver
|
||||
|
||||
val *os.File
|
||||
|
||||
initialized bool
|
||||
}
|
||||
|
||||
func newAnalogPin(pd *embd.PinDesc, drv embd.GPIODriver) embd.AnalogPin {
|
||||
return &analogPin{id: pd.ID, n: pd.AnalogLogical, drv: drv}
|
||||
}
|
||||
|
||||
func (p *analogPin) N() int {
|
||||
return p.n
|
||||
}
|
||||
|
||||
func (p *analogPin) init() error {
|
||||
if p.initialized {
|
||||
return nil
|
||||
}
|
||||
|
||||
var err error
|
||||
if err = p.ensureEnabled(); err != nil {
|
||||
return err
|
||||
}
|
||||
if p.val, err = p.valueFile(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p.initialized = true
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *analogPin) ensureEnabled() error {
|
||||
return ensureFeatureEnabled("cape-bone-iio")
|
||||
}
|
||||
|
||||
func (p *analogPin) valueFilePath() (string, error) {
|
||||
pattern := fmt.Sprintf("/sys/devices/ocp.*/helper.*/AIN%v", p.n)
|
||||
return embd.FindFirstMatchingFile(pattern)
|
||||
}
|
||||
|
||||
func (p *analogPin) openFile(path string) (*os.File, error) {
|
||||
return os.OpenFile(path, os.O_RDONLY, os.ModeExclusive)
|
||||
}
|
||||
|
||||
func (p *analogPin) valueFile() (*os.File, error) {
|
||||
path, err := p.valueFilePath()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return p.openFile(path)
|
||||
}
|
||||
|
||||
func (p *analogPin) Read() (int, error) {
|
||||
if err := p.init(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
p.val.Seek(0, 0)
|
||||
bytes, err := ioutil.ReadAll(p.val)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
str := string(bytes)
|
||||
str = strings.TrimSpace(str)
|
||||
return strconv.Atoi(str)
|
||||
}
|
||||
|
||||
func (p *analogPin) Close() error {
|
||||
if err := p.drv.Unregister(p.id); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !p.initialized {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := p.val.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p.initialized = false
|
||||
|
||||
return nil
|
||||
}
|
26
host/bbb/analogpin_test.go
Normal file
26
host/bbb/analogpin_test.go
Normal file
@ -0,0 +1,26 @@
|
||||
package bbb
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/kidoman/embd"
|
||||
)
|
||||
|
||||
func TestAnalogPinClose(t *testing.T) {
|
||||
pinMap := embd.PinMap{
|
||||
&embd.PinDesc{ID: "P1_1", Aliases: []string{"1"}, Caps: embd.CapAnalog},
|
||||
}
|
||||
driver := embd.NewGPIODriver(pinMap, nil, newAnalogPin, nil)
|
||||
pin, err := driver.AnalogPin(1)
|
||||
if err != nil {
|
||||
t.Fatalf("Looking up analog pin 1: got %v", err)
|
||||
}
|
||||
pin.Close()
|
||||
pin2, err := driver.AnalogPin(1)
|
||||
if err != nil {
|
||||
t.Fatalf("Looking up analog pin 1: got %v", err)
|
||||
}
|
||||
if pin == pin2 {
|
||||
t.Fatal("Looking up closed analog pin 1: but got the old instance")
|
||||
}
|
||||
}
|
170
host/bbb/bbb.go
Normal file
170
host/bbb/bbb.go
Normal file
@ -0,0 +1,170 @@
|
||||
/*
|
||||
Package bbb provides BeagleBone Black support.
|
||||
The following features are supported on Linux kernel 3.8+
|
||||
|
||||
GPIO (digital (rw), analog (ro), pwm)
|
||||
I²C
|
||||
LED
|
||||
*/
|
||||
package bbb
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/kidoman/embd"
|
||||
"github.com/kidoman/embd/host/generic"
|
||||
)
|
||||
|
||||
var pins = embd.PinMap{
|
||||
&embd.PinDesc{ID: "P8_07", Aliases: []string{"66", "GPIO_66", "Caps: TIMER4"}, Caps: embd.CapDigital | embd.CapGPMC, DigitalLogical: 66},
|
||||
&embd.PinDesc{ID: "P8_08", Aliases: []string{"67", "GPIO_67", "TIMER7"}, Caps: embd.CapDigital | embd.CapGPMC, DigitalLogical: 67},
|
||||
&embd.PinDesc{ID: "P8_09", Aliases: []string{"69", "GPIO_69", "TIMER5"}, Caps: embd.CapDigital | embd.CapGPMC, DigitalLogical: 69},
|
||||
&embd.PinDesc{ID: "P8_10", Aliases: []string{"68", "GPIO_68", "TIMER6"}, Caps: embd.CapDigital | embd.CapGPMC, DigitalLogical: 68},
|
||||
&embd.PinDesc{ID: "P8_11", Aliases: []string{"45", "GPIO_45"}, Caps: embd.CapDigital | embd.CapGPMC, DigitalLogical: 45},
|
||||
&embd.PinDesc{ID: "P8_12", Aliases: []string{"44", "GPIO_44"}, Caps: embd.CapDigital | embd.CapGPMC, DigitalLogical: 44},
|
||||
&embd.PinDesc{ID: "P8_13", Aliases: []string{"23", "GPIO_23", "EHRPWM2B"}, Caps: embd.CapDigital | embd.CapGPMC, DigitalLogical: 23},
|
||||
&embd.PinDesc{ID: "P8_14", Aliases: []string{"26", "GPIO_26"}, Caps: embd.CapDigital | embd.CapGPMC, DigitalLogical: 26},
|
||||
&embd.PinDesc{ID: "P8_15", Aliases: []string{"47", "GPIO_47"}, Caps: embd.CapDigital | embd.CapGPMC, DigitalLogical: 47},
|
||||
&embd.PinDesc{ID: "P8_16", Aliases: []string{"46", "GPIO_46"}, Caps: embd.CapDigital | embd.CapGPMC, DigitalLogical: 46},
|
||||
&embd.PinDesc{ID: "P8_17", Aliases: []string{"27", "GPIO_27"}, Caps: embd.CapDigital | embd.CapGPMC, DigitalLogical: 27},
|
||||
&embd.PinDesc{ID: "P8_18", Aliases: []string{"65", "GPIO_65"}, Caps: embd.CapDigital | embd.CapGPMC, DigitalLogical: 65},
|
||||
&embd.PinDesc{ID: "P8_19", Aliases: []string{"22", "GPIO_22", "EHRPWM2A"}, Caps: embd.CapDigital | embd.CapGPMC, DigitalLogical: 22},
|
||||
&embd.PinDesc{ID: "P8_26", Aliases: []string{"61", "GPIO_61"}, Caps: embd.CapDigital | embd.CapGPMC, DigitalLogical: 61},
|
||||
&embd.PinDesc{ID: "P8_27", Aliases: []string{"86", "GPIO_86"}, Caps: embd.CapDigital | embd.CapLCD, DigitalLogical: 86},
|
||||
&embd.PinDesc{ID: "P8_28", Aliases: []string{"88", "GPIO_88"}, Caps: embd.CapDigital | embd.CapLCD, DigitalLogical: 88},
|
||||
&embd.PinDesc{ID: "P8_29", Aliases: []string{"87", "GPIO_87"}, Caps: embd.CapDigital | embd.CapLCD, DigitalLogical: 87},
|
||||
&embd.PinDesc{ID: "P8_30", Aliases: []string{"89", "GPIO_89"}, Caps: embd.CapDigital | embd.CapLCD, DigitalLogical: 89},
|
||||
&embd.PinDesc{ID: "P8_31", Aliases: []string{"10", "GPIO_10", "UART5_CTSN"}, Caps: embd.CapDigital | embd.CapLCD, DigitalLogical: 10},
|
||||
&embd.PinDesc{ID: "P8_32", Aliases: []string{"11", "GPIO_11", "UART5_RTSN"}, Caps: embd.CapDigital | embd.CapLCD, DigitalLogical: 11},
|
||||
&embd.PinDesc{ID: "P8_33", Aliases: []string{"9", "GPIO_9 ", "UART4_RTSN"}, Caps: embd.CapDigital | embd.CapLCD, DigitalLogical: 9},
|
||||
&embd.PinDesc{ID: "P8_34", Aliases: []string{"81", "GPIO_81", "UART3_RTSN"}, Caps: embd.CapDigital | embd.CapLCD, DigitalLogical: 81},
|
||||
&embd.PinDesc{ID: "P8_35", Aliases: []string{"8", "GPIO_8 ", "UART4_CTSN"}, Caps: embd.CapDigital | embd.CapLCD, DigitalLogical: 8},
|
||||
&embd.PinDesc{ID: "P8_36", Aliases: []string{"80", "GPIO_80", "UART3_CTSN"}, Caps: embd.CapDigital | embd.CapLCD, DigitalLogical: 80},
|
||||
&embd.PinDesc{ID: "P8_37", Aliases: []string{"78", "GPIO_78", "UART5_TXD"}, Caps: embd.CapDigital | embd.CapLCD, DigitalLogical: 78},
|
||||
&embd.PinDesc{ID: "P8_38", Aliases: []string{"79", "GPIO_79", "UART5_RXD"}, Caps: embd.CapDigital | embd.CapLCD, DigitalLogical: 79},
|
||||
&embd.PinDesc{ID: "P8_39", Aliases: []string{"76", "GPIO_76"}, Caps: embd.CapDigital | embd.CapLCD, DigitalLogical: 76},
|
||||
&embd.PinDesc{ID: "P8_40", Aliases: []string{"77", "GPIO_77"}, Caps: embd.CapDigital | embd.CapLCD, DigitalLogical: 77},
|
||||
&embd.PinDesc{ID: "P8_41", Aliases: []string{"74", "GPIO_74"}, Caps: embd.CapDigital | embd.CapLCD, DigitalLogical: 74},
|
||||
&embd.PinDesc{ID: "P8_42", Aliases: []string{"75", "GPIO_75"}, Caps: embd.CapDigital | embd.CapLCD, DigitalLogical: 75},
|
||||
&embd.PinDesc{ID: "P8_43", Aliases: []string{"72", "GPIO_72"}, Caps: embd.CapDigital | embd.CapLCD, DigitalLogical: 72},
|
||||
&embd.PinDesc{ID: "P8_44", Aliases: []string{"73", "GPIO_73"}, Caps: embd.CapDigital | embd.CapLCD, DigitalLogical: 73},
|
||||
&embd.PinDesc{ID: "P8_45", Aliases: []string{"70", "GPIO_70"}, Caps: embd.CapDigital | embd.CapLCD, DigitalLogical: 70},
|
||||
&embd.PinDesc{ID: "P8_46", Aliases: []string{"71", "GPIO_71"}, Caps: embd.CapDigital | embd.CapLCD, DigitalLogical: 71},
|
||||
|
||||
&embd.PinDesc{ID: "P9_11", Aliases: []string{"30", "GPIO_30", "UART4_RXD"}, Caps: embd.CapDigital | embd.CapUART, DigitalLogical: 30},
|
||||
&embd.PinDesc{ID: "P9_12", Aliases: []string{"60", "GPIO_60", "GPIO1_28"}, Caps: embd.CapDigital, DigitalLogical: 60},
|
||||
&embd.PinDesc{ID: "P9_13", Aliases: []string{"31", "GPIO_31", "UART4_TXD"}, Caps: embd.CapDigital | embd.CapUART, DigitalLogical: 31},
|
||||
&embd.PinDesc{ID: "P9_14", Aliases: []string{"50", "GPIO_50", "EHRPWM1A"}, Caps: embd.CapDigital | embd.CapPWM, DigitalLogical: 50},
|
||||
&embd.PinDesc{ID: "P9_15", Aliases: []string{"48", "GPIO_48", "GPIO1_16"}, Caps: embd.CapDigital, DigitalLogical: 48},
|
||||
&embd.PinDesc{ID: "P9_16", Aliases: []string{"51", "GPIO_51", "EHRPWM1B"}, Caps: embd.CapDigital | embd.CapPWM, DigitalLogical: 51},
|
||||
&embd.PinDesc{ID: "P9_17", Aliases: []string{"5", "GPIO_5", "I2C1_SCL"}, Caps: embd.CapDigital | embd.CapI2C, DigitalLogical: 5},
|
||||
&embd.PinDesc{ID: "P9_18", Aliases: []string{"4", "GPIO_4", "I2C1_SDA"}, Caps: embd.CapDigital | embd.CapI2C, DigitalLogical: 4},
|
||||
&embd.PinDesc{ID: "P9_19", Aliases: []string{"13", "GPIO_13", "I2C2_SCL"}, Caps: embd.CapDigital | embd.CapI2C, DigitalLogical: 13},
|
||||
&embd.PinDesc{ID: "P9_20", Aliases: []string{"12", "GPIO_12", "I2C2_SDA"}, Caps: embd.CapDigital | embd.CapI2C, DigitalLogical: 12},
|
||||
&embd.PinDesc{ID: "P9_21", Aliases: []string{"3", "GPIO_3", "UART2_TXD"}, Caps: embd.CapDigital | embd.CapUART, DigitalLogical: 3},
|
||||
&embd.PinDesc{ID: "P9_22", Aliases: []string{"2", "GPIO_2", "UART2_RXD"}, Caps: embd.CapDigital | embd.CapUART, DigitalLogical: 2},
|
||||
&embd.PinDesc{ID: "P9_23", Aliases: []string{"49", "GPIO_49", "GPIO1_17"}, Caps: embd.CapDigital, DigitalLogical: 49},
|
||||
&embd.PinDesc{ID: "P9_24", Aliases: []string{"15", "GPIO_15", "UART1_TXD"}, Caps: embd.CapDigital | embd.CapUART, DigitalLogical: 15},
|
||||
&embd.PinDesc{ID: "P9_25", Aliases: []string{"117", "GPIO_117", "GPIO3_21"}, Caps: embd.CapDigital, DigitalLogical: 117},
|
||||
&embd.PinDesc{ID: "P9_26", Aliases: []string{"14", "GPIO_14", "UART1_RXD"}, Caps: embd.CapDigital | embd.CapUART, DigitalLogical: 14},
|
||||
&embd.PinDesc{ID: "P9_27", Aliases: []string{"115", "GPIO_115", "GPIO3_19"}, Caps: embd.CapDigital, DigitalLogical: 115},
|
||||
&embd.PinDesc{ID: "P9_28", Aliases: []string{"113", "GPIO_113", "SPI1_CS0"}, Caps: embd.CapDigital | embd.CapSPI, DigitalLogical: 113},
|
||||
&embd.PinDesc{ID: "P9_29", Aliases: []string{"111", "GPIO_111", "SPI1_D0"}, Caps: embd.CapDigital | embd.CapSPI, DigitalLogical: 111},
|
||||
&embd.PinDesc{ID: "P9_30", Aliases: []string{"112", "GPIO_112", "SPI1_D1"}, Caps: embd.CapDigital | embd.CapSPI, DigitalLogical: 112},
|
||||
&embd.PinDesc{ID: "P9_31", Aliases: []string{"110", "GPIO_110", "SPI1_SCLK"}, Caps: embd.CapDigital | embd.CapSPI, DigitalLogical: 110},
|
||||
&embd.PinDesc{ID: "P9_32", Aliases: []string{"VADC"}},
|
||||
&embd.PinDesc{ID: "P9_33", Aliases: []string{"4", "AIN4"}, Caps: embd.CapAnalog, AnalogLogical: 4},
|
||||
&embd.PinDesc{ID: "P9_34", Aliases: []string{"AGND"}},
|
||||
&embd.PinDesc{ID: "P9_35", Aliases: []string{"6", "AIN6"}, Caps: embd.CapAnalog, AnalogLogical: 6},
|
||||
&embd.PinDesc{ID: "P9_36", Aliases: []string{"5", "AIN5"}, Caps: embd.CapAnalog, AnalogLogical: 5},
|
||||
&embd.PinDesc{ID: "P9_37", Aliases: []string{"2", "AIN2"}, Caps: embd.CapAnalog, AnalogLogical: 2},
|
||||
&embd.PinDesc{ID: "P9_38", Aliases: []string{"3", "AIN3"}, Caps: embd.CapAnalog, AnalogLogical: 3},
|
||||
&embd.PinDesc{ID: "P9_39", Aliases: []string{"0", "AIN0"}, Caps: embd.CapAnalog, AnalogLogical: 0},
|
||||
&embd.PinDesc{ID: "P9_40", Aliases: []string{"1", "AIN1"}, Caps: embd.CapAnalog, AnalogLogical: 1},
|
||||
}
|
||||
|
||||
var ledMap = embd.LEDMap{
|
||||
"beaglebone:green:usr0": []string{"0", "USR0", "usr0"},
|
||||
"beaglebone:green:usr1": []string{"1", "USR1", "usr1"},
|
||||
"beaglebone:green:usr2": []string{"2", "USR2", "usr2"},
|
||||
"beaglebone:green:usr3": []string{"3", "USR3", "usr3"},
|
||||
}
|
||||
|
||||
func ensureFeatureEnabled(id string) error {
|
||||
pattern := "/sys/devices/bone_capemgr.*/slots"
|
||||
file, err := embd.FindFirstMatchingFile(pattern)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
bytes, err := ioutil.ReadFile(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
str := string(bytes)
|
||||
if strings.Contains(str, id) {
|
||||
return nil
|
||||
}
|
||||
slots, err := os.OpenFile(file, os.O_WRONLY, os.ModeExclusive)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer slots.Close()
|
||||
_, err = slots.WriteString(id)
|
||||
return err
|
||||
}
|
||||
|
||||
// This cannot be currently used to disable things like the
|
||||
// analog and pwm modules. Removing them from slots file can
|
||||
// potentially cause a kernel panic and unsettle things. So the
|
||||
// recommended thing to do is to simply reboot.
|
||||
func ensureFeatureDisabled(id string) error {
|
||||
pattern := "/sys/devices/bone_capemgr.*/slots"
|
||||
file, err := embd.FindFirstMatchingFile(pattern)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
slots, err := os.OpenFile(file, os.O_RDWR, os.ModeExclusive)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer slots.Close()
|
||||
scanner := bufio.NewScanner(slots)
|
||||
for scanner.Scan() {
|
||||
text := scanner.Text()
|
||||
if !strings.Contains(text, id) {
|
||||
continue
|
||||
}
|
||||
// Extract the id from the line
|
||||
idx := strings.Index(text, ":")
|
||||
if idx < 0 {
|
||||
// Something is off, bail
|
||||
continue
|
||||
}
|
||||
dis := strings.TrimSpace(text[:idx])
|
||||
slots.Seek(0, 0)
|
||||
_, err = slots.WriteString("-" + dis)
|
||||
return err
|
||||
}
|
||||
// Could not disable the feature
|
||||
return fmt.Errorf("embd: could not disable feature %q", id)
|
||||
}
|
||||
|
||||
func init() {
|
||||
embd.Register(embd.HostBBB, func(rev int) *embd.Descriptor {
|
||||
return &embd.Descriptor{
|
||||
GPIODriver: func() embd.GPIODriver {
|
||||
return embd.NewGPIODriver(pins, generic.NewDigitalPin, newAnalogPin, newPWMPin)
|
||||
},
|
||||
I2CDriver: func() embd.I2CDriver {
|
||||
return embd.NewI2CDriver(generic.NewI2CBus)
|
||||
},
|
||||
LEDDriver: func() embd.LEDDriver {
|
||||
return embd.NewLEDDriver(ledMap, generic.NewLED)
|
||||
},
|
||||
}
|
||||
})
|
||||
}
|
261
host/bbb/pwmpin.go
Normal file
261
host/bbb/pwmpin.go
Normal file
@ -0,0 +1,261 @@
|
||||
// PWM support on the BBB.
|
||||
|
||||
package bbb
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/golang/glog"
|
||||
"github.com/kidoman/embd"
|
||||
"github.com/kidoman/embd/util"
|
||||
)
|
||||
|
||||
const (
|
||||
// PWMDefaultPolarity represents the default polarity (Positve or 1) for pwm.
|
||||
PWMDefaultPolarity = embd.Positive
|
||||
|
||||
// PWMDefaultDuty represents the default duty (0ns) for pwm.
|
||||
PWMDefaultDuty = 0
|
||||
|
||||
// PWMDefaultPeriod represents the default period (500000ns) for pwm. Equals 2000 Hz.
|
||||
PWMDefaultPeriod = 500000
|
||||
|
||||
// PWMMaxPulseWidth represents the max period (1000000000ns) supported by pwm. Equals 1 Hz.
|
||||
PWMMaxPulseWidth = 1000000000
|
||||
)
|
||||
|
||||
type pwmPin struct {
|
||||
n string
|
||||
|
||||
drv embd.GPIODriver
|
||||
|
||||
period int
|
||||
polarity embd.Polarity
|
||||
|
||||
dutyf *os.File
|
||||
periodf *os.File
|
||||
polarityf *os.File
|
||||
|
||||
initialized bool
|
||||
}
|
||||
|
||||
func newPWMPin(pd *embd.PinDesc, drv embd.GPIODriver) embd.PWMPin {
|
||||
return &pwmPin{n: pd.ID, drv: drv}
|
||||
}
|
||||
|
||||
func (p *pwmPin) N() string {
|
||||
return p.n
|
||||
}
|
||||
|
||||
func (p *pwmPin) id() string {
|
||||
return "bone_pwm_" + p.n
|
||||
}
|
||||
|
||||
func (p *pwmPin) init() error {
|
||||
if p.initialized {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := p.ensurePWMEnabled(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := p.ensurePinEnabled(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
basePath, err := p.basePath()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := p.ensurePeriodFileExists(basePath, 500*time.Millisecond); err != nil {
|
||||
return err
|
||||
}
|
||||
if p.periodf, err = p.periodFile(basePath); err != nil {
|
||||
return err
|
||||
}
|
||||
if p.dutyf, err = p.dutyFile(basePath); err != nil {
|
||||
return err
|
||||
}
|
||||
if p.polarityf, err = p.polarityFile(basePath); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p.initialized = true
|
||||
|
||||
if err := p.reset(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *pwmPin) ensurePWMEnabled() error {
|
||||
return ensureFeatureEnabled("am33xx_pwm")
|
||||
}
|
||||
|
||||
func (p *pwmPin) ensurePinEnabled() error {
|
||||
return ensureFeatureEnabled(p.id())
|
||||
}
|
||||
|
||||
func (p *pwmPin) ensurePinDisabled() error {
|
||||
return ensureFeatureDisabled(p.id())
|
||||
}
|
||||
|
||||
func (p *pwmPin) basePath() (string, error) {
|
||||
pattern := "/sys/devices/ocp.*/pwm_test_" + p.n + ".*"
|
||||
return embd.FindFirstMatchingFile(pattern)
|
||||
}
|
||||
|
||||
func (p *pwmPin) openFile(path string) (*os.File, error) {
|
||||
return os.OpenFile(path, os.O_WRONLY, os.ModeExclusive)
|
||||
}
|
||||
|
||||
func (p *pwmPin) ensurePeriodFileExists(basePath string, d time.Duration) error {
|
||||
path := p.periodFilePath(basePath)
|
||||
timeout := time.After(d)
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-timeout:
|
||||
return errors.New("embd: period file not found before timeout")
|
||||
default:
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
// We are looping, wait a bit.
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
func (p *pwmPin) periodFilePath(basePath string) string {
|
||||
return path.Join(basePath, "period")
|
||||
}
|
||||
|
||||
func (p *pwmPin) periodFile(basePath string) (*os.File, error) {
|
||||
return p.openFile(p.periodFilePath(basePath))
|
||||
}
|
||||
|
||||
func (p *pwmPin) dutyFile(basePath string) (*os.File, error) {
|
||||
return p.openFile(path.Join(basePath, "duty"))
|
||||
}
|
||||
|
||||
func (p *pwmPin) polarityFile(basePath string) (*os.File, error) {
|
||||
return p.openFile(path.Join(basePath, "polarity"))
|
||||
}
|
||||
|
||||
func (p *pwmPin) SetPeriod(ns int) error {
|
||||
if err := p.init(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if ns > PWMMaxPulseWidth {
|
||||
return fmt.Errorf("embd: pwm period for %v is out of bounds (must be =< %vns)", p.n, PWMMaxPulseWidth)
|
||||
}
|
||||
|
||||
_, err := p.periodf.WriteString(strconv.Itoa(ns))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p.period = ns
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *pwmPin) SetDuty(ns int) error {
|
||||
if err := p.init(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if ns > PWMMaxPulseWidth {
|
||||
return fmt.Errorf("embd: pwm duty %v for pin %v is out of bounds (must be =< %vns)", p.n, PWMMaxPulseWidth)
|
||||
}
|
||||
|
||||
if ns > p.period {
|
||||
return fmt.Errorf("embd: pwm duty %v for pin %v is greater than the period %v", ns, p.n, p.period)
|
||||
}
|
||||
|
||||
_, err := p.dutyf.WriteString(strconv.Itoa(ns))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *pwmPin) SetMicroseconds(us int) error {
|
||||
if err := p.init(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if p.period != 20000000 {
|
||||
glog.Warningf("embd: pwm pin %v has freq %v hz. recommended 50 hz for servo mode", 1000000000/p.period)
|
||||
}
|
||||
duty := us * 1000 // in nanoseconds
|
||||
if duty > p.period {
|
||||
return fmt.Errorf("embd: calculated pwm duty %vns for pin %v (servo mode) is greater than the period %vns", duty, p.n, p.period)
|
||||
}
|
||||
return p.SetDuty(duty)
|
||||
}
|
||||
|
||||
func (p *pwmPin) SetAnalog(value byte) error {
|
||||
duty := util.Map(int64(value), 0, 255, 0, int64(p.period))
|
||||
return p.SetDuty(int(duty))
|
||||
}
|
||||
|
||||
func (p *pwmPin) SetPolarity(pol embd.Polarity) error {
|
||||
if err := p.init(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err := p.polarityf.WriteString(strconv.Itoa(int(pol)))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p.polarity = pol
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *pwmPin) reset() error {
|
||||
if err := p.SetPolarity(embd.Positive); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := p.SetDuty(PWMDefaultDuty); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := p.SetPeriod(PWMDefaultPeriod); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *pwmPin) Close() error {
|
||||
if err := p.drv.Unregister(p.n); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !p.initialized {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := p.reset(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := p.ensurePinDisabled(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
p.initialized = false
|
||||
|
||||
return nil
|
||||
}
|
26
host/bbb/pwmpin_test.go
Normal file
26
host/bbb/pwmpin_test.go
Normal file
@ -0,0 +1,26 @@
|
||||
package bbb
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/kidoman/embd"
|
||||
)
|
||||
|
||||
func TestPWMPinClose(t *testing.T) {
|
||||
pinMap := embd.PinMap{
|
||||
&embd.PinDesc{ID: "P1_1", Aliases: []string{"1"}, Caps: embd.CapPWM},
|
||||
}
|
||||
driver := embd.NewGPIODriver(pinMap, nil, nil, newPWMPin)
|
||||
pin, err := driver.PWMPin(1)
|
||||
if err != nil {
|
||||
t.Fatalf("Looking up pwm pin 1: got %v", err)
|
||||
}
|
||||
pin.Close()
|
||||
pin2, err := driver.PWMPin(1)
|
||||
if err != nil {
|
||||
t.Fatalf("Looking up pwm pin 1: got %v", err)
|
||||
}
|
||||
if pin == pin2 {
|
||||
t.Fatal("Looking up closed pwm pin 1: but got the old instance")
|
||||
}
|
||||
}
|
2
host/doc.go
Normal file
2
host/doc.go
Normal file
@ -0,0 +1,2 @@
|
||||
// Package host is a container for the various hosts supported by EMBD.
|
||||
package host
|
@ -2,7 +2,7 @@
|
||||
// This driver requires kernel version 3.8+ and should work uniformly
|
||||
// across all supported devices.
|
||||
|
||||
package embd
|
||||
package generic
|
||||
|
||||
import (
|
||||
"errors"
|
||||
@ -11,13 +11,15 @@ import (
|
||||
"path"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/kidoman/embd"
|
||||
)
|
||||
|
||||
type digitalPin struct {
|
||||
id string
|
||||
n int
|
||||
|
||||
drv GPIODriver
|
||||
drv embd.GPIODriver
|
||||
|
||||
dir *os.File
|
||||
val *os.File
|
||||
@ -28,7 +30,7 @@ type digitalPin struct {
|
||||
initialized bool
|
||||
}
|
||||
|
||||
func newDigitalPin(pd *PinDesc, drv GPIODriver) DigitalPin {
|
||||
func NewDigitalPin(pd *embd.PinDesc, drv embd.GPIODriver) embd.DigitalPin {
|
||||
return &digitalPin{id: pd.ID, n: pd.DigitalLogical, drv: drv, readBuf: make([]byte, 1)}
|
||||
}
|
||||
|
||||
@ -100,13 +102,13 @@ func (p *digitalPin) activeLowFile() (*os.File, error) {
|
||||
return p.openFile(path.Join(p.basePath(), "active_low"))
|
||||
}
|
||||
|
||||
func (p *digitalPin) SetDirection(dir Direction) error {
|
||||
func (p *digitalPin) SetDirection(dir embd.Direction) error {
|
||||
if err := p.init(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
str := "in"
|
||||
if dir == Out {
|
||||
if dir == embd.Out {
|
||||
str = "out"
|
||||
}
|
||||
_, err := p.dir.WriteString(str)
|
||||
@ -138,7 +140,7 @@ var (
|
||||
|
||||
func (p *digitalPin) write(val int) error {
|
||||
bytes := lowBytes
|
||||
if val == High {
|
||||
if val == embd.High {
|
||||
bytes = highBytes
|
||||
}
|
||||
_, err := p.val.Write(bytes)
|
||||
@ -158,9 +160,9 @@ func (p *digitalPin) TimePulse(state int) (time.Duration, error) {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
aroundState := Low
|
||||
if state == Low {
|
||||
aroundState = High
|
||||
aroundState := embd.Low
|
||||
if state == embd.Low {
|
||||
aroundState = embd.High
|
||||
}
|
||||
|
||||
// Wait for any previous pulse to end
|
@ -1,12 +1,16 @@
|
||||
package embd
|
||||
package generic
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/kidoman/embd"
|
||||
)
|
||||
|
||||
func TestDigitalPinClose(t *testing.T) {
|
||||
pinMap := PinMap{
|
||||
&PinDesc{ID: "P1_1", Aliases: []string{"1"}, Caps: CapDigital},
|
||||
pinMap := embd.PinMap{
|
||||
&embd.PinDesc{ID: "P1_1", Aliases: []string{"1"}, Caps: embd.CapDigital},
|
||||
}
|
||||
driver := newGPIODriver(pinMap, newDigitalPin, nil, nil)
|
||||
driver := embd.NewGPIODriver(pinMap, NewDigitalPin, nil, nil)
|
||||
pin, err := driver.DigitalPin(1)
|
||||
if err != nil {
|
||||
t.Fatalf("Looking up digital pin 1: got %v", err)
|
10
host/generic/doc.go
Normal file
10
host/generic/doc.go
Normal file
@ -0,0 +1,10 @@
|
||||
/*
|
||||
Package generic provides generic (to Linux) drivers for functionalities like
|
||||
|
||||
Digital I/O
|
||||
I²C
|
||||
LED control
|
||||
|
||||
They are used by the hosts to satiate the HAL.
|
||||
*/
|
||||
package generic
|
288
host/generic/i2cbus.go
Normal file
288
host/generic/i2cbus.go
Normal file
@ -0,0 +1,288 @@
|
||||
// I²C support.
|
||||
|
||||
package generic
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
"unsafe"
|
||||
|
||||
"github.com/kidoman/embd"
|
||||
)
|
||||
|
||||
const (
|
||||
delay = 20
|
||||
|
||||
slaveCmd = 0x0703 // Cmd to set slave address
|
||||
rdrwCmd = 0x0707 // Cmd to read/write data together
|
||||
|
||||
rd = 0x0001
|
||||
)
|
||||
|
||||
type i2c_msg struct {
|
||||
addr uint16
|
||||
flags uint16
|
||||
len uint16
|
||||
buf uintptr
|
||||
}
|
||||
|
||||
type i2c_rdwr_ioctl_data struct {
|
||||
msgs uintptr
|
||||
nmsg uint32
|
||||
}
|
||||
|
||||
type i2cBus struct {
|
||||
l byte
|
||||
file *os.File
|
||||
addr byte
|
||||
mu sync.Mutex
|
||||
|
||||
initialized bool
|
||||
}
|
||||
|
||||
func NewI2CBus(l byte) embd.I2CBus {
|
||||
return &i2cBus{l: l}
|
||||
}
|
||||
|
||||
func (b *i2cBus) init() error {
|
||||
if b.initialized {
|
||||
return nil
|
||||
}
|
||||
|
||||
var err error
|
||||
if b.file, err = os.OpenFile(fmt.Sprintf("/dev/i2c-%v", b.l), os.O_RDWR, os.ModeExclusive); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
b.initialized = true
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *i2cBus) setAddress(addr byte) error {
|
||||
if addr != b.addr {
|
||||
if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, b.file.Fd(), slaveCmd, uintptr(addr)); errno != 0 {
|
||||
return syscall.Errno(errno)
|
||||
}
|
||||
|
||||
b.addr = addr
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *i2cBus) ReadByte(addr byte) (byte, error) {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
|
||||
if err := b.setAddress(addr); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
bytes := make([]byte, 1)
|
||||
n, _ := b.file.Read(bytes)
|
||||
|
||||
if n != 1 {
|
||||
return 0, fmt.Errorf("i2c: Unexpected number (%v) of bytes read", n)
|
||||
}
|
||||
|
||||
return bytes[0], nil
|
||||
}
|
||||
|
||||
func (b *i2cBus) WriteByte(addr, value byte) error {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
|
||||
if err := b.setAddress(addr); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
n, err := b.file.Write([]byte{value})
|
||||
|
||||
if n != 1 {
|
||||
err = fmt.Errorf("i2c: Unexpected number (%v) of bytes written in WriteByte", n)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (b *i2cBus) WriteBytes(addr byte, value []byte) error {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
|
||||
if err := b.setAddress(addr); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for i := range value {
|
||||
n, err := b.file.Write([]byte{value[i]})
|
||||
|
||||
if n != 1 {
|
||||
return fmt.Errorf("i2c: Unexpected number (%v) of bytes written in WriteBytes", n)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
time.Sleep(delay * time.Millisecond)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *i2cBus) ReadFromReg(addr, reg byte, value []byte) error {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
|
||||
if err := b.setAddress(addr); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
hdrp := (*reflect.SliceHeader)(unsafe.Pointer(&value))
|
||||
|
||||
var messages [2]i2c_msg
|
||||
messages[0].addr = uint16(addr)
|
||||
messages[0].flags = 0
|
||||
messages[0].len = 1
|
||||
messages[0].buf = uintptr(unsafe.Pointer(®))
|
||||
|
||||
messages[1].addr = uint16(addr)
|
||||
messages[1].flags = rd
|
||||
messages[1].len = uint16(len(value))
|
||||
messages[1].buf = uintptr(unsafe.Pointer(hdrp.Data))
|
||||
|
||||
var packets i2c_rdwr_ioctl_data
|
||||
|
||||
packets.msgs = uintptr(unsafe.Pointer(&messages))
|
||||
packets.nmsg = 2
|
||||
|
||||
if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, b.file.Fd(), rdrwCmd, uintptr(unsafe.Pointer(&packets))); errno != 0 {
|
||||
return syscall.Errno(errno)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *i2cBus) ReadByteFromReg(addr, reg byte) (byte, error) {
|
||||
buf := make([]byte, 1)
|
||||
if err := b.ReadFromReg(addr, reg, buf); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return buf[0], nil
|
||||
}
|
||||
|
||||
func (b *i2cBus) ReadWordFromReg(addr, reg byte) (uint16, error) {
|
||||
buf := make([]byte, 2)
|
||||
if err := b.ReadFromReg(addr, reg, buf); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return uint16((uint16(buf[0]) << 8) | uint16(buf[1])), nil
|
||||
}
|
||||
|
||||
func (b *i2cBus) WriteToReg(addr, reg byte, value []byte) error {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
|
||||
if err := b.setAddress(addr); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
outbuf := append([]byte{reg}, value...)
|
||||
|
||||
hdrp := (*reflect.SliceHeader)(unsafe.Pointer(&outbuf))
|
||||
|
||||
var message i2c_msg
|
||||
message.addr = uint16(addr)
|
||||
message.flags = 0
|
||||
message.len = uint16(len(outbuf))
|
||||
message.buf = uintptr(unsafe.Pointer(&hdrp.Data))
|
||||
|
||||
var packets i2c_rdwr_ioctl_data
|
||||
|
||||
packets.msgs = uintptr(unsafe.Pointer(&message))
|
||||
packets.nmsg = 1
|
||||
|
||||
if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, b.file.Fd(), rdrwCmd, uintptr(unsafe.Pointer(&packets))); errno != 0 {
|
||||
return syscall.Errno(errno)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *i2cBus) WriteByteToReg(addr, reg, value byte) error {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
|
||||
if err := b.setAddress(addr); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
outbuf := [...]byte{
|
||||
reg,
|
||||
value,
|
||||
}
|
||||
|
||||
var message i2c_msg
|
||||
message.addr = uint16(addr)
|
||||
message.flags = 0
|
||||
message.len = uint16(len(outbuf))
|
||||
message.buf = uintptr(unsafe.Pointer(&outbuf))
|
||||
|
||||
var packets i2c_rdwr_ioctl_data
|
||||
|
||||
packets.msgs = uintptr(unsafe.Pointer(&message))
|
||||
packets.nmsg = 1
|
||||
|
||||
if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, b.file.Fd(), rdrwCmd, uintptr(unsafe.Pointer(&packets))); errno != 0 {
|
||||
return syscall.Errno(errno)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *i2cBus) WriteWordToReg(addr, reg byte, value uint16) error {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
|
||||
if err := b.setAddress(addr); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
outbuf := [...]byte{
|
||||
reg,
|
||||
byte(value >> 8),
|
||||
byte(value),
|
||||
}
|
||||
|
||||
var messages i2c_msg
|
||||
messages.addr = uint16(addr)
|
||||
messages.flags = 0
|
||||
messages.len = uint16(len(outbuf))
|
||||
messages.buf = uintptr(unsafe.Pointer(&outbuf))
|
||||
|
||||
var packets i2c_rdwr_ioctl_data
|
||||
|
||||
packets.msgs = uintptr(unsafe.Pointer(&messages))
|
||||
packets.nmsg = 1
|
||||
|
||||
if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, b.file.Fd(), rdrwCmd, uintptr(unsafe.Pointer(&packets))); errno != 0 {
|
||||
return syscall.Errno(errno)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *i2cBus) Close() error {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
|
||||
if !b.initialized {
|
||||
return nil
|
||||
}
|
||||
|
||||
return b.file.Close()
|
||||
}
|
113
host/generic/led.go
Normal file
113
host/generic/led.go
Normal file
@ -0,0 +1,113 @@
|
||||
// LED control support.
|
||||
|
||||
package generic
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/kidoman/embd"
|
||||
)
|
||||
|
||||
type led struct {
|
||||
id string
|
||||
|
||||
brightness *os.File
|
||||
|
||||
initialized bool
|
||||
}
|
||||
|
||||
func NewLED(id string) embd.LED {
|
||||
return &led{id: id}
|
||||
}
|
||||
|
||||
func (l *led) init() error {
|
||||
if l.initialized {
|
||||
return nil
|
||||
}
|
||||
|
||||
var err error
|
||||
if l.brightness, err = l.brightnessFile(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
l.initialized = true
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *led) brightnessFilePath() string {
|
||||
return fmt.Sprintf("/sys/class/leds/%v/brightness", l.id)
|
||||
}
|
||||
|
||||
func (l *led) openFile(path string) (*os.File, error) {
|
||||
return os.OpenFile(path, os.O_RDWR, os.ModeExclusive)
|
||||
}
|
||||
|
||||
func (l *led) brightnessFile() (*os.File, error) {
|
||||
return l.openFile(l.brightnessFilePath())
|
||||
}
|
||||
|
||||
func (l *led) On() error {
|
||||
if err := l.init(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err := l.brightness.WriteString("1")
|
||||
return err
|
||||
}
|
||||
|
||||
func (l *led) Off() error {
|
||||
if err := l.init(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err := l.brightness.WriteString("0")
|
||||
return err
|
||||
}
|
||||
|
||||
func (l *led) isOn() (bool, error) {
|
||||
l.brightness.Seek(0, 0)
|
||||
bytes, err := ioutil.ReadAll(l.brightness)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
str := string(bytes)
|
||||
str = strings.TrimSpace(str)
|
||||
if str == "1" {
|
||||
return true, nil
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (l *led) Toggle() error {
|
||||
if err := l.init(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
state, err := l.isOn()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if state {
|
||||
return l.Off()
|
||||
}
|
||||
return l.On()
|
||||
}
|
||||
|
||||
func (l *led) Close() error {
|
||||
if !l.initialized {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := l.brightness.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
l.initialized = false
|
||||
|
||||
return nil
|
||||
}
|
71
host/rpi/rpi.go
Normal file
71
host/rpi/rpi.go
Normal file
@ -0,0 +1,71 @@
|
||||
/*
|
||||
Package rpi provides Raspberry Pi support.
|
||||
The following features are supported on Linux kernel 3.8+
|
||||
|
||||
GPIO (digital (rw))
|
||||
I²C
|
||||
*/
|
||||
package rpi
|
||||
|
||||
import (
|
||||
"github.com/kidoman/embd"
|
||||
"github.com/kidoman/embd/host/generic"
|
||||
)
|
||||
|
||||
var rev1Pins = embd.PinMap{
|
||||
&embd.PinDesc{ID: "P1_3", Aliases: []string{"0", "GPIO_0", "SDA", "I2C0_SDA"}, Caps: embd.CapDigital | embd.CapI2C, DigitalLogical: 0},
|
||||
&embd.PinDesc{ID: "P1_5", Aliases: []string{"1", "GPIO_1", "SCL", "I2C0_SCL"}, Caps: embd.CapDigital | embd.CapI2C, DigitalLogical: 1},
|
||||
&embd.PinDesc{ID: "P1_7", Aliases: []string{"4", "GPIO_4", "GPCLK0"}, Caps: embd.CapDigital, DigitalLogical: 4},
|
||||
&embd.PinDesc{ID: "P1_8", Aliases: []string{"14", "GPIO_14", "TXD", "UART0_TXD"}, Caps: embd.CapDigital | embd.CapUART, DigitalLogical: 14},
|
||||
&embd.PinDesc{ID: "P1_10", Aliases: []string{"15", "GPIO_15", "RXD", "UART0_RXD"}, Caps: embd.CapDigital | embd.CapUART, DigitalLogical: 15},
|
||||
&embd.PinDesc{ID: "P1_11", Aliases: []string{"17", "GPIO_17"}, Caps: embd.CapDigital, DigitalLogical: 17},
|
||||
&embd.PinDesc{ID: "P1_12", Aliases: []string{"18", "GPIO_18", "PCM_CLK"}, Caps: embd.CapDigital, DigitalLogical: 18},
|
||||
&embd.PinDesc{ID: "P1_13", Aliases: []string{"21", "GPIO_21"}, Caps: embd.CapDigital, DigitalLogical: 21},
|
||||
&embd.PinDesc{ID: "P1_15", Aliases: []string{"22", "GPIO_22"}, Caps: embd.CapDigital, DigitalLogical: 22},
|
||||
&embd.PinDesc{ID: "P1_16", Aliases: []string{"23", "GPIO_23"}, Caps: embd.CapDigital, DigitalLogical: 23},
|
||||
&embd.PinDesc{ID: "P1_18", Aliases: []string{"24", "GPIO_24"}, Caps: embd.CapDigital, DigitalLogical: 24},
|
||||
&embd.PinDesc{ID: "P1_19", Aliases: []string{"10", "GPIO_10", "MOSI", "SPI0_MOSI"}, Caps: embd.CapDigital | embd.CapSPI, DigitalLogical: 10},
|
||||
&embd.PinDesc{ID: "P1_21", Aliases: []string{"9", "GPIO_9", "MISO", "SPI0_MISO"}, Caps: embd.CapDigital | embd.CapSPI, DigitalLogical: 9},
|
||||
&embd.PinDesc{ID: "P1_22", Aliases: []string{"25", "GPIO_25"}, Caps: embd.CapDigital, DigitalLogical: 25},
|
||||
&embd.PinDesc{ID: "P1_23", Aliases: []string{"11", "GPIO_11", "SCLK", "SPI0_SCLK"}, Caps: embd.CapDigital | embd.CapSPI, DigitalLogical: 11},
|
||||
&embd.PinDesc{ID: "P1_24", Aliases: []string{"8", "GPIO_8", "CE0", "SPI0_CE0_N"}, Caps: embd.CapDigital | embd.CapSPI, DigitalLogical: 8},
|
||||
&embd.PinDesc{ID: "P1_26", Aliases: []string{"7", "GPIO_7", "CE1", "SPI0_CE1_N"}, Caps: embd.CapDigital | embd.CapSPI, DigitalLogical: 7},
|
||||
}
|
||||
|
||||
var rev2Pins = embd.PinMap{
|
||||
&embd.PinDesc{ID: "P1_3", Aliases: []string{"2", "GPIO_2", "SDA", "I2C1_SDA"}, Caps: embd.CapDigital | embd.CapI2C, DigitalLogical: 2},
|
||||
&embd.PinDesc{ID: "P1_5", Aliases: []string{"3", "GPIO_3", "SCL", "I2C1_SCL"}, Caps: embd.CapDigital | embd.CapI2C, DigitalLogical: 3},
|
||||
&embd.PinDesc{ID: "P1_7", Aliases: []string{"4", "GPIO_4", "GPCLK0"}, Caps: embd.CapDigital, DigitalLogical: 4},
|
||||
&embd.PinDesc{ID: "P1_8", Aliases: []string{"14", "GPIO_14", "TXD", "UART0_TXD"}, Caps: embd.CapDigital | embd.CapUART, DigitalLogical: 14},
|
||||
&embd.PinDesc{ID: "P1_10", Aliases: []string{"15", "GPIO_15", "RXD", "UART0_RXD"}, Caps: embd.CapDigital | embd.CapUART, DigitalLogical: 15},
|
||||
&embd.PinDesc{ID: "P1_11", Aliases: []string{"17", "GPIO_17"}, Caps: embd.CapDigital, DigitalLogical: 17},
|
||||
&embd.PinDesc{ID: "P1_12", Aliases: []string{"18", "GPIO_18", "PCM_CLK"}, Caps: embd.CapDigital, DigitalLogical: 18},
|
||||
&embd.PinDesc{ID: "P1_13", Aliases: []string{"27", "GPIO_27"}, Caps: embd.CapDigital, DigitalLogical: 27},
|
||||
&embd.PinDesc{ID: "P1_15", Aliases: []string{"22", "GPIO_22"}, Caps: embd.CapDigital, DigitalLogical: 22},
|
||||
&embd.PinDesc{ID: "P1_16", Aliases: []string{"23", "GPIO_23"}, Caps: embd.CapDigital, DigitalLogical: 23},
|
||||
&embd.PinDesc{ID: "P1_18", Aliases: []string{"24", "GPIO_24"}, Caps: embd.CapDigital, DigitalLogical: 24},
|
||||
&embd.PinDesc{ID: "P1_19", Aliases: []string{"10", "GPIO_10", "MOSI", "SPI0_MOSI"}, Caps: embd.CapDigital | embd.CapSPI, DigitalLogical: 10},
|
||||
&embd.PinDesc{ID: "P1_21", Aliases: []string{"9", "GPIO_9", "MISO", "SPI0_MISO"}, Caps: embd.CapDigital | embd.CapSPI, DigitalLogical: 9},
|
||||
&embd.PinDesc{ID: "P1_22", Aliases: []string{"25", "GPIO_25"}, Caps: embd.CapDigital, DigitalLogical: 25},
|
||||
&embd.PinDesc{ID: "P1_23", Aliases: []string{"11", "GPIO_11", "SCLK", "SPI0_SCLK"}, Caps: embd.CapDigital | embd.CapSPI, DigitalLogical: 11},
|
||||
&embd.PinDesc{ID: "P1_24", Aliases: []string{"8", "GPIO_8", "CE0", "SPI0_CE0_N"}, Caps: embd.CapDigital | embd.CapSPI, DigitalLogical: 8},
|
||||
&embd.PinDesc{ID: "P1_26", Aliases: []string{"7", "GPIO_7", "CE1", "SPI0_CE1_N"}, Caps: embd.CapDigital | embd.CapSPI, DigitalLogical: 7},
|
||||
}
|
||||
|
||||
func init() {
|
||||
embd.Register(embd.HostRPi, func(rev int) *embd.Descriptor {
|
||||
var pins = rev1Pins
|
||||
if rev > 1 {
|
||||
pins = rev2Pins
|
||||
}
|
||||
|
||||
return &embd.Descriptor{
|
||||
GPIODriver: func() embd.GPIODriver {
|
||||
return embd.NewGPIODriver(pins, generic.NewDigitalPin, nil, nil)
|
||||
},
|
||||
I2CDriver: func() embd.I2CDriver {
|
||||
return embd.NewI2CDriver(generic.NewI2CBus)
|
||||
},
|
||||
}
|
||||
})
|
||||
}
|
4
i2c.go
4
i2c.go
@ -24,6 +24,9 @@ type I2CBus interface {
|
||||
WriteByteToReg(addr, reg, value byte) error
|
||||
// WriteU16ToReg
|
||||
WriteWordToReg(addr, reg byte, value uint16) error
|
||||
|
||||
// Close releases the resources associated with the bus.
|
||||
Close() error
|
||||
}
|
||||
|
||||
// I2CDriver interface interacts with the host descriptors to allow us
|
||||
@ -31,6 +34,7 @@ type I2CBus interface {
|
||||
type I2CDriver interface {
|
||||
Bus(l byte) I2CBus
|
||||
|
||||
// Close releases the resources associated with the driver.
|
||||
Close() error
|
||||
}
|
||||
|
||||
|
295
i2cdriver.go
295
i2cdriver.go
@ -1,25 +1,24 @@
|
||||
// Generic I2C driver.
|
||||
// Generic I²C driver.
|
||||
|
||||
package embd
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
"unsafe"
|
||||
)
|
||||
import "sync"
|
||||
|
||||
type i2cBusFactory func(byte) I2CBus
|
||||
|
||||
type i2cDriver struct {
|
||||
busMap map[byte]*i2cBus
|
||||
busMap map[byte]I2CBus
|
||||
busMapLock sync.Mutex
|
||||
|
||||
ibf i2cBusFactory
|
||||
}
|
||||
|
||||
func newI2CDriver() I2CDriver {
|
||||
// NewI2CDriver returns a I2CDriver interface which allows control
|
||||
// over the I²C subsystem.
|
||||
func NewI2CDriver(ibf i2cBusFactory) I2CDriver {
|
||||
return &i2cDriver{
|
||||
busMap: make(map[byte]*i2cBus),
|
||||
busMap: make(map[byte]I2CBus),
|
||||
ibf: ibf,
|
||||
}
|
||||
}
|
||||
|
||||
@ -27,280 +26,18 @@ func (i *i2cDriver) Bus(l byte) I2CBus {
|
||||
i.busMapLock.Lock()
|
||||
defer i.busMapLock.Unlock()
|
||||
|
||||
var b *i2cBus
|
||||
|
||||
if b = i.busMap[l]; b == nil {
|
||||
b = &i2cBus{l: l}
|
||||
i.busMap[l] = b
|
||||
if b, ok := i.busMap[l]; ok {
|
||||
return b
|
||||
}
|
||||
|
||||
b := i.ibf(l)
|
||||
i.busMap[l] = b
|
||||
return b
|
||||
}
|
||||
|
||||
func (i *i2cDriver) Close() error {
|
||||
for _, b := range i.busMap {
|
||||
b.Close()
|
||||
|
||||
delete(i.busMap, b.l)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
const (
|
||||
delay = 20
|
||||
|
||||
slaveCmd = 0x0703 // Cmd to set slave address
|
||||
rdrwCmd = 0x0707 // Cmd to read/write data together
|
||||
|
||||
rd = 0x0001
|
||||
)
|
||||
|
||||
type i2c_msg struct {
|
||||
addr uint16
|
||||
flags uint16
|
||||
len uint16
|
||||
buf uintptr
|
||||
}
|
||||
|
||||
type i2c_rdwr_ioctl_data struct {
|
||||
msgs uintptr
|
||||
nmsg uint32
|
||||
}
|
||||
|
||||
type i2cBus struct {
|
||||
l byte
|
||||
file *os.File
|
||||
addr byte
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func newI2CBus(l byte) (*i2cBus, error) {
|
||||
b := &i2cBus{l: l}
|
||||
|
||||
var err error
|
||||
if b.file, err = os.OpenFile(fmt.Sprintf("/dev/i2c-%v", b.l), os.O_RDWR, os.ModeExclusive); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return b, nil
|
||||
}
|
||||
|
||||
func (b *i2cBus) Close() error {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
|
||||
return b.file.Close()
|
||||
}
|
||||
|
||||
func (b *i2cBus) setAddress(addr byte) error {
|
||||
if addr != b.addr {
|
||||
if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, b.file.Fd(), slaveCmd, uintptr(addr)); errno != 0 {
|
||||
return syscall.Errno(errno)
|
||||
}
|
||||
|
||||
b.addr = addr
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *i2cBus) ReadByte(addr byte) (byte, error) {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
|
||||
if err := b.setAddress(addr); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
bytes := make([]byte, 1)
|
||||
n, _ := b.file.Read(bytes)
|
||||
|
||||
if n != 1 {
|
||||
return 0, fmt.Errorf("i2c: Unexpected number (%v) of bytes read", n)
|
||||
}
|
||||
|
||||
return bytes[0], nil
|
||||
}
|
||||
|
||||
func (b *i2cBus) WriteByte(addr, value byte) error {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
|
||||
if err := b.setAddress(addr); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
n, err := b.file.Write([]byte{value})
|
||||
|
||||
if n != 1 {
|
||||
err = fmt.Errorf("i2c: Unexpected number (%v) of bytes written in WriteByte", n)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (b *i2cBus) WriteBytes(addr byte, value []byte) error {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
|
||||
if err := b.setAddress(addr); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for i := range value {
|
||||
n, err := b.file.Write([]byte{value[i]})
|
||||
|
||||
if n != 1 {
|
||||
return fmt.Errorf("i2c: Unexpected number (%v) of bytes written in WriteBytes", n)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
time.Sleep(delay * time.Millisecond)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *i2cBus) ReadFromReg(addr, reg byte, value []byte) error {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
|
||||
if err := b.setAddress(addr); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
hdrp := (*reflect.SliceHeader)(unsafe.Pointer(&value))
|
||||
|
||||
var messages [2]i2c_msg
|
||||
messages[0].addr = uint16(addr)
|
||||
messages[0].flags = 0
|
||||
messages[0].len = 1
|
||||
messages[0].buf = uintptr(unsafe.Pointer(®))
|
||||
|
||||
messages[1].addr = uint16(addr)
|
||||
messages[1].flags = rd
|
||||
messages[1].len = uint16(len(value))
|
||||
messages[1].buf = uintptr(unsafe.Pointer(hdrp.Data))
|
||||
|
||||
var packets i2c_rdwr_ioctl_data
|
||||
|
||||
packets.msgs = uintptr(unsafe.Pointer(&messages))
|
||||
packets.nmsg = 2
|
||||
|
||||
if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, b.file.Fd(), rdrwCmd, uintptr(unsafe.Pointer(&packets))); errno != 0 {
|
||||
return syscall.Errno(errno)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *i2cBus) ReadByteFromReg(addr, reg byte) (byte, error) {
|
||||
buf := make([]byte, 1)
|
||||
if err := b.ReadFromReg(addr, reg, buf); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return buf[0], nil
|
||||
}
|
||||
|
||||
func (b *i2cBus) ReadWordFromReg(addr, reg byte) (uint16, error) {
|
||||
buf := make([]byte, 2)
|
||||
if err := b.ReadFromReg(addr, reg, buf); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return uint16((uint16(buf[0]) << 8) | uint16(buf[1])), nil
|
||||
}
|
||||
|
||||
func (b *i2cBus) WriteToReg(addr, reg byte, value []byte) error {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
|
||||
if err := b.setAddress(addr); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
outbuf := append([]byte{reg}, value...)
|
||||
|
||||
hdrp := (*reflect.SliceHeader)(unsafe.Pointer(&outbuf))
|
||||
|
||||
var message i2c_msg
|
||||
message.addr = uint16(addr)
|
||||
message.flags = 0
|
||||
message.len = uint16(len(outbuf))
|
||||
message.buf = uintptr(unsafe.Pointer(&hdrp.Data))
|
||||
|
||||
var packets i2c_rdwr_ioctl_data
|
||||
|
||||
packets.msgs = uintptr(unsafe.Pointer(&message))
|
||||
packets.nmsg = 1
|
||||
|
||||
if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, b.file.Fd(), rdrwCmd, uintptr(unsafe.Pointer(&packets))); errno != 0 {
|
||||
return syscall.Errno(errno)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *i2cBus) WriteByteToReg(addr, reg, value byte) error {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
|
||||
if err := b.setAddress(addr); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
outbuf := [...]byte{
|
||||
reg,
|
||||
value,
|
||||
}
|
||||
|
||||
var message i2c_msg
|
||||
message.addr = uint16(addr)
|
||||
message.flags = 0
|
||||
message.len = uint16(len(outbuf))
|
||||
message.buf = uintptr(unsafe.Pointer(&outbuf))
|
||||
|
||||
var packets i2c_rdwr_ioctl_data
|
||||
|
||||
packets.msgs = uintptr(unsafe.Pointer(&message))
|
||||
packets.nmsg = 1
|
||||
|
||||
if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, b.file.Fd(), rdrwCmd, uintptr(unsafe.Pointer(&packets))); errno != 0 {
|
||||
return syscall.Errno(errno)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (b *i2cBus) WriteWordToReg(addr, reg byte, value uint16) error {
|
||||
b.mu.Lock()
|
||||
defer b.mu.Unlock()
|
||||
|
||||
if err := b.setAddress(addr); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
outbuf := [...]byte{
|
||||
reg,
|
||||
byte(value >> 8),
|
||||
byte(value),
|
||||
}
|
||||
|
||||
var messages i2c_msg
|
||||
messages.addr = uint16(addr)
|
||||
messages.flags = 0
|
||||
messages.len = uint16(len(outbuf))
|
||||
messages.buf = uintptr(unsafe.Pointer(&outbuf))
|
||||
|
||||
var packets i2c_rdwr_ioctl_data
|
||||
|
||||
packets.msgs = uintptr(unsafe.Pointer(&messages))
|
||||
packets.nmsg = 1
|
||||
|
||||
if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, b.file.Fd(), rdrwCmd, uintptr(unsafe.Pointer(&packets))); errno != 0 {
|
||||
return syscall.Errno(errno)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
118
leddriver.go
118
leddriver.go
@ -5,24 +5,29 @@ package embd
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// LEDMap type represents a LED mapping for a host.
|
||||
type LEDMap map[string][]string
|
||||
|
||||
type ledFactory func(string) LED
|
||||
|
||||
type ledDriver struct {
|
||||
ledMap LEDMap
|
||||
|
||||
lf ledFactory
|
||||
|
||||
initializedLEDs map[string]LED
|
||||
}
|
||||
|
||||
func newLEDDriver(ledMap LEDMap) LEDDriver {
|
||||
// NewLEDDriver returns a LEDDriver interface which allows control
|
||||
// over the LED subsystem.
|
||||
func NewLEDDriver(ledMap LEDMap, lf ledFactory) LEDDriver {
|
||||
return &ledDriver{
|
||||
ledMap: ledMap,
|
||||
ledMap: ledMap,
|
||||
lf: lf,
|
||||
|
||||
initializedLEDs: map[string]LED{},
|
||||
}
|
||||
}
|
||||
@ -57,7 +62,7 @@ func (d *ledDriver) LED(k interface{}) (LED, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
led := newLED(id)
|
||||
led := d.lf(id)
|
||||
d.initializedLEDs[id] = led
|
||||
|
||||
return led, nil
|
||||
@ -72,104 +77,3 @@ func (d *ledDriver) Close() error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type led struct {
|
||||
id string
|
||||
|
||||
brightness *os.File
|
||||
|
||||
initialized bool
|
||||
}
|
||||
|
||||
func newLED(id string) LED {
|
||||
return &led{id: id}
|
||||
}
|
||||
|
||||
func (l *led) init() error {
|
||||
if l.initialized {
|
||||
return nil
|
||||
}
|
||||
|
||||
var err error
|
||||
if l.brightness, err = l.brightnessFile(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
l.initialized = true
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (l *led) brightnessFilePath() string {
|
||||
return fmt.Sprintf("/sys/class/leds/%v/brightness", l.id)
|
||||
}
|
||||
|
||||
func (l *led) openFile(path string) (*os.File, error) {
|
||||
return os.OpenFile(path, os.O_RDWR, os.ModeExclusive)
|
||||
}
|
||||
|
||||
func (l *led) brightnessFile() (*os.File, error) {
|
||||
return l.openFile(l.brightnessFilePath())
|
||||
}
|
||||
|
||||
func (l *led) On() error {
|
||||
if err := l.init(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err := l.brightness.WriteString("1")
|
||||
return err
|
||||
}
|
||||
|
||||
func (l *led) Off() error {
|
||||
if err := l.init(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err := l.brightness.WriteString("0")
|
||||
return err
|
||||
}
|
||||
|
||||
func (l *led) isOn() (bool, error) {
|
||||
l.brightness.Seek(0, 0)
|
||||
bytes, err := ioutil.ReadAll(l.brightness)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
str := string(bytes)
|
||||
str = strings.TrimSpace(str)
|
||||
if str == "1" {
|
||||
return true, nil
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (l *led) Toggle() error {
|
||||
if err := l.init(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
state, err := l.isOn()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if state {
|
||||
return l.Off()
|
||||
}
|
||||
return l.On()
|
||||
}
|
||||
|
||||
func (l *led) Close() error {
|
||||
if !l.initialized {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := l.brightness.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
l.initialized = false
|
||||
|
||||
return nil
|
||||
}
|
||||
|
63
rpi.go
63
rpi.go
@ -1,63 +0,0 @@
|
||||
// RaspberryPi support.
|
||||
// The following features are supported on Linux kernel 3.8+
|
||||
//
|
||||
// GPIO (digital (rw))
|
||||
// I2C
|
||||
|
||||
package embd
|
||||
|
||||
var rpiRev1Pins = PinMap{
|
||||
&PinDesc{ID: "P1_3", Aliases: []string{"0", "GPIO_0", "SDA", "I2C0_SDA"}, Caps: CapDigital | CapI2C, DigitalLogical: 0},
|
||||
&PinDesc{ID: "P1_5", Aliases: []string{"1", "GPIO_1", "SCL", "I2C0_SCL"}, Caps: CapDigital | CapI2C, DigitalLogical: 1},
|
||||
&PinDesc{ID: "P1_7", Aliases: []string{"4", "GPIO_4", "GPCLK0"}, Caps: CapDigital, DigitalLogical: 4},
|
||||
&PinDesc{ID: "P1_8", Aliases: []string{"14", "GPIO_14", "TXD", "UART0_TXD"}, Caps: CapDigital | CapUART, DigitalLogical: 14},
|
||||
&PinDesc{ID: "P1_10", Aliases: []string{"15", "GPIO_15", "RXD", "UART0_RXD"}, Caps: CapDigital | CapUART, DigitalLogical: 15},
|
||||
&PinDesc{ID: "P1_11", Aliases: []string{"17", "GPIO_17"}, Caps: CapDigital, DigitalLogical: 17},
|
||||
&PinDesc{ID: "P1_12", Aliases: []string{"18", "GPIO_18", "PCM_CLK"}, Caps: CapDigital, DigitalLogical: 18},
|
||||
&PinDesc{ID: "P1_13", Aliases: []string{"21", "GPIO_21"}, Caps: CapDigital, DigitalLogical: 21},
|
||||
&PinDesc{ID: "P1_15", Aliases: []string{"22", "GPIO_22"}, Caps: CapDigital, DigitalLogical: 22},
|
||||
&PinDesc{ID: "P1_16", Aliases: []string{"23", "GPIO_23"}, Caps: CapDigital, DigitalLogical: 23},
|
||||
&PinDesc{ID: "P1_18", Aliases: []string{"24", "GPIO_24"}, Caps: CapDigital, DigitalLogical: 24},
|
||||
&PinDesc{ID: "P1_19", Aliases: []string{"10", "GPIO_10", "MOSI", "SPI0_MOSI"}, Caps: CapDigital | CapSPI, DigitalLogical: 10},
|
||||
&PinDesc{ID: "P1_21", Aliases: []string{"9", "GPIO_9", "MISO", "SPI0_MISO"}, Caps: CapDigital | CapSPI, DigitalLogical: 9},
|
||||
&PinDesc{ID: "P1_22", Aliases: []string{"25", "GPIO_25"}, Caps: CapDigital, DigitalLogical: 25},
|
||||
&PinDesc{ID: "P1_23", Aliases: []string{"11", "GPIO_11", "SCLK", "SPI0_SCLK"}, Caps: CapDigital | CapSPI, DigitalLogical: 11},
|
||||
&PinDesc{ID: "P1_24", Aliases: []string{"8", "GPIO_8", "CE0", "SPI0_CE0_N"}, Caps: CapDigital | CapSPI, DigitalLogical: 8},
|
||||
&PinDesc{ID: "P1_26", Aliases: []string{"7", "GPIO_7", "CE1", "SPI0_CE1_N"}, Caps: CapDigital | CapSPI, DigitalLogical: 7},
|
||||
}
|
||||
|
||||
var rpiRev2Pins = PinMap{
|
||||
&PinDesc{ID: "P1_3", Aliases: []string{"2", "GPIO_2", "SDA", "I2C1_SDA"}, Caps: CapDigital | CapI2C, DigitalLogical: 2},
|
||||
&PinDesc{ID: "P1_5", Aliases: []string{"3", "GPIO_3", "SCL", "I2C1_SCL"}, Caps: CapDigital | CapI2C, DigitalLogical: 3},
|
||||
&PinDesc{ID: "P1_7", Aliases: []string{"4", "GPIO_4", "GPCLK0"}, Caps: CapDigital, DigitalLogical: 4},
|
||||
&PinDesc{ID: "P1_8", Aliases: []string{"14", "GPIO_14", "TXD", "UART0_TXD"}, Caps: CapDigital | CapUART, DigitalLogical: 14},
|
||||
&PinDesc{ID: "P1_10", Aliases: []string{"15", "GPIO_15", "RXD", "UART0_RXD"}, Caps: CapDigital | CapUART, DigitalLogical: 15},
|
||||
&PinDesc{ID: "P1_11", Aliases: []string{"17", "GPIO_17"}, Caps: CapDigital, DigitalLogical: 17},
|
||||
&PinDesc{ID: "P1_12", Aliases: []string{"18", "GPIO_18", "PCM_CLK"}, Caps: CapDigital, DigitalLogical: 18},
|
||||
&PinDesc{ID: "P1_13", Aliases: []string{"27", "GPIO_27"}, Caps: CapDigital, DigitalLogical: 27},
|
||||
&PinDesc{ID: "P1_15", Aliases: []string{"22", "GPIO_22"}, Caps: CapDigital, DigitalLogical: 22},
|
||||
&PinDesc{ID: "P1_16", Aliases: []string{"23", "GPIO_23"}, Caps: CapDigital, DigitalLogical: 23},
|
||||
&PinDesc{ID: "P1_18", Aliases: []string{"24", "GPIO_24"}, Caps: CapDigital, DigitalLogical: 24},
|
||||
&PinDesc{ID: "P1_19", Aliases: []string{"10", "GPIO_10", "MOSI", "SPI0_MOSI"}, Caps: CapDigital | CapSPI, DigitalLogical: 10},
|
||||
&PinDesc{ID: "P1_21", Aliases: []string{"9", "GPIO_9", "MISO", "SPI0_MISO"}, Caps: CapDigital | CapSPI, DigitalLogical: 9},
|
||||
&PinDesc{ID: "P1_22", Aliases: []string{"25", "GPIO_25"}, Caps: CapDigital, DigitalLogical: 25},
|
||||
&PinDesc{ID: "P1_23", Aliases: []string{"11", "GPIO_11", "SCLK", "SPI0_SCLK"}, Caps: CapDigital | CapSPI, DigitalLogical: 11},
|
||||
&PinDesc{ID: "P1_24", Aliases: []string{"8", "GPIO_8", "CE0", "SPI0_CE0_N"}, Caps: CapDigital | CapSPI, DigitalLogical: 8},
|
||||
&PinDesc{ID: "P1_26", Aliases: []string{"7", "GPIO_7", "CE1", "SPI0_CE1_N"}, Caps: CapDigital | CapSPI, DigitalLogical: 7},
|
||||
}
|
||||
|
||||
func init() {
|
||||
Register(HostRPi, func(rev int) *Descriptor {
|
||||
var pins = rpiRev1Pins
|
||||
if rev > 1 {
|
||||
pins = rpiRev2Pins
|
||||
}
|
||||
|
||||
return &Descriptor{
|
||||
GPIODriver: func() GPIODriver {
|
||||
return newGPIODriver(pins, newDigitalPin, nil, nil)
|
||||
},
|
||||
I2CDriver: newI2CDriver,
|
||||
}
|
||||
})
|
||||
}
|
@ -10,6 +10,8 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/kidoman/embd"
|
||||
|
||||
_ "github.com/kidoman/embd/host/all"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -7,6 +7,8 @@ import (
|
||||
"fmt"
|
||||
|
||||
"github.com/kidoman/embd"
|
||||
|
||||
_ "github.com/kidoman/embd/host/all"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -9,6 +9,8 @@ import (
|
||||
|
||||
"github.com/kidoman/embd"
|
||||
"github.com/kidoman/embd/sensor/bh1750fvi"
|
||||
|
||||
_ "github.com/kidoman/embd/host/all"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -9,6 +9,8 @@ import (
|
||||
|
||||
"github.com/kidoman/embd"
|
||||
"github.com/kidoman/embd/sensor/bmp085"
|
||||
|
||||
_ "github.com/kidoman/embd/host/all"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -9,6 +9,8 @@ import (
|
||||
|
||||
"github.com/kidoman/embd"
|
||||
"github.com/kidoman/embd/sensor/bmp180"
|
||||
|
||||
_ "github.com/kidoman/embd/host/all"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -7,6 +7,8 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/kidoman/embd"
|
||||
|
||||
_ "github.com/kidoman/embd/host/all"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -7,6 +7,8 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/kidoman/embd"
|
||||
|
||||
_ "github.com/kidoman/embd/host/all"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -7,6 +7,8 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/kidoman/embd"
|
||||
|
||||
_ "github.com/kidoman/embd/host/all"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -6,6 +6,8 @@ import (
|
||||
"flag"
|
||||
|
||||
"github.com/kidoman/embd"
|
||||
|
||||
_ "github.com/kidoman/embd/host/all"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -11,6 +11,8 @@ import (
|
||||
|
||||
"github.com/kidoman/embd"
|
||||
"github.com/kidoman/embd/sensor/l3gd20"
|
||||
|
||||
_ "github.com/kidoman/embd/host/all"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -12,6 +12,8 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/kidoman/embd"
|
||||
|
||||
_ "github.com/kidoman/embd/host/all"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -9,6 +9,8 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/kidoman/embd"
|
||||
|
||||
_ "github.com/kidoman/embd/host/all"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -9,6 +9,8 @@ import (
|
||||
|
||||
"github.com/kidoman/embd"
|
||||
"github.com/kidoman/embd/sensor/lsm303"
|
||||
|
||||
_ "github.com/kidoman/embd/host/all"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -11,6 +11,8 @@ import (
|
||||
|
||||
"github.com/kidoman/embd"
|
||||
"github.com/kidoman/embd/controller/mcp4725"
|
||||
|
||||
_ "github.com/kidoman/embd/host/all"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -10,6 +10,8 @@ import (
|
||||
|
||||
"github.com/kidoman/embd"
|
||||
"github.com/kidoman/embd/controller/pca9685"
|
||||
|
||||
_ "github.com/kidoman/embd/host/all"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -9,6 +9,9 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/kidoman/embd"
|
||||
"github.com/kidoman/embd/host/bbb"
|
||||
|
||||
_ "github.com/kidoman/embd/host/all"
|
||||
)
|
||||
|
||||
func main() {
|
||||
@ -25,7 +28,7 @@ func main() {
|
||||
}
|
||||
defer pwm.Close()
|
||||
|
||||
if err := pwm.SetDuty(embd.BBBPWMDefaultPeriod / 2); err != nil {
|
||||
if err := pwm.SetDuty(bbb.PWMDefaultPeriod / 2); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
|
@ -11,6 +11,8 @@ import (
|
||||
"github.com/kidoman/embd"
|
||||
"github.com/kidoman/embd/controller/pca9685"
|
||||
"github.com/kidoman/embd/motion/servo"
|
||||
|
||||
_ "github.com/kidoman/embd/host/all"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -10,6 +10,8 @@ import (
|
||||
|
||||
"github.com/kidoman/embd"
|
||||
"github.com/kidoman/embd/motion/servo"
|
||||
|
||||
_ "github.com/kidoman/embd/host/all"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -10,6 +10,8 @@ import (
|
||||
|
||||
"github.com/kidoman/embd/controller/servoblaster"
|
||||
"github.com/kidoman/embd/motion/servo"
|
||||
|
||||
_ "github.com/kidoman/embd/host/all"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -10,6 +10,8 @@ import (
|
||||
|
||||
"github.com/kidoman/embd"
|
||||
"github.com/kidoman/embd/sensor/tmp006"
|
||||
|
||||
_ "github.com/kidoman/embd/host/all"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -11,6 +11,8 @@ import (
|
||||
|
||||
"github.com/kidoman/embd"
|
||||
"github.com/kidoman/embd/sensor/us020"
|
||||
|
||||
_ "github.com/kidoman/embd/host/all"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
@ -9,6 +9,8 @@ import (
|
||||
"github.com/golang/glog"
|
||||
"github.com/kidoman/embd"
|
||||
"github.com/kidoman/embd/sensor/watersensor"
|
||||
|
||||
_ "github.com/kidoman/embd/host/all"
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
3
utils.go
3
utils.go
@ -2,8 +2,9 @@ package embd
|
||||
|
||||
import "path/filepath"
|
||||
|
||||
// FindFirstMatchingFile finds the first glob match in the filesystem.
|
||||
// Inspiration: https://github.com/mrmorphic/hwio/blob/master/hwio.go#L451
|
||||
func findFirstMatchingFile(glob string) (string, error) {
|
||||
func FindFirstMatchingFile(glob string) (string, error) {
|
||||
matches, err := filepath.Glob(glob)
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
Loading…
x
Reference in New Issue
Block a user