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