Brian Silverman | b5b46ca | 2016-03-13 01:14:17 -0500 | [diff] [blame] | 1 | #ifndef FRC971_WPILIB_DMA_H_ |
| 2 | #define FRC971_WPILIB_DMA_H_ |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 3 | |
Brian Silverman | d49fd78 | 2015-01-30 16:43:17 -0500 | [diff] [blame] | 4 | // Interface to the roboRIO FPGA's DMA features. |
Brian Silverman | b5b46ca | 2016-03-13 01:14:17 -0500 | [diff] [blame] | 5 | // TODO(Brian): Make this less wpilib-like and more frc971-like. |
Brian Silverman | d49fd78 | 2015-01-30 16:43:17 -0500 | [diff] [blame] | 6 | |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
| 9 | #include <array> |
| 10 | #include <memory> |
| 11 | |
Austin Schuh | 94f51e9 | 2017-10-30 19:25:32 -0700 | [diff] [blame^] | 12 | #if defined(WPILIB2017) || defined(WPILIB2018) |
Brian Silverman | e48dbc1 | 2017-02-04 20:06:29 -0800 | [diff] [blame] | 13 | #include "HAL/ChipObject.h" |
| 14 | #else |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 15 | #include "ChipObject.h" |
Brian Silverman | e48dbc1 | 2017-02-04 20:06:29 -0800 | [diff] [blame] | 16 | #endif |
Brian Silverman | d49fd78 | 2015-01-30 16:43:17 -0500 | [diff] [blame] | 17 | #include "ErrorBase.h" |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 18 | |
| 19 | class DMA; |
Austin Schuh | 94f51e9 | 2017-10-30 19:25:32 -0700 | [diff] [blame^] | 20 | #if defined(WPILIB2017) || defined(WPILIB2018) |
Brian Silverman | e48dbc1 | 2017-02-04 20:06:29 -0800 | [diff] [blame] | 21 | namespace frc { |
Brian Silverman | d49fd78 | 2015-01-30 16:43:17 -0500 | [diff] [blame] | 22 | class DigitalSource; |
| 23 | class AnalogInput; |
| 24 | class Encoder; |
Brian Silverman | e48dbc1 | 2017-02-04 20:06:29 -0800 | [diff] [blame] | 25 | } // namespace frc |
| 26 | #else |
| 27 | class DigitalSource; |
| 28 | class AnalogInput; |
| 29 | class Encoder; |
| 30 | namespace frc { |
| 31 | using ::DigitalSource; |
| 32 | using ::AnalogInput; |
| 33 | using ::Encoder; |
| 34 | } // namespace frc |
| 35 | #endif |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 36 | |
Brian Silverman | d49fd78 | 2015-01-30 16:43:17 -0500 | [diff] [blame] | 37 | // A POD class which stores the data from a DMA sample and provides safe ways to |
| 38 | // access it. |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 39 | class DMASample { |
| 40 | public: |
Brian Silverman | d49fd78 | 2015-01-30 16:43:17 -0500 | [diff] [blame] | 41 | DMASample() = default; |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 42 | |
Austin Schuh | 8f314a9 | 2015-11-22 21:35:40 -0800 | [diff] [blame] | 43 | // Returns the FPGA timestamp of the sample in seconds. |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 44 | double GetTimestamp() const; |
Austin Schuh | 8f314a9 | 2015-11-22 21:35:40 -0800 | [diff] [blame] | 45 | // Returns the FPGA timestamp of the sample in microseconds. |
Austin Schuh | 94f51e9 | 2017-10-30 19:25:32 -0700 | [diff] [blame^] | 46 | uint64_t GetTime() const; |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 47 | |
| 48 | // All Get methods either return the requested value, or set the Error. |
| 49 | |
| 50 | // Returns the value of the digital input in the sample. |
| 51 | bool Get(DigitalSource *input) const; |
| 52 | // Returns the raw value of the encoder in the sample. |
| 53 | int32_t GetRaw(Encoder *input) const; |
| 54 | // Returns the {1, 2, or 4} X scaled value of the encoder in the sample. |
| 55 | int32_t Get(Encoder *input) const; |
Brian Silverman | d49fd78 | 2015-01-30 16:43:17 -0500 | [diff] [blame] | 56 | // Returns the raw 12-bit value from the ADC. |
Austin Schuh | 58d8cdf | 2015-02-15 21:04:42 -0800 | [diff] [blame] | 57 | uint16_t GetValue(AnalogInput *input) const; |
Brian Silverman | d49fd78 | 2015-01-30 16:43:17 -0500 | [diff] [blame] | 58 | // Returns the scaled value of an analog input. |
| 59 | float GetVoltage(AnalogInput *input) const; |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 60 | |
| 61 | private: |
| 62 | friend DMA; |
| 63 | |
Austin Schuh | 94f51e9 | 2017-10-30 19:25:32 -0700 | [diff] [blame^] | 64 | void CalculateTimestamp(); |
| 65 | |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 66 | // Returns the offset of the sample type in the buffer, or -1 if it isn't in |
| 67 | // the sample. |
| 68 | ssize_t offset(int index) const; |
| 69 | |
| 70 | // TODO(austin): This should be re-used from WPILib... Once I merge this back |
| 71 | // into WPILib. |
| 72 | |
| 73 | DMA *dma_; |
Austin Schuh | 94f51e9 | 2017-10-30 19:25:32 -0700 | [diff] [blame^] | 74 | uint64_t fpga_timestamp_; |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 75 | uint32_t read_buffer_[64]; |
| 76 | }; |
| 77 | |
Brian Silverman | d49fd78 | 2015-01-30 16:43:17 -0500 | [diff] [blame] | 78 | // TODO(austin): ErrorBase... |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 79 | class DMA : public ErrorBase { |
| 80 | public: |
| 81 | DMA(); |
| 82 | virtual ~DMA(); |
| 83 | |
| 84 | // Sets whether or not DMA is paused. |
Austin Schuh | 91c7556 | 2015-12-20 22:23:10 -0800 | [diff] [blame] | 85 | // If not specified, the default is false. |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 86 | void SetPause(bool pause); |
| 87 | |
| 88 | // Sets the number of triggers that need to occur before a sample is saved. |
Austin Schuh | 91c7556 | 2015-12-20 22:23:10 -0800 | [diff] [blame] | 89 | // If not specified, the default is 1. |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 90 | void SetRate(uint32_t cycles); |
| 91 | |
| 92 | // Adds the input signal to the state to snapshot on the trigger event. |
Brian Silverman | d49fd78 | 2015-01-30 16:43:17 -0500 | [diff] [blame] | 93 | // It is safe to add the same input multiple times, but there is currently |
| 94 | // no way to remove one once it has been added. |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 95 | // Call Add() and SetExternalTrigger() before Start(). |
| 96 | void Add(Encoder *encoder); |
| 97 | void Add(DigitalSource *input); |
Brian Silverman | d49fd78 | 2015-01-30 16:43:17 -0500 | [diff] [blame] | 98 | void Add(AnalogInput *input); |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 99 | |
| 100 | // Configures DMA to trigger on an external trigger. There can only be 4 |
| 101 | // external triggers. |
| 102 | // Call Add() and SetExternalTrigger() before Start(). |
| 103 | void SetExternalTrigger(DigitalSource *input, bool rising, bool falling); |
| 104 | |
| 105 | // Starts reading samples into the buffer. Clears all previous samples before |
| 106 | // starting. |
| 107 | // Call Start() before Read(). |
| 108 | void Start(size_t queue_depth); |
| 109 | |
| 110 | enum ReadStatus { |
| 111 | STATUS_OK = 0, |
| 112 | STATUS_TIMEOUT = 1, |
| 113 | STATUS_ERROR = 2, |
| 114 | }; |
| 115 | |
| 116 | // Reads a sample from the DMA buffer, waiting up to timeout_ms for it. |
| 117 | // Returns a status code indicating whether the read worked, timed out, or |
| 118 | // failed. |
Brian Silverman | d49fd78 | 2015-01-30 16:43:17 -0500 | [diff] [blame] | 119 | // Returns in *remaining_out the number of DMA samples still queued after this |
| 120 | // Read(). |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 121 | // Call Add() and SetExternalTrigger() then Start() before Read(). |
| 122 | // The sample is only usable while this DMA object is left started. |
Brian Silverman | d49fd78 | 2015-01-30 16:43:17 -0500 | [diff] [blame] | 123 | ReadStatus Read(DMASample *sample, uint32_t timeout_ms, |
| 124 | size_t *remaining_out); |
| 125 | |
| 126 | // Translates a ReadStatus code to a string name. |
| 127 | static const char *NameOfReadStatus(ReadStatus s); |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 128 | |
| 129 | private: |
| 130 | ::std::unique_ptr<nFPGA::tDMAManager> manager_; // set by Start() |
| 131 | typedef nFPGA::nRoboRIO_FPGANamespace::tDMA tDMA; |
| 132 | friend DMASample; |
| 133 | |
| 134 | // The offsets into the sample structure for each DMA type, or -1 if it isn't |
| 135 | // in the set of values. |
Austin Schuh | 91c7556 | 2015-12-20 22:23:10 -0800 | [diff] [blame] | 136 | #ifdef WPILIB2015 |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 137 | ssize_t channel_offsets_[18]; |
Austin Schuh | 91c7556 | 2015-12-20 22:23:10 -0800 | [diff] [blame] | 138 | #else |
| 139 | ssize_t channel_offsets_[20]; |
| 140 | #endif |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 141 | |
| 142 | // The size of the data to read to get a sample. |
| 143 | size_t capture_size_ = 0; |
| 144 | tDMA::tConfig tconfig_; |
| 145 | tDMA *tdma_config_; |
| 146 | |
Austin Schuh | 91c7556 | 2015-12-20 22:23:10 -0800 | [diff] [blame] | 147 | #ifndef WPILIB2015 |
| 148 | ::std::array<bool, 8> trigger_channels_ = { |
| 149 | {false, false, false, false, false, false, false, false}}; |
| 150 | #else |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 151 | ::std::array<bool, 4> trigger_channels_ = {{false, false, false, false}}; |
Austin Schuh | 91c7556 | 2015-12-20 22:23:10 -0800 | [diff] [blame] | 152 | #endif |
Brian Silverman | 2aa83d7 | 2015-01-24 18:03:11 -0500 | [diff] [blame] | 153 | }; |
| 154 | |
Brian Silverman | b5b46ca | 2016-03-13 01:14:17 -0500 | [diff] [blame] | 155 | #endif // FRC971_WPILIB_DMA_H_ |