From 43e463d7f22fc8c1d05cde47844b6087545b9f43 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Tue, 21 Oct 2025 10:11:49 +0200 Subject: [PATCH] machine: use larger SPI MAXCNT on nrf52833 and nrf52840 These chips have a larger upper limit for the DMA transfer than the nrf52832. For best performance, we should be splitting the transfer in as large blocks as possible on the given hardware. --- src/machine/machine_nrf52.go | 2 ++ src/machine/machine_nrf52833.go | 2 ++ src/machine/machine_nrf52840.go | 2 ++ src/machine/machine_nrf52xxx.go | 16 +++++++--------- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/machine/machine_nrf52.go b/src/machine/machine_nrf52.go index 71c534325d..11d30c8ca9 100644 --- a/src/machine/machine_nrf52.go +++ b/src/machine/machine_nrf52.go @@ -69,3 +69,5 @@ const eraseBlockSizeValue = 4096 func eraseBlockSize() int64 { return eraseBlockSizeValue } + +const spiMaxBufferSize = 255 // from the datasheet: TXD.MAXCNT and RXD.MAXCNT diff --git a/src/machine/machine_nrf52833.go b/src/machine/machine_nrf52833.go index 60558eb0e4..9eff66c435 100644 --- a/src/machine/machine_nrf52833.go +++ b/src/machine/machine_nrf52833.go @@ -90,3 +90,5 @@ const eraseBlockSizeValue = 4096 func eraseBlockSize() int64 { return eraseBlockSizeValue } + +const spiMaxBufferSize = 0xffff // from the datasheet: TXD.MAXCNT and RXD.MAXCNT diff --git a/src/machine/machine_nrf52840.go b/src/machine/machine_nrf52840.go index 21a4367803..c30933ba4e 100644 --- a/src/machine/machine_nrf52840.go +++ b/src/machine/machine_nrf52840.go @@ -108,3 +108,5 @@ const eraseBlockSizeValue = 4096 func eraseBlockSize() int64 { return eraseBlockSizeValue } + +const spiMaxBufferSize = 0xffff // from the datasheet: TXD.MAXCNT and RXD.MAXCNT diff --git a/src/machine/machine_nrf52xxx.go b/src/machine/machine_nrf52xxx.go index a582a7aa56..8af53cad34 100644 --- a/src/machine/machine_nrf52xxx.go +++ b/src/machine/machine_nrf52xxx.go @@ -301,18 +301,16 @@ func (spi *SPI) Transfer(w byte) (byte, error) { // padded until they fit: if len(w) > len(r) the extra bytes received will be // dropped and if len(w) < len(r) extra 0 bytes will be sent. func (spi *SPI) Tx(w, r []byte) error { - // Unfortunately the hardware (on the nrf52832) only supports up to 255 - // bytes in the buffers, so if either w or r is longer than that the - // transfer needs to be broken up in pieces. - // The nrf52840 supports far larger buffers however, which isn't yet - // supported. + // Unfortunately the hardware (on the nrf52832) only supports a limited + // amount of bytes in the buffers (depending on the chip), so if either w or + // r is longer than that the transfer needs to be broken up in pieces. for len(r) != 0 || len(w) != 0 { // Prepare the SPI transfer: set the DMA pointers and lengths. // read buffer nr := uint32(len(r)) if nr > 0 { - if nr > 255 { - nr = 255 + if nr > spiMaxBufferSize { + nr = spiMaxBufferSize } spi.Bus.RXD.PTR.Set(uint32(uintptr(unsafe.Pointer(&r[0])))) r = r[nr:] @@ -322,8 +320,8 @@ func (spi *SPI) Tx(w, r []byte) error { // write buffer nw := uint32(len(w)) if nw > 0 { - if nw > 255 { - nw = 255 + if nw > spiMaxBufferSize { + nw = spiMaxBufferSize } spi.Bus.TXD.PTR.Set(uint32(uintptr(unsafe.Pointer(&w[0])))) w = w[nw:]