add simplest driver ports

This commit is contained in:
Patricio Whittingslow
2025-11-10 19:20:35 -03:00
committed by Ron Evans
parent 23833e69c7
commit 09fd01340b
10 changed files with 107 additions and 74 deletions
+7 -3
View File
@@ -5,9 +5,10 @@ package apa102 // import "tinygo.org/x/drivers/apa102"
import ( import (
"image/color" "image/color"
"machine"
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
) )
const ( const (
@@ -37,8 +38,11 @@ func New(b drivers.SPI) *Device {
// NewSoftwareSPI returns a new APA102 driver that will use a software based // NewSoftwareSPI returns a new APA102 driver that will use a software based
// implementation of the SPI protocol. // implementation of the SPI protocol.
func NewSoftwareSPI(sckPin, sdoPin machine.Pin, delay uint32) *Device { func NewSoftwareSPI(sckPin, sdoPin pin.Output, delay uint32) *Device {
return New(&bbSPI{SCK: sckPin, SDO: sdoPin, Delay: delay}) return New(&bbSPI{SCK: sckPin.Set, SDO: sdoPin.Set, Delay: delay, configurePins: func() {
legacy.ConfigurePinOut(sckPin)
legacy.ConfigurePinOut(sdoPin)
}})
} }
// WriteColors writes the given RGBA color slice out using the APA102 protocol. // WriteColors writes the given RGBA color slice out using the APA102 protocol.
+12 -6
View File
@@ -1,6 +1,9 @@
package apa102 package apa102
import "machine" import (
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
)
// bbSPI is a dumb bit-bang implementation of SPI protocol that is hardcoded // bbSPI is a dumb bit-bang implementation of SPI protocol that is hardcoded
// to mode 0 and ignores trying to receive data. Just enough for the APA102. // to mode 0 and ignores trying to receive data. Just enough for the APA102.
@@ -8,15 +11,18 @@ import "machine"
// most purposes other than the APA102 package. It might be desirable to make // most purposes other than the APA102 package. It might be desirable to make
// this more generic and include it in the TinyGo "machine" package instead. // this more generic and include it in the TinyGo "machine" package instead.
type bbSPI struct { type bbSPI struct {
SCK machine.Pin SCK pin.OutputFunc
SDO machine.Pin SDO pin.OutputFunc
Delay uint32 Delay uint32
configurePins func()
} }
// Configure sets up the SCK and SDO pins as outputs and sets them low // Configure sets up the SCK and SDO pins as outputs and sets them low
func (s *bbSPI) Configure() { func (s *bbSPI) Configure() {
s.SCK.Configure(machine.PinConfig{Mode: machine.PinOutput}) if s.configurePins == nil {
s.SDO.Configure(machine.PinConfig{Mode: machine.PinOutput}) panic(legacy.ErrConfigBeforeInstantiated)
}
s.configurePins()
s.SCK.Low() s.SCK.Low()
s.SDO.Low() s.SDO.Low()
if s.Delay == 0 { if s.Delay == 0 {
+31 -24
View File
@@ -1,31 +1,36 @@
package bmi160 package bmi160
import ( import (
"machine"
"time" "time"
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
) )
// DeviceSPI is the SPI interface to a BMI160 accelerometer/gyroscope. There is // DeviceSPI is the SPI interface to a BMI160 accelerometer/gyroscope. There is
// also an I2C interface, but it is not yet supported. // also an I2C interface, but it is not yet supported.
type DeviceSPI struct { type DeviceSPI struct {
// Chip select pin // Chip select pin
CSB machine.Pin csb pin.OutputFunc
buf [7]byte buf [7]byte
// SPI bus (requires chip select to be usable). // SPI bus (requires chip select to be usable).
Bus drivers.SPI bus drivers.SPI
configurePins func()
} }
// NewSPI returns a new device driver. The pin and SPI interface are not // 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 // touched, provide a fully configured SPI object and call Configure to start
// using this device. // using this device.
func NewSPI(csb machine.Pin, spi drivers.SPI) *DeviceSPI { func NewSPI(csb pin.Output, spi drivers.SPI) *DeviceSPI {
return &DeviceSPI{ return &DeviceSPI{
CSB: csb, // chip select csb: csb.Set, // chip select
Bus: spi, bus: spi,
configurePins: func() {
legacy.ConfigurePinOut(csb)
},
} }
} }
@@ -33,9 +38,11 @@ func NewSPI(csb machine.Pin, spi drivers.SPI) *DeviceSPI {
// configures the BMI160, but it does not configure the SPI interface (it is // configures the BMI160, but it does not configure the SPI interface (it is
// assumed to be up and running). // assumed to be up and running).
func (d *DeviceSPI) Configure() error { func (d *DeviceSPI) Configure() error {
d.CSB.Configure(machine.PinConfig{Mode: machine.PinOutput}) if d.configurePins == nil {
d.CSB.High() return legacy.ErrConfigBeforeInstantiated
}
d.configurePins()
d.csb.High()
// The datasheet recommends doing a register read from address 0x7F to get // The datasheet recommends doing a register read from address 0x7F to get
// SPI communication going: // SPI communication going:
// > If CSB sees a rising edge after power-up, the BMI160 interface switches // > If CSB sees a rising edge after power-up, the BMI160 interface switches
@@ -86,9 +93,9 @@ func (d *DeviceSPI) ReadTemperature() (temperature int32, err error) {
data[0] = 0x80 | reg_TEMPERATURE_0 data[0] = 0x80 | reg_TEMPERATURE_0
data[1] = 0 data[1] = 0
data[2] = 0 data[2] = 0
d.CSB.Low() d.csb.Low()
err = d.Bus.Tx(data, data) err = d.bus.Tx(data, data)
d.CSB.High() d.csb.High()
if err != nil { if err != nil {
return return
} }
@@ -123,9 +130,9 @@ func (d *DeviceSPI) ReadAcceleration() (x int32, y int32, z int32, err error) {
for i := 1; i < len(data); i++ { for i := 1; i < len(data); i++ {
data[i] = 0 data[i] = 0
} }
d.CSB.Low() d.csb.Low()
err = d.Bus.Tx(data, data) err = d.bus.Tx(data, data)
d.CSB.High() d.csb.High()
if err != nil { if err != nil {
return return
} }
@@ -153,9 +160,9 @@ func (d *DeviceSPI) ReadRotation() (x int32, y int32, z int32, err error) {
for i := 1; i < len(data); i++ { for i := 1; i < len(data); i++ {
data[i] = 0 data[i] = 0
} }
d.CSB.Low() d.csb.Low()
err = d.Bus.Tx(data, data) err = d.bus.Tx(data, data)
d.CSB.High() d.csb.High()
if err != nil { if err != nil {
return return
} }
@@ -201,9 +208,9 @@ func (d *DeviceSPI) readRegister(address uint8) uint8 {
data := d.buf[:2] data := d.buf[:2]
data[0] = 0x80 | address data[0] = 0x80 | address
data[1] = 0 data[1] = 0
d.CSB.Low() d.csb.Low()
d.Bus.Tx(data, data) d.bus.Tx(data, data)
d.CSB.High() d.csb.High()
return data[1] return data[1]
} }
@@ -217,7 +224,7 @@ func (d *DeviceSPI) writeRegister(address, data uint8) {
buf[0] = address buf[0] = address
buf[1] = data buf[1] = data
d.CSB.Low() d.csb.Low()
d.Bus.Tx(buf, buf) d.bus.Tx(buf, buf)
d.CSB.High() d.csb.High()
} }
+7 -7
View File
@@ -2,22 +2,22 @@
package buzzer // import "tinygo.org/x/drivers/buzzer" package buzzer // import "tinygo.org/x/drivers/buzzer"
import ( import (
"machine"
"time" "time"
"tinygo.org/x/drivers/internal/pin"
) )
// Device wraps a GPIO connection to a buzzer. // Device wraps a GPIO connection to a buzzer.
type Device struct { type Device struct {
pin machine.Pin pin pin.OutputFunc
High bool High bool
BPM float64 BPM float64
} }
// New returns a new buzzer driver given which pin to use // New returns a new buzzer driver given which pin to use
func New(pin machine.Pin) Device { func New(pin pin.Output) Device {
return Device{ return Device{
pin: pin, pin: pin.Set,
High: false, High: false,
BPM: 96.0, BPM: 96.0,
} }
@@ -25,14 +25,14 @@ func New(pin machine.Pin) Device {
// On sets the buzzer to a high state. // On sets the buzzer to a high state.
func (l *Device) On() (err error) { func (l *Device) On() (err error) {
l.pin.Set(true) l.pin.High()
l.High = true l.High = true
return return
} }
// Off sets the buzzer to a low state. // Off sets the buzzer to a low state.
func (l *Device) Off() (err error) { func (l *Device) Off() (err error) {
l.pin.Set(false) l.pin.Low()
l.High = false l.High = false
return return
} }
+3
View File
@@ -1,3 +1,6 @@
// Guarded because still unsure of how to deal with interrupt drivers.
//go:build tinygo
// Package ft6336 provides a driver for the FT6336 I2C Self-Capacitive touch // Package ft6336 provides a driver for the FT6336 I2C Self-Capacitive touch
// panel controller. // panel controller.
// //
+5 -5
View File
@@ -210,7 +210,7 @@ func (d *Device) SendCommand(command byte) {
d.bus.SetCommandMode(true) d.bus.SetCommandMode(true)
d.bus.Write([]byte{command}) d.bus.Write([]byte{command})
for d.busy(command == DISPLAY_CLEAR || command == CURSOR_HOME) { for d.isBusy(command == DISPLAY_CLEAR || command == CURSOR_HOME) {
} }
} }
@@ -219,7 +219,7 @@ func (d *Device) sendData(data byte) {
d.bus.SetCommandMode(false) d.bus.SetCommandMode(false)
d.bus.Write([]byte{data}) d.bus.Write([]byte{data})
for d.busy(false) { for d.isBusy(false) {
} }
} }
@@ -231,9 +231,9 @@ func (d *Device) CreateCharacter(cgramAddr uint8, data []byte) {
} }
} }
// busy returns true when hd447890 is busy // isBusy returns true when hd447890 is isBusy
// or after the timeout specified // or after the timeout specified
func (d *Device) busy(longDelay bool) bool { func (d *Device) isBusy(longDelay bool) bool {
if d.bus.WriteOnly() { if d.bus.WriteOnly() {
// Can't read busy flag if write only, so sleep a bit then return // Can't read busy flag if write only, so sleep a bit then return
if longDelay { if longDelay {
@@ -261,7 +261,7 @@ func (d *Device) busy(longDelay bool) bool {
// Busy returns true when hd447890 is busy // Busy returns true when hd447890 is busy
func (d *Device) Busy() bool { func (d *Device) Busy() bool {
return d.busy(false) return d.isBusy(false)
} }
// Size returns the current size of the display. // Size returns the current size of the display.
+4 -4
View File
@@ -3,9 +3,9 @@ package max6675
import ( import (
"errors" "errors"
"machine"
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/pin"
) )
// ErrThermocoupleOpen is returned when the thermocouple input is open. // ErrThermocoupleOpen is returned when the thermocouple input is open.
@@ -14,16 +14,16 @@ var ErrThermocoupleOpen = errors.New("thermocouple input open")
type Device struct { type Device struct {
bus drivers.SPI bus drivers.SPI
cs machine.Pin cs pin.OutputFunc
} }
// Create a new Device to read from a MAX6675 thermocouple. // Create a new Device to read from a MAX6675 thermocouple.
// Pins must be configured before use. Frequency for SPI // Pins must be configured before use. Frequency for SPI
// should be 4.3MHz maximum. // should be 4.3MHz maximum.
func NewDevice(bus drivers.SPI, cs machine.Pin) *Device { func NewDevice(bus drivers.SPI, cs pin.Output) *Device {
return &Device{ return &Device{
bus: bus, bus: bus,
cs: cs, cs: cs.Set,
} }
} }
+14 -9
View File
@@ -3,31 +3,36 @@
package max72xx package max72xx
import ( import (
"machine"
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
) )
type Device struct { type Device struct {
bus drivers.SPI bus drivers.SPI
cs machine.Pin cs pin.OutputFunc
configurePins func()
} }
// NewDriver creates a new max7219 connection. The SPI wire must already be configured // NewDriver creates a new max7219 connection. The SPI wire must already be configured
// The SPI frequency must not be higher than 10MHz. // The SPI frequency must not be higher than 10MHz.
// parameter cs: the datasheet also refers to this pin as "load" pin. // parameter cs: the datasheet also refers to this pin as "load" pin.
func NewDevice(bus drivers.SPI, cs machine.Pin) *Device { func NewDevice(bus drivers.SPI, cs pin.Output) *Device {
return &Device{ return &Device{
bus: bus, bus: bus,
cs: cs, cs: cs.Set,
configurePins: func() {
legacy.ConfigurePinOut(cs)
},
} }
} }
// Configure setups the pins. // Configure setups the pins.
func (driver *Device) Configure() { func (driver *Device) Configure() {
outPutConfig := machine.PinConfig{Mode: machine.PinOutput} if driver.configurePins == nil {
panic(legacy.ErrConfigBeforeInstantiated)
driver.cs.Configure(outPutConfig) }
driver.configurePins()
} }
// SetScanLimit sets the scan limit. Maximum is 8. // SetScanLimit sets the scan limit. Maximum is 8.
+16 -8
View File
@@ -8,18 +8,20 @@ package mcp2515 // import "tinygo.org/x/drivers/mcp2515"
import ( import (
"errors" "errors"
"fmt" "fmt"
"machine"
"time" "time"
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/legacy"
"tinygo.org/x/drivers/internal/pin"
) )
// Device wraps MCP2515 SPI CAN Module. // Device wraps MCP2515 SPI CAN Module.
type Device struct { type Device struct {
spi SPI spi SPI
cs machine.Pin cs pin.OutputFunc
msg *CANMsg msg *CANMsg
mcpMode byte mcpMode byte
configurePins func()
} }
// CANMsg stores CAN message fields. // CANMsg stores CAN message fields.
@@ -36,15 +38,18 @@ const (
) )
// New returns a new MCP2515 driver. Pass in a fully configured SPI bus. // New returns a new MCP2515 driver. Pass in a fully configured SPI bus.
func New(b drivers.SPI, csPin machine.Pin) *Device { func New(b drivers.SPI, csPin pin.Output) *Device {
d := &Device{ d := &Device{
spi: SPI{ spi: SPI{
bus: b, bus: b,
tx: make([]byte, 0, bufferSize), tx: make([]byte, 0, bufferSize),
rx: make([]byte, 0, bufferSize), rx: make([]byte, 0, bufferSize),
}, },
cs: csPin, cs: csPin.Set,
msg: &CANMsg{}, msg: &CANMsg{},
configurePins: func() {
legacy.ConfigurePinOut(csPin)
},
} }
return d return d
@@ -52,7 +57,10 @@ func New(b drivers.SPI, csPin machine.Pin) *Device {
// Configure sets up the device for communication. // Configure sets up the device for communication.
func (d *Device) Configure() { func (d *Device) Configure() {
d.cs.Configure(machine.PinConfig{Mode: machine.PinOutput}) if d.configurePins == nil {
panic(legacy.ErrConfigBeforeInstantiated)
}
d.configurePins()
} }
const beginTimeoutValue int = 10 const beginTimeoutValue int = 10
+8 -8
View File
@@ -6,18 +6,18 @@ package pcd8544 // import "tinygo.org/x/drivers/pcd8544"
import ( import (
"errors" "errors"
"image/color" "image/color"
"machine"
"time" "time"
"tinygo.org/x/drivers" "tinygo.org/x/drivers"
"tinygo.org/x/drivers/internal/pin"
) )
// Device wraps an SPI connection. // Device wraps an SPI connection.
type Device struct { type Device struct {
bus drivers.SPI bus drivers.SPI
dcPin machine.Pin dcPin pin.OutputFunc
rstPin machine.Pin rstPin pin.OutputFunc
scePin machine.Pin scePin pin.OutputFunc
buffer []byte buffer []byte
width int16 width int16
height int16 height int16
@@ -30,12 +30,12 @@ type Config struct {
} }
// New creates a new PCD8544 connection. The SPI bus must already be configured. // New creates a new PCD8544 connection. The SPI bus must already be configured.
func New(bus drivers.SPI, dcPin, rstPin, scePin machine.Pin) *Device { func New(bus drivers.SPI, dcPin, rstPin, scePin pin.Output) *Device {
return &Device{ return &Device{
bus: bus, bus: bus,
dcPin: dcPin, dcPin: dcPin.Set,
rstPin: rstPin, rstPin: rstPin.Set,
scePin: scePin, scePin: scePin.Set,
} }
} }