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