From 568c2a4363199f5075fb50e8d43643b5f4059ec6 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Tue, 28 Mar 2023 20:56:34 +0200 Subject: [PATCH] rp2040: remove SPI DataBits property As discussed on Slack, I believe this property does more harm than good: * I don't think it's used anywhere. None of the drivers use it. * It is not fully implemented. While values <= 8 might work fine, values larger than 8 result in extra zero bits (instead of anything sensible). * Worse, it doesn't return an error when it's out of range. This is not an optional property: if the SPI peripheral doesn't support a particular number of bits, it should return an error instead of silently limiting the number of bits. This will be confusing to users. Therefore, I propose we drop it. Maybe there are good uses for it (perhaps for displays that use big endian 16-bit values?), but without a good use case like a driver in tinygo.org/x/drivers, I think it's more trouble than it's worth. --- src/machine/machine_rp2040_spi.go | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/machine/machine_rp2040_spi.go b/src/machine/machine_rp2040_spi.go index 62c5c0c0..d21ad1f8 100644 --- a/src/machine/machine_rp2040_spi.go +++ b/src/machine/machine_rp2040_spi.go @@ -26,8 +26,6 @@ type SPIConfig struct { LSBFirst bool // Mode's two most LSB are CPOL and CPHA. i.e. Mode==2 (0b10) is CPOL=1, CPHA=0 Mode uint8 - // Number of data bits per transfer. Valid values 4..16. Default and recommended is 8. - DataBits uint8 // Serial clock pin SCK Pin // TX or Serial Data Out (MOSI if rp2040 is master) @@ -199,9 +197,6 @@ func (spi SPI) Configure(config SPIConfig) error { return errSPIInvalidSCK } - if config.DataBits < 4 || config.DataBits > 16 { - config.DataBits = 8 - } if config.Frequency == 0 { config.Frequency = defaultBaud } @@ -221,7 +216,7 @@ func (spi SPI) initSPI(config SPIConfig) (err error) { } err = spi.SetBaudRate(config.Frequency) // Set SPI Format (CPHA and CPOL) and frame format (default is Motorola) - spi.setFormat(config.DataBits, config.Mode, rp.XIP_SSI_CTRLR0_SPI_FRF_STD) + spi.setFormat(config.Mode, rp.XIP_SSI_CTRLR0_SPI_FRF_STD) // Always enable DREQ signals -- harmless if DMA is not listening spi.Bus.SSPDMACR.SetBits(rp.SPI0_SSPDMACR_TXDMAE | rp.SPI0_SSPDMACR_RXDMAE) @@ -231,13 +226,13 @@ func (spi SPI) initSPI(config SPIConfig) (err error) { } //go:inline -func (spi SPI) setFormat(databits, mode uint8, frameFormat uint32) { +func (spi SPI) setFormat(mode uint8, frameFormat uint32) { cpha := uint32(mode) & 1 cpol := uint32(mode>>1) & 1 spi.Bus.SSPCR0.ReplaceBits( (cpha<