blob: e539828a3d26bef438d4886a8c0240945b11b18f [file] [log] [blame]
Brian Silverman5f17a972016-02-28 01:49:32 -05001#include "frc971/wpilib/ADIS16448.h"
2
3#include "InterruptableSensorBase.h"
4#undef ERROR
5
6#include <inttypes.h>
7#include <math.h>
Austin Schuhf2a50ba2016-12-24 16:16:26 -08008#include <chrono>
Brian Silverman5f17a972016-02-28 01:49:32 -05009
10#include "aos/common/logging/queue_logging.h"
Austin Schuh943fcbd2016-04-03 21:35:41 -070011#include "aos/common/messages/robot_state.q.h"
Brian Silverman5f17a972016-02-28 01:49:32 -050012#include "aos/common/time.h"
13#include "aos/linux_code/init.h"
Brian Silverman5f17a972016-02-28 01:49:32 -050014#include "frc971/wpilib/imu.q.h"
Philipp Schrader29d54f22016-04-02 22:14:48 +000015#include "frc971/zeroing/averager.h"
Brian Silverman5f17a972016-02-28 01:49:32 -050016
17namespace frc971 {
18namespace wpilib {
19
Austin Schuhf2a50ba2016-12-24 16:16:26 -080020using ::aos::monotonic_clock;
21namespace chrono = ::std::chrono;
22
Brian Silverman5f17a972016-02-28 01:49:32 -050023template <uint8_t size>
24bool ADIS16448::DoTransaction(uint8_t to_send[size], uint8_t to_receive[size]) {
25 switch (spi_->Transaction(to_send, to_receive, size)) {
26 case -1:
27 LOG(INFO, "SPI::Transaction of %zd bytes failed\n", size);
28 return false;
29 case size:
Brian Silvermana70994f2017-03-16 22:32:55 -070030 if (dummy_spi_) {
31 uint8_t dummy_send, dummy_receive;
32 dummy_spi_->Transaction(&dummy_send, &dummy_receive, 1);
33 }
Brian Silverman5f17a972016-02-28 01:49:32 -050034 return true;
35 default:
36 LOG(FATAL, "SPI::Transaction returned something weird\n");
37 }
38}
39
40namespace {
41
42// Addresses pulled out of the documentation.
43constexpr uint8_t kMscCtrlAddress = 0x34;
44constexpr uint8_t kSmplPrdAddress = 0x36;
45constexpr uint8_t kDiagStatAddress = 0x3C;
46constexpr uint8_t kGlobalReadAddress = 0x3E;
47constexpr uint8_t kLotId1Address = 0x52;
48constexpr uint8_t kLotId2Address = 0x54;
49constexpr uint8_t kProdIdAddress = 0x56;
50constexpr uint8_t kSerialNumberAddress = 0x58;
51
Austin Schuh871a1362016-04-02 12:25:00 -070052// degree/second/LSB for the gyros.
53constexpr double kGyroLsbDegreeSecond = 1.0 / 25.0;
54// G/LSB for the accelerometers.
55constexpr double kAccelerometerLsbG = 1.0 / 1200.0;
56// gauss/LSB for the magnetometers.
57constexpr double kMagnetometerLsbGauss =
58 1.0 / (7.0 / 1000.0) /* mgauss to gauss */;
59// bar/LSB for the barometer.
60constexpr double kBarometerLsbPascal = 0.02 * 100;
61// degree/LSB C for the temperature sensor.
62constexpr double kTemperatureLsbDegree = 0.07386;
Brian Silverman5f17a972016-02-28 01:49:32 -050063// Degrees C corresponding to 0 for the temperature sensor.
64constexpr double kTemperatureZero = 31;
65
66// From somebody online who says this works with the sensor. I don't feel like
67// re-deriving this, and I can't find what all the CRC parameters are supposed
68// to be.
69const uint16_t kCrcTable[256] = {
70 0x0000, 0x17CE, 0x0FDF, 0x1811, 0x1FBE, 0x0870, 0x1061, 0x07AF, 0x1F3F,
71 0x08F1, 0x10E0, 0x072E, 0x0081, 0x174F, 0x0F5E, 0x1890, 0x1E3D, 0x09F3,
72 0x11E2, 0x062C, 0x0183, 0x164D, 0x0E5C, 0x1992, 0x0102, 0x16CC, 0x0EDD,
73 0x1913, 0x1EBC, 0x0972, 0x1163, 0x06AD, 0x1C39, 0x0BF7, 0x13E6, 0x0428,
74 0x0387, 0x1449, 0x0C58, 0x1B96, 0x0306, 0x14C8, 0x0CD9, 0x1B17, 0x1CB8,
75 0x0B76, 0x1367, 0x04A9, 0x0204, 0x15CA, 0x0DDB, 0x1A15, 0x1DBA, 0x0A74,
76 0x1265, 0x05AB, 0x1D3B, 0x0AF5, 0x12E4, 0x052A, 0x0285, 0x154B, 0x0D5A,
77 0x1A94, 0x1831, 0x0FFF, 0x17EE, 0x0020, 0x078F, 0x1041, 0x0850, 0x1F9E,
78 0x070E, 0x10C0, 0x08D1, 0x1F1F, 0x18B0, 0x0F7E, 0x176F, 0x00A1, 0x060C,
79 0x11C2, 0x09D3, 0x1E1D, 0x19B2, 0x0E7C, 0x166D, 0x01A3, 0x1933, 0x0EFD,
80 0x16EC, 0x0122, 0x068D, 0x1143, 0x0952, 0x1E9C, 0x0408, 0x13C6, 0x0BD7,
81 0x1C19, 0x1BB6, 0x0C78, 0x1469, 0x03A7, 0x1B37, 0x0CF9, 0x14E8, 0x0326,
82 0x0489, 0x1347, 0x0B56, 0x1C98, 0x1A35, 0x0DFB, 0x15EA, 0x0224, 0x058B,
83 0x1245, 0x0A54, 0x1D9A, 0x050A, 0x12C4, 0x0AD5, 0x1D1B, 0x1AB4, 0x0D7A,
84 0x156B, 0x02A5, 0x1021, 0x07EF, 0x1FFE, 0x0830, 0x0F9F, 0x1851, 0x0040,
85 0x178E, 0x0F1E, 0x18D0, 0x00C1, 0x170F, 0x10A0, 0x076E, 0x1F7F, 0x08B1,
86 0x0E1C, 0x19D2, 0x01C3, 0x160D, 0x11A2, 0x066C, 0x1E7D, 0x09B3, 0x1123,
87 0x06ED, 0x1EFC, 0x0932, 0x0E9D, 0x1953, 0x0142, 0x168C, 0x0C18, 0x1BD6,
88 0x03C7, 0x1409, 0x13A6, 0x0468, 0x1C79, 0x0BB7, 0x1327, 0x04E9, 0x1CF8,
89 0x0B36, 0x0C99, 0x1B57, 0x0346, 0x1488, 0x1225, 0x05EB, 0x1DFA, 0x0A34,
90 0x0D9B, 0x1A55, 0x0244, 0x158A, 0x0D1A, 0x1AD4, 0x02C5, 0x150B, 0x12A4,
91 0x056A, 0x1D7B, 0x0AB5, 0x0810, 0x1FDE, 0x07CF, 0x1001, 0x17AE, 0x0060,
92 0x1871, 0x0FBF, 0x172F, 0x00E1, 0x18F0, 0x0F3E, 0x0891, 0x1F5F, 0x074E,
93 0x1080, 0x162D, 0x01E3, 0x19F2, 0x0E3C, 0x0993, 0x1E5D, 0x064C, 0x1182,
94 0x0912, 0x1EDC, 0x06CD, 0x1103, 0x16AC, 0x0162, 0x1973, 0x0EBD, 0x1429,
95 0x03E7, 0x1BF6, 0x0C38, 0x0B97, 0x1C59, 0x0448, 0x1386, 0x0B16, 0x1CD8,
96 0x04C9, 0x1307, 0x14A8, 0x0366, 0x1B77, 0x0CB9, 0x0A14, 0x1DDA, 0x05CB,
97 0x1205, 0x15AA, 0x0264, 0x1A75, 0x0DBB, 0x152B, 0x02E5, 0x1AF4, 0x0D3A,
98 0x0A95, 0x1D5B, 0x054A, 0x1284};
99
100uint16_t CalculateCrc(const uint8_t *data, size_t data_length) {
101 uint16_t crc = 0xFFFF;
102 uint16_t byte;
103
104 while (data_length--) {
105 // Compute lower byte CRC first.
106 byte = data[1];
107 crc = (crc >> 8) ^ kCrcTable[(crc & 0x00FF) ^ byte];
108 // Compute upper byte of CRC.
109 byte = data[0];
110 crc = (crc >> 8) ^ kCrcTable[(crc & 0x00FF) ^ byte];
111 data += 2;
112 }
113 crc = ~crc; // Compute complement of CRC
114 return static_cast<uint16_t>(
115 (crc << 8) | (crc >> 8)); // Perform byte swap prior to returning CRC;
116}
117
118} // namespace
119
120ADIS16448::ADIS16448(SPI::Port port, DigitalInput *dio1)
121 : spi_(new SPI(port)), dio1_(dio1) {
Brian Silvermana70994f2017-03-16 22:32:55 -0700122 // 1MHz is the maximum supported for burst reads, but we
123 // want to go slower to hopefully make it more reliable.
124 spi_->SetClockRate(1e5);
Brian Silverman5f17a972016-02-28 01:49:32 -0500125 spi_->SetChipSelectActiveLow();
126 spi_->SetClockActiveLow();
127 spi_->SetSampleDataOnFalling();
128 spi_->SetMSBFirst();
129
130 dio1_->RequestInterrupts();
131 dio1_->SetUpSourceEdge(true, false);
132}
133
Brian Silvermana70994f2017-03-16 22:32:55 -0700134void ADIS16448::SetDummySPI(SPI::Port port) {
135 dummy_spi_.reset(new SPI(port));
136 // Pick the same settings here in case the roboRIO decides to try something
137 // stupid when switching.
138 if (dummy_spi_) {
139 dummy_spi_->SetClockRate(1e5);
140 dummy_spi_->SetChipSelectActiveLow();
141 dummy_spi_->SetClockActiveLow();
142 dummy_spi_->SetSampleDataOnFalling();
143 dummy_spi_->SetMSBFirst();
144 }
145}
146
147void ADIS16448::InitializeUntilSuccessful() {
148 while (run_ && !Initialize()) {
149 if (reset_) {
150 reset_->Set(false);
151 // Datasheet says this needs to be at least 10 us long, so 10 ms is
152 // plenty.
153 ::std::this_thread::sleep_for(::std::chrono::milliseconds(10));
154 reset_->Set(true);
155 // Datasheet says this takes 90 ms typically, and we want to give it
156 // plenty of margin.
157 ::std::this_thread::sleep_for(::std::chrono::milliseconds(150));
158 } else {
159 ::std::this_thread::sleep_for(::std::chrono::milliseconds(50));
160 }
161 }
162 LOG(INFO, "IMU initialized successfully\n");
163
164 ::aos::SetCurrentThreadRealtimePriority(33);
165}
166
Brian Silverman5f17a972016-02-28 01:49:32 -0500167void ADIS16448::operator()() {
Austin Schuhf266db52017-03-05 22:27:50 -0800168 // NI's SPI driver defaults to SCHED_OTHER. Find it's PID with ps, and change
169 // it to a RT priority of 33.
170 PCHECK(system(
171 "ps -ef | grep '\\[spi0\\]' | awk '{print $1}' | xargs chrt -f -p "
172 "33") == 0);
173
Brian Silverman5f17a972016-02-28 01:49:32 -0500174 ::aos::SetCurrentThreadName("IMU");
175
Brian Silvermana70994f2017-03-16 22:32:55 -0700176 InitializeUntilSuccessful();
Austin Schuhf266db52017-03-05 22:27:50 -0800177
Philipp Schrader29d54f22016-04-02 22:14:48 +0000178 // Rounded to approximate the 204.8 Hz.
179 constexpr size_t kImuSendRate = 205;
180
181 zeroing::Averager<double, 6 * kImuSendRate> average_gyro_x;
182 zeroing::Averager<double, 6 * kImuSendRate> average_gyro_y;
183 zeroing::Averager<double, 6 * kImuSendRate> average_gyro_z;
184
Brian Silverman5f17a972016-02-28 01:49:32 -0500185 bool got_an_interrupt = false;
186 while (run_) {
187 {
188 const InterruptableSensorBase::WaitResult result =
189 dio1_->WaitForInterrupt(0.1, !got_an_interrupt);
190 if (result == InterruptableSensorBase::kTimeout) {
191 LOG(WARNING, "IMU read timed out\n");
Brian Silvermana70994f2017-03-16 22:32:55 -0700192 InitializeUntilSuccessful();
Brian Silverman5f17a972016-02-28 01:49:32 -0500193 continue;
194 }
195 }
196 got_an_interrupt = true;
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800197 const monotonic_clock::time_point read_time = monotonic_clock::now();
Brian Silverman5f17a972016-02-28 01:49:32 -0500198
Austin Schuh871a1362016-04-02 12:25:00 -0700199 uint8_t to_send[2 * 14], to_receive[2 * 14];
200 memset(&to_send[0], 0, sizeof(to_send));
Brian Silverman5f17a972016-02-28 01:49:32 -0500201 to_send[0] = kGlobalReadAddress;
Austin Schuh871a1362016-04-02 12:25:00 -0700202 if (!DoTransaction<2 * 14>(to_send, to_receive)) continue;
Brian Silverman5f17a972016-02-28 01:49:32 -0500203
204 // If it's false now or another edge happened, then we're in trouble. This
205 // won't catch all instances of being a little bit slow (because of the
206 // interrupt delay among other things), but it will catch the code
207 // constantly falling behind, which seems like the most likely failure
208 // scenario.
209 if (!dio1_->Get() ||
210 dio1_->WaitForInterrupt(0, false) !=
211 InterruptableSensorBase::kTimeout) {
212 LOG(ERROR, "IMU read took too long\n");
213 continue;
214 }
215
216 {
217 const uint16_t calculated_crc = CalculateCrc(&to_receive[4], 11);
Austin Schuh871a1362016-04-02 12:25:00 -0700218 uint16_t received_crc =
219 to_receive[13 * 2 + 1] | (to_receive[13 * 2] << 8);
Brian Silverman5f17a972016-02-28 01:49:32 -0500220 if (received_crc != calculated_crc) {
221 LOG(WARNING, "received CRC %" PRIx16 " but calculated %" PRIx16 "\n",
222 received_crc, calculated_crc);
Brian Silvermana70994f2017-03-16 22:32:55 -0700223 InitializeUntilSuccessful();
Brian Silverman5f17a972016-02-28 01:49:32 -0500224 continue;
225 }
226 }
227
228 {
229 uint16_t diag_stat;
Austin Schuh871a1362016-04-02 12:25:00 -0700230 memcpy(&diag_stat, &to_receive[2], 2);
Brian Silvermana70994f2017-03-16 22:32:55 -0700231 if (!CheckDiagStatValue(diag_stat)) {
232 InitializeUntilSuccessful();
233 continue;
234 }
Brian Silverman5f17a972016-02-28 01:49:32 -0500235 }
236
237 auto message = imu_values.MakeMessage();
238 message->fpga_timestamp = dio1_->ReadRisingTimestamp();
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800239 message->monotonic_timestamp_ns =
240 chrono::duration_cast<chrono::nanoseconds>(read_time.time_since_epoch())
241 .count();
Brian Silverman5f17a972016-02-28 01:49:32 -0500242
243 message->gyro_x =
Austin Schuh871a1362016-04-02 12:25:00 -0700244 ConvertValue(&to_receive[4], kGyroLsbDegreeSecond * M_PI / 180.0);
Brian Silverman5f17a972016-02-28 01:49:32 -0500245 message->gyro_y =
Austin Schuh871a1362016-04-02 12:25:00 -0700246 ConvertValue(&to_receive[6], kGyroLsbDegreeSecond * M_PI / 180.0);
Brian Silverman5f17a972016-02-28 01:49:32 -0500247 message->gyro_z =
Austin Schuh871a1362016-04-02 12:25:00 -0700248 ConvertValue(&to_receive[8], kGyroLsbDegreeSecond * M_PI / 180.0);
Brian Silverman5f17a972016-02-28 01:49:32 -0500249
Philipp Schrader29d54f22016-04-02 22:14:48 +0000250 // The first few seconds of samples are averaged and subtracted from
251 // subsequent samples for zeroing purposes.
Austin Schuh943fcbd2016-04-03 21:35:41 -0700252 if (!gyros_are_zeroed_) {
Philipp Schrader29d54f22016-04-02 22:14:48 +0000253 average_gyro_x.AddData(message->gyro_x);
254 average_gyro_y.AddData(message->gyro_y);
255 average_gyro_z.AddData(message->gyro_z);
256
257 if (average_gyro_x.full() && average_gyro_y.full() &&
258 average_gyro_z.full()) {
Austin Schuh943fcbd2016-04-03 21:35:41 -0700259 ::aos::joystick_state.FetchLatest();
260 if (::aos::joystick_state.get() && ::aos::joystick_state->enabled) {
261 gyro_x_zeroed_offset_ = -average_gyro_x.GetAverage();
262 gyro_y_zeroed_offset_ = -average_gyro_y.GetAverage();
263 gyro_z_zeroed_offset_ = -average_gyro_z.GetAverage();
264 LOG(INFO, "total gyro zero offset X:%f, Y:%f, Z:%f\n",
265 gyro_x_zeroed_offset_, gyro_y_zeroed_offset_,
266 gyro_z_zeroed_offset_);
267 gyros_are_zeroed_ = true;
268 }
Philipp Schrader29d54f22016-04-02 22:14:48 +0000269 }
270 }
271
Austin Schuh943fcbd2016-04-03 21:35:41 -0700272 message->gyro_x += gyro_x_zeroed_offset_;
273 message->gyro_y += gyro_y_zeroed_offset_;
274 message->gyro_z += gyro_z_zeroed_offset_;
275
Austin Schuh871a1362016-04-02 12:25:00 -0700276 message->accelerometer_x =
277 ConvertValue(&to_receive[10], kAccelerometerLsbG);
278 message->accelerometer_y =
279 ConvertValue(&to_receive[12], kAccelerometerLsbG);
280 message->accelerometer_z =
281 ConvertValue(&to_receive[14], kAccelerometerLsbG);
Brian Silverman5f17a972016-02-28 01:49:32 -0500282
283 message->magnetometer_x =
Austin Schuh871a1362016-04-02 12:25:00 -0700284 ConvertValue(&to_receive[16], kMagnetometerLsbGauss);
Brian Silverman5f17a972016-02-28 01:49:32 -0500285 message->magnetometer_y =
Austin Schuh871a1362016-04-02 12:25:00 -0700286 ConvertValue(&to_receive[18], kMagnetometerLsbGauss);
Brian Silverman5f17a972016-02-28 01:49:32 -0500287 message->magnetometer_z =
Austin Schuh871a1362016-04-02 12:25:00 -0700288 ConvertValue(&to_receive[20], kMagnetometerLsbGauss);
Brian Silverman5f17a972016-02-28 01:49:32 -0500289
290 message->barometer =
Austin Schuh871a1362016-04-02 12:25:00 -0700291 ConvertValue(&to_receive[22], kBarometerLsbPascal, false);
Brian Silverman5f17a972016-02-28 01:49:32 -0500292
293 message->temperature =
Austin Schuh871a1362016-04-02 12:25:00 -0700294 ConvertValue(&to_receive[24], kTemperatureLsbDegree) + kTemperatureZero;
Brian Silverman5f17a972016-02-28 01:49:32 -0500295
296 LOG_STRUCT(DEBUG, "sending", *message);
297 if (!message.Send()) {
298 LOG(WARNING, "sending queue message failed\n");
299 }
300 }
301}
302
303float ADIS16448::ConvertValue(uint8_t *data, double lsb_per_output, bool sign) {
304 double value;
305 if (sign) {
Austin Schuh871a1362016-04-02 12:25:00 -0700306 int16_t raw_value = static_cast<int16_t>(
307 (static_cast<uint16_t>(data[0]) << 8) | static_cast<uint16_t>(data[1]));
Brian Silverman5f17a972016-02-28 01:49:32 -0500308 value = raw_value;
309 } else {
Austin Schuh871a1362016-04-02 12:25:00 -0700310 uint16_t raw_value =
311 (static_cast<uint16_t>(data[0]) << 8) | static_cast<uint16_t>(data[1]);
Brian Silverman5f17a972016-02-28 01:49:32 -0500312 value = raw_value;
313 }
314 return value * lsb_per_output;
315}
316
317bool ADIS16448::ReadRegister(uint8_t next_address, uint16_t *value) {
318 uint8_t to_send[2], to_receive[2];
319 to_send[0] = next_address;
320 to_send[1] = 0;
321
322 if (!DoTransaction<2>(to_send, to_receive)) return false;
323
Austin Schuh871a1362016-04-02 12:25:00 -0700324 if (value) {
325 memcpy(value, to_receive, 2);
326 }
Brian Silverman5f17a972016-02-28 01:49:32 -0500327 return true;
328}
329
330bool ADIS16448::WriteRegister(uint8_t address, uint16_t value) {
331 uint8_t to_send[4], to_receive[4];
332 to_send[0] = address | 0x80;
333 to_send[1] = value & 0xFF;
334 to_send[2] = address | 0x81;
335 to_send[3] = value >> 8;
336 if (!DoTransaction<4>(to_send, to_receive)) return false;
337 return true;
338}
339
340bool ADIS16448::CheckDiagStatValue(uint16_t value) const {
341 bool r = true;
Brian Silverman5f17a972016-02-28 01:49:32 -0500342 if (value & (1 << 2)) {
343 LOG(WARNING, "IMU gave flash update failure\n");
344 }
345 if (value & (1 << 3)) {
346 LOG(WARNING, "IMU gave SPI communication failure\n");
347 }
348 if (value & (1 << 4)) {
349 LOG(WARNING, "IMU gave sensor overrange\n");
350 }
351 if (value & (1 << 5)) {
352 LOG(WARNING, "IMU gave self-test failure\n");
353 r = false;
Austin Schuh871a1362016-04-02 12:25:00 -0700354 if (value & (1 << 10)) {
355 LOG(WARNING, "IMU gave X-axis gyro self-test failure\n");
356 }
357 if (value & (1 << 11)) {
358 LOG(WARNING, "IMU gave Y-axis gyro self-test failure\n");
359 }
360 if (value & (1 << 12)) {
361 LOG(WARNING, "IMU gave Z-axis gyro self-test failure\n");
362 }
363 if (value & (1 << 13)) {
364 LOG(WARNING, "IMU gave X-axis accelerometer self-test failure\n");
365 }
366 if (value & (1 << 14)) {
367 LOG(WARNING, "IMU gave Y-axis accelerometer self-test failure\n");
368 }
369 if (value & (1 << 15)) {
370 LOG(WARNING, "IMU gave Z-axis accelerometer self-test failure, %x\n",
371 value);
372 }
373 if (value & (1 << 0)) {
374 LOG(WARNING, "IMU gave magnetometer functional test failure\n");
375 }
376 if (value & (1 << 1)) {
377 LOG(WARNING, "IMU gave barometer functional test failure\n");
378 }
Brian Silverman5f17a972016-02-28 01:49:32 -0500379 }
380 if (value & (1 << 6)) {
381 LOG(WARNING, "IMU gave flash test checksum failure\n");
382 }
383 if (value & (1 << 8)) {
384 LOG(WARNING, "IMU says alarm 1 is active\n");
385 }
386 if (value & (1 << 9)) {
387 LOG(WARNING, "IMU says alarm 2 is active\n");
388 }
Brian Silverman5f17a972016-02-28 01:49:32 -0500389 return r;
390}
391
392bool ADIS16448::Initialize() {
393 if (!ReadRegister(kProdIdAddress, nullptr)) return false;
394 uint16_t product_id;
395 if (!ReadRegister(kLotId1Address, &product_id)) return false;
396 if (product_id != 0x4040) {
397 LOG(ERROR, "product ID is %" PRIx16 " instead of 0x4040\n", product_id);
398 return false;
399 }
400
401 uint16_t lot_id1, lot_id2, serial_number;
402 if (!ReadRegister(kLotId2Address, &lot_id1)) return false;
403 if (!ReadRegister(kSerialNumberAddress, &lot_id2)) return false;
404 if (!ReadRegister(0, &serial_number)) return false;
405 LOG(INFO, "have IMU %" PRIx16 "%" PRIx16 ": %" PRIx16 "\n", lot_id1, lot_id2,
406 serial_number);
407
408 // Divide the sampling by 2^2 = 4 to get 819.2 / 4 = 204.8 Hz.
Austin Schuh871a1362016-04-02 12:25:00 -0700409 if (!WriteRegister(kSmplPrdAddress, (2 << 8) | 1)) return false;
Brian Silverman5f17a972016-02-28 01:49:32 -0500410
411 // Start a self test.
412 if (!WriteRegister(kMscCtrlAddress, 1 << 10)) return false;
413 // Wait for the self test to finish.
414 {
415 uint16_t value;
416 do {
Austin Schuhf2a50ba2016-12-24 16:16:26 -0800417 ::std::this_thread::sleep_for(::std::chrono::milliseconds(10));
Brian Silverman5f17a972016-02-28 01:49:32 -0500418 if (!ReadRegister(kMscCtrlAddress, &value)) return false;
419 } while ((value & (1 << 10)) != 0);
420 }
421
422 if (!ReadRegister(kDiagStatAddress, nullptr)) return false;
423 uint16_t diag_stat;
424 if (!ReadRegister(0, &diag_stat)) return false;
425 if (!CheckDiagStatValue(diag_stat)) return false;
426
427 if (!WriteRegister(kMscCtrlAddress,
428 ((0 << 0) | // DIO1
429 (1 << 1) | // DIO goes high when data is valid
430 (1 << 2) | // enable DIO changing when data is vald
Austin Schuh871a1362016-04-02 12:25:00 -0700431 (1 << 4) | // enable CRC16 for burst mode
432 (1 << 6)))) {
Brian Silverman5f17a972016-02-28 01:49:32 -0500433 return false;
434 }
435 return true;
436}
437
438} // namespace wpilib
439} // namespace frc971