]> sigrok.org Git - libsigrok.git/blame - src/hardware/korad-kaxxxxp/protocol.c
korad-kaxxxxp: Simplify korad_kaxxxxp_receive_data() event loop function.
[libsigrok.git] / src / hardware / korad-kaxxxxp / 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>
3f9b48ae 5 * Copyright (C) 2018 Frank Stettner <frank-stettner@gmx.net>
e75ee7de
HV
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
d7083042
HV
24#define REQ_TIMEOUT_MS 500
25#define DEVICE_PROCESSING_TIME_MS 80
26
16fc7ee2 27SR_PRIV int korad_kaxxxxp_send_cmd(struct sr_serial_dev_inst *serial,
d7083042
HV
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
16fc7ee2 41SR_PRIV int korad_kaxxxxp_read_chars(struct sr_serial_dev_inst *serial,
d7083042
HV
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
68static 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);
ef2bcf11 77 sr_spew("Sleeping for processing %" PRIi64 " usec", sleeping_time);
d7083042
HV
78 }
79}
80
16fc7ee2 81SR_PRIV int korad_kaxxxxp_set_value(struct sr_serial_dev_inst *serial,
3f9b48ae 82 int target, struct dev_context *devc)
d7083042 83{
8f39d569 84 char *msg;
2c240774 85 const char *cmd;
d7083042
HV
86 float value;
87 int ret;
88
3f9b48ae 89 g_mutex_lock(&devc->rw_mutex);
d7083042
HV
90 give_device_time_to_process(devc);
91
3f9b48ae 92 switch (target) {
16fc7ee2
UH
93 case KAXXXXP_CURRENT:
94 case KAXXXXP_VOLTAGE:
95 case KAXXXXP_STATUS:
3f9b48ae
FS
96 sr_err("Can't set measurable parameter %d.", target);
97 g_mutex_unlock(&devc->rw_mutex);
d7083042 98 return SR_ERR;
16fc7ee2 99 case KAXXXXP_CURRENT_MAX:
d7083042
HV
100 cmd = "ISET1:%05.3f";
101 value = devc->current_max;
102 break;
16fc7ee2 103 case KAXXXXP_VOLTAGE_MAX:
d7083042
HV
104 cmd = "VSET1:%05.2f";
105 value = devc->voltage_max;
106 break;
16fc7ee2 107 case KAXXXXP_OUTPUT:
d7083042
HV
108 cmd = "OUT%01.0f";
109 value = (devc->output_enabled) ? 1 : 0;
110 break;
16fc7ee2 111 case KAXXXXP_BEEP:
d7083042
HV
112 cmd = "BEEP%01.0f";
113 value = (devc->beep_enabled) ? 1 : 0;
114 break;
16fc7ee2 115 case KAXXXXP_OCP:
c40ed60f 116 cmd = "OCP%01.0f";
9e5366df 117 value = (devc->ocp_enabled) ? 1 : 0;
c40ed60f 118 break;
16fc7ee2 119 case KAXXXXP_OVP:
c40ed60f 120 cmd = "OVP%01.0f";
9e5366df 121 value = (devc->ovp_enabled) ? 1 : 0;
c40ed60f 122 break;
16fc7ee2 123 case KAXXXXP_SAVE:
d7083042
HV
124 cmd = "SAV%01.0f";
125 if (devc->program < 1 || devc->program > 5) {
126 sr_err("Only programs 1-5 supported and %d isn't "
127 "between them.", devc->program);
3f9b48ae 128 g_mutex_unlock(&devc->rw_mutex);
d7083042
HV
129 return SR_ERR;
130 }
131 value = devc->program;
132 break;
16fc7ee2 133 case KAXXXXP_RECALL:
d7083042
HV
134 cmd = "RCL%01.0f";
135 if (devc->program < 1 || devc->program > 5) {
136 sr_err("Only programs 1-5 supported and %d isn't "
137 "between them.", devc->program);
3f9b48ae 138 g_mutex_unlock(&devc->rw_mutex);
d7083042
HV
139 return SR_ERR;
140 }
141 value = devc->program;
142 break;
143 default:
3f9b48ae
FS
144 sr_err("Don't know how to set %d.", target);
145 g_mutex_unlock(&devc->rw_mutex);
d7083042
HV
146 return SR_ERR;
147 }
148
8f39d569 149 msg = g_malloc0(20 + 1);
d7083042 150 if (cmd)
8f39d569 151 sr_snprintf_ascii(msg, 20, cmd, value);
d7083042 152
16fc7ee2 153 ret = korad_kaxxxxp_send_cmd(serial, msg);
d7083042 154 devc->req_sent_at = g_get_monotonic_time();
8f39d569 155 g_free(msg);
3f9b48ae
FS
156
157 g_mutex_unlock(&devc->rw_mutex);
d7083042
HV
158
159 return ret;
160}
161
3f9b48ae
FS
162SR_PRIV int korad_kaxxxxp_get_value(struct sr_serial_dev_inst *serial,
163 int target, struct dev_context *devc)
d7083042 164{
3f9b48ae
FS
165 int ret, count;
166 char reply[6];
167 float *value;
168 char status_byte;
d7083042 169
3f9b48ae 170 g_mutex_lock(&devc->rw_mutex);
d7083042
HV
171 give_device_time_to_process(devc);
172
3f9b48ae
FS
173 value = NULL;
174 count = 5;
175
176 switch (target) {
16fc7ee2 177 case KAXXXXP_CURRENT:
d7083042 178 /* Read current from device. */
16fc7ee2 179 ret = korad_kaxxxxp_send_cmd(serial, "IOUT1?");
3f9b48ae 180 value = &(devc->current);
d7083042 181 break;
16fc7ee2 182 case KAXXXXP_CURRENT_MAX:
d7083042 183 /* Read set current from device. */
16fc7ee2 184 ret = korad_kaxxxxp_send_cmd(serial, "ISET1?");
3f9b48ae 185 value = &(devc->current_max);
d7083042 186 break;
16fc7ee2 187 case KAXXXXP_VOLTAGE:
d7083042 188 /* Read voltage from device. */
16fc7ee2 189 ret = korad_kaxxxxp_send_cmd(serial, "VOUT1?");
3f9b48ae 190 value = &(devc->voltage);
d7083042 191 break;
16fc7ee2 192 case KAXXXXP_VOLTAGE_MAX:
d7083042 193 /* Read set voltage from device. */
16fc7ee2 194 ret = korad_kaxxxxp_send_cmd(serial, "VSET1?");
3f9b48ae 195 value = &(devc->voltage_max);
d7083042 196 break;
16fc7ee2
UH
197 case KAXXXXP_STATUS:
198 case KAXXXXP_OUTPUT:
3f9b48ae
FS
199 case KAXXXXP_OCP:
200 case KAXXXXP_OVP:
d7083042 201 /* Read status from device. */
16fc7ee2 202 ret = korad_kaxxxxp_send_cmd(serial, "STATUS?");
3f9b48ae 203 count = 1;
d7083042
HV
204 break;
205 default:
3f9b48ae
FS
206 sr_err("Don't know how to query %d.", target);
207 g_mutex_unlock(&devc->rw_mutex);
d7083042
HV
208 return SR_ERR;
209 }
210
211 devc->req_sent_at = g_get_monotonic_time();
d7083042 212
3f9b48ae
FS
213 if ((ret = korad_kaxxxxp_read_chars(serial, count, reply)) < 0) {
214 g_mutex_unlock(&devc->rw_mutex);
d7083042 215 return ret;
3f9b48ae 216 }
d7083042 217
3f9b48ae 218 reply[count] = 0;
d7083042 219
3f9b48ae
FS
220 if (value) {
221 sr_atof_ascii((const char *)&reply, value);
222 sr_dbg("value: %f", *value);
d7083042
HV
223 } else {
224 /* We have status reply. */
3f9b48ae 225 status_byte = reply[0];
d7083042
HV
226 /* Constant current */
227 devc->cc_mode[0] = !(status_byte & (1 << 0)); /* Channel one */
228 devc->cc_mode[1] = !(status_byte & (1 << 1)); /* Channel two */
229 /*
230 * Tracking
231 * status_byte & ((1 << 2) | (1 << 3))
232 * 00 independent 01 series 11 parallel
233 */
234 devc->beep_enabled = (1 << 4);
9e5366df 235 devc->ocp_enabled = (status_byte & (1 << 5));
d7083042 236 devc->output_enabled = (status_byte & (1 << 6));
c40ed60f
HV
237 /* Velleman LABPS3005 quirk */
238 if (devc->output_enabled)
9e5366df 239 devc->ovp_enabled = (status_byte & (1 << 7));
d7083042 240 sr_dbg("Status: 0x%02x", status_byte);
c40ed60f
HV
241 sr_spew("Status: CH1: constant %s CH2: constant %s. "
242 "Tracking would be %s. Device is "
243 "%s and %s. Buttons are %s. Output is %s "
244 "and extra byte is %s.",
d7083042
HV
245 (status_byte & (1 << 0)) ? "voltage" : "current",
246 (status_byte & (1 << 1)) ? "voltage" : "current",
c40ed60f 247 (status_byte & (1 << 2)) ? "parallel" : "series",
d7083042
HV
248 (status_byte & (1 << 3)) ? "tracking" : "independent",
249 (status_byte & (1 << 4)) ? "beeping" : "silent",
250 (status_byte & (1 << 5)) ? "locked" : "unlocked",
c40ed60f
HV
251 (status_byte & (1 << 6)) ? "enabled" : "disabled",
252 (status_byte & (1 << 7)) ? "true" : "false");
d7083042 253 }
3f9b48ae 254
8abdf006 255 /* Read the sixth byte from ISET? BUG workaround. */
3f9b48ae 256 if (target == KAXXXXP_CURRENT_MAX)
8abdf006 257 serial_read_blocking(serial, &status_byte, 1, 10);
3f9b48ae
FS
258
259 g_mutex_unlock(&devc->rw_mutex);
260
261 return ret;
262}
263
264SR_PRIV int korad_kaxxxxp_get_all_values(struct sr_serial_dev_inst *serial,
265 struct dev_context *devc)
266{
267 int ret, target;
268
269 for (target = KAXXXXP_CURRENT;
270 target <= KAXXXXP_STATUS; target++) {
271 if ((ret = korad_kaxxxxp_get_value(serial, target, devc)) < 0)
272 return ret;
273 }
d7083042
HV
274
275 return ret;
276}
277
278static void next_measurement(struct dev_context *devc)
279{
3f9b48ae 280 switch (devc->acquisition_target) {
16fc7ee2 281 case KAXXXXP_CURRENT:
3f9b48ae 282 devc->acquisition_target = KAXXXXP_VOLTAGE;
d7083042 283 break;
16fc7ee2 284 case KAXXXXP_VOLTAGE:
3f9b48ae 285 devc->acquisition_target = KAXXXXP_STATUS;
d7083042 286 break;
16fc7ee2 287 case KAXXXXP_STATUS:
3f9b48ae 288 devc->acquisition_target = KAXXXXP_CURRENT;
d7083042
HV
289 break;
290 default:
3f9b48ae
FS
291 devc->acquisition_target = KAXXXXP_CURRENT;
292 sr_err("Invalid target for next acquisition.");
d7083042
HV
293 }
294}
295
16fc7ee2 296SR_PRIV int korad_kaxxxxp_receive_data(int fd, int revents, void *cb_data)
e75ee7de 297{
d7083042 298 struct sr_dev_inst *sdi;
e75ee7de 299 struct dev_context *devc;
d7083042
HV
300 struct sr_serial_dev_inst *serial;
301 struct sr_datafeed_packet packet;
2e715341
UH
302 struct sr_datafeed_analog analog;
303 struct sr_analog_encoding encoding;
304 struct sr_analog_meaning meaning;
305 struct sr_analog_spec spec;
23165d7b 306 GSList *l;
e75ee7de
HV
307
308 (void)fd;
a7e48f3c 309 (void)revents;
e75ee7de
HV
310
311 if (!(sdi = cb_data))
312 return TRUE;
313
314 if (!(devc = sdi->priv))
315 return TRUE;
316
d7083042
HV
317 serial = sdi->conn;
318
a7e48f3c
FS
319 /* Get the value. */
320 korad_kaxxxxp_get_value(serial, devc->acquisition_target, devc);
321
322 /* Note: digits/spec_digits will be overridden later. */
323 sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
324
325 /* Send the value forward. */
326 packet.type = SR_DF_ANALOG;
327 packet.payload = &analog;
328 analog.num_samples = 1;
329 l = g_slist_copy(sdi->channels);
330 if (devc->acquisition_target == KAXXXXP_CURRENT) {
331 l = g_slist_remove_link(l, g_slist_nth(l, 0));
332 analog.meaning->channels = l;
333 analog.meaning->mq = SR_MQ_CURRENT;
334 analog.meaning->unit = SR_UNIT_AMPERE;
335 analog.meaning->mqflags = 0;
336 analog.encoding->digits = 3;
337 analog.spec->spec_digits = 3;
338 analog.data = &devc->current;
339 sr_session_send(sdi, &packet);
340 } else if (devc->acquisition_target == KAXXXXP_VOLTAGE) {
341 l = g_slist_remove_link(l, g_slist_nth(l, 1));
342 analog.meaning->channels = l;
343 analog.meaning->mq = SR_MQ_VOLTAGE;
344 analog.meaning->unit = SR_UNIT_VOLT;
345 analog.meaning->mqflags = SR_MQFLAG_DC;
346 analog.encoding->digits = 2;
347 analog.spec->spec_digits = 2;
348 analog.data = &devc->voltage;
349 sr_session_send(sdi, &packet);
350 sr_sw_limits_update_samples_read(&devc->limits, 1);
d7083042 351 }
a7e48f3c 352 next_measurement(devc);
d7083042 353
3f9b48ae 354 if (sr_sw_limits_check(&devc->limits))
d2f7c417 355 sr_dev_acquisition_stop(sdi);
e75ee7de
HV
356
357 return TRUE;
358}