spi: remove machine.SPI and replace with drivers.SPI interface for almost all SPI drivers

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram
2020-10-18 18:03:12 +02:00
committed by Ron Evans
parent df64ce8f50
commit 46c9ba9595
23 changed files with 135 additions and 124 deletions
+8 -6
View File
@@ -6,6 +6,8 @@ package apa102 // import "tinygo.org/x/drivers/apa102"
import (
"image/color"
"machine"
"tinygo.org/x/drivers"
)
const (
@@ -23,20 +25,20 @@ var startFrame = []byte{0x00, 0x00, 0x00, 0x00}
// Device wraps APA102 SPI LEDs.
type Device struct {
bus SPI
bus drivers.SPI
Order int
}
// The SPI interface specifies the minimum functionality that a bus
// implementation needs to provide for use by the APA102 driver. Hardware
// SPI from the TinyGo "machine" package implements this already.
type SPI interface {
Tx(w, r []byte) error
Transfer(b byte) (byte, error)
}
// type SPI interface {
// Tx(w, r []byte) error
// Transfer(b byte) (byte, error)
// }
// New returns a new APA102 driver. Pass in a fully configured SPI bus.
func New(b SPI) Device {
func New(b drivers.SPI) Device {
return Device{bus: b, Order: BGR}
}
+7 -4
View File
@@ -1,8 +1,11 @@
package bmi160
import "machine"
import (
"machine"
"time"
import "time"
"tinygo.org/x/drivers"
)
// DeviceSPI is the SPI interface to a BMI160 accelerometer/gyroscope. There is
// also an I2C interface, but it is not yet supported.
@@ -11,13 +14,13 @@ type DeviceSPI struct {
CSB machine.Pin
// SPI bus (requires chip select to be usable).
Bus machine.SPI
Bus drivers.SPI
}
// NewSPI returns a new device driver. The pin and SPI interface are not
// touched, provide a fully configured SPI object and call Configure to start
// using this device.
func NewSPI(csb machine.Pin, spi machine.SPI) *DeviceSPI {
func NewSPI(csb machine.Pin, spi drivers.SPI) *DeviceSPI {
return &DeviceSPI{
CSB: csb, // chip select
Bus: spi,
+6 -9
View File
@@ -41,15 +41,7 @@ var (
spi = machine.NINA_SPI
// this is the ESP chip that has the WIFININA firmware flashed on it
adaptor = &wifinina.Device{
SPI: spi,
CS: machine.NINA_CS,
ACK: machine.NINA_ACK,
GPIO0: machine.NINA_GPIO0,
RESET: machine.NINA_RESETN,
}
console = machine.UART0
adaptor *wifinina.Device
topic = "tinygo"
)
@@ -68,6 +60,11 @@ func main() {
})
// Init esp8266/esp32
adaptor = wifinina.New(spi,
machine.NINA_CS,
machine.NINA_ACK,
machine.NINA_GPIO0,
machine.NINA_RESETN)
adaptor.Configure()
connectToAP()
+6 -9
View File
@@ -40,15 +40,7 @@ var (
spi = machine.NINA_SPI
// this is the ESP chip that has the WIFININA firmware flashed on it
adaptor = &wifinina.Device{
SPI: spi,
CS: machine.NINA_CS,
ACK: machine.NINA_ACK,
GPIO0: machine.NINA_GPIO0,
RESET: machine.NINA_RESETN,
}
console = machine.UART0
adaptor *wifinina.Device
cl mqtt.Client
topicTx = "tinygo/tx"
@@ -75,6 +67,11 @@ func main() {
})
// Init esp8266/esp32
adaptor = wifinina.New(spi,
machine.NINA_CS,
machine.NINA_ACK,
machine.NINA_GPIO0,
machine.NINA_RESETN)
adaptor.Configure()
connectToAP()
+10 -13
View File
@@ -24,20 +24,9 @@ const ntpHost = "129.6.15.29"
const NTP_PACKET_SIZE = 48
var (
// this is the ESP chip that has the WIFININA firmware flashed on it
// these are the default pins for the Arduino Nano33 IoT.
adaptor = wifinina.Device{
SPI: machine.NINA_SPI,
CS: machine.NINA_CS,
ACK: machine.NINA_ACK,
GPIO0: machine.NINA_GPIO0,
RESET: machine.NINA_RESETN,
}
b = make([]byte, NTP_PACKET_SIZE)
console = machine.UART0
adaptor *wifinina.Device
b = make([]byte, NTP_PACKET_SIZE)
)
func main() {
@@ -50,6 +39,14 @@ func main() {
SDI: machine.NINA_SDI,
SCK: machine.NINA_SCK,
})
// these are the default pins for the Arduino Nano33 IoT.
adaptor = wifinina.New(machine.NINA_SPI,
machine.NINA_CS,
machine.NINA_ACK,
machine.NINA_GPIO0,
machine.NINA_RESETN)
adaptor.Configure()
// connect to access point
+6 -9
View File
@@ -35,15 +35,7 @@ var (
spi = machine.NINA_SPI
// this is the ESP chip that has the WIFININA firmware flashed on it
adaptor = &wifinina.Device{
SPI: spi,
CS: machine.NINA_CS,
ACK: machine.NINA_ACK,
GPIO0: machine.NINA_GPIO0,
RESET: machine.NINA_RESETN,
}
console = machine.UART0
adaptor *wifinina.Device
)
var buf = &bytes.Buffer{}
@@ -60,6 +52,11 @@ func main() {
SCK: machine.NINA_SCK,
})
adaptor = wifinina.New(spi,
machine.NINA_CS,
machine.NINA_ACK,
machine.NINA_GPIO0,
machine.NINA_RESETN)
adaptor.Configure()
connectToAP()
+9 -11
View File
@@ -22,19 +22,9 @@ const pass = ""
// IP address of the server aka "hub". Replace with your own info.
const hubIP = ""
// these are the default pins for the Arduino Nano33 IoT.
// change these to connect to a different UART or pins for the ESP8266/ESP32
var (
// this is the ESP chip that has the WIFININA firmware flashed on it
// these are the default pins for the Arduino Nano33 IoT.
adaptor = &wifinina.Device{
SPI: machine.NINA_SPI,
CS: machine.NINA_CS,
ACK: machine.NINA_ACK,
GPIO0: machine.NINA_GPIO0,
RESET: machine.NINA_RESETN,
}
adaptor *wifinina.Device
)
func main() {
@@ -47,6 +37,14 @@ func main() {
SDI: machine.NINA_SDI,
SCK: machine.NINA_SCK,
})
// these are the default pins for the Arduino Nano33 IoT.
// change these to connect to a different UART or pins for the ESP8266/ESP32
adaptor = wifinina.New(machine.NINA_SPI,
machine.NINA_CS,
machine.NINA_ACK,
machine.NINA_GPIO0,
machine.NINA_RESETN)
adaptor.Configure()
// connect to access point
+6 -9
View File
@@ -33,15 +33,7 @@ var (
spi = machine.NINA_SPI
// this is the ESP chip that has the WIFININA firmware flashed on it
adaptor = &wifinina.Device{
SPI: spi,
CS: machine.NINA_CS,
ACK: machine.NINA_ACK,
GPIO0: machine.NINA_GPIO0,
RESET: machine.NINA_RESETN,
}
console = machine.UART0
adaptor *wifinina.Device
)
var buf [256]byte
@@ -61,6 +53,11 @@ func main() {
SCK: machine.NINA_SCK,
})
adaptor = wifinina.New(spi,
machine.NINA_CS,
machine.NINA_ACK,
machine.NINA_GPIO0,
machine.NINA_RESETN)
adaptor.Configure()
connectToAP()
+8 -6
View File
@@ -133,12 +133,14 @@ func (dev *Device) Configure(config *DeviceConfig) (err error) {
time.Sleep(30 * time.Microsecond)
// Speed up to max device frequency
if dev.attrs.MaxClockSpeedMHz > 0 {
err := dev.trans.setClockSpeed(uint32(dev.attrs.MaxClockSpeedMHz) * 1e6)
if err != nil {
return err
}
}
// I propose a check here for max frequency, but not put that functionality directly into the driver.
// Either that or we have to change the signature of the SPI interface in the machine package itself.
// if dev.attrs.MaxClockSpeedMHz > 0 {
// err := dev.trans.setClockSpeed(uint32(dev.attrs.MaxClockSpeedMHz) * 1e6)
// if err != nil {
// return err
// }
// }
// Enable Quad Mode if available
if dev.trans.supportQuadMode() && dev.attrs.QuadEnableBitMask > 0 {
+7 -24
View File
@@ -1,11 +1,14 @@
package flash
import "machine"
import (
"machine"
"tinygo.org/x/drivers"
)
type transport interface {
configure(config *DeviceConfig)
supportQuadMode() bool
setClockSpeed(hz uint32) (err error)
runCommand(cmd byte) (err error)
readCommand(cmd byte, rsp []byte) (err error)
writeCommand(cmd byte, data []byte) (err error)
@@ -16,7 +19,7 @@ type transport interface {
// NewSPI returns a pointer to a flash device that uses a SPI peripheral to
// communicate with a serial memory chip.
func NewSPI(spi *machine.SPI, sdo, sdi, sck, cs machine.Pin) *Device {
func NewSPI(spi drivers.SPI, sdo, sdi, sck, cs machine.Pin) *Device {
return &Device{
trans: &spiTransport{
spi: spi,
@@ -29,7 +32,7 @@ func NewSPI(spi *machine.SPI, sdo, sdi, sck, cs machine.Pin) *Device {
}
type spiTransport struct {
spi *machine.SPI
spi drivers.SPI
sdo machine.Pin
sdi machine.Pin
sck machine.Pin
@@ -37,31 +40,11 @@ type spiTransport struct {
}
func (tr *spiTransport) configure(config *DeviceConfig) {
// Configure spi bus
tr.setClockSpeed(5000000)
// Configure chip select pin
tr.ss.Configure(machine.PinConfig{Mode: machine.PinOutput})
tr.ss.High()
}
func (tr *spiTransport) setClockSpeed(hz uint32) error {
// TODO: un-hardcode this max speed; it is probably a sensible
// default maximum for atsamd and nrf at least
if hz > 24*1e6 {
hz = 24 * 1e6
}
tr.spi.Configure(machine.SPIConfig{
Frequency: hz,
SDI: tr.sdi,
SDO: tr.sdo,
SCK: tr.sck,
LSBFirst: false,
Mode: 0,
})
return nil
}
func (tr *spiTransport) supportQuadMode() bool {
return false
}
+4 -2
View File
@@ -9,6 +9,8 @@ import (
"image/color"
"machine"
"time"
"tinygo.org/x/drivers"
)
type Config struct {
@@ -21,7 +23,7 @@ type Config struct {
}
type Device struct {
bus machine.SPI
bus drivers.SPI
a machine.Pin
b machine.Pin
c machine.Pin
@@ -52,7 +54,7 @@ type Device struct {
}
// New returns a new HUB75 driver. Pass in a fully configured SPI bus.
func New(b machine.SPI, latPin, oePin, aPin, bPin, cPin, dPin machine.Pin) Device {
func New(b drivers.SPI, latPin, oePin, aPin, bPin, cPin, dPin machine.Pin) Device {
aPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
bPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
cPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
+4 -2
View File
@@ -7,11 +7,13 @@ package mcp3008 // import "tinygo.org/x/drivers/mcp3008"
import (
"errors"
"machine"
"tinygo.org/x/drivers"
)
// Device wraps MCP3008 SPI ADC.
type Device struct {
bus machine.SPI
bus drivers.SPI
cs machine.Pin
tx []byte
rx []byte
@@ -32,7 +34,7 @@ type ADCPin struct {
}
// New returns a new MCP3008 driver. Pass in a fully configured SPI bus.
func New(b machine.SPI, csPin machine.Pin) *Device {
func New(b drivers.SPI, csPin machine.Pin) *Device {
d := &Device{bus: b,
cs: csPin,
tx: make([]byte, 3),
+4 -2
View File
@@ -9,11 +9,13 @@ import (
"image/color"
"machine"
"time"
"tinygo.org/x/drivers"
)
// Device wraps an SPI connection.
type Device struct {
bus machine.SPI
bus drivers.SPI
dcPin machine.Pin
rstPin machine.Pin
scePin machine.Pin
@@ -29,7 +31,7 @@ type Config struct {
}
// New creates a new PCD8544 connection. The SPI bus must already be configured.
func New(bus machine.SPI, dcPin, rstPin, scePin machine.Pin) *Device {
func New(bus drivers.SPI, dcPin, rstPin, scePin machine.Pin) *Device {
return &Device{
bus: bus,
dcPin: dcPin,
+7
View File
@@ -0,0 +1,7 @@
package drivers
// SPI represents a SPI bus. It is implemented by the machine.SPI type.
type SPI interface {
Tx(w, r []byte) error
Transfer(b byte) (byte, error)
}
+2 -2
View File
@@ -37,7 +37,7 @@ type I2CBus struct {
}
type SPIBus struct {
wire machine.SPI
wire drivers.SPI
dcPin machine.Pin
resetPin machine.Pin
csPin machine.Pin
@@ -62,7 +62,7 @@ func NewI2C(bus drivers.I2C) Device {
}
// NewSPI creates a new SSD1306 connection. The SPI wire must already be configured.
func NewSPI(bus machine.SPI, dcPin, resetPin, csPin machine.Pin) Device {
func NewSPI(bus drivers.SPI, dcPin, resetPin, csPin machine.Pin) Device {
dcPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
resetPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
csPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
+4 -2
View File
@@ -10,6 +10,8 @@ import (
"errors"
"time"
"tinygo.org/x/drivers"
)
type Model uint8
@@ -17,7 +19,7 @@ type Rotation uint8
// Device wraps an SPI connection.
type Device struct {
bus machine.SPI
bus drivers.SPI
dcPin machine.Pin
resetPin machine.Pin
csPin machine.Pin
@@ -35,7 +37,7 @@ type Config struct {
}
// New creates a new SSD1331 connection. The SPI wire must already be configured.
func New(bus machine.SPI, resetPin, dcPin, csPin machine.Pin) Device {
func New(bus drivers.SPI, resetPin, dcPin, csPin machine.Pin) Device {
dcPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
resetPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
csPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
+4 -2
View File
@@ -9,6 +9,8 @@ import (
"image/color"
"machine"
"time"
"tinygo.org/x/drivers"
)
var (
@@ -18,7 +20,7 @@ var (
// Device wraps an SPI connection.
type Device struct {
bus machine.SPI
bus drivers.SPI
dcPin machine.Pin
resetPin machine.Pin
csPin machine.Pin
@@ -40,7 +42,7 @@ type Config struct {
}
// New creates a new SSD1351 connection. The SPI wire must already be configured.
func New(bus machine.SPI, resetPin, dcPin, csPin, enPin, rwPin machine.Pin) Device {
func New(bus drivers.SPI, resetPin, dcPin, csPin, enPin, rwPin machine.Pin) Device {
return Device{
bus: bus,
dcPin: dcPin,
+4 -2
View File
@@ -10,6 +10,8 @@ import (
"time"
"errors"
"tinygo.org/x/drivers"
)
type Model uint8
@@ -17,7 +19,7 @@ type Rotation uint8
// Device wraps an SPI connection.
type Device struct {
bus machine.SPI
bus drivers.SPI
dcPin machine.Pin
resetPin machine.Pin
csPin machine.Pin
@@ -44,7 +46,7 @@ type Config struct {
}
// New creates a new ST7735 connection. The SPI wire must already be configured.
func New(bus machine.SPI, resetPin, dcPin, csPin, blPin machine.Pin) Device {
func New(bus drivers.SPI, resetPin, dcPin, csPin, blPin machine.Pin) Device {
dcPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
resetPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
csPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
+4 -3
View File
@@ -12,6 +12,8 @@ import (
"time"
"errors"
"tinygo.org/x/drivers"
)
type Rotation uint8
@@ -20,7 +22,7 @@ type FrameRate uint8
// Device wraps an SPI connection.
type Device struct {
bus machine.SPI
bus drivers.SPI
dcPin machine.Pin
resetPin machine.Pin
csPin machine.Pin
@@ -50,7 +52,7 @@ type Config struct {
}
// New creates a new ST7789 connection. The SPI wire must already be configured.
func New(bus machine.SPI, resetPin, dcPin, csPin, blPin machine.Pin) Device {
func New(bus drivers.SPI, resetPin, dcPin, csPin, blPin machine.Pin) Device {
dcPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
resetPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
csPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
@@ -276,7 +278,6 @@ func (d *Device) FillRectangle(x, y, width, height int16, c color.RGBA) error {
return nil
}
// FillRectangle fills a rectangle at a given coordinates with a buffer
func (d *Device) FillRectangleWithBuffer(x, y, width, height int16, buffer []color.RGBA) error {
i, j := d.Size()
+4 -2
View File
@@ -9,6 +9,8 @@ import (
"image/color"
"machine"
"time"
"tinygo.org/x/drivers"
)
type Config struct {
@@ -19,7 +21,7 @@ type Config struct {
}
type Device struct {
bus machine.SPI
bus drivers.SPI
cs machine.Pin
dc machine.Pin
rst machine.Pin
@@ -51,7 +53,7 @@ var lutPartialUpdate = [30]uint8{
}
// New returns a new epd2in13x driver. Pass in a fully configured SPI bus.
func New(bus machine.SPI, csPin, dcPin, rstPin, busyPin machine.Pin) Device {
func New(bus drivers.SPI, csPin, dcPin, rstPin, busyPin machine.Pin) Device {
csPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
dcPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
rstPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
+4 -2
View File
@@ -9,6 +9,8 @@ import (
"image/color"
"machine"
"time"
"tinygo.org/x/drivers"
)
type Config struct {
@@ -18,7 +20,7 @@ type Config struct {
}
type Device struct {
bus machine.SPI
bus drivers.SPI
cs machine.Pin
dc machine.Pin
rst machine.Pin
@@ -32,7 +34,7 @@ type Device struct {
type Color uint8
// New returns a new epd2in13x driver. Pass in a fully configured SPI bus.
func New(bus machine.SPI, csPin, dcPin, rstPin, busyPin machine.Pin) Device {
func New(bus drivers.SPI, csPin, dcPin, rstPin, busyPin machine.Pin) Device {
csPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
dcPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
rstPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
+4 -2
View File
@@ -12,6 +12,8 @@ import (
"image/color"
"machine"
"time"
"tinygo.org/x/drivers"
)
type Config struct {
@@ -22,7 +24,7 @@ type Config struct {
}
type Device struct {
bus machine.SPI
bus drivers.SPI
cs machine.Pin
dc machine.Pin
rst machine.Pin
@@ -38,7 +40,7 @@ type Device struct {
type Rotation uint8
// New returns a new epd4in2 driver. Pass in a fully configured SPI bus.
func New(bus machine.SPI, csPin, dcPin, rstPin, busyPin machine.Pin) Device {
func New(bus drivers.SPI, csPin, dcPin, rstPin, busyPin machine.Pin) Device {
csPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
dcPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
rstPin.Configure(machine.PinConfig{Mode: machine.PinOutput})
+13 -1
View File
@@ -13,6 +13,7 @@ import (
"machine"
"tinygo.org/x/drivers"
"tinygo.org/x/drivers/net"
)
@@ -258,7 +259,7 @@ type command struct {
}
type Device struct {
SPI machine.SPI
SPI drivers.SPI
CS machine.Pin
ACK machine.Pin
GPIO0 machine.Pin
@@ -268,6 +269,17 @@ type Device struct {
ssids [10]string
}
// New returns a new Wiifinina driver.
func New(bus drivers.SPI, csPin, ackPin, gpio0Pin, resetPin machine.Pin) *Device {
return &Device{
SPI: bus,
CS: csPin,
ACK: ackPin,
GPIO0: gpio0Pin,
RESET: resetPin,
}
}
func (d *Device) Configure() {
net.UseDriver(d.NewDriver())