-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathbmi160.go
220 lines (197 loc) · 7.13 KB
/
bmi160.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package bmi160
import (
"time"
"tinygo.org/x/drivers"
)
// DeviceSPI is the SPI interface to a BMI160 accelerometer/gyroscope. There is
// also an I2C interface, but it is not yet supported.
type DeviceSPI struct {
// Chip select pin
CSB drivers.Pin
buf [7]byte
// SPI bus (requires chip select to be usable).
Bus drivers.SPI
}
// NewSPI returns a new device driver. The pin and SPI interface are not
// touched, provide a fully configured SPI object and call Configure to start
// using this device.
func NewSPI(csb drivers.Pin, spi drivers.SPI) *DeviceSPI {
return &DeviceSPI{
CSB: csb, // chip select
Bus: spi,
}
}
// Configure configures the BMI160 for use. The CSB pin anf the SPI interface
// should be configured already. This function configures the BMI160 only.
func (d *DeviceSPI) Configure() error {
d.CSB.High()
// The datasheet recommends doing a register read from address 0x7F to get
// SPI communication going:
// > If CSB sees a rising edge after power-up, the BMI160 interface switches
// > to SPI until a reset or the next power-up occurs. Therefore, a CSB
// > rising edge is needed before starting the SPI communication. Hence, it
// > is recommended to perform a SPI single read access to the ADDRESS 0x7F
// > before the actual communication in order to use the SPI interface.
d.readRegister(0x7F)
// Power up the accelerometer. 0b0001_00nn is the command format, with 0b01
// indicating normal mode.
d.runCommand(0b0001_0001)
// Power up the gyroscope. 0b0001_01nn is the command format, with 0b01
// indicating normal mode.
d.runCommand(0b0001_0101)
// Wait until the device is fully initialized. Even after the command has
// finished, the gyroscope may not be fully powered on. Therefore, wait
// until we get an expected value.
// This takes 30ms or so.
for {
// Wait for the acc_pmu_status and gyr_pmu_status to both be 0b01.
if d.readRegister(reg_PMU_STATUS) == 0b0001_0100 {
break
}
}
return nil
}
// Connected check whether the device appears to be properly connected. It reads
// the CHIPID, which must be 0xD1 for the BMI160.
func (d *DeviceSPI) Connected() bool {
return d.readRegister(reg_CHIPID) == 0xD1
}
// Reset restores the device to the state after power up. This can be useful to
// easily disable the accelerometer and gyroscope to reduce current consumption.
func (d *DeviceSPI) Reset() error {
d.runCommand(0xB6) // softreset
return nil
}
// ReadTemperature returns the temperature in celsius milli degrees (°C/1000).
func (d *DeviceSPI) ReadTemperature() (temperature int32, err error) {
data := d.buf[:3]
data[0] = 0x80 | reg_TEMPERATURE_0
data[1] = 0
data[2] = 0
d.CSB.Low()
err = d.Bus.Tx(data, data)
d.CSB.High()
if err != nil {
return
}
rawTemperature := int16(uint16(data[1]) | uint16(data[2])<<8)
// 0x0000 is 23°C
// 0x7fff is ~87°C
// We use 0x8000 instead of 0x7fff to make the formula easier. The result
// should be near identical and shouldn't affect the result too much (the
// temperature sensor has an offset of around 2°C so isn't very reliable).
// So the formula is as follows:
// 1. Scale from 0x0000..0x8000 to 0..(87-23).
// rawTemperature * (87-23) / 0x8000
// 2. Convert to centidegrees.
// rawTemperature * 1000 * (87-23) / 0x8000
// 3. Add 23°C offset.
// rawTemperature * 1000 * (87-23) / 0x8000 + 23000
// 4. Simplify.
// rawTemperature * 1000 * 64 / 0x8000 + 23000
// rawTemperature * 64000 / 0x8000 + 23000
// rawTemperature * 125 / 64 + 23000
temperature = int32(rawTemperature)*125/64 + 23000
return
}
// ReadAcceleration reads the current acceleration from the device and returns
// it in µg (micro-gravity). When one of the axes is pointing straight to Earth
// and the sensor is not moving the returned value will be around 1000000 or
// -1000000.
func (d *DeviceSPI) ReadAcceleration() (x int32, y int32, z int32, err error) {
data := d.buf[:7]
data[0] = 0x80 | reg_ACC_XL
for i := 1; i < len(data); i++ {
data[i] = 0
}
d.CSB.Low()
err = d.Bus.Tx(data, data)
d.CSB.High()
if err != nil {
return
}
// Now do two things:
// 1. merge the two values to a 16-bit number (and cast to a 32-bit integer)
// 2. scale the value to bring it in the -1000000..1000000 range.
// This is done with a trick. What we do here is essentially multiply by
// 1000000 and divide by 16384 to get the original scale, but to avoid
// overflow we do it at 1/64 of the value:
// 1000000 / 64 = 15625
// 16384 / 64 = 256
x = int32(int16(uint16(data[1])|uint16(data[2])<<8)) * 15625 / 256
y = int32(int16(uint16(data[3])|uint16(data[4])<<8)) * 15625 / 256
z = int32(int16(uint16(data[5])|uint16(data[6])<<8)) * 15625 / 256
return
}
// ReadRotation reads the current rotation from the device and returns it in
// µ°/s (micro-degrees/sec). This means that if you were to do a complete
// rotation along one axis and while doing so integrate all values over time,
// you would get a value close to 360000000.
func (d *DeviceSPI) ReadRotation() (x int32, y int32, z int32, err error) {
data := d.buf[:7]
data[0] = 0x80 | reg_GYR_XL
for i := 1; i < len(data); i++ {
data[i] = 0
}
d.CSB.Low()
err = d.Bus.Tx(data, data)
d.CSB.High()
if err != nil {
return
}
// First the value is converted from a pair of bytes to a signed 16-bit
// value and then to a signed 32-bit value to avoid integer overflow.
// Then the value is scaled to µ°/s (micro-degrees per second).
// The default is 2000°/s full scale range for -32768..32767.
// The formula works as follows (taking X as an example):
// 1. Scale from 32768 to 2000. This means that it is in °/s units.
// rawX * 2000 / 32768
// 2. Scale to µ°/s by multiplying by 1e6.
// rawX * 1e6 * 2000 / 32768
// 3. Simplify.
// rawX * 2e9 / 32768
// rawX * 1953125 / 32
rawX := int32(int16(uint16(data[1]) | uint16(data[2])<<8))
rawY := int32(int16(uint16(data[3]) | uint16(data[4])<<8))
rawZ := int32(int16(uint16(data[5]) | uint16(data[6])<<8))
x = int32(int64(rawX) * 1953125 / 32)
y = int32(int64(rawY) * 1953125 / 32)
z = int32(int64(rawZ) * 1953125 / 32)
return
}
// runCommand runs a BMI160 command through the CMD register. It waits for the
// command to complete before returning.
func (d *DeviceSPI) runCommand(command uint8) {
d.writeRegister(reg_CMD, command)
for {
response := d.readRegister(reg_CMD)
if response == 0 {
return // command was completed
}
}
}
// readRegister reads from a single BMI160 register. It should only be used for
// single register reads, not for reading multiple registers at once.
func (d *DeviceSPI) readRegister(address uint8) uint8 {
// I don't know why but it appears necessary to sleep for a bit here.
time.Sleep(time.Millisecond)
data := d.buf[:2]
data[0] = 0x80 | address
data[1] = 0
d.CSB.Low()
d.Bus.Tx(data, data)
d.CSB.High()
return data[1]
}
// writeRegister writes a single byte BMI160 register. It should only be used
// for writing to a single register.
func (d *DeviceSPI) writeRegister(address, data uint8) {
// I don't know why but it appears necessary to sleep for a bit here.
time.Sleep(time.Millisecond)
buf := d.buf[:2]
buf[0] = address
buf[1] = data
d.CSB.Low()
d.Bus.Tx(buf, buf)
d.CSB.High()
}