]> sigrok.org Git - libsigrok.git/blob - src/hardware/korad-kaxxxxp/protocol.c
korad-kaxxxxp: Fix bug when setting values while acquisition is running.
[libsigrok.git] / src / hardware / korad-kaxxxxp / protocol.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2015 Hannu Vuolasaho <vuokkosetae@gmail.com>
5  * Copyright (C) 2018 Frank Stettner <frank-stettner@gmx.net>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22 #include "protocol.h"
23
24 #define REQ_TIMEOUT_MS 500
25 #define DEVICE_PROCESSING_TIME_MS 80
26
27 SR_PRIV int korad_kaxxxxp_send_cmd(struct sr_serial_dev_inst *serial,
28                                 const char *cmd)
29 {
30         int ret;
31
32         sr_dbg("Sending '%s'.", cmd);
33         if ((ret = serial_write_blocking(serial, cmd, strlen(cmd), 0)) < 0) {
34                 sr_err("Error sending command: %d.", ret);
35                 return ret;
36         }
37
38         return ret;
39 }
40
41 SR_PRIV int korad_kaxxxxp_read_chars(struct sr_serial_dev_inst *serial,
42                                 int count, char *buf)
43 {
44         int ret, received, turns;
45
46         received = 0;
47         turns = 0;
48
49         do {
50                 if ((ret = serial_read_blocking(serial, buf + received,
51                                 count - received,
52                                 serial_timeout(serial, count))) < 0) {
53                         sr_err("Error %d reading %d bytes from device.",
54                                ret, count);
55                         return ret;
56                 }
57                 received += ret;
58                 turns++;
59         } while ((received < count) && (turns < 100));
60
61         buf[count] = 0;
62
63         sr_spew("Received: '%s'.", buf);
64
65         return ret;
66 }
67
68 static void give_device_time_to_process(struct dev_context *devc)
69 {
70         int64_t sleeping_time;
71
72         sleeping_time = devc->req_sent_at + (DEVICE_PROCESSING_TIME_MS * 1000);
73         sleeping_time -= g_get_monotonic_time();
74
75         if (sleeping_time > 0) {
76                 g_usleep(sleeping_time);
77                 sr_spew("Sleeping for processing %" PRIi64 " usec", sleeping_time);
78         }
79 }
80
81 SR_PRIV int korad_kaxxxxp_set_value(struct sr_serial_dev_inst *serial,
82                                 int target, struct dev_context *devc)
83 {
84         char *msg;
85         const char *cmd;
86         float value;
87         int ret;
88
89         g_mutex_lock(&devc->rw_mutex);
90         give_device_time_to_process(devc);
91
92         switch (target) {
93         case KAXXXXP_CURRENT:
94         case KAXXXXP_VOLTAGE:
95         case KAXXXXP_STATUS:
96                 sr_err("Can't set measurable parameter %d.", target);
97                 g_mutex_unlock(&devc->rw_mutex);
98                 return SR_ERR;
99         case KAXXXXP_CURRENT_LIMIT:
100                 cmd = "ISET1:%05.3f";
101                 value = devc->set_current_limit;
102                 break;
103         case KAXXXXP_VOLTAGE_TARGET:
104                 cmd = "VSET1:%05.2f";
105                 value = devc->set_voltage_target;
106                 break;
107         case KAXXXXP_OUTPUT:
108                 cmd = "OUT%01.0f";
109                 value = (devc->set_output_enabled) ? 1 : 0;
110                 /* Set value back to recognize changes */
111                 devc->output_enabled = devc->set_output_enabled;
112                 break;
113         case KAXXXXP_BEEP:
114                 cmd = "BEEP%01.0f";
115                 value = (devc->set_beep_enabled) ? 1 : 0;
116                 break;
117         case KAXXXXP_OCP:
118                 cmd = "OCP%01.0f";
119                 value = (devc->set_ocp_enabled) ? 1 : 0;
120                 /* Set value back to recognize changes */
121                 devc->ocp_enabled = devc->set_ocp_enabled;
122                 break;
123         case KAXXXXP_OVP:
124                 cmd = "OVP%01.0f";
125                 value = (devc->set_ovp_enabled) ? 1 : 0;
126                 /* Set value back to recognize changes */
127                 devc->ovp_enabled = devc->set_ovp_enabled;
128                 break;
129         case KAXXXXP_SAVE:
130                 cmd = "SAV%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                         g_mutex_unlock(&devc->rw_mutex);
135                         return SR_ERR;
136                 }
137                 value = devc->program;
138                 break;
139         case KAXXXXP_RECALL:
140                 cmd = "RCL%01.0f";
141                 if (devc->program < 1 || devc->program > 5) {
142                         sr_err("Only programs 1-5 supported and %d isn't "
143                                "between them.", devc->program);
144                         g_mutex_unlock(&devc->rw_mutex);
145                         return SR_ERR;
146                 }
147                 value = devc->program;
148                 break;
149         default:
150                 sr_err("Don't know how to set %d.", target);
151                 g_mutex_unlock(&devc->rw_mutex);
152                 return SR_ERR;
153         }
154
155         msg = g_malloc0(20 + 1);
156         if (cmd)
157                 sr_snprintf_ascii(msg, 20, cmd, value);
158
159         ret = korad_kaxxxxp_send_cmd(serial, msg);
160         devc->req_sent_at = g_get_monotonic_time();
161         g_free(msg);
162
163         g_mutex_unlock(&devc->rw_mutex);
164
165         return ret;
166 }
167
168 SR_PRIV int korad_kaxxxxp_get_value(struct sr_serial_dev_inst *serial,
169                                 int target, struct dev_context *devc)
170 {
171         int ret, count;
172         char reply[6];
173         float *value;
174         char status_byte;
175
176         g_mutex_lock(&devc->rw_mutex);
177         give_device_time_to_process(devc);
178
179         value = NULL;
180         count = 5;
181
182         switch (target) {
183         case KAXXXXP_CURRENT:
184                 /* Read current from device. */
185                 ret = korad_kaxxxxp_send_cmd(serial, "IOUT1?");
186                 value = &(devc->current);
187                 break;
188         case KAXXXXP_CURRENT_LIMIT:
189                 /* Read set current from device. */
190                 ret = korad_kaxxxxp_send_cmd(serial, "ISET1?");
191                 value = &(devc->current_limit);
192                 break;
193         case KAXXXXP_VOLTAGE:
194                 /* Read voltage from device. */
195                 ret = korad_kaxxxxp_send_cmd(serial, "VOUT1?");
196                 value = &(devc->voltage);
197                 break;
198         case KAXXXXP_VOLTAGE_TARGET:
199                 /* Read set voltage from device. */
200                 ret = korad_kaxxxxp_send_cmd(serial, "VSET1?");
201                 value = &(devc->voltage_target);
202                 break;
203         case KAXXXXP_STATUS:
204         case KAXXXXP_OUTPUT:
205         case KAXXXXP_OCP:
206         case KAXXXXP_OVP:
207                 /* Read status from device. */
208                 ret = korad_kaxxxxp_send_cmd(serial, "STATUS?");
209                 count = 1;
210                 break;
211         default:
212                 sr_err("Don't know how to query %d.", target);
213                 g_mutex_unlock(&devc->rw_mutex);
214                 return SR_ERR;
215         }
216
217         devc->req_sent_at = g_get_monotonic_time();
218
219         if ((ret = korad_kaxxxxp_read_chars(serial, count, reply)) < 0) {
220                 g_mutex_unlock(&devc->rw_mutex);
221                 return ret;
222         }
223
224         reply[count] = 0;
225
226         if (value) {
227                 sr_atof_ascii((const char *)&reply, value);
228                 sr_dbg("value: %f", *value);
229         } else {
230                 /* We have status reply. */
231                 status_byte = reply[0];
232                 /* Constant current */
233                 devc->cc_mode[0] = !(status_byte & (1 << 0)); /* Channel one */
234                 devc->cc_mode[1] = !(status_byte & (1 << 1)); /* Channel two */
235                 /*
236                  * Tracking
237                  * status_byte & ((1 << 2) | (1 << 3))
238                  * 00 independent 01 series 11 parallel
239                  */
240                 devc->beep_enabled = (1 << 4);
241                 devc->ocp_enabled = (status_byte & (1 << 5));
242                 devc->output_enabled = (status_byte & (1 << 6));
243                 /* Velleman LABPS3005 quirk */
244                 if (devc->output_enabled)
245                         devc->ovp_enabled = (status_byte & (1 << 7));
246                 sr_dbg("Status: 0x%02x", status_byte);
247                 sr_spew("Status: CH1: constant %s CH2: constant %s. "
248                         "Tracking would be %s. Device is "
249                         "%s and %s. Buttons are %s. Output is %s "
250                         "and extra bit is %s.",
251                         (status_byte & (1 << 0)) ? "voltage" : "current",
252                         (status_byte & (1 << 1)) ? "voltage" : "current",
253                         (status_byte & (1 << 2)) ? "parallel" : "series",
254                         (status_byte & (1 << 3)) ? "tracking" : "independent",
255                         (status_byte & (1 << 4)) ? "beeping" : "silent",
256                         (status_byte & (1 << 5)) ? "locked" : "unlocked",
257                         (status_byte & (1 << 6)) ? "enabled" : "disabled",
258                         (status_byte & (1 << 7)) ? "true" : "false");
259         }
260
261         /* Read the sixth byte from ISET? BUG workaround. */
262         if (target == KAXXXXP_CURRENT_LIMIT)
263                 serial_read_blocking(serial, &status_byte, 1, 10);
264
265         g_mutex_unlock(&devc->rw_mutex);
266
267         return ret;
268 }
269
270 SR_PRIV int korad_kaxxxxp_get_all_values(struct sr_serial_dev_inst *serial,
271                                 struct dev_context *devc)
272 {
273         int ret, target;
274
275         for (target = KAXXXXP_CURRENT;
276                         target <= KAXXXXP_STATUS; target++) {
277                 if ((ret = korad_kaxxxxp_get_value(serial, target, devc)) < 0)
278                         return ret;
279         }
280
281         return ret;
282 }
283
284 static void next_measurement(struct dev_context *devc)
285 {
286         switch (devc->acquisition_target) {
287         case KAXXXXP_CURRENT:
288                 devc->acquisition_target = KAXXXXP_VOLTAGE;
289                 break;
290         case KAXXXXP_VOLTAGE:
291                 devc->acquisition_target = KAXXXXP_STATUS;
292                 break;
293         case KAXXXXP_STATUS:
294                 devc->acquisition_target = KAXXXXP_CURRENT;
295                 break;
296         default:
297                 devc->acquisition_target = KAXXXXP_CURRENT;
298                 sr_err("Invalid target for next acquisition.");
299         }
300 }
301
302 SR_PRIV int korad_kaxxxxp_receive_data(int fd, int revents, void *cb_data)
303 {
304         struct sr_dev_inst *sdi;
305         struct dev_context *devc;
306         struct sr_serial_dev_inst *serial;
307         struct sr_datafeed_packet packet;
308         struct sr_datafeed_analog analog;
309         struct sr_analog_encoding encoding;
310         struct sr_analog_meaning meaning;
311         struct sr_analog_spec spec;
312         GSList *l;
313
314         (void)fd;
315         (void)revents;
316
317         if (!(sdi = cb_data))
318                 return TRUE;
319
320         if (!(devc = sdi->priv))
321                 return TRUE;
322
323         serial = sdi->conn;
324
325         /* Get the value. */
326         korad_kaxxxxp_get_value(serial, devc->acquisition_target, devc);
327
328         /* Note: digits/spec_digits will be overridden later. */
329         sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
330
331         /* Send the value forward. */
332         packet.type = SR_DF_ANALOG;
333         packet.payload = &analog;
334         analog.num_samples = 1;
335         l = g_slist_copy(sdi->channels);
336         if (devc->acquisition_target == KAXXXXP_CURRENT) {
337                 l = g_slist_remove_link(l, g_slist_nth(l, 0));
338                 analog.meaning->channels = l;
339                 analog.meaning->mq = SR_MQ_CURRENT;
340                 analog.meaning->unit = SR_UNIT_AMPERE;
341                 analog.meaning->mqflags = SR_MQFLAG_DC;
342                 analog.encoding->digits = 3;
343                 analog.spec->spec_digits = 3;
344                 analog.data = &devc->current;
345                 sr_session_send(sdi, &packet);
346         } else if (devc->acquisition_target == KAXXXXP_VOLTAGE) {
347                 l = g_slist_remove_link(l, g_slist_nth(l, 1));
348                 analog.meaning->channels = l;
349                 analog.meaning->mq = SR_MQ_VOLTAGE;
350                 analog.meaning->unit = SR_UNIT_VOLT;
351                 analog.meaning->mqflags = SR_MQFLAG_DC;
352                 analog.encoding->digits = 2;
353                 analog.spec->spec_digits = 2;
354                 analog.data = &devc->voltage;
355                 sr_session_send(sdi, &packet);
356                 sr_sw_limits_update_samples_read(&devc->limits, 1);
357         }
358         next_measurement(devc);
359
360         if (sr_sw_limits_check(&devc->limits))
361                 sr_dev_acquisition_stop(sdi);
362
363         return TRUE;
364 }