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