blob: 1612ee88e3878c667d05c25ebd4406d40b305372 [file] [log] [blame]
Brian Silverman7be68ba2020-01-08 22:08:40 -08001#include "frc971/wpilib/ADIS16470.h"
2
Tyler Chatowbf0609c2021-07-31 16:13:27 -07003#include <cinttypes>
Brian Silverman7be68ba2020-01-08 22:08:40 -08004
Philipp Schrader790cb542023-07-05 21:06:52 -07005#include "glog/logging.h"
6
Austin Schuhac17fba2020-03-28 15:55:33 -07007#include "aos/containers/sized_array.h"
Brian Silverman7be68ba2020-01-08 22:08:40 -08008#include "aos/time/time.h"
9#include "hal/HAL.h"
10
11namespace frc971 {
12namespace wpilib {
13namespace {
Austin Schuhb51692d2021-04-10 15:27:25 -070014namespace chrono = std::chrono;
Brian Silverman7be68ba2020-01-08 22:08:40 -080015namespace registers {
16
17// Flash memory write count
18constexpr uint8_t FLASH_CNT = 0x00;
19// Diagnostic and operational status
20constexpr uint8_t DIAG_STAT = 0x02;
21// X-axis gyroscope output, lower word
22constexpr uint8_t X_GYRO_LOW = 0x04;
23// X-axis gyroscope output, upper word
24constexpr uint8_t X_GYRO_OUT = 0x06;
25// Y-axis gyroscope output, lower word
26constexpr uint8_t Y_GYRO_LOW = 0x08;
27// Y-axis gyroscope output, upper word
28constexpr uint8_t Y_GYRO_OUT = 0x0A;
29// Z-axis gyroscope output, lower word
30constexpr uint8_t Z_GYRO_LOW = 0x0C;
31// Z-axis gyroscope output, upper word
32constexpr uint8_t Z_GYRO_OUT = 0x0E;
33// X-axis accelerometer output, lower word
34constexpr uint8_t X_ACCL_LOW = 0x10;
35// X-axis accelerometer output, upper word
36constexpr uint8_t X_ACCL_OUT = 0x12;
37// Y-axis accelerometer output, lower word
38constexpr uint8_t Y_ACCL_OUT = 0x16;
39// Y-axis accelerometer output, upper word
40constexpr uint8_t Z_ACCL_LOW = 0x18;
41// Z-axis accelerometer output, lower word
42constexpr uint8_t Z_ACCL_OUT = 0x1A;
43// Z-axis accelerometer output, upper word
44constexpr uint8_t TEMP_OUT = 0x1C;
45// Temperature output (internal, not calibrated)
46constexpr uint8_t TIME_STAMP = 0x1E;
47// PPS mode time stamp
48constexpr uint8_t X_DELTANG_LOW = 0x24;
49// X-axis delta angle output, lower word
50constexpr uint8_t X_DELTANG_OUT = 0x26;
51// X-axis delta angle output, upper word
52constexpr uint8_t Y_DELTANG_LOW = 0x28;
53// Y-axis delta angle output, lower word
54constexpr uint8_t Y_DELTANG_OUT = 0x2A;
55// Y-axis delta angle output, upper word
56constexpr uint8_t Z_DELTANG_LOW = 0x2C;
57// Z-axis delta angle output, lower word
58constexpr uint8_t Z_DELTANG_OUT = 0x2E;
59// Z-axis delta angle output, upper word
60constexpr uint8_t X_DELTVEL_LOW = 0x30;
61// X-axis delta velocity output, lower word
62constexpr uint8_t X_DELTVEL_OUT = 0x32;
63// X-axis delta velocity output, upper word
64constexpr uint8_t Y_DELTVEL_LOW = 0x34;
65// Y-axis delta velocity output, lower word
66constexpr uint8_t Y_DELTVEL_OUT = 0x36;
67// Y-axis delta velocity output, upper word
68constexpr uint8_t Z_DELTVEL_LOW = 0x38;
69// Z-axis delta velocity output, lower word
70constexpr uint8_t Z_DELTVEL_OUT = 0x3A;
71// Z-axis delta velocity output, upper word
72constexpr uint8_t XG_BIAS_LOW = 0x40;
73// X-axis gyroscope bias offset correction, lower word
74constexpr uint8_t XG_BIAS_HIGH = 0x42;
75// X-axis gyroscope bias offset correction, upper word
76constexpr uint8_t YG_BIAS_LOW = 0x44;
77// Y-axis gyroscope bias offset correction, lower word
78constexpr uint8_t YG_BIAS_HIGH = 0x46;
79// Y-axis gyroscope bias offset correction, upper word
80constexpr uint8_t ZG_BIAS_LOW = 0x48;
81// Z-axis gyroscope bias offset correction, lower word
82constexpr uint8_t ZG_BIAS_HIGH = 0x4A;
83// Z-axis gyroscope bias offset correction, upper word
84constexpr uint8_t XA_BIAS_LOW = 0x4C;
85// X-axis accelerometer bias offset correction, lower word
86constexpr uint8_t XA_BIAS_HIGH = 0x4E;
87// X-axis accelerometer bias offset correction, upper word
88constexpr uint8_t YA_BIAS_LOW = 0x50;
89// Y-axis accelerometer bias offset correction, lower word
90constexpr uint8_t YA_BIAS_HIGH = 0x52;
91// Y-axis accelerometer bias offset correction, upper word
92constexpr uint8_t ZA_BIAS_LOW = 0x54;
93// Z-axis accelerometer bias offset correction, lower word
94constexpr uint8_t ZA_BIAS_HIGH = 0x56;
95// Z-axis accelerometer bias offset correction, upper word
96constexpr uint8_t FILT_CTRL = 0x5C;
97// Filter control
98constexpr uint8_t MSC_CTRL = 0x60;
99// Miscellaneous control
100constexpr uint8_t UP_SCALE = 0x62;
101// Clock scale factor, PPS mode
102constexpr uint8_t DEC_RATE = 0x64;
103// Decimation rate control (output data rate)
104constexpr uint8_t NULL_CNFG = 0x66;
105// Auto-null configuration control
106constexpr uint8_t GLOB_CMD = 0x68;
107// Global commands
108constexpr uint8_t FIRM_REV = 0x6C;
109// Firmware revision
110constexpr uint8_t FIRM_DM = 0x6E;
111// Firmware revision date, month and day
112constexpr uint8_t FIRM_Y = 0x70;
113// Firmware revision date, year
114constexpr uint8_t PROD_ID = 0x72;
115// Product identification
116constexpr uint8_t SERIAL_NUM = 0x74;
117// Serial number (relative to assembly lot)
118constexpr uint8_t USER_SCR1 = 0x76;
119// User scratch register 1
120constexpr uint8_t USER_SCR2 = 0x78;
121// User scratch register 2
122constexpr uint8_t USER_SCR3 = 0x7A;
123// User scratch register 3
124constexpr uint8_t FLSHCNT_LOW = 0x7C;
125// Flash update count, lower word
126constexpr uint8_t FLSHCNT_HIGH = 0x7E;
127// Flash update count, upper word
128constexpr uint8_t Y_ACCL_LOW = 0x14;
129
130} // namespace registers
131
132// The complete automatic packet we will send. This needs to include the dummy 0
133// bytes making up full 16-bit frames.
134// Note that in addition to the 24-byte limit from the FPGA, this is also
135// limited to 12 16-bit register reads by the IMU itself given that we're
136// reading at the full 2kHz rate.
137// We rotate the registers here by 1, such that the first thing we read is the
138// last thing triggered by the previous reading. We put DIAG_STAT in this
139// position because we don't care if it's one cycle stale.
140constexpr uint8_t kAutospiPacket[] = {
141 // X
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700142 registers::X_GYRO_OUT,
143 0,
144 registers::X_ACCL_OUT,
145 0,
146 registers::X_ACCL_LOW,
147 0,
Brian Silverman7be68ba2020-01-08 22:08:40 -0800148 // Y
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700149 registers::Y_GYRO_OUT,
150 0,
151 registers::Y_ACCL_OUT,
152 0,
153 registers::Y_ACCL_LOW,
154 0,
Brian Silverman7be68ba2020-01-08 22:08:40 -0800155 // Z
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700156 registers::Z_GYRO_OUT,
157 0,
158 registers::Z_ACCL_OUT,
159 0,
160 registers::Z_ACCL_LOW,
161 0,
Brian Silverman7be68ba2020-01-08 22:08:40 -0800162 // Other
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700163 registers::TEMP_OUT,
164 0,
165 registers::DIAG_STAT,
166 0,
Brian Silverman7be68ba2020-01-08 22:08:40 -0800167};
168// clang-format on
169
170static_assert((sizeof(kAutospiPacket) % 2) == 0,
171 "Need a whole number of register reads");
172
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700173static constexpr size_t kAutospiDataSize =
174 sizeof(kAutospiPacket) + 1 /* timestamp */;
Brian Silverman7be68ba2020-01-08 22:08:40 -0800175
176// radian/second/LSB for the gyros (for just the 16-bit value).
177constexpr double kGyroLsbRadianSecond =
178 1.0 / 10.0 * (2.0 * M_PI / 360.0) /* degrees -> radians */;
179// G/LSB for the accelerometers (for the full 32-bit value).
James Kuszmaulb145b322020-01-22 22:31:52 -0800180constexpr double kAccelerometerLsbG = 1.0 / 52'428'800.0;
Brian Silverman7be68ba2020-01-08 22:08:40 -0800181// C/LSB for the temperature.
182constexpr double kTemperatureLsbDegree = 0.1;
183
184// This is what the datasheet says PROD_ID should be.
185constexpr uint16_t kExpectedProductId = 0x4056;
186// This is the PROD_ID we observe.
187constexpr uint16_t kObservedProductId = 0x4256;
188
189} // namespace
190
191ADIS16470::ADIS16470(aos::EventLoop *event_loop, frc::SPI *spi,
192 frc::DigitalInput *data_ready, frc::DigitalOutput *reset)
193 : event_loop_(event_loop),
194 imu_values_sender_(
Austin Schuhac17fba2020-03-28 15:55:33 -0700195 event_loop_->MakeSender<::frc971::IMUValuesBatch>("/drivetrain")),
Brian Silverman7be68ba2020-01-08 22:08:40 -0800196 initialize_timer_(
197 event_loop_->AddTimer([this]() { DoInitializeStep(); })),
198 spi_(spi),
199 data_ready_(data_ready),
200 reset_(reset) {
201 // Rather than put the entire data packet into the header, just put a size
202 // there and verify it matches here.
203 CHECK_EQ(kAutospiDataSize, read_data_.size());
204
205 // We're not doing burst mode, so this is the IMU's rated speed.
206 spi_->SetClockRate(2'000'000);
207 spi_->SetChipSelectActiveLow();
James Kuszmaul9776b392023-01-14 14:08:08 -0800208 spi_->SetMode(frc::SPI::Mode::kMode3);
Brian Silverman7be68ba2020-01-08 22:08:40 -0800209
210 // NI's SPI driver defaults to SCHED_OTHER. Find it's PID with ps, and change
211 // it to a RT priority of 33.
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700212 PCHECK(system("busybox ps -ef | grep '\\[spi0\\]' | awk '{print $1}' | xargs "
213 "chrt -f -p "
214 "33") == 0);
215 PCHECK(system("busybox ps -ef | grep '\\[spi1\\]' | awk '{print $1}' | xargs "
216 "chrt -f -p "
217 "33") == 0);
Brian Silverman7be68ba2020-01-08 22:08:40 -0800218
219 event_loop_->OnRun([this]() { BeginInitialization(); });
220}
221
222void ADIS16470::DoReads() {
223 if (state_ != State::kRunning) {
224 // Not sure how to interpret data received now, so ignore it.
225 return;
226 }
227
Austin Schuhac17fba2020-03-28 15:55:33 -0700228 auto builder = imu_values_sender_.MakeBuilder();
229
Brian Silverman7be68ba2020-01-08 22:08:40 -0800230 int amount_to_read =
231 spi_->ReadAutoReceivedData(to_read_.data(), 0, 0 /* don't block */);
Austin Schuhac17fba2020-03-28 15:55:33 -0700232
233 aos::SizedArray<flatbuffers::Offset<IMUValues>, 50> readings_offsets;
Brian Silverman7be68ba2020-01-08 22:08:40 -0800234 while (true) {
235 if (amount_to_read == 0) break;
236 CHECK(!to_read_.empty());
237 const int amount_read_now = std::min<int>(amount_to_read, to_read_.size());
238 CHECK_GT(amount_read_now, 0) << "amount_to_read: " << amount_to_read
239 << ", to_read_.size(): " << to_read_.size();
240 spi_->ReadAutoReceivedData(to_read_.data(), amount_read_now,
241 0 /* don't block */);
242 to_read_ = to_read_.subspan(amount_read_now);
243 amount_to_read -= amount_read_now;
244
245 if (to_read_.empty()) {
Austin Schuhac17fba2020-03-28 15:55:33 -0700246 flatbuffers::Offset<IMUValues> reading_offset =
247 ProcessReading(builder.fbb());
248 readings_offsets.push_back(reading_offset);
Brian Silverman7be68ba2020-01-08 22:08:40 -0800249
250 // Reset for the next reading.
251 to_read_ = absl::MakeSpan(read_data_);
252 } else {
253 CHECK_EQ(amount_to_read, 0);
254 break;
255 }
256 }
Austin Schuhac17fba2020-03-28 15:55:33 -0700257
258 flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<IMUValues>>>
259 readings_offset = builder.fbb()->CreateVector(readings_offsets.data(),
260 readings_offsets.size());
261
262 IMUValuesBatch::Builder imu_values_batch_builder =
263 builder.MakeBuilder<IMUValuesBatch>();
264 imu_values_batch_builder.add_readings(readings_offset);
milind1f1dca32021-07-03 13:50:07 -0700265 builder.CheckOk(builder.Send(imu_values_batch_builder.Finish()));
Brian Silverman7be68ba2020-01-08 22:08:40 -0800266}
267
268void ADIS16470::DoInitializeStep() {
269 switch (state_) {
270 case State::kUninitialized: {
271 to_read_ = absl::MakeSpan(read_data_);
272
273 // First, set the SPI to normal mode so it stops trying to talk
274 // automatically.
275 spi_->StopAuto();
276
277 reset_->Set(false);
278 // Datasheet says it needs a 1 us pulse, so make sure we do something in
279 // between asserting and deasserting.
Austin Schuhb51692d2021-04-10 15:27:25 -0700280 std::this_thread::sleep_for(chrono::milliseconds(1));
Brian Silverman7be68ba2020-01-08 22:08:40 -0800281 reset_->Set(true);
282
283 state_ = State::kWaitForReset;
284 // Datasheet says it takes 193 ms to come out of reset, so give it some
285 // margin on top of that.
Philipp Schradera6712522023-07-05 20:25:11 -0700286 initialize_timer_->Schedule(event_loop_->monotonic_now() +
287 chrono::milliseconds(250));
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700288 } break;
Brian Silverman7be68ba2020-01-08 22:08:40 -0800289
290 case State::kWaitForReset: {
291 flatbuffers::Offset<ADIS16470DiagStat> start_diag_stat;
292 flatbuffers::Offset<ADIS16470DiagStat> self_test_diag_stat;
293 bool success = false;
294 auto builder = imu_values_sender_.MakeBuilder();
295
296 // Configure the IMU the way we want it.
297 const uint16_t product_id = ReadRegister(registers::PROD_ID, 0);
298 if (product_id == kExpectedProductId ||
299 product_id == kObservedProductId) {
300 const uint16_t start_diag_stat_value =
301 ReadRegister(registers::DIAG_STAT, 0);
302 start_diag_stat = PackDiagStat(builder.fbb(), start_diag_stat_value);
303 if (!DiagStatHasError(
304 *GetTemporaryPointer(*builder.fbb(), start_diag_stat))) {
305 WriteRegister(registers::FILT_CTRL, 0 /* no filtering */);
306 WriteRegister(
307 registers::MSC_CTRL,
308 (1 << 7) /* enable gyro linear g compensation */ |
309 (1 << 6) /* enable point of percussion alignment */ |
310 (0 << 2) /* internal clock mode */ |
311 (0 << 1) /* sync polarity, doesn't matter */ |
312 (1 << 0) /* data ready is active high */);
James Kuszmaul26dbd0d2020-09-28 20:08:08 -0700313 // Rate of the output will be 2000 / (DEC_RATE + 1) Hz.
Brian Silverman7be68ba2020-01-08 22:08:40 -0800314 WriteRegister(registers::DEC_RATE,
James Kuszmaul26dbd0d2020-09-28 20:08:08 -0700315 1 /* Average every pair of values. */);
Brian Silverman7be68ba2020-01-08 22:08:40 -0800316
317 // Start a sensor self test.
318 WriteRegister(registers::GLOB_CMD, 1 << 2);
319 // Datasheet says it takes 14ms, so give it some margin.
Austin Schuhb51692d2021-04-10 15:27:25 -0700320 std::this_thread::sleep_for(chrono::milliseconds(25));
Brian Silverman7be68ba2020-01-08 22:08:40 -0800321 // Read DIAG_STAT again, and queue up a read of the first part of the
322 // autospi data packet.
323 const uint16_t self_test_diag_stat_value =
324 ReadRegister(registers::DIAG_STAT, kAutospiPacket[0]);
325 self_test_diag_stat =
326 PackDiagStat(builder.fbb(), self_test_diag_stat_value);
327 if (!DiagStatHasError(
328 *GetTemporaryPointer(*builder.fbb(), self_test_diag_stat))) {
329 // Initialize automatic mode, but don't start it yet.
330 spi_->InitAuto(kAutospiDataSize * 100);
331 spi_->SetAutoTransmitData(kAutospiPacket,
332 0 /* no extra 0s at the end */);
333 // No idea what units the "stall period" is in. This value is just
334 // bigger than the 16us min from the datasheet. It does not appear
335 // to scale with SPICLK frequency. Empirically, this value comes out
336 // to 16.7us.
337 spi_->ConfigureAutoStall(
Brian Silverman7be68ba2020-01-08 22:08:40 -0800338 0 /* the minimum CS delay is enough for this IMU */, 670,
339 1 /* toggle CS every 2 8-bit bytes */);
340
341 // Read any data queued up by the FPGA.
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700342 while (true) {
Brian Silverman7be68ba2020-01-08 22:08:40 -0800343 uint32_t buffer;
344 if (spi_->ReadAutoReceivedData(&buffer, 1, 0 /* don't block */) ==
345 0) {
346 break;
347 }
348 }
349
350 // Finally, enable automatic mode so it starts reading data.
351 spi_->StartAutoTrigger(*data_ready_, true, false);
Austin Schuhb51692d2021-04-10 15:27:25 -0700352
353 // We need a bit of time for the auto trigger to start up so we have
354 // something to throw out. 1 khz trigger, so 2 ms gives us 2 cycles
355 // to hit it worst case.
356 std::this_thread::sleep_for(chrono::milliseconds(2));
357
358 // Throw out the first sample. It is almost always faulted due to
359 // how we start up, and it isn't worth tracking for downstream users
360 // to look at.
361 to_read_ = absl::MakeSpan(read_data_);
362 CHECK_EQ(spi_->ReadAutoReceivedData(
363 to_read_.data(), to_read_.size(),
364 1000.0 /* block for up to 1 second */),
365 static_cast<int>(to_read_.size()))
366 << ": Failed to read first sample.";
Brian Silverman7be68ba2020-01-08 22:08:40 -0800367 success = true;
368 }
369 }
370 }
371
372 IMUValues::Builder imu_builder = builder.MakeBuilder<IMUValues>();
373 imu_builder.add_product_id(product_id);
374 if (!start_diag_stat.IsNull()) {
375 imu_builder.add_start_diag_stat(start_diag_stat);
376 }
377 if (!self_test_diag_stat.IsNull()) {
378 imu_builder.add_self_test_diag_stat(self_test_diag_stat);
379 }
Austin Schuhac17fba2020-03-28 15:55:33 -0700380
381 const flatbuffers::Offset<IMUValues> readings_offsets =
382 imu_builder.Finish();
383 const flatbuffers::Offset<
384 flatbuffers::Vector<flatbuffers::Offset<IMUValues>>>
385 readings_offset = builder.fbb()->CreateVector(&readings_offsets, 1);
386
387 IMUValuesBatch::Builder imu_batch_builder =
388 builder.MakeBuilder<IMUValuesBatch>();
389 imu_batch_builder.add_readings(readings_offset);
milind1f1dca32021-07-03 13:50:07 -0700390 builder.CheckOk(builder.Send(imu_batch_builder.Finish()));
Brian Silverman7be68ba2020-01-08 22:08:40 -0800391 if (success) {
392 state_ = State::kRunning;
393 } else {
394 BeginInitialization();
395 }
Tyler Chatowbf0609c2021-07-31 16:13:27 -0700396 } break;
Brian Silverman7be68ba2020-01-08 22:08:40 -0800397
398 case State::kRunning:
399 LOG(FATAL) << "Not a reset state";
400 }
401}
402
Austin Schuhac17fba2020-03-28 15:55:33 -0700403flatbuffers::Offset<IMUValues> ADIS16470::ProcessReading(
404 flatbuffers::FlatBufferBuilder *fbb) {
Brian Silverman7be68ba2020-01-08 22:08:40 -0800405 // If we ever see this, we'll need to decide how to handle it. Probably reset
406 // everything and try again.
407 CHECK_EQ(0, spi_->GetAutoDroppedCount());
408
Brian Silverman7be68ba2020-01-08 22:08:40 -0800409 absl::Span<const uint32_t> to_process = read_data_;
410 hal::fpga_clock::time_point fpga_time;
411 {
412 int32_t status = 0;
413 const uint64_t fpga_expanded = HAL_ExpandFPGATime(to_process[0], &status);
414 CHECK_EQ(0, status);
415 fpga_time =
416 hal::fpga_clock::time_point(hal::fpga_clock::duration(fpga_expanded));
417 }
418 to_process = to_process.subspan(1);
419
420 const uint16_t diag_stat_value = (static_cast<uint16_t>(to_process[0]) << 8) |
421 static_cast<uint16_t>(to_process[1]);
Austin Schuhac17fba2020-03-28 15:55:33 -0700422 const auto diag_stat = PackDiagStat(fbb, diag_stat_value);
Brian Silverman7be68ba2020-01-08 22:08:40 -0800423 to_process = to_process.subspan(2);
424
Austin Schuhac17fba2020-03-28 15:55:33 -0700425 IMUValues::Builder imu_builder(*fbb);
Brian Silverman7be68ba2020-01-08 22:08:40 -0800426 imu_builder.add_fpga_timestamp(
427 aos::time::DurationInSeconds(fpga_time.time_since_epoch()));
428 imu_builder.add_monotonic_timestamp_ns(
429 time_converter_.FpgaToMonotonic(fpga_time).time_since_epoch().count());
430 imu_builder.add_previous_reading_diag_stat(diag_stat);
431
432 imu_builder.add_gyro_x(ConvertValue16(to_process, kGyroLsbRadianSecond));
433 to_process = to_process.subspan(2);
434 imu_builder.add_accelerometer_x(
435 ConvertValue32(to_process, kAccelerometerLsbG));
436 to_process = to_process.subspan(4);
437 imu_builder.add_gyro_y(ConvertValue16(to_process, kGyroLsbRadianSecond));
438 to_process = to_process.subspan(2);
439 imu_builder.add_accelerometer_y(
440 ConvertValue32(to_process, kAccelerometerLsbG));
441 to_process = to_process.subspan(4);
442 imu_builder.add_gyro_z(ConvertValue16(to_process, kGyroLsbRadianSecond));
443 to_process = to_process.subspan(2);
444 imu_builder.add_accelerometer_z(
445 ConvertValue32(to_process, kAccelerometerLsbG));
446 to_process = to_process.subspan(4);
447
448 imu_builder.add_temperature(
449 ConvertValue16(to_process, kTemperatureLsbDegree));
450 to_process = to_process.subspan(2);
451
452 CHECK(to_process.empty()) << "Have leftover bytes: " << to_process.size();
453
Austin Schuhac17fba2020-03-28 15:55:33 -0700454 return imu_builder.Finish();
Brian Silverman7be68ba2020-01-08 22:08:40 -0800455}
456
457double ADIS16470::ConvertValue32(absl::Span<const uint32_t> data,
458 double lsb_per_output) {
459 const uint32_t unsigned_value = (static_cast<uint32_t>(data[0]) << 24) |
460 (static_cast<uint32_t>(data[1]) << 16) |
461 (static_cast<uint32_t>(data[2]) << 8) |
462 static_cast<uint32_t>(data[3]);
463 int32_t signed_value;
464 memcpy(&signed_value, &unsigned_value, sizeof(unsigned_value));
465 return static_cast<double>(signed_value) * lsb_per_output;
466}
467
468double ADIS16470::ConvertValue16(absl::Span<const uint32_t> data,
469 double lsb_per_output) {
470 const uint16_t unsigned_value =
471 (static_cast<uint16_t>(data[0]) << 8) | static_cast<uint16_t>(data[1]);
472 int16_t signed_value;
473 memcpy(&signed_value, &unsigned_value, sizeof(unsigned_value));
474 return static_cast<double>(signed_value) * lsb_per_output;
475}
476
477flatbuffers::Offset<ADIS16470DiagStat> ADIS16470::PackDiagStat(
478 flatbuffers::FlatBufferBuilder *fbb, uint16_t value) {
479 ADIS16470DiagStat::Builder diag_stat_builder(*fbb);
480 diag_stat_builder.add_clock_error(value & (1 << 7));
481 diag_stat_builder.add_memory_failure(value & (1 << 6));
482 diag_stat_builder.add_sensor_failure(value & (1 << 5));
483 diag_stat_builder.add_standby_mode(value & (1 << 4));
484 diag_stat_builder.add_spi_communication_error(value & (1 << 3));
485 diag_stat_builder.add_flash_memory_update_error(value & (1 << 2));
486 diag_stat_builder.add_data_path_overrun(value & (1 << 1));
487 return diag_stat_builder.Finish();
488}
489
490bool ADIS16470::DiagStatHasError(const ADIS16470DiagStat &diag_stat) {
491 return diag_stat.clock_error() || diag_stat.memory_failure() ||
492 diag_stat.sensor_failure() || diag_stat.standby_mode() ||
493 diag_stat.spi_communication_error() ||
494 diag_stat.flash_memory_update_error() || diag_stat.data_path_overrun();
495}
496
497uint16_t ADIS16470::ReadRegister(uint8_t register_address,
498 uint8_t next_register_address) {
499 uint8_t send_buffer[2] = {static_cast<uint8_t>(register_address & 0x7f), 0};
500 uint8_t dummy[2];
501 spi_->Transaction(send_buffer, dummy, sizeof(send_buffer));
502 uint8_t receive_buffer[2];
503 uint8_t next_send_buffer[2] = {
504 static_cast<uint8_t>(next_register_address & 0x7f), 0};
505 spi_->Transaction(next_send_buffer, receive_buffer, sizeof(receive_buffer));
506 return (static_cast<uint16_t>(receive_buffer[0]) << 8) |
507 static_cast<uint16_t>(receive_buffer[1]);
508}
509
510void ADIS16470::WriteRegister(uint8_t register_address, uint16_t value) {
511 uint8_t buffer1[2] = {static_cast<uint8_t>(register_address | 0x80),
512 static_cast<uint8_t>(value & 0xff)};
513 uint8_t buffer2[2] = {static_cast<uint8_t>(register_address | 0x81),
514 static_cast<uint8_t>(value >> 8)};
515 spi_->Write(buffer1, sizeof(buffer1));
516 spi_->Write(buffer2, sizeof(buffer2));
517}
518
519} // namespace wpilib
520} // namespace frc971