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/PWM.h" |
| 6 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 7 | #include <algorithm> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 8 | #include <cmath> |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 9 | #include <cstdio> |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 10 | #include <thread> |
| 11 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 12 | #include "ConstantsInternal.h" |
| 13 | #include "DigitalInternal.h" |
| 14 | #include "HALInitializer.h" |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 15 | #include "HALInternal.h" |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 16 | #include "PortsInternal.h" |
| 17 | #include "hal/cpp/fpga_clock.h" |
| 18 | #include "hal/handles/HandlesInternal.h" |
| 19 | |
| 20 | using namespace hal; |
| 21 | |
| 22 | static inline int32_t GetMaxPositivePwm(DigitalPort* port) { |
| 23 | return port->maxPwm; |
| 24 | } |
| 25 | |
| 26 | static inline int32_t GetMinPositivePwm(DigitalPort* port) { |
| 27 | if (port->eliminateDeadband) { |
| 28 | return port->deadbandMaxPwm; |
| 29 | } else { |
| 30 | return port->centerPwm + 1; |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | static inline int32_t GetCenterPwm(DigitalPort* port) { |
| 35 | return port->centerPwm; |
| 36 | } |
| 37 | |
| 38 | static inline int32_t GetMaxNegativePwm(DigitalPort* port) { |
| 39 | if (port->eliminateDeadband) { |
| 40 | return port->deadbandMinPwm; |
| 41 | } else { |
| 42 | return port->centerPwm - 1; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | static inline int32_t GetMinNegativePwm(DigitalPort* port) { |
| 47 | return port->minPwm; |
| 48 | } |
| 49 | |
| 50 | static inline int32_t GetPositiveScaleFactor(DigitalPort* port) { |
| 51 | return GetMaxPositivePwm(port) - GetMinPositivePwm(port); |
| 52 | } ///< The scale for positive speeds. |
| 53 | |
| 54 | static inline int32_t GetNegativeScaleFactor(DigitalPort* port) { |
| 55 | return GetMaxNegativePwm(port) - GetMinNegativePwm(port); |
| 56 | } ///< The scale for negative speeds. |
| 57 | |
| 58 | static inline int32_t GetFullRangeScaleFactor(DigitalPort* port) { |
| 59 | return GetMaxPositivePwm(port) - GetMinNegativePwm(port); |
| 60 | } ///< The scale for positions. |
| 61 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 62 | namespace hal::init { |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 63 | void InitializePWM() {} |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 64 | } // namespace hal::init |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 65 | |
| 66 | extern "C" { |
| 67 | |
| 68 | HAL_DigitalHandle HAL_InitializePWMPort(HAL_PortHandle portHandle, |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 69 | const char* allocationLocation, |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 70 | int32_t* status) { |
| 71 | hal::init::CheckInit(); |
| 72 | initializeDigital(status); |
| 73 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 74 | if (*status != 0) { |
| 75 | return HAL_kInvalidHandle; |
| 76 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 77 | |
| 78 | int16_t channel = getPortHandleChannel(portHandle); |
| 79 | if (channel == InvalidHandleIndex || channel >= kNumPWMChannels) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 80 | *status = RESOURCE_OUT_OF_RANGE; |
| 81 | hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for PWM", 0, |
| 82 | kNumPWMChannels, channel); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 83 | return HAL_kInvalidHandle; |
| 84 | } |
| 85 | |
| 86 | uint8_t origChannel = static_cast<uint8_t>(channel); |
| 87 | |
| 88 | if (origChannel < kNumPWMHeaders) { |
| 89 | channel += kNumDigitalChannels; // remap Headers to end of allocations |
| 90 | } else { |
| 91 | channel = remapMXPPWMChannel(channel) + 10; // remap MXP to proper channel |
| 92 | } |
| 93 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 94 | HAL_DigitalHandle handle; |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 95 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 96 | auto port = digitalChannelHandles->Allocate(channel, HAL_HandleEnum::PWM, |
| 97 | &handle, status); |
| 98 | |
| 99 | if (*status != 0) { |
| 100 | if (port) { |
| 101 | hal::SetLastErrorPreviouslyAllocated(status, "PWM or DIO", origChannel, |
| 102 | port->previousAllocation); |
| 103 | } else { |
| 104 | hal::SetLastErrorIndexOutOfRange(status, "Invalid Index for PWM", 0, |
| 105 | kNumPWMChannels, origChannel); |
| 106 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 107 | return HAL_kInvalidHandle; // failed to allocate. Pass error back. |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | port->channel = origChannel; |
| 111 | |
| 112 | if (port->channel > tPWM::kNumHdrRegisters - 1) { |
| 113 | int32_t bitToSet = 1 << remapMXPPWMChannel(port->channel); |
| 114 | uint16_t specialFunctions = |
| 115 | digitalSystem->readEnableMXPSpecialFunction(status); |
| 116 | digitalSystem->writeEnableMXPSpecialFunction(specialFunctions | bitToSet, |
| 117 | status); |
| 118 | } |
| 119 | |
| 120 | // Defaults to allow an always valid config. |
| 121 | HAL_SetPWMConfig(handle, 2.0, 1.501, 1.5, 1.499, 1.0, status); |
| 122 | |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 123 | port->previousAllocation = allocationLocation ? allocationLocation : ""; |
| 124 | |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 125 | return handle; |
| 126 | } |
| 127 | void HAL_FreePWMPort(HAL_DigitalHandle pwmPortHandle, int32_t* status) { |
| 128 | auto port = digitalChannelHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM); |
| 129 | if (port == nullptr) { |
| 130 | *status = HAL_HANDLE_ERROR; |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | digitalChannelHandles->Free(pwmPortHandle, HAL_HandleEnum::PWM); |
| 135 | |
| 136 | // Wait for no other object to hold this handle. |
| 137 | auto start = hal::fpga_clock::now(); |
| 138 | while (port.use_count() != 1) { |
| 139 | auto current = hal::fpga_clock::now(); |
| 140 | if (start + std::chrono::seconds(1) < current) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 141 | std::puts("PWM handle free timeout"); |
| 142 | std::fflush(stdout); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 143 | break; |
| 144 | } |
| 145 | std::this_thread::yield(); |
| 146 | } |
| 147 | |
| 148 | if (port->channel > tPWM::kNumHdrRegisters - 1) { |
| 149 | int32_t bitToUnset = 1 << remapMXPPWMChannel(port->channel); |
| 150 | uint16_t specialFunctions = |
| 151 | digitalSystem->readEnableMXPSpecialFunction(status); |
| 152 | digitalSystem->writeEnableMXPSpecialFunction(specialFunctions & ~bitToUnset, |
| 153 | status); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | HAL_Bool HAL_CheckPWMChannel(int32_t channel) { |
| 158 | return channel < kNumPWMChannels && channel >= 0; |
| 159 | } |
| 160 | |
| 161 | void HAL_SetPWMConfig(HAL_DigitalHandle pwmPortHandle, double max, |
| 162 | double deadbandMax, double center, double deadbandMin, |
| 163 | double min, int32_t* status) { |
| 164 | auto port = digitalChannelHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM); |
| 165 | if (port == nullptr) { |
| 166 | *status = HAL_HANDLE_ERROR; |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | // calculate the loop time in milliseconds |
| 171 | double loopTime = |
| 172 | HAL_GetPWMLoopTiming(status) / (kSystemClockTicksPerMicrosecond * 1e3); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 173 | if (*status != 0) { |
| 174 | return; |
| 175 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 176 | |
| 177 | int32_t maxPwm = static_cast<int32_t>((max - kDefaultPwmCenter) / loopTime + |
| 178 | kDefaultPwmStepsDown - 1); |
| 179 | int32_t deadbandMaxPwm = static_cast<int32_t>( |
| 180 | (deadbandMax - kDefaultPwmCenter) / loopTime + kDefaultPwmStepsDown - 1); |
| 181 | int32_t centerPwm = static_cast<int32_t>( |
| 182 | (center - kDefaultPwmCenter) / loopTime + kDefaultPwmStepsDown - 1); |
| 183 | int32_t deadbandMinPwm = static_cast<int32_t>( |
| 184 | (deadbandMin - kDefaultPwmCenter) / loopTime + kDefaultPwmStepsDown - 1); |
| 185 | int32_t minPwm = static_cast<int32_t>((min - kDefaultPwmCenter) / loopTime + |
| 186 | kDefaultPwmStepsDown - 1); |
| 187 | |
| 188 | port->maxPwm = maxPwm; |
| 189 | port->deadbandMaxPwm = deadbandMaxPwm; |
| 190 | port->deadbandMinPwm = deadbandMinPwm; |
| 191 | port->centerPwm = centerPwm; |
| 192 | port->minPwm = minPwm; |
| 193 | port->configSet = true; |
| 194 | } |
| 195 | |
| 196 | void HAL_SetPWMConfigRaw(HAL_DigitalHandle pwmPortHandle, int32_t maxPwm, |
| 197 | int32_t deadbandMaxPwm, int32_t centerPwm, |
| 198 | int32_t deadbandMinPwm, int32_t minPwm, |
| 199 | int32_t* status) { |
| 200 | auto port = digitalChannelHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM); |
| 201 | if (port == nullptr) { |
| 202 | *status = HAL_HANDLE_ERROR; |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | port->maxPwm = maxPwm; |
| 207 | port->deadbandMaxPwm = deadbandMaxPwm; |
| 208 | port->deadbandMinPwm = deadbandMinPwm; |
| 209 | port->centerPwm = centerPwm; |
| 210 | port->minPwm = minPwm; |
| 211 | } |
| 212 | |
| 213 | void HAL_GetPWMConfigRaw(HAL_DigitalHandle pwmPortHandle, int32_t* maxPwm, |
| 214 | int32_t* deadbandMaxPwm, int32_t* centerPwm, |
| 215 | int32_t* deadbandMinPwm, int32_t* minPwm, |
| 216 | int32_t* status) { |
| 217 | auto port = digitalChannelHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM); |
| 218 | if (port == nullptr) { |
| 219 | *status = HAL_HANDLE_ERROR; |
| 220 | return; |
| 221 | } |
| 222 | *maxPwm = port->maxPwm; |
| 223 | *deadbandMaxPwm = port->deadbandMaxPwm; |
| 224 | *deadbandMinPwm = port->deadbandMinPwm; |
| 225 | *centerPwm = port->centerPwm; |
| 226 | *minPwm = port->minPwm; |
| 227 | } |
| 228 | |
| 229 | void HAL_SetPWMEliminateDeadband(HAL_DigitalHandle pwmPortHandle, |
| 230 | HAL_Bool eliminateDeadband, int32_t* status) { |
| 231 | auto port = digitalChannelHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM); |
| 232 | if (port == nullptr) { |
| 233 | *status = HAL_HANDLE_ERROR; |
| 234 | return; |
| 235 | } |
| 236 | port->eliminateDeadband = eliminateDeadband; |
| 237 | } |
| 238 | |
| 239 | HAL_Bool HAL_GetPWMEliminateDeadband(HAL_DigitalHandle pwmPortHandle, |
| 240 | int32_t* status) { |
| 241 | auto port = digitalChannelHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM); |
| 242 | if (port == nullptr) { |
| 243 | *status = HAL_HANDLE_ERROR; |
| 244 | return false; |
| 245 | } |
| 246 | return port->eliminateDeadband; |
| 247 | } |
| 248 | |
| 249 | void HAL_SetPWMRaw(HAL_DigitalHandle pwmPortHandle, int32_t value, |
| 250 | int32_t* status) { |
| 251 | auto port = digitalChannelHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM); |
| 252 | if (port == nullptr) { |
| 253 | *status = HAL_HANDLE_ERROR; |
| 254 | return; |
| 255 | } |
| 256 | |
| 257 | if (port->channel < tPWM::kNumHdrRegisters) { |
| 258 | pwmSystem->writeHdr(port->channel, value, status); |
| 259 | } else { |
| 260 | pwmSystem->writeMXP(port->channel - tPWM::kNumHdrRegisters, value, status); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | void HAL_SetPWMSpeed(HAL_DigitalHandle pwmPortHandle, double speed, |
| 265 | int32_t* status) { |
| 266 | auto port = digitalChannelHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM); |
| 267 | if (port == nullptr) { |
| 268 | *status = HAL_HANDLE_ERROR; |
| 269 | return; |
| 270 | } |
| 271 | if (!port->configSet) { |
| 272 | *status = INCOMPATIBLE_STATE; |
| 273 | return; |
| 274 | } |
| 275 | |
| 276 | DigitalPort* dPort = port.get(); |
| 277 | |
| 278 | if (std::isfinite(speed)) { |
| 279 | speed = std::clamp(speed, -1.0, 1.0); |
| 280 | } else { |
| 281 | speed = 0.0; |
| 282 | } |
| 283 | |
| 284 | // calculate the desired output pwm value by scaling the speed appropriately |
| 285 | int32_t rawValue; |
| 286 | if (speed == 0.0) { |
| 287 | rawValue = GetCenterPwm(dPort); |
| 288 | } else if (speed > 0.0) { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 289 | rawValue = |
| 290 | std::lround(speed * static_cast<double>(GetPositiveScaleFactor(dPort)) + |
| 291 | static_cast<double>(GetMinPositivePwm(dPort))); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 292 | } else { |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 293 | rawValue = |
| 294 | std::lround(speed * static_cast<double>(GetNegativeScaleFactor(dPort)) + |
| 295 | static_cast<double>(GetMaxNegativePwm(dPort))); |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 296 | } |
| 297 | |
| 298 | if (!((rawValue >= GetMinNegativePwm(dPort)) && |
| 299 | (rawValue <= GetMaxPositivePwm(dPort))) || |
| 300 | rawValue == kPwmDisabled) { |
| 301 | *status = HAL_PWM_SCALE_ERROR; |
| 302 | return; |
| 303 | } |
| 304 | |
| 305 | HAL_SetPWMRaw(pwmPortHandle, rawValue, status); |
| 306 | } |
| 307 | |
| 308 | void HAL_SetPWMPosition(HAL_DigitalHandle pwmPortHandle, double pos, |
| 309 | int32_t* status) { |
| 310 | auto port = digitalChannelHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM); |
| 311 | if (port == nullptr) { |
| 312 | *status = HAL_HANDLE_ERROR; |
| 313 | return; |
| 314 | } |
| 315 | if (!port->configSet) { |
| 316 | *status = INCOMPATIBLE_STATE; |
| 317 | return; |
| 318 | } |
| 319 | DigitalPort* dPort = port.get(); |
| 320 | |
| 321 | if (pos < 0.0) { |
| 322 | pos = 0.0; |
| 323 | } else if (pos > 1.0) { |
| 324 | pos = 1.0; |
| 325 | } |
| 326 | |
| 327 | // note, need to perform the multiplication below as floating point before |
| 328 | // converting to int |
| 329 | int32_t rawValue = static_cast<int32_t>( |
| 330 | (pos * static_cast<double>(GetFullRangeScaleFactor(dPort))) + |
| 331 | GetMinNegativePwm(dPort)); |
| 332 | |
| 333 | if (rawValue == kPwmDisabled) { |
| 334 | *status = HAL_PWM_SCALE_ERROR; |
| 335 | return; |
| 336 | } |
| 337 | |
| 338 | HAL_SetPWMRaw(pwmPortHandle, rawValue, status); |
| 339 | } |
| 340 | |
| 341 | void HAL_SetPWMDisabled(HAL_DigitalHandle pwmPortHandle, int32_t* status) { |
| 342 | HAL_SetPWMRaw(pwmPortHandle, kPwmDisabled, status); |
| 343 | } |
| 344 | |
| 345 | int32_t HAL_GetPWMRaw(HAL_DigitalHandle pwmPortHandle, int32_t* status) { |
| 346 | auto port = digitalChannelHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM); |
| 347 | if (port == nullptr) { |
| 348 | *status = HAL_HANDLE_ERROR; |
| 349 | return 0; |
| 350 | } |
| 351 | |
| 352 | if (port->channel < tPWM::kNumHdrRegisters) { |
| 353 | return pwmSystem->readHdr(port->channel, status); |
| 354 | } else { |
| 355 | return pwmSystem->readMXP(port->channel - tPWM::kNumHdrRegisters, status); |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | double HAL_GetPWMSpeed(HAL_DigitalHandle pwmPortHandle, int32_t* status) { |
| 360 | auto port = digitalChannelHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM); |
| 361 | if (port == nullptr) { |
| 362 | *status = HAL_HANDLE_ERROR; |
| 363 | return 0; |
| 364 | } |
| 365 | if (!port->configSet) { |
| 366 | *status = INCOMPATIBLE_STATE; |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | int32_t value = HAL_GetPWMRaw(pwmPortHandle, status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 371 | if (*status != 0) { |
| 372 | return 0; |
| 373 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 374 | DigitalPort* dPort = port.get(); |
| 375 | |
| 376 | if (value == kPwmDisabled) { |
| 377 | return 0.0; |
| 378 | } else if (value > GetMaxPositivePwm(dPort)) { |
| 379 | return 1.0; |
| 380 | } else if (value < GetMinNegativePwm(dPort)) { |
| 381 | return -1.0; |
| 382 | } else if (value > GetMinPositivePwm(dPort)) { |
| 383 | return static_cast<double>(value - GetMinPositivePwm(dPort)) / |
| 384 | static_cast<double>(GetPositiveScaleFactor(dPort)); |
| 385 | } else if (value < GetMaxNegativePwm(dPort)) { |
| 386 | return static_cast<double>(value - GetMaxNegativePwm(dPort)) / |
| 387 | static_cast<double>(GetNegativeScaleFactor(dPort)); |
| 388 | } else { |
| 389 | return 0.0; |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | double HAL_GetPWMPosition(HAL_DigitalHandle pwmPortHandle, int32_t* status) { |
| 394 | auto port = digitalChannelHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM); |
| 395 | if (port == nullptr) { |
| 396 | *status = HAL_HANDLE_ERROR; |
| 397 | return 0; |
| 398 | } |
| 399 | if (!port->configSet) { |
| 400 | *status = INCOMPATIBLE_STATE; |
| 401 | return 0; |
| 402 | } |
| 403 | |
| 404 | int32_t value = HAL_GetPWMRaw(pwmPortHandle, status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 405 | if (*status != 0) { |
| 406 | return 0; |
| 407 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 408 | DigitalPort* dPort = port.get(); |
| 409 | |
| 410 | if (value < GetMinNegativePwm(dPort)) { |
| 411 | return 0.0; |
| 412 | } else if (value > GetMaxPositivePwm(dPort)) { |
| 413 | return 1.0; |
| 414 | } else { |
| 415 | return static_cast<double>(value - GetMinNegativePwm(dPort)) / |
| 416 | static_cast<double>(GetFullRangeScaleFactor(dPort)); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | void HAL_LatchPWMZero(HAL_DigitalHandle pwmPortHandle, int32_t* status) { |
| 421 | auto port = digitalChannelHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM); |
| 422 | if (port == nullptr) { |
| 423 | *status = HAL_HANDLE_ERROR; |
| 424 | return; |
| 425 | } |
| 426 | |
| 427 | pwmSystem->writeZeroLatch(port->channel, true, status); |
| 428 | pwmSystem->writeZeroLatch(port->channel, false, status); |
| 429 | } |
| 430 | |
| 431 | void HAL_SetPWMPeriodScale(HAL_DigitalHandle pwmPortHandle, int32_t squelchMask, |
| 432 | int32_t* status) { |
| 433 | auto port = digitalChannelHandles->Get(pwmPortHandle, HAL_HandleEnum::PWM); |
| 434 | if (port == nullptr) { |
| 435 | *status = HAL_HANDLE_ERROR; |
| 436 | return; |
| 437 | } |
| 438 | |
| 439 | if (port->channel < tPWM::kNumPeriodScaleHdrElements) { |
| 440 | pwmSystem->writePeriodScaleHdr(port->channel, squelchMask, status); |
| 441 | } else { |
| 442 | pwmSystem->writePeriodScaleMXP( |
| 443 | port->channel - tPWM::kNumPeriodScaleHdrElements, squelchMask, status); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | int32_t HAL_GetPWMLoopTiming(int32_t* status) { |
| 448 | initializeDigital(status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 449 | if (*status != 0) { |
| 450 | return 0; |
| 451 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 452 | return pwmSystem->readLoopTiming(status); |
| 453 | } |
| 454 | |
| 455 | uint64_t HAL_GetPWMCycleStartTime(int32_t* status) { |
| 456 | initializeDigital(status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 457 | if (*status != 0) { |
| 458 | return 0; |
| 459 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 460 | |
| 461 | uint64_t upper1 = pwmSystem->readCycleStartTimeUpper(status); |
| 462 | uint32_t lower = pwmSystem->readCycleStartTime(status); |
| 463 | uint64_t upper2 = pwmSystem->readCycleStartTimeUpper(status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 464 | if (*status != 0) { |
| 465 | return 0; |
| 466 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 467 | if (upper1 != upper2) { |
| 468 | // Rolled over between the lower call, reread lower |
| 469 | lower = pwmSystem->readCycleStartTime(status); |
Austin Schuh | 812d0d1 | 2021-11-04 20:16:48 -0700 | [diff] [blame^] | 470 | if (*status != 0) { |
| 471 | return 0; |
| 472 | } |
Brian Silverman | 8fce748 | 2020-01-05 13:18:21 -0800 | [diff] [blame] | 473 | } |
| 474 | return (upper2 << 32) + lower; |
| 475 | } |
| 476 | |
| 477 | } // extern "C" |