mirror of
https://github.com/kidoman/embd
synced 2024-12-22 12:50:19 +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)
|
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() {
|
func init() {
|
||||||
embd.Register(embd.HostBBB, func(rev int) *embd.Descriptor {
|
embd.Register(embd.HostBBB, func(rev int) *embd.Descriptor {
|
||||||
return &embd.Descriptor{
|
return &embd.Descriptor{
|
||||||
@ -168,7 +175,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)
|
return embd.NewSPIDriver(spiDeviceMinor, generic.NewSPIBus, true, spiInitializer)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
@ -53,24 +53,36 @@ type spiBus struct {
|
|||||||
|
|
||||||
spiTransferData spiIocTransfer
|
spiTransferData spiIocTransfer
|
||||||
initialized bool
|
initialized bool
|
||||||
|
|
||||||
|
shouldInitialize bool
|
||||||
|
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) embd.SPIBus {
|
func NewSPIBus(spiDevMinor, mode, channel byte, speed, bpw, delay int, shouldInitialize bool, 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,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *spiBus) init() error {
|
func (b *spiBus) init() error {
|
||||||
|
if b.shouldInitialize {
|
||||||
|
if err := b.initializer(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
b.shouldInitialize = false
|
||||||
|
}
|
||||||
|
|
||||||
if b.initialized {
|
if b.initialized {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -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)
|
return embd.NewSPIDriver(spiDeviceMinor, generic.NewSPIBus, false, nil)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
16
spidriver.go
16
spidriver.go
@ -2,10 +2,12 @@ package embd
|
|||||||
|
|
||||||
import "sync"
|
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 {
|
type spiDriver struct {
|
||||||
spiDevMinor byte
|
spiDevMinor byte
|
||||||
|
shouldInitialize bool
|
||||||
|
initializer func() error
|
||||||
|
|
||||||
busMap map[byte]SPIBus
|
busMap map[byte]SPIBus
|
||||||
busMapLock sync.Mutex
|
busMapLock sync.Mutex
|
||||||
@ -13,10 +15,12 @@ type spiDriver struct {
|
|||||||
sbf spiBusFactory
|
sbf spiBusFactory
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSPIDriver(spiDevMinor byte, sbf spiBusFactory) SPIDriver {
|
func NewSPIDriver(spiDevMinor byte, sbf spiBusFactory, shouldInitialize bool, i func() error) SPIDriver {
|
||||||
return &spiDriver{
|
return &spiDriver{
|
||||||
spiDevMinor: spiDevMinor,
|
spiDevMinor: spiDevMinor,
|
||||||
sbf: sbf,
|
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()
|
s.busMapLock.Lock()
|
||||||
defer s.busMapLock.Unlock()
|
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
|
s.busMap[channel] = b
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user