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