001// Copyright (c) FIRST and other WPILib contributors.
002// Open Source Software; you can modify and/or share it under the terms of
003// the WPILib BSD license file in the root directory of this project.
004
005package edu.wpi.first.wpilibj;
006
007import edu.wpi.first.hal.DMAJNI;
008import edu.wpi.first.wpilibj.motorcontrol.PWMMotorController;
009
010/** Class for configuring Direct Memory Access (DMA) of FPGA inputs. */
011public class DMA implements AutoCloseable {
012  final int m_dmaHandle;
013
014  /** Default constructor. */
015  public DMA() {
016    m_dmaHandle = DMAJNI.initialize();
017  }
018
019  @Override
020  public void close() {
021    DMAJNI.free(m_dmaHandle);
022  }
023
024  /**
025   * Sets whether DMA is paused.
026   *
027   * @param pause True pauses DMA.
028   */
029  public void setPause(boolean pause) {
030    DMAJNI.setPause(m_dmaHandle, pause);
031  }
032
033  /**
034   * Sets DMA to trigger at an interval.
035   *
036   * @param periodSeconds Period at which to trigger DMA in seconds.
037   */
038  public void setTimedTrigger(double periodSeconds) {
039    DMAJNI.setTimedTrigger(m_dmaHandle, periodSeconds);
040  }
041
042  /**
043   * Sets number of DMA cycles to trigger.
044   *
045   * @param cycles Number of cycles.
046   */
047  public void setTimedTriggerCycles(int cycles) {
048    DMAJNI.setTimedTriggerCycles(m_dmaHandle, cycles);
049  }
050
051  /**
052   * Adds position data for an encoder to be collected by DMA.
053   *
054   * <p>This can only be called if DMA is not started.
055   *
056   * @param encoder Encoder to add to DMA.
057   */
058  public void addEncoder(Encoder encoder) {
059    DMAJNI.addEncoder(m_dmaHandle, encoder.m_encoder);
060  }
061
062  /**
063   * Adds timer data for an encoder to be collected by DMA.
064   *
065   * <p>This can only be called if DMA is not started.
066   *
067   * @param encoder Encoder to add to DMA.
068   */
069  public void addEncoderPeriod(Encoder encoder) {
070    DMAJNI.addEncoderPeriod(m_dmaHandle, encoder.m_encoder);
071  }
072
073  /**
074   * Adds position data for an counter to be collected by DMA.
075   *
076   * <p>This can only be called if DMA is not started.
077   *
078   * @param counter Counter to add to DMA.
079   */
080  public void addCounter(Counter counter) {
081    DMAJNI.addCounter(m_dmaHandle, counter.m_counter);
082  }
083
084  /**
085   * Adds timer data for an counter to be collected by DMA.
086   *
087   * <p>This can only be called if DMA is not started.
088   *
089   * @param counter Counter to add to DMA.
090   */
091  public void addCounterPeriod(Counter counter) {
092    DMAJNI.addCounterPeriod(m_dmaHandle, counter.m_counter);
093  }
094
095  /**
096   * Adds a digital source to be collected by DMA.
097   *
098   * <p>This can only be called if DMA is not started.
099   *
100   * @param digitalSource DigitalSource to add to DMA.
101   */
102  public void addDigitalSource(DigitalSource digitalSource) {
103    DMAJNI.addDigitalSource(m_dmaHandle, digitalSource.getPortHandleForRouting());
104  }
105
106  /**
107   * Adds a duty cycle input to be collected by DMA.
108   *
109   * <p>This can only be called if DMA is not started.
110   *
111   * @param dutyCycle DutyCycle to add to DMA.
112   */
113  public void addDutyCycle(DutyCycle dutyCycle) {
114    DMAJNI.addDutyCycle(m_dmaHandle, dutyCycle.m_handle);
115  }
116
117  /**
118   * Adds an analog input to be collected by DMA.
119   *
120   * <p>This can only be called if DMA is not started.
121   *
122   * @param analogInput AnalogInput to add to DMA.
123   */
124  public void addAnalogInput(AnalogInput analogInput) {
125    DMAJNI.addAnalogInput(m_dmaHandle, analogInput.m_port);
126  }
127
128  /**
129   * Adds averaged data of an analog input to be collected by DMA.
130   *
131   * <p>This can only be called if DMA is not started.
132   *
133   * @param analogInput AnalogInput to add to DMA.
134   */
135  public void addAveragedAnalogInput(AnalogInput analogInput) {
136    DMAJNI.addAveragedAnalogInput(m_dmaHandle, analogInput.m_port);
137  }
138
139  /**
140   * Adds accumulator data of an analog input to be collected by DMA.
141   *
142   * <p>This can only be called if DMA is not started.
143   *
144   * @param analogInput AnalogInput to add to DMA.
145   */
146  public void addAnalogAccumulator(AnalogInput analogInput) {
147    DMAJNI.addAnalogAccumulator(m_dmaHandle, analogInput.m_port);
148  }
149
150  /**
151   * Sets an external DMA trigger.
152   *
153   * @param source the source to trigger from.
154   * @param rising trigger on rising edge.
155   * @param falling trigger on falling edge.
156   * @return the index of the trigger
157   */
158  public int setExternalTrigger(DigitalSource source, boolean rising, boolean falling) {
159    return DMAJNI.setExternalTrigger(
160        m_dmaHandle,
161        source.getPortHandleForRouting(),
162        source.getAnalogTriggerTypeForRouting(),
163        rising,
164        falling);
165  }
166
167  /**
168   * Sets a DMA PWM edge trigger.
169   *
170   * @param pwm the PWM to trigger from.
171   * @param rising trigger on rising edge.
172   * @param falling trigger on falling edge.
173   * @return the index of the trigger
174   */
175  public int setPwmEdgeTrigger(PWM pwm, boolean rising, boolean falling) {
176    return DMAJNI.setExternalTrigger(m_dmaHandle, pwm.getHandle(), 0, rising, falling);
177  }
178
179  /**
180   * Sets a DMA PWMMotorController edge trigger.
181   *
182   * @param pwm the PWMMotorController to trigger from.
183   * @param rising trigger on rising edge.
184   * @param falling trigger on falling edge.
185   * @return the index of the trigger
186   */
187  public int setPwmEdgeTrigger(PWMMotorController pwm, boolean rising, boolean falling) {
188    return DMAJNI.setExternalTrigger(m_dmaHandle, pwm.getPwmHandle(), 0, rising, falling);
189  }
190
191  /**
192   * Clear all sensors from the DMA collection list.
193   *
194   * <p>This can only be called if DMA is not started.
195   */
196  public void clearSensors() {
197    DMAJNI.clearSensors(m_dmaHandle);
198  }
199
200  /**
201   * Clear all external triggers from the DMA trigger list.
202   *
203   * <p>This can only be called if DMA is not started.
204   */
205  public void clearExternalTriggers() {
206    DMAJNI.clearExternalTriggers(m_dmaHandle);
207  }
208
209  /**
210   * Starts DMA Collection.
211   *
212   * @param queueDepth The number of objects to be able to queue.
213   */
214  public void start(int queueDepth) {
215    DMAJNI.startDMA(m_dmaHandle, queueDepth);
216  }
217
218  /** Stops DMA Collection. */
219  public void stop() {
220    DMAJNI.stopDMA(m_dmaHandle);
221  }
222}