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