simplify package structure

This commit is contained in:
Karan Misra 2014-03-03 00:51:23 +05:30
parent 3cae4064dc
commit 36f2c0486d
41 changed files with 736 additions and 885 deletions

71
bbb.go Normal file
View File

@ -0,0 +1,71 @@
package embd
func init() {
Describers[HostBBB] = func(rev int) *Descriptor {
return &Descriptor{
GPIO: func() GPIO {
return newGPIODriver(bbbPins)
},
I2C: newI2CDriver,
}
}
}
var bbbPins = PinMap{
&PinDesc{66, []string{"P8_07", "GPIO_66", "TIMER4"}, CapNormal | CapGPMC},
&PinDesc{67, []string{"P8_08", "GPIO_67", "TIMER7"}, CapNormal | CapGPMC},
&PinDesc{69, []string{"P8_09", "GPIO_69", "TIMER5"}, CapNormal | CapGPMC},
&PinDesc{68, []string{"P8_10", "GPIO_68", "TIMER6"}, CapNormal | CapGPMC},
&PinDesc{45, []string{"P8_11", "GPIO_45"}, CapNormal | CapGPMC},
&PinDesc{44, []string{"P8_12", "GPIO_44"}, CapNormal | CapGPMC},
&PinDesc{23, []string{"P8_13", "GPIO_23", "EHRPWM2B"}, CapNormal | CapGPMC},
&PinDesc{26, []string{"P8_14", "GPIO_26"}, CapNormal | CapGPMC},
&PinDesc{47, []string{"P8_15", "GPIO_47"}, CapNormal | CapGPMC},
&PinDesc{46, []string{"P8_16", "GPIO_46"}, CapNormal | CapGPMC},
&PinDesc{27, []string{"P8_17", "GPIO_27"}, CapNormal | CapGPMC},
&PinDesc{65, []string{"P8_18", "GPIO_65"}, CapNormal | CapGPMC},
&PinDesc{22, []string{"P8_19", "GPIO_22", "EHRPWM2A"}, CapNormal | CapGPMC},
&PinDesc{61, []string{"P8_26", "GPIO_61"}, CapNormal | CapGPMC},
&PinDesc{86, []string{"P8_27", "GPIO_86"}, CapNormal | CapLCD},
&PinDesc{88, []string{"P8_28", "GPIO_88"}, CapNormal | CapLCD},
&PinDesc{87, []string{"P8_29", "GPIO_87"}, CapNormal | CapLCD},
&PinDesc{89, []string{"P8_30", "GPIO_89"}, CapNormal | CapLCD},
&PinDesc{10, []string{"P8_31", "GPIO_10", "UART5_CTSN"}, CapNormal | CapLCD},
&PinDesc{11, []string{"P8_32", "GPIO_11", "UART5_RTSN"}, CapNormal | CapLCD},
&PinDesc{9, []string{"P8_33", "GPIO_9 ", "UART4_RTSN"}, CapNormal | CapLCD},
&PinDesc{81, []string{"P8_34", "GPIO_81", "UART3_RTSN"}, CapNormal | CapLCD},
&PinDesc{8, []string{"P8_35", "GPIO_8 ", "UART4_CTSN"}, CapNormal | CapLCD},
&PinDesc{80, []string{"P8_36", "GPIO_80", "UART3_CTSN"}, CapNormal | CapLCD},
&PinDesc{78, []string{"P8_37", "GPIO_78", "UART5_TXD"}, CapNormal | CapLCD},
&PinDesc{79, []string{"P8_38", "GPIO_79", "UART5_RXD"}, CapNormal | CapLCD},
&PinDesc{76, []string{"P8_39", "GPIO_76"}, CapNormal | CapLCD},
&PinDesc{77, []string{"P8_40", "GPIO_77"}, CapNormal | CapLCD},
&PinDesc{74, []string{"P8_41", "GPIO_74"}, CapNormal | CapLCD},
&PinDesc{75, []string{"P8_42", "GPIO_75"}, CapNormal | CapLCD},
&PinDesc{72, []string{"P8_43", "GPIO_72"}, CapNormal | CapLCD},
&PinDesc{73, []string{"P8_44", "GPIO_73"}, CapNormal | CapLCD},
&PinDesc{70, []string{"P8_45", "GPIO_70"}, CapNormal | CapLCD},
&PinDesc{71, []string{"P8_46", "GPIO_71"}, CapNormal | CapLCD},
&PinDesc{30, []string{"P9_11", "GPIO_30", "UART4_RXD"}, CapNormal | CapUART},
&PinDesc{60, []string{"P9_12", "GPIO_60", "GPIO1_28"}, CapNormal},
&PinDesc{31, []string{"P9_13", "GPIO_31", "UART4_TXD"}, CapNormal | CapUART},
&PinDesc{50, []string{"P9_14", "GPIO_50", "EHRPWM1A"}, CapNormal | CapPWM},
&PinDesc{48, []string{"P9_15", "GPIO_48", "GPIO1_16"}, CapNormal},
&PinDesc{51, []string{"P9_16", "GPIO_51", "EHRPWM1B"}, CapNormal | CapPWM},
&PinDesc{5, []string{"P9_17", "GPIO_5", "I2C1_SCL"}, CapNormal | CapI2C},
&PinDesc{4, []string{"P9_18", "GPIO_4", "I2C1_SDA"}, CapNormal | CapI2C},
&PinDesc{13, []string{"P9_19", "GPIO_13", "I2C2_SCL"}, CapNormal | CapI2C},
&PinDesc{12, []string{"P9_20", "GPIO_12", "I2C2_SDA"}, CapNormal | CapI2C},
&PinDesc{3, []string{"P9_21", "GPIO_3", "UART2_TXD"}, CapNormal | CapUART},
&PinDesc{2, []string{"P9_22", "GPIO_2", "UART2_RXD"}, CapNormal | CapUART},
&PinDesc{49, []string{"P9_23", "GPIO_49", "GPIO1_17"}, CapNormal},
&PinDesc{15, []string{"P9_24", "GPIO_15", "UART1_TXD"}, CapNormal | CapUART},
&PinDesc{117, []string{"P9_25", "GPIO_117", "GPIO3_21"}, CapNormal},
&PinDesc{14, []string{"P9_26", "GPIO_14", "UART1_RXD"}, CapNormal | CapUART},
&PinDesc{115, []string{"P9_27", "GPIO_115", "GPIO3_19"}, CapNormal},
&PinDesc{113, []string{"P9_28", "GPIO_113", "SPI1_CS0"}, CapNormal | CapSPI},
&PinDesc{111, []string{"P9_29", "GPIO_111", "SPI1_D0"}, CapNormal | CapSPI},
&PinDesc{112, []string{"P9_30", "GPIO_112", "SPI1_D1"}, CapNormal | CapSPI},
&PinDesc{110, []string{"P9_31", "GPIO_110", "SPI1_SCLK"}, CapNormal | CapSPI},
}

View File

@ -4,8 +4,7 @@ package mcp4725
import (
"log"
"sync"
"github.com/kidoman/embd/i2c"
"github.com/kidoman/embd"
)
const (
@ -20,7 +19,7 @@ const (
// MCP4725 represents a MCP4725 DAC.
type MCP4725 struct {
// Bus to communicate over.
Bus i2c.Bus
Bus embd.I2CBus
// Addr of the sensor.
Addr byte
@ -32,7 +31,7 @@ type MCP4725 struct {
}
// New creates a new MCP4725 sensor.
func New(bus i2c.Bus, addr byte) *MCP4725 {
func New(bus embd.I2CBus, addr byte) *MCP4725 {
return &MCP4725{
Bus: bus,
Addr: addr,

View File

@ -6,8 +6,7 @@ import (
"math"
"sync"
"time"
"github.com/kidoman/embd/i2c"
"github.com/kidoman/embd"
"github.com/kidoman/embd/util"
)
@ -28,7 +27,7 @@ const (
// PCA9685 represents a PCA9685 PWM generator.
type PCA9685 struct {
Bus i2c.Bus
Bus embd.I2CBus
Addr byte
Freq int
@ -39,7 +38,7 @@ type PCA9685 struct {
}
// New creates a new PCA9685 interface.
func New(bus i2c.Bus, addr byte) *PCA9685 {
func New(bus embd.I2CBus, addr byte) *PCA9685 {
return &PCA9685{
Bus: bus,
Addr: addr,

View File

@ -1,18 +1,18 @@
package host
package embd
import "fmt"
type Descriptor struct {
GPIO func() interface{}
I2C func() interface{}
GPIO func() GPIO
I2C func() I2C
}
type Describer func(rev int) *Descriptor
var Describers = map[Host]Describer{}
func Describe() (*Descriptor, error) {
host, rev, err := Detect()
func DescribeHost() (*Descriptor, error) {
host, rev, err := DetectHost()
if err != nil {
return nil, err
}

View File

@ -1,4 +1,4 @@
package host
package embd
import (
"fmt"
@ -10,11 +10,11 @@ import (
type Host int
const (
Null Host = iota
RPi
BBB
CubieTruck
Galileo
HostNull Host = iota
HostRPi
HostBBB
HostCubieTruck
HostGalileo
)
func execOutput(name string, arg ...string) (output string, err error) {
@ -60,20 +60,20 @@ func kernelVersion() (major, minor, patch int, err error) {
return parseVersion(output)
}
func Detect() (Host, int, error) {
func DetectHost() (Host, int, error) {
major, minor, patch, err := kernelVersion()
if err != nil {
return Null, 0, err
return HostNull, 0, err
}
if major < 3 || (major == 3 && minor < 8) {
err = fmt.Errorf("embd: linux kernel versions lower than 3.8 are not supported. you have %v.%v.%v", major, minor, patch)
return Null, 0, err
return HostNull, 0, err
}
node, err := nodeName()
if err != nil {
return Null, 0, err
return HostNull, 0, err
}
var host Host
@ -81,12 +81,12 @@ func Detect() (Host, int, error) {
switch node {
case "raspberrypi":
host = RPi
host = HostRPi
case "beaglebone":
host = BBB
host = HostBBB
default:
err = fmt.Errorf("embd: your host %q is not supported at this moment. please request support at https://github.com/kidoman/embd/issues", node)
return Null, 0, err
return HostNull, 0, err
}
return host, rev, nil

View File

@ -1,4 +1,4 @@
package host
package embd
import "testing"

View File

@ -1,126 +0,0 @@
package gpio
import (
"errors"
"fmt"
"os"
"path"
"github.com/kidoman/embd/gpio"
)
type digitalPin struct {
n int
dir *os.File
val *os.File
activeLow *os.File
edge *os.File
}
func newDigitalPin(n int) (*digitalPin, error) {
p := &digitalPin{n: n}
if err := p.init(); err != nil {
return nil, err
}
return p, nil
}
func (p *digitalPin) init() error {
var err error
if p.dir, err = p.directionFile(); err != nil {
return err
}
if p.val, err = p.valueFile(); err != nil {
return err
}
if p.activeLow, err = p.activeLowFile(); err != nil {
return err
}
return nil
}
func (p *digitalPin) basePath() string {
return fmt.Sprintf("/sys/class/gpio/gpio%v", p.n)
}
func (p *digitalPin) openFile(path string) (*os.File, error) {
return os.OpenFile(path, os.O_RDWR, os.ModeExclusive)
}
func (p *digitalPin) directionFile() (*os.File, error) {
return p.openFile(path.Join(p.basePath(), "direction"))
}
func (p *digitalPin) valueFile() (*os.File, error) {
return p.openFile(path.Join(p.basePath(), "value"))
}
func (p *digitalPin) activeLowFile() (*os.File, error) {
return p.openFile(path.Join(p.basePath(), "active_low"))
}
func (p *digitalPin) SetDirection(dir gpio.Direction) error {
str := "in"
if dir == gpio.Out {
str = "out"
}
_, err := p.dir.WriteString(str)
return err
}
func (p *digitalPin) Read() (int, error) {
buf := make([]byte, 1)
if _, err := p.val.Read(buf); err != nil {
return 0, err
}
var val int
if buf[0] == '1' {
val = 1
}
return val, nil
}
func (p *digitalPin) Write(val int) error {
str := "0"
if val == gpio.High {
str = "1"
}
_, err := p.val.WriteString(str)
return err
}
func (p *digitalPin) ActiveLow(b bool) error {
str := "0"
if b {
str = "1"
}
_, err := p.activeLow.WriteString(str)
return err
}
func (p *digitalPin) PullUp() error {
return errors.New("not implemented")
}
func (p *digitalPin) PullDown() error {
return errors.New("not implemented")
}
func (p *digitalPin) Close() error {
if err := p.dir.Close(); err != nil {
return err
}
if err := p.val.Close(); err != nil {
return err
}
if err := p.activeLow.Close(); err != nil {
return err
}
if err := p.edge.Close(); err != nil {
return err
}
return nil
}

View File

@ -1,154 +0,0 @@
package gpio
import (
"fmt"
"os"
"strconv"
"github.com/golang/glog"
"github.com/kidoman/embd/gpio"
)
const (
Normal int = 1 << iota
I2C
UART
SPI
GPMC
LCD
PWM
)
type PinDesc struct {
N int
IDs []string
Caps int
}
type PinMap []*PinDesc
func (m PinMap) Lookup(k interface{}) (*PinDesc, bool) {
switch key := k.(type) {
case int:
for i := range m {
if m[i].N == key {
return m[i], true
}
}
case string:
for i := range m {
for j := range m[i].IDs {
if m[i].IDs[j] == key {
return m[i], true
}
}
}
}
return nil, false
}
type GPIO struct {
exporter, unexporter *os.File
initialized bool
pinMap PinMap
initializedPins map[int]*digitalPin
}
func New(pinMap PinMap) *GPIO {
return &GPIO{
pinMap: pinMap,
initializedPins: map[int]*digitalPin{},
}
}
func (io *GPIO) init() error {
if io.initialized {
return nil
}
var err error
if io.exporter, err = os.OpenFile("/sys/class/gpio/export", os.O_WRONLY, os.ModeExclusive); err != nil {
return err
}
if io.unexporter, err = os.OpenFile("/sys/class/gpio/unexport", os.O_WRONLY, os.ModeExclusive); err != nil {
return err
}
io.initialized = true
return nil
}
func (io *GPIO) lookupKey(key interface{}) (*PinDesc, bool) {
return io.pinMap.Lookup(key)
}
func (io *GPIO) export(n int) error {
_, err := io.exporter.WriteString(strconv.Itoa(n))
return err
}
func (io *GPIO) unexport(n int) error {
_, err := io.unexporter.WriteString(strconv.Itoa(n))
return err
}
func (io *GPIO) digitalPin(key interface{}) (*digitalPin, error) {
pd, found := io.lookupKey(key)
if !found {
err := fmt.Errorf("gpio: could not find pin matching %q", key)
return nil, err
}
n := pd.N
p, ok := io.initializedPins[n]
if ok {
return p, nil
}
if pd.Caps&Normal == 0 {
err := fmt.Errorf("gpio: sorry, pin %q cannot be used for GPIO", key)
return nil, err
}
if pd.Caps != Normal {
glog.Infof("gpio: pin %q is not a dedicated GPIO pin. please refer to the system reference manual for more details", key)
}
if err := io.export(n); err != nil {
return nil, err
}
p, err := newDigitalPin(n)
if err != nil {
io.unexport(n)
return nil, err
}
io.initializedPins[n] = p
return p, nil
}
func (io *GPIO) DigitalPin(key interface{}) (gpio.DigitalPin, error) {
if err := io.init(); err != nil {
return nil, err
}
return io.digitalPin(key)
}
func (io *GPIO) Close() error {
for n := range io.initializedPins {
io.unexport(n)
}
io.exporter.Close()
io.unexporter.Close()
return nil
}

View File

@ -1,43 +0,0 @@
// Package i2c enables gophers i2c speaking ability.
package i2c
import (
"sync"
"github.com/kidoman/embd/i2c"
)
type I2C struct {
busMap map[byte]*bus
busMapLock sync.Mutex
}
func New() *I2C {
return &I2C{
busMap: make(map[byte]*bus),
}
}
func (i *I2C) Bus(l byte) i2c.Bus {
i.busMapLock.Lock()
defer i.busMapLock.Unlock()
var b *bus
if b = i.busMap[l]; b == nil {
b = &bus{l: l}
i.busMap[l] = b
}
return b
}
func (i *I2C) Close() error {
for _, b := range i.busMap {
b.Close()
delete(i.busMap, b.l)
}
return nil
}

View File

@ -1,6 +1,4 @@
package gpio
import "github.com/kidoman/embd/host"
package embd
type Direction int
@ -27,31 +25,31 @@ type DigitalPin interface {
Close() error
}
type gpio interface {
type GPIO interface {
DigitalPin(key interface{}) (DigitalPin, error)
Close() error
}
var instance gpio
var gpioInstance GPIO
func Open() error {
desc, err := host.Describe()
func InitGPIO() error {
desc, err := DescribeHost()
if err != nil {
return err
}
instance = desc.GPIO().(gpio)
gpioInstance = desc.GPIO()
return nil
}
func Close() error {
return instance.Close()
func CloseGPIO() error {
return gpioInstance.Close()
}
func NewDigitalPin(key interface{}) (DigitalPin, error) {
return instance.DigitalPin(key)
return gpioInstance.DigitalPin(key)
}
func DigitalWrite(key interface{}, val int) error {

271
gpiogeneric.go Normal file
View File

@ -0,0 +1,271 @@
package embd
import (
"errors"
"fmt"
"os"
"path"
"strconv"
"github.com/golang/glog"
)
const (
CapNormal int = 1 << iota
CapI2C
CapUART
CapSPI
CapGPMC
CapLCD
CapPWM
)
type PinDesc struct {
N int
IDs []string
Caps int
}
type PinMap []*PinDesc
func (m PinMap) Lookup(k interface{}) (*PinDesc, bool) {
switch key := k.(type) {
case int:
for i := range m {
if m[i].N == key {
return m[i], true
}
}
case string:
for i := range m {
for j := range m[i].IDs {
if m[i].IDs[j] == key {
return m[i], true
}
}
}
}
return nil, false
}
type gpioDriver struct {
exporter, unexporter *os.File
initialized bool
pinMap PinMap
initializedPins map[int]*digitalPin
}
func newGPIODriver(pinMap PinMap) *gpioDriver {
return &gpioDriver{
pinMap: pinMap,
initializedPins: map[int]*digitalPin{},
}
}
func (io *gpioDriver) init() error {
if io.initialized {
return nil
}
var err error
if io.exporter, err = os.OpenFile("/sys/class/gpio/export", os.O_WRONLY, os.ModeExclusive); err != nil {
return err
}
if io.unexporter, err = os.OpenFile("/sys/class/gpio/unexport", os.O_WRONLY, os.ModeExclusive); err != nil {
return err
}
io.initialized = true
return nil
}
func (io *gpioDriver) lookupKey(key interface{}) (*PinDesc, bool) {
return io.pinMap.Lookup(key)
}
func (io *gpioDriver) export(n int) error {
_, err := io.exporter.WriteString(strconv.Itoa(n))
return err
}
func (io *gpioDriver) unexport(n int) error {
_, err := io.unexporter.WriteString(strconv.Itoa(n))
return err
}
func (io *gpioDriver) digitalPin(key interface{}) (*digitalPin, error) {
pd, found := io.lookupKey(key)
if !found {
err := fmt.Errorf("gpio: could not find pin matching %q", key)
return nil, err
}
n := pd.N
p, ok := io.initializedPins[n]
if ok {
return p, nil
}
if pd.Caps&CapNormal == 0 {
err := fmt.Errorf("gpio: sorry, pin %q cannot be used for GPIO", key)
return nil, err
}
if pd.Caps != CapNormal {
glog.Infof("gpio: pin %q is not a dedicated GPIO pin. please refer to the system reference manual for more details", key)
}
if err := io.export(n); err != nil {
return nil, err
}
p, err := newDigitalPin(n)
if err != nil {
io.unexport(n)
return nil, err
}
io.initializedPins[n] = p
return p, nil
}
func (io *gpioDriver) DigitalPin(key interface{}) (DigitalPin, error) {
if err := io.init(); err != nil {
return nil, err
}
return io.digitalPin(key)
}
func (io *gpioDriver) Close() error {
for n := range io.initializedPins {
io.unexport(n)
}
io.exporter.Close()
io.unexporter.Close()
return nil
}
type digitalPin struct {
n int
dir *os.File
val *os.File
activeLow *os.File
edge *os.File
}
func newDigitalPin(n int) (*digitalPin, error) {
p := &digitalPin{n: n}
if err := p.init(); err != nil {
return nil, err
}
return p, nil
}
func (p *digitalPin) init() error {
var err error
if p.dir, err = p.directionFile(); err != nil {
return err
}
if p.val, err = p.valueFile(); err != nil {
return err
}
if p.activeLow, err = p.activeLowFile(); err != nil {
return err
}
return nil
}
func (p *digitalPin) basePath() string {
return fmt.Sprintf("/sys/class/gpio/gpio%v", p.n)
}
func (p *digitalPin) openFile(path string) (*os.File, error) {
return os.OpenFile(path, os.O_RDWR, os.ModeExclusive)
}
func (p *digitalPin) directionFile() (*os.File, error) {
return p.openFile(path.Join(p.basePath(), "direction"))
}
func (p *digitalPin) valueFile() (*os.File, error) {
return p.openFile(path.Join(p.basePath(), "value"))
}
func (p *digitalPin) activeLowFile() (*os.File, error) {
return p.openFile(path.Join(p.basePath(), "active_low"))
}
func (p *digitalPin) SetDirection(dir Direction) error {
str := "in"
if dir == Out {
str = "out"
}
_, err := p.dir.WriteString(str)
return err
}
func (p *digitalPin) Read() (int, error) {
buf := make([]byte, 1)
if _, err := p.val.Read(buf); err != nil {
return 0, err
}
var val int
if buf[0] == '1' {
val = 1
}
return val, nil
}
func (p *digitalPin) Write(val int) error {
str := "0"
if val == High {
str = "1"
}
_, err := p.val.WriteString(str)
return err
}
func (p *digitalPin) ActiveLow(b bool) error {
str := "0"
if b {
str = "1"
}
_, err := p.activeLow.WriteString(str)
return err
}
func (p *digitalPin) PullUp() error {
return errors.New("not implemented")
}
func (p *digitalPin) PullDown() error {
return errors.New("not implemented")
}
func (p *digitalPin) Close() error {
if err := p.dir.Close(); err != nil {
return err
}
if err := p.val.Close(); err != nil {
return err
}
if err := p.activeLow.Close(); err != nil {
return err
}
if err := p.edge.Close(); err != nil {
return err
}
return nil
}

View File

@ -1,64 +0,0 @@
package bbb
import (
"github.com/kidoman/embd/driver/linux/gpio"
)
var pins = gpio.PinMap{
&gpio.PinDesc{66, []string{"P8_07", "GPIO_66", "TIMER4"}, gpio.Normal | gpio.GPMC},
&gpio.PinDesc{67, []string{"P8_08", "GPIO_67", "TIMER7"}, gpio.Normal | gpio.GPMC},
&gpio.PinDesc{69, []string{"P8_09", "GPIO_69", "TIMER5"}, gpio.Normal | gpio.GPMC},
&gpio.PinDesc{68, []string{"P8_10", "GPIO_68", "TIMER6"}, gpio.Normal | gpio.GPMC},
&gpio.PinDesc{45, []string{"P8_11", "GPIO_45"}, gpio.Normal | gpio.GPMC},
&gpio.PinDesc{44, []string{"P8_12", "GPIO_44"}, gpio.Normal | gpio.GPMC},
&gpio.PinDesc{23, []string{"P8_13", "GPIO_23", "EHRPWM2B"}, gpio.Normal | gpio.GPMC},
&gpio.PinDesc{26, []string{"P8_14", "GPIO_26"}, gpio.Normal | gpio.GPMC},
&gpio.PinDesc{47, []string{"P8_15", "GPIO_47"}, gpio.Normal | gpio.GPMC},
&gpio.PinDesc{46, []string{"P8_16", "GPIO_46"}, gpio.Normal | gpio.GPMC},
&gpio.PinDesc{27, []string{"P8_17", "GPIO_27"}, gpio.Normal | gpio.GPMC},
&gpio.PinDesc{65, []string{"P8_18", "GPIO_65"}, gpio.Normal | gpio.GPMC},
&gpio.PinDesc{22, []string{"P8_19", "GPIO_22", "EHRPWM2A"}, gpio.Normal | gpio.GPMC},
&gpio.PinDesc{61, []string{"P8_26", "GPIO_61"}, gpio.Normal | gpio.GPMC},
&gpio.PinDesc{86, []string{"P8_27", "GPIO_86"}, gpio.Normal | gpio.LCD},
&gpio.PinDesc{88, []string{"P8_28", "GPIO_88"}, gpio.Normal | gpio.LCD},
&gpio.PinDesc{87, []string{"P8_29", "GPIO_87"}, gpio.Normal | gpio.LCD},
&gpio.PinDesc{89, []string{"P8_30", "GPIO_89"}, gpio.Normal | gpio.LCD},
&gpio.PinDesc{10, []string{"P8_31", "GPIO_10", "UART5_CTSN"}, gpio.Normal | gpio.LCD},
&gpio.PinDesc{11, []string{"P8_32", "GPIO_11", "UART5_RTSN"}, gpio.Normal | gpio.LCD},
&gpio.PinDesc{9, []string{"P8_33", "GPIO_9 ", "UART4_RTSN"}, gpio.Normal | gpio.LCD},
&gpio.PinDesc{81, []string{"P8_34", "GPIO_81", "UART3_RTSN"}, gpio.Normal | gpio.LCD},
&gpio.PinDesc{8, []string{"P8_35", "GPIO_8 ", "UART4_CTSN"}, gpio.Normal | gpio.LCD},
&gpio.PinDesc{80, []string{"P8_36", "GPIO_80", "UART3_CTSN"}, gpio.Normal | gpio.LCD},
&gpio.PinDesc{78, []string{"P8_37", "GPIO_78", "UART5_TXD"}, gpio.Normal | gpio.LCD},
&gpio.PinDesc{79, []string{"P8_38", "GPIO_79", "UART5_RXD"}, gpio.Normal | gpio.LCD},
&gpio.PinDesc{76, []string{"P8_39", "GPIO_76"}, gpio.Normal | gpio.LCD},
&gpio.PinDesc{77, []string{"P8_40", "GPIO_77"}, gpio.Normal | gpio.LCD},
&gpio.PinDesc{74, []string{"P8_41", "GPIO_74"}, gpio.Normal | gpio.LCD},
&gpio.PinDesc{75, []string{"P8_42", "GPIO_75"}, gpio.Normal | gpio.LCD},
&gpio.PinDesc{72, []string{"P8_43", "GPIO_72"}, gpio.Normal | gpio.LCD},
&gpio.PinDesc{73, []string{"P8_44", "GPIO_73"}, gpio.Normal | gpio.LCD},
&gpio.PinDesc{70, []string{"P8_45", "GPIO_70"}, gpio.Normal | gpio.LCD},
&gpio.PinDesc{71, []string{"P8_46", "GPIO_71"}, gpio.Normal | gpio.LCD},
&gpio.PinDesc{30, []string{"P9_11", "GPIO_30", "UART4_RXD"}, gpio.Normal | gpio.UART},
&gpio.PinDesc{60, []string{"P9_12", "GPIO_60", "GPIO1_28"}, gpio.Normal},
&gpio.PinDesc{31, []string{"P9_13", "GPIO_31", "UART4_TXD"}, gpio.Normal | gpio.UART},
&gpio.PinDesc{50, []string{"P9_14", "GPIO_50", "EHRPWM1A"}, gpio.Normal | gpio.PWM},
&gpio.PinDesc{48, []string{"P9_15", "GPIO_48", "GPIO1_16"}, gpio.Normal},
&gpio.PinDesc{51, []string{"P9_16", "GPIO_51", "EHRPWM1B"}, gpio.Normal | gpio.PWM},
&gpio.PinDesc{5, []string{"P9_17", "GPIO_5", "I2C1_SCL"}, gpio.Normal | gpio.I2C},
&gpio.PinDesc{4, []string{"P9_18", "GPIO_4", "I2C1_SDA"}, gpio.Normal | gpio.I2C},
&gpio.PinDesc{13, []string{"P9_19", "GPIO_13", "I2C2_SCL"}, gpio.Normal | gpio.I2C},
&gpio.PinDesc{12, []string{"P9_20", "GPIO_12", "I2C2_SDA"}, gpio.Normal | gpio.I2C},
&gpio.PinDesc{3, []string{"P9_21", "GPIO_3", "UART2_TXD"}, gpio.Normal | gpio.UART},
&gpio.PinDesc{2, []string{"P9_22", "GPIO_2", "UART2_RXD"}, gpio.Normal | gpio.UART},
&gpio.PinDesc{49, []string{"P9_23", "GPIO_49", "GPIO1_17"}, gpio.Normal},
&gpio.PinDesc{15, []string{"P9_24", "GPIO_15", "UART1_TXD"}, gpio.Normal | gpio.UART},
&gpio.PinDesc{117, []string{"P9_25", "GPIO_117", "GPIO3_21"}, gpio.Normal},
&gpio.PinDesc{14, []string{"P9_26", "GPIO_14", "UART1_RXD"}, gpio.Normal | gpio.UART},
&gpio.PinDesc{115, []string{"P9_27", "GPIO_115", "GPIO3_19"}, gpio.Normal},
&gpio.PinDesc{113, []string{"P9_28", "GPIO_113", "SPI1_CS0"}, gpio.Normal | gpio.SPI},
&gpio.PinDesc{111, []string{"P9_29", "GPIO_111", "SPI1_D0"}, gpio.Normal | gpio.SPI},
&gpio.PinDesc{112, []string{"P9_30", "GPIO_112", "SPI1_D1"}, gpio.Normal | gpio.SPI},
&gpio.PinDesc{110, []string{"P9_31", "GPIO_110", "SPI1_SCLK"}, gpio.Normal | gpio.SPI},
}

View File

@ -1,22 +0,0 @@
package bbb
import (
lgpio "github.com/kidoman/embd/driver/linux/gpio"
li2c "github.com/kidoman/embd/driver/linux/i2c"
"github.com/kidoman/embd/host"
)
func init() {
host.Describers[host.BBB] = describer
}
func describer(rev int) *host.Descriptor {
return &host.Descriptor{
GPIO: func() interface{} {
return lgpio.New(pins)
},
I2C: func() interface{} {
return li2c.New()
},
}
}

View File

@ -1,45 +0,0 @@
package rpi
import (
"github.com/kidoman/embd/driver/linux/gpio"
)
var rev1Pins = gpio.PinMap{
&gpio.PinDesc{0, []string{"P1_3", "GPIO_0", "SDA", "I2C0_SDA"}, gpio.Normal | gpio.I2C},
&gpio.PinDesc{1, []string{"P1_5", "GPIO_1", "SCL", "I2C0_SCL"}, gpio.Normal | gpio.I2C},
&gpio.PinDesc{4, []string{"P1_7", "GPIO_4", "GPCLK0"}, gpio.Normal},
&gpio.PinDesc{14, []string{"P1_8", "GPIO_14", "TXD", "UART0_TXD"}, gpio.Normal | gpio.UART},
&gpio.PinDesc{15, []string{"P1_10", "GPIO_15", "RXD", "UART0_RXD"}, gpio.Normal | gpio.UART},
&gpio.PinDesc{17, []string{"P1_11", "GPIO_17"}, gpio.Normal},
&gpio.PinDesc{18, []string{"P1_12", "GPIO_18", "PCM_CLK"}, gpio.Normal},
&gpio.PinDesc{21, []string{"P1_13", "GPIO_21"}, gpio.Normal},
&gpio.PinDesc{22, []string{"P1_15", "GPIO_22"}, gpio.Normal},
&gpio.PinDesc{23, []string{"P1_16", "GPIO_23"}, gpio.Normal},
&gpio.PinDesc{24, []string{"P1_18", "GPIO_24"}, gpio.Normal},
&gpio.PinDesc{10, []string{"P1_19", "GPIO_10", "MOSI", "SPI0_MOSI"}, gpio.Normal | gpio.SPI},
&gpio.PinDesc{9, []string{"P1_21", "GPIO_9", "MISO", "SPI0_MISO"}, gpio.Normal | gpio.SPI},
&gpio.PinDesc{25, []string{"P1_22", "GPIO_25"}, gpio.Normal},
&gpio.PinDesc{11, []string{"P1_23", "GPIO_11", "SCLK", "SPI0_SCLK"}, gpio.Normal | gpio.SPI},
&gpio.PinDesc{8, []string{"P1_24", "GPIO_8", "CE0", "SPI0_CE0_N"}, gpio.Normal | gpio.SPI},
&gpio.PinDesc{7, []string{"P1_26", "GPIO_7", "CE1", "SPI0_CE1_N"}, gpio.Normal | gpio.SPI},
}
var rev2Pins = gpio.PinMap{
&gpio.PinDesc{2, []string{"P1_3", "GPIO_2", "SDA", "I2C1_SDA"}, gpio.Normal | gpio.I2C},
&gpio.PinDesc{3, []string{"P1_5", "GPIO_3", "SCL", "I2C1_SCL"}, gpio.Normal | gpio.I2C},
&gpio.PinDesc{4, []string{"P1_7", "GPIO_4", "GPCLK0"}, gpio.Normal},
&gpio.PinDesc{14, []string{"P1_8", "GPIO_14", "TXD", "UART0_TXD"}, gpio.Normal | gpio.UART},
&gpio.PinDesc{15, []string{"P1_10", "GPIO_15", "RXD", "UART0_RXD"}, gpio.Normal | gpio.UART},
&gpio.PinDesc{17, []string{"P1_11", "GPIO_17"}, gpio.Normal},
&gpio.PinDesc{18, []string{"P1_12", "GPIO_18", "PCM_CLK"}, gpio.Normal},
&gpio.PinDesc{27, []string{"P1_13", "GPIO_27"}, gpio.Normal},
&gpio.PinDesc{22, []string{"P1_15", "GPIO_22"}, gpio.Normal},
&gpio.PinDesc{23, []string{"P1_16", "GPIO_23"}, gpio.Normal},
&gpio.PinDesc{24, []string{"P1_18", "GPIO_24"}, gpio.Normal},
&gpio.PinDesc{10, []string{"P1_19", "GPIO_10", "MOSI", "SPI0_MOSI"}, gpio.Normal | gpio.SPI},
&gpio.PinDesc{9, []string{"P1_21", "GPIO_9", "MISO", "SPI0_MISO"}, gpio.Normal | gpio.SPI},
&gpio.PinDesc{25, []string{"P1_22", "GPIO_25"}, gpio.Normal},
&gpio.PinDesc{11, []string{"P1_23", "GPIO_11", "SCLK", "SPI0_SCLK"}, gpio.Normal | gpio.SPI},
&gpio.PinDesc{8, []string{"P1_24", "GPIO_8", "CE0", "SPI0_CE0_N"}, gpio.Normal | gpio.SPI},
&gpio.PinDesc{7, []string{"P1_26", "GPIO_7", "CE1", "SPI0_CE1_N"}, gpio.Normal | gpio.SPI},
}

View File

@ -1,27 +0,0 @@
package rpi
import (
lgpio "github.com/kidoman/embd/driver/linux/gpio"
li2c "github.com/kidoman/embd/driver/linux/i2c"
"github.com/kidoman/embd/host"
)
func init() {
host.Describers[host.RPi] = describer
}
func describer(rev int) *host.Descriptor {
var pins = rev1Pins
if rev > 1 {
pins = rev2Pins
}
return &host.Descriptor{
GPIO: func() interface{} {
return lgpio.New(pins)
},
I2C: func() interface{} {
return li2c.New()
},
}
}

View File

@ -1,9 +1,6 @@
// Package i2c enables gophers i2c speaking ability.
package i2c
package embd
import "github.com/kidoman/embd/host"
type Bus interface {
type I2CBus interface {
// ReadByte reads a byte from the given address.
ReadByte(addr byte) (value byte, err error)
// WriteByte writes a byte to the given address.
@ -27,28 +24,28 @@ type Bus interface {
}
type I2C interface {
Bus(l byte) Bus
Bus(l byte) I2CBus
Close() error
}
var instance I2C
var i2cInstance I2C
func Open() error {
desc, err := host.Describe()
func InitI2C() error {
desc, err := DescribeHost()
if err != nil {
return err
}
instance = desc.I2C().(I2C)
i2cInstance = desc.I2C()
return nil
}
func Close() error {
return instance.Close()
func CloseI2C() error {
return i2cInstance.Close()
}
func NewBus(l byte) Bus {
return instance.Bus(l)
func NewI2CBus(l byte) I2CBus {
return i2cInstance.Bus(l)
}

View File

@ -1,4 +1,4 @@
package i2c
package embd
import (
"fmt"
@ -10,6 +10,41 @@ import (
"unsafe"
)
type i2cDriver struct {
busMap map[byte]*i2cBus
busMapLock sync.Mutex
}
func newI2CDriver() I2C {
return &i2cDriver{
busMap: make(map[byte]*i2cBus),
}
}
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
}
return b
}
func (i *i2cDriver) Close() error {
for _, b := range i.busMap {
b.Close()
delete(i.busMap, b.l)
}
return nil
}
const (
delay = 20
@ -31,15 +66,15 @@ type i2c_rdwr_ioctl_data struct {
nmsg uint32
}
type bus struct {
type i2cBus struct {
l byte
file *os.File
addr byte
mu sync.Mutex
}
func newBus(l byte) (*bus, error) {
b := &bus{l: l}
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 {
@ -49,14 +84,14 @@ func newBus(l byte) (*bus, error) {
return b, nil
}
func (b *bus) Close() error {
func (b *i2cBus) Close() error {
b.mu.Lock()
defer b.mu.Unlock()
return b.file.Close()
}
func (b *bus) setAddress(addr byte) (err error) {
func (b *i2cBus) setAddress(addr byte) (err error) {
if addr != b.addr {
if _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, b.file.Fd(), slaveCmd, uintptr(addr)); errno != 0 {
err = syscall.Errno(errno)
@ -69,7 +104,7 @@ func (b *bus) setAddress(addr byte) (err error) {
return
}
func (b *bus) ReadByte(addr byte) (value byte, err error) {
func (b *i2cBus) ReadByte(addr byte) (value byte, err error) {
b.mu.Lock()
defer b.mu.Unlock()
@ -89,7 +124,7 @@ func (b *bus) ReadByte(addr byte) (value byte, err error) {
return
}
func (b *bus) WriteByte(addr, value byte) (err error) {
func (b *i2cBus) WriteByte(addr, value byte) (err error) {
b.mu.Lock()
defer b.mu.Unlock()
@ -106,7 +141,7 @@ func (b *bus) WriteByte(addr, value byte) (err error) {
return
}
func (b *bus) WriteBytes(addr byte, value []byte) (err error) {
func (b *i2cBus) WriteBytes(addr byte, value []byte) (err error) {
b.mu.Lock()
defer b.mu.Unlock()
@ -130,7 +165,7 @@ func (b *bus) WriteBytes(addr byte, value []byte) (err error) {
return nil
}
func (b *bus) ReadFromReg(addr, reg byte, value []byte) (err error) {
func (b *i2cBus) ReadFromReg(addr, reg byte, value []byte) (err error) {
b.mu.Lock()
defer b.mu.Unlock()
@ -163,7 +198,7 @@ func (b *bus) ReadFromReg(addr, reg byte, value []byte) (err error) {
return nil
}
func (b *bus) ReadByteFromReg(addr, reg byte) (value byte, err error) {
func (b *i2cBus) ReadByteFromReg(addr, reg byte) (value byte, err error) {
buf := make([]byte, 1)
if err = b.ReadFromReg(addr, reg, buf); err != nil {
return
@ -172,7 +207,7 @@ func (b *bus) ReadByteFromReg(addr, reg byte) (value byte, err error) {
return
}
func (b *bus) ReadWordFromReg(addr, reg byte) (value uint16, err error) {
func (b *i2cBus) ReadWordFromReg(addr, reg byte) (value uint16, err error) {
buf := make([]byte, 2)
if err = b.ReadFromReg(addr, reg, buf); err != nil {
return
@ -181,7 +216,7 @@ func (b *bus) ReadWordFromReg(addr, reg byte) (value uint16, err error) {
return
}
func (b *bus) WriteToReg(addr, reg byte, value []byte) (err error) {
func (b *i2cBus) WriteToReg(addr, reg byte, value []byte) (err error) {
b.mu.Lock()
defer b.mu.Unlock()
@ -212,7 +247,7 @@ func (b *bus) WriteToReg(addr, reg byte, value []byte) (err error) {
return
}
func (b *bus) WriteByteToReg(addr, reg, value byte) (err error) {
func (b *i2cBus) WriteByteToReg(addr, reg, value byte) (err error) {
b.mu.Lock()
defer b.mu.Unlock()
@ -244,7 +279,7 @@ func (b *bus) WriteByteToReg(addr, reg, value byte) (err error) {
return
}
func (b *bus) WriteWordToReg(addr, reg byte, value uint16) (err error) {
func (b *i2cBus) WriteWordToReg(addr, reg byte, value uint16) (err error) {
b.mu.Lock()
defer b.mu.Unlock()

57
rpi.go Normal file
View File

@ -0,0 +1,57 @@
package embd
func init() {
Describers[HostRPi] = func(rev int) *Descriptor {
var pins = rpiRev1Pins
if rev > 1 {
pins = rpiRev2Pins
}
return &Descriptor{
GPIO: func() GPIO {
return newGPIODriver(pins)
},
I2C: newI2CDriver,
}
}
}
var rpiRev1Pins = PinMap{
&PinDesc{0, []string{"P1_3", "GPIO_0", "SDA", "I2C0_SDA"}, CapNormal | CapI2C},
&PinDesc{1, []string{"P1_5", "GPIO_1", "SCL", "I2C0_SCL"}, CapNormal | CapI2C},
&PinDesc{4, []string{"P1_7", "GPIO_4", "GPCLK0"}, CapNormal},
&PinDesc{14, []string{"P1_8", "GPIO_14", "TXD", "UART0_TXD"}, CapNormal | CapUART},
&PinDesc{15, []string{"P1_10", "GPIO_15", "RXD", "UART0_RXD"}, CapNormal | CapUART},
&PinDesc{17, []string{"P1_11", "GPIO_17"}, CapNormal},
&PinDesc{18, []string{"P1_12", "GPIO_18", "PCM_CLK"}, CapNormal},
&PinDesc{21, []string{"P1_13", "GPIO_21"}, CapNormal},
&PinDesc{22, []string{"P1_15", "GPIO_22"}, CapNormal},
&PinDesc{23, []string{"P1_16", "GPIO_23"}, CapNormal},
&PinDesc{24, []string{"P1_18", "GPIO_24"}, CapNormal},
&PinDesc{10, []string{"P1_19", "GPIO_10", "MOSI", "SPI0_MOSI"}, CapNormal | CapSPI},
&PinDesc{9, []string{"P1_21", "GPIO_9", "MISO", "SPI0_MISO"}, CapNormal | CapSPI},
&PinDesc{25, []string{"P1_22", "GPIO_25"}, CapNormal},
&PinDesc{11, []string{"P1_23", "GPIO_11", "SCLK", "SPI0_SCLK"}, CapNormal | CapSPI},
&PinDesc{8, []string{"P1_24", "GPIO_8", "CE0", "SPI0_CE0_N"}, CapNormal | CapSPI},
&PinDesc{7, []string{"P1_26", "GPIO_7", "CE1", "SPI0_CE1_N"}, CapNormal | CapSPI},
}
var rpiRev2Pins = PinMap{
&PinDesc{2, []string{"P1_3", "GPIO_2", "SDA", "I2C1_SDA"}, CapNormal | CapI2C},
&PinDesc{3, []string{"P1_5", "GPIO_3", "SCL", "I2C1_SCL"}, CapNormal | CapI2C},
&PinDesc{4, []string{"P1_7", "GPIO_4", "GPCLK0"}, CapNormal},
&PinDesc{14, []string{"P1_8", "GPIO_14", "TXD", "UART0_TXD"}, CapNormal | CapUART},
&PinDesc{15, []string{"P1_10", "GPIO_15", "RXD", "UART0_RXD"}, CapNormal | CapUART},
&PinDesc{17, []string{"P1_11", "GPIO_17"}, CapNormal},
&PinDesc{18, []string{"P1_12", "GPIO_18", "PCM_CLK"}, CapNormal},
&PinDesc{27, []string{"P1_13", "GPIO_27"}, CapNormal},
&PinDesc{22, []string{"P1_15", "GPIO_22"}, CapNormal},
&PinDesc{23, []string{"P1_16", "GPIO_23"}, CapNormal},
&PinDesc{24, []string{"P1_18", "GPIO_24"}, CapNormal},
&PinDesc{10, []string{"P1_19", "GPIO_10", "MOSI", "SPI0_MOSI"}, CapNormal | CapSPI},
&PinDesc{9, []string{"P1_21", "GPIO_9", "MISO", "SPI0_MISO"}, CapNormal | CapSPI},
&PinDesc{25, []string{"P1_22", "GPIO_25"}, CapNormal},
&PinDesc{11, []string{"P1_23", "GPIO_11", "SCLK", "SPI0_SCLK"}, CapNormal | CapSPI},
&PinDesc{8, []string{"P1_24", "GPIO_8", "CE0", "SPI0_CE0_N"}, CapNormal | CapSPI},
&PinDesc{7, []string{"P1_26", "GPIO_7", "CE1", "SPI0_CE1_N"}, CapNormal | CapSPI},
}

View File

@ -6,17 +6,17 @@ import (
"log"
"time"
"github.com/kidoman/embd/i2c"
"github.com/kidoman/embd"
"github.com/kidoman/embd/sensor/bh1750fvi"
)
func main() {
if err := i2c.Open(); err != nil {
if err := embd.InitI2C(); err != nil {
panic(err)
}
defer i2c.Close()
defer embd.CloseI2C()
bus := i2c.NewBus(1)
<