Skip to content

Commit cec83a3

Browse files
committed
refactor(src/machine/nrf52xxx/spi): move buffer allocation to Transfer method, use pointer receiver
Signed-off-by: Paul Schroeder <milkpirate@users.noreply.github.com>
1 parent 227f6dc commit cec83a3

File tree

1 file changed

+5
-14
lines changed

1 file changed

+5
-14
lines changed

src/machine/machine_nrf52xxx.go

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -203,16 +203,8 @@ func (a ADC) Get() uint16 {
203203
// SPI on the NRF.
204204
type SPI struct {
205205
Bus *nrf.SPIM_Type
206-
buf *[1]byte // 1-byte buffer for the Transfer method
207206
}
208207

209-
// There are 3 SPI interfaces on the NRF528xx.
210-
var (
211-
SPI0 = SPI{Bus: nrf.SPIM0, buf: new([1]byte)}
212-
SPI1 = SPI{Bus: nrf.SPIM1, buf: new([1]byte)}
213-
SPI2 = SPI{Bus: nrf.SPIM2, buf: new([1]byte)}
214-
)
215-
216208
// SPIConfig is used to store config info for SPI.
217209
type SPIConfig struct {
218210
Frequency uint32
@@ -224,7 +216,7 @@ type SPIConfig struct {
224216
}
225217

226218
// Configure is intended to setup the SPI interface.
227-
func (spi SPI) Configure(config SPIConfig) error {
219+
func (spi *SPI) Configure(config SPIConfig) error {
228220
// Disable bus to configure it
229221
spi.Bus.ENABLE.Set(nrf.SPIM_ENABLE_ENABLE_Disabled)
230222

@@ -276,10 +268,9 @@ func (spi SPI) Configure(config SPIConfig) error {
276268
}
277269

278270
// Transfer writes/reads a single byte using the SPI interface.
279-
func (spi SPI) Transfer(w byte) (byte, error) {
280-
buf := spi.buf[:]
281-
buf[0] = w
282-
err := spi.Tx(buf[:], buf[:])
271+
func (spi *SPI) Transfer(w byte) (byte, error) {
272+
buf := []byte{w}
273+
err := spi.Tx(buf, buf)
283274
return buf[0], err
284275
}
285276

@@ -288,7 +279,7 @@ func (spi SPI) Transfer(w byte) (byte, error) {
288279
// as bytes read. Therefore, if the number of bytes don't match it will be
289280
// padded until they fit: if len(w) > len(r) the extra bytes received will be
290281
// dropped and if len(w) < len(r) extra 0 bytes will be sent.
291-
func (spi SPI) Tx(w, r []byte) error {
282+
func (spi *SPI) Tx(w, r []byte) error {
292283
// Unfortunately the hardware (on the nrf52832) only supports up to 255
293284
// bytes in the buffers, so if either w or r is longer than that the
294285
// transfer needs to be broken up in pieces.

0 commit comments

Comments
 (0)