-
Notifications
You must be signed in to change notification settings - Fork 5
/
toscaSmon.c
387 lines (355 loc) · 10.3 KB
/
toscaSmon.c
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <epicsTypes.h>
#include <iocsh.h>
#include <epicsStdioRedirect.h>
#include <regDev.h>
#include "toscaReg.h"
#include "toscaMap.h"
#include <epicsExport.h>
#define TOSCA_EXTERN_DEBUG
#define TOSCA_DEBUG_NAME toscaReg
#include "toscaDebug.h"
static const char* smonAddrToStr(unsigned int addr)
{
switch (addr)
{
case 0x00: return "Temp";
case 0x01: return "Vccint";
case 0x02: return "Vccaux";
case 0x03: return "Vadj";
case 0x04: return "VrefP";
case 0x05: return "VrefN";
case 0x08: return "Supply offs";
case 0x09: return "ADC offs";
case 0x10: return "Vaux[0]";
case 0x11: return "Vaux[1]";
case 0x12: return "Vaux[2]";
case 0x13: return "Vaux[3]";
case 0x14: return "Vaux[4]";
case 0x15: return "Vaux[5]";
case 0x16: return "Vaux[6]";
case 0x17: return "Vaux[7]";
case 0x18: return "Vaux[8]";
case 0x19: return "Vaux[9]";
case 0x1a: return "Vaux[A]";
case 0x1b: return "Vaux[B]";
case 0x1c: return "Vaux[C]";
case 0x1d: return "Vaux[D]";
case 0x1e: return "Vaux[E]";
case 0x1f: return "Vaux[F]";
case 0x20: return "Temp Max";
case 0x21: return "Vccint Max";
case 0x22: return "Vccaux Max";
case 0x24: return "Temp Min";
case 0x25: return "Vccint Min";
case 0x26: return "Vccaux Min";
case 0x3f: return "Flag";
case 0x40: return "Config #0";
case 0x41: return "Config #1";
case 0x42: return "Config #2";
case 0x43: return "Test #0";
case 0x44: return "Test #1";
case 0x45: return "Test #2";
case 0x46: return "Test #3";
case 0x47: return "Test #4";
case 0x48: return "Seq #0";
case 0x49: return "Seq #1";
case 0x4a: return "Seq #2";
case 0x4b: return "Seq #3";
case 0x4c: return "Seq #4";
case 0x4d: return "Seq #5";
case 0x4e: return "Seq #6";
case 0x4f: return "Seq #7";
case 0x50: return "Alarm #0";
case 0x51: return "Alarm #1";
case 0x52: return "Alarm #2";
case 0x53: return "Alarm #3";
case 0x54: return "Alarm #4";
case 0x55: return "Alarm #5";
case 0x56: return "Alarm #6";
case 0x57: return "Alarm #7";
default: return "Undefined";
}
}
static void smonFormat(unsigned int addr, unsigned int val)
{
printf("0x%04x", val);
switch (addr)
{
case 0x00: /* Temp */
case 0x20: /* Temp Max */
case 0x24: /* Temp Min */
printf(" =% .2f C ", (val>>6) * 503.975 / 1024 - 273.15);
return;
case 0x01: /* Vccint */
case 0x21: /* Vccint Max */
case 0x25: /* Vccint Min */
case 0x02: /* Vccaux */
case 0x22: /* Vccaux Max */
case 0x26: /* Vccaux Min */
case 0x04: /* VrepP */
case 0x05: /* VrepN */
case 0x03: /*VP/VN */
case 0x08: /* supply Offset */
case 0x09: /* ADC Offset */
case 0x10 /*VauxP[x]/VauxN[x] */:
case 0x11:
case 0x12:
case 0x13:
case 0x14:
case 0x15:
case 0x16:
case 0x17:
case 0x18:
case 0x19:
case 0x1a:
case 0x1b:
case 0x1c:
case 0x1d:
case 0x1e:
case 0x1f:
printf(" =% .3f V ", (val>>6) * 3.0 / 1024);
return;
case 0x3f:
case 0x40:
case 0x41:
case 0x42:
printf(" = %u%u%u%u:%u%u%u%u:%u%u%u%u:%u%u%u%u",
(val>>15)&1, (val>>14)&1, (val>>13)&1, (val>>12)&1,
(val>>11)&1, (val>>10)&1, (val>>9)&1, (val>>8)&1,
(val>>7)&1, (val>>6)&1, (val>>5)&1, (val>>4)&1,
(val>>3)&1, (val>>2)&1, (val>>1)&1, val&1);
return;
default:
printf(" ");
}
}
static void smonShow(unsigned int addr, unsigned int val)
{
if (val == 0xffffffff && errno != 0)
{
fprintf(stderr, "%m\n");
return;
}
printf("%-11s ", smonAddrToStr(addr));
smonFormat(addr, val);
printf("\n");
}
static const iocshFuncDef toscaSmonReadDef =
{ "toscaSmonRead", 1, (const iocshArg *[]) {
&(iocshArg) { "[device:][address]", iocshArgString },
}};
static void toscaSmonReadFunc(const iocshArgBuf *args)
{
unsigned int addr, dev=-1;
char* addrstr = args[0].sval;
char *p;
if (addrstr && strchr(addrstr, ':'))
{
dev = strtol(addrstr, &p, 0);
printf("device %d\n", dev);
if (p == addrstr)
{
fprintf(stderr, "invalid address %s\n", addrstr);
return;
}
addrstr = p+1;
}
if (!addrstr || !*addrstr)
{
unsigned int d, devs = toscaNumDevices();
for (addr = 0; addr < 0x43; addr++)
{
if (addr == 0x06) addr = 0x08;
if (addr == 0x0a) addr = 0x10;
if (addr == 0x23) addr = 0x24;
if (addr == 0x27) addr = 0x3f;
printf("0x%02x ", addr);
printf("%-11s ", smonAddrToStr(addr));
if (dev != -1)
{
smonFormat(addr, toscaSmonRead(addr|dev<<16));
}
else
{
for (d = 0; d < devs; d++)
{
if (d) printf(" | ");
smonFormat(addr, toscaSmonRead(addr|d<<16));
}
}
printf("\n");
}
return;
}
addr = strtol(args[0].sval, &p, 0);
if (p == addrstr)
{
fprintf(stderr, "invalid address %s\n", addrstr);
return;
}
smonShow(addr, toscaSmonRead(addr));
}
static const iocshFuncDef toscaSmonWriteDef =
{ "toscaSmonWrite", 2, (const iocshArg *[]) {
&(iocshArg) { "address", iocshArgInt },
&(iocshArg) { "value", iocshArgInt },
}};
static void toscaSmonWriteFunc(const iocshArgBuf *args)
{
unsigned int addr = args[0].ival;
unsigned int val = args[1].ival;
errno = 0;
smonShow(addr, toscaSmonWrite(addr, val));
}
static const iocshFuncDef toscaSmonWriteMaskedDef =
{ "toscaSmonWriteMasked", 3, (const iocshArg *[]) {
&(iocshArg) { "address", iocshArgInt },
&(iocshArg) { "mask", iocshArgInt },
&(iocshArg) { "value", iocshArgInt },
}};
static void toscaSmonWriteMaskedFunc(const iocshArgBuf *args)
{
unsigned int addr = args[0].ival;
unsigned int mask = args[1].ival;
unsigned int val = args[2].ival;
errno = 0;
smonShow(addr, toscaSmonWriteMasked(addr, mask, val));
}
static const iocshFuncDef toscaSmonSetDef =
{ "toscaSmonSet", 2, (const iocshArg *[]) {
&(iocshArg) { "address", iocshArgInt },
&(iocshArg) { "bitsToSet", iocshArgInt },
}};
static void toscaSmonSetFunc(const iocshArgBuf *args)
{
unsigned int addr = args[0].ival;
unsigned int val = args[1].ival;
errno = 0;
smonShow(addr, toscaSmonSet(addr, val));
}
static const iocshFuncDef toscaSmonClearDef =
{ "toscaSmonClear", 2, (const iocshArg *[]) {
&(iocshArg) { "address", iocshArgInt },
&(iocshArg) { "bitsToClear", iocshArgInt },
}};
static void toscaSmonClearFunc(const iocshArgBuf *args)
{
unsigned int addr = args[0].ival;
unsigned int val = args[1].ival;
errno = 0;
smonShow(addr, toscaSmonClear(addr, val));
}
/* RegDev Interface */
struct regDevice
{
void* nope;
};
void smonDevReport(regDevice *device __attribute__((unused)), int level __attribute__((unused)))
{
printf("Tosca Virtex FPGA System Monitor\n");
}
int smonDevRead(
regDevice *device,
size_t offset,
unsigned int dlen,
size_t nelem,
void* pdata,
int priority __attribute__((unused)),
regDevTransferComplete callback __attribute__((unused)),
const char* user)
{
size_t i;
if (dlen != 2)
{
error("%s %s: dlen must be 4 bytes", regDevName(device), user);
return -1;
}
for (i = 0; i < nelem; i++)
((epicsUInt16*) pdata)[i] = toscaSmonRead(offset+i);
return 0;
}
int smonDevWrite(
regDevice *device,
size_t offset,
unsigned int dlen,
size_t nelem,
void* pdata,
void* pmask,
int priority __attribute__((unused)),
regDevTransferComplete callback __attribute__((unused)),
const char* user)
{
size_t i;
if (dlen != 2)
{
error("%s %s: dlen must be 2 bytes", regDevName(device), user);
return -1;
}
if (pmask && *(epicsUInt16*)pmask != 0xffff)
{
epicsUInt16 mask = *(epicsUInt16*)pmask;
for (i = 0; i < nelem; i++)
{
toscaSmonWriteMasked(offset+i, mask, ((epicsUInt16*) pdata)[i]);
}
}
else
for (i = 0; i < nelem; i++)
toscaSmonWrite(offset+i, ((epicsUInt16*) pdata)[i]);
return 0;
}
struct regDevSupport smonDev = {
.report = smonDevReport,
.read = smonDevRead,
.write = smonDevWrite,
};
int toscaSmonDevConfigure(const char* name)
{
regDevice *device = NULL;
if (!name || !name[0])
{
printf("usage: toscaSmonDevConfigure name\n");
return -1;
}
device = malloc(sizeof(regDevice));
if (!device)
{
fprintf(stderr, "malloc regDevice failed: %m\n");
goto fail;
}
if (regDevRegisterDevice(name, &smonDev, device, 0x100) != SUCCESS)
{
fprintf(stderr, "regDevRegisterDevice() failed: %m\n");
goto fail;
}
if (regDevInstallWorkQueue(device, 100) != SUCCESS)
{
fprintf(stderr, "regDevInstallWorkQueue() failed: %m\n");
return -1;
}
return 0;
fail:
free(device);
return -1;
}
static const iocshFuncDef toscaSmonDevConfigureDef =
{ "toscaSmonDevConfigure", 1, (const iocshArg *[]) {
&(iocshArg) { "name", iocshArgString },
}};
static void toscaSmonDevConfigureFunc(const iocshArgBuf *args)
{
toscaSmonDevConfigure(args[0].sval);
}
static void toscaSmonRegistrar(void)
{
iocshRegister(&toscaSmonDevConfigureDef, toscaSmonDevConfigureFunc);
iocshRegister(&toscaSmonReadDef, toscaSmonReadFunc);
iocshRegister(&toscaSmonWriteDef, toscaSmonWriteFunc);
iocshRegister(&toscaSmonWriteMaskedDef, toscaSmonWriteMaskedFunc);
iocshRegister(&toscaSmonSetDef, toscaSmonSetFunc);
iocshRegister(&toscaSmonClearDef, toscaSmonClearFunc);
}
epicsExportRegistrar(toscaSmonRegistrar);