Skip to content

Commit 5ecefde

Browse files
aykevldeadprogram
authored andcommitted
bmi160: avoid heap allocations
The nrf52 series chips use DMA for writing and reading SPI data, which means that the transmit and receive buffer slices escape. This can easily be solved by pre-allocating the buffer in the BMI160 driver and reusing this buffer every time.
1 parent 9adbde9 commit 5ecefde

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

bmi160/bmi160.go

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ type DeviceSPI struct {
1313
// Chip select pin
1414
CSB machine.Pin
1515

16+
buf [7]byte
17+
1618
// SPI bus (requires chip select to be usable).
1719
Bus drivers.SPI
1820
}
@@ -80,7 +82,10 @@ func (d *DeviceSPI) Reset() error {
8082

8183
// ReadTemperature returns the temperature in celsius milli degrees (°C/1000).
8284
func (d *DeviceSPI) ReadTemperature() (temperature int32, err error) {
83-
data := []byte{0x80 | reg_TEMPERATURE_0, 0, 0}
85+
data := d.buf[:3]
86+
data[0] = 0x80 | reg_TEMPERATURE_0
87+
data[1] = 0
88+
data[2] = 0
8489
d.CSB.Low()
8590
err = d.Bus.Tx(data, data)
8691
d.CSB.High()
@@ -113,7 +118,11 @@ func (d *DeviceSPI) ReadTemperature() (temperature int32, err error) {
113118
// and the sensor is not moving the returned value will be around 1000000 or
114119
// -1000000.
115120
func (d *DeviceSPI) ReadAcceleration() (x int32, y int32, z int32, err error) {
116-
data := []byte{0x80 | reg_ACC_XL, 0, 0, 0, 0, 0, 0}
121+
data := d.buf[:7]
122+
data[0] = 0x80 | reg_ACC_XL
123+
for i := 1; i < len(data); i++ {
124+
data[i] = 0
125+
}
117126
d.CSB.Low()
118127
err = d.Bus.Tx(data, data)
119128
d.CSB.High()
@@ -139,7 +148,11 @@ func (d *DeviceSPI) ReadAcceleration() (x int32, y int32, z int32, err error) {
139148
// rotation along one axis and while doing so integrate all values over time,
140149
// you would get a value close to 360000000.
141150
func (d *DeviceSPI) ReadRotation() (x int32, y int32, z int32, err error) {
142-
data := []byte{0x80 | reg_GYR_XL, 0, 0, 0, 0, 0, 0}
151+
data := d.buf[:7]
152+
data[0] = 0x80 | reg_GYR_XL
153+
for i := 1; i < len(data); i++ {
154+
data[i] = 0
155+
}
143156
d.CSB.Low()
144157
err = d.Bus.Tx(data, data)
145158
d.CSB.High()
@@ -185,7 +198,9 @@ func (d *DeviceSPI) readRegister(address uint8) uint8 {
185198
// I don't know why but it appears necessary to sleep for a bit here.
186199
time.Sleep(time.Millisecond)
187200

188-
data := []byte{0x80 | address, 0}
201+
data := d.buf[:2]
202+
data[0] = 0x80 | address
203+
data[1] = 0
189204
d.CSB.Low()
190205
d.Bus.Tx(data, data)
191206
d.CSB.High()
@@ -198,7 +213,11 @@ func (d *DeviceSPI) writeRegister(address, data uint8) {
198213
// I don't know why but it appears necessary to sleep for a bit here.
199214
time.Sleep(time.Millisecond)
200215

216+
buf := d.buf[:2]
217+
buf[0] = address
218+
buf[1] = data
219+
201220
d.CSB.Low()
202-
d.Bus.Tx([]byte{address, data}, []byte{0, 0})
221+
d.Bus.Tx(buf, buf)
203222
d.CSB.High()
204223
}

0 commit comments

Comments
 (0)