]> sigrok.org Git - libsigrok.git/blame - src/hardware/korad-kdxxxxp/protocol.c
build: Delete generated files on make clean
[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;
c40ed60f
HV
112 case KDXXXXP_OCP:
113 cmd = "OCP%01.0f";
9e5366df 114 value = (devc->ocp_enabled) ? 1 : 0;
c40ed60f
HV
115 break;
116 case KDXXXXP_OVP:
117 cmd = "OVP%01.0f";
9e5366df 118 value = (devc->ovp_enabled) ? 1 : 0;
c40ed60f 119 break;
d7083042
HV
120 case KDXXXXP_SAVE:
121 cmd = "SAV%01.0f";
122 if (devc->program < 1 || devc->program > 5) {
123 sr_err("Only programs 1-5 supported and %d isn't "
124 "between them.", devc->program);
125 return SR_ERR;
126 }
127 value = devc->program;
128 break;
129 case KDXXXXP_RECALL:
130 cmd = "RCL%01.0f";
131 if (devc->program < 1 || devc->program > 5) {
132 sr_err("Only programs 1-5 supported and %d isn't "
133 "between them.", devc->program);
134 return SR_ERR;
135 }
136 value = devc->program;
137 break;
138 default:
139 sr_err("Don't know how to set %d.", devc->target);
140 return SR_ERR;
141 }
142
143 if (cmd)
144 snprintf(msg, 20, cmd, value);
145
146 ret = korad_kdxxxxp_send_cmd(serial, msg);
147 devc->req_sent_at = g_get_monotonic_time();
148 devc->reply_pending = FALSE;
149
150 return ret;
151}
152
153SR_PRIV int korad_kdxxxxp_query_value(struct sr_serial_dev_inst *serial,
154 struct dev_context *devc)
155{
156 int ret;
157
158 give_device_time_to_process(devc);
159
160 switch(devc->target){
161 case KDXXXXP_CURRENT:
162 /* Read current from device. */
163 ret = korad_kdxxxxp_send_cmd(serial, "IOUT1?");
164 break;
165 case KDXXXXP_CURRENT_MAX:
166 /* Read set current from device. */
167 ret = korad_kdxxxxp_send_cmd(serial, "ISET1?");
168 break;
169 case KDXXXXP_VOLTAGE:
170 /* Read voltage from device. */
171 ret = korad_kdxxxxp_send_cmd(serial, "VOUT1?");
172 break;
173 case KDXXXXP_VOLTAGE_MAX:
174 /* Read set voltage from device. */
175 ret = korad_kdxxxxp_send_cmd(serial, "VSET1?");
176 break;
177 case KDXXXXP_STATUS:
178 case KDXXXXP_OUTPUT:
179 /* Read status from device. */
180 ret = korad_kdxxxxp_send_cmd(serial, "STATUS?");
181 break;
182 default:
183 sr_err("Don't know how to query %d.", devc->target);
184 return SR_ERR;
185 }
186
187 devc->req_sent_at = g_get_monotonic_time();
188 devc->reply_pending = TRUE;
189
190 return ret;
191}
192
193SR_PRIV int korad_kdxxxxp_get_all_values(struct sr_serial_dev_inst *serial,
194 struct dev_context *devc)
195{
196 int ret;
197
198 for (devc->target = KDXXXXP_CURRENT;
199 devc->target <= KDXXXXP_STATUS; devc->target++) {
200 if ((ret = korad_kdxxxxp_query_value(serial, devc)) < 0)
201 return ret;
202 if ((ret = korad_kdxxxxp_get_reply(serial, devc)) < 0)
203 return ret;
204 }
205
206 return ret;
207}
208
209SR_PRIV int korad_kdxxxxp_get_reply(struct sr_serial_dev_inst *serial,
210 struct dev_context *devc)
211{
212 double value;
b16d975a 213 int count, ret, i;
d7083042
HV
214 float *target;
215 char status_byte;
216
217 target = NULL;
218 count = 5;
219
220 switch (devc->target) {
221 case KDXXXXP_CURRENT:
222 /* Read current from device. */
223 target = &(devc->current);
224 break;
225 case KDXXXXP_CURRENT_MAX:
226 /* Read set current from device. */
227 target = &(devc->current_max);
228 break;
229 case KDXXXXP_VOLTAGE:
230 /* Read voltage from device. */
231 target = &(devc->voltage);
232 break;
233 case KDXXXXP_VOLTAGE_MAX:
234 /* Read set voltage from device. */
235 target = &(devc->voltage_max);
236 break;
237 case KDXXXXP_STATUS:
238 case KDXXXXP_OUTPUT:
239 /* Read status from device. */
240 count = 1;
241 break;
242 default:
243 sr_err("Don't know where to put repply %d.", devc->target);
244 }
245
246 if ((ret = korad_kdxxxxp_read_chars(serial, count, devc->reply)) < 0)
247 return ret;
248
249 devc->reply[count] = 0;
250
251 if (target) {
9e5366df 252 /* Handle the strange 'M'. */
b16d975a 253 if (devc->reply[0] == 'M') {
9e5366df 254 for (i = 1; i < count; i++)
b16d975a 255 devc->reply[i - 1] = devc->reply[i];
9e5366df
UH
256 /* Get the last character. */
257 if ((i = korad_kdxxxxp_read_chars(serial, 1,
b16d975a
HV
258 &(devc->reply[count]))) < 0)
259 return i;
260 }
d7083042
HV
261 value = g_ascii_strtod(devc->reply, NULL);
262 *target = (float)value;
263 sr_dbg("value: %f",value);
264 } else {
265 /* We have status reply. */
266 status_byte = devc->reply[0];
267 /* Constant current */
268 devc->cc_mode[0] = !(status_byte & (1 << 0)); /* Channel one */
269 devc->cc_mode[1] = !(status_byte & (1 << 1)); /* Channel two */
270 /*
271 * Tracking
272 * status_byte & ((1 << 2) | (1 << 3))
273 * 00 independent 01 series 11 parallel
274 */
275 devc->beep_enabled = (1 << 4);
9e5366df 276 devc->ocp_enabled = (status_byte & (1 << 5));
d7083042 277 devc->output_enabled = (status_byte & (1 << 6));
c40ed60f
HV
278 /* Velleman LABPS3005 quirk */
279 if (devc->output_enabled)
9e5366df 280 devc->ovp_enabled = (status_byte & (1 << 7));
d7083042 281 sr_dbg("Status: 0x%02x", status_byte);
c40ed60f
HV
282 sr_spew("Status: CH1: constant %s CH2: constant %s. "
283 "Tracking would be %s. Device is "
284 "%s and %s. Buttons are %s. Output is %s "
285 "and extra byte is %s.",
d7083042
HV
286 (status_byte & (1 << 0)) ? "voltage" : "current",
287 (status_byte & (1 << 1)) ? "voltage" : "current",
c40ed60f 288 (status_byte & (1 << 2)) ? "parallel" : "series",
d7083042
HV
289 (status_byte & (1 << 3)) ? "tracking" : "independent",
290 (status_byte & (1 << 4)) ? "beeping" : "silent",
291 (status_byte & (1 << 5)) ? "locked" : "unlocked",
c40ed60f
HV
292 (status_byte & (1 << 6)) ? "enabled" : "disabled",
293 (status_byte & (1 << 7)) ? "true" : "false");
d7083042
HV
294 }
295
296 devc->reply_pending = FALSE;
297
298 return ret;
299}
300
301static void next_measurement(struct dev_context *devc)
302{
303 switch (devc->target) {
304 case KDXXXXP_CURRENT:
305 devc->target = KDXXXXP_VOLTAGE;
306 break;
307 case KDXXXXP_CURRENT_MAX:
308 devc->target = KDXXXXP_CURRENT;
309 break;
310 case KDXXXXP_VOLTAGE:
311 devc->target = KDXXXXP_STATUS;
312 break;
313 case KDXXXXP_VOLTAGE_MAX:
314 devc->target = KDXXXXP_CURRENT;
315 break;
9e5366df 316 /* Read back what was set. */
c40ed60f
HV
317 case KDXXXXP_BEEP:
318 case KDXXXXP_OCP:
319 case KDXXXXP_OVP:
d7083042
HV
320 case KDXXXXP_OUTPUT:
321 devc->target = KDXXXXP_STATUS;
322 break;
323 case KDXXXXP_STATUS:
324 devc->target = KDXXXXP_CURRENT;
325 break;
326 default:
327 devc->target = KDXXXXP_CURRENT;
328 }
329}
330
e75ee7de
HV
331SR_PRIV int korad_kdxxxxp_receive_data(int fd, int revents, void *cb_data)
332{
d7083042 333 struct sr_dev_inst *sdi;
e75ee7de 334 struct dev_context *devc;
d7083042
HV
335 struct sr_serial_dev_inst *serial;
336 struct sr_datafeed_packet packet;
5faebab2 337 struct sr_datafeed_analog_old analog;
d7083042 338 int64_t t, elapsed_us;
e75ee7de
HV
339
340 (void)fd;
341
342 if (!(sdi = cb_data))
343 return TRUE;
344
345 if (!(devc = sdi->priv))
346 return TRUE;
347
d7083042
HV
348 serial = sdi->conn;
349
e75ee7de 350 if (revents == G_IO_IN) {
d7083042
HV
351 /* Get the value. */
352 korad_kdxxxxp_get_reply(serial, devc);
353
354 /* Send the value forward. */
5faebab2 355 packet.type = SR_DF_ANALOG_OLD;
d7083042
HV
356 packet.payload = &analog;
357 analog.channels = sdi->channels;
358 analog.num_samples = 1;
359 if (devc->target == KDXXXXP_CURRENT) {
360 analog.mq = SR_MQ_CURRENT;
361 analog.unit = SR_UNIT_AMPERE;
362 analog.mqflags = 0;
363 analog.data = &devc->current;
364 sr_session_send(sdi, &packet);
365 }
366 if (devc->target == KDXXXXP_VOLTAGE) {
367 analog.mq = SR_MQ_VOLTAGE;
368 analog.unit = SR_UNIT_VOLT;
369 analog.mqflags = SR_MQFLAG_DC;
370 analog.data = &devc->voltage;
371 sr_session_send(sdi, &packet);
372 devc->num_samples++;
373 }
374 next_measurement(devc);
375 } else {
376 /* Time out */
377 if (!devc->reply_pending) {
378 if (korad_kdxxxxp_query_value(serial, devc) < 0)
379 return TRUE;
380 devc->req_sent_at = g_get_monotonic_time();
381 devc->reply_pending = TRUE;
382 }
383 }
384
385 if (devc->limit_samples && (devc->num_samples >= devc->limit_samples)) {
386 sr_info("Requested number of samples reached.");
387 sdi->driver->dev_acquisition_stop(sdi, cb_data);
388 return TRUE;
389 }
390
391 if (devc->limit_msec) {
392 t = (g_get_monotonic_time() - devc->starttime) / 1000;
393 if (t > (int64_t)devc->limit_msec) {
394 sr_info("Requested time limit reached.");
395 sdi->driver->dev_acquisition_stop(sdi, cb_data);
396 return TRUE;
397 }
398 }
399
400 /* Request next packet, if required. */
401 if (sdi->status == SR_ST_ACTIVE) {
402 if (devc->reply_pending) {
403 elapsed_us = g_get_monotonic_time() - devc->req_sent_at;
404 if (elapsed_us > (REQ_TIMEOUT_MS * 1000))
405 devc->reply_pending = FALSE;
406 return TRUE;
407 }
408
e75ee7de
HV
409 }
410
411 return TRUE;
412}