Skip to content

Commit 8a31ae1

Browse files
aykevldeadprogram
authored andcommitted
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.
1 parent 26ee3fb commit 8a31ae1

File tree

4 files changed

+13
-9
lines changed

4 files changed

+13
-9
lines changed

src/machine/machine_nrf52.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,5 @@ const eraseBlockSizeValue = 4096
6969
func eraseBlockSize() int64 {
7070
return eraseBlockSizeValue
7171
}
72+
73+
const spiMaxBufferSize = 255 // from the datasheet: TXD.MAXCNT and RXD.MAXCNT

src/machine/machine_nrf52833.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,5 @@ const eraseBlockSizeValue = 4096
9090
func eraseBlockSize() int64 {
9191
return eraseBlockSizeValue
9292
}
93+
94+
const spiMaxBufferSize = 0xffff // from the datasheet: TXD.MAXCNT and RXD.MAXCNT

src/machine/machine_nrf52840.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,3 +108,5 @@ const eraseBlockSizeValue = 4096
108108
func eraseBlockSize() int64 {
109109
return eraseBlockSizeValue
110110
}
111+
112+
const spiMaxBufferSize = 0xffff // from the datasheet: TXD.MAXCNT and RXD.MAXCNT

src/machine/machine_nrf52xxx.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -301,18 +301,16 @@ func (spi *SPI) Transfer(w byte) (byte, error) {
301301
// padded until they fit: if len(w) > len(r) the extra bytes received will be
302302
// dropped and if len(w) < len(r) extra 0 bytes will be sent.
303303
func (spi *SPI) Tx(w, r []byte) error {
304-
// Unfortunately the hardware (on the nrf52832) only supports up to 255
305-
// bytes in the buffers, so if either w or r is longer than that the
306-
// transfer needs to be broken up in pieces.
307-
// The nrf52840 supports far larger buffers however, which isn't yet
308-
// supported.
304+
// Unfortunately the hardware (on the nrf52832) only supports a limited
305+
// amount of bytes in the buffers (depending on the chip), so if either w or
306+
// r is longer than that the transfer needs to be broken up in pieces.
309307
for len(r) != 0 || len(w) != 0 {
310308
// Prepare the SPI transfer: set the DMA pointers and lengths.
311309
// read buffer
312310
nr := uint32(len(r))
313311
if nr > 0 {
314-
if nr > 255 {
315-
nr = 255
312+
if nr > spiMaxBufferSize {
313+
nr = spiMaxBufferSize
316314
}
317315
spi.Bus.RXD.PTR.Set(uint32(uintptr(unsafe.Pointer(&r[0]))))
318316
r = r[nr:]
@@ -322,8 +320,8 @@ func (spi *SPI) Tx(w, r []byte) error {
322320
// write buffer
323321
nw := uint32(len(w))
324322
if nw > 0 {
325-
if nw > 255 {
326-
nw = 255
323+
if nw > spiMaxBufferSize {
324+
nw = spiMaxBufferSize
327325
}
328326
spi.Bus.TXD.PTR.Set(uint32(uintptr(unsafe.Pointer(&w[0]))))
329327
w = w[nw:]

0 commit comments

Comments
 (0)