blob: 6b1e9097b85f41c5315a3489cc5e64b3ac5c6751 [file] [log] [blame]
Austin Schuh812d0d12021-11-04 20:16:48 -07001// 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 Silverman8fce7482020-01-05 13:18:21 -08004
5#pragma once
6
7#include <stdint.h>
8
9#include <memory>
Austin Schuh812d0d12021-11-04 20:16:48 -070010#include <string>
Brian Silverman8fce7482020-01-05 13:18:21 -080011
12#include <wpi/mutex.h>
13
14#include "PortsInternal.h"
15#include "hal/AnalogTrigger.h"
16#include "hal/ChipObject.h"
17#include "hal/Ports.h"
18#include "hal/Types.h"
19#include "hal/handles/DigitalHandleResource.h"
20#include "hal/handles/HandlesInternal.h"
21
22namespace hal {
23
24/**
25 * MXP channels when used as digital output PWM are offset from actual value
26 */
27constexpr int32_t kMXPDigitalPWMOffset = 6;
28
29constexpr int32_t kExpectedLoopTiming = 40;
30
31/**
32 * kDefaultPwmPeriod is in ms
33 *
34 * - 20ms periods (50 Hz) are the "safest" setting in that this works for all
35 * devices
36 * - 20ms periods seem to be desirable for Vex Motors
37 * - 20ms periods are the specified period for HS-322HD servos, but work
38 * reliably down to 10.0 ms; starting at about 8.5ms, the servo sometimes hums
39 * and get hot; by 5.0ms the hum is nearly continuous
40 * - 10ms periods work well for Victor 884
41 * - 5ms periods allows higher update rates for Luminary Micro Jaguar speed
42 * controllers. Due to the shipping firmware on the Jaguar, we can't run the
43 * update period less than 5.05 ms.
44 *
45 * kDefaultPwmPeriod is the 1x period (5.05 ms). In hardware, the period
46 * scaling is implemented as an output squelch to get longer periods for old
47 * devices.
48 */
49constexpr double kDefaultPwmPeriod = 5.05;
50/**
51 * kDefaultPwmCenter is the PWM range center in ms
52 */
53constexpr double kDefaultPwmCenter = 1.5;
54/**
55 * kDefaultPWMStepsDown is the number of PWM steps below the centerpoint
56 */
57constexpr int32_t kDefaultPwmStepsDown = 1000;
58constexpr int32_t kPwmDisabled = 0;
59
60extern std::unique_ptr<tDIO> digitalSystem;
61extern std::unique_ptr<tRelay> relaySystem;
62extern std::unique_ptr<tPWM> pwmSystem;
63extern std::unique_ptr<tSPI> spiSystem;
64
65struct DigitalPort {
66 uint8_t channel;
67 bool configSet = false;
68 bool eliminateDeadband = false;
69 int32_t maxPwm = 0;
70 int32_t deadbandMaxPwm = 0;
71 int32_t centerPwm = 0;
72 int32_t deadbandMinPwm = 0;
73 int32_t minPwm = 0;
Austin Schuh812d0d12021-11-04 20:16:48 -070074 std::string previousAllocation;
Brian Silverman8fce7482020-01-05 13:18:21 -080075};
76
77extern DigitalHandleResource<HAL_DigitalHandle, DigitalPort,
78 kNumDigitalChannels + kNumPWMHeaders>*
79 digitalChannelHandles;
80
81extern wpi::mutex digitalDIOMutex;
82
83/**
84 * Initialize the digital system.
85 */
86void initializeDigital(int32_t* status);
87
88/**
89 * remap the digital source channel and set the module.
90 * If it's an analog trigger, determine the module from the high order routing
91 * channel else do normal digital input remapping based on channel number
92 * (MXP)
93 */
94bool remapDigitalSource(HAL_Handle digitalSourceHandle,
95 HAL_AnalogTriggerType analogTriggerType,
96 uint8_t& channel, uint8_t& module, bool& analogTrigger);
97
98/**
99 * Remap the Digital Channel to map to the bitfield channel of the FPGA
100 */
101constexpr int32_t remapDigitalChannelToBitfieldChannel(int32_t channel) {
102 // First 10 are headers
Austin Schuh812d0d12021-11-04 20:16:48 -0700103 if (channel < kNumDigitalHeaders) {
104 return channel;
105 // 2nd group of 16 are mxp. So if mxp port, add 6, since they start at 10
106 } else if (channel < kNumDigitalMXPChannels) {
Brian Silverman8fce7482020-01-05 13:18:21 -0800107 return channel + 6;
Austin Schuh812d0d12021-11-04 20:16:48 -0700108 // Assume SPI, so remove MXP channels
109 } else {
Brian Silverman8fce7482020-01-05 13:18:21 -0800110 return channel - kNumDigitalMXPChannels;
Austin Schuh812d0d12021-11-04 20:16:48 -0700111 }
Brian Silverman8fce7482020-01-05 13:18:21 -0800112}
113
114/**
115 * Map DIO channel numbers from their physical number (10 to 26) to their
116 * position in the bit field.
117 */
118int32_t remapMXPChannel(int32_t channel);
119
120int32_t remapMXPPWMChannel(int32_t channel);
121
122/**
123 * Map SPI channel numbers from their physical number (27 to 31) to their
124 * position in the bit field.
125 */
126int32_t remapSPIChannel(int32_t channel);
127
128} // namespace hal