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