nrf: don't trigger a heap allocation in SPI.Transfer

By using a 1-byte buffer, two heap allocations each `SPI.Transfer` call
can be avoided.
Этот коммит содержится в:
Ayke van Laethem 2021-05-30 18:11:51 +02:00 коммит произвёл Ron Evans
родитель 8b79e82686
коммит e8c4c4a865

Просмотреть файл

@ -115,13 +115,14 @@ func (a ADC) Get() uint16 {
// SPI on the NRF. // SPI on the NRF.
type SPI struct { type SPI struct {
Bus *nrf.SPIM_Type Bus *nrf.SPIM_Type
buf *[1]byte // 1-byte buffer for the Transfer method
} }
// There are 3 SPI interfaces on the NRF528xx. // There are 3 SPI interfaces on the NRF528xx.
var ( var (
SPI0 = SPI{Bus: nrf.SPIM0} SPI0 = SPI{Bus: nrf.SPIM0, buf: new([1]byte)}
SPI1 = SPI{Bus: nrf.SPIM1} SPI1 = SPI{Bus: nrf.SPIM1, buf: new([1]byte)}
SPI2 = SPI{Bus: nrf.SPIM2} SPI2 = SPI{Bus: nrf.SPIM2, buf: new([1]byte)}
) )
// SPIConfig is used to store config info for SPI. // SPIConfig is used to store config info for SPI.
@ -207,10 +208,10 @@ func (spi SPI) Configure(config SPIConfig) {
// Transfer writes/reads a single byte using the SPI interface. // Transfer writes/reads a single byte using the SPI interface.
func (spi SPI) Transfer(w byte) (byte, error) { func (spi SPI) Transfer(w byte) (byte, error) {
var wbuf, rbuf [1]byte buf := spi.buf[:]
wbuf[0] = w buf[0] = w
err := spi.Tx(wbuf[:], rbuf[:]) err := spi.Tx(buf[:], buf[:])
return rbuf[0], err return buf[0], err
} }
// Tx handles read/write operation for SPI interface. Since SPI is a syncronous // Tx handles read/write operation for SPI interface. Since SPI is a syncronous