blob: 446ded469d5759dad74ba71fb869a2f79dec7734 [file] [log] [blame]
Brian Silverman2aa83d72015-01-24 18:03:11 -05001#ifndef _DMA_H_
2#define _DMA_H_
3
Brian Silvermand49fd782015-01-30 16:43:17 -05004// Interface to the roboRIO FPGA's DMA features.
5
Brian Silverman2aa83d72015-01-24 18:03:11 -05006#include <stdint.h>
7
8#include <array>
9#include <memory>
10
11#include "ChipObject.h"
Brian Silvermand49fd782015-01-30 16:43:17 -050012#include "ErrorBase.h"
Brian Silverman2aa83d72015-01-24 18:03:11 -050013
14class DMA;
Brian Silvermand49fd782015-01-30 16:43:17 -050015class DigitalSource;
16class AnalogInput;
17class Encoder;
Brian Silverman2aa83d72015-01-24 18:03:11 -050018
Brian Silvermand49fd782015-01-30 16:43:17 -050019// A POD class which stores the data from a DMA sample and provides safe ways to
20// access it.
Brian Silverman2aa83d72015-01-24 18:03:11 -050021class DMASample {
22 public:
Brian Silvermand49fd782015-01-30 16:43:17 -050023 DMASample() = default;
Brian Silverman2aa83d72015-01-24 18:03:11 -050024
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 Silvermand49fd782015-01-30 16:43:17 -050036 // 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 Silverman2aa83d72015-01-24 18:03:11 -050040
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 Silvermand49fd782015-01-30 16:43:17 -050055// TODO(austin): ErrorBase...
Brian Silverman2aa83d72015-01-24 18:03:11 -050056class 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 Silvermand49fd782015-01-30 16:43:17 -050068 // 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 Silverman2aa83d72015-01-24 18:03:11 -050070 // Call Add() and SetExternalTrigger() before Start().
71 void Add(Encoder *encoder);
72 void Add(DigitalSource *input);
Brian Silvermand49fd782015-01-30 16:43:17 -050073 void Add(AnalogInput *input);
Brian Silverman2aa83d72015-01-24 18:03:11 -050074
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 Silvermand49fd782015-01-30 16:43:17 -050094 // Returns in *remaining_out the number of DMA samples still queued after this
95 // Read().
Brian Silverman2aa83d72015-01-24 18:03:11 -050096 // Call Add() and SetExternalTrigger() then Start() before Read().
97 // The sample is only usable while this DMA object is left started.
Brian Silvermand49fd782015-01-30 16:43:17 -050098 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 Silverman2aa83d72015-01-24 18:03:11 -0500103
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_