blob: e07ab36bc55e40c351626bd4ac89626ca295ef1e [file] [log] [blame]
Brian Silvermanb5b46ca2016-03-13 01:14:17 -05001#ifndef FRC971_WPILIB_DMA_H_
2#define FRC971_WPILIB_DMA_H_
Brian Silverman2aa83d72015-01-24 18:03:11 -05003
Brian Silvermand49fd782015-01-30 16:43:17 -05004// Interface to the roboRIO FPGA's DMA features.
Brian Silvermanb5b46ca2016-03-13 01:14:17 -05005// TODO(Brian): Make this less wpilib-like and more frc971-like.
Brian Silvermand49fd782015-01-30 16:43:17 -05006
Brian Silverman2aa83d72015-01-24 18:03:11 -05007#include <stdint.h>
8
9#include <array>
10#include <memory>
11
Brian Silvermane48dbc12017-02-04 20:06:29 -080012#ifdef WPILIB2017
13#include "HAL/ChipObject.h"
14#else
Brian Silverman2aa83d72015-01-24 18:03:11 -050015#include "ChipObject.h"
Brian Silvermane48dbc12017-02-04 20:06:29 -080016#endif
Brian Silvermand49fd782015-01-30 16:43:17 -050017#include "ErrorBase.h"
Brian Silverman2aa83d72015-01-24 18:03:11 -050018
19class DMA;
Brian Silvermane48dbc12017-02-04 20:06:29 -080020#ifdef WPILIB2017
21namespace frc {
Brian Silvermand49fd782015-01-30 16:43:17 -050022class DigitalSource;
23class AnalogInput;
24class Encoder;
Brian Silvermane48dbc12017-02-04 20:06:29 -080025} // namespace frc
26#else
27class DigitalSource;
28class AnalogInput;
29class Encoder;
30namespace frc {
31using ::DigitalSource;
32using ::AnalogInput;
33using ::Encoder;
34} // namespace frc
35#endif
Brian Silverman2aa83d72015-01-24 18:03:11 -050036
Brian Silvermand49fd782015-01-30 16:43:17 -050037// A POD class which stores the data from a DMA sample and provides safe ways to
38// access it.
Brian Silverman2aa83d72015-01-24 18:03:11 -050039class DMASample {
40 public:
Brian Silvermand49fd782015-01-30 16:43:17 -050041 DMASample() = default;
Brian Silverman2aa83d72015-01-24 18:03:11 -050042
Austin Schuh8f314a92015-11-22 21:35:40 -080043 // Returns the FPGA timestamp of the sample in seconds.
Brian Silverman2aa83d72015-01-24 18:03:11 -050044 double GetTimestamp() const;
Austin Schuh8f314a92015-11-22 21:35:40 -080045 // Returns the FPGA timestamp of the sample in microseconds.
46 uint32_t GetTime() const;
Brian Silverman2aa83d72015-01-24 18:03:11 -050047
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 Silvermand49fd782015-01-30 16:43:17 -050056 // Returns the raw 12-bit value from the ADC.
Austin Schuh58d8cdf2015-02-15 21:04:42 -080057 uint16_t GetValue(AnalogInput *input) const;
Brian Silvermand49fd782015-01-30 16:43:17 -050058 // Returns the scaled value of an analog input.
59 float GetVoltage(AnalogInput *input) const;
Brian Silverman2aa83d72015-01-24 18:03:11 -050060
61 private:
62 friend DMA;
63
64 // Returns the offset of the sample type in the buffer, or -1 if it isn't in
65 // the sample.
66 ssize_t offset(int index) const;
67
68 // TODO(austin): This should be re-used from WPILib... Once I merge this back
69 // into WPILib.
70
71 DMA *dma_;
72 uint32_t read_buffer_[64];
73};
74
Brian Silvermand49fd782015-01-30 16:43:17 -050075// TODO(austin): ErrorBase...
Brian Silverman2aa83d72015-01-24 18:03:11 -050076class DMA : public ErrorBase {
77 public:
78 DMA();
79 virtual ~DMA();
80
81 // Sets whether or not DMA is paused.
Austin Schuh91c75562015-12-20 22:23:10 -080082 // If not specified, the default is false.
Brian Silverman2aa83d72015-01-24 18:03:11 -050083 void SetPause(bool pause);
84
85 // Sets the number of triggers that need to occur before a sample is saved.
Austin Schuh91c75562015-12-20 22:23:10 -080086 // If not specified, the default is 1.
Brian Silverman2aa83d72015-01-24 18:03:11 -050087 void SetRate(uint32_t cycles);
88
89 // Adds the input signal to the state to snapshot on the trigger event.
Brian Silvermand49fd782015-01-30 16:43:17 -050090 // It is safe to add the same input multiple times, but there is currently
91 // no way to remove one once it has been added.
Brian Silverman2aa83d72015-01-24 18:03:11 -050092 // Call Add() and SetExternalTrigger() before Start().
93 void Add(Encoder *encoder);
94 void Add(DigitalSource *input);
Brian Silvermand49fd782015-01-30 16:43:17 -050095 void Add(AnalogInput *input);
Brian Silverman2aa83d72015-01-24 18:03:11 -050096
97 // Configures DMA to trigger on an external trigger. There can only be 4
98 // external triggers.
99 // Call Add() and SetExternalTrigger() before Start().
100 void SetExternalTrigger(DigitalSource *input, bool rising, bool falling);
101
102 // Starts reading samples into the buffer. Clears all previous samples before
103 // starting.
104 // Call Start() before Read().
105 void Start(size_t queue_depth);
106
107 enum ReadStatus {
108 STATUS_OK = 0,
109 STATUS_TIMEOUT = 1,
110 STATUS_ERROR = 2,
111 };
112
113 // Reads a sample from the DMA buffer, waiting up to timeout_ms for it.
114 // Returns a status code indicating whether the read worked, timed out, or
115 // failed.
Brian Silvermand49fd782015-01-30 16:43:17 -0500116 // Returns in *remaining_out the number of DMA samples still queued after this
117 // Read().
Brian Silverman2aa83d72015-01-24 18:03:11 -0500118 // Call Add() and SetExternalTrigger() then Start() before Read().
119 // The sample is only usable while this DMA object is left started.
Brian Silvermand49fd782015-01-30 16:43:17 -0500120 ReadStatus Read(DMASample *sample, uint32_t timeout_ms,
121 size_t *remaining_out);
122
123 // Translates a ReadStatus code to a string name.
124 static const char *NameOfReadStatus(ReadStatus s);
Brian Silverman2aa83d72015-01-24 18:03:11 -0500125
126 private:
127 ::std::unique_ptr<nFPGA::tDMAManager> manager_; // set by Start()
128 typedef nFPGA::nRoboRIO_FPGANamespace::tDMA tDMA;
129 friend DMASample;
130
131 // The offsets into the sample structure for each DMA type, or -1 if it isn't
132 // in the set of values.
Austin Schuh91c75562015-12-20 22:23:10 -0800133#ifdef WPILIB2015
Brian Silverman2aa83d72015-01-24 18:03:11 -0500134 ssize_t channel_offsets_[18];
Austin Schuh91c75562015-12-20 22:23:10 -0800135#else
136 ssize_t channel_offsets_[20];
137#endif
Brian Silverman2aa83d72015-01-24 18:03:11 -0500138
139 // The size of the data to read to get a sample.
140 size_t capture_size_ = 0;
141 tDMA::tConfig tconfig_;
142 tDMA *tdma_config_;
143
Austin Schuh91c75562015-12-20 22:23:10 -0800144#ifndef WPILIB2015
145 ::std::array<bool, 8> trigger_channels_ = {
146 {false, false, false, false, false, false, false, false}};
147#else
Brian Silverman2aa83d72015-01-24 18:03:11 -0500148 ::std::array<bool, 4> trigger_channels_ = {{false, false, false, false}};
Austin Schuh91c75562015-12-20 22:23:10 -0800149#endif
Brian Silverman2aa83d72015-01-24 18:03:11 -0500150};
151
Brian Silvermanb5b46ca2016-03-13 01:14:17 -0500152#endif // FRC971_WPILIB_DMA_H_