]> sigrok.org Git - libsigrok.git/blame - src/hardware/korad-kdxxxxp/protocol.c
Support for regulation status and fix for mysterious M
[libsigrok.git] / src / hardware / korad-kdxxxxp / protocol.c
CommitLineData
e75ee7de
HV
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2015 Hannu Vuolasaho <vuokkosetae@gmail.com>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <config.h>
21#include "protocol.h"
22
d7083042
HV
23#define REQ_TIMEOUT_MS 500
24#define DEVICE_PROCESSING_TIME_MS 80
25
26SR_PRIV int korad_kdxxxxp_send_cmd(struct sr_serial_dev_inst *serial,
27 const char *cmd)
28{
29 int ret;
30
31 sr_dbg("Sending '%s'.", cmd);
32 if ((ret = serial_write_blocking(serial, cmd, strlen(cmd), 0)) < 0) {
33 sr_err("Error sending command: %d.", ret);
34 return ret;
35 }
36
37 return ret;
38}
39
40SR_PRIV int korad_kdxxxxp_read_chars(struct sr_serial_dev_inst *serial,
41 int count, char *buf)
42{
43 int ret, received, turns;
44
45 received = 0;
46 turns = 0;
47
48 do {
49 if ((ret = serial_read_blocking(serial, buf + received,
50 count - received,
51 serial_timeout(serial, count))) < 0) {
52 sr_err("Error %d reading %d bytes from device.",
53 ret, count);
54 return ret;
55 }
56 received += ret;
57 turns++;
58 } while ((received < count) && (turns < 100));
59
60 buf[count] = 0;
61
62 sr_spew("Received: '%s'.", buf);
63
64 return ret;
65}
66
67static void give_device_time_to_process(struct dev_context *devc)
68{
69 int64_t sleeping_time;
70
71 sleeping_time = devc->req_sent_at + (DEVICE_PROCESSING_TIME_MS * 1000);
72 sleeping_time -= g_get_monotonic_time();
73
74 if (sleeping_time > 0) {
75 g_usleep(sleeping_time);
ef2bcf11 76 sr_spew("Sleeping for processing %" PRIi64 " usec", sleeping_time);
d7083042
HV
77 }
78}
79
80SR_PRIV int korad_kdxxxxp_set_value(struct sr_serial_dev_inst *serial,
81 struct dev_context *devc)
82{
83 char msg[21], *cmd;
84 float value;
85 int ret;
86
87 give_device_time_to_process(devc);
88
89 msg[20] = 0;
90 switch(devc->target){
91 case KDXXXXP_CURRENT:
92 case KDXXXXP_VOLTAGE:
93 case KDXXXXP_STATUS:
94 sr_err("Can't set measurable parameter.");
95 return SR_ERR;
96 case KDXXXXP_CURRENT_MAX:
97 cmd = "ISET1:%05.3f";
98 value = devc->current_max;
99 break;
100 case KDXXXXP_VOLTAGE_MAX:
101 cmd = "VSET1:%05.2f";
102 value = devc->voltage_max;
103 break;
104 case KDXXXXP_OUTPUT:
105 cmd = "OUT%01.0f";
106 value = (devc->output_enabled) ? 1 : 0;
107 break;
108 case KDXXXXP_BEEP:
109 cmd = "BEEP%01.0f";
110 value = (devc->beep_enabled) ? 1 : 0;
111 break;
112 case KDXXXXP_SAVE:
113 cmd = "SAV%01.0f";
114 if (devc->program < 1 || devc->program > 5) {
115 sr_err("Only programs 1-5 supported and %d isn't "
116 "between them.", devc->program);
117 return SR_ERR;
118 }
119 value = devc->program;
120 break;
121 case KDXXXXP_RECALL:
122 cmd = "RCL%01.0f";
123 if (devc->program < 1 || devc->program > 5) {
124 sr_err("Only programs 1-5 supported and %d isn't "
125 "between them.", devc->program);
126 return SR_ERR;
127 }
128 value = devc->program;
129 break;
130 default:
131 sr_err("Don't know how to set %d.", devc->target);
132 return SR_ERR;
133 }
134
135 if (cmd)
136 snprintf(msg, 20, cmd, value);
137
138 ret = korad_kdxxxxp_send_cmd(serial, msg);
139 devc->req_sent_at = g_get_monotonic_time();
140 devc->reply_pending = FALSE;
141
142 return ret;
143}
144
145SR_PRIV int korad_kdxxxxp_query_value(struct sr_serial_dev_inst *serial,
146 struct dev_context *devc)
147{
148 int ret;
149
150 give_device_time_to_process(devc);
151
152 switch(devc->target){
153 case KDXXXXP_CURRENT:
154 /* Read current from device. */
155 ret = korad_kdxxxxp_send_cmd(serial, "IOUT1?");
156 break;
157 case KDXXXXP_CURRENT_MAX:
158 /* Read set current from device. */
159 ret = korad_kdxxxxp_send_cmd(serial, "ISET1?");
160 break;
161 case KDXXXXP_VOLTAGE:
162 /* Read voltage from device. */
163 ret = korad_kdxxxxp_send_cmd(serial, "VOUT1?");
164 break;
165 case KDXXXXP_VOLTAGE_MAX:
166 /* Read set voltage from device. */
167 ret = korad_kdxxxxp_send_cmd(serial, "VSET1?");
168 break;
169 case KDXXXXP_STATUS:
170 case KDXXXXP_OUTPUT:
171 /* Read status from device. */
172 ret = korad_kdxxxxp_send_cmd(serial, "STATUS?");
173 break;
174 default:
175 sr_err("Don't know how to query %d.", devc->target);
176 return SR_ERR;
177 }
178
179 devc->req_sent_at = g_get_monotonic_time();
180 devc->reply_pending = TRUE;
181
182 return ret;
183}
184
185SR_PRIV int korad_kdxxxxp_get_all_values(struct sr_serial_dev_inst *serial,
186 struct dev_context *devc)
187{
188 int ret;
189
190 for (devc->target = KDXXXXP_CURRENT;
191 devc->target <= KDXXXXP_STATUS; devc->target++) {
192 if ((ret = korad_kdxxxxp_query_value(serial, devc)) < 0)
193 return ret;
194 if ((ret = korad_kdxxxxp_get_reply(serial, devc)) < 0)
195 return ret;
196 }
197
198 return ret;
199}
200
201SR_PRIV int korad_kdxxxxp_get_reply(struct sr_serial_dev_inst *serial,
202 struct dev_context *devc)
203{
204 double value;
b16d975a 205 int count, ret, i;
d7083042
HV
206 float *target;
207 char status_byte;
208
209 target = NULL;
210 count = 5;
211
212 switch (devc->target) {
213 case KDXXXXP_CURRENT:
214 /* Read current from device. */
215 target = &(devc->current);
216 break;
217 case KDXXXXP_CURRENT_MAX:
218 /* Read set current from device. */
219 target = &(devc->current_max);
220 break;
221 case KDXXXXP_VOLTAGE:
222 /* Read voltage from device. */
223 target = &(devc->voltage);
224 break;
225 case KDXXXXP_VOLTAGE_MAX:
226 /* Read set voltage from device. */
227 target = &(devc->voltage_max);
228 break;
229 case KDXXXXP_STATUS:
230 case KDXXXXP_OUTPUT:
231 /* Read status from device. */
232 count = 1;
233 break;
234 default:
235 sr_err("Don't know where to put repply %d.", devc->target);
236 }
237
238 if ((ret = korad_kdxxxxp_read_chars(serial, count, devc->reply)) < 0)
239 return ret;
240
241 devc->reply[count] = 0;
242
243 if (target) {
b16d975a
HV
244 /* Handle the strange 'M' */
245 if (devc->reply[0] == 'M') {
246 for (i = 1; i < count; ++i) {
247 devc->reply[i - 1] = devc->reply[i];
248 }
249 /* Get the last character */
250 if (( i = korad_kdxxxxp_read_chars(serial, 1,
251 &(devc->reply[count]))) < 0)
252 return i;
253 }
d7083042
HV
254 value = g_ascii_strtod(devc->reply, NULL);
255 *target = (float)value;
256 sr_dbg("value: %f",value);
257 } else {
258 /* We have status reply. */
259 status_byte = devc->reply[0];
260 /* Constant current */
261 devc->cc_mode[0] = !(status_byte & (1 << 0)); /* Channel one */
262 devc->cc_mode[1] = !(status_byte & (1 << 1)); /* Channel two */
263 /*
264 * Tracking
265 * status_byte & ((1 << 2) | (1 << 3))
266 * 00 independent 01 series 11 parallel
267 */
268 devc->beep_enabled = (1 << 4);
269 /* status_byte & (1 << 5) Unlocked */
270
271 devc->output_enabled = (status_byte & (1 << 6));
272 sr_dbg("Status: 0x%02x", status_byte);
273 sr_spew("Status: CH1: constant %s CH2: constant %s. Device is "
274 "%s and %s. Buttons are %s. Output is %s ",
275 (status_byte & (1 << 0)) ? "voltage" : "current",
276 (status_byte & (1 << 1)) ? "voltage" : "current",
277 (status_byte & (1 << 3)) ? "tracking" : "independent",
278 (status_byte & (1 << 4)) ? "beeping" : "silent",
279 (status_byte & (1 << 5)) ? "locked" : "unlocked",
280 (status_byte & (1 << 6)) ? "enabled" : "disabled");
281 }
282
283 devc->reply_pending = FALSE;
284
285 return ret;
286}
287
288static void next_measurement(struct dev_context *devc)
289{
290 switch (devc->target) {
291 case KDXXXXP_CURRENT:
292 devc->target = KDXXXXP_VOLTAGE;
293 break;
294 case KDXXXXP_CURRENT_MAX:
295 devc->target = KDXXXXP_CURRENT;
296 break;
297 case KDXXXXP_VOLTAGE:
298 devc->target = KDXXXXP_STATUS;
299 break;
300 case KDXXXXP_VOLTAGE_MAX:
301 devc->target = KDXXXXP_CURRENT;
302 break;
303 case KDXXXXP_OUTPUT:
304 devc->target = KDXXXXP_STATUS;
305 break;
306 case KDXXXXP_STATUS:
307 devc->target = KDXXXXP_CURRENT;
308 break;
309 default:
310 devc->target = KDXXXXP_CURRENT;
311 }
312}
313
e75ee7de
HV
314SR_PRIV int korad_kdxxxxp_receive_data(int fd, int revents, void *cb_data)
315{
d7083042 316 struct sr_dev_inst *sdi;
e75ee7de 317 struct dev_context *devc;
d7083042
HV
318 struct sr_serial_dev_inst *serial;
319 struct sr_datafeed_packet packet;
5faebab2 320 struct sr_datafeed_analog_old analog;
d7083042 321 int64_t t, elapsed_us;
e75ee7de
HV
322
323 (void)fd;
324
325 if (!(sdi = cb_data))
326 return TRUE;
327
328 if (!(devc = sdi->priv))
329 return TRUE;
330
d7083042
HV
331 serial = sdi->conn;
332
e75ee7de 333 if (revents == G_IO_IN) {
d7083042
HV
334 /* Get the value. */
335 korad_kdxxxxp_get_reply(serial, devc);
336
337 /* Send the value forward. */
5faebab2 338 packet.type = SR_DF_ANALOG_OLD;
d7083042
HV
339 packet.payload = &analog;
340 analog.channels = sdi->channels;
341 analog.num_samples = 1;
342 if (devc->target == KDXXXXP_CURRENT) {
343 analog.mq = SR_MQ_CURRENT;
344 analog.unit = SR_UNIT_AMPERE;
345 analog.mqflags = 0;
346 analog.data = &devc->current;
347 sr_session_send(sdi, &packet);
348 }
349 if (devc->target == KDXXXXP_VOLTAGE) {
350 analog.mq = SR_MQ_VOLTAGE;
351 analog.unit = SR_UNIT_VOLT;
352 analog.mqflags = SR_MQFLAG_DC;
353 analog.data = &devc->voltage;
354 sr_session_send(sdi, &packet);
355 devc->num_samples++;
356 }
357 next_measurement(devc);
358 } else {
359 /* Time out */
360 if (!devc->reply_pending) {
361 if (korad_kdxxxxp_query_value(serial, devc) < 0)
362 return TRUE;
363 devc->req_sent_at = g_get_monotonic_time();
364 devc->reply_pending = TRUE;
365 }
366 }
367
368 if (devc->limit_samples && (devc->num_samples >= devc->limit_samples)) {
369 sr_info("Requested number of samples reached.");
370 sdi->driver->dev_acquisition_stop(sdi, cb_data);
371 return TRUE;
372 }
373
374 if (devc->limit_msec) {
375 t = (g_get_monotonic_time() - devc->starttime) / 1000;
376 if (t > (int64_t)devc->limit_msec) {
377 sr_info("Requested time limit reached.");
378 sdi->driver->dev_acquisition_stop(sdi, cb_data);
379 return TRUE;
380 }
381 }
382
383 /* Request next packet, if required. */
384 if (sdi->status == SR_ST_ACTIVE) {
385 if (devc->reply_pending) {
386 elapsed_us = g_get_monotonic_time() - devc->req_sent_at;
387 if (elapsed_us > (REQ_TIMEOUT_MS * 1000))
388 devc->reply_pending = FALSE;
389 return TRUE;
390 }
391
e75ee7de
HV
392 }
393
394 return TRUE;
395}