-
-
Notifications
You must be signed in to change notification settings - Fork 368
Expand file tree
/
Copy pathicm55686.h
More file actions
244 lines (197 loc) · 7.58 KB
/
icm55686.h
File metadata and controls
244 lines (197 loc) · 7.58 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
SlimeVR Code is placed under the MIT license
Copyright (c) 2026 Gorbit99 & SlimeVR Contributors
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#pragma once
#include <algorithm>
#include <array>
#include <cstdint>
#include "../../../sensorinterface/RegisterInterface.h"
#include "callbacks.h"
#include "vqf.h"
namespace SlimeVR::Sensors::SoftFusion::Drivers {
// Driver uses acceleration range at 32g
// and gyroscope range at 4000dps
// using high resolution mode
// Uses 32.768kHz clock
// Gyroscope ODR = 204.8Hz, accel ODR = 102.4Hz
// Timestamps reading not used, as they're useless (constant predefined increment)
struct ICM55686 {
static constexpr auto Name = "ICM-55686";
static constexpr auto Type = SensorTypeID::ICM55686;
static constexpr uint8_t Address = 0x68;
static constexpr float GyrTs = 1.0 / 204.8;
static constexpr float AccTs = 1.0 / 102.4;
static constexpr float TempTs = 1.0 / 409.6;
static constexpr float MagTs = 1.0 / 100;
static constexpr float GyroSensitivity = (1 << 19) / 4000.0f;
static constexpr float AccelSensitivity = (1 << 19) / 32.0f;
static constexpr float TemperatureBias = 25.0f;
static constexpr float TemperatureSensitivity = 128.0f;
static constexpr float TemperatureZROChange = 20.0f;
static constexpr VQFParams SensorVQFParams{};
RegisterInterface& m_RegisterInterface;
SlimeVR::Logging::Logger& m_Logger;
ICM55686(RegisterInterface& registerInterface, SlimeVR::Logging::Logger& logger)
: m_RegisterInterface{registerInterface}
, m_Logger{logger} {}
struct Regs {
static constexpr uint8_t TempData = 0x0c;
struct WhoAmI {
static constexpr uint8_t reg = 0x72;
static constexpr uint8_t value = 0x08;
};
struct DeviceConfig {
static constexpr uint8_t reg = 0x7f;
static constexpr uint8_t valueSwReset = 0b11;
};
struct GyroConfig {
static constexpr uint8_t reg = 0x20;
static constexpr uint8_t value
= (0b0000 << 4) | 0b1000; // 4000dps, odr=204.8Hz
};
struct AccelConfig {
static constexpr uint8_t reg = 0x1f;
static constexpr uint8_t value
= (0b000 << 4) | 0b1001; // 32g, odr = 102.4Hz
};
struct FifoConfig0 {
static constexpr uint8_t reg = 0x21;
static constexpr uint8_t value
= (0b10 << 6) | (0b001111); // stop on full mode, FIFO depth
// 8k bytes <-- this disables all APEX
// features, but we don't need them
};
struct FifoConfig3 {
static constexpr uint8_t reg = 0x25;
static constexpr uint8_t value = (0b1 << 0) | (0b1 << 1) | (0b1 << 2)
| (0b1 << 3); // enable FIFO,
// enable accel,
// enable gyro,
// enable hires mode
};
struct PwrMgmt0 {
static constexpr uint8_t reg = 0x14;
static constexpr uint8_t value
= (0b11 << 0)
| (0b11 << 2); // accel in low noise mode, gyro in low noise
};
struct IOCPADScenarioOvrd {
static constexpr uint8_t reg = 0x35;
static constexpr uint8_t value = 0b00000110; // pin 9 to clkin
};
struct RtcConfig {
static constexpr uint8_t reg = 0x2a;
static constexpr uint8_t value = 0b00100011; // enable RTC
};
static constexpr uint8_t FifoCount = 0x16;
static constexpr uint8_t FifoData = 0x18;
};
#pragma pack(push, 1)
struct FifoEntryAligned {
uint16_t accel[3];
uint16_t gyro[3];
uint16_t temp;
uint16_t timestamp;
uint8_t lsb[3];
};
#pragma pack(pop)
static constexpr size_t FullFifoEntrySize = sizeof(FifoEntryAligned) + 1;
static_assert(FullFifoEntrySize == 20);
bool initialize() {
m_RegisterInterface.writeReg(
Regs::DeviceConfig::reg,
Regs::DeviceConfig::valueSwReset
);
delay(35);
#if IMU_USE_EXTERNAL_CLOCK
m_RegisterInterface.writeReg(
Regs::IOCPADScenarioOvrd::reg,
Regs::IOCPADScenarioOvrd::value
);
m_RegisterInterface.writeReg(Regs::RtcConfig::reg, Regs::RtcConfig::value);
#endif
m_RegisterInterface.writeReg(Regs::GyroConfig::reg, Regs::GyroConfig::value);
m_RegisterInterface.writeReg(Regs::AccelConfig::reg, Regs::AccelConfig::value);
m_RegisterInterface.writeReg(Regs::FifoConfig0::reg, Regs::FifoConfig0::value);
m_RegisterInterface.writeReg(Regs::FifoConfig3::reg, Regs::FifoConfig3::value);
m_RegisterInterface.writeReg(Regs::PwrMgmt0::reg, Regs::PwrMgmt0::value);
read_buffer.resize(FullFifoEntrySize * MaxReadings);
delay(1);
return true;
}
static constexpr size_t MaxReadings = 8;
// Allocate on heap so that it does not take up stack space, which can result in
// stack overflow and panic
std::vector<uint8_t> read_buffer;
bool bulkRead(DriverCallbacks<int32_t>&& callbacks) {
constexpr auto InvalidReading = static_cast<int16_t>(0x8000);
size_t fifo_packets
= __builtin_bswap16(m_RegisterInterface.readReg16(Regs::FifoCount));
auto packets_to_read = std::min(fifo_packets, MaxReadings);
size_t bytes_to_read = packets_to_read * FullFifoEntrySize;
m_RegisterInterface
.readBytes(Regs::FifoData, bytes_to_read, read_buffer.data());
for (auto i = 0u; i < bytes_to_read; i += FullFifoEntrySize) {
uint8_t header = read_buffer[i];
bool has_gyro = header & (1 << 5);
bool has_accel = header & (1 << 6);
FifoEntryAligned entry;
memcpy(
&entry,
&read_buffer[i + 0x1],
sizeof(FifoEntryAligned)
); // skip fifo header
int16_t gyro[3] = {
static_cast<int16_t>(__builtin_bswap16(entry.gyro[0])),
static_cast<int16_t>(__builtin_bswap16(entry.gyro[1])),
static_cast<int16_t>(__builtin_bswap16(entry.gyro[2])),
};
if (has_gyro && gyro[0] != InvalidReading) {
const int32_t gyroData[3]{
static_cast<int32_t>(gyro[0]) << 4 | (entry.lsb[0] & 0xf),
static_cast<int32_t>(gyro[1]) << 4 | (entry.lsb[1] & 0xf),
static_cast<int32_t>(gyro[2]) << 4 | (entry.lsb[2] & 0xf),
};
callbacks.processGyroSample(gyroData, GyrTs);
}
int16_t accel[3] = {
static_cast<int16_t>(__builtin_bswap16(entry.accel[0])),
static_cast<int16_t>(__builtin_bswap16(entry.accel[1])),
static_cast<int16_t>(__builtin_bswap16(entry.accel[2])),
};
if (has_accel && accel[0] != InvalidReading) {
const int32_t accelData[3]{
static_cast<int32_t>(accel[0]) << 4
| (static_cast<int32_t>((entry.lsb[0]) & 0xf0) >> 4),
static_cast<int32_t>(accel[1]) << 4
| (static_cast<int32_t>((entry.lsb[1]) & 0xf0) >> 4),
static_cast<int32_t>(accel[2]) << 4
| (static_cast<int32_t>((entry.lsb[2]) & 0xf0) >> 4),
};
callbacks.processAccelSample(accelData, AccTs);
}
auto temp = static_cast<int16_t>(__builtin_bswap16(entry.temp));
if (temp != InvalidReading) {
callbacks.processTempSample(temp, TempTs);
}
}
return fifo_packets > MaxReadings;
}
};
} // namespace SlimeVR::Sensors::SoftFusion::Drivers