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