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