mirror of
https://github.com/kidoman/embd
synced 2025-01-03 10:31:36 +01:00
spi: got rid of shouldInitialize arg in spibus (redundant).
This commit is contained in:
parent
f80e6f624d
commit
454eca42d0
@ -180,7 +180,7 @@ func init() {
|
|||||||
return embd.NewLEDDriver(ledMap, generic.NewLED)
|
return embd.NewLEDDriver(ledMap, generic.NewLED)
|
||||||
},
|
},
|
||||||
SPIDriver: func() embd.SPIDriver {
|
SPIDriver: func() embd.SPIDriver {
|
||||||
return embd.NewSPIDriver(spiDeviceMinor, generic.NewSPIBus, true, spiInitializer)
|
return embd.NewSPIDriver(spiDeviceMinor, generic.NewSPIBus, spiInitializer)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -54,24 +54,22 @@ type spiBus struct {
|
|||||||
spiTransferData spiIOCTransfer
|
spiTransferData spiIOCTransfer
|
||||||
initialized bool
|
initialized bool
|
||||||
|
|
||||||
shouldInitialize bool
|
initializer func() error
|
||||||
initializer func() error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func spiIOCMessageN(n uint32) uint32 {
|
func spiIOCMessageN(n uint32) uint32 {
|
||||||
return (spiIOCMessage0 + (n * spiIOCIncrementor))
|
return (spiIOCMessage0 + (n * spiIOCIncrementor))
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSPIBus(spiDevMinor, mode, channel byte, speed, bpw, delay int, shouldInitialize bool, i func() error) embd.SPIBus {
|
func NewSPIBus(spiDevMinor, mode, channel byte, speed, bpw, delay int, i func() error) embd.SPIBus {
|
||||||
return &spiBus{
|
return &spiBus{
|
||||||
spiDevMinor: spiDevMinor,
|
spiDevMinor: spiDevMinor,
|
||||||
mode: mode,
|
mode: mode,
|
||||||
channel: channel,
|
channel: channel,
|
||||||
speed: speed,
|
speed: speed,
|
||||||
bpw: bpw,
|
bpw: bpw,
|
||||||
delayms: delay,
|
delayms: delay,
|
||||||
shouldInitialize: shouldInitialize,
|
initializer: i,
|
||||||
initializer: i,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,11 +78,10 @@ func (b *spiBus) init() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if b.shouldInitialize {
|
if b.initializer != nil {
|
||||||
if err := b.initializer(); err != nil {
|
if err := b.initializer(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
b.shouldInitialize = false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
@ -93,20 +90,17 @@ func (b *spiBus) init() error {
|
|||||||
}
|
}
|
||||||
glog.V(3).Infof("spi: sucessfully opened file /dev/spidev%v.%v", b.spiDevMinor, b.channel)
|
glog.V(3).Infof("spi: sucessfully opened file /dev/spidev%v.%v", b.spiDevMinor, b.channel)
|
||||||
|
|
||||||
err = b.setMode()
|
if err = b.setMode(); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
b.spiTransferData = spiIOCTransfer{}
|
b.spiTransferData = spiIOCTransfer{}
|
||||||
|
|
||||||
err = b.setSpeed()
|
if err = b.setSpeed(); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = b.setBPW()
|
if err = b.setBPW(); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,7 +77,7 @@ func init() {
|
|||||||
return embd.NewLEDDriver(ledMap, generic.NewLED)
|
return embd.NewLEDDriver(ledMap, generic.NewLED)
|
||||||
},
|
},
|
||||||
SPIDriver: func() embd.SPIDriver {
|
SPIDriver: func() embd.SPIDriver {
|
||||||
return embd.NewSPIDriver(spiDeviceMinor, generic.NewSPIBus, false, nil)
|
return embd.NewSPIDriver(spiDeviceMinor, generic.NewSPIBus, nil)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
18
spidriver.go
18
spidriver.go
@ -2,12 +2,11 @@ package embd
|
|||||||
|
|
||||||
import "sync"
|
import "sync"
|
||||||
|
|
||||||
type spiBusFactory func(byte, byte, byte, int, int, int, bool, func() error) SPIBus
|
type spiBusFactory func(byte, byte, byte, int, int, int, func() error) SPIBus
|
||||||
|
|
||||||
type spiDriver struct {
|
type spiDriver struct {
|
||||||
spiDevMinor byte
|
spiDevMinor byte
|
||||||
shouldInitialize bool
|
initializer func() error
|
||||||
initializer func() error
|
|
||||||
|
|
||||||
busMap map[byte]SPIBus
|
busMap map[byte]SPIBus
|
||||||
busMapLock sync.Mutex
|
busMapLock sync.Mutex
|
||||||
@ -15,12 +14,11 @@ type spiDriver struct {
|
|||||||
sbf spiBusFactory
|
sbf spiBusFactory
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSPIDriver(spiDevMinor byte, sbf spiBusFactory, shouldInitialize bool, i func() error) SPIDriver {
|
func NewSPIDriver(spiDevMinor byte, sbf spiBusFactory, i func() error) SPIDriver {
|
||||||
return &spiDriver{
|
return &spiDriver{
|
||||||
spiDevMinor: spiDevMinor,
|
spiDevMinor: spiDevMinor,
|
||||||
sbf: sbf,
|
sbf: sbf,
|
||||||
shouldInitialize: shouldInitialize,
|
initializer: i,
|
||||||
initializer: i,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -28,7 +26,7 @@ func (s *spiDriver) Bus(mode, channel byte, speed, bpw, delay int) SPIBus {
|
|||||||
s.busMapLock.Lock()
|
s.busMapLock.Lock()
|
||||||
defer s.busMapLock.Unlock()
|
defer s.busMapLock.Unlock()
|
||||||
|
|
||||||
b := s.sbf(s.spiDevMinor, mode, channel, speed, bpw, delay, s.shouldInitialize, s.initializer)
|
b := s.sbf(s.spiDevMinor, mode, channel, speed, bpw, delay, s.initializer)
|
||||||
s.busMap = make(map[byte]SPIBus)
|
s.busMap = make(map[byte]SPIBus)
|
||||||
s.busMap[channel] = b
|
s.busMap[channel] = b
|
||||||
return b
|
return b
|
||||||
|
Loading…
x
Reference in New Issue
Block a user