1
0
mirror of https://github.com/kidoman/embd synced 2024-12-22 04:40:04 +01:00

gpio: pwm support for bbb

This commit is contained in:
Kunal Powar 2014-03-28 08:24:42 +05:30 committed by kunalpowar
parent 8d9f991fb0
commit 14b078a43e
7 changed files with 329 additions and 28 deletions

260
bbb.go
View File

@ -1,25 +1,28 @@
// BeagleBone Black support.
// The following features are supported on Linux kernel 3.8+
//
// GPIO (both digital (rw) and analog (ro))
// GPIO (digital (rw), analog (ro), pwm)
// I2C
// LED
package embd
import (
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"strconv"
"strings"
"time"
)
func init() {
Register(HostBBB, func(rev int) *Descriptor {
return &Descriptor{
GPIODriver: func() GPIODriver {
return newGPIODriver(bbbPins, newDigitalPin, newBBBAnalogPin)
return newGPIODriver(bbbPins, newDigitalPin, newBBBAnalogPin, newBBBPWMPin)
},
I2CDriver: newI2CDriver,
LEDDriver: func() LEDDriver {
@ -104,6 +107,53 @@ var bbbLEDMap = LEDMap{
"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 needs a little more work.
func bbbEnsureFeatureDisabled(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
}
type bbbAnalogPin struct {
n int
@ -139,27 +189,7 @@ func (p *bbbAnalogPin) init() error {
}
func (p *bbbAnalogPin) ensureEnabled() 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, "cape-bone-iio") {
return nil
}
// Not initialized yet
slots, err := os.OpenFile(file, os.O_WRONLY, os.ModeExclusive)
if err != nil {
return err
}
defer slots.Close()
_, err = slots.WriteString("cape-bone-iio")
return err
return bbbEnsureFeatureEnabled("cape-bone-iio")
}
func (p *bbbAnalogPin) valueFilePath() (string, error) {
@ -207,3 +237,187 @@ func (p *bbbAnalogPin) Close() error {
return nil
}
const BBBPWMDefaultPolarity = Positive
const BBBPWMDefaultDuty = 0
const BBBPWMDefaultPeriod = 500000
const BBBPWMMaxPulseWidth = 1000000000
type bbbPWMPin struct {
n string
duty *os.File
period *os.File
polarity *os.File
initialized bool
}
func newBBBPWMPin(n string) PWMPin {
return &bbbPWMPin{n: n}
}
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.period, err = p.periodFile(basePath); err != nil {
return err
}
if p.duty, err = p.dutyFile(basePath); err != nil {
return err
}
if p.polarity, err = p.polarityFile(basePath); err != nil {
return err
}
p.initialized = true
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 nil
// 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.period.WriteString(strconv.Itoa(ns))
return err
}
func (p *bbbPWMPin) SetDuty(ns int) error {
if err := p.init(); err != nil {
return err
}
if ns > BBBPWMMaxPulseWidth {
return fmt.Errorf("embd: pwm duty for %v is out of bounds (must be =< %vns)", p.n, BBBPWMMaxPulseWidth)
}
_, err := p.duty.WriteString(strconv.Itoa(ns))
return err
}
func (p *bbbPWMPin) SetPolarity(pol Polarity) error {
if err := p.init(); err != nil {
return err
}
_, err := p.polarity.WriteString(strconv.Itoa(int(pol)))
return err
}
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 !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
}

38
gpio.go
View File

@ -61,6 +61,35 @@ type AnalogPin interface {
Close() error
}
// The Polarity type indicates the polarity of a pwm pin.
type Polarity int
const (
// Positive represents (default) positive polarity.
Positive Polarity = iota
// Negative represents negative polarity.
Negative
)
// PWMPin implements access to a pwm capable GPIO pin.
type PWMPin interface {
// N returns the logical PWM id.
N() string
// SetPeriod sets the period of a pwm pin.
SetPeriod(ns int) error
// SetDuty sets the duty of a pwm pin.
SetDuty(ns int) error
// SetPolarity sets the polarity of a pwm pin.
SetPolarity(pol Polarity) error
// Close releases the resources associated with the pin.
Close() error
}
// GPIODriver implements a generic GPIO driver.
type GPIODriver interface {
// DigitalPin returns a pin capable of doing digital IO.
@ -69,6 +98,9 @@ type GPIODriver interface {
// AnalogPin returns a pin capable of doing analog IO.
AnalogPin(key interface{}) (AnalogPin, error)
// PWMPin returns a pin capable of generating PWM.
PWMPin(key interface{}) (PWMPin, error)
// Close releases the resources associated with the driver.
Close() error
}
@ -178,3 +210,9 @@ func AnalogRead(key interface{}) (int, error) {
return pin.Read()
}
// NewPWMPin returns a PWMPin interface which allows PWM signal
// generation over a the PWM pin.
func NewPWMPin(key interface{}) (PWMPin, error) {
return gpioDriverInstance.PWMPin(key)
}

View File

@ -16,15 +16,17 @@ type gpioDriver struct {
dpf func(n int) DigitalPin
apf func(n int) AnalogPin
ppf func(n string) PWMPin
initializedPins map[string]pin
}
func newGPIODriver(pinMap PinMap, dpf func(n int) DigitalPin, apf func(n int) AnalogPin) GPIODriver {
func newGPIODriver(pinMap PinMap, dpf func(n int) DigitalPin, apf func(n int) AnalogPin, ppf func(n string) PWMPin) GPIODriver {
return &gpioDriver{
pinMap: pinMap,
dpf: dpf,
apf: apf,
ppf: ppf,
initializedPins: map[string]pin{},
}
@ -62,6 +64,22 @@ func (io *gpioDriver) AnalogPin(key interface{}) (AnalogPin, error) {
return p, nil
}
func (io *gpioDriver) PWMPin(key interface{}) (PWMPin, error) {
if io.ppf == nil {
return nil, errors.New("gpio: pwm not supported on this host")
}
pd, found := io.pinMap.Lookup(key, CapPWM)
if !found {
return nil, fmt.Errorf("gpio: could not find pin matching %v", key)
}
p := io.ppf(pd.ID)
io.initializedPins[pd.ID] = p
return p, nil
}
func (io *gpioDriver) Close() error {
for _, p := range io.initializedPins {
if err := p.Close(); err != nil {

View File

@ -52,7 +52,7 @@ func TestGpioDriverDigitalPin(t *testing.T) {
var pinMap = PinMap{
&PinDesc{ID: "P1_1", Aliases: []string{"1"}, Caps: CapDigital, DigitalLogical: 1},
}
driver := newGPIODriver(pinMap, newFakeDigitalPin, nil)
driver := newGPIODriver(pinMap, newFakeDigitalPin, nil, nil)
for _, test := range tests {
pin, err := driver.DigitalPin(test.key)
if err != nil {
@ -99,7 +99,7 @@ func TestGpioDriverAnalogPin(t *testing.T) {
var pinMap = PinMap{
&PinDesc{ID: "P1_1", Aliases: []string{"1"}, Caps: CapAnalog, AnalogLogical: 1},
}
driver := newGPIODriver(pinMap, nil, newFakeAnalogPin)
driver := newGPIODriver(pinMap, nil, newFakeAnalogPin, nil)
for _, test := range tests {
pin, err := driver.AnalogPin(test.key)
if err != nil {
@ -117,7 +117,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)
driver := newGPIODriver(pinMap, nil, nil, nil)
_, err := driver.DigitalPin(1)
if err == nil {
t.Fatal("Looking up digital pin 1: did not get error")

2
rpi.go
View File

@ -15,7 +15,7 @@ func init() {
return &Descriptor{
GPIODriver: func() GPIODriver {
return newGPIODriver(pins, newDigitalPin, nil)
return newGPIODriver(pins, newDigitalPin, nil, nil)
},
I2CDriver: newI2CDriver,
}

1
samples/.gitignore vendored
View File

@ -13,6 +13,7 @@ ledshort
lsm303
mcp4725
pca9685
pwm
servo
servoblaster
tmp006

30
samples/pwm.go Normal file
View File

@ -0,0 +1,30 @@
// +build ignore
// PWM example, works OOTB on a BBB.
package main
import (
"time"
"github.com/kidoman/embd"
)
func main() {
if err := embd.InitGPIO(); err != nil {
panic(err)
}
defer embd.CloseGPIO()
pwm, err := embd.NewPWMPin("P9_14")
if err != nil {
panic(err)
}
defer pwm.Close()
if err := pwm.SetDuty(embd.BBBPWMDefaultPeriod / 2); err != nil {
panic(err)
}
time.Sleep(1 * time.Second)
}