blob: a90e95a938c5f1fcb9f88feb9f058c433a25e1db [file] [log] [blame]
Brian Silverman07ec88e2014-12-28 00:13:08 -08001#include "frc971/wpilib/gyro_interface.h"
2
Austin Schuhf2a50ba2016-12-24 16:16:26 -08003#include <chrono>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07004#include <cinttypes>
Brian Silverman07ec88e2014-12-28 00:13:08 -08005
John Park33858a32018-09-28 23:05:48 -07006#include "aos/logging/logging.h"
7#include "aos/time/time.h"
Brian Silverman07ec88e2014-12-28 00:13:08 -08008
9#ifndef M_PI
10#define M_PI 3.14159265358979323846
11#endif
12
13namespace frc971 {
14namespace wpilib {
15
Parker Schuhd3b7a8872018-02-19 16:42:27 -080016GyroInterface::GyroInterface() : gyro_(new frc::SPI(frc::SPI::kOnboardCS0)) {
Brian Silverman07ec88e2014-12-28 00:13:08 -080017 // The gyro goes up to 8.08MHz.
18 // The myRIO goes up to 4MHz, so the roboRIO probably does too.
19 gyro_->SetClockRate(4e6);
20 gyro_->SetChipSelectActiveLow();
James Kuszmaul9776b392023-01-14 14:08:08 -080021 gyro_->SetMode(frc::SPI::Mode::kMode3);
Brian Silverman07ec88e2014-12-28 00:13:08 -080022}
23
24bool GyroInterface::InitializeGyro() {
25 uint32_t result;
26 if (!DoTransaction(0x20000003, &result)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070027 AOS_LOG(WARNING, "failed to start a self-check\n");
Brian Silverman07ec88e2014-12-28 00:13:08 -080028 return false;
29 }
30 if (result != 1) {
31 // We might have hit a parity error or something and are now retrying, so
32 // this isn't a very big deal.
Austin Schuhf257f3c2019-10-27 21:00:43 -070033 AOS_LOG(INFO, "gyro unexpected initial response 0x%" PRIx32 "\n", result);
Brian Silverman07ec88e2014-12-28 00:13:08 -080034 }
35
36 // Wait for it to assert the fault conditions before reading them.
Austin Schuhf2a50ba2016-12-24 16:16:26 -080037 ::std::this_thread::sleep_for(::std::chrono::milliseconds(50));
Brian Silverman07ec88e2014-12-28 00:13:08 -080038
39 if (!DoTransaction(0x20000000, &result)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070040 AOS_LOG(WARNING, "failed to clear latched non-fault data\n");
Brian Silverman07ec88e2014-12-28 00:13:08 -080041 return false;
42 }
Austin Schuhf257f3c2019-10-27 21:00:43 -070043 AOS_LOG(DEBUG, "gyro dummy response is 0x%" PRIx32 "\n", result);
Brian Silverman07ec88e2014-12-28 00:13:08 -080044
45 if (!DoTransaction(0x20000000, &result)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070046 AOS_LOG(WARNING, "failed to read self-test data\n");
Brian Silverman07ec88e2014-12-28 00:13:08 -080047 return false;
48 }
49 if (ExtractStatus(result) != 2) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070050 AOS_LOG(WARNING, "gyro first value 0x%" PRIx32 " not self-test data\n",
51 result);
Brian Silverman07ec88e2014-12-28 00:13:08 -080052 return false;
53 }
54 if (ExtractErrors(result) != 0x7F) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070055 AOS_LOG(WARNING,
56 "gyro first value 0x%" PRIx32 " does not have all errors\n",
57 result);
Brian Silverman07ec88e2014-12-28 00:13:08 -080058 return false;
59 }
60
61 if (!DoTransaction(0x20000000, &result)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070062 AOS_LOG(WARNING, "failed to clear latched self-test data\n");
Brian Silverman07ec88e2014-12-28 00:13:08 -080063 return false;
64 }
65 if (ExtractStatus(result) != 2) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070066 AOS_LOG(WARNING, "gyro second value 0x%" PRIx32 " not self-test data\n",
67 result);
Brian Silverman07ec88e2014-12-28 00:13:08 -080068 return false;
69 }
70
71 return true;
72}
73
74bool GyroInterface::DoTransaction(uint32_t to_write, uint32_t *result) {
75 static const uint8_t kBytes = 4;
76 static_assert(kBytes == sizeof(to_write),
77 "need the same number of bytes as sizeof(the data)");
78
Brian Silverman5f17a972016-02-28 01:49:32 -050079 if (__builtin_parity(to_write & ~1) == 0) {
80 to_write |= 1;
81 } else {
82 to_write &= ~1;
83 }
Brian Silverman07ec88e2014-12-28 00:13:08 -080084
85 uint8_t to_send[kBytes], to_receive[kBytes];
86 const uint32_t to_write_flipped = __builtin_bswap32(to_write);
87 memcpy(to_send, &to_write_flipped, kBytes);
88
89 switch (gyro_->Transaction(to_send, to_receive, kBytes)) {
90 case -1:
Austin Schuhf257f3c2019-10-27 21:00:43 -070091 AOS_LOG(INFO, "SPI::Transaction failed\n");
Brian Silverman07ec88e2014-12-28 00:13:08 -080092 return false;
93 case kBytes:
94 break;
95 default:
Austin Schuhf257f3c2019-10-27 21:00:43 -070096 AOS_LOG(FATAL, "SPI::Transaction returned something weird\n");
Brian Silverman07ec88e2014-12-28 00:13:08 -080097 }
98
99 memcpy(result, to_receive, kBytes);
100 if (__builtin_parity(*result & 0xFFFF) != 1) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700101 AOS_LOG(INFO, "high byte parity failure\n");
Brian Silverman07ec88e2014-12-28 00:13:08 -0800102 return false;
103 }
104 if (__builtin_parity(*result) != 1) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700105 AOS_LOG(INFO, "whole value parity failure\n");
Brian Silverman07ec88e2014-12-28 00:13:08 -0800106 return false;
107 }
108
109 *result = __builtin_bswap32(*result);
110 return true;
111}
112
113uint16_t GyroInterface::DoRead(uint8_t address) {
114 const uint32_t command = (0x8 << 28) | (address << 17);
115 uint32_t response;
116 while (true) {
117 if (!DoTransaction(command, &response)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700118 AOS_LOG(WARNING, "reading 0x%" PRIx8 " failed\n", address);
Brian Silverman07ec88e2014-12-28 00:13:08 -0800119 continue;
120 }
121 if ((response & 0xEFE00000) != 0x4E000000) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700122 AOS_LOG(WARNING,
123 "gyro read from 0x%" PRIx8 " gave unexpected response 0x%" PRIx32
124 "\n",
125 address, response);
Brian Silverman07ec88e2014-12-28 00:13:08 -0800126 continue;
127 }
128 return (response >> 5) & 0xFFFF;
129 }
130}
131
132double GyroInterface::ExtractAngle(uint32_t value) {
Brian Silverman5f17a972016-02-28 01:49:32 -0500133 const int16_t reading = -static_cast<int16_t>(value >> 10 & 0xFFFF);
Brian Silverman07ec88e2014-12-28 00:13:08 -0800134 return static_cast<double>(reading) * 2.0 * M_PI / 360.0 / 80.0;
135}
136
137uint32_t GyroInterface::ReadPartID() {
138 return (DoRead(0x0E) << 16) | DoRead(0x10);
139}
140
141uint32_t GyroInterface::GetReading() {
142 uint32_t result;
143 if (!DoTransaction(0x20000000, &result)) {
144 return 0;
145 }
146 return result;
147}
148
149} // namespace wpilib
150} // namespace frc971