]> sigrok.org Git - libsigrok.git/blob - src/hardware/korad-kdxxxxp/protocol.c
90ee55f180607e6ba5eddf7f051ace073911e1c1
[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], *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
145 SR_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
185 SR_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
201 SR_PRIV int korad_kdxxxxp_get_reply(struct sr_serial_dev_inst *serial,
202                                 struct dev_context *devc)
203 {
204         double value;
205         int count, ret;
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) {
244                 value = g_ascii_strtod(devc->reply, NULL);
245                 *target = (float)value;
246                 sr_dbg("value: %f",value);
247         } else {
248                 /* We have status reply. */
249                 status_byte = devc->reply[0];
250                 /* Constant current */
251                 devc->cc_mode[0] = !(status_byte & (1 << 0)); /* Channel one */
252                 devc->cc_mode[1] = !(status_byte & (1 << 1)); /* Channel two */
253                 /*
254                  * Tracking
255                  * status_byte & ((1 << 2) | (1 << 3))
256                  * 00 independent 01 series 11 parallel
257                  */
258                 devc->beep_enabled = (1 << 4);
259                 /* status_byte & (1 << 5) Unlocked */
260
261                 devc->output_enabled = (status_byte & (1 << 6));
262                 sr_dbg("Status: 0x%02x", status_byte);
263                 sr_spew("Status: CH1: constant %s CH2: constant %s. Device is "
264                         "%s and %s. Buttons are %s. Output is %s ",
265                         (status_byte & (1 << 0)) ? "voltage" : "current",
266                         (status_byte & (1 << 1)) ? "voltage" : "current",
267                         (status_byte & (1 << 3)) ? "tracking" : "independent",
268                         (status_byte & (1 << 4)) ? "beeping" : "silent",
269                         (status_byte & (1 << 5)) ? "locked" : "unlocked",
270                         (status_byte & (1 << 6)) ? "enabled" : "disabled");
271         }
272
273         devc->reply_pending = FALSE;
274
275         return ret;
276 }
277
278 static void next_measurement(struct dev_context *devc)
279 {
280         switch (devc->target) {
281         case KDXXXXP_CURRENT:
282                 devc->target = KDXXXXP_VOLTAGE;
283                 break;
284         case KDXXXXP_CURRENT_MAX:
285                 devc->target = KDXXXXP_CURRENT;
286                 break;
287         case KDXXXXP_VOLTAGE:
288                 devc->target = KDXXXXP_STATUS;
289                 break;
290         case KDXXXXP_VOLTAGE_MAX:
291                 devc->target = KDXXXXP_CURRENT;
292                 break;
293         case KDXXXXP_OUTPUT:
294                 devc->target = KDXXXXP_STATUS;
295                 break;
296         case KDXXXXP_STATUS:
297                 devc->target = KDXXXXP_CURRENT;
298                 break;
299         default:
300                 devc->target = KDXXXXP_CURRENT;
301         }
302 }
303
304 SR_PRIV int korad_kdxxxxp_receive_data(int fd, int revents, void *cb_data)
305 {
306         struct sr_dev_inst *sdi;
307         struct dev_context *devc;
308         struct sr_serial_dev_inst *serial;
309         struct sr_datafeed_packet packet;
310         struct sr_datafeed_analog analog;
311         int64_t t, elapsed_us;
312
313         (void)fd;
314
315         if (!(sdi = cb_data))
316                 return TRUE;
317
318         if (!(devc = sdi->priv))
319                 return TRUE;
320
321         serial = sdi->conn;
322
323         if (revents == G_IO_IN) {
324                 /* Get the value. */
325                 korad_kdxxxxp_get_reply(serial, devc);
326
327                 /* Send the value forward. */
328                 packet.type = SR_DF_ANALOG;
329                 packet.payload = &analog;
330                 analog.channels = sdi->channels;
331                 analog.num_samples = 1;
332                 if (devc->target == KDXXXXP_CURRENT) {
333                         analog.mq = SR_MQ_CURRENT;
334                         analog.unit = SR_UNIT_AMPERE;
335                         analog.mqflags = 0;
336                         analog.data = &devc->current;
337                         sr_session_send(sdi, &packet);
338                 }
339                 if (devc->target == KDXXXXP_VOLTAGE) {
340                         analog.mq = SR_MQ_VOLTAGE;
341                         analog.unit = SR_UNIT_VOLT;
342                         analog.mqflags = SR_MQFLAG_DC;
343                         analog.data = &devc->voltage;
344                         sr_session_send(sdi, &packet);
345                         devc->num_samples++;
346                 }
347                 next_measurement(devc);
348         } else {
349                 /* Time out */
350                 if (!devc->reply_pending) {
351                         if (korad_kdxxxxp_query_value(serial, devc) < 0)
352                                 return TRUE;
353                         devc->req_sent_at = g_get_monotonic_time();
354                         devc->reply_pending = TRUE;
355                 }
356         }
357
358         if (devc->limit_samples && (devc->num_samples >= devc->limit_samples)) {
359                 sr_info("Requested number of samples reached.");
360                 sdi->driver->dev_acquisition_stop(sdi, cb_data);
361                 return TRUE;
362         }
363
364         if (devc->limit_msec) {
365                 t = (g_get_monotonic_time() - devc->starttime) / 1000;
366                 if (t > (int64_t)devc->limit_msec) {
367                         sr_info("Requested time limit reached.");
368                         sdi->driver->dev_acquisition_stop(sdi, cb_data);
369                         return TRUE;
370                 }
371         }
372
373         /* Request next packet, if required. */
374         if (sdi->status == SR_ST_ACTIVE) {
375                 if (devc->reply_pending) {
376                         elapsed_us = g_get_monotonic_time() - devc->req_sent_at;
377                         if (elapsed_us > (REQ_TIMEOUT_MS * 1000))
378                                 devc->reply_pending = FALSE;
379                         return TRUE;
380                 }
381
382         }
383
384         return TRUE;
385 }