blob: eeacfce02e27a766c38581688348861ae055aa8f [file] [log] [blame]
Brian Silverman07ec88e2014-12-28 00:13:08 -08001#include "frc971/wpilib/gyro_interface.h"
2
3#include <inttypes.h>
Austin Schuhf2a50ba2016-12-24 16:16:26 -08004#include <chrono>
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();
21 gyro_->SetClockActiveHigh();
22 gyro_->SetSampleDataOnRising();
23 gyro_->SetMSBFirst();
24}
25
26bool GyroInterface::InitializeGyro() {
27 uint32_t result;
28 if (!DoTransaction(0x20000003, &result)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070029 AOS_LOG(WARNING, "failed to start a self-check\n");
Brian Silverman07ec88e2014-12-28 00:13:08 -080030 return false;
31 }
32 if (result != 1) {
33 // We might have hit a parity error or something and are now retrying, so
34 // this isn't a very big deal.
Austin Schuhf257f3c2019-10-27 21:00:43 -070035 AOS_LOG(INFO, "gyro unexpected initial response 0x%" PRIx32 "\n", result);
Brian Silverman07ec88e2014-12-28 00:13:08 -080036 }
37
38 // Wait for it to assert the fault conditions before reading them.
Austin Schuhf2a50ba2016-12-24 16:16:26 -080039 ::std::this_thread::sleep_for(::std::chrono::milliseconds(50));
Brian Silverman07ec88e2014-12-28 00:13:08 -080040
41 if (!DoTransaction(0x20000000, &result)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070042 AOS_LOG(WARNING, "failed to clear latched non-fault data\n");
Brian Silverman07ec88e2014-12-28 00:13:08 -080043 return false;
44 }
Austin Schuhf257f3c2019-10-27 21:00:43 -070045 AOS_LOG(DEBUG, "gyro dummy response is 0x%" PRIx32 "\n", result);
Brian Silverman07ec88e2014-12-28 00:13:08 -080046
47 if (!DoTransaction(0x20000000, &result)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070048 AOS_LOG(WARNING, "failed to read self-test data\n");
Brian Silverman07ec88e2014-12-28 00:13:08 -080049 return false;
50 }
51 if (ExtractStatus(result) != 2) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070052 AOS_LOG(WARNING, "gyro first value 0x%" PRIx32 " not self-test data\n",
53 result);
Brian Silverman07ec88e2014-12-28 00:13:08 -080054 return false;
55 }
56 if (ExtractErrors(result) != 0x7F) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070057 AOS_LOG(WARNING,
58 "gyro first value 0x%" PRIx32 " does not have all errors\n",
59 result);
Brian Silverman07ec88e2014-12-28 00:13:08 -080060 return false;
61 }
62
63 if (!DoTransaction(0x20000000, &result)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070064 AOS_LOG(WARNING, "failed to clear latched self-test data\n");
Brian Silverman07ec88e2014-12-28 00:13:08 -080065 return false;
66 }
67 if (ExtractStatus(result) != 2) {
Austin Schuhf257f3c2019-10-27 21:00:43 -070068 AOS_LOG(WARNING, "gyro second value 0x%" PRIx32 " not self-test data\n",
69 result);
Brian Silverman07ec88e2014-12-28 00:13:08 -080070 return false;
71 }
72
73 return true;
74}
75
76bool GyroInterface::DoTransaction(uint32_t to_write, uint32_t *result) {
77 static const uint8_t kBytes = 4;
78 static_assert(kBytes == sizeof(to_write),
79 "need the same number of bytes as sizeof(the data)");
80
Brian Silverman5f17a972016-02-28 01:49:32 -050081 if (__builtin_parity(to_write & ~1) == 0) {
82 to_write |= 1;
83 } else {
84 to_write &= ~1;
85 }
Brian Silverman07ec88e2014-12-28 00:13:08 -080086
87 uint8_t to_send[kBytes], to_receive[kBytes];
88 const uint32_t to_write_flipped = __builtin_bswap32(to_write);
89 memcpy(to_send, &to_write_flipped, kBytes);
90
91 switch (gyro_->Transaction(to_send, to_receive, kBytes)) {
92 case -1:
Austin Schuhf257f3c2019-10-27 21:00:43 -070093 AOS_LOG(INFO, "SPI::Transaction failed\n");
Brian Silverman07ec88e2014-12-28 00:13:08 -080094 return false;
95 case kBytes:
96 break;
97 default:
Austin Schuhf257f3c2019-10-27 21:00:43 -070098 AOS_LOG(FATAL, "SPI::Transaction returned something weird\n");
Brian Silverman07ec88e2014-12-28 00:13:08 -080099 }
100
101 memcpy(result, to_receive, kBytes);
102 if (__builtin_parity(*result & 0xFFFF) != 1) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700103 AOS_LOG(INFO, "high byte parity failure\n");
Brian Silverman07ec88e2014-12-28 00:13:08 -0800104 return false;
105 }
106 if (__builtin_parity(*result) != 1) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700107 AOS_LOG(INFO, "whole value parity failure\n");
Brian Silverman07ec88e2014-12-28 00:13:08 -0800108 return false;
109 }
110
111 *result = __builtin_bswap32(*result);
112 return true;
113}
114
115uint16_t GyroInterface::DoRead(uint8_t address) {
116 const uint32_t command = (0x8 << 28) | (address << 17);
117 uint32_t response;
118 while (true) {
119 if (!DoTransaction(command, &response)) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700120 AOS_LOG(WARNING, "reading 0x%" PRIx8 " failed\n", address);
Brian Silverman07ec88e2014-12-28 00:13:08 -0800121 continue;
122 }
123 if ((response & 0xEFE00000) != 0x4E000000) {
Austin Schuhf257f3c2019-10-27 21:00:43 -0700124 AOS_LOG(WARNING,
125 "gyro read from 0x%" PRIx8 " gave unexpected response 0x%" PRIx32
126 "\n",
127 address, response);
Brian Silverman07ec88e2014-12-28 00:13:08 -0800128 continue;
129 }
130 return (response >> 5) & 0xFFFF;
131 }
132}
133
134double GyroInterface::ExtractAngle(uint32_t value) {
Brian Silverman5f17a972016-02-28 01:49:32 -0500135 const int16_t reading = -static_cast<int16_t>(value >> 10 & 0xFFFF);
Brian Silverman07ec88e2014-12-28 00:13:08 -0800136 return static_cast<double>(reading) * 2.0 * M_PI / 360.0 / 80.0;
137}
138
139uint32_t GyroInterface::ReadPartID() {
140 return (DoRead(0x0E) << 16) | DoRead(0x10);
141}
142
143uint32_t GyroInterface::GetReading() {
144 uint32_t result;
145 if (!DoTransaction(0x20000000, &result)) {
146 return 0;
147 }
148 return result;
149}
150
151} // namespace wpilib
152} // namespace frc971