WPILibC++ 2025.0.0-alpha-1-9-ga2beb75
DMA.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include "hal/AnalogTrigger.h"
8#include "hal/Types.h"
9
10/**
11 * @defgroup hal_dma DMA Functions
12 * @ingroup hal_capi
13 * @{
14 */
15
16/**
17 * The DMA Read Status.
18 */
23};
24
25/**
26 * Buffer for containing all DMA data for a specific sample.
27 */
29 uint32_t readBuffer[74];
30 int32_t channelOffsets[22];
31 uint64_t timeStamp;
32 uint32_t captureSize;
34};
35
36#ifdef __cplusplus
37extern "C" {
38#endif
39
40/**
41 * Initializes an object for performing DMA transfers.
42 *
43 * @param[out] status Error status variable. 0 on success.
44 * @return the created dma handle
45 */
47
48/**
49 * Frees a DMA object.
50 *
51 * @param handle the dma handle
52 */
54
55/**
56 * Pauses or unpauses a DMA transfer.
57 *
58 * This can only be called while DMA is running.
59 *
60 * @param[in] handle the dma handle
61 * @param[in] pause true to pause transfers, false to resume.
62 * @param[out] status Error status variable. 0 on success.
63 */
64void HAL_SetDMAPause(HAL_DMAHandle handle, HAL_Bool pause, int32_t* status);
65
66/**
67 * Sets DMA transfers to occur at a specific timed interval.
68 *
69 * This will remove any external triggers. Only timed or external is supported.
70 *
71 * Only 1 timed period is supported.
72 *
73 * This can only be called if DMA is not started.
74 *
75 * @param[in] handle the dma handle
76 * @param[in] periodSeconds the period to trigger in seconds
77 * @param[out] status Error status variable. 0 on success.
78 */
79void HAL_SetDMATimedTrigger(HAL_DMAHandle handle, double periodSeconds,
80 int32_t* status);
81
82/**
83 * Sets DMA transfers to occur at a specific timed interval in FPGA cycles.
84 *
85 * This will remove any external triggers. Only timed or external is supported.
86 *
87 * Only 1 timed period is supported
88 *
89 * The FPGA currently runs at 40 MHz, but this can change.
90 * HAL_GetSystemClockTicksPerMicrosecond can be used to get a computable value
91 * for this.
92 *
93 * This can only be called if DMA is not started.
94 *
95 * @param[in] handle the dma handle
96 * @param[in] cycles the period to trigger in FPGA cycles
97 * @param[out] status Error status variable. 0 on success.
98 */
99void HAL_SetDMATimedTriggerCycles(HAL_DMAHandle handle, uint32_t cycles,
100 int32_t* status);
101
102/**
103 * Adds position data for an encoder to be collected by DMA.
104 *
105 * This can only be called if DMA is not started.
106 *
107 * @param[in] handle the dma handle
108 * @param[in] encoderHandle the encoder to add
109 * @param[out] status Error status variable. 0 on success.
110 */
112 int32_t* status);
113
114/**
115 * Adds timer data for an encoder to be collected by DMA.
116 *
117 * This can only be called if DMA is not started.
118 *
119 * @param[in] handle the dma handle
120 * @param[in] encoderHandle the encoder to add
121 * @param[out] status Error status variable. 0 on success.
122 */
124 HAL_EncoderHandle encoderHandle, int32_t* status);
125
126/**
127 * Adds position data for an counter to be collected by DMA.
128 *
129 * This can only be called if DMA is not started.
130 *
131 * @param[in] handle the dma handle
132 * @param[in] counterHandle the counter to add
133 * @param[out] status Error status variable. 0 on success.
134 */
136 int32_t* status);
137
138/**
139 * Adds timer data for an counter to be collected by DMA.
140 *
141 * This can only be called if DMA is not started.
142 *
143 * @param[in] handle the dma handle
144 * @param[in] counterHandle the counter to add
145 * @param[out] status Error status variable. 0 on success.
146 */
148 HAL_CounterHandle counterHandle, int32_t* status);
149
150/**
151 * Adds a digital source to be collected by DMA.
152 *
153 * This can only be called if DMA is not started.
154 *
155 * @param[in] handle the dma handle
156 * @param[in] digitalSourceHandle the digital source to add
157 * @param[out] status Error status variable. 0 on success.
158 */
160 HAL_Handle digitalSourceHandle, int32_t* status);
161
162/**
163 * Adds an analog input to be collected by DMA.
164 *
165 * This can only be called if DMA is not started.
166 *
167 * @param[in] handle the dma handle
168 * @param[in] aInHandle the analog input to add
169 * @param[out] status Error status variable. 0 on success.
170 */
172 HAL_AnalogInputHandle aInHandle, int32_t* status);
173
174/**
175 * Adds averaged data of an analog input to be collected by DMA.
176 *
177 * This can only be called if DMA is not started.
178 *
179 * @param[in] handle the dma handle
180 * @param[in] aInHandle the analog input to add
181 * @param[out] status Error status variable. 0 on success.
182 */
184 HAL_AnalogInputHandle aInHandle,
185 int32_t* status);
186
187/**
188 * Adds accumulator data of an analog input to be collected by DMA.
189 *
190 * This can only be called if DMA is not started.
191 *
192 * @param[in] handle the dma handle
193 * @param[in] aInHandle the analog input to add
194 * @param[out] status Error status variable. 0 on success.
195 */
197 HAL_AnalogInputHandle aInHandle,
198 int32_t* status);
199
200/**
201 * Adds a duty cycle input to be collected by DMA.
202 *
203 * This can only be called if DMA is not started.
204 *
205 * @param[in] handle the dma handle
206 * @param[in] dutyCycleHandle the duty cycle input to add
207 * @param[out] status Error status variable. 0 on success.
208 */
210 HAL_DutyCycleHandle dutyCycleHandle, int32_t* status);
211
212/**
213 * Sets DMA transfers to occur on an external trigger.
214 *
215 * This will remove any timed trigger set. Only timed or external is supported.
216 *
217 * Up to 8 external triggers are currently supported.
218 *
219 * This can only be called if DMA is not started.
220 *
221 * @param[in] handle the dma handle
222 * @param[in] digitalSourceHandle the digital source handle (either a
223 * HAL_AnalogTriggerHandle or a
224 * HAL_DigitalHandle)
225 * @param[in] analogTriggerType the analog trigger type if the source is an
226 * analog trigger
227 * @param[in] rising true to trigger on rising edge
228 * @param[in] falling true to trigger on falling edge
229 * @param[out] status Error status variable. 0 on success.
230 * @return the index of the trigger
231 */
233 HAL_Handle digitalSourceHandle,
234 HAL_AnalogTriggerType analogTriggerType,
235 HAL_Bool rising, HAL_Bool falling,
236 int32_t* status);
237
238/**
239 * Clear all sensors from the DMA collection list.
240 *
241 * This can only be called if DMA is not started.
242 *
243 * @param[in] handle the dma handle
244 * @param[out] status Error status variable. 0 on success.
245 */
246void HAL_ClearDMASensors(HAL_DMAHandle handle, int32_t* status);
247
248/**
249 * Clear all external triggers from the DMA trigger list.
250 *
251 * This can only be called if DMA is not started.
252 *
253 * @param[in] handle the dma handle
254 * @param[out] status Error status variable. 0 on success.
255 */
256void HAL_ClearDMAExternalTriggers(HAL_DMAHandle handle, int32_t* status);
257
258/**
259 * Starts DMA Collection.
260 *
261 * @param[in] handle the dma handle
262 * @param[in] queueDepth the number of objects to be able to queue
263 * @param[out] status Error status variable. 0 on success.
264 */
265void HAL_StartDMA(HAL_DMAHandle handle, int32_t queueDepth, int32_t* status);
266
267/**
268 * Stops DMA Collection.
269 *
270 * @param[in] handle the dma handle
271 * @param[out] status Error status variable. 0 on success.
272 */
273void HAL_StopDMA(HAL_DMAHandle handle, int32_t* status);
274
275/**
276 * Gets the direct pointer to the DMA object.
277 *
278 * This is only to be used if absolute maximum performance is required. This
279 * will only be valid until the handle is freed.
280 *
281 * @param handle the dma handle
282 */
284
285/**
286 * Reads a DMA sample using a direct DMA pointer.
287 *
288 * See HAL_ReadDMA for full documentation.
289 *
290 * @param[in] dmaPointer direct DMA pointer
291 * @param[in] dmaSample the sample object to place data into
292 * @param[in] timeoutSeconds the time to wait for data to be queued before
293 * timing out
294 * @param[in] remainingOut the number of samples remaining in the queue
295 * @param[out] status Error status variable. 0 on success.
296 */
298 HAL_DMASample* dmaSample,
299 double timeoutSeconds,
300 int32_t* remainingOut,
301 int32_t* status);
302
303/**
304 * Reads a DMA sample from the queue.
305 *
306 *
307 * @param[in] handle the dma handle
308 * @param[in] dmaSample the sample object to place data into
309 * @param[in] timeoutSeconds the time to wait for data to be queued before
310 * timing out
311 * @param[in] remainingOut the number of samples remaining in the queue
312 * @param[out] status Error status variable. 0 on success.
313 * @return the success result of the sample read
314 */
316 HAL_DMASample* dmaSample,
317 double timeoutSeconds, int32_t* remainingOut,
318 int32_t* status);
319
320// The following are helper functions for reading data from samples
321
322/**
323 * Returns the timestamp of the sample.
324 * This is in the same time domain as HAL_GetFPGATime().
325 *
326 * @param[in] dmaSample the sample to read from
327 * @param[out] status Error status variable. 0 on success.
328 * @return timestamp in microseconds since FPGA Initialization
329 */
330uint64_t HAL_GetDMASampleTime(const HAL_DMASample* dmaSample, int32_t* status);
331
332/**
333 * Returns the raw distance data for an encoder from the sample.
334 *
335 * This can be scaled with DistancePerPulse and DecodingScaleFactor to match the
336 * result of GetDistance()
337 *
338 * @param[in] dmaSample the sample to read from
339 * @param[in] encoderHandle the encoder handle
340 * @param[out] status Error status variable. 0 on success.
341 * @return raw encoder ticks
342 */
344 HAL_EncoderHandle encoderHandle,
345 int32_t* status);
346
347/**
348 * Returns the distance data for an counter from the sample.
349 *
350 * @param[in] dmaSample the sample to read from
351 * @param[in] counterHandle the counter handle
352 * @param[out] status Error status variable. 0 on success.
353 * @return counter ticks
354 */
355int32_t HAL_GetDMASampleCounter(const HAL_DMASample* dmaSample,
356 HAL_CounterHandle counterHandle,
357 int32_t* status);
358
359/**
360 * Returns the raw period data for an encoder from the sample.
361 *
362 * This can be scaled with DistancePerPulse and DecodingScaleFactor to match the
363 * result of GetRate()
364 *
365 * @param[in] dmaSample the sample to read from
366 * @param[in] encoderHandle the encoder handle
367 * @param[out] status Error status variable. 0 on success.
368 * @return raw encoder period
369 */
371 HAL_EncoderHandle encoderHandle,
372 int32_t* status);
373
374/**
375 * Returns the period data for an counter from the sample.
376 *
377 * @param[in] dmaSample the sample to read from
378 * @param[in] counterHandle the counter handle
379 * @param[out] status Error status variable. 0 on success.
380 * @return counter period
381 */
383 HAL_CounterHandle counterHandle,
384 int32_t* status);
385
386/**
387 * Returns the state of a digital source from the sample.
388 *
389 * @param[in] dmaSample the sample to read from
390 * @param[in] dSourceHandle the digital source handle
391 * @param[out] status Error status variable. 0 on success.
392 * @return digital source state
393 */
395 HAL_Handle dSourceHandle,
396 int32_t* status);
397
398/**
399 * Returns the raw analog data for an analog input from the sample.
400 *
401 * This can be scaled with HAL_GetAnalogValueToVolts to match GetVoltage().
402 *
403 * @param[in] dmaSample the sample to read from
404 * @param[in] aInHandle the analog input handle
405 * @param[out] status Error status variable. 0 on success.
406 * @return raw analog data
407 */
409 HAL_AnalogInputHandle aInHandle,
410 int32_t* status);
411
412/**
413 * Returns the raw averaged analog data for an analog input from the sample.
414 *
415 * This can be scaled with HAL_GetAnalogValueToVolts to match
416 * GetAveragedVoltage().
417 *
418 * @param[in] dmaSample the sample to read from
419 * @param[in] aInHandle the analog input handle
420 * @param[out] status Error status variable. 0 on success.
421 * @return raw averaged analog data
422 */
424 HAL_AnalogInputHandle aInHandle,
425 int32_t* status);
426
427/**
428 * Returns the analog accumulator data for an analog input from the sample.
429 *
430 * @param[in] dmaSample the sample to read from
431 * @param[in] aInHandle the analog input handle
432 * @param[in] count the accumulator count
433 * @param[in] value the accumulator value
434 * @param[out] status Error status variable. 0 on success.
435 */
437 HAL_AnalogInputHandle aInHandle,
438 int64_t* count, int64_t* value,
439 int32_t* status);
440
441/**
442 * Returns the raw duty cycle input ratio data from the sample.
443 *
444 * Use HAL_GetDutyCycleOutputScaleFactor to scale this to a percentage.
445 *
446 * @param[in] dmaSample the sample to read from
447 * @param[in] dutyCycleHandle the duty cycle handle
448 * @param[out] status Error status variable. 0 on success.
449 * @return raw duty cycle input data
450 */
452 HAL_DutyCycleHandle dutyCycleHandle,
453 int32_t* status);
454
455#ifdef __cplusplus
456} // extern "C"
457#endif
458/** @} */
@ HAL_ENUM
Definition: Value.h:14
HAL_AnalogTriggerType
The type of analog trigger to trigger on.
Definition: AnalogTrigger.h:20
int32_t HAL_SetDMAExternalTrigger(HAL_DMAHandle handle, HAL_Handle digitalSourceHandle, HAL_AnalogTriggerType analogTriggerType, HAL_Bool rising, HAL_Bool falling, int32_t *status)
Sets DMA transfers to occur on an external trigger.
int32_t HAL_GetDMASampleDutyCycleOutputRaw(const HAL_DMASample *dmaSample, HAL_DutyCycleHandle dutyCycleHandle, int32_t *status)
Returns the raw duty cycle input ratio data from the sample.
void HAL_StartDMA(HAL_DMAHandle handle, int32_t queueDepth, int32_t *status)
Starts DMA Collection.
void HAL_SetDMATimedTriggerCycles(HAL_DMAHandle handle, uint32_t cycles, int32_t *status)
Sets DMA transfers to occur at a specific timed interval in FPGA cycles.
enum HAL_DMAReadStatus HAL_ReadDMA(HAL_DMAHandle handle, HAL_DMASample *dmaSample, double timeoutSeconds, int32_t *remainingOut, int32_t *status)
Reads a DMA sample from the queue.
int32_t HAL_GetDMASampleAveragedAnalogInputRaw(const HAL_DMASample *dmaSample, HAL_AnalogInputHandle aInHandle, int32_t *status)
Returns the raw averaged analog data for an analog input from the sample.
void HAL_StopDMA(HAL_DMAHandle handle, int32_t *status)
Stops DMA Collection.
HAL_Bool HAL_GetDMASampleDigitalSource(const HAL_DMASample *dmaSample, HAL_Handle dSourceHandle, int32_t *status)
Returns the state of a digital source from the sample.
void HAL_SetDMATimedTrigger(HAL_DMAHandle handle, double periodSeconds, int32_t *status)
Sets DMA transfers to occur at a specific timed interval.
int32_t HAL_GetDMASampleEncoderPeriodRaw(const HAL_DMASample *dmaSample, HAL_EncoderHandle encoderHandle, int32_t *status)
Returns the raw period data for an encoder from the sample.
int32_t HAL_GetDMASampleCounter(const HAL_DMASample *dmaSample, HAL_CounterHandle counterHandle, int32_t *status)
Returns the distance data for an counter from the sample.
HAL_DMAReadStatus
The DMA Read Status.
Definition: DMA.h:19
void * HAL_GetDMADirectPointer(HAL_DMAHandle handle)
Gets the direct pointer to the DMA object.
void HAL_SetDMAPause(HAL_DMAHandle handle, HAL_Bool pause, int32_t *status)
Pauses or unpauses a DMA transfer.
int32_t HAL_GetDMASampleCounterPeriod(const HAL_DMASample *dmaSample, HAL_CounterHandle counterHandle, int32_t *status)
Returns the period data for an counter from the sample.
int32_t HAL_GetDMASampleAnalogInputRaw(const HAL_DMASample *dmaSample, HAL_AnalogInputHandle aInHandle, int32_t *status)
Returns the raw analog data for an analog input from the sample.
void HAL_GetDMASampleAnalogAccumulator(const HAL_DMASample *dmaSample, HAL_AnalogInputHandle aInHandle, int64_t *count, int64_t *value, int32_t *status)
Returns the analog accumulator data for an analog input from the sample.
void HAL_AddDMAEncoder(HAL_DMAHandle handle, HAL_EncoderHandle encoderHandle, int32_t *status)
Adds position data for an encoder to be collected by DMA.
void HAL_AddDMACounterPeriod(HAL_DMAHandle handle, HAL_CounterHandle counterHandle, int32_t *status)
Adds timer data for an counter to be collected by DMA.
void HAL_AddDMACounter(HAL_DMAHandle handle, HAL_CounterHandle counterHandle, int32_t *status)
Adds position data for an counter to be collected by DMA.
void HAL_AddDMADutyCycle(HAL_DMAHandle handle, HAL_DutyCycleHandle dutyCycleHandle, int32_t *status)
Adds a duty cycle input to be collected by DMA.
uint64_t HAL_GetDMASampleTime(const HAL_DMASample *dmaSample, int32_t *status)
Returns the timestamp of the sample.
void HAL_ClearDMASensors(HAL_DMAHandle handle, int32_t *status)
Clear all sensors from the DMA collection list.
void HAL_AddDMAAnalogAccumulator(HAL_DMAHandle handle, HAL_AnalogInputHandle aInHandle, int32_t *status)
Adds accumulator data of an analog input to be collected by DMA.
void HAL_AddDMAAveragedAnalogInput(HAL_DMAHandle handle, HAL_AnalogInputHandle aInHandle, int32_t *status)
Adds averaged data of an analog input to be collected by DMA.
void HAL_ClearDMAExternalTriggers(HAL_DMAHandle handle, int32_t *status)
Clear all external triggers from the DMA trigger list.
void HAL_AddDMAEncoderPeriod(HAL_DMAHandle handle, HAL_EncoderHandle encoderHandle, int32_t *status)
Adds timer data for an encoder to be collected by DMA.
void HAL_AddDMADigitalSource(HAL_DMAHandle handle, HAL_Handle digitalSourceHandle, int32_t *status)
Adds a digital source to be collected by DMA.
enum HAL_DMAReadStatus HAL_ReadDMADirect(void *dmaPointer, HAL_DMASample *dmaSample, double timeoutSeconds, int32_t *remainingOut, int32_t *status)
Reads a DMA sample using a direct DMA pointer.
int32_t HAL_GetDMASampleEncoderRaw(const HAL_DMASample *dmaSample, HAL_EncoderHandle encoderHandle, int32_t *status)
Returns the raw distance data for an encoder from the sample.
void HAL_AddDMAAnalogInput(HAL_DMAHandle handle, HAL_AnalogInputHandle aInHandle, int32_t *status)
Adds an analog input to be collected by DMA.
void HAL_FreeDMA(HAL_DMAHandle handle)
Frees a DMA object.
HAL_DMAHandle HAL_InitializeDMA(int32_t *status)
Initializes an object for performing DMA transfers.
@ HAL_DMA_ERROR
Definition: DMA.h:22
@ HAL_DMA_TIMEOUT
Definition: DMA.h:21
@ HAL_DMA_OK
Definition: DMA.h:20
int32_t HAL_Bool
Definition: Types.h:73
int32_t HAL_Handle
Definition: Types.h:17
HAL_Handle HAL_DMAHandle
Definition: Types.h:57
HAL_Handle HAL_EncoderHandle
Definition: Types.h:35
HAL_Handle HAL_DutyCycleHandle
Definition: Types.h:59
HAL_Handle HAL_CounterHandle
Definition: Types.h:29
HAL_Handle HAL_AnalogInputHandle
Definition: Types.h:21
constexpr auto count() -> size_t
Definition: core.h:1222
Buffer for containing all DMA data for a specific sample.
Definition: DMA.h:28
int32_t channelOffsets[22]
Definition: DMA.h:30
uint64_t timeStamp
Definition: DMA.h:31
uint8_t triggerChannels
Definition: DMA.h:33
uint32_t readBuffer[74]
Definition: DMA.h:29
uint32_t captureSize
Definition: DMA.h:32