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 "hal/Interrupts.h" |
| 6 | |
| 7 | #include <memory> |
| 8 | |
| 9 | #include <wpi/SafeThread.h> |
| 10 | |
| 11 | #include "DigitalInternal.h" |
| 12 | #include "HALInitializer.h" |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 13 | #include "HALInternal.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 14 | #include "PortsInternal.h" |
| 15 | #include "hal/ChipObject.h" |
| 16 | #include "hal/Errors.h" |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 17 | #include "hal/HALBase.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 18 | #include "hal/handles/HandlesInternal.h" |
| 19 | #include "hal/handles/LimitedHandleResource.h" |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 20 | #include "hal/roborio/InterruptManager.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 21 | |
| 22 | using namespace hal; |
| 23 | |
| 24 | namespace { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 25 | |
| 26 | struct Interrupt { |
| 27 | std::unique_ptr<tInterrupt> anInterrupt; |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 28 | InterruptManager& manager = InterruptManager::GetInstance(); |
| 29 | NiFpga_IrqContext irqContext = nullptr; |
| 30 | uint32_t mask; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | } // namespace |
| 34 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 35 | static LimitedHandleResource<HAL_InterruptHandle, Interrupt, kNumInterrupts, |
| 36 | HAL_HandleEnum::Interrupt>* interruptHandles; |
| 37 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 38 | namespace hal::init { |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 39 | void InitializeInterrupts() { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 40 | static LimitedHandleResource<HAL_InterruptHandle, Interrupt, kNumInterrupts, |
| 41 | HAL_HandleEnum::Interrupt> |
| 42 | iH; |
| 43 | interruptHandles = &iH; |
| 44 | } |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 45 | } // namespace hal::init |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 46 | |
| 47 | extern "C" { |
| 48 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 49 | HAL_InterruptHandle HAL_InitializeInterrupts(int32_t* status) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 50 | hal::init::CheckInit(); |
| 51 | HAL_InterruptHandle handle = interruptHandles->Allocate(); |
| 52 | if (handle == HAL_kInvalidHandle) { |
| 53 | *status = NO_AVAILABLE_RESOURCES; |
| 54 | return HAL_kInvalidHandle; |
| 55 | } |
| 56 | auto anInterrupt = interruptHandles->Get(handle); |
| 57 | uint32_t interruptIndex = static_cast<uint32_t>(getHandleIndex(handle)); |
| 58 | // Expects the calling leaf class to allocate an interrupt index. |
| 59 | anInterrupt->anInterrupt.reset(tInterrupt::create(interruptIndex, status)); |
| 60 | anInterrupt->anInterrupt->writeConfig_WaitForAck(false, status); |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 61 | anInterrupt->irqContext = anInterrupt->manager.GetContext(); |
| 62 | anInterrupt->mask = (1u << interruptIndex) | (1u << (interruptIndex + 8u)); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 63 | return handle; |
| 64 | } |
| 65 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 66 | void HAL_CleanInterrupts(HAL_InterruptHandle interruptHandle) { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 67 | auto anInterrupt = interruptHandles->Get(interruptHandle); |
| 68 | interruptHandles->Free(interruptHandle); |
| 69 | if (anInterrupt == nullptr) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame] | 70 | return; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 71 | } |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 72 | if (anInterrupt->irqContext) { |
| 73 | anInterrupt->manager.ReleaseContext(anInterrupt->irqContext); |
| 74 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | int64_t HAL_WaitForInterrupt(HAL_InterruptHandle interruptHandle, |
| 78 | double timeout, HAL_Bool ignorePrevious, |
| 79 | int32_t* status) { |
| 80 | uint32_t result; |
| 81 | auto anInterrupt = interruptHandles->Get(interruptHandle); |
| 82 | if (anInterrupt == nullptr) { |
| 83 | *status = HAL_HANDLE_ERROR; |
| 84 | return 0; |
| 85 | } |
| 86 | |
James Kuszmaul | cf32412 | 2023-01-14 14:07:17 -0800 | [diff] [blame^] | 87 | result = anInterrupt->manager.WaitForInterrupt( |
| 88 | anInterrupt->irqContext, anInterrupt->mask, ignorePrevious, |
| 89 | static_cast<uint32_t>(timeout * 1e3), status); |
| 90 | |
| 91 | // Don't report a timeout as an error - the return code is enough to tell |
| 92 | // that a timeout happened. |
| 93 | if (*status == -NiFpga_Status_IrqTimeout) { |
| 94 | *status = NiFpga_Status_Success; |
| 95 | } |
| 96 | |
| 97 | return result; |
| 98 | } |
| 99 | |
| 100 | int64_t HAL_WaitForMultipleInterrupts(HAL_InterruptHandle interruptHandle, |
| 101 | int64_t mask, double timeout, |
| 102 | HAL_Bool ignorePrevious, |
| 103 | int32_t* status) { |
| 104 | uint32_t result; |
| 105 | auto anInterrupt = interruptHandles->Get(interruptHandle); |
| 106 | if (anInterrupt == nullptr) { |
| 107 | *status = HAL_HANDLE_ERROR; |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | result = anInterrupt->manager.WaitForInterrupt( |
| 112 | anInterrupt->irqContext, mask, ignorePrevious, |
| 113 | static_cast<uint32_t>(timeout * 1e3), status); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 114 | |
| 115 | // Don't report a timeout as an error - the return code is enough to tell |
| 116 | // that a timeout happened. |
| 117 | if (*status == -NiFpga_Status_IrqTimeout) { |
| 118 | *status = NiFpga_Status_Success; |
| 119 | } |
| 120 | |
| 121 | return result; |
| 122 | } |
| 123 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 124 | int64_t HAL_ReadInterruptRisingTimestamp(HAL_InterruptHandle interruptHandle, |
| 125 | int32_t* status) { |
| 126 | auto anInterrupt = interruptHandles->Get(interruptHandle); |
| 127 | if (anInterrupt == nullptr) { |
| 128 | *status = HAL_HANDLE_ERROR; |
| 129 | return 0; |
| 130 | } |
| 131 | uint32_t timestamp = anInterrupt->anInterrupt->readRisingTimeStamp(status); |
| 132 | return timestamp; |
| 133 | } |
| 134 | |
| 135 | int64_t HAL_ReadInterruptFallingTimestamp(HAL_InterruptHandle interruptHandle, |
| 136 | int32_t* status) { |
| 137 | auto anInterrupt = interruptHandles->Get(interruptHandle); |
| 138 | if (anInterrupt == nullptr) { |
| 139 | *status = HAL_HANDLE_ERROR; |
| 140 | return 0; |
| 141 | } |
| 142 | uint32_t timestamp = anInterrupt->anInterrupt->readFallingTimeStamp(status); |
| 143 | return timestamp; |
| 144 | } |
| 145 | |
| 146 | void HAL_RequestInterrupts(HAL_InterruptHandle interruptHandle, |
| 147 | HAL_Handle digitalSourceHandle, |
| 148 | HAL_AnalogTriggerType analogTriggerType, |
| 149 | int32_t* status) { |
| 150 | auto anInterrupt = interruptHandles->Get(interruptHandle); |
| 151 | if (anInterrupt == nullptr) { |
| 152 | *status = HAL_HANDLE_ERROR; |
| 153 | return; |
| 154 | } |
| 155 | anInterrupt->anInterrupt->writeConfig_WaitForAck(false, status); |
| 156 | bool routingAnalogTrigger = false; |
| 157 | uint8_t routingChannel = 0; |
| 158 | uint8_t routingModule = 0; |
| 159 | bool success = |
| 160 | remapDigitalSource(digitalSourceHandle, analogTriggerType, routingChannel, |
| 161 | routingModule, routingAnalogTrigger); |
| 162 | if (!success) { |
| 163 | *status = HAL_HANDLE_ERROR; |
| 164 | return; |
| 165 | } |
| 166 | anInterrupt->anInterrupt->writeConfig_Source_AnalogTrigger( |
| 167 | routingAnalogTrigger, status); |
| 168 | anInterrupt->anInterrupt->writeConfig_Source_Channel(routingChannel, status); |
| 169 | anInterrupt->anInterrupt->writeConfig_Source_Module(routingModule, status); |
| 170 | } |
| 171 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 172 | void HAL_SetInterruptUpSourceEdge(HAL_InterruptHandle interruptHandle, |
| 173 | HAL_Bool risingEdge, HAL_Bool fallingEdge, |
| 174 | int32_t* status) { |
| 175 | auto anInterrupt = interruptHandles->Get(interruptHandle); |
| 176 | if (anInterrupt == nullptr) { |
| 177 | *status = HAL_HANDLE_ERROR; |
| 178 | return; |
| 179 | } |
| 180 | anInterrupt->anInterrupt->writeConfig_RisingEdge(risingEdge, status); |
| 181 | anInterrupt->anInterrupt->writeConfig_FallingEdge(fallingEdge, status); |
| 182 | } |
| 183 | |
Austin Schuh | 1e69f94 | 2020-11-14 15:06:14 -0800 | [diff] [blame] | 184 | void HAL_ReleaseWaitingInterrupt(HAL_InterruptHandle interruptHandle, |
| 185 | int32_t* status) { |
| 186 | auto anInterrupt = interruptHandles->Get(interruptHandle); |
| 187 | if (anInterrupt == nullptr) { |
| 188 | *status = HAL_HANDLE_ERROR; |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | uint32_t interruptIndex = |
| 193 | static_cast<uint32_t>(getHandleIndex(interruptHandle)); |
| 194 | |
| 195 | hal::ReleaseFPGAInterrupt(interruptIndex); |
| 196 | hal::ReleaseFPGAInterrupt(interruptIndex + 8); |
| 197 | } |
| 198 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 199 | } // extern "C" |