Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 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. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 4 | |
| 5 | #include "frc/DMA.h" |
| 6 | |
| 7 | #include <frc/AnalogInput.h> |
| 8 | #include <frc/Counter.h> |
| 9 | #include <frc/DigitalSource.h> |
| 10 | #include <frc/DutyCycle.h> |
| 11 | #include <frc/Encoder.h> |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 12 | #include <frc/PWM.h> |
| 13 | #include <frc/motorcontrol/PWMMotorController.h> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 14 | |
| 15 | #include <hal/DMA.h> |
| 16 | #include <hal/HALBase.h> |
| 17 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 18 | #include "frc/AnalogInput.h" |
| 19 | #include "frc/Counter.h" |
| 20 | #include "frc/DigitalSource.h" |
| 21 | #include "frc/DutyCycle.h" |
| 22 | #include "frc/Encoder.h" |
| 23 | #include "frc/Errors.h" |
| 24 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 25 | using namespace frc; |
| 26 | |
| 27 | DMA::DMA() { |
| 28 | int32_t status = 0; |
| 29 | dmaHandle = HAL_InitializeDMA(&status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 30 | FRC_CheckErrorStatus(status, "{}", "InitializeDMA"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 31 | } |
| 32 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 33 | DMA::~DMA() { |
| 34 | HAL_FreeDMA(dmaHandle); |
| 35 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 36 | |
| 37 | void DMA::SetPause(bool pause) { |
| 38 | int32_t status = 0; |
| 39 | HAL_SetDMAPause(dmaHandle, pause, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 40 | FRC_CheckErrorStatus(status, "{}", "SetPause"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 41 | } |
| 42 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 43 | void DMA::SetTimedTrigger(units::second_t seconds) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 44 | int32_t status = 0; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 45 | HAL_SetDMATimedTrigger(dmaHandle, seconds.value(), &status); |
| 46 | FRC_CheckErrorStatus(status, "{}", "SetTimedTrigger"); |
| 47 | } |
| 48 | |
| 49 | void DMA::SetTimedTriggerCycles(int cycles) { |
| 50 | int32_t status = 0; |
| 51 | HAL_SetDMATimedTriggerCycles(dmaHandle, cycles, &status); |
| 52 | FRC_CheckErrorStatus(status, "{}", "SetTimedTriggerCycles"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void DMA::AddEncoder(const Encoder* encoder) { |
| 56 | int32_t status = 0; |
| 57 | HAL_AddDMAEncoder(dmaHandle, encoder->m_encoder, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 58 | FRC_CheckErrorStatus(status, "{}", "AddEncoder"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void DMA::AddEncoderPeriod(const Encoder* encoder) { |
| 62 | int32_t status = 0; |
| 63 | HAL_AddDMAEncoderPeriod(dmaHandle, encoder->m_encoder, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 64 | FRC_CheckErrorStatus(status, "{}", "AddEncoderPeriod"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void DMA::AddCounter(const Counter* counter) { |
| 68 | int32_t status = 0; |
| 69 | HAL_AddDMACounter(dmaHandle, counter->m_counter, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 70 | FRC_CheckErrorStatus(status, "{}", "AddCounter"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void DMA::AddCounterPeriod(const Counter* counter) { |
| 74 | int32_t status = 0; |
| 75 | HAL_AddDMACounterPeriod(dmaHandle, counter->m_counter, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 76 | FRC_CheckErrorStatus(status, "{}", "AddCounterPeriod"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void DMA::AddDigitalSource(const DigitalSource* digitalSource) { |
| 80 | int32_t status = 0; |
| 81 | HAL_AddDMADigitalSource(dmaHandle, digitalSource->GetPortHandleForRouting(), |
| 82 | &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 83 | FRC_CheckErrorStatus(status, "{}", "AddDigitalSource"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void DMA::AddDutyCycle(const DutyCycle* dutyCycle) { |
| 87 | int32_t status = 0; |
| 88 | HAL_AddDMADutyCycle(dmaHandle, dutyCycle->m_handle, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 89 | FRC_CheckErrorStatus(status, "{}", "AddDutyCycle"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | void DMA::AddAnalogInput(const AnalogInput* analogInput) { |
| 93 | int32_t status = 0; |
| 94 | HAL_AddDMAAnalogInput(dmaHandle, analogInput->m_port, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 95 | FRC_CheckErrorStatus(status, "{}", "AddAnalogInput"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | void DMA::AddAveragedAnalogInput(const AnalogInput* analogInput) { |
| 99 | int32_t status = 0; |
| 100 | HAL_AddDMAAveragedAnalogInput(dmaHandle, analogInput->m_port, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 101 | FRC_CheckErrorStatus(status, "{}", "AddAveragedAnalogInput"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | void DMA::AddAnalogAccumulator(const AnalogInput* analogInput) { |
| 105 | int32_t status = 0; |
| 106 | HAL_AddDMAAnalogAccumulator(dmaHandle, analogInput->m_port, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 107 | FRC_CheckErrorStatus(status, "{}", "AddAnalogAccumulator"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 110 | int DMA::SetExternalTrigger(DigitalSource* source, bool rising, bool falling) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 111 | int32_t status = 0; |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 112 | int32_t idx = |
| 113 | HAL_SetDMAExternalTrigger(dmaHandle, source->GetPortHandleForRouting(), |
| 114 | static_cast<HAL_AnalogTriggerType>( |
| 115 | source->GetAnalogTriggerTypeForRouting()), |
| 116 | rising, falling, &status); |
| 117 | FRC_CheckErrorStatus(status, "{}", "SetExternalTrigger"); |
| 118 | return idx; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 121 | int DMA::SetPwmEdgeTrigger(PWMMotorController* source, bool rising, |
| 122 | bool falling) { |
| 123 | int32_t status = 0; |
| 124 | int32_t idx = HAL_SetDMAExternalTrigger( |
| 125 | dmaHandle, source->GetPwm()->m_handle, |
| 126 | HAL_AnalogTriggerType::HAL_Trigger_kInWindow, rising, falling, &status); |
| 127 | FRC_CheckErrorStatus(status, "{}", "SetPWmEdgeTrigger"); |
| 128 | return idx; |
| 129 | } |
| 130 | |
| 131 | int DMA::SetPwmEdgeTrigger(PWM* source, bool rising, bool falling) { |
| 132 | int32_t status = 0; |
| 133 | int32_t idx = HAL_SetDMAExternalTrigger( |
| 134 | dmaHandle, source->m_handle, HAL_AnalogTriggerType::HAL_Trigger_kInWindow, |
| 135 | rising, falling, &status); |
| 136 | FRC_CheckErrorStatus(status, "{}", "SetPWmEdgeTrigger"); |
| 137 | return idx; |
| 138 | } |
| 139 | |
| 140 | void DMA::ClearSensors() { |
| 141 | int32_t status = 0; |
| 142 | HAL_ClearDMASensors(dmaHandle, &status); |
| 143 | FRC_CheckErrorStatus(status, "{}", "ClearSensors"); |
| 144 | } |
| 145 | |
| 146 | void DMA::ClearExternalTriggers() { |
| 147 | int32_t status = 0; |
| 148 | HAL_ClearDMAExternalTriggers(dmaHandle, &status); |
| 149 | FRC_CheckErrorStatus(status, "{}", "ClearExternalTriggers"); |
| 150 | } |
| 151 | |
| 152 | void DMA::Start(int queueDepth) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 153 | int32_t status = 0; |
| 154 | HAL_StartDMA(dmaHandle, queueDepth, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 155 | FRC_CheckErrorStatus(status, "{}", "Start"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 156 | } |
| 157 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 158 | void DMA::Stop() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 159 | int32_t status = 0; |
| 160 | HAL_StopDMA(dmaHandle, &status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 161 | FRC_CheckErrorStatus(status, "{}", "Stop"); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 162 | } |