Skip to content
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

refactor: use *SPI everywhere to make consistent for implementations. #4756

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
6 changes: 3 additions & 3 deletions src/machine/board_feather-stm32f405.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,15 +186,15 @@ const (
)

var (
SPI1 = SPI{
SPI1 = &SPI{
Bus: stm32.SPI2,
AltFuncSelector: AF5_SPI1_SPI2,
}
SPI2 = SPI{
SPI2 = &SPI{
Bus: stm32.SPI3,
AltFuncSelector: AF6_SPI3,
}
SPI3 = SPI{
SPI3 = &SPI{
Bus: stm32.SPI1,
AltFuncSelector: AF5_SPI1_SPI2,
}
Expand Down
4 changes: 2 additions & 2 deletions src/machine/board_lgt92.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ var (
I2C0 = I2C1

// SPI
SPI0 = SPI{
SPI0 = &SPI{
Bus: stm32.SPI1,
}
SPI1 = &SPI0
SPI1 = SPI0
)

func init() {
Expand Down
4 changes: 2 additions & 2 deletions src/machine/board_maixbit_baremetal.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import "device/kendryte"

// SPI on the MAix Bit.
var (
SPI0 = SPI{
SPI0 = &SPI{
Bus: kendryte.SPI0,
}
SPI1 = SPI{
SPI1 = &SPI{
Bus: kendryte.SPI1,
}
)
4 changes: 2 additions & 2 deletions src/machine/board_mksnanov3.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ const (

// Since the first interface is named SPI1, both SPI0 and SPI1 refer to SPI1.
var (
SPI0 = SPI{
SPI0 = &SPI{
Bus: stm32.SPI1,
AltFuncSelector: AF5_SPI1_SPI2,
}
SPI1 = &SPI0
SPI1 = SPI0
)

const (
Expand Down
4 changes: 2 additions & 2 deletions src/machine/board_nucleol031k6.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ var (
I2C0 = I2C1

// SPI
SPI0 = SPI{
SPI0 = &SPI{
Bus: stm32.SPI1,
AltFuncSelector: 0,
}
SPI1 = &SPI0
SPI1 = SPI0
)

func init() {
Expand Down
4 changes: 2 additions & 2 deletions src/machine/board_stm32f469disco.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ const (
// Since the first interface is named SPI1, both SPI0 and SPI1 refer to SPI1.
// TODO: implement SPI2 and SPI3.
var (
SPI0 = SPI{
SPI0 = &SPI{
Bus: stm32.SPI1,
AltFuncSelector: AF5_SPI1_SPI2,
}
SPI1 = &SPI0
SPI1 = SPI0
)

const (
Expand Down
4 changes: 2 additions & 2 deletions src/machine/board_stm32f4disco.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ const (
// Since the first interface is named SPI1, both SPI0 and SPI1 refer to SPI1.
// TODO: implement SPI2 and SPI3.
var (
SPI0 = SPI{
SPI0 = &SPI{
Bus: stm32.SPI1,
AltFuncSelector: AF5_SPI1_SPI2,
}
SPI1 = &SPI0
SPI1 = SPI0
)

const (
Expand Down
4 changes: 2 additions & 2 deletions src/machine/machine_atmega.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ type SPI struct {
}

// Configure is intended to setup the SPI interface.
func (s SPI) Configure(config SPIConfig) error {
func (s *SPI) Configure(config SPIConfig) error {

// This is only here to help catch a bug with the configuration
// where a machine missed a value.
Expand Down Expand Up @@ -330,7 +330,7 @@ func (s SPI) Configure(config SPIConfig) error {
}

// Transfer writes the byte into the register and returns the read content
func (s SPI) Transfer(b byte) (byte, error) {
func (s *SPI) Transfer(b byte) (byte, error) {
s.spdr.Set(uint8(b))

for !s.spsr.HasBits(s.spsrSPIF) {
Expand Down
2 changes: 1 addition & 1 deletion src/machine/machine_atmega1280.go
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,7 @@ func (pwm PWM) Set(channel uint8, value uint32) {
}

// SPI configuration
var SPI0 = SPI{
var SPI0 = &SPI{
spcr: avr.SPCR,
spdr: avr.SPDR,
spsr: avr.SPSR,
Expand Down
2 changes: 1 addition & 1 deletion src/machine/machine_atmega1284p.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (p Pin) getPortMask() (*volatile.Register8, uint8) {
}

// SPI configuration
var SPI0 = SPI{
var SPI0 = &SPI{
spcr: avr.SPCR,
spsr: avr.SPSR,
spdr: avr.SPDR,
Expand Down
2 changes: 1 addition & 1 deletion src/machine/machine_atmega2560.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (p Pin) getPortMask() (*volatile.Register8, uint8) {
}

// SPI configuration
var SPI0 = SPI{
var SPI0 = &SPI{
spcr: avr.SPCR,
spdr: avr.SPDR,
spsr: avr.SPSR,
Expand Down
2 changes: 1 addition & 1 deletion src/machine/machine_atmega328p.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var I2C0 = &I2C{
}

// SPI configuration
var SPI0 = SPI{
var SPI0 = &SPI{
spcr: avr.SPCR,
spdr: avr.SPDR,
spsr: avr.SPSR,
Expand Down
4 changes: 2 additions & 2 deletions src/machine/machine_atmega328pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ var I2C1 = &I2C{
}

// SPI configuration
var SPI0 = SPI{
var SPI0 = &SPI{
spcr: avr.SPCR0,
spdr: avr.SPDR0,
spsr: avr.SPSR0,
Expand All @@ -82,7 +82,7 @@ var SPI0 = SPI{
cs: PB2,
}

var SPI1 = SPI{
var SPI1 = &SPI{
spcr: avr.SPCR1,
spdr: avr.SPDR1,
spsr: avr.SPSR1,
Expand Down
12 changes: 6 additions & 6 deletions src/machine/machine_atsamd21.go
Original file line number Diff line number Diff line change
Expand Up @@ -1225,7 +1225,7 @@ type SPIConfig struct {
}

// Configure is intended to setup the SPI interface.
func (spi SPI) Configure(config SPIConfig) error {
func (spi *SPI) Configure(config SPIConfig) error {
// Use default pins if not set.
if config.SCK == 0 && config.SDO == 0 && config.SDI == 0 {
config.SCK = SPI0_SCK_PIN
Expand Down Expand Up @@ -1347,7 +1347,7 @@ func (spi SPI) Configure(config SPIConfig) error {
}

// Transfer writes/reads a single byte using the SPI interface.
func (spi SPI) Transfer(w byte) (byte, error) {
func (spi *SPI) Transfer(w byte) (byte, error) {
// write data
spi.Bus.DATA.Set(uint32(w))

Expand Down Expand Up @@ -1376,7 +1376,7 @@ func (spi SPI) Transfer(w byte) (byte, error) {
// This form sends zeros, putting the result into the rx buffer. Good for reading a "result packet":
//
// spi.Tx(nil, rx)
func (spi SPI) Tx(w, r []byte) error {
func (spi *SPI) Tx(w, r []byte) error {
switch {
case w == nil:
// read only, so write zero and read a result.
Expand All @@ -1397,7 +1397,7 @@ func (spi SPI) Tx(w, r []byte) error {
return nil
}

func (spi SPI) tx(tx []byte) {
func (spi *SPI) tx(tx []byte) {
for i := 0; i < len(tx); i++ {
for !spi.Bus.INTFLAG.HasBits(sam.SERCOM_SPI_INTFLAG_DRE) {
}
Expand All @@ -1412,7 +1412,7 @@ func (spi SPI) tx(tx []byte) {
}
}

func (spi SPI) rx(rx []byte) {
func (spi *SPI) rx(rx []byte) {
spi.Bus.DATA.Set(0)
for !spi.Bus.INTFLAG.HasBits(sam.SERCOM_SPI_INTFLAG_DRE) {
}
Expand All @@ -1428,7 +1428,7 @@ func (spi SPI) rx(rx []byte) {
rx[len(rx)-1] = byte(spi.Bus.DATA.Get())
}

func (spi SPI) txrx(tx, rx []byte) {
func (spi *SPI) txrx(tx, rx []byte) {
spi.Bus.DATA.Set(uint32(tx[0]))
for !spi.Bus.INTFLAG.HasBits(sam.SERCOM_SPI_INTFLAG_DRE) {
}
Expand Down
12 changes: 6 additions & 6 deletions src/machine/machine_atsamd51.go
Original file line number Diff line number Diff line change
Expand Up @@ -1432,7 +1432,7 @@ type SPIConfig struct {
}

// Configure is intended to setup the SPI interface.
func (spi SPI) Configure(config SPIConfig) error {
func (spi *SPI) Configure(config SPIConfig) error {
// Use default pins if not set.
if config.SCK == 0 && config.SDO == 0 && config.SDI == 0 {
config.SCK = SPI0_SCK_PIN
Expand Down Expand Up @@ -1575,7 +1575,7 @@ func (spi SPI) Configure(config SPIConfig) error {
}

// Transfer writes/reads a single byte using the SPI interface.
func (spi SPI) Transfer(w byte) (byte, error) {
func (spi *SPI) Transfer(w byte) (byte, error) {
// write data
spi.Bus.DATA.Set(uint32(w))

Expand Down Expand Up @@ -1604,7 +1604,7 @@ func (spi SPI) Transfer(w byte) (byte, error) {
// This form sends zeros, putting the result into the rx buffer. Good for reading a "result packet":
//
// spi.Tx(nil, rx)
func (spi SPI) Tx(w, r []byte) error {
func (spi *SPI) Tx(w, r []byte) error {
switch {
case w == nil:
// read only, so write zero and read a result.
Expand All @@ -1625,7 +1625,7 @@ func (spi SPI) Tx(w, r []byte) error {
return nil
}

func (spi SPI) tx(tx []byte) {
func (spi *SPI) tx(tx []byte) {
for i := 0; i < len(tx); i++ {
for !spi.Bus.INTFLAG.HasBits(sam.SERCOM_SPIM_INTFLAG_DRE) {
}
Expand All @@ -1640,7 +1640,7 @@ func (spi SPI) tx(tx []byte) {
}
}

func (spi SPI) rx(rx []byte) {
func (spi *SPI) rx(rx []byte) {
spi.Bus.DATA.Set(0)
for !spi.Bus.INTFLAG.HasBits(sam.SERCOM_SPIM_INTFLAG_DRE) {
}
Expand All @@ -1656,7 +1656,7 @@ func (spi SPI) rx(rx []byte) {
rx[len(rx)-1] = byte(spi.Bus.DATA.Get())
}

func (spi SPI) txrx(tx, rx []byte) {
func (spi *SPI) txrx(tx, rx []byte) {
spi.Bus.DATA.Set(uint32(tx[0]))
for !spi.Bus.INTFLAG.HasBits(sam.SERCOM_SPIM_INTFLAG_DRE) {
}
Expand Down
6 changes: 3 additions & 3 deletions src/machine/machine_esp32.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ type SPIConfig struct {
}

// Configure and make the SPI peripheral ready to use.
func (spi SPI) Configure(config SPIConfig) error {
func (spi *SPI) Configure(config SPIConfig) error {
if config.Frequency == 0 {
config.Frequency = 4e6 // default to 4MHz
}
Expand Down Expand Up @@ -445,7 +445,7 @@ func (spi SPI) Configure(config SPIConfig) error {

// Transfer writes/reads a single byte using the SPI interface. If you need to
// transfer larger amounts of data, Tx will be faster.
func (spi SPI) Transfer(w byte) (byte, error) {
func (spi *SPI) Transfer(w byte) (byte, error) {
spi.Bus.MISO_DLEN.Set(7 << esp.SPI_MISO_DLEN_USR_MISO_DBITLEN_Pos)
spi.Bus.MOSI_DLEN.Set(7 << esp.SPI_MOSI_DLEN_USR_MOSI_DBITLEN_Pos)

Expand All @@ -464,7 +464,7 @@ func (spi SPI) Transfer(w byte) (byte, error) {
// interface, there must always be the same number of bytes written as bytes read.
// This is accomplished by sending zero bits if r is bigger than w or discarding
// the incoming data if w is bigger than r.
func (spi SPI) Tx(w, r []byte) error {
func (spi *SPI) Tx(w, r []byte) error {
toTransfer := len(w)
if len(r) > toTransfer {
toTransfer = len(r)
Expand Down
6 changes: 3 additions & 3 deletions src/machine/machine_esp32c3_spi.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func freqToClockDiv(hz uint32) uint32 {
}

// Configure and make the SPI peripheral ready to use.
func (spi SPI) Configure(config SPIConfig) error {
func (spi *SPI) Configure(config SPIConfig) error {
// right now this is only setup to work for the esp32c3 spi2 bus
if spi.Bus != esp.SPI2 {
return ErrInvalidSPIBus
Expand Down Expand Up @@ -216,7 +216,7 @@ func (spi SPI) Configure(config SPIConfig) error {

// Transfer writes/reads a single byte using the SPI interface. If you need to
// transfer larger amounts of data, Tx will be faster.
func (spi SPI) Transfer(w byte) (byte, error) {
func (spi *SPI) Transfer(w byte) (byte, error) {
spi.Bus.SetMS_DLEN_MS_DATA_BITLEN(7)

spi.Bus.SetW0(uint32(w))
Expand All @@ -238,7 +238,7 @@ func (spi SPI) Transfer(w byte) (byte, error) {
// interface, there must always be the same number of bytes written as bytes read.
// This is accomplished by sending zero bits if r is bigger than w or discarding
// the incoming data if w is bigger than r.
func (spi SPI) Tx(w, r []byte) error {
func (spi *SPI) Tx(w, r []byte) error {
toTransfer := len(w)
if len(r) > toTransfer {
toTransfer = len(r)
Expand Down
4 changes: 2 additions & 2 deletions src/machine/machine_fe310.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ type SPIConfig struct {
}

// Configure is intended to setup the SPI interface.
func (spi SPI) Configure(config SPIConfig) error {
func (spi *SPI) Configure(config SPIConfig) error {
// Use default pins if not set.
if config.SCK == 0 && config.SDO == 0 && config.SDI == 0 {
config.SCK = SPI0_SCK_PIN
Expand Down Expand Up @@ -197,7 +197,7 @@ func (spi SPI) Configure(config SPIConfig) error {
}

// Transfer writes/reads a single byte using the SPI interface.
func (spi SPI) Transfer(w byte) (byte, error) {
func (spi *SPI) Transfer(w byte) (byte, error) {
// wait for tx ready
for spi.Bus.TXDATA.HasBits(sifive.QSPI_TXDATA_FULL) {
}
Expand Down
6 changes: 3 additions & 3 deletions src/machine/machine_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ type SPIConfig struct {
Mode uint8
}

func (spi SPI) Configure(config SPIConfig) error {
func (spi *SPI) Configure(config SPIConfig) error {
spiConfigure(spi.Bus, config.SCK, config.SDO, config.SDI)
return nil
}

// Transfer writes/reads a single byte using the SPI interface.
func (spi SPI) Transfer(w byte) (byte, error) {
func (spi *SPI) Transfer(w byte) (byte, error) {
return spiTransfer(spi.Bus, w), nil
}

Expand All @@ -87,7 +87,7 @@ func (spi SPI) Transfer(w byte) (byte, error) {
// This form sends zeros, putting the result into the rx buffer. Good for reading a "result packet":
//
// spi.Tx(nil, rx)
func (spi SPI) Tx(w, r []byte) error {
func (spi *SPI) Tx(w, r []byte) error {
var wptr, rptr *byte
var wlen, rlen int
if len(w) != 0 {
Expand Down
4 changes: 2 additions & 2 deletions src/machine/machine_generic_peripherals.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ package machine
var (
UART0 = hardwareUART0
UART1 = hardwareUART1
SPI0 = SPI{0}
SPI1 = SPI{1}
SPI0 = &SPI{0}
SPI1 = &SPI{1}
I2C0 = &I2C{0}
)
Loading
Loading