Skip to content

Commit

Permalink
Refactor spi.TX to get cleaner look
Browse files Browse the repository at this point in the history
  • Loading branch information
Pertti Erkkilä committed Aug 24, 2023
1 parent d444983 commit d3a4e8c
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions src/machine/machine_nrf52xxx.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,28 +288,27 @@ func (spi SPI) Tx(w, r []byte) error {
// supported.
for len(r) != 0 || len(w) != 0 {
// Prepare the SPI transfer: set the DMA pointers and lengths.
if len(r) != 0 {
spi.Bus.RXD.PTR.Set(uint32(uintptr(unsafe.Pointer(&r[0]))))
n := uint32(len(r))
if n > 255 {
n = 255
// read buffer
nr := uint32(len(r))
if nr > 0 {
if nr > 255 {
nr = 255
}
spi.Bus.RXD.MAXCNT.Set(n)
r = r[n:]
} else {
spi.Bus.RXD.MAXCNT.Set(0)
spi.Bus.RXD.PTR.Set(uint32(uintptr(unsafe.Pointer(&r[0]))))
r = r[nr:]
}
if len(w) != 0 {
spi.Bus.TXD.PTR.Set(uint32(uintptr(unsafe.Pointer(&w[0]))))
n := uint32(len(w))
if n > 255 {
n = 255
spi.Bus.RXD.MAXCNT.Set(nr)

// write buffer
nw := uint32(len(w))
if nw > 0 {
if nw > 255 {
nw = 255
}
spi.Bus.TXD.MAXCNT.Set(n)
w = w[n:]
} else {
spi.Bus.TXD.MAXCNT.Set(0)
spi.Bus.TXD.PTR.Set(uint32(uintptr(unsafe.Pointer(&w[0]))))
w = w[nw:]
}
spi.Bus.TXD.MAXCNT.Set(nw)

// Do the transfer.
// Note: this can be improved by not waiting until the transfer is
Expand Down

0 comments on commit d3a4e8c

Please sign in to comment.