blob: 8dca72caa5ba87d6920458dd16607488bdd2935d [file] [log] [blame]
Brian Silvermanb5b46ca2016-03-13 01:14:17 -05001#include "frc971/wpilib/dma.h"
Brian Silverman2aa83d72015-01-24 18:03:11 -05002
Austin Schuh58d8cdf2015-02-15 21:04:42 -08003#include <string.h>
4
Brian Silverman2aa83d72015-01-24 18:03:11 -05005#include <algorithm>
Brian Silvermand49fd782015-01-30 16:43:17 -05006#include <type_traits>
7
8#include "DigitalSource.h"
9#include "AnalogInput.h"
10#include "Encoder.h"
Brian Silvermane48dbc12017-02-04 20:06:29 -080011#include "HAL/HAL.h"
Brian Silvermand49fd782015-01-30 16:43:17 -050012
13// Interface to the roboRIO FPGA's DMA features.
Brian Silverman2aa83d72015-01-24 18:03:11 -050014
15// Like tEncoder::tOutput with the bitfields reversed.
16typedef union {
17 struct {
18 unsigned Direction: 1;
19 signed Value: 31;
20 };
21 struct {
22 unsigned value: 32;
23 };
24} t1Output;
25
Brian Silvermane48dbc12017-02-04 20:06:29 -080026static const int32_t kNumHeaders = 10;
Brian Silverman2aa83d72015-01-24 18:03:11 -050027
Austin Schuh91c75562015-12-20 22:23:10 -080028static constexpr ssize_t kChannelSize[20] = {2, 2, 4, 4, 2, 2, 4, 4, 3, 3,
29 2, 1, 4, 4, 4, 4, 4, 4, 4, 4};
Brian Silvermane48dbc12017-02-04 20:06:29 -080030
Brian Silverman2aa83d72015-01-24 18:03:11 -050031enum DMAOffsetConstants {
32 kEnable_AI0_Low = 0,
33 kEnable_AI0_High = 1,
34 kEnable_AIAveraged0_Low = 2,
35 kEnable_AIAveraged0_High = 3,
36 kEnable_AI1_Low = 4,
37 kEnable_AI1_High = 5,
38 kEnable_AIAveraged1_Low = 6,
39 kEnable_AIAveraged1_High = 7,
40 kEnable_Accumulator0 = 8,
41 kEnable_Accumulator1 = 9,
42 kEnable_DI = 10,
43 kEnable_AnalogTriggers = 11,
44 kEnable_Counters_Low = 12,
45 kEnable_Counters_High = 13,
46 kEnable_CounterTimers_Low = 14,
47 kEnable_CounterTimers_High = 15,
Austin Schuh91c75562015-12-20 22:23:10 -080048 kEnable_Encoders_Low = 16,
49 kEnable_Encoders_High = 17,
50 kEnable_EncoderTimers_Low = 18,
51 kEnable_EncoderTimers_High = 19,
Brian Silverman2aa83d72015-01-24 18:03:11 -050052};
53
54DMA::DMA() {
55 tRioStatusCode status = 0;
56 tdma_config_ = tDMA::create(&status);
Austin Schuh58d8cdf2015-02-15 21:04:42 -080057 tdma_config_->writeConfig_ExternalClock(false, &status);
Brian Silvermane48dbc12017-02-04 20:06:29 -080058 wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
Brian Silverman2aa83d72015-01-24 18:03:11 -050059 if (status != 0) {
60 return;
61 }
62 SetRate(1);
63 SetPause(false);
64}
65
66DMA::~DMA() {
67 tRioStatusCode status = 0;
68
69 manager_->stop(&status);
70 delete tdma_config_;
71}
72
73void DMA::SetPause(bool pause) {
74 tRioStatusCode status = 0;
75 tdma_config_->writeConfig_Pause(pause, &status);
Brian Silvermane48dbc12017-02-04 20:06:29 -080076 wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
Brian Silverman2aa83d72015-01-24 18:03:11 -050077}
78
79void DMA::SetRate(uint32_t cycles) {
80 if (cycles < 1) {
81 cycles = 1;
82 }
83 tRioStatusCode status = 0;
84 tdma_config_->writeRate(cycles, &status);
Brian Silvermane48dbc12017-02-04 20:06:29 -080085 wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
Brian Silverman2aa83d72015-01-24 18:03:11 -050086}
87
Austin Schuh58d8cdf2015-02-15 21:04:42 -080088void DMA::Add(Encoder *encoder) {
Brian Silverman2aa83d72015-01-24 18:03:11 -050089 tRioStatusCode status = 0;
90
91 if (manager_) {
92 wpi_setErrorWithContext(NiFpga_Status_InvalidParameter,
93 "DMA::Add() only works before DMA::Start()");
94 return;
95 }
Austin Schuh91c75562015-12-20 22:23:10 -080096 const int index = encoder->GetFPGAIndex();
97
Austin Schuh91c75562015-12-20 22:23:10 -080098 if (index < 4) {
99 // TODO(austin): Encoder uses a Counter for 1x or 2x; quad for 4x...
100 tdma_config_->writeConfig_Enable_Encoders_Low(true, &status);
101 } else if (index < 8) {
102 // TODO(austin): Encoder uses a Counter for 1x or 2x; quad for 4x...
103 tdma_config_->writeConfig_Enable_Encoders_High(true, &status);
104 } else {
105 wpi_setErrorWithContext(
106 NiFpga_Status_InvalidParameter,
107 "FPGA encoder index is not in the 4 that get logged.");
108 return;
109 }
Brian Silverman2aa83d72015-01-24 18:03:11 -0500110
Brian Silvermane48dbc12017-02-04 20:06:29 -0800111 wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
Brian Silverman2aa83d72015-01-24 18:03:11 -0500112}
113
114void DMA::Add(DigitalSource * /*input*/) {
115 tRioStatusCode status = 0;
116
117 if (manager_) {
118 wpi_setErrorWithContext(NiFpga_Status_InvalidParameter,
119 "DMA::Add() only works before DMA::Start()");
120 return;
121 }
122
123 tdma_config_->writeConfig_Enable_DI(true, &status);
Brian Silvermane48dbc12017-02-04 20:06:29 -0800124 wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
Brian Silverman2aa83d72015-01-24 18:03:11 -0500125}
126
Brian Silvermand49fd782015-01-30 16:43:17 -0500127void DMA::Add(AnalogInput *input) {
128 tRioStatusCode status = 0;
129
130 if (manager_) {
131 wpi_setErrorWithContext(NiFpga_Status_InvalidParameter,
132 "DMA::Add() only works before DMA::Start()");
133 return;
134 }
135
Brian Silvermand49fd782015-01-30 16:43:17 -0500136 if (input->GetChannel() <= 3) {
137 tdma_config_->writeConfig_Enable_AI0_Low(true, &status);
138 } else {
139 tdma_config_->writeConfig_Enable_AI0_High(true, &status);
140 }
Brian Silvermane48dbc12017-02-04 20:06:29 -0800141 wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
Brian Silvermand49fd782015-01-30 16:43:17 -0500142}
143
Brian Silverman2aa83d72015-01-24 18:03:11 -0500144void DMA::SetExternalTrigger(DigitalSource *input, bool rising, bool falling) {
145 tRioStatusCode status = 0;
146
147 if (manager_) {
148 wpi_setErrorWithContext(NiFpga_Status_InvalidParameter,
149 "DMA::SetExternalTrigger() only works before DMA::Start()");
150 return;
151 }
152
153 auto index =
154 ::std::find(trigger_channels_.begin(), trigger_channels_.end(), false);
155 if (index == trigger_channels_.end()) {
156 wpi_setErrorWithContext(NiFpga_Status_InvalidParameter,
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800157 "DMA: No channels left");
Brian Silverman2aa83d72015-01-24 18:03:11 -0500158 return;
159 }
160 *index = true;
161
Austin Schuhb19fddb2015-11-22 22:25:29 -0800162 const int channel_index = ::std::distance(trigger_channels_.begin(), index);
Brian Silverman2aa83d72015-01-24 18:03:11 -0500163
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800164 const bool is_external_clock =
165 tdma_config_->readConfig_ExternalClock(&status);
166 if (status == 0) {
167 if (!is_external_clock) {
168 tdma_config_->writeConfig_ExternalClock(true, &status);
Brian Silvermane48dbc12017-02-04 20:06:29 -0800169 wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800170 if (status != 0) {
171 return;
172 }
173 }
174 } else {
Brian Silvermane48dbc12017-02-04 20:06:29 -0800175 wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
Brian Silverman2aa83d72015-01-24 18:03:11 -0500176 return;
177 }
178
Austin Schuh3b5b69b2015-10-31 18:55:47 -0700179 nFPGA::nRoboRIO_FPGANamespace::tDMA::tExternalTriggers new_trigger;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800180
181 new_trigger.FallingEdge = falling;
182 new_trigger.RisingEdge = rising;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800183 new_trigger.ExternalClockSource_AnalogTrigger = false;
Austin Schuh91c75562015-12-20 22:23:10 -0800184 unsigned char module = 0;
Austin Schuh94f51e92017-10-30 19:25:32 -0700185 uint32_t channel = input->GetChannel();
Austin Schuh91c75562015-12-20 22:23:10 -0800186 if (channel >= kNumHeaders) {
187 module = 1;
188 channel -= kNumHeaders;
189 } else {
190 module = 0;
191 }
192
193 new_trigger.ExternalClockSource_Module = module;
194 new_trigger.ExternalClockSource_Channel = channel;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800195
Austin Schuhb19fddb2015-11-22 22:25:29 -0800196// Configures the trigger to be external, not off the FPGA clock.
Austin Schuh91c75562015-12-20 22:23:10 -0800197 tdma_config_->writeExternalTriggers(channel_index / 4, channel_index % 4,
198 new_trigger, &status);
199 if (status != 0) {
Brian Silvermane48dbc12017-02-04 20:06:29 -0800200 wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
Austin Schuh91c75562015-12-20 22:23:10 -0800201 return;
202 }
Brian Silverman2aa83d72015-01-24 18:03:11 -0500203}
204
205DMA::ReadStatus DMA::Read(DMASample *sample, uint32_t timeout_ms,
Brian Silvermand49fd782015-01-30 16:43:17 -0500206 size_t *remaining_out) {
Brian Silverman2aa83d72015-01-24 18:03:11 -0500207 tRioStatusCode status = 0;
208 size_t remainingBytes = 0;
Brian Silvermand49fd782015-01-30 16:43:17 -0500209 *remaining_out = 0;
Brian Silverman2aa83d72015-01-24 18:03:11 -0500210
211 if (!manager_.get()) {
212 wpi_setErrorWithContext(NiFpga_Status_InvalidParameter,
213 "DMA::Read() only works after DMA::Start()");
214 return STATUS_ERROR;
215 }
216
Brian Silvermand49fd782015-01-30 16:43:17 -0500217 sample->dma_ = this;
Brian Silverman2aa83d72015-01-24 18:03:11 -0500218 manager_->read(sample->read_buffer_, capture_size_, timeout_ms,
219 &remainingBytes, &status);
Austin Schuh94f51e92017-10-30 19:25:32 -0700220 sample->CalculateTimestamp();
Brian Silverman2aa83d72015-01-24 18:03:11 -0500221
Brian Silverman2aa83d72015-01-24 18:03:11 -0500222 // TODO(jerry): Do this only if status == 0?
Brian Silvermand49fd782015-01-30 16:43:17 -0500223 *remaining_out = remainingBytes / capture_size_;
Brian Silverman2aa83d72015-01-24 18:03:11 -0500224
Brian Silverman2aa83d72015-01-24 18:03:11 -0500225 // TODO(austin): Check that *remainingBytes % capture_size_ == 0 and deal
226 // with it if it isn't. Probably meant that we overflowed?
227 if (status == 0) {
228 return STATUS_OK;
229 } else if (status == NiFpga_Status_FifoTimeout) {
230 return STATUS_TIMEOUT;
231 } else {
Brian Silvermane48dbc12017-02-04 20:06:29 -0800232 wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
Brian Silverman2aa83d72015-01-24 18:03:11 -0500233 return STATUS_ERROR;
234 }
235}
236
Brian Silvermand49fd782015-01-30 16:43:17 -0500237const char *DMA::NameOfReadStatus(ReadStatus s) {
238 switch (s) {
239 case STATUS_OK: return "OK";
240 case STATUS_TIMEOUT: return "TIMEOUT";
241 case STATUS_ERROR: return "ERROR";
242 default: return "(bad ReadStatus code)";
243 }
244}
245
Brian Silverman2aa83d72015-01-24 18:03:11 -0500246void DMA::Start(size_t queue_depth) {
247 tRioStatusCode status = 0;
248 tconfig_ = tdma_config_->readConfig(&status);
Brian Silvermane48dbc12017-02-04 20:06:29 -0800249 wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
Brian Silverman2aa83d72015-01-24 18:03:11 -0500250 if (status != 0) {
251 return;
252 }
253
254 {
255 size_t accum_size = 0;
256#define SET_SIZE(bit) \
257 if (tconfig_.bit) { \
258 channel_offsets_[k##bit] = accum_size; \
259 accum_size += kChannelSize[k##bit]; \
260 } else { \
261 channel_offsets_[k##bit] = -1; \
262 }
263
264 SET_SIZE(Enable_AI0_Low);
265 SET_SIZE(Enable_AI0_High);
266 SET_SIZE(Enable_AIAveraged0_Low);
267 SET_SIZE(Enable_AIAveraged0_High);
268 SET_SIZE(Enable_AI1_Low);
269 SET_SIZE(Enable_AI1_High);
270 SET_SIZE(Enable_AIAveraged1_Low);
271 SET_SIZE(Enable_AIAveraged1_High);
272 SET_SIZE(Enable_Accumulator0);
273 SET_SIZE(Enable_Accumulator1);
274 SET_SIZE(Enable_DI);
275 SET_SIZE(Enable_AnalogTriggers);
276 SET_SIZE(Enable_Counters_Low);
277 SET_SIZE(Enable_Counters_High);
278 SET_SIZE(Enable_CounterTimers_Low);
279 SET_SIZE(Enable_CounterTimers_High);
Austin Schuh91c75562015-12-20 22:23:10 -0800280 SET_SIZE(Enable_Encoders_Low);
281 SET_SIZE(Enable_Encoders_High);
282 SET_SIZE(Enable_EncoderTimers_Low);
283 SET_SIZE(Enable_EncoderTimers_High);
Brian Silverman2aa83d72015-01-24 18:03:11 -0500284#undef SET_SIZE
285 capture_size_ = accum_size + 1;
286 }
287
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800288 manager_.reset(
Austin Schuh94f51e92017-10-30 19:25:32 -0700289 new nFPGA::tDMAManager(1, queue_depth * capture_size_, &status));
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800290
Brian Silvermane48dbc12017-02-04 20:06:29 -0800291 wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
Brian Silverman2aa83d72015-01-24 18:03:11 -0500292 if (status != 0) {
293 return;
294 }
295 // Start, stop, start to clear the buffer.
296 manager_->start(&status);
Brian Silvermane48dbc12017-02-04 20:06:29 -0800297 wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
Brian Silverman2aa83d72015-01-24 18:03:11 -0500298 if (status != 0) {
299 return;
300 }
301 manager_->stop(&status);
Brian Silvermane48dbc12017-02-04 20:06:29 -0800302 wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
Brian Silverman2aa83d72015-01-24 18:03:11 -0500303 if (status != 0) {
304 return;
305 }
306 manager_->start(&status);
Brian Silvermane48dbc12017-02-04 20:06:29 -0800307 wpi_setErrorWithContext(status, HAL_GetErrorMessage(status));
Brian Silverman2aa83d72015-01-24 18:03:11 -0500308 if (status != 0) {
309 return;
310 }
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800311
Brian Silverman2aa83d72015-01-24 18:03:11 -0500312}
313
Brian Silvermand49fd782015-01-30 16:43:17 -0500314static_assert(::std::is_pod<DMASample>::value, "DMASample needs to be POD");
315
Austin Schuh94f51e92017-10-30 19:25:32 -0700316ssize_t DMASample::offset(int index) const {
317 return dma_->channel_offsets_[index];
318}
Brian Silverman2aa83d72015-01-24 18:03:11 -0500319
Austin Schuh94f51e92017-10-30 19:25:32 -0700320void DMASample::CalculateTimestamp() {
321 uint32_t lower_sample = read_buffer_[dma_->capture_size_ - 1];
322#if WPILIB2018
323 int32_t status = 0;
324 fpga_timestamp_ = HAL_ExpandFPGATime(lower_sample, &status);
325 assert(status == 0);
326#else
327 fpga_timestamp_ = lower_sample;
328#endif
329}
330
331uint64_t DMASample::GetTime() const {
332 return fpga_timestamp_;
Austin Schuh8f314a92015-11-22 21:35:40 -0800333}
334
Brian Silverman2aa83d72015-01-24 18:03:11 -0500335double DMASample::GetTimestamp() const {
Austin Schuh8f314a92015-11-22 21:35:40 -0800336 return static_cast<double>(GetTime()) * 0.000001;
Brian Silverman2aa83d72015-01-24 18:03:11 -0500337}
338
339bool DMASample::Get(DigitalSource *input) const {
340 if (offset(kEnable_DI) == -1) {
Austin Schuh91c75562015-12-20 22:23:10 -0800341 wpi_setStaticErrorWithContext(
342 dma_, NiFpga_Status_ResourceNotFound,
Brian Silvermane48dbc12017-02-04 20:06:29 -0800343 HAL_GetErrorMessage(NiFpga_Status_ResourceNotFound));
Brian Silverman2aa83d72015-01-24 18:03:11 -0500344 return false;
345 }
Austin Schuh94f51e92017-10-30 19:25:32 -0700346 const uint32_t channel = input->GetChannel();
Brian Silvermane48dbc12017-02-04 20:06:29 -0800347 if (channel < kNumHeaders) {
348 return (read_buffer_[offset(kEnable_DI)] >> channel) & 0x1;
Brian Silverman2aa83d72015-01-24 18:03:11 -0500349 } else {
Brian Silvermane48dbc12017-02-04 20:06:29 -0800350 return (read_buffer_[offset(kEnable_DI)] >> (channel + 6)) & 0x1;
Brian Silverman2aa83d72015-01-24 18:03:11 -0500351 }
352}
353
354int32_t DMASample::GetRaw(Encoder *input) const {
Austin Schuh91c75562015-12-20 22:23:10 -0800355 int index = input->GetFPGAIndex();
356 uint32_t dmaWord = 0;
Austin Schuh91c75562015-12-20 22:23:10 -0800357 if (index < 4) {
358 if (offset(kEnable_Encoders_Low) == -1) {
359 wpi_setStaticErrorWithContext(
360 dma_, NiFpga_Status_ResourceNotFound,
Brian Silvermane48dbc12017-02-04 20:06:29 -0800361 HAL_GetErrorMessage(NiFpga_Status_ResourceNotFound));
Austin Schuh91c75562015-12-20 22:23:10 -0800362 return -1;
363 }
364 dmaWord = read_buffer_[offset(kEnable_Encoders_Low) + index];
365 } else if (index < 8) {
366 if (offset(kEnable_Encoders_High) == -1) {
367 wpi_setStaticErrorWithContext(
368 dma_, NiFpga_Status_ResourceNotFound,
Brian Silvermane48dbc12017-02-04 20:06:29 -0800369 HAL_GetErrorMessage(NiFpga_Status_ResourceNotFound));
Austin Schuh91c75562015-12-20 22:23:10 -0800370 return -1;
371 }
372 dmaWord = read_buffer_[offset(kEnable_Encoders_High) + (index - 4)];
373 } else {
374 wpi_setStaticErrorWithContext(
375 dma_, NiFpga_Status_ResourceNotFound,
Brian Silvermane48dbc12017-02-04 20:06:29 -0800376 HAL_GetErrorMessage(NiFpga_Status_ResourceNotFound));
Austin Schuh91c75562015-12-20 22:23:10 -0800377 return 0;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800378 }
379
Brian Silverman2aa83d72015-01-24 18:03:11 -0500380 int32_t result = 0;
381
Austin Schuhb19fddb2015-11-22 22:25:29 -0800382 // Extract the 31-bit signed tEncoder::tOutput Value using a struct with the
383 // reverse packed field order of tOutput. This gets Value from the high
384 // order 31 bits of output on little-endian ARM using gcc. This works
385 // even though C/C++ doesn't guarantee bitfield order.
386 t1Output output;
Brian Silverman2aa83d72015-01-24 18:03:11 -0500387
Austin Schuhb19fddb2015-11-22 22:25:29 -0800388 output.value = dmaWord;
389 result = output.Value;
Brian Silverman2aa83d72015-01-24 18:03:11 -0500390
391 return result;
392}
393
394int32_t DMASample::Get(Encoder *input) const {
395 int32_t raw = GetRaw(input);
396
Brian Silvermand49fd782015-01-30 16:43:17 -0500397 return raw / input->GetEncodingScale();
398}
399
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800400uint16_t DMASample::GetValue(AnalogInput *input) const {
Austin Schuh91c75562015-12-20 22:23:10 -0800401 uint32_t channel = input->GetChannel();
402 uint32_t dmaWord;
403 if (channel < 4) {
404 if (offset(kEnable_AI0_Low) == -1) {
405 wpi_setStaticErrorWithContext(
406 dma_, NiFpga_Status_ResourceNotFound,
Brian Silvermane48dbc12017-02-04 20:06:29 -0800407 HAL_GetErrorMessage(NiFpga_Status_ResourceNotFound));
Austin Schuh91c75562015-12-20 22:23:10 -0800408 return 0xffff;
409 }
410 dmaWord = read_buffer_[offset(kEnable_AI0_Low) + channel / 2];
411 } else if (channel < 8) {
412 if (offset(kEnable_AI0_High) == -1) {
413 wpi_setStaticErrorWithContext(
414 dma_, NiFpga_Status_ResourceNotFound,
Brian Silvermane48dbc12017-02-04 20:06:29 -0800415 HAL_GetErrorMessage(NiFpga_Status_ResourceNotFound));
Austin Schuh91c75562015-12-20 22:23:10 -0800416 return 0xffff;
417 }
418 dmaWord = read_buffer_[offset(kEnable_AI0_High) + (channel - 4) / 2];
419 } else {
420 wpi_setStaticErrorWithContext(
421 dma_, NiFpga_Status_ResourceNotFound,
Brian Silvermane48dbc12017-02-04 20:06:29 -0800422 HAL_GetErrorMessage(NiFpga_Status_ResourceNotFound));
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800423 return 0xffff;
Brian Silvermand49fd782015-01-30 16:43:17 -0500424 }
Austin Schuhc6cc4102015-02-15 23:19:53 -0800425 if (channel % 2) {
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800426 return (dmaWord >> 16) & 0xffff;
427 } else {
Austin Schuh91c75562015-12-20 22:23:10 -0800428 return dmaWord & 0xffff;
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800429 }
Brian Silvermand49fd782015-01-30 16:43:17 -0500430 return static_cast<int16_t>(dmaWord);
431}
432
433float DMASample::GetVoltage(AnalogInput *input) const {
Austin Schuh58d8cdf2015-02-15 21:04:42 -0800434 uint16_t value = GetValue(input);
435 if (value == 0xffff) return 0.0;
Brian Silvermand49fd782015-01-30 16:43:17 -0500436 uint32_t lsb_weight = input->GetLSBWeight();
437 int32_t offset = input->GetOffset();
438 float voltage = lsb_weight * 1.0e-9 * value - offset * 1.0e-9;
439 return voltage;
Brian Silverman2aa83d72015-01-24 18:03:11 -0500440}