mirror of
https://github.com/tinygo-org/drivers.git
synced 2026-07-31 21:17:49 +00:00
Compare commits
37 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0eabf16c36 | |||
| f9db7a6d0f | |||
| d6a420bb08 | |||
| c7229ddba6 | |||
| 744fda5eec | |||
| 027c91272e | |||
| 5847506ba6 | |||
| e35e6b8e13 | |||
| 408851a9f5 | |||
| bd88b70511 | |||
| 34da2d208a | |||
| 5cb360a4bf | |||
| 51b604ce97 | |||
| ec680be784 | |||
| 5fb935001e | |||
| 297ad416d3 | |||
| 3fa08112db | |||
| b639f7b12e | |||
| 28d625abfd | |||
| 228e57cf98 | |||
| 6cf1eb86e5 | |||
| 857ab80ae6 | |||
| a31ba26a6c | |||
| 303ec94529 | |||
| 833990f44d | |||
| 28d87eb0c5 | |||
| ae9e8f915e | |||
| 45fad80c3e | |||
| 0304d30b78 | |||
| 7de0a0814e | |||
| 80356fd9d9 | |||
| c4ff8242a7 | |||
| 82c41dbf14 | |||
| c4168864fd | |||
| dbc9022f6a | |||
| d41bc0b85f | |||
| e7f90166ad |
@@ -1,3 +1,28 @@
|
||||
0.33.0
|
||||
---
|
||||
- **new devices**
|
||||
- **ens160**
|
||||
- Add ens160 i2c driver
|
||||
- **lsm303dlhc**
|
||||
- added support for LSM303DLHC e-Compass; (#783)
|
||||
- **seesaw**
|
||||
- add support for Adafruit Seesaw encoders
|
||||
|
||||
- **enhancements**
|
||||
- **ws2812**
|
||||
- add RP2350 support
|
||||
- **ssd1306**
|
||||
- avoid unnecessary heap allocations (#767)
|
||||
- **gps**
|
||||
- allow gps init with address
|
||||
- **lsm6ds3tr**
|
||||
- avoid unnecessary heap allocations (#766)
|
||||
|
||||
- **bugfixes**
|
||||
- **gps**
|
||||
- Fix gps time calculation (#785)
|
||||
|
||||
|
||||
0.32.0
|
||||
---
|
||||
- **enhancements**
|
||||
|
||||
+3
-2
@@ -7,6 +7,7 @@ import (
|
||||
"image/color"
|
||||
|
||||
"tinygo.org/x/drivers"
|
||||
"tinygo.org/x/drivers/internal/legacy"
|
||||
"tinygo.org/x/drivers/internal/pin"
|
||||
)
|
||||
|
||||
@@ -39,8 +40,8 @@ func New(b drivers.SPI) *Device {
|
||||
// implementation of the SPI protocol.
|
||||
func NewSoftwareSPI(sckPin, sdoPin pin.Output, delay uint32) *Device {
|
||||
return New(&bbSPI{SCK: sckPin.Set, SDO: sdoPin.Set, Delay: delay, configurePins: func() {
|
||||
pin.ConfigureOutput(sckPin)
|
||||
pin.ConfigureOutput(sdoPin)
|
||||
legacy.ConfigurePinOut(sckPin)
|
||||
legacy.ConfigurePinOut(sdoPin)
|
||||
}})
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -1,6 +1,7 @@
|
||||
package apa102
|
||||
|
||||
import (
|
||||
"tinygo.org/x/drivers/internal/legacy"
|
||||
"tinygo.org/x/drivers/internal/pin"
|
||||
)
|
||||
|
||||
@@ -19,7 +20,7 @@ type bbSPI struct {
|
||||
// Configure sets up the SCK and SDO pins as outputs and sets them low
|
||||
func (s *bbSPI) Configure() {
|
||||
if s.configurePins == nil {
|
||||
panic(pin.ErrConfigBeforeInstantiated)
|
||||
panic(legacy.ErrConfigBeforeInstantiated)
|
||||
}
|
||||
s.configurePins()
|
||||
s.SCK.Low()
|
||||
|
||||
+3
-3
@@ -4,6 +4,7 @@ import (
|
||||
"time"
|
||||
|
||||
"tinygo.org/x/drivers"
|
||||
"tinygo.org/x/drivers/internal/legacy"
|
||||
"tinygo.org/x/drivers/internal/pin"
|
||||
)
|
||||
|
||||
@@ -28,7 +29,7 @@ func NewSPI(csb pin.Output, spi drivers.SPI) *DeviceSPI {
|
||||
csb: csb.Set, // chip select
|
||||
bus: spi,
|
||||
configurePins: func() {
|
||||
pin.ConfigureOutput(csb)
|
||||
legacy.ConfigurePinOut(csb)
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -38,11 +39,10 @@ func NewSPI(csb pin.Output, spi drivers.SPI) *DeviceSPI {
|
||||
// assumed to be up and running).
|
||||
func (d *DeviceSPI) Configure() error {
|
||||
if d.configurePins == nil {
|
||||
return pin.ErrConfigBeforeInstantiated
|
||||
return legacy.ErrConfigBeforeInstantiated
|
||||
}
|
||||
d.configurePins()
|
||||
d.csb.High()
|
||||
|
||||
// The datasheet recommends doing a register read from address 0x7F to get
|
||||
// SPI communication going:
|
||||
// > If CSB sees a rising edge after power-up, the BMI160 interface switches
|
||||
|
||||
@@ -62,8 +62,8 @@ func (d *Device) Move(steps int32) {
|
||||
|
||||
// Off turns off all motor pins
|
||||
func (d *Device) Off() {
|
||||
for _, p := range d.pins {
|
||||
p.Low()
|
||||
for _, pin := range d.pins {
|
||||
pin.Low()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -7,20 +7,17 @@ import (
|
||||
"tinygo.org/x/drivers/internal/pin"
|
||||
)
|
||||
|
||||
func NewCrossPlatform(stepcount, rpm uint, mode StepMode, pins [4]pin.Output) (*Device, error) {
|
||||
func NewCrossPlatform(stepcount, rpm uint, mode StepMode, pins [4]pin.OutputFunc) (*Device, error) {
|
||||
if stepcount == 0 || rpm == 0 {
|
||||
return nil, errors.New("zero rpm and/or stepcount")
|
||||
}
|
||||
var ps [4]pin.OutputFunc
|
||||
|
||||
for i := range pins {
|
||||
if pins[i] == nil {
|
||||
return nil, errors.New("nil pin")
|
||||
}
|
||||
ps[i] = pins[i].Set
|
||||
}
|
||||
d := &Device{
|
||||
pins: ps,
|
||||
pins: pins,
|
||||
stepDelay: time.Second * 60 / time.Duration((stepcount * rpm)),
|
||||
stepMode: mode,
|
||||
config: func() {},
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"machine"
|
||||
"time"
|
||||
|
||||
"tinygo.org/x/drivers/internal/legacy"
|
||||
"tinygo.org/x/drivers/internal/pin"
|
||||
)
|
||||
|
||||
@@ -20,10 +21,10 @@ func New(config DeviceConfig) (*Device, error) {
|
||||
stepDelay: time.Second * 60 / time.Duration((config.StepCount * config.RPM)),
|
||||
stepMode: config.Mode,
|
||||
config: func() {
|
||||
pin.ConfigureOutput(config.Pin1)
|
||||
pin.ConfigureOutput(config.Pin2)
|
||||
pin.ConfigureOutput(config.Pin3)
|
||||
pin.ConfigureOutput(config.Pin4)
|
||||
legacy.ConfigurePinOut(config.Pin1)
|
||||
legacy.ConfigurePinOut(config.Pin2)
|
||||
legacy.ConfigurePinOut(config.Pin3)
|
||||
legacy.ConfigurePinOut(config.Pin4)
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
@@ -31,7 +32,7 @@ func New(config DeviceConfig) (*Device, error) {
|
||||
// Configure configures the pins of the Device
|
||||
func (d *Device) Configure() {
|
||||
if d.config == nil {
|
||||
panic(pin.ErrConfigBeforeInstantiated)
|
||||
panic(legacy.ErrConfigBeforeInstantiated)
|
||||
}
|
||||
d.config()
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
//go:build tinygo && (rp2040 || stm32 || k210 || esp32c3 || nrf || sam || (avr && (atmega328p || atmega328pb)))
|
||||
//go:build tinygo && (rp2040 || rp2350 || stm32 || k210 || esp32c3 || nrf || sam || (avr && (atmega328p || atmega328pb)))
|
||||
|
||||
// Implementation based on:
|
||||
// https://gist.github.com/aykevl/3fc1683ed77bb0a9c07559dfe857304a
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"machine"
|
||||
"time"
|
||||
|
||||
"tinygo.org/x/drivers"
|
||||
"tinygo.org/x/drivers/honeyhsc"
|
||||
)
|
||||
|
||||
// Data taken from https://github.com/rodan/honeywell_hsc_ssc_i2c/blob/master/hsc_ssc_i2c.cpp
|
||||
// these defaults are valid for the HSCMRNN030PA2A3 chip
|
||||
const (
|
||||
i2cAddress = 0x28
|
||||
// 10%
|
||||
outputMinimum = 0x666
|
||||
// 90% of 2^14 - 1
|
||||
outputMax = 0x399A
|
||||
// min is 0 for sensors that give absolute values
|
||||
pressureMin = 0
|
||||
// 30psi (and we want results in millipascals)
|
||||
// pressureMax = 206842.7
|
||||
pressureMax = 206843 * 1000
|
||||
)
|
||||
|
||||
func main() {
|
||||
bus := machine.I2C0
|
||||
err := bus.Configure(machine.I2CConfig{
|
||||
Frequency: 400_000, // 100kHz minimum and 400kHz I2C maximum clock. 50 to 800 for SPI.
|
||||
SDA: machine.I2C0_SDA_PIN,
|
||||
SCL: machine.I2C0_SCL_PIN,
|
||||
})
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
sensor := honeyhsc.NewDevI2C(bus, i2cAddress, outputMinimum, outputMax, pressureMin, pressureMax)
|
||||
for {
|
||||
time.Sleep(time.Second)
|
||||
const measuremask = drivers.Pressure | drivers.Temperature
|
||||
err := sensor.Update(measuremask)
|
||||
if err != nil {
|
||||
println("error updating measurements:", err.Error())
|
||||
continue
|
||||
}
|
||||
P := sensor.Pressure()
|
||||
T := sensor.Temperature()
|
||||
println("pressure:", P, "temperature:", T)
|
||||
}
|
||||
}
|
||||
+12
-3
@@ -14,9 +14,18 @@ func main() {
|
||||
i2c.Configure(machine.I2CConfig{SCL: machine.SCL1_PIN, SDA: machine.SDA1_PIN})
|
||||
|
||||
accel := lis3dh.New(i2c)
|
||||
accel.Address = lis3dh.Address1 // address on the Circuit Playground Express
|
||||
accel.Configure()
|
||||
accel.SetRange(lis3dh.RANGE_2_G)
|
||||
err := accel.Configure(lis3dh.Config{
|
||||
Address: lis3dh.Address1, // address on the Circuit Playground Express
|
||||
})
|
||||
for err != nil {
|
||||
println("could not configure LIS3DH:", err)
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
err = accel.SetRange(lis3dh.RANGE_2_G)
|
||||
for err != nil {
|
||||
println("could not set acceleration range:", err)
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
|
||||
println(accel.Connected())
|
||||
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"machine"
|
||||
"time"
|
||||
|
||||
"tinygo.org/x/drivers/si5351"
|
||||
)
|
||||
|
||||
// Simple demo of the SI5351 clock generator.
|
||||
// This is like the Arduino library example:
|
||||
// https://github.com/adafruit/Adafruit_Si5351_Library/blob/master/examples/si5351/si5351.ino
|
||||
// Which will configure the chip with:
|
||||
// - PLL A at 900mhz
|
||||
// - PLL B at 616.66667mhz
|
||||
// - Clock 0 at 112.5mhz, using PLL A as a source divided by 8
|
||||
// - Clock 1 at 13.5531mhz, using PLL B as a source divided by 45.5
|
||||
// - Clock 2 at 10.76khz, using PLL B as a source divided by 900 and further divided with an R divider of 64.
|
||||
|
||||
func main() {
|
||||
time.Sleep(5 * time.Second)
|
||||
|
||||
println("Si5351 Clockgen Test")
|
||||
println()
|
||||
|
||||
// Configure I2C bus
|
||||
machine.I2C0.Configure(machine.I2CConfig{})
|
||||
|
||||
// Create driver instance
|
||||
clockgen := si5351.New(machine.I2C0)
|
||||
|
||||
// Verify device wired properly
|
||||
connected, err := clockgen.Connected()
|
||||
if err != nil {
|
||||
println("Unable to read device status")
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
if !connected {
|
||||
for {
|
||||
println("Unable to detect si5351 device")
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}
|
||||
|
||||
// Initialise device
|
||||
clockgen.Configure()
|
||||
|
||||
// Now configue the PLLs and clock outputs.
|
||||
// The PLLs can be configured with a multiplier and division of the on-board
|
||||
// 25mhz reference crystal. For example configure PLL A to 900mhz by multiplying
|
||||
// by 36. This uses an integer multiplier which is more accurate over time
|
||||
// but allows less of a range of frequencies compared to a fractional
|
||||
// multiplier shown next.
|
||||
clockgen.ConfigurePLL(si5351.PLL_A, 36, 0, 1) // Multiply 25mhz by 36
|
||||
println("PLL A frequency: 900mhz")
|
||||
|
||||
// And next configure PLL B to 616.6667mhz by multiplying 25mhz by 24.667 using
|
||||
// the fractional multiplier configuration. Notice you specify the integer
|
||||
// multiplier and then a numerator and denominator as separate values, i.e.
|
||||
// numerator 2 and denominator 3 means 2/3 or 0.667. This fractional
|
||||
// configuration is susceptible to some jitter over time but can set a larger
|
||||
// range of frequencies.
|
||||
clockgen.ConfigurePLL(si5351.PLL_B, 24, 2, 3) // Multiply 25mhz by 24.667 (24 2/3)
|
||||
println("PLL B frequency: 616.6667mhz")
|
||||
|
||||
// Now configure the clock outputs. Each is driven by a PLL frequency as input
|
||||
// and then further divides that down to a specific frequency.
|
||||
// Configure clock 0 output to be driven by PLL A divided by 8, so an output
|
||||
// of 112.5mhz (900mhz / 8). Again this uses the most precise integer division
|
||||
// but can't set as wide a range of values.
|
||||
clockgen.ConfigureMultisynth(0, si5351.PLL_A, 8, 0, 1) // Divide by 8 (8 0/1)
|
||||
println("Clock 0: 112.5mhz")
|
||||
|
||||
// Next configure clock 1 to be driven by PLL B divided by 45.5 to get
|
||||
// 13.5531mhz (616.6667mhz / 45.5). This uses fractional division and again
|
||||
// notice the numerator and denominator are explicitly specified. This is less
|
||||
// precise but allows a large range of frequencies.
|
||||
clockgen.ConfigureMultisynth(1, si5351.PLL_B, 45, 1, 2) // Divide by 45.5 (45 1/2)
|
||||
println("Clock 1: 13.5531mhz")
|
||||
|
||||
// Finally configure clock 2 to be driven by PLL B divided once by 900 to get
|
||||
// down to 685.15 khz and then further divided by a special R divider that
|
||||
// divides 685.15 khz by 64 to get a final output of 10.706khz.
|
||||
clockgen.ConfigureMultisynth(2, si5351.PLL_B, 900, 0, 1) // Divide by 900 (900 0/1)
|
||||
// Set the R divider, this can be a value of:
|
||||
// - R_DIV_1: divider of 1
|
||||
// - R_DIV_2: divider of 2
|
||||
// - R_DIV_4: divider of 4
|
||||
// - R_DIV_8: divider of 8
|
||||
// - R_DIV_16: divider of 16
|
||||
// - R_DIV_32: divider of 32
|
||||
// - R_DIV_64: divider of 64
|
||||
// - R_DIV_128: divider of 128
|
||||
clockgen.ConfigureRdiv(2, si5351.R_DIV_64)
|
||||
println("Clock 2: 10.706khz")
|
||||
|
||||
// After configuring PLLs and clocks, enable the outputs.
|
||||
clockgen.EnableOutputs()
|
||||
|
||||
for {
|
||||
time.Sleep(5 * time.Second)
|
||||
println()
|
||||
println("Clock 0: 112.5mhz")
|
||||
println("Clock 1: 13.5531mhz")
|
||||
println("Clock 2: 10.706khz")
|
||||
}
|
||||
}
|
||||
@@ -46,6 +46,10 @@ var DefaultDeviceIdentifier = DeviceIdentifierFunc(func(id JedecID) Attrs {
|
||||
return GD25Q16C()
|
||||
case 0xC84017:
|
||||
return GD25Q64C()
|
||||
case 0x856015:
|
||||
return P25Q16H()
|
||||
case 0xEF4014:
|
||||
return W25Q80DV()
|
||||
case 0xEF4015:
|
||||
return W25Q16JVIQ()
|
||||
case 0xEF4016:
|
||||
@@ -239,6 +243,24 @@ func GD25Q64C() Attrs {
|
||||
}
|
||||
}
|
||||
|
||||
// Settings for the Puya P25Q16H 2MiB SPI flash.
|
||||
// Datasheet: https://files.seeedstudio.com/wiki/github_weiruanexample/Flash_P25Q16H-UXH-IR_Datasheet.pdf
|
||||
func P25Q16H() Attrs {
|
||||
return Attrs{
|
||||
TotalSize: 1 << 21, // 2 MiB
|
||||
StartUp: 5000 * time.Microsecond,
|
||||
JedecID: JedecID{0x85, 0x60, 0x15},
|
||||
MaxClockSpeedMHz: 55,
|
||||
QuadEnableBitMask: 0x02,
|
||||
HasSectorProtection: true,
|
||||
SupportsFastRead: true,
|
||||
SupportsQSPI: true,
|
||||
SupportsQSPIWrites: true,
|
||||
WriteStatusSplit: true,
|
||||
SingleStatusByte: false,
|
||||
}
|
||||
}
|
||||
|
||||
// Settings for the Winbond W25Q16JV-IQ 2MiB SPI flash. Note that JV-IM has a
|
||||
// different .memory_type (0x70) Datasheet:
|
||||
// https://www.winbond.com/resource-files/w25q16jv%20spi%20revf%2005092017.pdf
|
||||
@@ -380,6 +402,25 @@ func W25Q80DL() Attrs {
|
||||
TotalSize: 1 << 20, // 1 MiB
|
||||
StartUp: 5000 * time.Microsecond,
|
||||
JedecID: JedecID{0xEF, 0x60, 0x14},
|
||||
MaxClockSpeedMHz: 80,
|
||||
QuadEnableBitMask: 0x02,
|
||||
HasSectorProtection: false,
|
||||
SupportsFastRead: true,
|
||||
SupportsQSPI: true,
|
||||
SupportsQSPIWrites: false,
|
||||
WriteStatusSplit: false,
|
||||
SingleStatusByte: false,
|
||||
}
|
||||
}
|
||||
|
||||
// Settings for the Winbond W25Q80DV 2MiB SPI flash.
|
||||
// Datasheet:
|
||||
// https://www.winbond.com/resource-files/w25q80dv%20dl_revh_10022015.pdf
|
||||
func W25Q80DV() Attrs {
|
||||
return Attrs{
|
||||
TotalSize: 1 << 21, // 2 MiB
|
||||
StartUp: 5000 * time.Microsecond,
|
||||
JedecID: JedecID{0xEF, 0x40, 0x14},
|
||||
MaxClockSpeedMHz: 104,
|
||||
QuadEnableBitMask: 0x02,
|
||||
HasSectorProtection: false,
|
||||
|
||||
+12
-13
@@ -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
|
||||
// panel controller.
|
||||
//
|
||||
@@ -5,29 +8,28 @@
|
||||
package ft6336
|
||||
|
||||
import (
|
||||
"machine"
|
||||
|
||||
"tinygo.org/x/drivers"
|
||||
"tinygo.org/x/drivers/internal/legacy"
|
||||
"tinygo.org/x/drivers/internal/pin"
|
||||
"tinygo.org/x/drivers/touch"
|
||||
)
|
||||
|
||||
// Device wraps FT6336 I2C Self-Capacitive touch
|
||||
type Device struct {
|
||||
bus drivers.I2C
|
||||
buf []byte
|
||||
Address uint8
|
||||
configurePins func()
|
||||
bus drivers.I2C
|
||||
buf []byte
|
||||
Address uint8
|
||||
intPin machine.Pin
|
||||
}
|
||||
|
||||
// New returns FT6336 device for the provided I2C bus using default address.
|
||||
func New(i2c drivers.I2C, intPin pin.Input) *Device {
|
||||
func New(i2c drivers.I2C, intPin machine.Pin) *Device {
|
||||
return &Device{
|
||||
bus: i2c,
|
||||
buf: make([]byte, 11),
|
||||
Address: Address,
|
||||
configurePins: func() {
|
||||
pin.ConfigureInputPulldown(intPin)
|
||||
},
|
||||
intPin: intPin,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,11 +39,8 @@ type Config struct {
|
||||
|
||||
// Configure the FT6336 device.
|
||||
func (d *Device) Configure(config Config) error {
|
||||
if d.configurePins == nil {
|
||||
return pin.ErrConfigBeforeInstantiated
|
||||
}
|
||||
d.write1Byte(0xA4, 0x00)
|
||||
d.configurePins()
|
||||
d.intPin.Configure(machine.PinConfig{Mode: machine.PinInputPulldown})
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
+5
-4
@@ -10,6 +10,7 @@ import (
|
||||
"errors"
|
||||
|
||||
"tinygo.org/x/drivers"
|
||||
"tinygo.org/x/drivers/internal/legacy"
|
||||
"tinygo.org/x/drivers/internal/pin"
|
||||
)
|
||||
|
||||
@@ -53,10 +54,10 @@ type Config struct {
|
||||
|
||||
// New creates a new ST7789 connection. The SPI wire must already be configured.
|
||||
func New(bus drivers.SPI, resetPin, dcPin, csPin, blPin pin.Output) Device {
|
||||
pin.ConfigureOutput(resetPin)
|
||||
pin.ConfigureOutput(dcPin)
|
||||
pin.ConfigureOutput(csPin)
|
||||
pin.ConfigureOutput(blPin)
|
||||
legacy.ConfigurePinOut(resetPin)
|
||||
legacy.ConfigurePinOut(dcPin)
|
||||
legacy.ConfigurePinOut(csPin)
|
||||
legacy.ConfigurePinOut(blPin)
|
||||
return Device{
|
||||
bus: bus,
|
||||
resetPin: resetPin.Set,
|
||||
|
||||
+4
-3
@@ -7,6 +7,7 @@ package hcsr04
|
||||
import (
|
||||
"time"
|
||||
|
||||
"tinygo.org/x/drivers/internal/legacy"
|
||||
"tinygo.org/x/drivers/internal/pin"
|
||||
)
|
||||
|
||||
@@ -25,8 +26,8 @@ func New(trigger pin.Output, echo pin.Input) Device {
|
||||
trigger: trigger.Set,
|
||||
echo: echo.Get,
|
||||
configurePins: func() {
|
||||
pin.ConfigureOutput(trigger)
|
||||
pin.ConfigureInput(echo)
|
||||
legacy.ConfigurePinOut(trigger)
|
||||
legacy.ConfigurePinInput(echo)
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -34,7 +35,7 @@ func New(trigger pin.Output, echo pin.Input) Device {
|
||||
// Configure configures the pins of the Device
|
||||
func (d *Device) Configure() {
|
||||
if d.configurePins == nil {
|
||||
panic(pin.ErrConfigBeforeInstantiated)
|
||||
panic(legacy.ErrConfigBeforeInstantiated)
|
||||
}
|
||||
d.configurePins()
|
||||
}
|
||||
|
||||
+191
@@ -0,0 +1,191 @@
|
||||
package honeyhsc
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"math"
|
||||
|
||||
"tinygo.org/x/drivers"
|
||||
)
|
||||
|
||||
var (
|
||||
errSensorMissing = errors.New("hsc: not connected")
|
||||
errDiagnostic = errors.New("hsc: diagnostic error")
|
||||
)
|
||||
|
||||
const (
|
||||
measuremask = drivers.Pressure | drivers.Temperature
|
||||
statusMask = 0b1100_0000
|
||||
statusOffset = 6
|
||||
)
|
||||
|
||||
// DevI2C is the TruStability® High Accuracy Silicon Ceramic (HSC) Series is a piezoresistive silicon pressure sensor offering a ratiometric
|
||||
// analog or digital output for reading pressure over the specified full scale pressure span and temperature range.
|
||||
type DevI2C struct {
|
||||
bus drivers.I2C
|
||||
dev
|
||||
addr uint8
|
||||
buf [6]byte
|
||||
}
|
||||
|
||||
// NewDevI2C creates and returns a new DevI2C that communicates with an HSC device over the provided I2C bus.
|
||||
// Parameters:
|
||||
// - bus: the I2C bus to use.
|
||||
// - addr: the 7-bit I2C address of the sensor.
|
||||
// - outMin, outMax: raw output code range (counts) corresponding to the pressure span. Depends on sensor model.
|
||||
// - pMin, pMax: pressure range endpoints in millipascals (mPa). Depends on sensor model.
|
||||
//
|
||||
// The returned DevI2C will use these calibration parameters to convert raw bridge counts to pressure.
|
||||
func NewDevI2C(bus drivers.I2C, addr, outMin, outMax uint16, pMin, pMax int32) *DevI2C {
|
||||
h := &DevI2C{
|
||||
bus: bus,
|
||||
addr: uint8(addr),
|
||||
dev: dev{
|
||||
cmin: outMin,
|
||||
cmax: outMax,
|
||||
pmin: pMin,
|
||||
pmax: pMax,
|
||||
},
|
||||
}
|
||||
return h
|
||||
}
|
||||
|
||||
// ReadTemperature reads and returns the temperature in milliKelvin (mC) from the I2C-attached HSC device.
|
||||
// It performs an Update internally to get the latest temperature value.
|
||||
func (h *DevI2C) ReadTemperature() (int32, error) {
|
||||
err := h.Update(drivers.Temperature)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return h.Temperature(), nil
|
||||
}
|
||||
|
||||
// Update reads both temperature and pressure data from the I2C-attached HSC device when
|
||||
// the requested measurement mask includes pressure or temperature.
|
||||
// If neither pressure nor temperature is requested, Update is a no-op.
|
||||
func (d *DevI2C) Update(which drivers.Measurement) error {
|
||||
// Update performs an I2C transaction to read 4 bytes, parses the status bits, 14-bit bridge data and
|
||||
// temperature bits, and forwards them to the internal update routine. Any I2C transport error is returned,
|
||||
// as well as errors produced by the internal update (e.g. errSensorMissing, errDiagnostic).
|
||||
if which&measuremask == 0 {
|
||||
return nil
|
||||
}
|
||||
rbuf := d.buf[:4]
|
||||
wbuf := d.buf[4:6]
|
||||
const reg = 0
|
||||
value := (d.addr << 1) | 1
|
||||
wbuf[0] = reg
|
||||
wbuf[1] = value
|
||||
err := d.bus.Tx(uint16(d.addr), wbuf, rbuf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
status := (rbuf[0] & statusMask) >> statusOffset
|
||||
bridgeData := (uint16(rbuf[0]&^statusMask) << 8) | uint16(rbuf[1])
|
||||
tempData := uint16(rbuf[2])<<8 | uint16(rbuf[3]&0xe0)>>5
|
||||
return d.dev.update(status, bridgeData, tempData)
|
||||
}
|
||||
|
||||
type pinout func(level bool)
|
||||
|
||||
// DevI2C is the TruStability® High Accuracy Silicon Ceramic (HSC) Series is a piezoresistive silicon pressure sensor offering a ratiometric
|
||||
// analog or digital output for reading pressure over the specified full scale pressure span and temperature range.
|
||||
type DevSPI struct {
|
||||
spi drivers.SPI
|
||||
cs pinout
|
||||
dev
|
||||
buf [4]byte
|
||||
}
|
||||
|
||||
// NewDevSPI creates and returns a new DevSPI that communicates with an HSC device over SPI.
|
||||
// Parameters:
|
||||
// - conn: the SPI connection to use.
|
||||
// - cs: a chip-select function that drives the device select line low/high.
|
||||
// - outMin, outMax: raw output code range (counts) corresponding to the pressure span. Depends on sensor model.
|
||||
// - pMin, pMax: pressure range endpoints in millipascals (mPa). Depends on sensor model.
|
||||
//
|
||||
// The function returns the constructed DevSPI and an error value (currently always nil).
|
||||
func NewDevSPI(conn drivers.SPI, cs pinout, outMin, outMax uint16, pMin, pMax int32) (*DevSPI, error) {
|
||||
h := &DevSPI{
|
||||
spi: conn,
|
||||
cs: cs,
|
||||
dev: dev{
|
||||
cmin: outMin,
|
||||
cmax: outMax,
|
||||
pmin: pMin,
|
||||
pmax: pMax,
|
||||
},
|
||||
}
|
||||
return h, nil
|
||||
}
|
||||
|
||||
// ReadTemperature reads and returns the temperature in milliKelvin (mC) from the SPI-attached HSC device.
|
||||
// It performs an Update internally to get the latest temperature value.
|
||||
func (h *DevSPI) ReadTemperature() (int32, error) {
|
||||
err := h.Update(drivers.Temperature)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return h.Temperature(), nil
|
||||
}
|
||||
|
||||
// Update reads pressure and temperature data from the SPI-attached HSC device when the requested measurement mask includes
|
||||
// pressure or temperature. If neither pressure nor temperature is requested, Update is a no-op.
|
||||
func (h *DevSPI) Update(which drivers.Measurement) error {
|
||||
// It toggles the provided chip-select, performs an SPI transfer to read 4 bytes, parses the status bits,
|
||||
// 14-bit bridge data and temperature bits, and forwards them to the internal update routine. Any SPI
|
||||
// transport error is returned, as well as errors produced by the internal update (e.g. errSensorMissing, errDiagnostic).
|
||||
if which&measuremask == 0 {
|
||||
return nil
|
||||
}
|
||||
buf := &h.buf
|
||||
h.cs(false)
|
||||
err := h.spi.Tx(nil, buf[:4])
|
||||
h.cs(true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// First two bits are status bits.
|
||||
status := (buf[0] & statusMask) >> statusOffset
|
||||
bridgeData := (uint16(buf[0]&^statusMask) << 8) | uint16(buf[1])
|
||||
|
||||
tempData := uint16(buf[2])<<8 | uint16(buf[3]&0xe0)>>5
|
||||
return h.dev.update(status, bridgeData, tempData)
|
||||
}
|
||||
|
||||
type dev struct {
|
||||
pressure int32
|
||||
temp int32
|
||||
cmin, cmax uint16
|
||||
pmin, pmax int32
|
||||
}
|
||||
|
||||
// Pressure returns the most recently computed pressure value in millipascals (mPa).
|
||||
// The value is taken from the last successful Update.
|
||||
func (d *dev) Pressure() int32 {
|
||||
return d.pressure
|
||||
}
|
||||
|
||||
// Temperature returns the most recently read temperature value in milliKelvin (mC).
|
||||
// The value is taken from the last successful Update.
|
||||
func (d *dev) Temperature() int32 {
|
||||
return d.temp + 273_150
|
||||
}
|
||||
|
||||
// update interprets raw sensor fields (status, bridgeData, tempData) and updates the dev's stored
|
||||
// pressure and temperature. It returns errSensorMissing when the temperature raw value indicates no sensor
|
||||
// (tempData == math.MaxUint16), errDiagnostic when the status indicates a device diagnostic condition
|
||||
// (status == 3), or nil on success. Pressure is computed with integer arithmetic using the configured
|
||||
// cmin/cmax -> pmin/pmax linear mapping in order to avoid overflows.
|
||||
func (d *dev) update(status uint8, bridgeData, tempData uint16) error {
|
||||
if tempData == math.MaxUint16 {
|
||||
return errSensorMissing
|
||||
} else if status == 3 {
|
||||
return errDiagnostic
|
||||
}
|
||||
|
||||
// Take care not to overflow here.
|
||||
p := (int32(bridgeData)-int32(d.cmin))*(d.pmax-d.pmin)/int32(d.cmax-d.cmin) + d.pmin
|
||||
d.temp = int32(tempData)
|
||||
d.pressure = p
|
||||
return nil
|
||||
}
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
package hts221
|
||||
|
||||
import "tinygo.org/x/drivers"
|
||||
|
||||
// Configure sets up the HTS221 device for communication.
|
||||
func (d *Device) Configure() {
|
||||
// read calibration data
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
package legacy
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"tinygo.org/x/drivers/internal/pin"
|
||||
)
|
||||
|
||||
// The pingconfig group of files serve to abstract away
|
||||
// pin configuration calls on the machine.Pin type.
|
||||
// It was observed this way of developing drivers was
|
||||
// non-portable and unusable on "big" Go projects so
|
||||
// future projects should NOT configure pins in driver code.
|
||||
// Users must configure pins before passing them as arguments
|
||||
// to drivers.
|
||||
|
||||
// ConfigurePinOut is a legacy function used to configure pins as outputs.
|
||||
//
|
||||
// Deprecated: Do not configure pins in drivers.
|
||||
// This is a legacy feature and should only be used by drivers that
|
||||
// previously configured pins in initialization to avoid breaking users.
|
||||
func ConfigurePinOut(po pin.Output) {
|
||||
configurePinOut(po)
|
||||
}
|
||||
|
||||
// ConfigurePinInput is a legacy function used to configure pins as inputs.
|
||||
//
|
||||
// Deprecated: Do not configure pins in drivers.
|
||||
// This is a legacy feature and should only be used by drivers that
|
||||
// previously configured pins in initialization to avoid breaking users.
|
||||
func ConfigurePinInputPulldown(pi pin.Input) {
|
||||
configurePinInputPulldown(pi)
|
||||
}
|
||||
|
||||
// ConfigurePinInput is a legacy function used to configure pins as inputs.
|
||||
//
|
||||
// Deprecated: Do not configure pins in drivers.
|
||||
// This is a legacy feature and should only be used by drivers that
|
||||
// previously configured pins in initialization to avoid breaking users.
|
||||
func ConfigurePinInput(pi pin.Input) {
|
||||
configurePinInput(pi)
|
||||
}
|
||||
|
||||
// ConfigurePinInput is a legacy function used to configure pins as inputs.
|
||||
//
|
||||
// Deprecated: Do not configure pins in drivers.
|
||||
// This is a legacy feature and should only be used by drivers that
|
||||
// previously configured pins in initialization to avoid breaking users.
|
||||
func ConfigurePinInputPullup(pi pin.Input) {
|
||||
configurePinInputPullup(pi)
|
||||
}
|
||||
|
||||
// PinIsNoPin returns true if the argument is a machine.Pin type and is the machine.NoPin predeclared type.
|
||||
//
|
||||
// Deprecated: Drivers do not require pin knowledge from now on.
|
||||
func PinIsNoPin(pin any) bool {
|
||||
return pinIsNoPin(pin)
|
||||
}
|
||||
|
||||
var (
|
||||
ErrConfigBeforeInstantiated = errors.New("device must be instantiated with New before calling Configure method")
|
||||
)
|
||||
@@ -0,0 +1,15 @@
|
||||
//go:build !tinygo
|
||||
|
||||
package legacy
|
||||
|
||||
import "tinygo.org/x/drivers/internal/pin"
|
||||
|
||||
// This file compiles for non-tinygo builds
|
||||
// for use with "big" or "upstream" Go where
|
||||
// there is no machine package.
|
||||
|
||||
func configurePinOut(p pin.Output) {}
|
||||
func configurePinInput(p pin.Input) {}
|
||||
func configurePinInputPulldown(p pin.Input) {}
|
||||
func configurePinInputPullup(p pin.Input) {}
|
||||
func pinIsNoPin(a any) bool { return false }
|
||||
@@ -1,6 +1,6 @@
|
||||
//go:build baremetal && fe310
|
||||
|
||||
package pin
|
||||
package legacy
|
||||
|
||||
import "machine"
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
//go:build baremetal && !fe310
|
||||
|
||||
package pin
|
||||
package legacy
|
||||
|
||||
import "machine"
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
//go:build baremetal
|
||||
|
||||
package legacy
|
||||
|
||||
import (
|
||||
"machine"
|
||||
|
||||
"tinygo.org/x/drivers/internal/pin"
|
||||
)
|
||||
|
||||
func configurePinOut(po pin.Output) {
|
||||
configurePin(po, machine.PinOutput)
|
||||
}
|
||||
|
||||
func configurePinInputPulldown(pi pin.Input) {
|
||||
configurePin(pi, pulldown) // some chips do not have pull down, in which case pulldown==machine.PinInput.
|
||||
}
|
||||
|
||||
func configurePinInput(pi pin.Input) {
|
||||
configurePin(pi, machine.PinInput)
|
||||
}
|
||||
|
||||
func configurePinInputPullup(pi pin.Input) {
|
||||
configurePin(pi, pullup) // some chips do not have pull up, in which case pullup==machine.PinInput.
|
||||
}
|
||||
|
||||
func pinIsNoPin(a any) bool {
|
||||
p, ok := a.(machine.Pin)
|
||||
return ok && p == machine.NoPin
|
||||
}
|
||||
|
||||
func configurePin(p any, mode machine.PinMode) {
|
||||
machinePin, ok := p.(machine.Pin)
|
||||
if ok {
|
||||
machinePin.Configure(machine.PinConfig{Mode: mode})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
// package pin implements a TinyGo Pin HAL.
|
||||
// It serves to eliminate machine.Pin from driver constructors
|
||||
// so that drivers can be used in "big" Go projects where
|
||||
// there is no machine package.
|
||||
// This file contains both function and interface-style Pin HAL definitions.
|
||||
package pin
|
||||
|
||||
// OutputFunc is hardware abstraction for a pin which outputs a
|
||||
// digital signal (high or low level).
|
||||
//
|
||||
// // Code conversion demo: from machine.Pin to pin.OutputFunc
|
||||
// led := machine.LED
|
||||
// led.Configure(machine.PinConfig{Mode: machine.Output})
|
||||
// var pin pin.OutputFunc = led.Set // Going from a machine.Pin to a pin.OutputFunc
|
||||
//
|
||||
// This is an alternative to [Output] which is an interface type.
|
||||
type OutputFunc func(level bool)
|
||||
|
||||
// High sets the underlying pin's level to high. This is equivalent to calling PinOutput(true).
|
||||
func (setPin OutputFunc) High() {
|
||||
setPin(true)
|
||||
}
|
||||
|
||||
// Low sets the underlying pin's level to low. This is equivalent to calling PinOutput(false).
|
||||
func (setPin OutputFunc) Low() {
|
||||
setPin(false)
|
||||
}
|
||||
|
||||
// InputFunc is hardware abstraction for a pin which receives a
|
||||
// digital signal and reads it (high or low level).
|
||||
//
|
||||
// // Code conversion demo: from machine.Pin to pin.InputFunc
|
||||
// input := machine.LED
|
||||
// input.Configure(machine.PinConfig{Mode: machine.PinInputPulldown}) // or use machine.PinInputPullup or machine.Input
|
||||
// var pin pin.InputFunc = input.Get // Going from a machine.Pin to a pin.InputFunc
|
||||
//
|
||||
// This is an alternative to [Input] which is an interface type.
|
||||
type InputFunc func() (level bool)
|
||||
|
||||
// // Below is an example on how to define a input/output pin HAL for a
|
||||
// // pin that must switch between input and output mode:
|
||||
//
|
||||
// var pinIsOutput bool
|
||||
// var po PinOutputFunc = func(b bool) {
|
||||
// if !pinIsOutput {
|
||||
// pin.Configure(outputMode)
|
||||
// pinIsOutput = true
|
||||
// }
|
||||
// pin.Set(b)
|
||||
// }
|
||||
//
|
||||
// var pi PinInputFunc = func() bool {
|
||||
// if pinIsOutput {
|
||||
// pin.Configure(inputMode)
|
||||
// pinIsOutput = false
|
||||
// }
|
||||
// return pin.Get()
|
||||
// }
|
||||
|
||||
// Output interface represents a pin hardware abstraction layer for a pin that can output a digital signal.
|
||||
//
|
||||
// This is an alternative to [OutputFunc] abstraction which is a function type.
|
||||
type Output interface {
|
||||
Set(level bool)
|
||||
}
|
||||
|
||||
// Input interface represents a pin hardware abstraction layer for a pin that can read a digital signal.
|
||||
//
|
||||
// This is an alternative to [InputFunc] abstraction which is a function type.
|
||||
type Input interface {
|
||||
Get() (level bool)
|
||||
}
|
||||
@@ -1,98 +0,0 @@
|
||||
package pin
|
||||
|
||||
import "errors"
|
||||
|
||||
// OutputFunc is hardware abstraction for a pin which outputs a
|
||||
// digital signal (high or low level).
|
||||
//
|
||||
// // Code conversion demo: from machine.Pin to pin.OutputFunc
|
||||
// led := machine.LED
|
||||
// led.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
// var p pin.OutputFuncFunc = led.Set // Going from a machine.Pin to a pin.OutputFunc
|
||||
type OutputFunc func(level bool)
|
||||
|
||||
func (o OutputFunc) High() {
|
||||
o(true)
|
||||
}
|
||||
|
||||
func (o OutputFunc) Low() {
|
||||
o(false)
|
||||
}
|
||||
|
||||
// InputFunc is hardware abstraction for a pin which receives a
|
||||
// digital signal and reads it (high or low level).
|
||||
//
|
||||
// // Code conversion demo: from machine.Pin to pin.InputFunc
|
||||
// input := machine.LED
|
||||
// input.Configure(machine.PinConfig{Mode: machine.PinInputPulldown}) // or use machine.PinInputPullup or machine.PinInput
|
||||
// var p pin.InputFunc = input.Get // Going from a machine.Pin to a drivers.PinInput
|
||||
type InputFunc func() (level bool)
|
||||
|
||||
// PinOutput represents a pin hardware abstraction layer for a pin that can output a digital signal.
|
||||
// This is an wrapper to pin.OutputFunc abstraction which is a function type.
|
||||
//
|
||||
// func New(p1, p2, p3 pin.Output) *Device {
|
||||
// return NewWithPinfuncs(p1.Set, p2.Set, p3.Set)
|
||||
// }
|
||||
//
|
||||
// func NewWithPinfuncs(p1, p2, p3 pin.OutputFunc) *Device {
|
||||
// return &Device{p1:p1, p2:p2, p3:p3}
|
||||
// }
|
||||
//
|
||||
// [relevant issue]: https://github.com/tinygo-org/drivers/pull/749/files
|
||||
type Output interface {
|
||||
Set(level bool)
|
||||
}
|
||||
|
||||
// PinInput represents a pin hardware abstraction layer. See [PinOutput] for
|
||||
// more information on why this is "legacy".
|
||||
type Input interface {
|
||||
Get() (level bool)
|
||||
}
|
||||
|
||||
// ConfigureOutput is a legacy function used to configure pins as outputs.
|
||||
//
|
||||
// Deprecated: You should not configure pins in drivers.
|
||||
// This is a legacy feature and should only be used by drivers that
|
||||
// previously configured pins in initialization to avoid breaking users.
|
||||
func ConfigureOutput(po Output) {
|
||||
configureOutput(po)
|
||||
}
|
||||
|
||||
// ConfigureInput is a legacy function used to configure pins as inputs.
|
||||
//
|
||||
// Deprecated: You should not configure pins in drivers.
|
||||
// This is a legacy feature and should only be used by drivers that
|
||||
// previously configured pins in initialization to avoid breaking users.
|
||||
func ConfigureInputPulldown(pi Input) {
|
||||
configureInputPulldown(pi)
|
||||
}
|
||||
|
||||
// ConfigureInput is a legacy function used to configure pins as inputs.
|
||||
//
|
||||
// Deprecated: You should not configure pins in drivers.
|
||||
// This is a legacy feature and should only be used by drivers that
|
||||
// previously configured pins in initialization to avoid breaking users.
|
||||
func ConfigureInput(pi Input) {
|
||||
configureInput(pi)
|
||||
}
|
||||
|
||||
// ConfigureInputPullup is a legacy function used to configure pins as inputs.
|
||||
//
|
||||
// Deprecated: You should not configure pins in drivers.
|
||||
// This is a legacy feature and should only be used by drivers that
|
||||
// previously configured pins in initialization to avoid breaking users.
|
||||
func ConfigureInputPullup(pi Input) {
|
||||
configureInputPullup(pi)
|
||||
}
|
||||
|
||||
// IsNotPin returns true if the argument is a machine.Pin type and is the machine.NoPin predeclared type.
|
||||
//
|
||||
// Deprecated: Drivers should not require pin knowledge.
|
||||
func IsNotPin(pin any) bool {
|
||||
return isNotPin(pin)
|
||||
}
|
||||
|
||||
var (
|
||||
ErrConfigBeforeInstantiated = errors.New("device must be instantiated with New before calling Configure method")
|
||||
)
|
||||
@@ -1,9 +0,0 @@
|
||||
//go:build !baremetal
|
||||
|
||||
package pin
|
||||
|
||||
func configureOutput(p Output) {}
|
||||
func configureInput(p Input) {}
|
||||
func configureInputPulldown(p Input) {}
|
||||
func configureInputPullup(p Input) {}
|
||||
func isNotPin(a any) bool { return false }
|
||||
@@ -1,33 +0,0 @@
|
||||
//go:build baremetal
|
||||
|
||||
package pin
|
||||
|
||||
import "machine"
|
||||
|
||||
func configureOutput(p Output) {
|
||||
configure(p, machine.PinOutput)
|
||||
}
|
||||
|
||||
func configureInputPulldown(p Input) {
|
||||
configure(p, pulldown) // some chips do not have pull down, in which case pulldown==machine.PinInput.
|
||||
}
|
||||
|
||||
func configureInput(p Input) {
|
||||
configure(p, machine.PinInput)
|
||||
}
|
||||
|
||||
func configureInputPullup(p Input) {
|
||||
configure(p, pullup) // some chips do not have pull up, in which case pullup==machine.PinInput.
|
||||
}
|
||||
|
||||
func isNotPin(a any) bool {
|
||||
p, ok := a.(machine.Pin)
|
||||
return ok && p == machine.NoPin
|
||||
}
|
||||
|
||||
func configure(p any, mode machine.PinMode) {
|
||||
machinePin, ok := p.(machine.Pin)
|
||||
if ok {
|
||||
machinePin.Configure(machine.PinConfig{Mode: mode})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
package regmap
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"io"
|
||||
|
||||
"tinygo.org/x/drivers"
|
||||
)
|
||||
|
||||
// Device8 implements common logic to most 8-bit peripherals with an I2C or SPI bus.
|
||||
// All methods expect the target to support conventional register read and write operations
|
||||
// where the first byte sent is the register address being accessed.
|
||||
//
|
||||
// All methods use an internal buffer and perform no dynamic memory allocation.
|
||||
type Device8 struct {
|
||||
buf [10]byte
|
||||
}
|
||||
|
||||
// clear zeroes Device8's buffers.
|
||||
func (d *Device8) clear() {
|
||||
d.buf = [10]byte{}
|
||||
}
|
||||
|
||||
// I2C methods.
|
||||
|
||||
// Read8I2C reads a single byte from register addr of the device at i2cAddr using the provided I2C bus.
|
||||
func (d *Device8) Read8I2C(bus drivers.I2C, i2cAddr uint16, addr uint8) (byte, error) {
|
||||
d.buf[0] = addr
|
||||
err := bus.Tx(i2cAddr, d.buf[0:1], d.buf[1:2])
|
||||
return d.buf[1], err
|
||||
}
|
||||
|
||||
// Read16I2C reads a 16-bit value from register addr of the device at i2cAddr using the provided I2C bus.
|
||||
// The byte order is specified by order.
|
||||
func (d *Device8) Read16I2C(bus drivers.I2C, i2cAddr uint16, addr uint8, order binary.ByteOrder) (uint16, error) {
|
||||
d.buf[0] = addr
|
||||
err := bus.Tx(i2cAddr, d.buf[0:1], d.buf[1:3])
|
||||
return order.Uint16(d.buf[1:3]), err
|
||||
}
|
||||
|
||||
// Read32I2C reads a 32-bit value from register addr of the device at i2cAddr using the provided I2C bus.
|
||||
// The byte order is specified by order.
|
||||
func (d *Device8) Read32I2C(bus drivers.I2C, i2cAddr uint16, addr uint8, order binary.ByteOrder) (uint32, error) {
|
||||
d.buf[0] = addr
|
||||
err := bus.Tx(i2cAddr, d.buf[0:1], d.buf[1:5])
|
||||
return order.Uint32(d.buf[1:5]), err
|
||||
}
|
||||
|
||||
// ReadDataI2C reads dataLength bytes from register addr of the device at i2cAddr using the provided I2C bus.
|
||||
// The data is stored in dataDestination.
|
||||
func (d *Device8) ReadDataI2C(bus drivers.I2C, i2cAddr uint16, addr uint8, dataDestination []byte) error {
|
||||
d.buf[0] = addr
|
||||
return bus.Tx(i2cAddr, d.buf[:1], dataDestination)
|
||||
}
|
||||
|
||||
// Write8I2C writes a single byte value to register addr of the device at i2cAddr using the provided I2C bus.
|
||||
func (d *Device8) Write8I2C(bus drivers.I2C, i2cAddr uint16, addr, value uint8) error {
|
||||
d.buf[0] = addr
|
||||
d.buf[1] = value
|
||||
return bus.Tx(i2cAddr, d.buf[:2], nil)
|
||||
}
|
||||
|
||||
// Write16I2C writes a 16-bit value to register addr of the device at i2cAddr using the provided I2C bus.
|
||||
// The byte order is specified by order.
|
||||
func (d *Device8) Write16I2C(bus drivers.I2C, i2cAddr uint16, addr uint8, value uint16, order binary.ByteOrder) error {
|
||||
d.buf[0] = addr
|
||||
order.PutUint16(d.buf[1:3], value)
|
||||
return bus.Tx(i2cAddr, d.buf[0:3], nil)
|
||||
}
|
||||
|
||||
// Write32I2C writes a 32-bit value to register addr of the device at i2cAddr using the provided I2C bus.
|
||||
// The byte order is specified by order.
|
||||
func (d *Device8) Write32I2C(bus drivers.I2C, i2cAddr uint16, addr uint8, value uint32, order binary.ByteOrder) error {
|
||||
d.buf[0] = addr
|
||||
order.PutUint32(d.buf[1:5], value)
|
||||
return bus.Tx(i2cAddr, d.buf[0:5], nil)
|
||||
}
|
||||
|
||||
// SPI methods.
|
||||
|
||||
// Read8SPI reads a single byte from register addr using the provided SPI bus.
|
||||
func (d *Device8) Read8SPI(bus drivers.SPI, addr uint8) (byte, error) {
|
||||
d.clear()
|
||||
d.buf[0] = addr
|
||||
err := bus.Tx(d.buf[0:1], d.buf[1:2]) // We suppose data is returned after first byte in SPI.
|
||||
return d.buf[1], err
|
||||
}
|
||||
|
||||
// Read16SPI reads a 16-bit value from register addr using the provided SPI bus. The byte order is specified by order.
|
||||
func (d *Device8) Read16SPI(bus drivers.SPI, addr uint8, order binary.ByteOrder) (uint16, error) {
|
||||
d.clear()
|
||||
d.buf[0] = addr
|
||||
err := bus.Tx(d.buf[0:3], d.buf[3:6]) // We suppose data is returned after first byte in SPI.
|
||||
return order.Uint16(d.buf[4:6]), err
|
||||
}
|
||||
|
||||
// Read32SPI reads a 32-bit value from register addr using the provided SPI bus. The byte order is specified by order.
|
||||
func (d *Device8) Read32SPI(bus drivers.SPI, addr uint8, order binary.ByteOrder) (uint32, error) {
|
||||
d.clear()
|
||||
d.buf[0] = addr
|
||||
err := bus.Tx(d.buf[0:5], d.buf[5:10]) // We suppose data is returned after first byte in SPI.
|
||||
return order.Uint32(d.buf[6:10]), err
|
||||
}
|
||||
|
||||
// ReadDataSPI reads data from a 8bit device address. It assumes data at register address is sent back
|
||||
// from device after first byte is written as address.
|
||||
// It needs the auxiliary buffer length to be large enough to contain both the write and read portions of buffer,
|
||||
// so 2*(dataLength+1) < len(auxiliaryBuf) must hold.
|
||||
func (d *Device8) ReadDataSPI(bus drivers.SPI, addr uint8, dataLength int, auxiliaryBuf []byte) ([]byte, error) {
|
||||
split := len(auxiliaryBuf) / 2
|
||||
if split < dataLength+1 {
|
||||
return nil, io.ErrShortBuffer
|
||||
}
|
||||
|
||||
wbuf, rbuf := auxiliaryBuf[:split], auxiliaryBuf[split:]
|
||||
wbuf[0] = addr
|
||||
err := bus.Tx(wbuf, rbuf)
|
||||
return rbuf[1:], err
|
||||
}
|
||||
|
||||
// Write8SPI writes a single byte value to register addr using the provided SPI bus.
|
||||
func (d *Device8) Write8SPI(bus drivers.SPI, addr, value uint8) error {
|
||||
d.clear()
|
||||
d.buf[0] = addr
|
||||
d.buf[1] = value
|
||||
return bus.Tx(d.buf[:2], nil)
|
||||
}
|
||||
|
||||
// Write16SPI writes a 16-bit value to register addr using the provided SPI bus. The byte order is specified by order.
|
||||
func (d *Device8) Write16SPI(bus drivers.SPI, addr uint8, value uint16, order binary.ByteOrder) error {
|
||||
d.clear()
|
||||
d.buf[0] = addr
|
||||
order.PutUint16(d.buf[1:3], value)
|
||||
return bus.Tx(d.buf[:3], nil)
|
||||
}
|
||||
|
||||
// Write32SPI writes a 32-bit value to register addr using the provided SPI bus. The byte order is specified by order.
|
||||
func (d *Device8) Write32SPI(bus drivers.SPI, addr uint8, value uint32, order binary.ByteOrder) error {
|
||||
d.clear()
|
||||
d.buf[0] = addr
|
||||
order.PutUint32(d.buf[1:5], value)
|
||||
return bus.Tx(d.buf[:5], nil)
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
package regmap
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
|
||||
"tinygo.org/x/drivers"
|
||||
)
|
||||
|
||||
// Device8SPI implements common logic to most 8-bit peripherals with an SPI bus.
|
||||
// All methods expect the target to support conventional register read and write operations
|
||||
// where the first byte sent is the register address being accessed.
|
||||
//
|
||||
// All methods use an internal buffer and perform no dynamic memory allocation.
|
||||
type Device8SPI struct {
|
||||
bus drivers.SPI
|
||||
order binary.ByteOrder
|
||||
d Device8
|
||||
}
|
||||
|
||||
// SetBus sets the SPI bus and byte order for the Device8SPI.
|
||||
//
|
||||
// As a hint, most SPI devices use big-endian (MSB) byte order.
|
||||
// - Big endian: A value of 0x1234 is transmitted as 0x12 followed by 0x34.
|
||||
// - Little endian: A value of 0x1234 is transmitted as 0x34 followed by 0x12.
|
||||
func (d *Device8SPI) SetBus(bus drivers.SPI, order binary.ByteOrder) {
|
||||
d.bus = bus
|
||||
d.order = order
|
||||
}
|
||||
|
||||
// Read8 reads a single byte from register addr.
|
||||
func (d *Device8SPI) Read8(addr uint8) (byte, error) {
|
||||
return d.d.Read8SPI(d.bus, addr)
|
||||
}
|
||||
|
||||
// Read16 reads a 16-bit value from register addr.
|
||||
func (d *Device8SPI) Read16(addr uint8) (uint16, error) {
|
||||
return d.d.Read16SPI(d.bus, addr, d.order)
|
||||
}
|
||||
|
||||
// Read32 reads a 32-bit value from register addr.
|
||||
func (d *Device8SPI) Read32(addr uint8) (uint32, error) {
|
||||
return d.d.Read32SPI(d.bus, addr, d.order)
|
||||
}
|
||||
|
||||
// ReadData reads dataLength bytes from register addr. Due to the internal functioning of
|
||||
// SPI, an auxiliary buffer must be provided to perform the operation and avoid memory allocation.
|
||||
// The returned slice is a subslice of auxBuffer containing the read data.
|
||||
func (d *Device8SPI) ReadData(addr uint8, datalength int, auxBuffer []byte) ([]byte, error) {
|
||||
return d.d.ReadDataSPI(d.bus, addr, datalength, auxBuffer)
|
||||
}
|
||||
|
||||
// Write8 writes a single byte value to register addr.
|
||||
func (d *Device8SPI) Write8(addr, value uint8) error {
|
||||
return d.d.Write8SPI(d.bus, addr, value)
|
||||
}
|
||||
|
||||
// Write16 writes a 16-bit value to register addr.
|
||||
func (d *Device8SPI) Write16(addr uint8, value uint16) error {
|
||||
return d.d.Write16SPI(d.bus, addr, value, d.order)
|
||||
}
|
||||
|
||||
// Write32 writes a 32-bit value to register addr.
|
||||
func (d *Device8SPI) Write32(addr uint8, value uint32) error {
|
||||
return d.d.Write32SPI(d.bus, addr, value, d.order)
|
||||
}
|
||||
|
||||
// Device8I2C implements common logic to most 8-bit peripherals with an I2C bus.
|
||||
// All methods expect the target to support conventional register read and write operations
|
||||
// where the first byte sent is the register address being accessed.
|
||||
//
|
||||
// All methods use an internal buffer and perform no dynamic memory allocation.
|
||||
type Device8I2C struct {
|
||||
bus drivers.I2C
|
||||
i2cAddr uint16
|
||||
order binary.ByteOrder
|
||||
d Device8
|
||||
}
|
||||
|
||||
// SetBus sets the I2C bus, device address, and byte order for the Device8I2C.
|
||||
//
|
||||
// As a hint, most I2C devices use big-endian (MSB) byte order.
|
||||
// - Big endian: A value of 0x1234 is transmitted as 0x12 followed by 0x34.
|
||||
// - Little endian: A value of 0x1234 is transmitted as 0x34 followed by 0x12.
|
||||
func (d *Device8I2C) SetBus(bus drivers.I2C, i2cAddr uint16, order binary.ByteOrder) {
|
||||
d.bus = bus
|
||||
d.i2cAddr = i2cAddr
|
||||
d.order = order
|
||||
}
|
||||
|
||||
// Read8 reads a single byte from register addr.
|
||||
func (d *Device8I2C) Read8(addr uint8) (byte, error) {
|
||||
return d.d.Read8I2C(d.bus, d.i2cAddr, addr)
|
||||
}
|
||||
|
||||
// Read16 reads a 16-bit value from register addr.
|
||||
func (d *Device8I2C) Read16(addr uint8) (uint16, error) {
|
||||
return d.d.Read16I2C(d.bus, d.i2cAddr, addr, d.order)
|
||||
}
|
||||
|
||||
// Read32 reads a 32-bit value from register addr.
|
||||
func (d *Device8I2C) Read32(addr uint8) (uint32, error) {
|
||||
return d.d.Read32I2C(d.bus, d.i2cAddr, addr, d.order)
|
||||
}
|
||||
|
||||
// ReadData reads dataLength bytes from register addr.
|
||||
func (d *Device8I2C) ReadData(addr uint8, dataDestination []byte) error {
|
||||
return d.d.ReadDataI2C(d.bus, d.i2cAddr, addr, dataDestination)
|
||||
}
|
||||
|
||||
// Write8 writes a single byte value to register addr.
|
||||
func (d *Device8I2C) Write8(addr, value uint8) error {
|
||||
return d.d.Write8I2C(d.bus, d.i2cAddr, addr, value)
|
||||
}
|
||||
|
||||
// Write16 writes a 16-bit value to register addr.
|
||||
func (d *Device8I2C) Write16(addr uint8, value uint16) error {
|
||||
return d.d.Write16I2C(d.bus, d.i2cAddr, addr, value, d.order)
|
||||
}
|
||||
|
||||
// Write32 writes a 32-bit value to register addr.
|
||||
func (d *Device8I2C) Write32(addr uint8, value uint32) error {
|
||||
return d.d.Write32I2C(d.bus, d.i2cAddr, addr, value, d.order)
|
||||
}
|
||||
+115
-36
@@ -11,37 +11,57 @@ import (
|
||||
// Device wraps an I2C connection to a LIS3DH device.
|
||||
type Device struct {
|
||||
bus drivers.I2C
|
||||
Address uint16
|
||||
address uint16
|
||||
r Range
|
||||
accel [6]byte // stored acceleration data (from the Update call)
|
||||
}
|
||||
|
||||
// Driver configuration, used for the Configure call. All fields are optional.
|
||||
type Config struct {
|
||||
Address uint16
|
||||
}
|
||||
|
||||
// New creates a new LIS3DH connection. The I2C bus must already be configured.
|
||||
//
|
||||
// This function only creates the Device object, it does not touch the device.
|
||||
func New(bus drivers.I2C) Device {
|
||||
return Device{bus: bus, Address: Address0}
|
||||
return Device{bus: bus, address: Address0}
|
||||
}
|
||||
|
||||
// Configure sets up the device for communication
|
||||
func (d *Device) Configure() {
|
||||
func (d *Device) Configure(config Config) error {
|
||||
if config.Address != 0 {
|
||||
d.address = config.Address
|
||||
}
|
||||
|
||||
// enable all axes, normal mode
|
||||
legacy.WriteRegister(d.bus, uint8(d.Address), REG_CTRL1, []byte{0x07})
|
||||
err := legacy.WriteRegister(d.bus, uint8(d.address), REG_CTRL1, []byte{0x07})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 400Hz rate
|
||||
d.SetDataRate(DATARATE_400_HZ)
|
||||
err = d.SetDataRate(DATARATE_400_HZ)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// High res & BDU enabled
|
||||
legacy.WriteRegister(d.bus, uint8(d.Address), REG_CTRL4, []byte{0x88})
|
||||
err = legacy.WriteRegister(d.bus, uint8(d.address), REG_CTRL4, []byte{0x88})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// get current range
|
||||
d.r = d.ReadRange()
|
||||
d.r, err = d.ReadRange()
|
||||
return err
|
||||
}
|
||||
|
||||
// Connected returns whether a LIS3DH has been found.
|
||||
// It does a "who am I" request and checks the response.
|
||||
func (d *Device) Connected() bool {
|
||||
data := []byte{0}
|
||||
err := legacy.ReadRegister(d.bus, uint8(d.Address), WHO_AM_I, data)
|
||||
err := legacy.ReadRegister(d.bus, uint8(d.address), WHO_AM_I, data)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
@@ -49,46 +69,51 @@ func (d *Device) Connected() bool {
|
||||
}
|
||||
|
||||
// SetDataRate sets the speed of data collected by the LIS3DH.
|
||||
func (d *Device) SetDataRate(rate DataRate) {
|
||||
func (d *Device) SetDataRate(rate DataRate) error {
|
||||
ctl1 := []byte{0}
|
||||
err := legacy.ReadRegister(d.bus, uint8(d.Address), REG_CTRL1, ctl1)
|
||||
err := legacy.ReadRegister(d.bus, uint8(d.address), REG_CTRL1, ctl1)
|
||||
if err != nil {
|
||||
println(err.Error())
|
||||
return err
|
||||
}
|
||||
// mask off bits
|
||||
ctl1[0] &^= 0xf0
|
||||
ctl1[0] |= (byte(rate) << 4)
|
||||
legacy.WriteRegister(d.bus, uint8(d.Address), REG_CTRL1, ctl1)
|
||||
return legacy.WriteRegister(d.bus, uint8(d.address), REG_CTRL1, ctl1)
|
||||
}
|
||||
|
||||
// SetRange sets the G range for LIS3DH.
|
||||
func (d *Device) SetRange(r Range) {
|
||||
func (d *Device) SetRange(r Range) error {
|
||||
ctl := []byte{0}
|
||||
err := legacy.ReadRegister(d.bus, uint8(d.Address), REG_CTRL4, ctl)
|
||||
err := legacy.ReadRegister(d.bus, uint8(d.address), REG_CTRL4, ctl)
|
||||
if err != nil {
|
||||
println(err.Error())
|
||||
return err
|
||||
}
|
||||
// mask off bits
|
||||
ctl[0] &^= 0x30
|
||||
ctl[0] |= (byte(r) << 4)
|
||||
legacy.WriteRegister(d.bus, uint8(d.Address), REG_CTRL4, ctl)
|
||||
err = legacy.WriteRegister(d.bus, uint8(d.address), REG_CTRL4, ctl)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// store the new range
|
||||
d.r = r
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// ReadRange returns the current G range for LIS3DH.
|
||||
func (d *Device) ReadRange() (r Range) {
|
||||
func (d *Device) ReadRange() (r Range, err error) {
|
||||
ctl := []byte{0}
|
||||
err := legacy.ReadRegister(d.bus, uint8(d.Address), REG_CTRL4, ctl)
|
||||
err = legacy.ReadRegister(d.bus, uint8(d.address), REG_CTRL4, ctl)
|
||||
if err != nil {
|
||||
println(err.Error())
|
||||
return 0, err
|
||||
}
|
||||
// mask off bits
|
||||
r = Range(ctl[0] >> 4)
|
||||
r &= 0x03
|
||||
|
||||
return r
|
||||
return r, nil
|
||||
}
|
||||
|
||||
// ReadAcceleration reads the current acceleration from the device and returns
|
||||
@@ -96,28 +121,17 @@ func (d *Device) ReadRange() (r Range) {
|
||||
// and the sensor is not moving the returned value will be around 1000000 or
|
||||
// -1000000.
|
||||
func (d *Device) ReadAcceleration() (int32, int32, int32, error) {
|
||||
x, y, z := d.ReadRawAcceleration()
|
||||
divider := float32(1)
|
||||
switch d.r {
|
||||
case RANGE_16_G:
|
||||
divider = 1365
|
||||
case RANGE_8_G:
|
||||
divider = 4096
|
||||
case RANGE_4_G:
|
||||
divider = 8190
|
||||
case RANGE_2_G:
|
||||
divider = 16380
|
||||
}
|
||||
|
||||
return int32(float32(x) / divider * 1000000), int32(float32(y) / divider * 1000000), int32(float32(z) / divider * 1000000), nil
|
||||
rawX, rawY, rawZ := d.ReadRawAcceleration()
|
||||
x, y, z := normalizeRange(rawX, rawY, rawZ, d.r)
|
||||
return x, y, z, nil
|
||||
}
|
||||
|
||||
// ReadRawAcceleration returns the raw x, y and z axis from the LIS3DH
|
||||
func (d *Device) ReadRawAcceleration() (x int16, y int16, z int16) {
|
||||
legacy.WriteRegister(d.bus, uint8(d.Address), REG_OUT_X_L|0x80, nil)
|
||||
legacy.WriteRegister(d.bus, uint8(d.address), REG_OUT_X_L|0x80, nil)
|
||||
|
||||
data := []byte{0, 0, 0, 0, 0, 0}
|
||||
d.bus.Tx(d.Address, nil, data)
|
||||
d.bus.Tx(d.address, nil, data)
|
||||
|
||||
x = int16((uint16(data[1]) << 8) | uint16(data[0]))
|
||||
y = int16((uint16(data[3]) << 8) | uint16(data[2]))
|
||||
@@ -125,3 +139,68 @@ func (d *Device) ReadRawAcceleration() (x int16, y int16, z int16) {
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// Update the sensor values of the 'which' parameter. Only acceleration is
|
||||
// supported at the moment.
|
||||
func (d *Device) Update(which drivers.Measurement) error {
|
||||
if which&drivers.Acceleration != 0 {
|
||||
// Read raw acceleration values and store them in the driver.
|
||||
err := legacy.WriteRegister(d.bus, uint8(d.address), REG_OUT_X_L|0x80, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = d.bus.Tx(d.address, nil, d.accel[:])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Acceleration returns the last read acceleration in µg (micro-gravity).
|
||||
// When one of the axes is pointing straight to Earth and the sensor is not
|
||||
// moving the returned value will be around 1000000 or -1000000.
|
||||
func (d *Device) Acceleration() (x, y, z int32) {
|
||||
// Extract the raw 16-bit values.
|
||||
rawX := int16((uint16(d.accel[1]) << 8) | uint16(d.accel[0]))
|
||||
rawY := int16((uint16(d.accel[3]) << 8) | uint16(d.accel[2]))
|
||||
rawZ := int16((uint16(d.accel[5]) << 8) | uint16(d.accel[4]))
|
||||
|
||||
// Normalize these values, to be in µg (micro-gravity).
|
||||
return normalizeRange(rawX, rawY, rawZ, d.r)
|
||||
}
|
||||
|
||||
// Convert raw 16-bit values to normalized 32-bit values while avoiding floats
|
||||
// and divisions.
|
||||
func normalizeRange(rawX, rawY, rawZ int16, r Range) (x, y, z int32) {
|
||||
// We're going to convert the 16-bit raw values to values in the range
|
||||
// -1000_000..1000_000. For now we're going to assume a range of 16G, we'll
|
||||
// adjust that range later.
|
||||
// The formula is derived as follows, and carefully selected to avoid
|
||||
// overflow and integer divisions (the division will be optimized to a
|
||||
// bitshift):
|
||||
// x = x * 1000_000 / 2048
|
||||
// x = x * (1000_000/64) / (2048/64)
|
||||
// x = x * 15625 / 32
|
||||
x = int32(rawX) * 15625 / 32
|
||||
y = int32(rawY) * 15625 / 32
|
||||
z = int32(rawZ) * 15625 / 32
|
||||
|
||||
// Now we need to normalize the three values, since we assumed 16G before.
|
||||
shift := uint32(0)
|
||||
switch r {
|
||||
case RANGE_16_G:
|
||||
shift = 0
|
||||
case RANGE_8_G:
|
||||
shift = 1
|
||||
case RANGE_4_G:
|
||||
shift = 2
|
||||
case RANGE_2_G:
|
||||
shift = 3
|
||||
}
|
||||
x >>= shift
|
||||
y >>= shift
|
||||
z >>= shift
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
+35
-28
@@ -7,7 +7,6 @@ import (
|
||||
"errors"
|
||||
|
||||
"tinygo.org/x/drivers"
|
||||
"tinygo.org/x/drivers/internal/legacy"
|
||||
)
|
||||
|
||||
type AccelRange uint8
|
||||
@@ -28,7 +27,7 @@ type Device struct {
|
||||
accelMultiplier int32
|
||||
gyroMultiplier int32
|
||||
magMultiplier int32
|
||||
buf [6]uint8
|
||||
buf [7]uint8 // up to 6 bytes for read + 1 byte for the register address
|
||||
}
|
||||
|
||||
// Configuration for LSM9DS1 device.
|
||||
@@ -61,10 +60,15 @@ func New(bus drivers.I2C) *Device {
|
||||
// Case of boolean false and error nil means I2C is up,
|
||||
// but "who am I" responses have unexpected values.
|
||||
func (d *Device) Connected() bool {
|
||||
data1, data2 := d.buf[:1], d.buf[1:2]
|
||||
legacy.ReadRegister(d.bus, d.AccelAddress, WHO_AM_I, data1)
|
||||
legacy.ReadRegister(d.bus, d.MagAddress, WHO_AM_I_M, data2)
|
||||
return data1[0] == 0x68 && data2[0] == 0x3D
|
||||
data, err := d.readBytes(d.AccelAddress, WHO_AM_I, 1)
|
||||
if err != nil || data[0] != 0x68 {
|
||||
return false
|
||||
}
|
||||
data, err = d.readBytes(d.MagAddress, WHO_AM_I_M, 1)
|
||||
if err != nil || data[0] != 0x3D {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// ReadAcceleration reads the current acceleration from the device and returns
|
||||
@@ -72,8 +76,7 @@ func (d *Device) Connected() bool {
|
||||
// and the sensor is not moving the returned value will be around 1000000 or
|
||||
// -1000000.
|
||||
func (d *Device) ReadAcceleration() (x, y, z int32, err error) {
|
||||
data := d.buf[:6]
|
||||
err = legacy.ReadRegister(d.bus, uint8(d.AccelAddress), OUT_X_L_XL, data)
|
||||
data, err := d.readBytes(d.AccelAddress, OUT_X_L_XL, 6)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -88,8 +91,7 @@ func (d *Device) ReadAcceleration() (x, y, z int32, err error) {
|
||||
// rotation along one axis and while doing so integrate all values over time,
|
||||
// you would get a value close to 360000000.
|
||||
func (d *Device) ReadRotation() (x, y, z int32, err error) {
|
||||
data := d.buf[:6]
|
||||
err = legacy.ReadRegister(d.bus, uint8(d.AccelAddress), OUT_X_L_G, data)
|
||||
data, err := d.readBytes(d.AccelAddress, OUT_X_L_G, 6)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -102,8 +104,7 @@ func (d *Device) ReadRotation() (x, y, z int32, err error) {
|
||||
// ReadMagneticField reads the current magnetic field from the device and returns
|
||||
// it in nT (nanotesla). 1 G (gauss) = 100_000 nT (nanotesla).
|
||||
func (d *Device) ReadMagneticField() (x, y, z int32, err error) {
|
||||
data := d.buf[:6]
|
||||
err = legacy.ReadRegister(d.bus, uint8(d.MagAddress), OUT_X_L_M, data)
|
||||
data, err := d.readBytes(d.MagAddress, OUT_X_L_M, 6)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -115,8 +116,7 @@ func (d *Device) ReadMagneticField() (x, y, z int32, err error) {
|
||||
|
||||
// ReadTemperature returns the temperature in Celsius milli degrees (°C/1000)
|
||||
func (d *Device) ReadTemperature() (t int32, err error) {
|
||||
data := d.buf[:2]
|
||||
err = legacy.ReadRegister(d.bus, uint8(d.AccelAddress), OUT_TEMP_L, data)
|
||||
data, err := d.readBytes(d.AccelAddress, OUT_TEMP_L, 2)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -167,20 +167,16 @@ func (d *Device) doConfigure(cfg Configuration) (err error) {
|
||||
d.magMultiplier = 58
|
||||
}
|
||||
|
||||
data := d.buf[:1]
|
||||
|
||||
// Configure accelerometer
|
||||
// Sample rate & measurement range
|
||||
data[0] = uint8(cfg.AccelSampleRate)<<5 | uint8(cfg.AccelRange)<<3
|
||||
err = legacy.WriteRegister(d.bus, d.AccelAddress, CTRL_REG6_XL, data)
|
||||
err = d.writeByte(d.AccelAddress, CTRL_REG6_XL, uint8(cfg.AccelSampleRate)<<5|uint8(cfg.AccelRange)<<3)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Configure gyroscope
|
||||
// Sample rate & measurement range
|
||||
data[0] = uint8(cfg.GyroSampleRate)<<5 | uint8(cfg.GyroRange)<<3
|
||||
err = legacy.WriteRegister(d.bus, d.AccelAddress, CTRL_REG1_G, data)
|
||||
err = d.writeByte(d.AccelAddress, CTRL_REG1_G, uint8(cfg.GyroSampleRate)<<5|uint8(cfg.GyroRange)<<3)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -190,33 +186,44 @@ func (d *Device) doConfigure(cfg Configuration) (err error) {
|
||||
// Temperature compensation enabled
|
||||
// High-performance mode XY axis
|
||||
// Sample rate
|
||||
data[0] = 0b10000000 | 0b01000000 | uint8(cfg.MagSampleRate)<<2
|
||||
err = legacy.WriteRegister(d.bus, d.MagAddress, CTRL_REG1_M, data)
|
||||
err = d.writeByte(d.MagAddress, CTRL_REG1_M, 0b10000000|0b01000000|uint8(cfg.MagSampleRate)<<2)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Measurement range
|
||||
data[0] = uint8(cfg.MagRange) << 5
|
||||
err = legacy.WriteRegister(d.bus, d.MagAddress, CTRL_REG2_M, data)
|
||||
err = d.writeByte(d.MagAddress, CTRL_REG2_M, uint8(cfg.MagRange)<<5)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// Continuous-conversion mode
|
||||
// https://electronics.stackexchange.com/questions/237397/continuous-conversion-vs-single-conversion-mode
|
||||
data[0] = 0b00000000
|
||||
err = legacy.WriteRegister(d.bus, d.MagAddress, CTRL_REG3_M, data)
|
||||
err = d.writeByte(d.MagAddress, CTRL_REG3_M, 0b00000000)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// High-performance mode Z axis
|
||||
data[0] = 0b00001000
|
||||
err = legacy.WriteRegister(d.bus, d.MagAddress, CTRL_REG4_M, data)
|
||||
err = d.writeByte(d.MagAddress, CTRL_REG4_M, 0b00001000)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Device) readBytes(addr, reg, size uint8) ([]byte, error) {
|
||||
d.buf[0] = reg
|
||||
err := d.bus.Tx(uint16(addr), d.buf[0:1], d.buf[1:size+1])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return d.buf[1 : size+1], nil
|
||||
}
|
||||
|
||||
func (d *Device) writeByte(addr, reg, value uint8) error {
|
||||
d.buf[0] = reg
|
||||
d.buf[1] = value
|
||||
return d.bus.Tx(uint16(addr), d.buf[0:2], nil)
|
||||
}
|
||||
|
||||
+5
-4
@@ -4,6 +4,7 @@ package max72xx
|
||||
|
||||
import (
|
||||
"tinygo.org/x/drivers"
|
||||
"tinygo.org/x/drivers/internal/legacy"
|
||||
"tinygo.org/x/drivers/internal/pin"
|
||||
)
|
||||
|
||||
@@ -21,7 +22,7 @@ func NewDevice(bus drivers.SPI, cs pin.Output) *Device {
|
||||
bus: bus,
|
||||
cs: cs.Set,
|
||||
configurePins: func() {
|
||||
pin.ConfigureOutput(cs)
|
||||
legacy.ConfigurePinOut(cs)
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -29,7 +30,7 @@ func NewDevice(bus drivers.SPI, cs pin.Output) *Device {
|
||||
// Configure setups the pins.
|
||||
func (driver *Device) Configure() {
|
||||
if driver.configurePins == nil {
|
||||
panic(pin.ErrConfigBeforeInstantiated)
|
||||
panic(legacy.ErrConfigBeforeInstantiated)
|
||||
}
|
||||
driver.configurePins()
|
||||
}
|
||||
@@ -93,8 +94,8 @@ func (driver *Device) writeByte(data byte) {
|
||||
|
||||
// WriteCommand write data to a given register.
|
||||
func (driver *Device) WriteCommand(register, data byte) {
|
||||
driver.cs(false)
|
||||
driver.cs.Low()
|
||||
driver.writeByte(register)
|
||||
driver.writeByte(data)
|
||||
driver.cs(true)
|
||||
driver.cs.High()
|
||||
}
|
||||
|
||||
+3
-2
@@ -11,6 +11,7 @@ import (
|
||||
"time"
|
||||
|
||||
"tinygo.org/x/drivers"
|
||||
"tinygo.org/x/drivers/internal/legacy"
|
||||
"tinygo.org/x/drivers/internal/pin"
|
||||
)
|
||||
|
||||
@@ -47,7 +48,7 @@ func New(b drivers.SPI, csPin pin.Output) *Device {
|
||||
cs: csPin.Set,
|
||||
msg: &CANMsg{},
|
||||
configurePins: func() {
|
||||
pin.ConfigureOutput(csPin)
|
||||
legacy.ConfigurePinOut(csPin)
|
||||
},
|
||||
}
|
||||
|
||||
@@ -57,7 +58,7 @@ func New(b drivers.SPI, csPin pin.Output) *Device {
|
||||
// Configure sets up the device for communication.
|
||||
func (d *Device) Configure() {
|
||||
if d.configurePins == nil {
|
||||
panic(pin.ErrConfigBeforeInstantiated)
|
||||
panic(legacy.ErrConfigBeforeInstantiated)
|
||||
}
|
||||
d.configurePins()
|
||||
}
|
||||
|
||||
+24
-16
@@ -5,8 +5,9 @@ package onewire // import "tinygo.org/x/drivers/onewire"
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"machine"
|
||||
"time"
|
||||
|
||||
"tinygo.org/x/drivers/internal/pin"
|
||||
)
|
||||
|
||||
// OneWire ROM commands
|
||||
@@ -19,7 +20,8 @@ const (
|
||||
|
||||
// Device wraps a connection to an 1-Wire devices.
|
||||
type Device struct {
|
||||
p machine.Pin
|
||||
set pin.OutputFunc
|
||||
get pin.InputFunc
|
||||
}
|
||||
|
||||
// Config wraps a configuration to an 1-Wire devices.
|
||||
@@ -32,24 +34,30 @@ var (
|
||||
errReadAddress = errors.New("Error: OneWire. Read address error: CRC mismatch.")
|
||||
)
|
||||
|
||||
// New creates a new GPIO 1-Wire connection.
|
||||
// The pin must be pulled up to the VCC via a resistor greater than 500 ohms (default 4.7k).
|
||||
func New(p machine.Pin) Device {
|
||||
return Device{
|
||||
p: p,
|
||||
// NewFromFuncs expects pin setter and getter for DQ line. Ideally this driver should receive
|
||||
// a one-wire bus HAL abstraction, but I was asked to show how this could be done using pin HAL so here goes.
|
||||
func NewFromFuncs(getPinLevel pin.InputFunc, setPinLevel pin.OutputFunc) *Device {
|
||||
return &Device{
|
||||
set: setPinLevel,
|
||||
get: getPinLevel,
|
||||
}
|
||||
}
|
||||
|
||||
// Configure initializes the protocol.
|
||||
func (d *Device) Configure(config Config) {}
|
||||
|
||||
// By setting the pin value one should configure as output and also expect a more
|
||||
// consistent behaviour across all tinygo hosts by pulling DQ line low consistently. Win-win.
|
||||
func (d *Device) cfgOut() { d.set.Low() }
|
||||
func (d *Device) cfgIn() { d.get() }
|
||||
|
||||
// Reset pull DQ line low, then up.
|
||||
func (d Device) Reset() error {
|
||||
d.p.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
d.cfgOut()
|
||||
time.Sleep(480 * time.Microsecond)
|
||||
d.p.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
|
||||
d.cfgIn()
|
||||
time.Sleep(70 * time.Microsecond)
|
||||
precence := d.p.Get()
|
||||
precence := d.get()
|
||||
time.Sleep(410 * time.Microsecond)
|
||||
if precence {
|
||||
return errNoPresence
|
||||
@@ -59,14 +67,14 @@ func (d Device) Reset() error {
|
||||
|
||||
// WriteBit transmits a bit to 1-Wire bus.
|
||||
func (d Device) WriteBit(data uint8) {
|
||||
d.p.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
d.cfgOut()
|
||||
if data&1 == 1 { // Send '1'
|
||||
time.Sleep(5 * time.Microsecond)
|
||||
d.p.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
|
||||
d.cfgIn()
|
||||
time.Sleep(60 * time.Microsecond)
|
||||
} else { // Send '0'
|
||||
time.Sleep(60 * time.Microsecond)
|
||||
d.p.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
|
||||
d.cfgIn()
|
||||
time.Sleep(5 * time.Microsecond)
|
||||
}
|
||||
}
|
||||
@@ -81,11 +89,11 @@ func (d Device) Write(data uint8) {
|
||||
|
||||
// ReadBit receives a bit from 1-Wire bus.
|
||||
func (d Device) ReadBit() (data uint8) {
|
||||
d.p.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
d.cfgOut()
|
||||
time.Sleep(3 * time.Microsecond)
|
||||
d.p.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
|
||||
d.cfgIn()
|
||||
time.Sleep(8 * time.Microsecond)
|
||||
if d.p.Get() {
|
||||
if d.get() {
|
||||
data = 1
|
||||
}
|
||||
time.Sleep(60 * time.Microsecond)
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
//go:build tinygo
|
||||
|
||||
package onewire
|
||||
|
||||
import (
|
||||
"machine"
|
||||
|
||||
"tinygo.org/x/drivers/internal/legacy"
|
||||
)
|
||||
|
||||
// New creates a new GPIO 1-Wire connection.
|
||||
// The pin must be pulled up to the VCC via a resistor greater than 500 ohms (default 4.7k).
|
||||
func New(p machine.Pin) Device {
|
||||
isOut := false
|
||||
return Device{
|
||||
set: func(level bool) {
|
||||
if !isOut {
|
||||
legacy.ConfigurePinOut(p)
|
||||
isOut = true
|
||||
}
|
||||
p.Set(level)
|
||||
},
|
||||
get: func() (level bool) {
|
||||
if isOut {
|
||||
legacy.ConfigurePinInputPullup(p)
|
||||
isOut = false
|
||||
}
|
||||
return p.Get()
|
||||
},
|
||||
}
|
||||
}
|
||||
+45
-3
@@ -9,9 +9,30 @@ import (
|
||||
"tinygo.org/x/drivers/pixel"
|
||||
)
|
||||
|
||||
func TestImageRGB888(t *testing.T) {
|
||||
image := pixel.NewImage[pixel.RGB888](5, 3)
|
||||
if width, height := image.Size(); width != 5 || height != 3 {
|
||||
t.Errorf("image.Size(): expected 5, 3 but got %d, %d", width, height)
|
||||
}
|
||||
for _, c := range []color.RGBA{
|
||||
{R: 0xff, A: 0xff},
|
||||
{G: 0xff, A: 0xff},
|
||||
{B: 0xff, A: 0xff},
|
||||
{R: 0x10, A: 0xff},
|
||||
{G: 0x10, A: 0xff},
|
||||
{B: 0x10, A: 0xff},
|
||||
} {
|
||||
image.Set(4, 2, pixel.NewColor[pixel.RGB888](c.R, c.G, c.B))
|
||||
c2 := image.Get(4, 2).RGBA()
|
||||
if c2 != c {
|
||||
t.Errorf("failed to roundtrip color: expected %v but got %v", c, c2)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestImageRGB565BE(t *testing.T) {
|
||||
image := pixel.NewImage[pixel.RGB565BE](5, 3)
|
||||
if width, height := image.Size(); width != 5 && height != 3 {
|
||||
if width, height := image.Size(); width != 5 || height != 3 {
|
||||
t.Errorf("image.Size(): expected 5, 3 but got %d, %d", width, height)
|
||||
}
|
||||
for _, c := range []color.RGBA{
|
||||
@@ -30,9 +51,30 @@ func TestImageRGB565BE(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestImageRGB555(t *testing.T) {
|
||||
image := pixel.NewImage[pixel.RGB555](5, 3)
|
||||
if width, height := image.Size(); width != 5 || height != 3 {
|
||||
t.Errorf("image.Size(): expected 5, 3 but got %d, %d", width, height)
|
||||
}
|
||||
for _, c := range []color.RGBA{
|
||||
{R: 0xff, A: 0xff},
|
||||
{G: 0xff, A: 0xff},
|
||||
{B: 0xff, A: 0xff},
|
||||
{R: 0x10, A: 0xff},
|
||||
{G: 0x10, A: 0xff},
|
||||
{B: 0x10, A: 0xff},
|
||||
} {
|
||||
image.Set(4, 2, pixel.NewColor[pixel.RGB555](c.R, c.G, c.B))
|
||||
c2 := image.Get(4, 2).RGBA()
|
||||
if c2 != c {
|
||||
t.Errorf("failed to roundtrip color: expected %v but got %v", c, c2)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestImageRGB444BE(t *testing.T) {
|
||||
image := pixel.NewImage[pixel.RGB444BE](5, 3)
|
||||
if width, height := image.Size(); width != 5 && height != 3 {
|
||||
if width, height := image.Size(); width != 5 || height != 3 {
|
||||
t.Errorf("image.Size(): expected 5, 3 but got %d, %d", width, height)
|
||||
}
|
||||
for _, c := range []color.RGBA{
|
||||
@@ -67,7 +109,7 @@ func TestImageRGB444BE(t *testing.T) {
|
||||
|
||||
func TestImageMonochrome(t *testing.T) {
|
||||
image := pixel.NewImage[pixel.Monochrome](128, 64)
|
||||
if width, height := image.Size(); width != 128 && height != 64 {
|
||||
if width, height := image.Size(); width != 128 || height != 64 {
|
||||
t.Errorf("image.Size(): expected 128, 64 but got %d, %d", width, height)
|
||||
}
|
||||
for _, expected := range []color.RGBA{
|
||||
|
||||
+3
-3
@@ -161,9 +161,9 @@ func (c RGB555) BitsPerPixel() int {
|
||||
|
||||
func (c RGB555) RGBA() color.RGBA {
|
||||
color := color.RGBA{
|
||||
R: uint8(c>>10) << 3,
|
||||
G: uint8(c>>5) << 3,
|
||||
B: uint8(c) << 3,
|
||||
R: (uint8(c) & 0x1F) << 3,
|
||||
G: (uint8(c>>5) & 0x1F) << 3,
|
||||
B: (uint8(c>>10) & 0x1F) << 3,
|
||||
A: 255,
|
||||
}
|
||||
// Correct color rounding, so that 0xff roundtrips back to 0xff.
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
package shiftregister
|
||||
|
||||
import (
|
||||
"tinygo.org/x/drivers/internal/legacy"
|
||||
"tinygo.org/x/drivers/internal/pin"
|
||||
)
|
||||
|
||||
@@ -37,9 +38,9 @@ func New(Bits NumberBit, Latch, Clock, Out pin.Output) *Device {
|
||||
out: Out.Set,
|
||||
bits: Bits,
|
||||
config: func() {
|
||||
pin.ConfigureOutput(Latch)
|
||||
pin.ConfigureOutput(Clock)
|
||||
pin.ConfigureOutput(Out)
|
||||
legacy.ConfigurePinOut(Latch)
|
||||
legacy.ConfigurePinOut(Clock)
|
||||
legacy.ConfigurePinOut(Out)
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -47,23 +48,23 @@ func New(Bits NumberBit, Latch, Clock, Out pin.Output) *Device {
|
||||
// Configure set hardware configuration
|
||||
func (d *Device) Configure() {
|
||||
if d.config == nil {
|
||||
panic(pin.ErrConfigBeforeInstantiated)
|
||||
panic(legacy.ErrConfigBeforeInstantiated)
|
||||
}
|
||||
d.latch(true)
|
||||
d.latch.High()
|
||||
}
|
||||
|
||||
// WriteMask applies mask's bits to register's outputs pin
|
||||
// mask's MSB set Q1, LSB set Q8 (for 8 bits mask)
|
||||
func (d *Device) WriteMask(mask uint32) {
|
||||
d.mask = mask // Keep the mask for individual addressing
|
||||
d.latch(false)
|
||||
d.latch.Low()
|
||||
for i := 0; i < int(d.bits); i++ {
|
||||
d.clock(false)
|
||||
d.clock.Low()
|
||||
d.out(mask&1 != 0)
|
||||
mask = mask >> 1
|
||||
d.clock(true)
|
||||
d.clock.High()
|
||||
}
|
||||
d.latch(true)
|
||||
d.latch.High()
|
||||
}
|
||||
|
||||
// GetShiftPin return an individually addressable pin
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package si5351
|
||||
|
||||
// The I2C address which this device listens to.
|
||||
const AddressDefault = 0x60 // Assumes ADDR pin is low
|
||||
const AddressAlternative = 0x61 // Assumes ADDR pin is high
|
||||
|
||||
const (
|
||||
OUTPUT_ENABLE_CONTROL = 3
|
||||
|
||||
CLK0_CONTROL = 16
|
||||
CLK1_CONTROL = 17
|
||||
CLK2_CONTROL = 18
|
||||
CLK3_CONTROL = 19
|
||||
CLK4_CONTROL = 20
|
||||
CLK5_CONTROL = 21
|
||||
CLK6_CONTROL = 22
|
||||
CLK7_CONTROL = 23
|
||||
|
||||
MULTISYNTH0_PARAMETERS_1 = 42
|
||||
MULTISYNTH0_PARAMETERS_3 = 44
|
||||
MULTISYNTH1_PARAMETERS_1 = 50
|
||||
MULTISYNTH1_PARAMETERS_3 = 52
|
||||
MULTISYNTH2_PARAMETERS_1 = 58
|
||||
MULTISYNTH2_PARAMETERS_3 = 60
|
||||
|
||||
SPREAD_SPECTRUM_PARAMETERS = 149
|
||||
|
||||
PLL_RESET = 177
|
||||
|
||||
CRYSTAL_INTERNAL_LOAD_CAPACITANCE = 183
|
||||
)
|
||||
|
||||
const (
|
||||
CRYSTAL_LOAD_6PF = (1 << 6)
|
||||
CRYSTAL_LOAD_8PF = (2 << 6)
|
||||
CRYSTAL_LOAD_10PF = (3 << 6)
|
||||
)
|
||||
|
||||
const (
|
||||
CRYSTAL_FREQ_25MHZ = 25000000
|
||||
CRYSTAL_FREQ_27MHZ = 27000000
|
||||
)
|
||||
|
||||
const (
|
||||
PLL_A = iota
|
||||
PLL_B
|
||||
)
|
||||
|
||||
const (
|
||||
R_DIV_1 = iota
|
||||
R_DIV_2
|
||||
R_DIV_4
|
||||
R_DIV_8
|
||||
R_DIV_16
|
||||
R_DIV_32
|
||||
R_DIV_64
|
||||
R_DIV_128
|
||||
)
|
||||
|
||||
const (
|
||||
MULTISYNTH_DIV_4 = 4
|
||||
MULTISYNTH_DIV_6 = 6
|
||||
MULTISYNTH_DIV_8 = 8
|
||||
)
|
||||
@@ -0,0 +1,448 @@
|
||||
package si5351
|
||||
|
||||
import (
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
|
||||
"tinygo.org/x/drivers"
|
||||
"tinygo.org/x/drivers/internal/regmap"
|
||||
)
|
||||
|
||||
// Device wraps an I2C connection to a SI5351 device.
|
||||
type Device struct {
|
||||
bus drivers.I2C
|
||||
Address uint8
|
||||
|
||||
rw regmap.Device8I2C
|
||||
initialised bool
|
||||
crystalFreq uint32
|
||||
crystalLoad uint8
|
||||
pllaConfigured bool
|
||||
pllaFreq uint32
|
||||
pllbConfigured bool
|
||||
pllbFreq uint32
|
||||
lastRdivValue [3]uint8
|
||||
}
|
||||
|
||||
var ErrNotInitialised = errors.New("Si5351 not initialised")
|
||||
var ErrInvalidParameter = errors.New("Si5351 invalid parameter")
|
||||
|
||||
// New creates a new SI5351 connection. The I2C bus must already be configured.
|
||||
//
|
||||
// This function only creates the Device object, it does not touch the device.
|
||||
func New(bus drivers.I2C) Device {
|
||||
rw := regmap.Device8I2C{}
|
||||
rw.SetBus(bus, AddressDefault, binary.BigEndian)
|
||||
|
||||
return Device{
|
||||
bus: bus,
|
||||
rw: rw,
|
||||
Address: AddressDefault,
|
||||
crystalFreq: CRYSTAL_FREQ_25MHZ,
|
||||
crystalLoad: CRYSTAL_LOAD_10PF,
|
||||
}
|
||||
}
|
||||
|
||||
// Configure sets up the device for communication
|
||||
// TODO error handling
|
||||
func (d *Device) Configure() error {
|
||||
// // Disable all outputs setting CLKx_DIS high
|
||||
d.rw.Write8(OUTPUT_ENABLE_CONTROL, 0xFF)
|
||||
|
||||
// Set the load capacitance for the XTAL
|
||||
d.rw.Write8(CRYSTAL_INTERNAL_LOAD_CAPACITANCE, d.crystalLoad)
|
||||
|
||||
// Power down all output drivers
|
||||
buf := []byte{CLK0_CONTROL, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80}
|
||||
d.bus.Tx(uint16(d.Address), buf, nil)
|
||||
|
||||
// Disable spread spectrum output.
|
||||
if err := d.DisableSpreadSpectrum(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
d.initialised = true
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Connected returns whether a device at SI5351 address has been found.
|
||||
func (d *Device) Connected() (bool, error) {
|
||||
if err := d.bus.Tx(uint16(d.Address), []byte{}, []byte{0}); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// EnableSpreadSpectrum enables spread spectrum modulation to reduce EMI.
|
||||
func (d *Device) EnableSpreadSpectrum() error {
|
||||
data, err := d.rw.Read8(SPREAD_SPECTRUM_PARAMETERS)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
data |= 0x80
|
||||
return d.rw.Write8(SPREAD_SPECTRUM_PARAMETERS, data)
|
||||
}
|
||||
|
||||
func (d *Device) DisableSpreadSpectrum() error {
|
||||
data, err := d.rw.Read8(SPREAD_SPECTRUM_PARAMETERS)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
data &^= 0x80
|
||||
return d.rw.Write8(SPREAD_SPECTRUM_PARAMETERS, data)
|
||||
}
|
||||
|
||||
func (d *Device) OutputEnable(output uint8, enable bool) error {
|
||||
if !d.initialised {
|
||||
return ErrNotInitialised
|
||||
}
|
||||
|
||||
// Read the current value of the OUTPUT_ENABLE_CONTROL register
|
||||
regVal, err := d.rw.Read8(OUTPUT_ENABLE_CONTROL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Modify regVal based on clk and enable
|
||||
if enable {
|
||||
regVal &= ^(1 << output)
|
||||
} else {
|
||||
regVal |= (1 << output)
|
||||
}
|
||||
|
||||
// Write the modified value back to the OUTPUT_ENABLE_CONTROL register
|
||||
return d.rw.Write8(OUTPUT_ENABLE_CONTROL, regVal)
|
||||
}
|
||||
|
||||
func (d *Device) EnableOutputs() error {
|
||||
if !d.initialised {
|
||||
return ErrNotInitialised
|
||||
}
|
||||
|
||||
return d.rw.Write8(OUTPUT_ENABLE_CONTROL, 0x00)
|
||||
}
|
||||
|
||||
func (d *Device) DisableOutputs() error {
|
||||
if !d.initialised {
|
||||
return ErrNotInitialised
|
||||
}
|
||||
return d.rw.Write8(OUTPUT_ENABLE_CONTROL, 0xFF)
|
||||
}
|
||||
|
||||
// ConfigurePLL sets the multiplier for the specified PLL
|
||||
// pll The PLL to configure, which must be one of the following:
|
||||
// - PLL_A
|
||||
// - PLL_B
|
||||
//
|
||||
// mult The PLL integer multiplier (must be between 15 and 90)
|
||||
//
|
||||
// num The 20-bit numerator for fractional output (0..1,048,575).
|
||||
// Set this to '0' for integer output.
|
||||
//
|
||||
// denom The 20-bit denominator for fractional output (1..1,048,575).
|
||||
// Set this to '1' or higher to avoid divider by zero errors.
|
||||
//
|
||||
// PLL Configuration
|
||||
// fVCO is the PLL output, and must be between 600..900MHz, where:
|
||||
//
|
||||
// fVCO = fXTAL * (a+(b/c))
|
||||
//
|
||||
// fXTAL = the crystal input frequency
|
||||
// a = an integer between 15 and 90
|
||||
// b = the fractional numerator (0..1,048,575)
|
||||
// c = the fractional denominator (1..1,048,575)
|
||||
//
|
||||
// NOTE: Try to use integers whenever possible to avoid clock jitter
|
||||
// (only use the a part, setting b to '0' and c to '1').
|
||||
//
|
||||
// See: http://www.silabs.com/Support%20Documents/TechnicalDocs/AN619.pdf
|
||||
func (d *Device) ConfigurePLL(pll uint8, mult uint8, num uint32, denom uint32) error {
|
||||
// Basic validation
|
||||
if !d.initialised {
|
||||
return ErrNotInitialised
|
||||
}
|
||||
// mult = 15..90
|
||||
if !((mult > 14) && (mult < 91)) {
|
||||
return ErrInvalidParameter
|
||||
}
|
||||
// Avoid divide by zero
|
||||
if !(denom > 0) {
|
||||
return ErrInvalidParameter
|
||||
}
|
||||
// 20-bit limit
|
||||
if !(num <= 0xFFFFF) {
|
||||
return ErrInvalidParameter
|
||||
}
|
||||
// 20-bit limit
|
||||
if !(denom <= 0xFFFFF) {
|
||||
return ErrInvalidParameter
|
||||
}
|
||||
|
||||
// PLL Multiplier Equations
|
||||
//
|
||||
// P1 register is an 18-bit value using following formula:
|
||||
//
|
||||
// P1[17:0] = 128 * mult + floor(128*(num/denom)) - 512
|
||||
//
|
||||
// P2 register is a 20-bit value using the following formula:
|
||||
//
|
||||
// P2[19:0] = 128 * num - denom * floor(128*(num/denom))
|
||||
//
|
||||
// P3 register is a 20-bit value using the following formula:
|
||||
//
|
||||
// P3[19:0] = denom
|
||||
//
|
||||
|
||||
// Set PLL config registers
|
||||
var p1, p2, p3 uint32
|
||||
if num == 0 {
|
||||
// Integer mode
|
||||
p1 = 128*uint32(mult) - 512
|
||||
p2 = num
|
||||
p3 = denom
|
||||
} else {
|
||||
// Fractional mode
|
||||
p1 = uint32(128*float64(mult) + math.Floor(128*(float64(num)/float64(denom))) - 512)
|
||||
p2 = uint32(128*float64(num) - float64(denom)*math.Floor(128*(float64(num)/float64(denom))))
|
||||
p3 = denom
|
||||
}
|
||||
|
||||
// Get the appropriate starting point for the PLL registers
|
||||
baseaddr := uint8(26)
|
||||
if pll == PLL_B {
|
||||
baseaddr = 34
|
||||
}
|
||||
|
||||
// The datasheet is a nightmare of typos and inconsistencies here!
|
||||
data := [8]byte{}
|
||||
data[0] = uint8((p3 & 0x0000FF00) >> 8)
|
||||
data[1] = uint8(p3 & 0x000000FF)
|
||||
data[2] = uint8((p1 & 0x00030000) >> 16)
|
||||
data[3] = uint8((p1 & 0x0000FF00) >> 8)
|
||||
data[4] = uint8(p1 & 0x000000FF)
|
||||
data[5] = uint8(((p3 & 0x000F0000) >> 12) | ((p2 & 0x000F0000) >> 16))
|
||||
data[6] = uint8((p2 & 0x0000FF00) >> 8)
|
||||
data[7] = uint8(p2 & 0x000000FF)
|
||||
if err := d.bus.Tx(uint16(baseaddr), data[:], nil); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Reset both PLLs
|
||||
if err := d.rw.Write8(PLL_RESET, (1<<7)|(1<<5)); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Store the frequency settings for use with the Multisynth helper
|
||||
fvco := float64(d.crystalFreq) * (float64(mult) + (float64(num) / float64(denom)))
|
||||
if pll == PLL_A {
|
||||
d.pllaConfigured = true
|
||||
d.pllaFreq = uint32(math.Floor(fvco))
|
||||
} else {
|
||||
d.pllbConfigured = true
|
||||
d.pllbFreq = uint32(math.Floor(fvco))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ConfigureMultisynth divider, which determines the
|
||||
// output clock frequency based on the specified PLL input.
|
||||
//
|
||||
// output The output channel to use (0..2)
|
||||
//
|
||||
// pll The PLL input source to use, which must be one of:
|
||||
// - PLL_A
|
||||
// - PLL_B
|
||||
//
|
||||
// div The integer divider for the Multisynth output.
|
||||
//
|
||||
// If pure integer values are used, this value must be one of:
|
||||
// - MULTISYNTH_DIV_4
|
||||
// - MULTISYNTH_DIV_6
|
||||
// - MULTISYNTH_DIV_8
|
||||
// If fractional output is used, this value must be between 8 and 900.
|
||||
//
|
||||
// num The 20-bit numerator for fractional output (0..1,048,575).
|
||||
//
|
||||
// Set this to '0' for integer output.
|
||||
//
|
||||
// denom The 20-bit denominator for fractional output (1..1,048,575).
|
||||
//
|
||||
// Set this to '1' or higher to avoid divide by zero errors.
|
||||
//
|
||||
// # Output Clock Configuration
|
||||
//
|
||||
// The multisynth dividers are applied to the specified PLL output,
|
||||
// and are used to reduce the PLL output to a valid range (500kHz
|
||||
// to 160MHz). The relationship can be seen in this formula, where
|
||||
// fVCO is the PLL output frequency and MSx is the multisynth divider:
|
||||
//
|
||||
// fOUT = fVCO / MSx
|
||||
//
|
||||
// Valid multisynth dividers are 4, 6, or 8 when using integers,
|
||||
// or any fractional values between 8 + 1/1,048,575 and 900 + 0/1
|
||||
// The following formula is used for the fractional mode divider:
|
||||
//
|
||||
// a + b / c
|
||||
//
|
||||
// a = The integer value, which must be 4, 6 or 8 in integer mode (MSx_INT=1) or 8..900 in fractional mode (MSx_INT=0).
|
||||
// b = The fractional numerator (0..1,048,575)
|
||||
// c = The fractional denominator (1..1,048,575)
|
||||
//
|
||||
// NOTE: Try to use integers whenever possible to avoid clock jitter
|
||||
// NOTE: For output frequencies > 150MHz, you must set the divider
|
||||
//
|
||||
// to 4 and adjust to PLL to generate the frequency (for example
|
||||
// a PLL of 640 to generate a 160MHz output clock). This is not
|
||||
// yet supported in the driver, which limits frequencies to 500kHz .. 150MHz.
|
||||
//
|
||||
// NOTE: For frequencies below 500kHz (down to 8kHz) Rx_DIV must be
|
||||
//
|
||||
// used, but this isn't currently implemented in the driver.
|
||||
func (d *Device) ConfigureMultisynth(output uint8, pll uint8, div uint32, num uint32, denom uint32) error {
|
||||
// Basic validation
|
||||
if !d.initialised {
|
||||
return ErrNotInitialised
|
||||
}
|
||||
// Channel range
|
||||
if !(output < 3) {
|
||||
return fmt.Errorf("output channel must be between 0 and 2")
|
||||
}
|
||||
// Divider integer value
|
||||
if !((div > 3) && (div < 2049)) {
|
||||
return ErrInvalidParameter
|
||||
}
|
||||
// Avoid divide by zero
|
||||
if !(denom > 0) {
|
||||
return ErrInvalidParameter
|
||||
}
|
||||
// 20-bit limit
|
||||
if !(num <= 0xFFFFF) {
|
||||
return ErrInvalidParameter
|
||||
}
|
||||
// 20-bit limit
|
||||
if !(denom <= 0xFFFFF) {
|
||||
return ErrInvalidParameter
|
||||
}
|
||||
|
||||
// Make sure the requested PLL has been initialised
|
||||
if pll == PLL_A && !d.pllaConfigured {
|
||||
return ErrInvalidParameter
|
||||
}
|
||||
if pll == PLL_B && !d.pllbConfigured {
|
||||
return ErrInvalidParameter
|
||||
}
|
||||
|
||||
// Output Multisynth Divider Equations
|
||||
//
|
||||
// where: a = div, b = num and c = denom
|
||||
//
|
||||
// P1 register is an 18-bit value using following formula:
|
||||
//
|
||||
// P1[17:0] = 128 * a + floor(128*(b/c)) - 512
|
||||
//
|
||||
// P2 register is a 20-bit value using the following formula:
|
||||
//
|
||||
// P2[19:0] = 128 * b - c * floor(128*(b/c))
|
||||
//
|
||||
// P3 register is a 20-bit value using the following formula:
|
||||
//
|
||||
// P3[19:0] = c
|
||||
//
|
||||
|
||||
// Set PLL config registers
|
||||
var p1, p2, p3 uint32
|
||||
if num == 0 {
|
||||
// Integer mode
|
||||
p1 = 128*div - 512
|
||||
p2 = 0
|
||||
p3 = denom
|
||||
} else if denom == 1 {
|
||||
// Fractional mode, simplified calculations
|
||||
p1 = 128*div + 128*num - 512
|
||||
p2 = 128*num - 128
|
||||
p3 = 1
|
||||
} else {
|
||||
// Fractional mode
|
||||
p1 = uint32(128*float64(div) + math.Floor(128*(float64(num)/float64(denom))) - 512)
|
||||
p2 = uint32(128*float64(num) - float64(denom)*math.Floor(128*(float64(num)/float64(denom))))
|
||||
p3 = denom
|
||||
}
|
||||
|
||||
// Get the appropriate starting point for the PLL registers
|
||||
baseaddr := uint8(0)
|
||||
switch output {
|
||||
case 0:
|
||||
baseaddr = MULTISYNTH0_PARAMETERS_1
|
||||
case 1:
|
||||
baseaddr = MULTISYNTH1_PARAMETERS_1
|
||||
case 2:
|
||||
baseaddr = MULTISYNTH2_PARAMETERS_1
|
||||
}
|
||||
|
||||
// Set the MSx config registers
|
||||
data := [8]byte{}
|
||||
data[0] = uint8((p3 & 0xFF00) >> 8)
|
||||
data[1] = uint8(p3 & 0xFF)
|
||||
data[2] = uint8(((p1 & 0x30000) >> 16)) | d.lastRdivValue[output]
|
||||
data[3] = uint8((p1 & 0xFF00) >> 8)
|
||||
data[4] = uint8(p1 & 0xFF)
|
||||
data[5] = uint8(((p3 & 0xF0000) >> 12) | ((p2 & 0xF0000) >> 16))
|
||||
data[6] = uint8((p2 & 0xFF00) >> 8)
|
||||
data[7] = uint8(p2 & 0xFF)
|
||||
if err := d.bus.Tx(uint16(baseaddr), data[:], nil); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Configure the clk control and enable the output
|
||||
// TODO: Check if the clk control byte needs to be updated.
|
||||
clkControlReg := uint8(0x0F) // 8mA drive strength, MS0 as CLK0 source, Clock not inverted, powered up
|
||||
if pll == PLL_B {
|
||||
clkControlReg |= (1 << 5) // Uses PLLB
|
||||
}
|
||||
if num == 0 {
|
||||
clkControlReg |= (1 << 6) // Integer mode
|
||||
}
|
||||
|
||||
var register uint8
|
||||
switch output {
|
||||
case 0:
|
||||
register = CLK0_CONTROL
|
||||
case 1:
|
||||
register = CLK1_CONTROL
|
||||
case 2:
|
||||
register = CLK2_CONTROL
|
||||
}
|
||||
|
||||
return d.rw.Write8(register, clkControlReg)
|
||||
}
|
||||
|
||||
func (d *Device) ConfigureRdiv(output uint8, div uint8) error {
|
||||
// Channel range
|
||||
if !(output < 3) {
|
||||
return ErrInvalidParameter
|
||||
}
|
||||
|
||||
var register uint8
|
||||
switch output {
|
||||
case 0:
|
||||
register = MULTISYNTH0_PARAMETERS_3
|
||||
case 1:
|
||||
register = MULTISYNTH1_PARAMETERS_3
|
||||
case 2:
|
||||
register = MULTISYNTH2_PARAMETERS_3
|
||||
}
|
||||
|
||||
data, err := d.rw.Read8(register)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
d.lastRdivValue[output] = (div & 0x07) << 4
|
||||
data = (data & 0x0F) | d.lastRdivValue[output]
|
||||
return d.rw.Write8(register, data)
|
||||
}
|
||||
@@ -144,6 +144,7 @@ tinygo build -size short -o ./build/test.hex -target=pico ./examples/tmc5160/mai
|
||||
tinygo build -size short -o ./build/test.uf2 -target=nicenano ./examples/sharpmem/main.go
|
||||
tinygo build -size short -o ./build/test.hex -target=feather-nrf52840 ./examples/max6675/main.go
|
||||
tinygo build -size short -o ./build/test.hex -target=pico ./examples/ens160/main.go
|
||||
tinygo build -size short -o ./build/test.hex -target=pico ./examples/si5351/main.go
|
||||
# network examples (espat)
|
||||
tinygo build -size short -o ./build/test.hex -target=challenger-rp2040 ./examples/net/ntpclient/
|
||||
# network examples (wifinina)
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
//go:build baremetal
|
||||
//go:build tinygo
|
||||
|
||||
package ssd1289
|
||||
|
||||
|
||||
+10
-8
@@ -5,8 +5,10 @@ package ssd1289
|
||||
|
||||
import (
|
||||
"image/color"
|
||||
"machine"
|
||||
"time"
|
||||
|
||||
"tinygo.org/x/drivers/internal/legacy"
|
||||
"tinygo.org/x/drivers/internal/pin"
|
||||
)
|
||||
|
||||
@@ -25,7 +27,7 @@ type Device struct {
|
||||
const width = int16(240)
|
||||
const height = int16(320)
|
||||
|
||||
func New(rs pin.Output, wr pin.Output, cs pin.Output, rst pin.Output, bus Bus) Device {
|
||||
func New(rs machine.Pin, wr machine.Pin, cs machine.Pin, rst machine.Pin, bus Bus) Device {
|
||||
d := Device{
|
||||
rs: rs.Set,
|
||||
wr: wr.Set,
|
||||
@@ -33,14 +35,14 @@ func New(rs pin.Output, wr pin.Output, cs pin.Output, rst pin.Output, bus Bus) D
|
||||
rst: rst.Set,
|
||||
bus: bus,
|
||||
}
|
||||
pin.ConfigureOutput(rs)
|
||||
pin.ConfigureOutput(wr)
|
||||
pin.ConfigureOutput(cs)
|
||||
pin.ConfigureOutput(rst)
|
||||
legacy.ConfigurePinOut(rs)
|
||||
legacy.ConfigurePinOut(wr)
|
||||
legacy.ConfigurePinOut(cs)
|
||||
legacy.ConfigurePinOut(rst)
|
||||
|
||||
d.cs.High()
|
||||
d.rst.High()
|
||||
d.wr.High()
|
||||
cs.Set(true)
|
||||
rst.Set(true)
|
||||
wr.Set(true)
|
||||
|
||||
return d
|
||||
}
|
||||
|
||||
+5
-5
@@ -5,12 +5,12 @@ package ssd1331 // import "tinygo.org/x/drivers/ssd1331"
|
||||
|
||||
import (
|
||||
"image/color"
|
||||
"machine"
|
||||
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"tinygo.org/x/drivers"
|
||||
"tinygo.org/x/drivers/internal/legacy"
|
||||
"tinygo.org/x/drivers/internal/pin"
|
||||
)
|
||||
|
||||
@@ -37,10 +37,10 @@ type Config struct {
|
||||
}
|
||||
|
||||
// New creates a new SSD1331 connection. The SPI wire must already be configured.
|
||||
func New(bus drivers.SPI, resetPin, dcPin, csPin machine.Pin) Device {
|
||||
pin.ConfigureOutput(dcPin)
|
||||
pin.ConfigureOutput(resetPin)
|
||||
pin.ConfigureOutput(csPin)
|
||||
func New(bus drivers.SPI, resetPin, dcPin, csPin pin.Output) Device {
|
||||
legacy.ConfigurePinOut(dcPin)
|
||||
legacy.ConfigurePinOut(resetPin)
|
||||
legacy.ConfigurePinOut(csPin)
|
||||
return Device{
|
||||
bus: bus,
|
||||
dcPin: dcPin.Set,
|
||||
|
||||
+7
-6
@@ -9,6 +9,7 @@ import (
|
||||
"time"
|
||||
|
||||
"tinygo.org/x/drivers"
|
||||
"tinygo.org/x/drivers/internal/legacy"
|
||||
"tinygo.org/x/drivers/internal/pin"
|
||||
)
|
||||
|
||||
@@ -51,11 +52,11 @@ func New(bus drivers.SPI, resetPin, dcPin, csPin, enPin, rwPin pin.Output) Devic
|
||||
enPin: enPin.Set,
|
||||
rwPin: rwPin.Set,
|
||||
configurePins: func() {
|
||||
pin.ConfigureOutput(dcPin)
|
||||
pin.ConfigureOutput(resetPin)
|
||||
pin.ConfigureOutput(csPin)
|
||||
pin.ConfigureOutput(enPin)
|
||||
pin.ConfigureOutput(rwPin)
|
||||
legacy.ConfigurePinOut(dcPin)
|
||||
legacy.ConfigurePinOut(resetPin)
|
||||
legacy.ConfigurePinOut(csPin)
|
||||
legacy.ConfigurePinOut(enPin)
|
||||
legacy.ConfigurePinOut(rwPin)
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -63,7 +64,7 @@ func New(bus drivers.SPI, resetPin, dcPin, csPin, enPin, rwPin pin.Output) Devic
|
||||
// Configure initializes the display with default configuration
|
||||
func (d *Device) Configure(cfg Config) {
|
||||
if d.configurePins == nil {
|
||||
panic(pin.ErrConfigBeforeInstantiated)
|
||||
panic(legacy.ErrConfigBeforeInstantiated)
|
||||
}
|
||||
if cfg.Width == 0 {
|
||||
cfg.Width = 128
|
||||
|
||||
+7
-6
@@ -10,6 +10,7 @@ import (
|
||||
"errors"
|
||||
|
||||
"tinygo.org/x/drivers"
|
||||
"tinygo.org/x/drivers/internal/legacy"
|
||||
"tinygo.org/x/drivers/internal/pin"
|
||||
"tinygo.org/x/drivers/pixel"
|
||||
)
|
||||
@@ -72,10 +73,10 @@ func New(bus drivers.SPI, resetPin, dcPin, csPin, blPin pin.Output) Device {
|
||||
// NewOf creates a new ST7735 connection with a particular pixel format. The SPI
|
||||
// wire must already be configured.
|
||||
func NewOf[T Color](bus drivers.SPI, resetPin, dcPin, csPin, blPin pin.Output) DeviceOf[T] {
|
||||
pin.ConfigureOutput(dcPin)
|
||||
pin.ConfigureOutput(resetPin)
|
||||
pin.ConfigureOutput(csPin)
|
||||
pin.ConfigureOutput(blPin)
|
||||
legacy.ConfigurePinOut(dcPin)
|
||||
legacy.ConfigurePinOut(resetPin)
|
||||
legacy.ConfigurePinOut(csPin)
|
||||
legacy.ConfigurePinOut(blPin)
|
||||
return DeviceOf[T]{
|
||||
bus: bus,
|
||||
dcPin: dcPin.Set,
|
||||
@@ -438,9 +439,9 @@ func (d *DeviceOf[T]) Size() (w, h int16) {
|
||||
// EnableBacklight enables or disables the backlight
|
||||
func (d *DeviceOf[T]) EnableBacklight(enable bool) {
|
||||
if enable {
|
||||
d.blPin(true)
|
||||
d.blPin.High()
|
||||
} else {
|
||||
d.blPin(false)
|
||||
d.blPin.Low()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+6
-5
@@ -13,6 +13,7 @@ import (
|
||||
"errors"
|
||||
|
||||
"tinygo.org/x/drivers"
|
||||
"tinygo.org/x/drivers/internal/legacy"
|
||||
"tinygo.org/x/drivers/internal/pin"
|
||||
"tinygo.org/x/drivers/pixel"
|
||||
)
|
||||
@@ -90,12 +91,12 @@ func New(bus drivers.SPI, resetPin, dcPin, csPin, blPin pin.Output) Device {
|
||||
// NewOf creates a new ST7789 connection with a particular pixel format. The SPI
|
||||
// wire must already be configured.
|
||||
func NewOf[T Color](bus drivers.SPI, resetPin, dcPin, csPin, blPin pin.Output) DeviceOf[T] {
|
||||
pin.ConfigureOutput(dcPin)
|
||||
pin.ConfigureOutput(resetPin)
|
||||
pin.ConfigureOutput(csPin)
|
||||
pin.ConfigureOutput(blPin)
|
||||
legacy.ConfigurePinOut(dcPin)
|
||||
legacy.ConfigurePinOut(resetPin)
|
||||
legacy.ConfigurePinOut(csPin)
|
||||
legacy.ConfigurePinOut(blPin)
|
||||
var cs pin.OutputFunc
|
||||
if !pin.IsNotPin(csPin) {
|
||||
if !legacy.PinIsNoPin(csPin) {
|
||||
cs = csPin.Set
|
||||
}
|
||||
return DeviceOf[T]{
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
//go:build tinygo
|
||||
|
||||
package sx127x
|
||||
|
||||
import (
|
||||
|
||||
+2
-2
@@ -67,9 +67,9 @@ func (d *Device) SetRadioController(rc RadioController) error {
|
||||
|
||||
// Reset re-initialize the sx127x device
|
||||
func (d *Device) Reset() {
|
||||
d.rstPin(false)
|
||||
d.rstPin.Low()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
d.rstPin(true)
|
||||
d.rstPin.High()
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}
|
||||
|
||||
|
||||
+6
-5
@@ -11,6 +11,7 @@ import (
|
||||
"time"
|
||||
|
||||
"tinygo.org/x/drivers"
|
||||
"tinygo.org/x/drivers/internal/legacy"
|
||||
"tinygo.org/x/drivers/internal/pin"
|
||||
"tinygo.org/x/drivers/pixel"
|
||||
)
|
||||
@@ -34,7 +35,7 @@ type Device struct {
|
||||
cs pin.OutputFunc
|
||||
dc pin.OutputFunc
|
||||
rst pin.OutputFunc
|
||||
isBusy drivers.PinInput
|
||||
isBusy pin.InputFunc
|
||||
width int16
|
||||
height int16
|
||||
buffer []uint8
|
||||
@@ -50,10 +51,10 @@ type Speed uint8
|
||||
|
||||
// New returns a new uc8151 driver. Pass in a fully configured SPI bus.
|
||||
func New(bus drivers.SPI, csPin, dcPin, rstPin pin.Output, busyPin pin.Input) Device {
|
||||
pin.ConfigureOutput(csPin)
|
||||
pin.ConfigureOutput(dcPin)
|
||||
pin.ConfigureOutput(rstPin)
|
||||
pin.ConfigureInput(busyPin)
|
||||
legacy.ConfigurePinOut(csPin)
|
||||
legacy.ConfigurePinOut(dcPin)
|
||||
legacy.ConfigurePinOut(rstPin)
|
||||
legacy.ConfigurePinInput(busyPin)
|
||||
return Device{
|
||||
bus: bus,
|
||||
cs: csPin.Set,
|
||||
|
||||
+1
-1
@@ -2,4 +2,4 @@ package drivers
|
||||
|
||||
// Version returns a user-readable string showing the version of the drivers package for support purposes.
|
||||
// Update this value before release of new version of software.
|
||||
const Version = "0.32.0"
|
||||
const Version = "0.33.0"
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
"machine"
|
||||
"time"
|
||||
|
||||
"tinygo.org/x/drivers"
|
||||
"tinygo.org/x/drivers/internal/legacy"
|
||||
"tinygo.org/x/drivers/internal/pin"
|
||||
)
|
||||
|
||||
@@ -29,7 +29,7 @@ type Device struct {
|
||||
cs pin.OutputFunc
|
||||
dc pin.OutputFunc
|
||||
rst pin.OutputFunc
|
||||
isBusy drivers.PinInput
|
||||
isBusy pin.InputFunc
|
||||
configurePins func()
|
||||
buffer []uint8
|
||||
rotation Rotation
|
||||
@@ -91,17 +91,17 @@ func New(bus *machine.SPI, csPin, dcPin, rstPin pin.Output, busyPin pin.Input) D
|
||||
rst: rstPin.Set,
|
||||
isBusy: busyPin.Get,
|
||||
configurePins: func() {
|
||||
pin.ConfigureOutput(csPin)
|
||||
pin.ConfigureOutput(dcPin)
|
||||
pin.ConfigureOutput(rstPin)
|
||||
pin.ConfigureInput(busyPin)
|
||||
legacy.ConfigurePinOut(csPin)
|
||||
legacy.ConfigurePinOut(dcPin)
|
||||
legacy.ConfigurePinOut(rstPin)
|
||||
legacy.ConfigurePinInput(busyPin)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (d *Device) LDirInit(cfg Config) {
|
||||
if d.configurePins == nil {
|
||||
panic(pin.ErrConfigBeforeInstantiated)
|
||||
panic(legacy.ErrConfigBeforeInstantiated)
|
||||
}
|
||||
d.configurePins()
|
||||
|
||||
@@ -160,7 +160,7 @@ func (d *Device) LDirInit(cfg Config) {
|
||||
|
||||
func (d *Device) HDirInit(cfg Config) {
|
||||
if d.configurePins == nil {
|
||||
panic(pin.ErrConfigBeforeInstantiated)
|
||||
panic(legacy.ErrConfigBeforeInstantiated)
|
||||
}
|
||||
d.configurePins()
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"time"
|
||||
|
||||
"tinygo.org/x/drivers"
|
||||
"tinygo.org/x/drivers/internal/legacy"
|
||||
"tinygo.org/x/drivers/internal/pin"
|
||||
)
|
||||
|
||||
@@ -24,7 +25,7 @@ type Device struct {
|
||||
cs pin.OutputFunc
|
||||
dc pin.OutputFunc
|
||||
rst pin.OutputFunc
|
||||
isBusy drivers.PinInput
|
||||
isBusy pin.InputFunc
|
||||
logicalWidth int16
|
||||
width int16
|
||||
height int16
|
||||
@@ -54,10 +55,10 @@ var lutPartialUpdate = [30]uint8{
|
||||
|
||||
// New returns a new epd2in13x driver. Pass in a fully configured SPI bus.
|
||||
func New(bus drivers.SPI, csPin, dcPin, rstPin pin.Output, busyPin pin.Input) Device {
|
||||
pin.ConfigureOutput(csPin)
|
||||
pin.ConfigureOutput(dcPin)
|
||||
pin.ConfigureOutput(rstPin)
|
||||
pin.ConfigureInput(busyPin)
|
||||
legacy.ConfigurePinOut(csPin)
|
||||
legacy.ConfigurePinOut(dcPin)
|
||||
legacy.ConfigurePinOut(rstPin)
|
||||
legacy.ConfigurePinInput(busyPin)
|
||||
return Device{
|
||||
bus: bus,
|
||||
cs: csPin.Set,
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"time"
|
||||
|
||||
"tinygo.org/x/drivers"
|
||||
"tinygo.org/x/drivers/internal/legacy"
|
||||
"tinygo.org/x/drivers/internal/pin"
|
||||
)
|
||||
|
||||
@@ -23,7 +24,7 @@ type Device struct {
|
||||
cs pin.OutputFunc
|
||||
dc pin.OutputFunc
|
||||
rst pin.OutputFunc
|
||||
isBusy drivers.PinInput
|
||||
isBusy pin.InputFunc
|
||||
width int16
|
||||
height int16
|
||||
buffer [][]uint8
|
||||
@@ -34,10 +35,10 @@ type Color uint8
|
||||
|
||||
// New returns a new epd2in13x driver. Pass in a fully configured SPI bus.
|
||||
func New(bus drivers.SPI, csPin, dcPin, rstPin pin.Output, busyPin pin.Input) Device {
|
||||
pin.ConfigureOutput(csPin)
|
||||
pin.ConfigureOutput(dcPin)
|
||||
pin.ConfigureOutput(rstPin)
|
||||
pin.ConfigureInput(busyPin)
|
||||
legacy.ConfigurePinOut(csPin)
|
||||
legacy.ConfigurePinOut(dcPin)
|
||||
legacy.ConfigurePinOut(rstPin)
|
||||
legacy.ConfigurePinInput(busyPin)
|
||||
return Device{
|
||||
bus: bus,
|
||||
cs: csPin.Set,
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"time"
|
||||
|
||||
"tinygo.org/x/drivers"
|
||||
"tinygo.org/x/drivers/internal/legacy"
|
||||
"tinygo.org/x/drivers/internal/pin"
|
||||
)
|
||||
|
||||
@@ -19,12 +20,11 @@ const (
|
||||
const Baudrate = 4_000_000 // 4 MHz
|
||||
|
||||
type Device struct {
|
||||
bus drivers.SPI
|
||||
cs pin.OutputFunc
|
||||
dc pin.OutputFunc
|
||||
rst pin.OutputFunc
|
||||
isBusy drivers.PinInput
|
||||
|
||||
bus drivers.SPI
|
||||
cs pin.OutputFunc
|
||||
dc pin.OutputFunc
|
||||
rst pin.OutputFunc
|
||||
isBusy pin.InputFunc
|
||||
blackBuffer []byte
|
||||
redBuffer []byte
|
||||
}
|
||||
@@ -43,6 +43,26 @@ func New(bus drivers.SPI) Device {
|
||||
}
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
ResetPin pin.Output
|
||||
DataPin pin.Output
|
||||
ChipSelectPin pin.Output
|
||||
BusyPin pin.Input
|
||||
}
|
||||
|
||||
// Configure configures the device and its pins.
|
||||
func (d *Device) Configure(c Config) error {
|
||||
d.cs = c.ChipSelectPin.Set
|
||||
d.dc = c.DataPin.Set
|
||||
d.rst = c.ResetPin.Set
|
||||
d.isBusy = c.BusyPin.Get
|
||||
legacy.ConfigurePinOut(c.ChipSelectPin)
|
||||
legacy.ConfigurePinOut(c.DataPin)
|
||||
legacy.ConfigurePinOut(c.ResetPin)
|
||||
legacy.ConfigurePinInput(c.BusyPin)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *Device) Size() (x, y int16) {
|
||||
return displayWidth, displayHeight
|
||||
}
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
//go:build baremetal
|
||||
|
||||
package epd2in66b
|
||||
|
||||
import "machine"
|
||||
|
||||
type Config struct {
|
||||
ResetPin machine.Pin
|
||||
DataPin machine.Pin
|
||||
ChipSelectPin machine.Pin
|
||||
BusyPin machine.Pin
|
||||
}
|
||||
|
||||
// Configure configures the device and its pins.
|
||||
func (d *Device) Configure(c Config) error {
|
||||
cs := c.ChipSelectPin
|
||||
dc := c.DataPin
|
||||
rst := c.ResetPin
|
||||
busy := c.BusyPin
|
||||
|
||||
cs.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
dc.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
rst.Configure(machine.PinConfig{Mode: machine.PinOutput})
|
||||
busy.Configure(machine.PinConfig{Mode: machine.PinInput})
|
||||
d.cs = cs.Set
|
||||
d.dc = dc.Set
|
||||
d.rst = rst.Set
|
||||
d.isBusy = busy.Get
|
||||
return nil
|
||||
}
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
"time"
|
||||
|
||||
"tinygo.org/x/drivers"
|
||||
"tinygo.org/x/drivers/internal/legacy"
|
||||
"tinygo.org/x/drivers/internal/pin"
|
||||
)
|
||||
|
||||
@@ -31,7 +32,7 @@ type Device struct {
|
||||
cs pin.OutputFunc
|
||||
dc pin.OutputFunc
|
||||
rst pin.OutputFunc
|
||||
isBusy drivers.PinInput
|
||||
isBusy pin.InputFunc
|
||||
logicalWidth int16
|
||||
width int16
|
||||
height int16
|
||||
@@ -62,10 +63,10 @@ var lutPartialUpdate = [30]uint8{
|
||||
|
||||
// New returns a new epd2in9 driver. Pass in a fully configured SPI bus.
|
||||
func New(bus drivers.SPI, csPin, dcPin, rstPin pin.Output, busyPin pin.Input) Device {
|
||||
pin.ConfigureOutput(csPin)
|
||||
pin.ConfigureOutput(dcPin)
|
||||
pin.ConfigureOutput(rstPin)
|
||||
pin.ConfigureInput(busyPin)
|
||||
legacy.ConfigurePinOut(csPin)
|
||||
legacy.ConfigurePinOut(dcPin)
|
||||
legacy.ConfigurePinOut(rstPin)
|
||||
legacy.ConfigurePinInput(busyPin)
|
||||
return Device{
|
||||
bus: bus,
|
||||
cs: csPin.Set,
|
||||
@@ -122,7 +123,7 @@ func (d *Device) Configure(cfg Config) {
|
||||
d.SendCommand(DATA_ENTRY_MODE_SETTING)
|
||||
d.SendData(0x03) // X increment; Y increment
|
||||
|
||||
d.SetLUT.High()
|
||||
d.SetLUT(true)
|
||||
}
|
||||
|
||||
// Reset resets the device
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"time"
|
||||
|
||||
"tinygo.org/x/drivers"
|
||||
"tinygo.org/x/drivers/internal/legacy"
|
||||
"tinygo.org/x/drivers/internal/pin"
|
||||
)
|
||||
|
||||
@@ -28,7 +29,7 @@ type Device struct {
|
||||
cs pin.OutputFunc
|
||||
dc pin.OutputFunc
|
||||
rst pin.OutputFunc
|
||||
isBusy drivers.PinInput
|
||||
isBusy pin.InputFunc
|
||||
logicalWidth int16
|
||||
width int16
|
||||
height int16
|
||||
@@ -41,10 +42,10 @@ type Rotation uint8
|
||||
|
||||
// New returns a new epd4in2 driver. Pass in a fully configured SPI bus.
|
||||
func New(bus drivers.SPI, csPin, dcPin, rstPin pin.Output, busyPin pin.Input) Device {
|
||||
pin.ConfigureOutput(csPin)
|
||||
pin.ConfigureOutput(dcPin)
|
||||
pin.ConfigureOutput(rstPin)
|
||||
pin.ConfigureInput(busyPin)
|
||||
legacy.ConfigurePinOut(csPin)
|
||||
legacy.ConfigurePinOut(dcPin)
|
||||
legacy.ConfigurePinOut(rstPin)
|
||||
legacy.ConfigurePinInput(busyPin)
|
||||
return Device{
|
||||
bus: bus,
|
||||
cs: csPin.Set,
|
||||
|
||||
Reference in New Issue
Block a user