-
Notifications
You must be signed in to change notification settings - Fork 0
/
DigitalModule.cpp
363 lines (326 loc) · 10.3 KB
/
DigitalModule.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
/*----------------------------------------------------------------------------*/
/* Copyright (c) FIRST 2008. All Rights Reserved. */
/* Open Source Software - may be modified and shared by FRC teams. The code */
/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
/*----------------------------------------------------------------------------*/
#include "DigitalModule.h"
#include "I2C.h"
#include "PWM.h"
#include "Resource.h"
#include "Synchronized.h"
#include "Utility.h"
#include "WPIStatus.h"
static Resource *DIOChannels = NULL;
/**
* Get an instance of an Digital Module.
* Singleton digital module creation where a module is allocated on the first use
* and the same module is returned on subsequent uses.
*/
DigitalModule* DigitalModule::GetInstance(UINT32 slot)
{
CheckDigitalModule(slot);
if (m_modules[slot] == NULL)
{
m_modules[slot] = new DigitalModule(slot);
}
return (DigitalModule*)m_modules[slot];
}
UINT32 DigitalModule::SlotToIndex(UINT32 slot)
{
const UINT32 mapping[] = {0,0,0,0,0,1,0,0};
return mapping[slot - 1];
}
/**
* Create a new instance of an digital module.
* Create an instance of the digital module object. Initialize all the parameters
* to reasonable values on start.
* Setting a global value on an digital module can be done only once unless subsequent
* values are set the previously set value.
* Digital modules are a singleton, so the constructor is never called outside of this class.
*/
DigitalModule::DigitalModule(UINT32 slot)
: Module(slot)
, m_fpgaDIO (NULL)
{
Resource::CreateResourceObject(&DIOChannels, tDIO::kNumSystems * kDigitalChannels);
m_fpgaDIO = new tDIO(SlotToIndex(m_slot), &status);
// Make sure that the 9403 IONode has had a chance to initialize before continuing.
while(m_fpgaDIO->readLoopTiming(&status) == 0) taskDelay(1);
if (m_fpgaDIO->readLoopTiming(&status) != kExpectedLoopTiming)
{
wpi_fatal(LoopTimingError);
printf("DIO LoopTiming: %d, expecting: %d\n", m_fpgaDIO->readLoopTiming(&status), kExpectedLoopTiming);
}
m_fpgaDIO->writePWMConfig_Period(PWM::kDefaultPwmPeriod, &status);
m_fpgaDIO->writePWMConfig_MinHigh(PWM::kDefaultMinPwmHigh, &status);
// Ensure that PWM output values are set to OFF
for (UINT32 pwm_index = 1; pwm_index <= kPwmChannels; pwm_index++)
{
SetPWM(pwm_index, PWM::kPwmDisabled);
SetPWMPeriodScale(pwm_index, 3); // Set all to 4x by default.
}
// Turn off all relay outputs.
m_fpgaDIO->writeSlowValue_RelayFwd(0, &status);
m_fpgaDIO->writeSlowValue_RelayRev(0, &status);
// Create a semaphore to protect changes to the relay values
m_relaySemaphore = semMCreate(SEM_Q_PRIORITY | SEM_DELETE_SAFE | SEM_INVERSION_SAFE);
AddToSingletonList();
}
DigitalModule::~DigitalModule()
{
semDelete(m_relaySemaphore);
m_relaySemaphore = NULL;
delete m_fpgaDIO;
m_modules[m_slot] = NULL;
}
/**
* Set a PWM channel to the desired value. The values range from 0 to 255 and the period is controlled
* by the PWM Period and MinHigh registers.
*
* @param channel The PWM channel to set.
* @param value The PWM value to set.
*/
void DigitalModule::SetPWM(UINT32 channel, UINT8 value)
{
CheckPWMChannel(channel);
m_fpgaDIO->writePWMValue(channel - 1, value, &status);
}
/**
* Get a value from a PWM channel. The values range from 0 to 255.
*
* @param channel The PWM channel to read from.
* @return The raw PWM value.
*/
UINT8 DigitalModule::GetPWM(UINT32 channel)
{
CheckPWMChannel(channel);
return m_fpgaDIO->readPWMValue(channel - 1, &status);
}
/**
* Set how how often the PWM signal is squelched, thus scaling the period.
*
* @param channel The PWM channel to configure.
* @param squelchMask The 2-bit mask of outputs to squelch.
*/
void DigitalModule::SetPWMPeriodScale(UINT32 channel, UINT32 squelchMask)
{
CheckPWMChannel(channel);
m_fpgaDIO->writePWMPeriodScale(channel - 1, squelchMask, &status);
}
/**
* Set the state of a relay.
* Set the state of a relay output to be forward. Relays have two outputs and each is
* independently set to 0v or 12v.
*/
void DigitalModule::SetRelayForward(UINT32 channel, bool on)
{
status = 0;
CheckRelayChannel(channel);
{
Synchronized sync(m_relaySemaphore);
UINT8 forwardRelays = m_fpgaDIO->readSlowValue_RelayFwd(&status);
if (on)
forwardRelays |= 1 << (channel - 1);
else
forwardRelays &= ~(1 << (channel - 1));
m_fpgaDIO->writeSlowValue_RelayFwd(forwardRelays, &status);
}
wpi_assertCleanStatus(status);
}
/**
* Set the state of a relay.
* Set the state of a relay output to be reverse. Relays have two outputs and each is
* independently set to 0v or 12v.
*/
void DigitalModule::SetRelayReverse(UINT32 channel, bool on)
{
status = 0;
CheckRelayChannel(channel);
{
Synchronized sync(m_relaySemaphore);
UINT8 reverseRelays = m_fpgaDIO->readSlowValue_RelayRev(&status);
if (on)
reverseRelays |= 1 << (channel - 1);
else
reverseRelays &= ~(1 << (channel - 1));
m_fpgaDIO->writeSlowValue_RelayRev(reverseRelays, &status);
}
wpi_assertCleanStatus(status);
}
/**
* Get the current state of the forward relay channel
*/
bool DigitalModule::GetRelayForward(UINT32 channel)
{
status = 0;
UINT8 forwardRelays = m_fpgaDIO->readSlowValue_RelayFwd(&status);
return (forwardRelays & (1 << (channel - 1))) != 0;
}
/**
* Get the current state of all of the forward relay channels on this module.
*/
UINT8 DigitalModule::GetRelayForward(void)
{
status = 0;
return m_fpgaDIO->readSlowValue_RelayFwd(&status);
}
/**
* Get the current state of the reverse relay channel
*/
bool DigitalModule::GetRelayReverse(UINT32 channel)
{
status = 0;
UINT8 reverseRelays = m_fpgaDIO->readSlowValue_RelayRev(&status);
return (reverseRelays & (1 << (channel - 1))) != 0;
}
/**
* Get the current state of all of the reverse relay channels on this module.
*/
UINT8 DigitalModule::GetRelayReverse(void)
{
status = 0;
return m_fpgaDIO->readSlowValue_RelayRev(&status);
}
/**
* Allocate Digital I/O channels.
* Allocate channels so that they are not accidently reused. Also the direction is set at the
* time of the allocation.
*/
bool DigitalModule::AllocateDIO(UINT32 channel, bool input)
{
status = 0;
DIOChannels->Allocate(kDigitalChannels * SlotToIndex(m_slot) + channel - 1);
UINT32 outputEnable = m_fpgaDIO->readOutputEnable(&status);
UINT32 bitToSet = 1 << (RemapDigitalChannel(channel - 1));
UINT32 outputEnableValue;
if (input)
{
outputEnableValue = outputEnable & (~ bitToSet ); // clear the bit for read
}
else
{
outputEnableValue = outputEnable | bitToSet; // set the bit for write
}
m_fpgaDIO->writeOutputEnable(outputEnableValue, &status);
wpi_assertCleanStatus(status);
return true;
}
/**
* Free the resource associated with a digital I/O channel.
*/
void DigitalModule::FreeDIO(UINT32 channel)
{
DIOChannels->Free(kDigitalChannels * SlotToIndex(m_slot) + channel - 1);
}
/**
* Write a digital I/O bit to the FPGA.
* Set a single value on a digital I/O channel.
*/
void DigitalModule::SetDIO(UINT32 channel, short value)
{
status = 0;
if (value != 0 && value != 1)
{
wpi_fatal(NonBinaryDigitalValue);
if (value != 0)
value = 1;
}
UINT16 currentDIO = m_fpgaDIO->readDO(&status);
if(value == 0)
{
currentDIO = currentDIO & ~(1 << RemapDigitalChannel(channel - 1));
}
else if (value == 1)
{
currentDIO = currentDIO | (1 << RemapDigitalChannel(channel - 1));
}
m_fpgaDIO->writeDO(currentDIO, &status);
wpi_assertCleanStatus(status);
}
/**
* Read a digital I/O bit from the FPGA.
* Get a single value from a digital I/O channel.
*/
bool DigitalModule::GetDIO(UINT32 channel)
{
status = 0;
UINT32 currentDIO = m_fpgaDIO->readDI(&status);
//Shift 00000001 over channel-1 places.
//AND it against the currentDIO
//if it == 0, then return false
//else return true
wpi_assertCleanStatus(status);
return ((currentDIO >> RemapDigitalChannel(channel - 1)) & 1) != 0;
}
/**
* Read the state of all the Digital I/O lines from the FPGA
* These are not remapped to logical order. They are still in hardware order.
*/
UINT16 DigitalModule::GetDIO(void)
{
status = 0;
return m_fpgaDIO->readDI(&status);
}
/**
* Read the direction of a the Digital I/O lines
* A 1 bit means output and a 0 bit means input.
*/
bool DigitalModule::GetDIODirection(UINT32 channel)
{
status = 0;
UINT32 currentOutputEnable = m_fpgaDIO->readOutputEnable(&status);
//Shift 00000001 over channel-1 places.
//AND it against the currentOutputEnable
//if it == 0, then return false
//else return true
wpi_assertCleanStatus(status);
return ((currentOutputEnable >> RemapDigitalChannel(channel - 1)) & 1) != 0;
}
/**
* Read the direction of all the Digital I/O lines from the FPGA
* A 1 bit means output and a 0 bit means input.
* These are not remapped to logical order. They are still in hardware order.
*/
UINT16 DigitalModule::GetDIODirection(void)
{
status = 0;
return m_fpgaDIO->readOutputEnable(&status);
}
/**
* Generate a single pulse.
* Write a pulse to the specified digital output channel. There can only be a single pulse going at any time.
*/
void DigitalModule::Pulse(UINT32 channel, float pulseLength)
{
UINT16 mask = 1 << RemapDigitalChannel(channel - 1);
m_fpgaDIO->writePulseLength((UINT8)(1.0e9 * pulseLength / (m_fpgaDIO->readLoopTiming(&status) * 25)), &status);
m_fpgaDIO->writePulse(mask, &status);
wpi_assertCleanStatus(status);
}
/**
* Check a DIO line to see if it is currently generating a pulse.
*/
bool DigitalModule::IsPulsing(UINT32 channel)
{
UINT16 mask = 1 << RemapDigitalChannel(channel - 1);
UINT16 pulseRegister = m_fpgaDIO->readPulse(&status);
wpi_assertCleanStatus(status);
return (pulseRegister & mask) != 0;
}
/**
* Check if any DIO line is currently generating a pulse.
*/
bool DigitalModule::IsPulsing()
{
UINT16 pulseRegister = m_fpgaDIO->readPulse(&status);
wpi_assertCleanStatus(status);
return pulseRegister != 0;
}
/**
* Return a pointer to an I2C object for this digital module
* The caller is responsible for deleting the pointer.
*/
I2C* DigitalModule::GetI2C(UINT32 address)
{
return new I2C(this, address);
}