James Kuszmaul | 4b81d30 | 2019-12-14 20:53:14 -0800 | [diff] [blame] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) 2019 FIRST. All Rights Reserved. */ |
| 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #include "frc/DMA.h" |
| 9 | |
| 10 | #include <frc/AnalogInput.h> |
| 11 | #include <frc/Counter.h> |
| 12 | #include <frc/DigitalSource.h> |
| 13 | #include <frc/DutyCycle.h> |
| 14 | #include <frc/Encoder.h> |
| 15 | |
| 16 | #include <hal/DMA.h> |
| 17 | #include <hal/HALBase.h> |
| 18 | |
| 19 | using namespace frc; |
| 20 | |
| 21 | DMA::DMA() { |
| 22 | int32_t status = 0; |
| 23 | dmaHandle = HAL_InitializeDMA(&status); |
| 24 | wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); |
| 25 | } |
| 26 | |
| 27 | DMA::~DMA() { HAL_FreeDMA(dmaHandle); } |
| 28 | |
| 29 | void DMA::SetPause(bool pause) { |
| 30 | int32_t status = 0; |
| 31 | HAL_SetDMAPause(dmaHandle, pause, &status); |
| 32 | wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); |
| 33 | } |
| 34 | |
| 35 | void DMA::SetRate(int cycles) { |
| 36 | int32_t status = 0; |
| 37 | HAL_SetDMARate(dmaHandle, cycles, &status); |
| 38 | wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); |
| 39 | } |
| 40 | |
| 41 | void DMA::AddEncoder(const Encoder* encoder) { |
| 42 | int32_t status = 0; |
| 43 | HAL_AddDMAEncoder(dmaHandle, encoder->m_encoder, &status); |
| 44 | wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); |
| 45 | } |
| 46 | |
| 47 | void DMA::AddEncoderPeriod(const Encoder* encoder) { |
| 48 | int32_t status = 0; |
| 49 | HAL_AddDMAEncoderPeriod(dmaHandle, encoder->m_encoder, &status); |
| 50 | wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); |
| 51 | } |
| 52 | |
| 53 | void DMA::AddCounter(const Counter* counter) { |
| 54 | int32_t status = 0; |
| 55 | HAL_AddDMACounter(dmaHandle, counter->m_counter, &status); |
| 56 | wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); |
| 57 | } |
| 58 | |
| 59 | void DMA::AddCounterPeriod(const Counter* counter) { |
| 60 | int32_t status = 0; |
| 61 | HAL_AddDMACounterPeriod(dmaHandle, counter->m_counter, &status); |
| 62 | wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); |
| 63 | } |
| 64 | |
| 65 | void DMA::AddDigitalSource(const DigitalSource* digitalSource) { |
| 66 | int32_t status = 0; |
| 67 | HAL_AddDMADigitalSource(dmaHandle, digitalSource->GetPortHandleForRouting(), |
| 68 | &status); |
| 69 | wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); |
| 70 | } |
| 71 | |
| 72 | void DMA::AddDutyCycle(const DutyCycle* dutyCycle) { |
| 73 | int32_t status = 0; |
| 74 | HAL_AddDMADutyCycle(dmaHandle, dutyCycle->m_handle, &status); |
| 75 | wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); |
| 76 | } |
| 77 | |
| 78 | void DMA::AddAnalogInput(const AnalogInput* analogInput) { |
| 79 | int32_t status = 0; |
| 80 | HAL_AddDMAAnalogInput(dmaHandle, analogInput->m_port, &status); |
| 81 | wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); |
| 82 | } |
| 83 | |
| 84 | void DMA::AddAveragedAnalogInput(const AnalogInput* analogInput) { |
| 85 | int32_t status = 0; |
| 86 | HAL_AddDMAAveragedAnalogInput(dmaHandle, analogInput->m_port, &status); |
| 87 | wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); |
| 88 | } |
| 89 | |
| 90 | void DMA::AddAnalogAccumulator(const AnalogInput* analogInput) { |
| 91 | int32_t status = 0; |
| 92 | HAL_AddDMAAnalogAccumulator(dmaHandle, analogInput->m_port, &status); |
| 93 | wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); |
| 94 | } |
| 95 | |
| 96 | void DMA::SetExternalTrigger(DigitalSource* source, bool rising, bool falling) { |
| 97 | int32_t status = 0; |
| 98 | HAL_SetDMAExternalTrigger(dmaHandle, source->GetPortHandleForRouting(), |
| 99 | static_cast<HAL_AnalogTriggerType>( |
| 100 | source->GetAnalogTriggerTypeForRouting()), |
| 101 | rising, falling, &status); |
| 102 | wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); |
| 103 | } |
| 104 | |
| 105 | void DMA::StartDMA(int queueDepth) { |
| 106 | int32_t status = 0; |
| 107 | HAL_StartDMA(dmaHandle, queueDepth, &status); |
| 108 | wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); |
| 109 | } |
| 110 | |
| 111 | void DMA::StopDMA() { |
| 112 | int32_t status = 0; |
| 113 | HAL_StopDMA(dmaHandle, &status); |
| 114 | wpi_setErrorWithContext(status, HAL_GetErrorMessage(status)); |
| 115 | } |