2014-04-28 01:18:14 +05:30
|
|
|
package embd
|
|
|
|
|
|
|
|
import "sync"
|
|
|
|
|
2016-09-05 23:13:14 -07:00
|
|
|
type spiBusFactory func(int, byte, byte, int, int, int, func() error) SPIBus
|
2014-04-28 01:18:14 +05:30
|
|
|
|
|
|
|
type spiDriver struct {
|
2016-09-05 23:13:14 -07:00
|
|
|
spiDevMinor int
|
2014-05-22 01:21:09 +05:30
|
|
|
initializer func() error
|
2014-04-28 01:18:14 +05:30
|
|
|
|
|
|
|
busMap map[byte]SPIBus
|
|
|
|
busMapLock sync.Mutex
|
|
|
|
|
2014-04-28 03:13:57 +05:30
|
|
|
sbf spiBusFactory
|
2014-04-28 01:18:14 +05:30
|
|
|
}
|
|
|
|
|
2014-05-22 02:31:42 +05:30
|
|
|
// NewSPIDriver returns a SPIDriver interface which allows control
|
|
|
|
// over the SPI bus.
|
2016-09-05 23:13:14 -07:00
|
|
|
func NewSPIDriver(spiDevMinor int, sbf spiBusFactory, i func() error) SPIDriver {
|
2014-04-28 01:18:14 +05:30
|
|
|
return &spiDriver{
|
2014-05-22 01:21:09 +05:30
|
|
|
spiDevMinor: spiDevMinor,
|
|
|
|
sbf: sbf,
|
|
|
|
initializer: i,
|
2014-04-28 01:18:14 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-22 02:31:42 +05:30
|
|
|
// Bus returns a SPIBus interface which allows us to use spi functionalities
|
2014-04-28 01:18:14 +05:30
|
|
|
func (s *spiDriver) Bus(mode, channel byte, speed, bpw, delay int) SPIBus {
|
|
|
|
s.busMapLock.Lock()
|
|
|
|
defer s.busMapLock.Unlock()
|
|
|
|
|
2014-05-22 01:21:09 +05:30
|
|
|
b := s.sbf(s.spiDevMinor, mode, channel, speed, bpw, delay, s.initializer)
|
2014-05-03 21:35:09 +05:30
|
|
|
s.busMap = make(map[byte]SPIBus)
|
2014-04-28 01:18:14 +05:30
|
|
|
s.busMap[channel] = b
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
2014-05-22 02:31:42 +05:30
|
|
|
// Close cleans up all the initialized SPIbus
|
2014-04-28 01:18:14 +05:30
|
|
|
func (s *spiDriver) Close() error {
|
|
|
|
for _, b := range s.busMap {
|
|
|
|
b.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|