Jan Breuer
2012-11-27 884a288cc0f15f5ab9ea36077b08ff002ff7eac2
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
/*-
 * Copyright (c) 2012-2013 Jan Breuer,
 *
 * All Rights Reserved
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
 
/**
 * @file   scpi_ieee488.c
 * @date   Thu Nov 15 10:58:45 UTC 2012
 * 
 * @brief  Implementation of IEEE488.2 commands and state model
 * 
 * 
 */
 
#include "scpi_parser.h"
#include "scpi_ieee488.h"
#include "scpi_error.h"
#include "scpi_constants.h"
 
/* register array */
static scpi_reg_val_t regs[SCPI_REG_COUNT];
 
/**
 * Update register value
 * @param name - register name
 */
static void SCPI_RegUpdate(scpi_reg_name_t name) {
    SCPI_RegSet(name, SCPI_RegGet(name));
}
 
/**
 * Get register value
 * @param name - register name
 * @return register value
 */
scpi_reg_val_t SCPI_RegGet(scpi_reg_name_t name) {
    if (name < SCPI_REG_COUNT) {
        return regs[name];
    } else {
        return 0;
    }
}
 
/**
 * Set register value
 * @param name - register name
 * @param val - new value
 */
void SCPI_RegSet(scpi_reg_name_t name, scpi_reg_val_t val) {
    if (name >= SCPI_REG_COUNT) {
        return;
    }
 
    // set register value
    regs[name] = val;
 
    switch (name) {
        case SCPI_REG_STB:
            if (val & (SCPI_RegGet(SCPI_REG_SRE) &~STB_SRQ)) {
                val |= STB_SRQ;
            } else {
                val &= ~STB_SRQ;
            }
            break;
        case SCPI_REG_SRE:
            SCPI_RegUpdate(SCPI_REG_STB);
            break;
        case SCPI_REG_ESR:
            if (val & SCPI_RegGet(SCPI_REG_ESE)) {
                SCPI_RegSetBits(SCPI_REG_STB, STB_ESR);
            } else {
                SCPI_RegClearBits(SCPI_REG_STB, STB_ESR);
            }
            break;
        case SCPI_REG_ESE:
            SCPI_RegUpdate(SCPI_REG_ESR);
            break;
        case SCPI_REG_QUES:
            if (val & SCPI_RegGet(SCPI_REG_QUESE)) {
                SCPI_RegSetBits(SCPI_REG_STB, STB_QES);
            } else {
                SCPI_RegClearBits(SCPI_REG_STB, STB_QES);
            }
            break;
        case SCPI_REG_QUESE:
            SCPI_RegUpdate(SCPI_REG_QUES);
            break;
        case SCPI_REG_OPER:
            if (val & SCPI_RegGet(SCPI_REG_OPERE)) {
                SCPI_RegSetBits(SCPI_REG_STB, STB_OPS);
            } else {
                SCPI_RegClearBits(SCPI_REG_STB, STB_OPS);
            }
            break;
        case SCPI_REG_OPERE:
            SCPI_RegUpdate(SCPI_REG_OPER);
            break;
            
            
        case SCPI_REG_COUNT:
            // nothing to do
            break;
    }
 
    // set updated register value
    regs[name] = val;
 
}
 
/**
 * Set register bits
 * @param name - register name
 * @param bits bit mask
 */
void SCPI_RegSetBits(scpi_reg_name_t name, scpi_reg_val_t bits) {
    SCPI_RegSet(name, SCPI_RegGet(name) | bits);
}
 
/**
 * Clear register bits
 * @param name - register name
 * @param bits bit mask
 */
void SCPI_RegClearBits(scpi_reg_name_t name, scpi_reg_val_t bits) {
    SCPI_RegSet(name, SCPI_RegGet(name) & ~bits);
}
 
/* ============ */
 
void SCPI_EventClear(void) {
    // TODO
    SCPI_RegSet(SCPI_REG_ESR, 0);
}
 
/**
 * *CLS - This command clears all status data structures in a device. 
 *        For a device which minimally complies with SCPI. (SCPI std 4.1.3.2)
 * @param context
 * @return 
 */
scpi_result_t SCPI_CoreCls(scpi_t * context) {
    (void) context;
    SCPI_EventClear();
    SCPI_ErrorClear(context);
    SCPI_RegSet(SCPI_REG_OPER, 0);
    SCPI_RegSet(SCPI_REG_QUES, 0);
    return SCPI_RES_OK;
}
 
/**
 * *ESE
 * @param context
 * @return 
 */
scpi_result_t SCPI_CoreEse(scpi_t * context) {
    int32_t new_ESE;
    if (SCPI_ParamInt(context, &new_ESE, TRUE)) {
        SCPI_RegSet(SCPI_REG_ESE, new_ESE);
    }
    return SCPI_RES_OK;
}
 
/**
 * *ESE?
 * @param context
 * @return 
 */
scpi_result_t SCPI_CoreEseQ(scpi_t * context) {
    (void) context;
    SCPI_ResultInt(context, SCPI_RegGet(SCPI_REG_ESE));
    return SCPI_RES_OK;
}
 
/**
 * *ESR?
 * @param context
 * @return 
 */
scpi_result_t SCPI_CoreEsrQ(scpi_t * context) {
    (void) context;
    SCPI_ResultInt(context, SCPI_RegGet(SCPI_REG_ESR));
    SCPI_RegSet(SCPI_REG_ESR, 0);
    return SCPI_RES_OK;
}
 
/**
 * *IDN?
 * @param context
 * @return 
 */
scpi_result_t SCPI_CoreIdnQ(scpi_t * context) {
    (void) context;
    SCPI_ResultString(context, SCPI_MANUFACTURE);
    SCPI_ResultString(context, SCPI_DEV_NAME);
    SCPI_ResultString(context, SCPI_DEV_VERSION);
    return SCPI_RES_OK;
}
 
/**
 * *OPC
 * @param context
 * @return 
 */
scpi_result_t SCPI_CoreOpc(scpi_t * context) {
    (void) context;
    SCPI_RegSetBits(SCPI_REG_ESR, ESR_OPC);
    return SCPI_RES_OK;
}
 
/**
 * *OPC?
 * @param context
 * @return 
 */
scpi_result_t SCPI_CoreOpcQ(scpi_t * context) {
    (void) context;
    // Operation is always completed
    SCPI_ResultInt(context, 1);
    return SCPI_RES_OK;
}
 
/**
 * *RST
 * @param context
 * @return 
 */
scpi_result_t SCPI_CoreRst(scpi_t * context) {
    if (context && context->interface && context->interface->reset) {
        return context->interface->reset(context);
    }
    return SCPI_RES_OK;
}
 
/**
 * *SRE
 * @param context
 * @return 
 */
scpi_result_t SCPI_CoreSre(scpi_t * context) {
    int32_t new_SRE;
    if (SCPI_ParamInt(context, &new_SRE, TRUE)) {
        SCPI_RegSet(SCPI_REG_SRE, new_SRE);
    }
    return SCPI_RES_OK;
}
 
/**
 * *SRE?
 * @param context
 * @return 
 */
scpi_result_t SCPI_CoreSreQ(scpi_t * context) {
    (void) context;
    SCPI_ResultInt(context, SCPI_RegGet(SCPI_REG_SRE));
    return SCPI_RES_OK;
}
 
/**
 * *STB?
 * @param context
 * @return 
 */
scpi_result_t SCPI_CoreStbQ(scpi_t * context) {
    (void) context;
    SCPI_ResultInt(context, SCPI_RegGet(SCPI_REG_STB));
    return SCPI_RES_OK;
}
 
/**
 * *TST?
 * @param context
 * @return 
 */
scpi_result_t SCPI_CoreTstQ(scpi_t * context) {
    (void) context;
    int result = 0;
    if (context && context->interface && context->interface->test) {
        result = context->interface->test(context);
    }    
    SCPI_ResultInt(context, result);
    return SCPI_RES_OK;
}
 
/**
 * *WAI
 * @param context
 * @return 
 */
scpi_result_t SCPI_CoreWai(scpi_t * context) {
    (void) context;
    // NOP
    return SCPI_RES_OK;
}