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