Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 1 | #include "dma.h" |
| 2 | |
Austin Schuh | 58d8cdf | 2015-02-15 21:04:42 -0800 | [diff] [blame] | 3 | #include <string.h> |
| 4 | |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 5 | #include <algorithm> |
Brian Silverman | d49fd78 | 2015-01-30 16:43:17 -0500 | [diff] [blame] | 6 | #include <type_traits> |
| 7 | |
| 8 | #include "DigitalSource.h" |
| 9 | #include "AnalogInput.h" |
| 10 | #include "Encoder.h" |
| 11 | |
Austin Schuh | 58d8cdf | 2015-02-15 21:04:42 -0800 | [diff] [blame] | 12 | |
Brian Silverman | d49fd78 | 2015-01-30 16:43:17 -0500 | [diff] [blame] | 13 | // Interface to the roboRIO FPGA's DMA features. |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 14 | |
| 15 | // Like tEncoder::tOutput with the bitfields reversed. |
| 16 | typedef union { |
| 17 | struct { |
| 18 | unsigned Direction: 1; |
| 19 | signed Value: 31; |
| 20 | }; |
| 21 | struct { |
| 22 | unsigned value: 32; |
| 23 | }; |
| 24 | } t1Output; |
| 25 | |
| 26 | static const uint32_t kNumHeaders = 10; |
| 27 | |
| 28 | static constexpr ssize_t kChannelSize[18] = {2, 2, 4, 4, 2, 2, 4, 4, 3, |
| 29 | 3, 2, 1, 4, 4, 4, 4, 4, 4}; |
| 30 | |
| 31 | enum 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, |
| 48 | kEnable_Encoders = 16, |
| 49 | kEnable_EncoderTimers = 17, |
| 50 | }; |
| 51 | |
| 52 | DMA::DMA() { |
| 53 | tRioStatusCode status = 0; |
| 54 | tdma_config_ = tDMA::create(&status); |
Austin Schuh | 58d8cdf | 2015-02-15 21:04:42 -0800 | [diff] [blame] | 55 | tdma_config_->writeConfig_ExternalClock(false, &status); |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 56 | wpi_setErrorWithContext(status, getHALErrorMessage(status)); |
Austin Schuh | b19fddb | 2015-11-22 22:25:29 -0800 | [diff] [blame] | 57 | #ifdef WPILIB2015 |
Austin Schuh | 58d8cdf | 2015-02-15 21:04:42 -0800 | [diff] [blame] | 58 | NiFpga_WriteU32(0x10000, 0x1832c, 0x0); |
Austin Schuh | b19fddb | 2015-11-22 22:25:29 -0800 | [diff] [blame] | 59 | #endif |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 60 | if (status != 0) { |
| 61 | return; |
| 62 | } |
| 63 | SetRate(1); |
| 64 | SetPause(false); |
| 65 | } |
| 66 | |
| 67 | DMA::~DMA() { |
| 68 | tRioStatusCode status = 0; |
| 69 | |
| 70 | manager_->stop(&status); |
| 71 | delete tdma_config_; |
| 72 | } |
| 73 | |
| 74 | void DMA::SetPause(bool pause) { |
| 75 | tRioStatusCode status = 0; |
| 76 | tdma_config_->writeConfig_Pause(pause, &status); |
| 77 | wpi_setErrorWithContext(status, getHALErrorMessage(status)); |
| 78 | } |
| 79 | |
| 80 | void DMA::SetRate(uint32_t cycles) { |
| 81 | if (cycles < 1) { |
| 82 | cycles = 1; |
| 83 | } |
| 84 | tRioStatusCode status = 0; |
| 85 | tdma_config_->writeRate(cycles, &status); |
| 86 | wpi_setErrorWithContext(status, getHALErrorMessage(status)); |
| 87 | } |
| 88 | |
Austin Schuh | 58d8cdf | 2015-02-15 21:04:42 -0800 | [diff] [blame] | 89 | void DMA::Add(Encoder *encoder) { |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 90 | tRioStatusCode status = 0; |
| 91 | |
| 92 | if (manager_) { |
| 93 | wpi_setErrorWithContext(NiFpga_Status_InvalidParameter, |
| 94 | "DMA::Add() only works before DMA::Start()"); |
| 95 | return; |
| 96 | } |
Austin Schuh | 58d8cdf | 2015-02-15 21:04:42 -0800 | [diff] [blame] | 97 | if (encoder->GetFPGAIndex() >= 4) { |
| 98 | wpi_setErrorWithContext( |
| 99 | NiFpga_Status_InvalidParameter, |
| 100 | "FPGA encoder index is not in the 4 that get logged."); |
| 101 | } |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 102 | |
| 103 | // TODO(austin): Encoder uses a Counter for 1x or 2x; quad for 4x... |
| 104 | tdma_config_->writeConfig_Enable_Encoders(true, &status); |
| 105 | wpi_setErrorWithContext(status, getHALErrorMessage(status)); |
| 106 | } |
| 107 | |
| 108 | void DMA::Add(DigitalSource * /*input*/) { |
| 109 | tRioStatusCode status = 0; |
| 110 | |
| 111 | if (manager_) { |
| 112 | wpi_setErrorWithContext(NiFpga_Status_InvalidParameter, |
| 113 | "DMA::Add() only works before DMA::Start()"); |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | tdma_config_->writeConfig_Enable_DI(true, &status); |
| 118 | wpi_setErrorWithContext(status, getHALErrorMessage(status)); |
| 119 | } |
| 120 | |
Brian Silverman | d49fd78 | 2015-01-30 16:43:17 -0500 | [diff] [blame] | 121 | void DMA::Add(AnalogInput *input) { |
| 122 | tRioStatusCode status = 0; |
| 123 | |
| 124 | if (manager_) { |
| 125 | wpi_setErrorWithContext(NiFpga_Status_InvalidParameter, |
| 126 | "DMA::Add() only works before DMA::Start()"); |
| 127 | return; |
| 128 | } |
| 129 | |
Brian Silverman | d49fd78 | 2015-01-30 16:43:17 -0500 | [diff] [blame] | 130 | if (input->GetChannel() <= 3) { |
| 131 | tdma_config_->writeConfig_Enable_AI0_Low(true, &status); |
| 132 | } else { |
| 133 | tdma_config_->writeConfig_Enable_AI0_High(true, &status); |
| 134 | } |
| 135 | wpi_setErrorWithContext(status, getHALErrorMessage(status)); |
| 136 | } |
| 137 | |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 138 | void DMA::SetExternalTrigger(DigitalSource *input, bool rising, bool falling) { |
| 139 | tRioStatusCode status = 0; |
| 140 | |
| 141 | if (manager_) { |
| 142 | wpi_setErrorWithContext(NiFpga_Status_InvalidParameter, |
| 143 | "DMA::SetExternalTrigger() only works before DMA::Start()"); |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | auto index = |
| 148 | ::std::find(trigger_channels_.begin(), trigger_channels_.end(), false); |
| 149 | if (index == trigger_channels_.end()) { |
| 150 | wpi_setErrorWithContext(NiFpga_Status_InvalidParameter, |
Austin Schuh | 58d8cdf | 2015-02-15 21:04:42 -0800 | [diff] [blame] | 151 | "DMA: No channels left"); |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 152 | return; |
| 153 | } |
| 154 | *index = true; |
| 155 | |
Austin Schuh | b19fddb | 2015-11-22 22:25:29 -0800 | [diff] [blame] | 156 | const int channel_index = ::std::distance(trigger_channels_.begin(), index); |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 157 | |
Austin Schuh | 58d8cdf | 2015-02-15 21:04:42 -0800 | [diff] [blame] | 158 | const bool is_external_clock = |
| 159 | tdma_config_->readConfig_ExternalClock(&status); |
| 160 | if (status == 0) { |
| 161 | if (!is_external_clock) { |
| 162 | tdma_config_->writeConfig_ExternalClock(true, &status); |
| 163 | wpi_setErrorWithContext(status, getHALErrorMessage(status)); |
| 164 | if (status != 0) { |
| 165 | return; |
| 166 | } |
| 167 | } |
| 168 | } else { |
| 169 | wpi_setErrorWithContext(status, getHALErrorMessage(status)); |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 170 | return; |
| 171 | } |
| 172 | |
Austin Schuh | 3b5b69b | 2015-10-31 18:55:47 -0700 | [diff] [blame] | 173 | nFPGA::nRoboRIO_FPGANamespace::tDMA::tExternalTriggers new_trigger; |
Austin Schuh | 58d8cdf | 2015-02-15 21:04:42 -0800 | [diff] [blame] | 174 | |
| 175 | new_trigger.FallingEdge = falling; |
| 176 | new_trigger.RisingEdge = rising; |
| 177 | new_trigger.ExternalClockSource_AnalogTrigger = |
| 178 | input->GetAnalogTriggerForRouting(); |
| 179 | new_trigger.ExternalClockSource_AnalogTrigger = false; |
| 180 | new_trigger.ExternalClockSource_Module = input->GetModuleForRouting(); |
| 181 | new_trigger.ExternalClockSource_Channel = input->GetChannelForRouting(); |
| 182 | |
Austin Schuh | b19fddb | 2015-11-22 22:25:29 -0800 | [diff] [blame] | 183 | // Configures the trigger to be external, not off the FPGA clock. |
| 184 | #ifndef WPILIB2015 |
Austin Schuh | 58d8cdf | 2015-02-15 21:04:42 -0800 | [diff] [blame] | 185 | tdma_config_->writeExternalTriggers(channel_index, new_trigger, &status); |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 186 | wpi_setErrorWithContext(status, getHALErrorMessage(status)); |
Austin Schuh | b19fddb | 2015-11-22 22:25:29 -0800 | [diff] [blame] | 187 | #else |
Austin Schuh | 58d8cdf | 2015-02-15 21:04:42 -0800 | [diff] [blame] | 188 | uint32_t current_triggers; |
Austin Schuh | b19fddb | 2015-11-22 22:25:29 -0800 | [diff] [blame] | 189 | tRioStatusCode register_status = |
| 190 | NiFpga_ReadU32(0x10000, 0x1832c, ¤t_triggers); |
Austin Schuh | 58d8cdf | 2015-02-15 21:04:42 -0800 | [diff] [blame] | 191 | if (register_status != 0) { |
| 192 | wpi_setErrorWithContext(register_status, getHALErrorMessage(status)); |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 193 | return; |
| 194 | } |
Austin Schuh | 58d8cdf | 2015-02-15 21:04:42 -0800 | [diff] [blame] | 195 | current_triggers = (current_triggers & ~(0xff << (channel_index * 8))) | |
| 196 | (new_trigger.value << (channel_index * 8)); |
| 197 | register_status = NiFpga_WriteU32(0x10000, 0x1832c, current_triggers); |
| 198 | if (register_status != 0) { |
| 199 | wpi_setErrorWithContext(register_status, getHALErrorMessage(status)); |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 200 | return; |
| 201 | } |
Austin Schuh | b19fddb | 2015-11-22 22:25:29 -0800 | [diff] [blame] | 202 | #endif |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | DMA::ReadStatus DMA::Read(DMASample *sample, uint32_t timeout_ms, |
Brian Silverman | d49fd78 | 2015-01-30 16:43:17 -0500 | [diff] [blame] | 206 | size_t *remaining_out) { |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 207 | tRioStatusCode status = 0; |
| 208 | size_t remainingBytes = 0; |
Brian Silverman | d49fd78 | 2015-01-30 16:43:17 -0500 | [diff] [blame] | 209 | *remaining_out = 0; |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 210 | |
| 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 Silverman | d49fd78 | 2015-01-30 16:43:17 -0500 | [diff] [blame] | 217 | sample->dma_ = this; |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 218 | manager_->read(sample->read_buffer_, capture_size_, timeout_ms, |
| 219 | &remainingBytes, &status); |
| 220 | |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 221 | // TODO(jerry): Do this only if status == 0? |
Brian Silverman | d49fd78 | 2015-01-30 16:43:17 -0500 | [diff] [blame] | 222 | *remaining_out = remainingBytes / capture_size_; |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 223 | |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 224 | // TODO(austin): Check that *remainingBytes % capture_size_ == 0 and deal |
| 225 | // with it if it isn't. Probably meant that we overflowed? |
| 226 | if (status == 0) { |
| 227 | return STATUS_OK; |
| 228 | } else if (status == NiFpga_Status_FifoTimeout) { |
| 229 | return STATUS_TIMEOUT; |
| 230 | } else { |
| 231 | wpi_setErrorWithContext(status, getHALErrorMessage(status)); |
| 232 | return STATUS_ERROR; |
| 233 | } |
| 234 | } |
| 235 | |
Brian Silverman | d49fd78 | 2015-01-30 16:43:17 -0500 | [diff] [blame] | 236 | const char *DMA::NameOfReadStatus(ReadStatus s) { |
| 237 | switch (s) { |
| 238 | case STATUS_OK: return "OK"; |
| 239 | case STATUS_TIMEOUT: return "TIMEOUT"; |
| 240 | case STATUS_ERROR: return "ERROR"; |
| 241 | default: return "(bad ReadStatus code)"; |
| 242 | } |
| 243 | } |
| 244 | |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 245 | void DMA::Start(size_t queue_depth) { |
| 246 | tRioStatusCode status = 0; |
| 247 | tconfig_ = tdma_config_->readConfig(&status); |
| 248 | wpi_setErrorWithContext(status, getHALErrorMessage(status)); |
| 249 | if (status != 0) { |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | { |
| 254 | size_t accum_size = 0; |
| 255 | #define SET_SIZE(bit) \ |
| 256 | if (tconfig_.bit) { \ |
| 257 | channel_offsets_[k##bit] = accum_size; \ |
| 258 | accum_size += kChannelSize[k##bit]; \ |
| 259 | } else { \ |
| 260 | channel_offsets_[k##bit] = -1; \ |
| 261 | } |
| 262 | |
| 263 | SET_SIZE(Enable_AI0_Low); |
| 264 | SET_SIZE(Enable_AI0_High); |
| 265 | SET_SIZE(Enable_AIAveraged0_Low); |
| 266 | SET_SIZE(Enable_AIAveraged0_High); |
| 267 | SET_SIZE(Enable_AI1_Low); |
| 268 | SET_SIZE(Enable_AI1_High); |
| 269 | SET_SIZE(Enable_AIAveraged1_Low); |
| 270 | SET_SIZE(Enable_AIAveraged1_High); |
| 271 | SET_SIZE(Enable_Accumulator0); |
| 272 | SET_SIZE(Enable_Accumulator1); |
| 273 | SET_SIZE(Enable_DI); |
| 274 | SET_SIZE(Enable_AnalogTriggers); |
| 275 | SET_SIZE(Enable_Counters_Low); |
| 276 | SET_SIZE(Enable_Counters_High); |
| 277 | SET_SIZE(Enable_CounterTimers_Low); |
| 278 | SET_SIZE(Enable_CounterTimers_High); |
| 279 | SET_SIZE(Enable_Encoders); |
| 280 | SET_SIZE(Enable_EncoderTimers); |
| 281 | #undef SET_SIZE |
| 282 | capture_size_ = accum_size + 1; |
| 283 | } |
| 284 | |
Austin Schuh | 58d8cdf | 2015-02-15 21:04:42 -0800 | [diff] [blame] | 285 | manager_.reset( |
| 286 | new nFPGA::tDMAManager(0, queue_depth * capture_size_, &status)); |
| 287 | |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 288 | wpi_setErrorWithContext(status, getHALErrorMessage(status)); |
| 289 | if (status != 0) { |
| 290 | return; |
| 291 | } |
| 292 | // Start, stop, start to clear the buffer. |
| 293 | manager_->start(&status); |
| 294 | wpi_setErrorWithContext(status, getHALErrorMessage(status)); |
| 295 | if (status != 0) { |
| 296 | return; |
| 297 | } |
| 298 | manager_->stop(&status); |
| 299 | wpi_setErrorWithContext(status, getHALErrorMessage(status)); |
| 300 | if (status != 0) { |
| 301 | return; |
| 302 | } |
| 303 | manager_->start(&status); |
| 304 | wpi_setErrorWithContext(status, getHALErrorMessage(status)); |
| 305 | if (status != 0) { |
| 306 | return; |
| 307 | } |
Austin Schuh | 58d8cdf | 2015-02-15 21:04:42 -0800 | [diff] [blame] | 308 | |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 309 | } |
| 310 | |
Brian Silverman | d49fd78 | 2015-01-30 16:43:17 -0500 | [diff] [blame] | 311 | static_assert(::std::is_pod<DMASample>::value, "DMASample needs to be POD"); |
| 312 | |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 313 | ssize_t DMASample::offset(int index) const { return dma_->channel_offsets_[index]; } |
| 314 | |
Austin Schuh | 8f314a9 | 2015-11-22 21:35:40 -0800 | [diff] [blame] | 315 | uint32_t DMASample::GetTime() const { |
| 316 | return read_buffer_[dma_->capture_size_ - 1]; |
| 317 | } |
| 318 | |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 319 | double DMASample::GetTimestamp() const { |
Austin Schuh | 8f314a9 | 2015-11-22 21:35:40 -0800 | [diff] [blame] | 320 | return static_cast<double>(GetTime()) * 0.000001; |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 321 | } |
| 322 | |
| 323 | bool DMASample::Get(DigitalSource *input) const { |
| 324 | if (offset(kEnable_DI) == -1) { |
| 325 | wpi_setStaticErrorWithContext(dma_, |
| 326 | NiFpga_Status_ResourceNotFound, |
| 327 | getHALErrorMessage(NiFpga_Status_ResourceNotFound)); |
| 328 | return false; |
| 329 | } |
| 330 | if (input->GetChannelForRouting() < kNumHeaders) { |
| 331 | return (read_buffer_[offset(kEnable_DI)] >> |
| 332 | input->GetChannelForRouting()) & |
| 333 | 0x1; |
| 334 | } else { |
| 335 | return (read_buffer_[offset(kEnable_DI)] >> |
| 336 | (input->GetChannelForRouting() + 6)) & |
| 337 | 0x1; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | int32_t DMASample::GetRaw(Encoder *input) const { |
| 342 | if (offset(kEnable_Encoders) == -1) { |
| 343 | wpi_setStaticErrorWithContext(dma_, |
| 344 | NiFpga_Status_ResourceNotFound, |
| 345 | getHALErrorMessage(NiFpga_Status_ResourceNotFound)); |
| 346 | return -1; |
| 347 | } |
| 348 | |
Austin Schuh | c6cc410 | 2015-02-15 23:19:53 -0800 | [diff] [blame] | 349 | if (input->GetFPGAIndex() >= 4) { |
Austin Schuh | 58d8cdf | 2015-02-15 21:04:42 -0800 | [diff] [blame] | 350 | wpi_setStaticErrorWithContext(dma_, |
| 351 | NiFpga_Status_ResourceNotFound, |
| 352 | getHALErrorMessage(NiFpga_Status_ResourceNotFound)); |
| 353 | } |
| 354 | |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 355 | uint32_t dmaWord = |
| 356 | read_buffer_[offset(kEnable_Encoders) + input->GetFPGAIndex()]; |
| 357 | int32_t result = 0; |
| 358 | |
Austin Schuh | b19fddb | 2015-11-22 22:25:29 -0800 | [diff] [blame] | 359 | // Extract the 31-bit signed tEncoder::tOutput Value using a struct with the |
| 360 | // reverse packed field order of tOutput. This gets Value from the high |
| 361 | // order 31 bits of output on little-endian ARM using gcc. This works |
| 362 | // even though C/C++ doesn't guarantee bitfield order. |
| 363 | t1Output output; |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 364 | |
Austin Schuh | b19fddb | 2015-11-22 22:25:29 -0800 | [diff] [blame] | 365 | output.value = dmaWord; |
| 366 | result = output.Value; |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 367 | |
| 368 | return result; |
| 369 | } |
| 370 | |
| 371 | int32_t DMASample::Get(Encoder *input) const { |
| 372 | int32_t raw = GetRaw(input); |
| 373 | |
Brian Silverman | d49fd78 | 2015-01-30 16:43:17 -0500 | [diff] [blame] | 374 | return raw / input->GetEncodingScale(); |
| 375 | } |
| 376 | |
Austin Schuh | 58d8cdf | 2015-02-15 21:04:42 -0800 | [diff] [blame] | 377 | uint16_t DMASample::GetValue(AnalogInput *input) const { |
Brian Silverman | d49fd78 | 2015-01-30 16:43:17 -0500 | [diff] [blame] | 378 | if (offset(kEnable_Encoders) == -1) { |
| 379 | wpi_setStaticErrorWithContext(dma_, |
| 380 | NiFpga_Status_ResourceNotFound, |
| 381 | getHALErrorMessage(NiFpga_Status_ResourceNotFound)); |
Austin Schuh | 58d8cdf | 2015-02-15 21:04:42 -0800 | [diff] [blame] | 382 | return 0xffff; |
Brian Silverman | d49fd78 | 2015-01-30 16:43:17 -0500 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | uint32_t dmaWord; |
Austin Schuh | 58d8cdf | 2015-02-15 21:04:42 -0800 | [diff] [blame] | 386 | uint32_t channel = input->GetChannel(); |
Austin Schuh | c6cc410 | 2015-02-15 23:19:53 -0800 | [diff] [blame] | 387 | if (channel <= 3) { |
Austin Schuh | 58d8cdf | 2015-02-15 21:04:42 -0800 | [diff] [blame] | 388 | dmaWord = read_buffer_[offset(kEnable_AI0_Low) + channel / 2]; |
Brian Silverman | d49fd78 | 2015-01-30 16:43:17 -0500 | [diff] [blame] | 389 | } else { |
Austin Schuh | 58d8cdf | 2015-02-15 21:04:42 -0800 | [diff] [blame] | 390 | dmaWord = read_buffer_[offset(kEnable_AI0_High) + (channel - 4) / 2]; |
Brian Silverman | d49fd78 | 2015-01-30 16:43:17 -0500 | [diff] [blame] | 391 | } |
Austin Schuh | c6cc410 | 2015-02-15 23:19:53 -0800 | [diff] [blame] | 392 | if (channel % 2) { |
Austin Schuh | 58d8cdf | 2015-02-15 21:04:42 -0800 | [diff] [blame] | 393 | return (dmaWord >> 16) & 0xffff; |
| 394 | } else { |
| 395 | return (dmaWord) & 0xffff; |
| 396 | } |
Brian Silverman | d49fd78 | 2015-01-30 16:43:17 -0500 | [diff] [blame] | 397 | return static_cast<int16_t>(dmaWord); |
| 398 | } |
| 399 | |
| 400 | float DMASample::GetVoltage(AnalogInput *input) const { |
Austin Schuh | 58d8cdf | 2015-02-15 21:04:42 -0800 | [diff] [blame] | 401 | uint16_t value = GetValue(input); |
| 402 | if (value == 0xffff) return 0.0; |
Brian Silverman | d49fd78 | 2015-01-30 16:43:17 -0500 | [diff] [blame] | 403 | uint32_t lsb_weight = input->GetLSBWeight(); |
| 404 | int32_t offset = input->GetOffset(); |
| 405 | float voltage = lsb_weight * 1.0e-9 * value - offset * 1.0e-9; |
| 406 | return voltage; |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 407 | } |