-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSPiiPlusAuxDriver.cpp
More file actions
302 lines (233 loc) · 9.32 KB
/
Copy pathSPiiPlusAuxDriver.cpp
File metadata and controls
302 lines (233 loc) · 9.32 KB
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
#include <stdlib.h>
#include <sstream>
#include <iocsh.h>
#include <epicsThread.h>
#include <epicsExit.h>
// asynMotorController.h includes asynPortDriver.h
#include <asynMotorController.h>
#include <asynMotorAxis.h>
#include <epicsExport.h>
#include "SPiiPlusAuxDriver.h"
static const char *driverName = "SPiiPlusAuxIO";
/* C Function which runs the profile thread */
static void SPiiPlusAuxIOThreadC(void *pPvt)
{
SPiiPlusAuxIO *pAux = (SPiiPlusAuxIO*)pPvt;
pAux->pollerThread();
}
static void shutdownCallback(void *pPvt)
{
SPiiPlusAuxIO *pAux = static_cast<SPiiPlusAuxIO *>(pPvt);
pAux->lock();
pAux->shuttingDown_ = 1;
pAux->unlock();
}
SPiiPlusAuxIO::SPiiPlusAuxIO(const char *ACSAuxPortName, const char* asynPortName, int numChannels, double pollPeriod)
: asynPortDriver(ACSAuxPortName, numChannels,
asynInt32Mask | asynFloat64Mask | asynUInt32DigitalMask | asynDrvUserMask, // Interfaces that we implement
asynInt32Mask | asynFloat64Mask | asynUInt32DigitalMask, // Interfaces that do callbacks
ASYN_MULTIDEVICE | ASYN_CANBLOCK, 1, /* ASYN_CANBLOCK=1, ASYN_MULTIDEVICE=1, autoConnect=1 */
0, 0), /* Default priority and stack size */
pollPeriod_(pollPeriod),
forceCallback_(1)
{
const char* ACSCommPortSuffix = "Comm";
char* ACSCommPortName;
ACSCommPortName = (char *) malloc(strlen(ACSAuxPortName) + strlen(ACSCommPortSuffix));
strcpy(ACSCommPortName, ACSAuxPortName);
strcat(ACSCommPortName, ACSCommPortSuffix);
pComm_ = new SPiiPlusComm(ACSCommPortName, asynPortName, numChannels);
/* Set an EPICS exit handler that will shut down polling before asyn kills the IP sockets */
epicsAtExit(shutdownCallback, this);
/*
* MP4U controllers have significantly larger arrays for the AIN, AOUT, IN and OUT commands than the controller
* has I/O channels. Assume the user will specify a numChannels that is appropriate for their controller.
*/
// Digital I/O parameters
createParam(digitalInputString, asynParamUInt32Digital, &digitalInput_);
createParam(digitalOutputString, asynParamUInt32Digital, &digitalOutput_);
createParam(analogInputString, asynParamFloat64, &analogInput_);
createParam(analogOutputString, asynParamFloat64, &analogOutput_);
/* Start the thread to poll digital inputs and do callbacks to
* device support */
epicsThreadCreate("SPiiPlusAuxIOPoller",
epicsThreadPriorityLow,
epicsThreadGetStackSize(epicsThreadStackMedium),
(EPICSTHREADFUNC)SPiiPlusAuxIOThreadC,
this);
}
// This is needed to resolve a build error: undefined reference to `vtable for SPiiPlusAuxIO'
SPiiPlusAuxIO::~SPiiPlusAuxIO()
{
// Does the SPiiPlusComm destructor need to be explicitly called here?
}
asynStatus SPiiPlusAuxIO::writeBits(epicsUInt32 chan, epicsUInt32 mask, epicsUInt32 value)
{
asynStatus status=asynSuccess;
std::stringstream cmd;
int i;
epicsUInt32 outValue=0, outMask;
static const char *functionName = "writeBits";
if (mask == 0xffffffff)
{
// Use OUT(#) to set all 32-bits simultaneously
cmd << "OUT(" << chan << ") = " << value;
status = pComm_->writeReadAck(cmd);
}
else
{
// Use OUT(#).# to set the desired bits individually
for (i=0, outMask=1; i<MAX_BITS; i++, outMask = (outMask<<1)) {
//
outValue = ((value & outMask) == 0) ? 0 : 1;
if ((mask & outMask) != 0) {
// Only write the value if the mask has this bit set
cmd << "OUT(" << chan << ")." << i << " = " << outValue;
status = pComm_->writeReadAck(cmd);
}
}
}
if (status == asynSuccess) {
asynPrint(this->pasynUserSelf, ASYN_TRACEIO_DRIVER,
"%s:%s, port %s, wrote outValue=0x%x, value=0x%x, mask=0x%x, chan=%d\n",
driverName, functionName, this->portName, outValue, value, mask, chan);
} else {
asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
"%s:%s, port %s, ERROR writing outValue=0x%x, value=0x%x, mask=0x%x, chan=%d, status=%d\n",
driverName, functionName, this->portName, outValue, value, mask, chan, status);
}
return status;
}
asynStatus SPiiPlusAuxIO::writeUInt32Digital(asynUser *pasynUser, epicsUInt32 value, epicsUInt32 mask)
{
int function = pasynUser->reason;
int status=asynSuccess;
int chan;
//static const char *functionName = "writeUInt32Digital";
// Get the channel num
status = getAddress(pasynUser, &chan);
setUIntDigitalParam(chan, function, value, mask);
if (function == digitalOutput_) {
status |= writeBits(chan, mask, value);
}
callParamCallbacks();
return (status==0) ? asynSuccess : asynError;
}
asynStatus SPiiPlusAuxIO::writeAnalog(epicsUInt32 chan, epicsFloat64 value)
{
asynStatus status=asynSuccess;
std::stringstream cmd;
static const char *functionName = "writeAnalog";
// Enforce the limits to avoid controller errors
if (value > 100.0)
value = 100.0;
if (value < -100.0)
value = -100.0;
// The analog output value is in percent [-100, 100]
cmd << "AOUT(" << chan << ") = " << value;
status = pComm_->writeReadAck(cmd);
if (status == asynSuccess) {
asynPrint(this->pasynUserSelf, ASYN_TRACEIO_DRIVER,
"%s:%s, port %s, wrote value=%lf, chan=%d\n",
driverName, functionName, this->portName, value, chan);
} else {
asynPrint(this->pasynUserSelf, ASYN_TRACE_ERROR,
"%s:%s, port %s, ERROR writing value=%lf, chan=%d, status=%d\n",
driverName, functionName, this->portName, value, chan, status);
}
return status;
}
asynStatus SPiiPlusAuxIO::writeFloat64(asynUser *pasynUser, epicsFloat64 value)
{
int function = pasynUser->reason;
int status=asynSuccess;
int chan;
//static const char *functionName = "writeFloat64";
this->getAddress(pasynUser, &chan);
setDoubleParam(chan, function, value);
if (function == analogOutput_) {
status = writeAnalog(chan, value);
}
callParamCallbacks(chan);
return (status==0) ? asynSuccess : asynError;
}
void SPiiPlusAuxIO::pollerThread()
{
/* This function runs in a separate thread. It waits for the poll time */
static const char *functionName = "pollerThread";
epicsUInt32 changedInBits, changedOutBits;
int i;
int status;
while(1) {
lock();
// Stop the thread if the IOC is shutting down
if (shuttingDown_) {
unlock();
break;
}
// assume status is good
status = asynSuccess;
// assume each IN() or OUT() channel always returns 32 bits
// max IN/OUT/AIN/AOUT index before MP4U returns an error is 255 -> 256
status = pComm_->getDoubleArray(buffer_, "AIN", 0, maxAddr, 0, 0);
memcpy(ain_, buffer_, maxAddr * sizeof(double));
status |= pComm_->getDoubleArray(buffer_, "AOUT", 0, maxAddr, 0, 0);
memcpy(aout_, buffer_, maxAddr * sizeof(double));
status |= pComm_->getIntegerArray(buffer_, "IN", 0, maxAddr, 0, 0);
memcpy(in_, buffer_, maxAddr * sizeof(int));
status |= pComm_->getIntegerArray(buffer_, "OUT", 0, maxAddr, 0, 0);
memcpy(out_, buffer_, maxAddr * sizeof(int));
if (status)
asynPrint(pasynUserSelf, ASYN_TRACE_ERROR,
"%s:%s: ERROR reading I/O, status=%d\n",
driverName, functionName, status);
for (i=0; i<maxAddr; i++) {
setDoubleParam(i, analogInput_, ain_[i]);
setDoubleParam(i, analogOutput_, aout_[i]);
changedInBits = in_[i] ^ prev_in_[i];
if (forceCallback_ || (changedInBits != 0)) {
prev_in_[i] = in_[i];
setUIntDigitalParam(i, digitalInput_, in_[i], 0xFFFFFFFF);
}
changedOutBits = out_[i] ^ prev_out_[i];
if (forceCallback_ || (changedOutBits != 0)) {
prev_out_[i] = out_[i];
setUIntDigitalParam(i, digitalOutput_, out_[i], 0xFFFFFFFF);
}
callParamCallbacks(i);
}
if (forceCallback_) {
forceCallback_ = 0;
}
unlock();
epicsThreadSleep(pollPeriod_);
}
}
extern "C"
{
/** Configuration command, called directly or from iocsh */
int AcsMotionAuxIOConfig(const char *auxIOPortName, const char* asynPortName, int numChannels, double pollPeriod)
{
SPiiPlusAuxIO *pSPiiPlusAuxIO = new SPiiPlusAuxIO(auxIOPortName, asynPortName, numChannels, pollPeriod);
pSPiiPlusAuxIO = NULL; /* This is just to avoid compiler warnings */
return(asynSuccess);
}
static const iocshArg configArg0 = { "Aux IO port name", iocshArgString};
static const iocshArg configArg1 = { "Asyn port name", iocshArgString};
static const iocshArg configArg2 = { "Max I/O channels", iocshArgInt};
static const iocshArg configArg3 = { "Poll period (s)", iocshArgDouble};
static const iocshArg * const AcsMotionConfigArgs[] = {&configArg0,
&configArg1,
&configArg2,
&configArg3};
static const iocshFuncDef configAcsMotionAuxIO = {"AcsMotionAuxIOConfig", 4, AcsMotionConfigArgs};
static void AcsMotionAuxIOCallFunc(const iocshArgBuf *args)
{
AcsMotionAuxIOConfig(args[0].sval, args[1].sval, args[2].ival, args[3].dval);
}
void AcsMotionAuxIORegister(void)
{
iocshRegister(&configAcsMotionAuxIO,AcsMotionAuxIOCallFunc);
}
epicsExportRegistrar(AcsMotionAuxIORegister);
}