go-rpio/examples/spi/spi.go

46 lines
812 B
Go
Raw Normal View History

2018-11-29 17:29:11 +01:00
/*
SPI example
*/
package main
import (
2021-08-19 03:39:30 +02:00
"github.com/stianeikeland/go-rpio/v4"
2018-11-29 17:29:11 +01:00
"fmt"
)
func main() {
if err := rpio.Open(); err != nil {
panic(err)
}
if err := rpio.SpiBegin(rpio.Spi0); err != nil {
panic(err)
}
rpio.SpiChipSelect(0) // Select CE0 slave
// Send
rpio.SpiTransmit(0xFF) // send single byte
2021-08-19 03:39:30 +02:00
rpio.SpiTransmit(0xDE, 0xAD, 0xBE) // send several bytes
2018-11-29 17:29:11 +01:00
data := []byte{'H', 'e', 'l', 'l', 'o', 0}
2021-08-19 03:39:30 +02:00
rpio.SpiTransmit(data...) // send slice of bytes
2018-11-29 17:29:11 +01:00
// Receive
received := rpio.SpiReceive(5) // receive 5 bytes, (sends 5 x 0s)
fmt.Println(received)
// Send & Receive
buffer := []byte{ 0xDE, 0xED, 0xBE, 0xEF }
rpio.SpiExchange(buffer) // buffer is populated with received data
fmt.Println(buffer)
rpio.SpiEnd(rpio.Spi0)
rpio.Close()
}