mirror of
https://github.com/kidoman/embd
synced 2024-12-22 04:40:04 +01:00
spi: use a initializer if required by any host
This commit is contained in:
parent
faa686f01c
commit
42033238e2
@ -155,6 +155,13 @@ func ensureFeatureDisabled(id string) error {
|
||||
return fmt.Errorf("embd: could not disable feature %q", id)
|
||||
}
|
||||
|
||||
func spiInitializer() error {
|
||||
if err := ensureFeatureEnabled("BB-SPIDEV0"); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
embd.Register(embd.HostBBB, func(rev int) *embd.Descriptor {
|
||||
return &embd.Descriptor{
|
||||
@ -168,7 +175,7 @@ func init() {
|
||||
return embd.NewLEDDriver(ledMap, generic.NewLED)
|
||||
},
|
||||
SPIDriver: func() embd.SPIDriver {
|
||||
return embd.NewSPIDriver(spiDeviceMinor, generic.NewSPIBus)
|
||||
return embd.NewSPIDriver(spiDeviceMinor, generic.NewSPIBus, true, spiInitializer)
|
||||
},
|
||||
}
|
||||
})
|
||||
|
@ -53,24 +53,36 @@ type spiBus struct {
|
||||
|
||||
spiTransferData spiIocTransfer
|
||||
initialized bool
|
||||
|
||||
shouldInitialize bool
|
||||
initializer func() error
|
||||
}
|
||||
|
||||
func spiIocMessageN(n uint32) uint32 {
|
||||
return (spiIocMessage0 + (n * spiIocIncrementor))
|
||||
}
|
||||
|
||||
func NewSPIBus(spiDevMinor, mode, channel byte, speed, bpw, delay int) embd.SPIBus {
|
||||
func NewSPIBus(spiDevMinor, mode, channel byte, speed, bpw, delay int, shouldInitialize bool, i func() error) embd.SPIBus {
|
||||
return &spiBus{
|
||||
spiDevMinor: spiDevMinor,
|
||||
mode: mode,
|
||||
channel: channel,
|
||||
speed: speed,
|
||||
bpw: bpw,
|
||||
delayms: delay,
|
||||
spiDevMinor: spiDevMinor,
|
||||
mode: mode,
|
||||
channel: channel,
|
||||
speed: speed,
|
||||
bpw: bpw,
|
||||
delayms: delay,
|
||||
shouldInitialize: shouldInitialize,
|
||||
initializer: i,
|
||||
}
|
||||
}
|
||||
|
||||
func (b *spiBus) init() error {
|
||||
if b.shouldInitialize {
|
||||
if err := b.initializer(); err != nil {
|
||||
return err
|
||||
}
|
||||
b.shouldInitialize = false
|
||||
}
|
||||
|
||||
if b.initialized {
|
||||
return nil
|
||||
}
|
||||
|
@ -77,7 +77,7 @@ func init() {
|
||||
return embd.NewLEDDriver(ledMap, generic.NewLED)
|
||||
},
|
||||
SPIDriver: func() embd.SPIDriver {
|
||||
return embd.NewSPIDriver(spiDeviceMinor, generic.NewSPIBus)
|
||||
return embd.NewSPIDriver(spiDeviceMinor, generic.NewSPIBus, false, nil)
|
||||
},
|
||||
}
|
||||
})
|
||||
|
16
spidriver.go
16
spidriver.go
@ -2,10 +2,12 @@ package embd
|
||||
|
||||
import "sync"
|
||||
|
||||
type spiBusFactory func(byte, byte, byte, int, int, int) SPIBus
|
||||
type spiBusFactory func(byte, byte, byte, int, int, int, bool, func() error) SPIBus
|
||||
|
||||
type spiDriver struct {
|
||||
spiDevMinor byte
|
||||
spiDevMinor byte
|
||||
shouldInitialize bool
|
||||
initializer func() error
|
||||
|
||||
busMap map[byte]SPIBus
|
||||
busMapLock sync.Mutex
|
||||
@ -13,10 +15,12 @@ type spiDriver struct {
|
||||
sbf spiBusFactory
|
||||
}
|
||||
|
||||
func NewSPIDriver(spiDevMinor byte, sbf spiBusFactory) SPIDriver {
|
||||
func NewSPIDriver(spiDevMinor byte, sbf spiBusFactory, shouldInitialize bool, i func() error) SPIDriver {
|
||||
return &spiDriver{
|
||||
spiDevMinor: spiDevMinor,
|
||||
sbf: sbf,
|
||||
spiDevMinor: spiDevMinor,
|
||||
sbf: sbf,
|
||||
shouldInitialize: shouldInitialize,
|
||||
initializer: i,
|
||||
}
|
||||
}
|
||||
|
||||
@ -24,7 +28,7 @@ func (s *spiDriver) Bus(mode, channel byte, speed, bpw, delay int) SPIBus {
|
||||
s.busMapLock.Lock()
|
||||
defer s.busMapLock.Unlock()
|
||||
|
||||
b := s.sbf(s.spiDevMinor, mode, channel, speed, bpw, delay)
|
||||
b := s.sbf(s.spiDevMinor, mode, channel, speed, bpw, delay, s.shouldInitialize, s.initializer)
|
||||
s.busMap[channel] = b
|
||||
return b
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user