Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ jobs:
- run:
name: "Enforce Go Formatted Code"
command: make fmt-check
- run:
name: "Run unit tests"
command: make unit-test
- run:
name: "Run build and smoke tests"
command: make smoke-test
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -158,4 +158,13 @@ endif
tinygo build -size short -o ./build/test.hex -target=circuitplay-express ./examples/lis2mdl/main.go
@md5sum ./build/test.hex

test: clean fmt-check smoke-test
DRIVERS = $(wildcard */)
NOTESTS = build examples flash semihosting pcd8544 shiftregister st7789 microphone mcp3008 gps microbitmatrix \
hcsr04 ssd1331 ws2812 thermistor apa102 easystepper ssd1351 ili9341 wifinina shifter hub75 \
hd44780 buzzer ssd1306 espat l9110x st7735 bmi160 l293x
TESTS = $(filter-out $(addsuffix /%,$(NOTESTS)),$(DRIVERS))

unit-test:
@go test -v $(addprefix ./,$(TESTS))

test: clean fmt-check unit-test smoke-test
14 changes: 4 additions & 10 deletions apa102/apa102.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package apa102 // import "tinygo.org/x/drivers/apa102"
import (
"image/color"
"machine"

"tinygo.org/x/drivers"
)

const (
Expand All @@ -23,20 +25,12 @@ 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)
}

// 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}
}

Expand Down
11 changes: 7 additions & 4 deletions bmi160/bmi160.go
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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,
Expand Down
15 changes: 6 additions & 9 deletions examples/wifinina/mqttclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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()
Expand Down
15 changes: 6 additions & 9 deletions examples/wifinina/mqttsub/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()
Expand Down
23 changes: 10 additions & 13 deletions examples/wifinina/ntpclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
Expand Down
15 changes: 6 additions & 9 deletions examples/wifinina/tcpclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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()
Expand Down
20 changes: 9 additions & 11 deletions examples/wifinina/udpstation/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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
Expand Down
15 changes: 6 additions & 9 deletions examples/wifinina/webclient/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down
2 changes: 2 additions & 0 deletions flash/flash.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ func (dev *Device) Configure(config *DeviceConfig) (err error) {
time.Sleep(30 * time.Microsecond)

// Speed up to max device frequency
// 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 {
Expand Down
4 changes: 3 additions & 1 deletion flash/transport_spi.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package flash

import "machine"
import (
"machine"
)

type transport interface {
configure(config *DeviceConfig)
Expand Down
6 changes: 4 additions & 2 deletions hub75/hub75.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"image/color"
"machine"
"time"

"tinygo.org/x/drivers"
)

type Config struct {
Expand All @@ -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
Expand Down Expand Up @@ -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})
Expand Down
6 changes: 4 additions & 2 deletions mcp3008/mcp3008.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
Expand Down
6 changes: 4 additions & 2 deletions pcd8544/pcd8544.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
13 changes: 13 additions & 0 deletions spi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package drivers

// SPI represents a SPI bus. It is implemented by the machine.SPI type.
type SPI interface {
// Tx transmits the given buffer w and receives at the same time the buffer r.
// The two buffers must be the same length. The only exception is when w or r are nil,
// in which case Tx only transmits (without receiving) or only receives (while sending 0 bytes).
Tx(w, r []byte) error

// Transfer writes a single byte out on the SPI bus and receives a byte at the same time.
// If you want to transfer multiple bytes, it is more efficient to use Tx instead.
Transfer(b byte) (byte, error)
}
Loading