Skip to content

Commit

Permalink
machine/spi: use interface to ensure uniformity for all machine imple…
Browse files Browse the repository at this point in the history
…mentations

Signed-off-by: deadprogram <[email protected]>
  • Loading branch information
deadprogram committed Oct 17, 2023
1 parent 7019c4e commit fa4ca63
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/machine/machine_generic.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ type SPIConfig struct {
Mode uint8
}

func (spi SPI) Configure(config SPIConfig) {
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.
Expand Down
4 changes: 3 additions & 1 deletion src/machine/machine_mimxrt1062_spi.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ var (
)

// Configure is intended to setup an SPI interface for transmit/receive.
func (spi *SPI) Configure(config SPIConfig) {
func (spi *SPI) Configure(config SPIConfig) error {

const defaultSpiFreq = 4000000 // 4 MHz

Expand Down Expand Up @@ -132,6 +132,8 @@ func (spi *SPI) Configure(config SPIConfig) {
spi.Bus.CR.Set(nxp.LPSPI_CR_MEN)

spi.configured = true

return nil
}

// Transfer writes/reads a single byte using the SPI interface.
Expand Down
4 changes: 3 additions & 1 deletion src/machine/machine_nrf51.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type SPIConfig struct {
}

// Configure is intended to setup the SPI interface.
func (spi SPI) Configure(config SPIConfig) {
func (spi SPI) Configure(config SPIConfig) error {
// Disable bus to configure it
spi.Bus.ENABLE.Set(nrf.SPI_ENABLE_ENABLE_Disabled)

Expand Down Expand Up @@ -117,6 +117,8 @@ func (spi SPI) Configure(config SPIConfig) {

// Re-enable bus now that it is configured.
spi.Bus.ENABLE.Set(nrf.SPI_ENABLE_ENABLE_Enabled)

return nil
}

// Transfer writes/reads a single byte using the SPI interface.
Expand Down
4 changes: 3 additions & 1 deletion src/machine/machine_nrf52xxx.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ type SPIConfig struct {
}

// Configure is intended to setup the SPI interface.
func (spi SPI) Configure(config SPIConfig) {
func (spi SPI) Configure(config SPIConfig) error {
// Disable bus to configure it
spi.Bus.ENABLE.Set(nrf.SPIM_ENABLE_ENABLE_Disabled)

Expand Down Expand Up @@ -265,6 +265,8 @@ func (spi SPI) Configure(config SPIConfig) {

// Re-enable bus now that it is configured.
spi.Bus.ENABLE.Set(nrf.SPIM_ENABLE_ENABLE_Enabled)

return nil
}

// Transfer writes/reads a single byte using the SPI interface.
Expand Down
4 changes: 3 additions & 1 deletion src/machine/machine_stm32_spi.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type SPIConfig struct {
}

// Configure is intended to setup the STM32 SPI1 interface.
func (spi SPI) Configure(config SPIConfig) {
func (spi SPI) Configure(config SPIConfig) error {

// -- CONFIGURING THE SPI IN MASTER MODE --
//
Expand Down Expand Up @@ -93,6 +93,8 @@ func (spi SPI) Configure(config SPIConfig) {

// enable SPI
spi.Bus.CR1.SetBits(stm32.SPI_CR1_SPE)

return nil
}

// Transfer writes/reads a single byte using the SPI interface.
Expand Down
11 changes: 11 additions & 0 deletions src/machine/spi.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,14 @@ var (
ErrTxInvalidSliceSize = errors.New("SPI write and read slices must be same size")
errSPIInvalidMachineConfig = errors.New("SPI port was not configured properly by the machine")
)

// If you are getting a compile error on this line please check to see you've
// correctly implemented the methods on the SPI type. They must match
// the interface method signatures type to type perfectly.
// If not implementing the SPI type please remove your target from the build tags
// at the top of this file.
var _ interface { // 2
Configure(config SPIConfig) error
Tx(w, r []byte) error
Transfer(w byte) (byte, error)
} = (*SPI)(nil)

0 comments on commit fa4ca63

Please sign in to comment.