]>
Commit | Line | Data |
---|---|---|
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 | ||
16fc7ee2 | 26 | SR_PRIV int korad_kaxxxxp_send_cmd(struct sr_serial_dev_inst *serial, |
d7083042 HV |
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 | ||
16fc7ee2 | 40 | SR_PRIV int korad_kaxxxxp_read_chars(struct sr_serial_dev_inst *serial, |
d7083042 HV |
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); | |
ef2bcf11 | 76 | sr_spew("Sleeping for processing %" PRIi64 " usec", sleeping_time); |
d7083042 HV |
77 | } |
78 | } | |
79 | ||
16fc7ee2 | 80 | SR_PRIV int korad_kaxxxxp_set_value(struct sr_serial_dev_inst *serial, |
d7083042 HV |
81 | struct dev_context *devc) |
82 | { | |
2c240774 UH |
83 | char msg[21]; |
84 | const char *cmd; | |
d7083042 HV |
85 | float value; |
86 | int ret; | |
87 | ||
88 | give_device_time_to_process(devc); | |
89 | ||
90 | msg[20] = 0; | |
0cadb8a3 | 91 | switch (devc->target) { |
16fc7ee2 UH |
92 | case KAXXXXP_CURRENT: |
93 | case KAXXXXP_VOLTAGE: | |
94 | case KAXXXXP_STATUS: | |
d7083042 HV |
95 | sr_err("Can't set measurable parameter."); |
96 | return SR_ERR; | |
16fc7ee2 | 97 | case KAXXXXP_CURRENT_MAX: |
d7083042 HV |
98 | cmd = "ISET1:%05.3f"; |
99 | value = devc->current_max; | |
100 | break; | |
16fc7ee2 | 101 | case KAXXXXP_VOLTAGE_MAX: |
d7083042 HV |
102 | cmd = "VSET1:%05.2f"; |
103 | value = devc->voltage_max; | |
104 | break; | |
16fc7ee2 | 105 | case KAXXXXP_OUTPUT: |
d7083042 HV |
106 | cmd = "OUT%01.0f"; |
107 | value = (devc->output_enabled) ? 1 : 0; | |
108 | break; | |
16fc7ee2 | 109 | case KAXXXXP_BEEP: |
d7083042 HV |
110 | cmd = "BEEP%01.0f"; |
111 | value = (devc->beep_enabled) ? 1 : 0; | |
112 | break; | |
16fc7ee2 | 113 | case KAXXXXP_OCP: |
c40ed60f | 114 | cmd = "OCP%01.0f"; |
9e5366df | 115 | value = (devc->ocp_enabled) ? 1 : 0; |
c40ed60f | 116 | break; |
16fc7ee2 | 117 | case KAXXXXP_OVP: |
c40ed60f | 118 | cmd = "OVP%01.0f"; |
9e5366df | 119 | value = (devc->ovp_enabled) ? 1 : 0; |
c40ed60f | 120 | break; |
16fc7ee2 | 121 | case KAXXXXP_SAVE: |
d7083042 HV |
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; | |
16fc7ee2 | 130 | case KAXXXXP_RECALL: |
d7083042 HV |
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 | ||
16fc7ee2 | 147 | ret = korad_kaxxxxp_send_cmd(serial, msg); |
d7083042 HV |
148 | devc->req_sent_at = g_get_monotonic_time(); |
149 | devc->reply_pending = FALSE; | |
150 | ||
151 | return ret; | |
152 | } | |
153 | ||
16fc7ee2 | 154 | SR_PRIV int korad_kaxxxxp_query_value(struct sr_serial_dev_inst *serial, |
d7083042 HV |
155 | struct dev_context *devc) |
156 | { | |
157 | int ret; | |
158 | ||
159 | give_device_time_to_process(devc); | |
160 | ||
0cadb8a3 | 161 | switch (devc->target) { |
16fc7ee2 | 162 | case KAXXXXP_CURRENT: |
d7083042 | 163 | /* Read current from device. */ |
16fc7ee2 | 164 | ret = korad_kaxxxxp_send_cmd(serial, "IOUT1?"); |
d7083042 | 165 | break; |
16fc7ee2 | 166 | case KAXXXXP_CURRENT_MAX: |
d7083042 | 167 | /* Read set current from device. */ |
16fc7ee2 | 168 | ret = korad_kaxxxxp_send_cmd(serial, "ISET1?"); |
d7083042 | 169 | break; |
16fc7ee2 | 170 | case KAXXXXP_VOLTAGE: |
d7083042 | 171 | /* Read voltage from device. */ |
16fc7ee2 | 172 | ret = korad_kaxxxxp_send_cmd(serial, "VOUT1?"); |
d7083042 | 173 | break; |
16fc7ee2 | 174 | case KAXXXXP_VOLTAGE_MAX: |
d7083042 | 175 | /* Read set voltage from device. */ |
16fc7ee2 | 176 | ret = korad_kaxxxxp_send_cmd(serial, "VSET1?"); |
d7083042 | 177 | break; |
16fc7ee2 UH |
178 | case KAXXXXP_STATUS: |
179 | case KAXXXXP_OUTPUT: | |
d7083042 | 180 | /* Read status from device. */ |
16fc7ee2 | 181 | ret = korad_kaxxxxp_send_cmd(serial, "STATUS?"); |
d7083042 HV |
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 | ||
16fc7ee2 | 194 | SR_PRIV int korad_kaxxxxp_get_all_values(struct sr_serial_dev_inst *serial, |
d7083042 HV |
195 | struct dev_context *devc) |
196 | { | |
197 | int ret; | |
198 | ||
16fc7ee2 UH |
199 | for (devc->target = KAXXXXP_CURRENT; |
200 | devc->target <= KAXXXXP_STATUS; devc->target++) { | |
201 | if ((ret = korad_kaxxxxp_query_value(serial, devc)) < 0) | |
d7083042 | 202 | return ret; |
16fc7ee2 | 203 | if ((ret = korad_kaxxxxp_get_reply(serial, devc)) < 0) |
d7083042 HV |
204 | return ret; |
205 | } | |
206 | ||
207 | return ret; | |
208 | } | |
209 | ||
16fc7ee2 | 210 | SR_PRIV int korad_kaxxxxp_get_reply(struct sr_serial_dev_inst *serial, |
d7083042 HV |
211 | struct dev_context *devc) |
212 | { | |
213 | double value; | |
8abdf006 | 214 | int count, ret; |
d7083042 HV |
215 | float *target; |
216 | char status_byte; | |
217 | ||
218 | target = NULL; | |
219 | count = 5; | |
220 | ||
221 | switch (devc->target) { | |
16fc7ee2 | 222 | case KAXXXXP_CURRENT: |
d7083042 HV |
223 | /* Read current from device. */ |
224 | target = &(devc->current); | |
225 | break; | |
16fc7ee2 | 226 | case KAXXXXP_CURRENT_MAX: |
d7083042 HV |
227 | /* Read set current from device. */ |
228 | target = &(devc->current_max); | |
229 | break; | |
16fc7ee2 | 230 | case KAXXXXP_VOLTAGE: |
d7083042 HV |
231 | /* Read voltage from device. */ |
232 | target = &(devc->voltage); | |
233 | break; | |
16fc7ee2 | 234 | case KAXXXXP_VOLTAGE_MAX: |
d7083042 HV |
235 | /* Read set voltage from device. */ |
236 | target = &(devc->voltage_max); | |
237 | break; | |
16fc7ee2 UH |
238 | case KAXXXXP_STATUS: |
239 | case KAXXXXP_OUTPUT: | |
d7083042 HV |
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 | ||
16fc7ee2 | 247 | if ((ret = korad_kaxxxxp_read_chars(serial, count, devc->reply)) < 0) |
d7083042 HV |
248 | return ret; |
249 | ||
250 | devc->reply[count] = 0; | |
251 | ||
252 | if (target) { | |
253 | value = g_ascii_strtod(devc->reply, NULL); | |
254 | *target = (float)value; | |
255 | sr_dbg("value: %f",value); | |
256 | } else { | |
257 | /* We have status reply. */ | |
258 | status_byte = devc->reply[0]; | |
259 | /* Constant current */ | |
260 | devc->cc_mode[0] = !(status_byte & (1 << 0)); /* Channel one */ | |
261 | devc->cc_mode[1] = !(status_byte & (1 << 1)); /* Channel two */ | |
262 | /* | |
263 | * Tracking | |
264 | * status_byte & ((1 << 2) | (1 << 3)) | |
265 | * 00 independent 01 series 11 parallel | |
266 | */ | |
267 | devc->beep_enabled = (1 << 4); | |
9e5366df | 268 | devc->ocp_enabled = (status_byte & (1 << 5)); |
d7083042 | 269 | devc->output_enabled = (status_byte & (1 << 6)); |
c40ed60f HV |
270 | /* Velleman LABPS3005 quirk */ |
271 | if (devc->output_enabled) | |
9e5366df | 272 | devc->ovp_enabled = (status_byte & (1 << 7)); |
d7083042 | 273 | sr_dbg("Status: 0x%02x", status_byte); |
c40ed60f HV |
274 | sr_spew("Status: CH1: constant %s CH2: constant %s. " |
275 | "Tracking would be %s. Device is " | |
276 | "%s and %s. Buttons are %s. Output is %s " | |
277 | "and extra byte is %s.", | |
d7083042 HV |
278 | (status_byte & (1 << 0)) ? "voltage" : "current", |
279 | (status_byte & (1 << 1)) ? "voltage" : "current", | |
c40ed60f | 280 | (status_byte & (1 << 2)) ? "parallel" : "series", |
d7083042 HV |
281 | (status_byte & (1 << 3)) ? "tracking" : "independent", |
282 | (status_byte & (1 << 4)) ? "beeping" : "silent", | |
283 | (status_byte & (1 << 5)) ? "locked" : "unlocked", | |
c40ed60f HV |
284 | (status_byte & (1 << 6)) ? "enabled" : "disabled", |
285 | (status_byte & (1 << 7)) ? "true" : "false"); | |
d7083042 | 286 | } |
8abdf006 HV |
287 | /* Read the sixth byte from ISET? BUG workaround. */ |
288 | if (devc->target == KAXXXXP_CURRENT_MAX) | |
289 | serial_read_blocking(serial, &status_byte, 1, 10); | |
d7083042 HV |
290 | devc->reply_pending = FALSE; |
291 | ||
292 | return ret; | |
293 | } | |
294 | ||
295 | static void next_measurement(struct dev_context *devc) | |
296 | { | |
297 | switch (devc->target) { | |
16fc7ee2 UH |
298 | case KAXXXXP_CURRENT: |
299 | devc->target = KAXXXXP_VOLTAGE; | |
d7083042 | 300 | break; |
16fc7ee2 UH |
301 | case KAXXXXP_CURRENT_MAX: |
302 | devc->target = KAXXXXP_CURRENT; | |
d7083042 | 303 | break; |
16fc7ee2 UH |
304 | case KAXXXXP_VOLTAGE: |
305 | devc->target = KAXXXXP_STATUS; | |
d7083042 | 306 | break; |
16fc7ee2 UH |
307 | case KAXXXXP_VOLTAGE_MAX: |
308 | devc->target = KAXXXXP_CURRENT; | |
d7083042 | 309 | break; |
9e5366df | 310 | /* Read back what was set. */ |
16fc7ee2 UH |
311 | case KAXXXXP_BEEP: |
312 | case KAXXXXP_OCP: | |
313 | case KAXXXXP_OVP: | |
314 | case KAXXXXP_OUTPUT: | |
315 | devc->target = KAXXXXP_STATUS; | |
d7083042 | 316 | break; |
16fc7ee2 UH |
317 | case KAXXXXP_STATUS: |
318 | devc->target = KAXXXXP_CURRENT; | |
d7083042 HV |
319 | break; |
320 | default: | |
16fc7ee2 | 321 | devc->target = KAXXXXP_CURRENT; |
d7083042 HV |
322 | } |
323 | } | |
324 | ||
16fc7ee2 | 325 | SR_PRIV int korad_kaxxxxp_receive_data(int fd, int revents, void *cb_data) |
e75ee7de | 326 | { |
d7083042 | 327 | struct sr_dev_inst *sdi; |
e75ee7de | 328 | struct dev_context *devc; |
d7083042 HV |
329 | struct sr_serial_dev_inst *serial; |
330 | struct sr_datafeed_packet packet; | |
2e715341 UH |
331 | struct sr_datafeed_analog analog; |
332 | struct sr_analog_encoding encoding; | |
333 | struct sr_analog_meaning meaning; | |
334 | struct sr_analog_spec spec; | |
597deef9 | 335 | uint64_t elapsed_us; |
e75ee7de HV |
336 | |
337 | (void)fd; | |
338 | ||
339 | if (!(sdi = cb_data)) | |
340 | return TRUE; | |
341 | ||
342 | if (!(devc = sdi->priv)) | |
343 | return TRUE; | |
344 | ||
d7083042 HV |
345 | serial = sdi->conn; |
346 | ||
e75ee7de | 347 | if (revents == G_IO_IN) { |
d7083042 | 348 | /* Get the value. */ |
16fc7ee2 | 349 | korad_kaxxxxp_get_reply(serial, devc); |
d7083042 | 350 | |
869c8375 | 351 | /* Note: digits/spec_digits will be overridden later. */ |
2e715341 UH |
352 | sr_analog_init(&analog, &encoding, &meaning, &spec, 0); |
353 | ||
d7083042 | 354 | /* Send the value forward. */ |
2e715341 | 355 | packet.type = SR_DF_ANALOG; |
d7083042 | 356 | packet.payload = &analog; |
2e715341 | 357 | analog.meaning->channels = sdi->channels; |
d7083042 | 358 | analog.num_samples = 1; |
16fc7ee2 | 359 | if (devc->target == KAXXXXP_CURRENT) { |
2e715341 UH |
360 | analog.meaning->mq = SR_MQ_CURRENT; |
361 | analog.meaning->unit = SR_UNIT_AMPERE; | |
362 | analog.meaning->mqflags = 0; | |
841c15a5 AJ |
363 | analog.encoding->digits = 3; |
364 | analog.spec->spec_digits = 3; | |
d7083042 HV |
365 | analog.data = &devc->current; |
366 | sr_session_send(sdi, &packet); | |
367 | } | |
16fc7ee2 | 368 | if (devc->target == KAXXXXP_VOLTAGE) { |
2e715341 UH |
369 | analog.meaning->mq = SR_MQ_VOLTAGE; |
370 | analog.meaning->unit = SR_UNIT_VOLT; | |
371 | analog.meaning->mqflags = SR_MQFLAG_DC; | |
841c15a5 AJ |
372 | analog.encoding->digits = 2; |
373 | analog.spec->spec_digits = 2; | |
d7083042 HV |
374 | analog.data = &devc->voltage; |
375 | sr_session_send(sdi, &packet); | |
597deef9 | 376 | sr_sw_limits_update_samples_read(&devc->limits, 1); |
d7083042 HV |
377 | } |
378 | next_measurement(devc); | |
379 | } else { | |
380 | /* Time out */ | |
381 | if (!devc->reply_pending) { | |
16fc7ee2 | 382 | if (korad_kaxxxxp_query_value(serial, devc) < 0) |
d7083042 HV |
383 | return TRUE; |
384 | devc->req_sent_at = g_get_monotonic_time(); | |
385 | devc->reply_pending = TRUE; | |
386 | } | |
387 | } | |
388 | ||
597deef9 | 389 | if (sr_sw_limits_check(&devc->limits)) { |
d2f7c417 | 390 | sr_dev_acquisition_stop(sdi); |
d7083042 HV |
391 | return TRUE; |
392 | } | |
393 | ||
d7083042 HV |
394 | /* Request next packet, if required. */ |
395 | if (sdi->status == SR_ST_ACTIVE) { | |
396 | if (devc->reply_pending) { | |
397 | elapsed_us = g_get_monotonic_time() - devc->req_sent_at; | |
398 | if (elapsed_us > (REQ_TIMEOUT_MS * 1000)) | |
399 | devc->reply_pending = FALSE; | |
400 | return TRUE; | |
401 | } | |
402 | ||
e75ee7de HV |
403 | } |
404 | ||
405 | return TRUE; | |
406 | } |