-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscpiparser.h
454 lines (367 loc) · 14.5 KB
/
scpiparser.h
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#ifndef SCPIPARSER_H
#define SCPIPARSER_H
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <QObject>
#include "ieee488.h"
#include "error.h"
#include "constants.h"
#include "units.h"
#include <utils_private.h>
/* scpi commands */
typedef enum _scpi_result_t {
SCPI_RES_OK = 1,
SCPI_RES_ERR = -1
}scpi_result_t;
/* scpi units */
enum scpi_unit_t {
SCPI_UNIT_NONE,
SCPI_UNIT_VOLT,
SCPI_UNIT_AMPER,
SCPI_UNIT_OHM,
SCPI_UNIT_HERTZ,
SCPI_UNIT_CELSIUS,
SCPI_UNIT_SECONDS,
SCPI_UNIT_DISTANCE
};
struct scpi_unit_def_t {
const char * name;
scpi_unit_t unit;
double mult;
};
#define SCPI_UNITS_LIST_END {NULL, SCPI_UNIT_NONE, 0}
enum scpi_special_number_t {
SCPI_NUM_NUMBER,
SCPI_NUM_MIN,
SCPI_NUM_MAX,
SCPI_NUM_DEF,
SCPI_NUM_UP,
SCPI_NUM_DOWN,
SCPI_NUM_NAN,
SCPI_NUM_INF,
SCPI_NUM_NINF
};
struct scpi_special_number_def_t {
const char * name;
scpi_special_number_t type;
};
#define SCPI_SPECIAL_NUMBERS_LIST_END {NULL, SCPI_NUM_NUMBER}
struct scpi_number_t {
double value;
scpi_unit_t unit;
scpi_special_number_t type;
};
/* IEEE 488.2 registers */
enum scpi_reg_name_t {
SCPI_REG_STB = 0, /* Status Byte */
SCPI_REG_SRE, /* Service Request Enable Register */
SCPI_REG_ESR, /* Standard Event Status Register (ESR, SESR) */
SCPI_REG_ESE, /* Event Status Enable Register */
SCPI_REG_OPER, /* OPERation Status Register */
SCPI_REG_OPERE, /* OPERation Status Enable Register */
SCPI_REG_QUES, /* QUEStionable status register */
SCPI_REG_QUESE, /* QUEStionable status Enable Register */
/* last definition - number of registers */
SCPI_REG_COUNT
};
enum scpi_ctrl_name_t {
SCPI_CTRL_SRQ = 1, /* service request */
SCPI_CTRL_GTL, /* Go to local */
SCPI_CTRL_SDC, /* Selected device clear */
SCPI_CTRL_PPC, /* Parallel poll configure */
SCPI_CTRL_GET, /* Group execute trigger */
SCPI_CTRL_TCT, /* Take control */
SCPI_CTRL_LLO, /* Device clear */
SCPI_CTRL_DCL, /* Local lockout */
SCPI_CTRL_PPU, /* Parallel poll unconfigure */
SCPI_CTRL_SPE, /* Serial poll enable */
SCPI_CTRL_SPD, /* Serial poll disable */
SCPI_CTRL_MLA, /* My local address */
SCPI_CTRL_UNL, /* Unlisten */
SCPI_CTRL_MTA, /* My talk address */
SCPI_CTRL_UNT, /* Untalk */
SCPI_CTRL_MSA /* My secondary address */
};
typedef uint16_t scpi_reg_val_t;
class SCPIParser : public QObject
{
Q_OBJECT
public:
explicit SCPIParser(QObject *parent = 0);
signals:
public slots:
public:
typedef bool scpi_bool_t;
/* typedef enum { FALSE = 0, TRUE } scpi_bool_t; */
/* scpi interface */
struct scpi_buffer_t {
size_t length;
size_t position;
char * data;
};
typedef size_t(SCPIParser::*scpi_write_t)(const char * data, size_t len);
typedef scpi_result_t(SCPIParser::*scpi_write_control_t)(scpi_ctrl_name_t ctrl, scpi_reg_val_t val);
typedef int (SCPIParser::*scpi_error_callback_t)(int_fast16_t error);
typedef scpi_result_t(SCPIParser::*scpi_command_callback_t)();
/* scpi error queue */
typedef void * scpi_error_queue_t;
struct scpi_command_t {
const char * pattern;
scpi_command_callback_t callback;
};
struct scpi_param_list_t {
const scpi_command_t * cmd;
const char * parameters;
size_t length;
};
#define SCPI_CMD_LIST_END {NULL, NULL, }
struct scpi_interface_t {
scpi_error_callback_t error;
scpi_write_t write;
scpi_write_control_t control;
scpi_command_callback_t flush;
scpi_command_callback_t reset;
scpi_command_callback_t test;
};
struct _scpi_t {
const scpi_command_t * cmdlist;
scpi_buffer_t buffer;
scpi_param_list_t paramlist;
scpi_interface_t * interface;
int_fast16_t output_count;
int_fast16_t input_count;
scpi_bool_t cmd_error;
scpi_error_queue_t error_queue;
scpi_reg_val_t * registers;
const scpi_unit_def_t * units;
const scpi_special_number_def_t * special_numbers;
void * user_context;
const char * idn[4];
};
typedef struct _scpi_t scpi_t;
#define FIFO_SIZE 16
struct _fifo_t {
int16_t wr;
int16_t rd;
int16_t size;
int16_t data[FIFO_SIZE];
};
typedef struct _fifo_t fifo_t;
#define SCPI_DEBUG_COMMAND(a)
struct error_reg {
int16_t from;
int16_t to;
scpi_reg_val_t bit;
};
#define ERROR_DEFS_N 8
void SCPI_Init();
int SCPI_Input(const char * data, size_t len);
int SCPI_Parse(char * data, size_t len);
size_t SCPI_ResultString(const char * data);
size_t SCPI_ResultInt(int32_t val);
size_t SCPI_ResultDouble(double val);
size_t SCPI_ResultText(const char * data);
size_t SCPI_ResultBool(scpi_bool_t val);
scpi_bool_t SCPI_ParamInt(int32_t * value, scpi_bool_t mandatory);
scpi_bool_t SCPI_ParamDouble(double * value, scpi_bool_t mandatory);
scpi_bool_t SCPI_ParamString(const char ** value, size_t * len, scpi_bool_t mandatory);
scpi_bool_t SCPI_ParamText(const char ** value, size_t * len, scpi_bool_t mandatory);
scpi_bool_t SCPI_ParamBool(scpi_bool_t * value, scpi_bool_t mandatory);
scpi_bool_t SCPI_ParamChoice(const char * options[], int32_t * value, scpi_bool_t mandatory);
scpi_bool_t translateSpecialNumber(const scpi_special_number_def_t * specs, const char * str, size_t len, scpi_number_t * value);
const char * translateSpecialNumberInverse(const scpi_special_number_def_t * specs, scpi_special_number_t type);
const scpi_unit_def_t * translateUnit(const scpi_unit_def_t * units, const char * unit, size_t len);
scpi_bool_t transformNumber(const char * unit, size_t len, scpi_number_t * value);
const char * translateUnitInverse(const scpi_unit_def_t * units, const scpi_unit_t unit);
size_t writeData(const char * data, size_t len);
int flushData() ;
size_t writeDelimiter();
size_t writeNewLine();
void processCommand();
scpi_bool_t findCommand(const char * cmdline_ptr, size_t cmdline_len, size_t cmd_len);
void SCPI_ErrorAddInternal(int16_t err);
fifo_t local_error_queue;
struct error_reg errs[ERROR_DEFS_N] = {
{-100, -199, ESR_CER}, /* Command error (e.g. syntax error) ch 21.8.9 */
{-200, -299, ESR_EER}, /* Execution Error (e.g. range error) ch 21.8.10 */
{-300, -399, ESR_DER}, /* Device specific error -300, -399 ch 21.8.11 */
{-400, -499, ESR_QER}, /* Query error -400, -499 ch 21.8.12 */
{-500, -599, ESR_PON}, /* Power on event -500, -599 ch 21.8.13 */
{-600, -699, ESR_URQ}, /* User Request Event -600, -699 ch 21.8.14 */
{-700, -799, ESR_REQ}, /* Request Control Event -700, -799 ch 21.8.15 */
{-800, -899, ESR_OPC}, /* Operation Complete Event -800, -899 ch 21.8.16 */
};
size_t cmdTerminatorPos(const char * cmd, size_t len);
size_t cmdlineSeparatorPos(const char * cmd, size_t len);
const char * cmdlineSeparator(const char * cmd, size_t len);
const char * cmdlineTerminator(const char * cmd, size_t len);
size_t skipCmdLine(const char * cmd, size_t len);
void paramSkipBytes(size_t num);
void paramSkipWhitespace();
scpi_bool_t paramNext(scpi_bool_t mandatory);
//error
void SCPI_ErrorInit();
void SCPI_ErrorClear();
int16_t SCPI_ErrorPop();
void SCPI_ErrorPush(int16_t err);
int32_t SCPI_ErrorCount();
const char * SCPI_ErrorTranslate(int16_t err);
//debug
scpi_bool_t SCPI_DebugCommand();
//fifo
void fifo_init(fifo_t * fifo);
void fifo_clear(fifo_t * fifo);
scpi_bool_t fifo_add(fifo_t * fifo, int16_t value);
scpi_bool_t fifo_remove(fifo_t * fifo, int16_t * value);
scpi_bool_t fifo_count(fifo_t * fifo, int16_t * value);
//units
// extern const scpi_unit_def_t scpi_units_def[];
// extern const scpi_special_number_def_t scpi_special_numbers_def[];
/*
* multipliers IEEE 488.2-1992 tab 7-2
* 1E18 EX
* 1E15 PE
* 1E12 T
* 1E9 G
* 1E6 MA (use M for OHM and HZ)
* 1E3 K
* 1E-3 M (disaalowed for OHM and HZ)
* 1E-6 U
* 1E-9 N
* 1E-12 P
* 1E-15 F
* 1E-18 A
*/
/*
* units definition IEEE 488.2-1992 tab 7-1
*/
const scpi_unit_def_t scpi_units_def[24] = {
/* voltage */
{/* name */ "UV", /* unit */ SCPI_UNIT_VOLT, /* mult */ 1e-6},
{/* name */ "MV", /* unit */ SCPI_UNIT_VOLT, /* mult */ 1e-3},
{/* name */ "V", /* unit */ SCPI_UNIT_VOLT, /* mult */ 1},
{/* name */ "KV", /* unit */ SCPI_UNIT_VOLT, /* mult */ 1e3},
/* current */
{/* name */ "UA", /* unit */ SCPI_UNIT_AMPER, /* mult */ 1e-6},
{/* name */ "MA", /* unit */ SCPI_UNIT_AMPER, /* mult */ 1e-3},
{/* name */ "A", /* unit */ SCPI_UNIT_AMPER, /* mult */ 1},
{/* name */ "KA", /* unit */ SCPI_UNIT_AMPER, /* mult */ 1e3},
/* resistance */
{/* name */ "OHM", /* unit */ SCPI_UNIT_OHM, /* mult */ 1},
{/* name */ "KOHM", /* unit */ SCPI_UNIT_OHM, /* mult */ 1e3},
{/* name */ "MOHM", /* unit */ SCPI_UNIT_OHM, /* mult */ 1e6},
/* frequency */
{/* name */ "HZ", /* unit */ SCPI_UNIT_HERTZ, /* mult */ 1},
{/* name */ "KHZ", /* unit */ SCPI_UNIT_HERTZ, /* mult */ 1e3},
{/* name */ "MHZ", /* unit */ SCPI_UNIT_HERTZ, /* mult */ 1e6},
{/* name */ "GHZ", /* unit */ SCPI_UNIT_HERTZ, /* mult */ 1e9},
/* temperature */
{/* name */ "CEL", /* unit */ SCPI_UNIT_CELSIUS, /* mult */ 1},
/* time */
{/* name */ "PS", /* unit */ SCPI_UNIT_SECONDS, /* mult */ 1e-12},
{/* name */ "NS", /* unit */ SCPI_UNIT_SECONDS, /* mult */ 1e-9},
{/* name */ "US", /* unit */ SCPI_UNIT_SECONDS, /* mult */ 1e-6},
{/* name */ "MS", /* unit */ SCPI_UNIT_SECONDS, /* mult */ 1e-3},
{/* name */ "S", /* unit */ SCPI_UNIT_SECONDS, /* mult */ 1},
{/* name */ "MIN", /* unit */ SCPI_UNIT_SECONDS, /* mult */ 60},
{/* name */ "HR", /* unit */ SCPI_UNIT_SECONDS, /* mult */ 3600},
SCPI_UNITS_LIST_END,
};
/*
* Special number values definition
*/
const scpi_special_number_def_t scpi_special_numbers_def[9] = {
{/* name */ "MINimum", /* type */ SCPI_NUM_MIN},
{/* name */ "MAXimum", /* type */ SCPI_NUM_MAX},
{/* name */ "DEFault", /* type */ SCPI_NUM_DEF},
{/* name */ "UP", /* type */ SCPI_NUM_UP},
{/* name */ "DOWN", /* type */ SCPI_NUM_DOWN},
{/* name */ "NAN", /* type */ SCPI_NUM_NAN},
{/* name */ "INFinity", /* type */ SCPI_NUM_INF},
{/* name */ "NINF", /* type */ SCPI_NUM_NINF},
SCPI_SPECIAL_NUMBERS_LIST_END,
};
scpi_bool_t SCPI_ParamNumber(scpi_number_t * value, scpi_bool_t mandatory);
size_t SCPI_NumberToStr(scpi_number_t * value, char * str, size_t len);
//ieee
scpi_result_t SCPI_CoreCls();
scpi_result_t SCPI_CoreEse();
scpi_result_t SCPI_CoreEseQ();
scpi_result_t SCPI_CoreEsrQ();
scpi_result_t SCPI_CoreIdnQ();
scpi_result_t SCPI_CoreOpc();
scpi_result_t SCPI_CoreOpcQ();
scpi_result_t SCPI_CoreRst();
scpi_result_t SCPI_CoreSre();
scpi_result_t SCPI_CoreSreQ();
scpi_result_t SCPI_CoreStbQ();
scpi_result_t SCPI_CoreTstQ();
scpi_result_t SCPI_CoreWai();
void SCPI_EventClear() ;
#define STB_R01 0x01 /* Not used */
#define STB_PRO 0x02 /* Protection Event Flag */
#define STB_QMA 0x04 /* Error/Event queue message available */
#define STB_QES 0x08 /* Questionable status */
#define STB_MAV 0x10 /* Message Available */
#define STB_ESR 0x20 /* Standard Event Status Register */
#define STB_SRQ 0x40 /* Service Request */
#define STB_OPS 0x80 /* Operation Status Flag */
#define ESR_OPC 0x01 /* Operation complete */
#define ESR_REQ 0x02 /* Request Control */
#define ESR_QER 0x04 /* Query Error */
#define ESR_DER 0x08 /* Device Dependent Error */
#define ESR_EER 0x10 /* Execution Error (e.g. range error) */
#define ESR_CER 0x20 /* Command error (e.g. syntax error) */
#define ESR_URQ 0x40 /* User Request */
#define ESR_PON 0x80 /* Power On */
scpi_reg_val_t SCPI_RegGet(scpi_reg_name_t name);
void SCPI_RegSet(scpi_reg_name_t name, scpi_reg_val_t val);
void SCPI_RegSetBits(scpi_reg_name_t name, scpi_reg_val_t bits);
void SCPI_RegClearBits(scpi_reg_name_t name, scpi_reg_val_t bits);
void regUpdate(scpi_reg_name_t name);
void regUpdateSTB(scpi_reg_val_t val, scpi_reg_name_t mask, scpi_reg_val_t stbBits);
size_t writeControl(scpi_ctrl_name_t ctrl, scpi_reg_val_t val);
scpi_command_t scpi_commands[3] = {
{"*CLS", &SCPIParser::SCPI_CoreCls,},
SCPI_CMD_LIST_END
};
size_t SCPI_Write(const char * data, size_t len);
int SCPI_Error(int_fast16_t err);
scpi_result_t SCPI_Control(scpi_ctrl_name_t ctrl, scpi_reg_val_t val);
scpi_result_t SCPI_Reset();
scpi_result_t SCPI_Test();
scpi_result_t SCPI_Flush();
scpi_interface_t scpi_interface = {
/* error */ &SCPIParser::SCPI_Error,
/* write */ &SCPIParser::SCPI_Write,
/* control */ &SCPIParser::SCPI_Control,
/* flush */ &SCPIParser::SCPI_Flush,
/* reset */ &SCPIParser::SCPI_Reset,
/* test */ &SCPIParser::SCPI_Test,
};
#define SCPI_INPUT_BUFFER_LENGTH 256
char scpi_input_buffer[SCPI_INPUT_BUFFER_LENGTH];
scpi_reg_val_t scpi_regs[SCPI_REG_COUNT];
scpi_t context = {
/* cmdlist */ scpi_commands,
/* buffer */ { /* length */ SCPI_INPUT_BUFFER_LENGTH, /* position */ 0, /* data */ scpi_input_buffer, },
/* paramlist */ { /* cmd */ NULL, /* parameters */ NULL, /* length */ 0, },
/* interface */ &scpi_interface,
/* output_count */ 0,
/* input_count */ 0,
/* cmd_error */ FALSE,
/* error_queue */ NULL,
/* registers */ scpi_regs,
/* units */ scpi_units_def,
/* special_numbers */ scpi_special_numbers_def,
/* user_context */ NULL,
/* idn */ {"MANUFACTURE", "INSTR2013", NULL, "01-02"},
};
};
#endif // SCPIPARSER_H