blob: 46e1f71d87ae02e0de6cc5f8d04fb1d3d33bd0d7 [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 <array>
Tyler Chatowbf0609c2021-07-31 16:13:27 -07008#include <cstdint>
Brian Silverman2aa83d72015-01-24 18:03:11 -05009#include <memory>
10
Austin Schuhf6b94632019-02-02 22:11:27 -080011#include "hal/ChipObject.h"
Brian Silverman2aa83d72015-01-24 18:03:11 -050012
13class DMA;
Brian Silvermane48dbc12017-02-04 20:06:29 -080014namespace frc {
Brian Silvermand49fd782015-01-30 16:43:17 -050015class DigitalSource;
16class AnalogInput;
17class Encoder;
Brian Silvermane48dbc12017-02-04 20:06:29 -080018} // namespace frc
Brian Silverman2aa83d72015-01-24 18:03:11 -050019
Brian Silvermand49fd782015-01-30 16:43:17 -050020// A POD class which stores the data from a DMA sample and provides safe ways to
21// access it.
Brian Silverman2aa83d72015-01-24 18:03:11 -050022class DMASample {
23 public:
Brian Silvermand49fd782015-01-30 16:43:17 -050024 DMASample() = default;
Brian Silverman2aa83d72015-01-24 18:03:11 -050025
Austin Schuh8f314a92015-11-22 21:35:40 -080026 // Returns the FPGA timestamp of the sample in seconds.
Brian Silverman2aa83d72015-01-24 18:03:11 -050027 double GetTimestamp() const;
Austin Schuh8f314a92015-11-22 21:35:40 -080028 // Returns the FPGA timestamp of the sample in microseconds.
Austin Schuh94f51e92017-10-30 19:25:32 -070029 uint64_t GetTime() const;
Brian Silverman2aa83d72015-01-24 18:03:11 -050030
31 // All Get methods either return the requested value, or set the Error.
32
33 // Returns the value of the digital input in the sample.
Parker Schuhd3b7a8872018-02-19 16:42:27 -080034 bool Get(frc::DigitalSource *input) const;
Brian Silverman2aa83d72015-01-24 18:03:11 -050035 // Returns the raw value of the encoder in the sample.
Parker Schuhd3b7a8872018-02-19 16:42:27 -080036 int32_t GetRaw(frc::Encoder *input) const;
Brian Silverman2aa83d72015-01-24 18:03:11 -050037 // Returns the {1, 2, or 4} X scaled value of the encoder in the sample.
Parker Schuhd3b7a8872018-02-19 16:42:27 -080038 int32_t Get(frc::Encoder *input) const;
Brian Silvermand49fd782015-01-30 16:43:17 -050039 // Returns the raw 12-bit value from the ADC.
Parker Schuhd3b7a8872018-02-19 16:42:27 -080040 uint16_t GetValue(frc::AnalogInput *input) const;
Brian Silvermand49fd782015-01-30 16:43:17 -050041 // Returns the scaled value of an analog input.
Parker Schuhd3b7a8872018-02-19 16:42:27 -080042 float GetVoltage(frc::AnalogInput *input) const;
Brian Silverman2aa83d72015-01-24 18:03:11 -050043
44 private:
45 friend DMA;
46
Austin Schuh94f51e92017-10-30 19:25:32 -070047 void CalculateTimestamp();
48
Brian Silverman2aa83d72015-01-24 18:03:11 -050049 // Returns the offset of the sample type in the buffer, or -1 if it isn't in
50 // the sample.
51 ssize_t offset(int index) const;
52
53 // TODO(austin): This should be re-used from WPILib... Once I merge this back
54 // into WPILib.
55
56 DMA *dma_;
Austin Schuh94f51e92017-10-30 19:25:32 -070057 uint64_t fpga_timestamp_;
Brian Silverman2aa83d72015-01-24 18:03:11 -050058 uint32_t read_buffer_[64];
59};
60
Parker Schuhd3b7a8872018-02-19 16:42:27 -080061class DMA {
Brian Silverman2aa83d72015-01-24 18:03:11 -050062 public:
63 DMA();
64 virtual ~DMA();
65
66 // Sets whether or not DMA is paused.
Austin Schuh91c75562015-12-20 22:23:10 -080067 // If not specified, the default is false.
Brian Silverman2aa83d72015-01-24 18:03:11 -050068 void SetPause(bool pause);
69
70 // Sets the number of triggers that need to occur before a sample is saved.
Austin Schuh91c75562015-12-20 22:23:10 -080071 // If not specified, the default is 1.
Brian Silverman2aa83d72015-01-24 18:03:11 -050072 void SetRate(uint32_t cycles);
73
74 // Adds the input signal to the state to snapshot on the trigger event.
Brian Silvermand49fd782015-01-30 16:43:17 -050075 // It is safe to add the same input multiple times, but there is currently
76 // no way to remove one once it has been added.
Brian Silverman2aa83d72015-01-24 18:03:11 -050077 // Call Add() and SetExternalTrigger() before Start().
Parker Schuhd3b7a8872018-02-19 16:42:27 -080078 void Add(frc::Encoder *encoder);
79 void Add(frc::DigitalSource *input);
80 void Add(frc::AnalogInput *input);
Brian Silverman2aa83d72015-01-24 18:03:11 -050081
82 // Configures DMA to trigger on an external trigger. There can only be 4
83 // external triggers.
84 // Call Add() and SetExternalTrigger() before Start().
Parker Schuhd3b7a8872018-02-19 16:42:27 -080085 void SetExternalTrigger(frc::DigitalSource *input, bool rising, bool falling);
Brian Silverman2aa83d72015-01-24 18:03:11 -050086
87 // Starts reading samples into the buffer. Clears all previous samples before
88 // starting.
89 // Call Start() before Read().
90 void Start(size_t queue_depth);
91
92 enum ReadStatus {
93 STATUS_OK = 0,
94 STATUS_TIMEOUT = 1,
95 STATUS_ERROR = 2,
96 };
97
98 // Reads a sample from the DMA buffer, waiting up to timeout_ms for it.
99 // Returns a status code indicating whether the read worked, timed out, or
100 // failed.
Brian Silvermand49fd782015-01-30 16:43:17 -0500101 // Returns in *remaining_out the number of DMA samples still queued after this
102 // Read().
Brian Silverman2aa83d72015-01-24 18:03:11 -0500103 // Call Add() and SetExternalTrigger() then Start() before Read().
104 // The sample is only usable while this DMA object is left started.
Brian Silvermand49fd782015-01-30 16:43:17 -0500105 ReadStatus Read(DMASample *sample, uint32_t timeout_ms,
106 size_t *remaining_out);
107
108 // Translates a ReadStatus code to a string name.
109 static const char *NameOfReadStatus(ReadStatus s);
Brian Silverman2aa83d72015-01-24 18:03:11 -0500110
111 private:
112 ::std::unique_ptr<nFPGA::tDMAManager> manager_; // set by Start()
113 typedef nFPGA::nRoboRIO_FPGANamespace::tDMA tDMA;
114 friend DMASample;
115
Parker Schuhd3b7a8872018-02-19 16:42:27 -0800116// The offsets into the sample structure for each DMA type, or -1 if it isn't
117// in the set of values.
Austin Schuh91c75562015-12-20 22:23:10 -0800118#ifdef WPILIB2015
Brian Silverman2aa83d72015-01-24 18:03:11 -0500119 ssize_t channel_offsets_[18];
Austin Schuh91c75562015-12-20 22:23:10 -0800120#else
121 ssize_t channel_offsets_[20];
122#endif
Brian Silverman2aa83d72015-01-24 18:03:11 -0500123
124 // The size of the data to read to get a sample.
125 size_t capture_size_ = 0;
126 tDMA::tConfig tconfig_;
127 tDMA *tdma_config_;
128
Austin Schuh91c75562015-12-20 22:23:10 -0800129#ifndef WPILIB2015
130 ::std::array<bool, 8> trigger_channels_ = {
131 {false, false, false, false, false, false, false, false}};
132#else
Brian Silverman2aa83d72015-01-24 18:03:11 -0500133 ::std::array<bool, 4> trigger_channels_ = {{false, false, false, false}};
Austin Schuh91c75562015-12-20 22:23:10 -0800134#endif
Brian Silverman2aa83d72015-01-24 18:03:11 -0500135};
136
Brian Silvermanb5b46ca2016-03-13 01:14:17 -0500137#endif // FRC971_WPILIB_DMA_H_