mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-28 02:11:43 +00:00
[hal] Add support for DMA to Java (#3158)
This commit is contained in:
@@ -4,6 +4,14 @@
|
||||
|
||||
#include "frc/DMA.h"
|
||||
|
||||
#include <frc/AnalogInput.h>
|
||||
#include <frc/Counter.h>
|
||||
#include <frc/DigitalSource.h>
|
||||
#include <frc/DutyCycle.h>
|
||||
#include <frc/Encoder.h>
|
||||
#include <frc/PWM.h>
|
||||
#include <frc/motorcontrol/PWMMotorController.h>
|
||||
|
||||
#include <hal/DMA.h>
|
||||
#include <hal/HALBase.h>
|
||||
|
||||
@@ -32,10 +40,16 @@ void DMA::SetPause(bool pause) {
|
||||
FRC_CheckErrorStatus(status, "{}", "SetPause");
|
||||
}
|
||||
|
||||
void DMA::SetRate(int cycles) {
|
||||
void DMA::SetTimedTrigger(units::second_t seconds) {
|
||||
int32_t status = 0;
|
||||
HAL_SetDMARate(dmaHandle, cycles, &status);
|
||||
FRC_CheckErrorStatus(status, "{}", "SetRate");
|
||||
HAL_SetDMATimedTrigger(dmaHandle, seconds.to<double>(), &status);
|
||||
FRC_CheckErrorStatus(status, "{}", "SetTimedTrigger");
|
||||
}
|
||||
|
||||
void DMA::SetTimedTriggerCycles(int cycles) {
|
||||
int32_t status = 0;
|
||||
HAL_SetDMATimedTriggerCycles(dmaHandle, cycles, &status);
|
||||
FRC_CheckErrorStatus(status, "{}", "SetTimedTriggerCycles");
|
||||
}
|
||||
|
||||
void DMA::AddEncoder(const Encoder* encoder) {
|
||||
@@ -93,23 +107,56 @@ void DMA::AddAnalogAccumulator(const AnalogInput* analogInput) {
|
||||
FRC_CheckErrorStatus(status, "{}", "AddAnalogAccumulator");
|
||||
}
|
||||
|
||||
void DMA::SetExternalTrigger(DigitalSource* source, bool rising, bool falling) {
|
||||
int DMA::SetExternalTrigger(DigitalSource* source, bool rising, bool falling) {
|
||||
int32_t status = 0;
|
||||
HAL_SetDMAExternalTrigger(dmaHandle, source->GetPortHandleForRouting(),
|
||||
static_cast<HAL_AnalogTriggerType>(
|
||||
source->GetAnalogTriggerTypeForRouting()),
|
||||
rising, falling, &status);
|
||||
int32_t idx =
|
||||
HAL_SetDMAExternalTrigger(dmaHandle, source->GetPortHandleForRouting(),
|
||||
static_cast<HAL_AnalogTriggerType>(
|
||||
source->GetAnalogTriggerTypeForRouting()),
|
||||
rising, falling, &status);
|
||||
FRC_CheckErrorStatus(status, "{}", "SetExternalTrigger");
|
||||
return idx;
|
||||
}
|
||||
|
||||
void DMA::StartDMA(int queueDepth) {
|
||||
int DMA::SetPwmEdgeTrigger(PWMMotorController* source, bool rising,
|
||||
bool falling) {
|
||||
int32_t status = 0;
|
||||
int32_t idx = HAL_SetDMAExternalTrigger(
|
||||
dmaHandle, source->GetPwm()->m_handle,
|
||||
HAL_AnalogTriggerType::HAL_Trigger_kInWindow, rising, falling, &status);
|
||||
FRC_CheckErrorStatus(status, "{}", "SetPWmEdgeTrigger");
|
||||
return idx;
|
||||
}
|
||||
|
||||
int DMA::SetPwmEdgeTrigger(PWM* source, bool rising, bool falling) {
|
||||
int32_t status = 0;
|
||||
int32_t idx = HAL_SetDMAExternalTrigger(
|
||||
dmaHandle, source->m_handle, HAL_AnalogTriggerType::HAL_Trigger_kInWindow,
|
||||
rising, falling, &status);
|
||||
FRC_CheckErrorStatus(status, "{}", "SetPWmEdgeTrigger");
|
||||
return idx;
|
||||
}
|
||||
|
||||
void DMA::ClearSensors() {
|
||||
int32_t status = 0;
|
||||
HAL_ClearDMASensors(dmaHandle, &status);
|
||||
FRC_CheckErrorStatus(status, "{}", "ClearSensors");
|
||||
}
|
||||
|
||||
void DMA::ClearExternalTriggers() {
|
||||
int32_t status = 0;
|
||||
HAL_ClearDMAExternalTriggers(dmaHandle, &status);
|
||||
FRC_CheckErrorStatus(status, "{}", "ClearExternalTriggers");
|
||||
}
|
||||
|
||||
void DMA::Start(int queueDepth) {
|
||||
int32_t status = 0;
|
||||
HAL_StartDMA(dmaHandle, queueDepth, &status);
|
||||
FRC_CheckErrorStatus(status, "{}", "StartDMA");
|
||||
FRC_CheckErrorStatus(status, "{}", "Start");
|
||||
}
|
||||
|
||||
void DMA::StopDMA() {
|
||||
void DMA::Stop() {
|
||||
int32_t status = 0;
|
||||
HAL_StopDMA(dmaHandle, &status);
|
||||
FRC_CheckErrorStatus(status, "{}", "StopDMA");
|
||||
FRC_CheckErrorStatus(status, "{}", "Stop");
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <hal/Types.h>
|
||||
#include <units/time.h>
|
||||
|
||||
namespace frc {
|
||||
class Encoder;
|
||||
@@ -13,6 +14,8 @@ class DigitalSource;
|
||||
class DutyCycle;
|
||||
class AnalogInput;
|
||||
class DMASample;
|
||||
class PWM;
|
||||
class PWMMotorController;
|
||||
|
||||
class DMA {
|
||||
friend class DMASample;
|
||||
@@ -25,7 +28,8 @@ class DMA {
|
||||
DMA(DMA&& other) = default;
|
||||
|
||||
void SetPause(bool pause);
|
||||
void SetRate(int cycles);
|
||||
void SetTimedTrigger(units::second_t seconds);
|
||||
void SetTimedTriggerCycles(int cycles);
|
||||
|
||||
void AddEncoder(const Encoder* encoder);
|
||||
void AddEncoderPeriod(const Encoder* encoder);
|
||||
@@ -41,10 +45,15 @@ class DMA {
|
||||
void AddAveragedAnalogInput(const AnalogInput* analogInput);
|
||||
void AddAnalogAccumulator(const AnalogInput* analogInput);
|
||||
|
||||
void SetExternalTrigger(DigitalSource* source, bool rising, bool falling);
|
||||
int SetExternalTrigger(DigitalSource* source, bool rising, bool falling);
|
||||
int SetPwmEdgeTrigger(PWM* pwm, bool rising, bool falling);
|
||||
int SetPwmEdgeTrigger(PWMMotorController* pwm, bool rising, bool falling);
|
||||
|
||||
void StartDMA(int queueDepth);
|
||||
void StopDMA();
|
||||
void ClearSensors();
|
||||
void ClearExternalTriggers();
|
||||
|
||||
void Start(int queueDepth);
|
||||
void Stop();
|
||||
|
||||
private:
|
||||
hal::Handle<HAL_DMAHandle> dmaHandle;
|
||||
|
||||
@@ -19,11 +19,16 @@
|
||||
namespace frc {
|
||||
class DMASample : public HAL_DMASample {
|
||||
public:
|
||||
HAL_DMAReadStatus Update(const DMA* dma, units::second_t timeout,
|
||||
int32_t* remaining, int32_t* status) {
|
||||
units::millisecond_t ms = timeout;
|
||||
auto timeoutMs = ms.to<int32_t>();
|
||||
return HAL_ReadDMA(dma->dmaHandle, this, timeoutMs, remaining, status);
|
||||
enum class DMAReadStatus {
|
||||
kOk = HAL_DMA_OK,
|
||||
kTimeout = HAL_DMA_TIMEOUT,
|
||||
kError = HAL_DMA_ERROR
|
||||
};
|
||||
|
||||
DMAReadStatus Update(const DMA* dma, units::second_t timeout,
|
||||
int32_t* remaining, int32_t* status) {
|
||||
return static_cast<DMAReadStatus>(HAL_ReadDMA(
|
||||
dma->dmaHandle, this, timeout.to<double>(), remaining, status));
|
||||
}
|
||||
|
||||
uint64_t GetTime() const { return timeStamp; }
|
||||
|
||||
@@ -12,6 +12,7 @@
|
||||
|
||||
namespace frc {
|
||||
class AddressableLED;
|
||||
class DMA;
|
||||
|
||||
/**
|
||||
* Class implements the PWM generation in the FPGA.
|
||||
@@ -33,6 +34,7 @@ class AddressableLED;
|
||||
class PWM : public wpi::Sendable, public wpi::SendableHelper<PWM> {
|
||||
public:
|
||||
friend class AddressableLED;
|
||||
friend class DMA;
|
||||
/**
|
||||
* Represents the amount to multiply the minimum servo-pulse pwm period by.
|
||||
*/
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
#include "frc/motorcontrol/MotorController.h"
|
||||
|
||||
namespace frc {
|
||||
class DMA;
|
||||
|
||||
/**
|
||||
* Common base class for all PWM Motor Controllers.
|
||||
@@ -24,6 +25,8 @@ class PWMMotorController : public MotorController,
|
||||
public wpi::Sendable,
|
||||
public wpi::SendableHelper<PWMMotorController> {
|
||||
public:
|
||||
friend class DMA;
|
||||
|
||||
PWMMotorController(PWMMotorController&&) = default;
|
||||
PWMMotorController& operator=(PWMMotorController&&) = default;
|
||||
|
||||
@@ -74,6 +77,8 @@ class PWMMotorController : public MotorController,
|
||||
|
||||
private:
|
||||
bool m_isInverted = false;
|
||||
|
||||
PWM* GetPwm() { return &m_pwm; }
|
||||
};
|
||||
|
||||
} // namespace frc
|
||||
|
||||
Reference in New Issue
Block a user