revision: modify proposed pin HAL.

Signed-off-by: deadprogram <ron@hybridgroup.com>
This commit is contained in:
deadprogram
2025-09-16 20:33:50 +02:00
parent f55c0b1853
commit dcf53ac04a
38 changed files with 554 additions and 554 deletions
+16 -16
View File
@@ -4,14 +4,14 @@ import (
"time"
"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
// also an I2C interface, but it is not yet supported.
type DeviceSPI struct {
// Chip select pin
csb drivers.PinOutput
csb pin.OutputFunc
buf [7]byte
@@ -23,12 +23,12 @@ type DeviceSPI struct {
// 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 legacy.PinOutput, spi drivers.SPI) *DeviceSPI {
func NewSPI(csb pin.Output, spi drivers.SPI) *DeviceSPI {
return &DeviceSPI{
csb: csb.Set, // chip select
bus: spi,
configurePins: func() {
legacy.ConfigurePinOut(csb)
pin.ConfigureOutput(csb)
},
}
}
@@ -38,10 +38,10 @@ func NewSPI(csb legacy.PinOutput, spi drivers.SPI) *DeviceSPI {
// assumed to be up and running).
func (d *DeviceSPI) Configure() error {
if d.configurePins == nil {
return legacy.ErrConfigBeforeInstantiated
return pin.ErrConfigBeforeInstantiated
}
d.configurePins()
d.csb(true)
d.csb.High()
// The datasheet recommends doing a register read from address 0x7F to get
// SPI communication going:
@@ -93,9 +93,9 @@ func (d *DeviceSPI) ReadTemperature() (temperature int32, err error) {
data[0] = 0x80 | reg_TEMPERATURE_0
data[1] = 0
data[2] = 0
d.csb(false)
d.csb.Low()
err = d.bus.Tx(data, data)
d.csb(true)
d.csb.High()
if err != nil {
return
}
@@ -130,9 +130,9 @@ func (d *DeviceSPI) ReadAcceleration() (x int32, y int32, z int32, err error) {
for i := 1; i < len(data); i++ {
data[i] = 0
}
d.csb(false)
d.csb.Low()
err = d.bus.Tx(data, data)
d.csb(true)
d.csb.High()
if err != nil {
return
}
@@ -160,9 +160,9 @@ func (d *DeviceSPI) ReadRotation() (x int32, y int32, z int32, err error) {
for i := 1; i < len(data); i++ {
data[i] = 0
}
d.csb(false)
d.csb.Low()
err = d.bus.Tx(data, data)
d.csb(true)
d.csb.High()
if err != nil {
return
}
@@ -208,9 +208,9 @@ func (d *DeviceSPI) readRegister(address uint8) uint8 {
data := d.buf[:2]
data[0] = 0x80 | address
data[1] = 0
d.csb(false)
d.csb.Low()
d.bus.Tx(data, data)
d.csb(true)
d.csb.High()
return data[1]
}
@@ -224,7 +224,7 @@ func (d *DeviceSPI) writeRegister(address, data uint8) {
buf[0] = address
buf[1] = data
d.csb(false)
d.csb.Low()
d.bus.Tx(buf, buf)
d.csb(true)
d.csb.High()
}