Skip to content

chore: update all SPI usage to use either *machine.SPI or drivers.SPI #746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/flash/console/spi/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
func main() {
console_example.RunFor(
flash.NewSPI(
&machine.SPI1,
machine.SPI1,
machine.SPI1_SDO_PIN,
machine.SPI1_SDI_PIN,
machine.SPI1_SCK_PIN,
Expand Down
2 changes: 1 addition & 1 deletion examples/sdcard/console/feather-m4.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func init() {
spi = &machine.SPI0
spi = machine.SPI0
sckPin = machine.SPI0_SCK_PIN
sdoPin = machine.SPI0_SDO_PIN
sdiPin = machine.SPI0_SDI_PIN
Expand Down
2 changes: 1 addition & 1 deletion examples/sdcard/console/grandcentral-m4.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func init() {
spi = &machine.SPI1
spi = machine.SPI1
sckPin = machine.SDCARD_SCK_PIN
sdoPin = machine.SDCARD_SDO_PIN
sdiPin = machine.SDCARD_SDI_PIN
Expand Down
2 changes: 1 addition & 1 deletion examples/sdcard/console/m0.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func init() {
spi = &machine.SPI0
spi = machine.SPI0
sckPin = machine.SPI0_SCK_PIN
sdoPin = machine.SPI0_SDO_PIN
sdiPin = machine.SPI0_SDI_PIN
Expand Down
2 changes: 1 addition & 1 deletion examples/sdcard/console/p1am-100.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func init() {
spi = &machine.SDCARD_SPI
spi = machine.SDCARD_SPI
sckPin = machine.SDCARD_SCK_PIN
sdoPin = machine.SDCARD_SDO_PIN
sdiPin = machine.SDCARD_SDI_PIN
Expand Down
2 changes: 1 addition & 1 deletion examples/sdcard/console/pygamer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func init() {
spi = &machine.SPI0
spi = machine.SPI0
sckPin = machine.SPI0_SCK_PIN
sdoPin = machine.SPI0_SDO_PIN
sdiPin = machine.SPI0_SDI_PIN
Expand Down
2 changes: 1 addition & 1 deletion examples/sdcard/console/pyportal.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func init() {
spi = &machine.SPI0
spi = machine.SPI0
sckPin = machine.SPI0_SCK_PIN
sdoPin = machine.SPI0_SDO_PIN
sdiPin = machine.SPI0_SDI_PIN
Expand Down
2 changes: 1 addition & 1 deletion examples/sdcard/console/wioterminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func init() {
spi = &machine.SPI2
spi = machine.SPI2
sckPin = machine.SCK2
sdoPin = machine.SDO2
sdiPin = machine.SDI2
Expand Down
2 changes: 1 addition & 1 deletion examples/tmc5160/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func main() {
//bind csPin to driverAdddress
driverAddress := uint8(0) // Let's assume we are working with driver at address 0x01
// Step 3. Bind the communication interface to the protocol
comm := tmc5160.NewSPIComm(*spi, csPins)
comm := tmc5160.NewSPIComm(spi, csPins)
// Step 4. Define your stepper like this below
//stepper := tmc5160.NewStepper(angle , gearRatio vSupply rCoil , lCoil , iPeak , rSense , mSteps, fclk )
stepper := tmc5160.NewDefaultStepper() // Default Stepper should be used only for testing.
Expand Down
4 changes: 2 additions & 2 deletions ili9341/spi_atsamd21.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
)

type spiDriver struct {
bus machine.SPI
bus *machine.SPI
}

func NewSPI(bus machine.SPI, dc, cs, rst machine.Pin) *Device {
func NewSPI(bus *machine.SPI, dc, cs, rst machine.Pin) *Device {
return &Device{
dc: dc,
cs: cs,
Expand Down
4 changes: 2 additions & 2 deletions ili9341/spi_atsamd51.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
)

type spiDriver struct {
bus machine.SPI
bus *machine.SPI
}

func NewSPI(bus machine.SPI, dc, cs, rst machine.Pin) *Device {
func NewSPI(bus *machine.SPI, dc, cs, rst machine.Pin) *Device {
return &Device{
dc: dc,
cs: cs,
Expand Down
6 changes: 4 additions & 2 deletions max6675/max6675.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,23 @@ package max6675
import (
"errors"
"machine"

"tinygo.org/x/drivers"
)

// ErrThermocoupleOpen is returned when the thermocouple input is open.
// i.e. not attached or faulty
var ErrThermocoupleOpen = errors.New("thermocouple input open")

type Device struct {
bus machine.SPI
bus drivers.SPI
cs machine.Pin
}

// Create a new Device to read from a MAX6675 thermocouple.
// Pins must be configured before use. Frequency for SPI
// should be 4.3MHz maximum.
func NewDevice(bus machine.SPI, cs machine.Pin) *Device {
func NewDevice(bus drivers.SPI, cs machine.Pin) *Device {
return &Device{
bus: bus,
cs: cs,
Expand Down
6 changes: 4 additions & 2 deletions max72xx/max72xx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@ package max72xx

import (
"machine"

"tinygo.org/x/drivers"
)

type Device struct {
bus machine.SPI
bus drivers.SPI
cs machine.Pin
}

// NewDriver creates a new max7219 connection. The SPI wire must already be configured
// The SPI frequency must not be higher than 10MHz.
// parameter cs: the datasheet also refers to this pin as "load" pin.
func NewDevice(bus machine.SPI, cs machine.Pin) *Device {
func NewDevice(bus drivers.SPI, cs machine.Pin) *Device {
return &Device{
bus: bus,
cs: cs,
Expand Down
3 changes: 2 additions & 1 deletion p1am/p1am.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import (
)

type P1AM struct {
bus machine.SPI
bus *machine.SPI

slaveSelectPin, slaveAckPin, baseEnablePin machine.Pin

// SkipAutoConfig will skip loading a default configuration into each module.
Expand Down
2 changes: 1 addition & 1 deletion sx127x/sx127x.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (d *Device) GetRadioEventChan() chan lora.RadioEvent {
}

// New creates a new SX127x connection. The SPI bus must already be configured.
func New(spi machine.SPI, rstPin machine.Pin) *Device {
func New(spi drivers.SPI, rstPin machine.Pin) *Device {
k := Device{
spi: spi,
rstPin: rstPin,
Expand Down
2 changes: 1 addition & 1 deletion tmc5160/README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ if err != nil {

## API Reference

NewSPIComm(spi machine.SPI, csPins map[uint8]machine.Pin) *SPIComm
NewSPIComm(spi *machine.SPI, csPins map[uint8]machine.Pin) *SPIComm

Creates a new SPI communication interface for the TMC5160.

Expand Down
12 changes: 6 additions & 6 deletions tmc5160/spicomm.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ func (e CustomError) Error() string {

// SPIComm implements RegisterComm for SPI-based communication
type SPIComm struct {
spi machine.SPI
spi *machine.SPI
CsPins map[uint8]machine.Pin // Map to store CS pin for each Driver by its address
}

// NewSPIComm creates a new SPIComm instance.
func NewSPIComm(spi machine.SPI, csPins map[uint8]machine.Pin) *SPIComm {
func NewSPIComm(spi *machine.SPI, csPins map[uint8]machine.Pin) *SPIComm {
return &SPIComm{
spi: spi,
CsPins: csPins,
Expand All @@ -31,7 +31,7 @@ func NewSPIComm(spi machine.SPI, csPins map[uint8]machine.Pin) *SPIComm {
// Setup initializes the SPI communication with the Driver and configures all CS pins.
func (comm *SPIComm) Setup() error {
// Check if SPI is initialized
if comm.spi == (machine.SPI{}) {
if comm.spi == nil {
return CustomError("SPI not initialized")
}

Expand Down Expand Up @@ -66,7 +66,7 @@ func (comm *SPIComm) WriteRegister(register uint8, value uint32, driverAddress u
addressWithWriteAccess := register | 0x80

// Send the address and the data to write (split into 4 bytes)
_, err := spiTransfer40(&comm.spi, addressWithWriteAccess, value)
_, err := spiTransfer40(comm.spi, addressWithWriteAccess, value)
if err != nil {
csPin.High()
return CustomError("Failed to write register")
Expand All @@ -88,7 +88,7 @@ func (comm *SPIComm) ReadRegister(register uint8, driverAddress uint8) (uint32,
csPin.Low()

// Step 1: Send a dummy write operation to begin the read sequence
_, err := spiTransfer40(&comm.spi, register, 0x00) // Send dummy data
_, err := spiTransfer40(comm.spi, register, 0x00) // Send dummy data
if err != nil {
csPin.High()
return 0, CustomError("Failed to send dummy write")
Expand All @@ -97,7 +97,7 @@ func (comm *SPIComm) ReadRegister(register uint8, driverAddress uint8) (uint32,
time.Sleep(176 * time.Nanosecond)
csPin.Low()
// Step 2: Send the register read request again to get the actual value
response, err := spiTransfer40(&comm.spi, register, 0x00) // Send again to get actual register data
response, err := spiTransfer40(comm.spi, register, 0x00) // Send again to get actual register data
if err != nil {
csPin.High()
return 0, CustomError("Failed to read register")
Expand Down
4 changes: 2 additions & 2 deletions waveshare-epd/epd1in54/epd1in54.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type Config struct {
}

type Device struct {
bus machine.SPI
bus *machine.SPI
cs machine.Pin
dc machine.Pin
rst machine.Pin
Expand Down Expand Up @@ -79,7 +79,7 @@ var partialRefresh = [159]uint8{
}

// New returns a new epd1in54 driver. Pass in a fully configured SPI bus.
func New(bus machine.SPI, csPin, dcPin, rstPin, busyPin machine.Pin) Device {
func New(bus *machine.SPI, csPin, dcPin, rstPin, busyPin machine.Pin) Device {
return Device{
buffer: make([]uint8, (uint32(Width)*uint32(Height))/8),
bus: bus,
Expand Down