blob: 73b1357dd5897c6bcbf986658db4f17797b5822e [file] [log] [blame]
Austin Schuh812d0d12021-11-04 20:16:48 -07001// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
Brian Silverman8fce7482020-01-05 13:18:21 -08004
5#include "hal/Accelerometer.h"
6
7#include <stdint.h>
8
9#include <cassert>
10#include <cstdio>
11#include <memory>
12
13#include "HALInitializer.h"
14#include "hal/ChipObject.h"
15#include "hal/HAL.h"
16
17using namespace hal;
18
19// The 7-bit I2C address with a 0 "send" bit
20static constexpr uint8_t kSendAddress = (0x1c << 1) | 0;
21
22// The 7-bit I2C address with a 1 "receive" bit
23static constexpr uint8_t kReceiveAddress = (0x1c << 1) | 1;
24
25static constexpr uint8_t kControlTxRx = 1;
26static constexpr uint8_t kControlStart = 2;
27static constexpr uint8_t kControlStop = 4;
28
29static std::unique_ptr<tAccel> accel;
30static HAL_AccelerometerRange accelerometerRange;
31
32// Register addresses
33enum Register {
34 kReg_Status = 0x00,
35 kReg_OutXMSB = 0x01,
36 kReg_OutXLSB = 0x02,
37 kReg_OutYMSB = 0x03,
38 kReg_OutYLSB = 0x04,
39 kReg_OutZMSB = 0x05,
40 kReg_OutZLSB = 0x06,
41 kReg_Sysmod = 0x0B,
42 kReg_IntSource = 0x0C,
43 kReg_WhoAmI = 0x0D,
44 kReg_XYZDataCfg = 0x0E,
45 kReg_HPFilterCutoff = 0x0F,
46 kReg_PLStatus = 0x10,
47 kReg_PLCfg = 0x11,
48 kReg_PLCount = 0x12,
49 kReg_PLBfZcomp = 0x13,
50 kReg_PLThsReg = 0x14,
51 kReg_FFMtCfg = 0x15,
52 kReg_FFMtSrc = 0x16,
53 kReg_FFMtThs = 0x17,
54 kReg_FFMtCount = 0x18,
55 kReg_TransientCfg = 0x1D,
56 kReg_TransientSrc = 0x1E,
57 kReg_TransientThs = 0x1F,
58 kReg_TransientCount = 0x20,
59 kReg_PulseCfg = 0x21,
60 kReg_PulseSrc = 0x22,
61 kReg_PulseThsx = 0x23,
62 kReg_PulseThsy = 0x24,
63 kReg_PulseThsz = 0x25,
64 kReg_PulseTmlt = 0x26,
65 kReg_PulseLtcy = 0x27,
66 kReg_PulseWind = 0x28,
67 kReg_ASlpCount = 0x29,
68 kReg_CtrlReg1 = 0x2A,
69 kReg_CtrlReg2 = 0x2B,
70 kReg_CtrlReg3 = 0x2C,
71 kReg_CtrlReg4 = 0x2D,
72 kReg_CtrlReg5 = 0x2E,
73 kReg_OffX = 0x2F,
74 kReg_OffY = 0x30,
75 kReg_OffZ = 0x31
76};
77
Austin Schuh812d0d12021-11-04 20:16:48 -070078namespace hal::init {
Brian Silverman8fce7482020-01-05 13:18:21 -080079void InitializeAccelerometer() {}
Austin Schuh812d0d12021-11-04 20:16:48 -070080} // namespace hal::init
Brian Silverman8fce7482020-01-05 13:18:21 -080081
82namespace hal {
83
84static void writeRegister(Register reg, uint8_t data);
85static uint8_t readRegister(Register reg);
86
87/**
88 * Initialize the accelerometer.
89 */
90static void initializeAccelerometer() {
91 hal::init::CheckInit();
Austin Schuh812d0d12021-11-04 20:16:48 -070092 int32_t status = 0;
Brian Silverman8fce7482020-01-05 13:18:21 -080093
94 if (!accel) {
95 accel.reset(tAccel::create(&status));
96
97 accelerometerRange = HAL_AccelerometerRange::HAL_AccelerometerRange_k2G;
98
99 // Enable I2C
100 accel->writeCNFG(1, &status);
101
102 // Set the counter to 100 kbps
103 accel->writeCNTR(213, &status);
104
105 // The device identification number should be 0x2a
106 assert(readRegister(kReg_WhoAmI) == 0x2a);
107 }
108}
109
110static void writeRegister(Register reg, uint8_t data) {
111 int32_t status = 0;
112 uint64_t initialTime;
113
114 accel->writeADDR(kSendAddress, &status);
115
116 // Send a start transmit/receive message with the register address
117 accel->writeCNTL(kControlStart | kControlTxRx, &status);
118 accel->writeDATO(reg, &status);
119 accel->strobeGO(&status);
120
121 // Execute and wait until it's done (up to a millisecond)
122 initialTime = HAL_GetFPGATime(&status);
123 while (accel->readSTAT(&status) & 1) {
Austin Schuh812d0d12021-11-04 20:16:48 -0700124 if (HAL_GetFPGATime(&status) > initialTime + 1000)
125 break;
Brian Silverman8fce7482020-01-05 13:18:21 -0800126 }
127
128 // Send a stop transmit/receive message with the data
129 accel->writeCNTL(kControlStop | kControlTxRx, &status);
130 accel->writeDATO(data, &status);
131 accel->strobeGO(&status);
132
133 // Execute and wait until it's done (up to a millisecond)
134 initialTime = HAL_GetFPGATime(&status);
135 while (accel->readSTAT(&status) & 1) {
Austin Schuh812d0d12021-11-04 20:16:48 -0700136 if (HAL_GetFPGATime(&status) > initialTime + 1000)
137 break;
Brian Silverman8fce7482020-01-05 13:18:21 -0800138 }
139}
140
141static uint8_t readRegister(Register reg) {
142 int32_t status = 0;
143 uint64_t initialTime;
144
145 // Send a start transmit/receive message with the register address
146 accel->writeADDR(kSendAddress, &status);
147 accel->writeCNTL(kControlStart | kControlTxRx, &status);
148 accel->writeDATO(reg, &status);
149 accel->strobeGO(&status);
150
151 // Execute and wait until it's done (up to a millisecond)
152 initialTime = HAL_GetFPGATime(&status);
153 while (accel->readSTAT(&status) & 1) {
Austin Schuh812d0d12021-11-04 20:16:48 -0700154 if (HAL_GetFPGATime(&status) > initialTime + 1000)
155 break;
Brian Silverman8fce7482020-01-05 13:18:21 -0800156 }
157
158 // Receive a message with the data and stop
159 accel->writeADDR(kReceiveAddress, &status);
160 accel->writeCNTL(kControlStart | kControlStop | kControlTxRx, &status);
161 accel->strobeGO(&status);
162
163 // Execute and wait until it's done (up to a millisecond)
164 initialTime = HAL_GetFPGATime(&status);
165 while (accel->readSTAT(&status) & 1) {
Austin Schuh812d0d12021-11-04 20:16:48 -0700166 if (HAL_GetFPGATime(&status) > initialTime + 1000)
167 break;
Brian Silverman8fce7482020-01-05 13:18:21 -0800168 }
169
170 return accel->readDATI(&status);
171}
172
173/**
174 * Convert a 12-bit raw acceleration value into a scaled double in units of
175 * 1 g-force, taking into account the accelerometer range.
176 */
177static double unpackAxis(int16_t raw) {
Austin Schuh1e69f942020-11-14 15:06:14 -0800178 // The raw value is actually 12 bits, not 16, so we need to propagate the
Brian Silverman8fce7482020-01-05 13:18:21 -0800179 // 2's complement sign bit to the unused 4 bits for this to work with
180 // negative numbers.
181 raw <<= 4;
182 raw >>= 4;
183
184 switch (accelerometerRange) {
185 case HAL_AccelerometerRange_k2G:
186 return raw / 1024.0;
187 case HAL_AccelerometerRange_k4G:
188 return raw / 512.0;
189 case HAL_AccelerometerRange_k8G:
190 return raw / 256.0;
191 default:
192 return 0.0;
193 }
194}
195
196} // namespace hal
197
198extern "C" {
199
200void HAL_SetAccelerometerActive(HAL_Bool active) {
201 initializeAccelerometer();
202
203 uint8_t ctrlReg1 = readRegister(kReg_CtrlReg1);
204 ctrlReg1 &= ~1; // Clear the existing active bit
205 writeRegister(kReg_CtrlReg1, ctrlReg1 | (active ? 1 : 0));
206}
207
208void HAL_SetAccelerometerRange(HAL_AccelerometerRange range) {
209 initializeAccelerometer();
210
211 accelerometerRange = range;
212
213 uint8_t xyzDataCfg = readRegister(kReg_XYZDataCfg);
214 xyzDataCfg &= ~3; // Clear the existing two range bits
215 writeRegister(kReg_XYZDataCfg, xyzDataCfg | range);
216}
217
218double HAL_GetAccelerometerX(void) {
219 initializeAccelerometer();
220
221 int32_t raw =
222 (readRegister(kReg_OutXMSB) << 4) | (readRegister(kReg_OutXLSB) >> 4);
223 return unpackAxis(raw);
224}
225
226double HAL_GetAccelerometerY(void) {
227 initializeAccelerometer();
228
229 int32_t raw =
230 (readRegister(kReg_OutYMSB) << 4) | (readRegister(kReg_OutYLSB) >> 4);
231 return unpackAxis(raw);
232}
233
234double HAL_GetAccelerometerZ(void) {
235 initializeAccelerometer();
236
237 int32_t raw =
238 (readRegister(kReg_OutZMSB) << 4) | (readRegister(kReg_OutZLSB) >> 4);
239 return unpackAxis(raw);
240}
241
242} // extern "C"