@@ -8,15 +8,22 @@ import (
88 "sync"
99 "time"
1010
11- "github.com/stianeikeland/go-rpio/v4"
1211 "lautenbacher.net/goleds/config"
1312 "lautenbacher.net/goleds/producer"
1413 "lautenbacher.net/goleds/util"
14+ "periph.io/x/conn/v3/gpio"
15+ "periph.io/x/conn/v3/gpio/gpioreg"
16+ "periph.io/x/conn/v3/physic"
17+ "periph.io/x/conn/v3/spi"
18+ "periph.io/x/conn/v3/spi/spireg"
19+ "periph.io/x/host/v3"
1520)
1621
1722type RaspberryPiPlatform struct {
1823 * AbstractPlatform
1924 ledDriver ledDriver
25+ spiPort spi.PortCloser
26+ spiConn spi.Conn
2027 spiMutex sync.Mutex
2128 spimultiplexcfg map [string ]gpiocfg
2229 sensorViewer * SensorViewer
@@ -26,8 +33,8 @@ type RaspberryPiPlatform struct {
2633}
2734
2835type gpiocfg struct {
29- low []rpio. Pin
30- high []rpio. Pin
36+ low []gpio. PinIO
37+ high []gpio. PinIO
3138}
3239
3340func NewRaspberryPiPlatform (conf * config.Config ) * RaspberryPiPlatform {
@@ -55,29 +62,45 @@ func (s *RaspberryPiPlatform) Start(pool *sync.Pool) error {
5562 s .segments = parseDisplaySegments (s .config .Hardware .Display )
5663
5764 slog .Info ("Initialise GPIO and Spi..." )
58- if err := rpio . Open (); err != nil {
59- return fmt .Errorf ("failed to open rpio : %w" , err )
65+ if _ , err := host . Init (); err != nil {
66+ return fmt .Errorf ("failed to init periph : %w" , err )
6067 }
61- if err := rpio .SpiBegin (rpio .Spi0 ); err != nil {
62- return fmt .Errorf ("failed to begin spi: %w" , err )
68+
69+ var err error
70+ s .spiPort , err = spireg .Open ("" )
71+ if err != nil {
72+ return fmt .Errorf ("failed to open spi: %w" , err )
6373 }
6474
65- rpio .SpiSpeed (s .config .Hardware .SPIFrequency )
75+ s .spiConn , err = s .spiPort .Connect (physic .Frequency (s .config .Hardware .SPIFrequency )* physic .Hertz , spi .Mode0 , 8 )
76+ if err != nil {
77+ return fmt .Errorf ("failed to connect to spi device: %w" , err )
78+ }
6679
6780 s .spimultiplexcfg = make (map [string ]gpiocfg , len (s .config .Hardware .SpiMultiplexGPIO ))
6881
6982 for key , cfg := range s .config .Hardware .SpiMultiplexGPIO {
70- low := make ([]rpio.Pin , 0 , len (cfg .Low ))
71- high := make ([]rpio.Pin , 0 , len (cfg .High ))
72- for _ , pin := range cfg .Low {
73- rpiopin := rpio .Pin (pin )
74- rpiopin .Output ()
75- low = append (low , rpiopin )
83+ low := make ([]gpio.PinIO , 0 , len (cfg .Low ))
84+ high := make ([]gpio.PinIO , 0 , len (cfg .High ))
85+ for _ , pinName := range cfg .Low {
86+ pin := gpioreg .ByName (fmt .Sprintf ("GPIO%d" , pinName ))
87+ if pin == nil {
88+ return fmt .Errorf ("failed to find pin %d" , pinName )
89+ }
90+ if err := pin .Out (gpio .Low ); err != nil {
91+ return fmt .Errorf ("failed to set pin %d to output: %w" , pinName , err )
92+ }
93+ low = append (low , pin )
7694 }
77- for _ , pin := range cfg .High {
78- rpiopin := rpio .Pin (pin )
79- rpiopin .Output ()
80- high = append (high , rpiopin )
95+ for _ , pinName := range cfg .High {
96+ pin := gpioreg .ByName (fmt .Sprintf ("GPIO%d" , pinName ))
97+ if pin == nil {
98+ return fmt .Errorf ("failed to find pin %d" , pinName )
99+ }
100+ if err := pin .Out (gpio .High ); err != nil {
101+ return fmt .Errorf ("failed to set pin %d to output: %w" , pinName , err )
102+ }
103+ high = append (high , pin )
81104 }
82105 s .spimultiplexcfg [key ] = gpiocfg {
83106 low : low ,
@@ -122,10 +145,22 @@ func (s *RaspberryPiPlatform) Stop() {
122145 s .sensorWg .Wait ()
123146
124147 // Now, safely close hardware
125- rpio .SpiEnd (rpio .Spi0 )
126- if err := rpio .Close (); err != nil {
127- slog .Error ("Error closing rpio" , "error" , err )
148+ if s .spiPort != nil {
149+ if err := s .spiPort .Close (); err != nil {
150+ slog .Error ("Error closing spi port" , "error" , err )
151+ }
152+ s .spiPort = nil
153+ }
154+
155+ for _ , cfg := range s .spimultiplexcfg {
156+ for _ , pin := range cfg .low {
157+ pin .Halt ()
158+ }
159+ for _ , pin := range cfg .high {
160+ pin .Halt ()
161+ }
128162 }
163+ s .spimultiplexcfg = nil
129164
130165 // If there is a SensorViewer TUI, close it.
131166 if s .sensorViewer != nil {
@@ -153,13 +188,15 @@ func (s *RaspberryPiPlatform) spiExchangeMultiplex(index string, data []byte) []
153188 // The existence of the key is guaranteed by the config validation at startup.
154189 cfg := s .spimultiplexcfg [index ]
155190 for _ , pin := range cfg .low {
156- pin .Low ( )
191+ pin .Out ( gpio . Low )
157192 }
158193 for _ , pin := range cfg .high {
159- pin .High ( )
194+ pin .Out ( gpio . High )
160195 }
161196
162- rpio .SpiExchange (data )
197+ if err := s .spiConn .Tx (data , data ); err != nil {
198+ slog .Error ("spi transaction failed" , "error" , err )
199+ }
163200 return data
164201}
165202
0 commit comments