]>
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> | |
2e129b8b | 5 | * Copyright (C) 2018-2019 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 | 27 | SR_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 | 41 | SR_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 | ||
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); | |
ef2bcf11 | 77 | sr_spew("Sleeping for processing %" PRIi64 " usec", sleeping_time); |
d7083042 HV |
78 | } |
79 | } | |
80 | ||
16fc7ee2 | 81 | SR_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; |
8da30037 | 99 | case KAXXXXP_CURRENT_LIMIT: |
d7083042 | 100 | cmd = "ISET1:%05.3f"; |
8da30037 | 101 | value = devc->set_current_limit; |
d7083042 | 102 | break; |
8da30037 | 103 | case KAXXXXP_VOLTAGE_TARGET: |
d7083042 | 104 | cmd = "VSET1:%05.2f"; |
8da30037 | 105 | value = devc->set_voltage_target; |
d7083042 | 106 | break; |
16fc7ee2 | 107 | case KAXXXXP_OUTPUT: |
d7083042 | 108 | cmd = "OUT%01.0f"; |
8da30037 FS |
109 | value = (devc->set_output_enabled) ? 1 : 0; |
110 | /* Set value back to recognize changes */ | |
111 | devc->output_enabled = devc->set_output_enabled; | |
d7083042 | 112 | break; |
16fc7ee2 | 113 | case KAXXXXP_BEEP: |
d7083042 | 114 | cmd = "BEEP%01.0f"; |
8da30037 | 115 | value = (devc->set_beep_enabled) ? 1 : 0; |
d7083042 | 116 | break; |
16fc7ee2 | 117 | case KAXXXXP_OCP: |
c40ed60f | 118 | cmd = "OCP%01.0f"; |
8da30037 FS |
119 | value = (devc->set_ocp_enabled) ? 1 : 0; |
120 | /* Set value back to recognize changes */ | |
121 | devc->ocp_enabled = devc->set_ocp_enabled; | |
c40ed60f | 122 | break; |
16fc7ee2 | 123 | case KAXXXXP_OVP: |
c40ed60f | 124 | cmd = "OVP%01.0f"; |
8da30037 FS |
125 | value = (devc->set_ovp_enabled) ? 1 : 0; |
126 | /* Set value back to recognize changes */ | |
127 | devc->ovp_enabled = devc->set_ovp_enabled; | |
c40ed60f | 128 | break; |
16fc7ee2 | 129 | case KAXXXXP_SAVE: |
d7083042 HV |
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); | |
3f9b48ae | 134 | g_mutex_unlock(&devc->rw_mutex); |
d7083042 HV |
135 | return SR_ERR; |
136 | } | |
137 | value = devc->program; | |
138 | break; | |
16fc7ee2 | 139 | case KAXXXXP_RECALL: |
d7083042 HV |
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); | |
3f9b48ae | 144 | g_mutex_unlock(&devc->rw_mutex); |
d7083042 HV |
145 | return SR_ERR; |
146 | } | |
147 | value = devc->program; | |
148 | break; | |
149 | default: | |
3f9b48ae FS |
150 | sr_err("Don't know how to set %d.", target); |
151 | g_mutex_unlock(&devc->rw_mutex); | |
d7083042 HV |
152 | return SR_ERR; |
153 | } | |
154 | ||
8f39d569 | 155 | msg = g_malloc0(20 + 1); |
d7083042 | 156 | if (cmd) |
8f39d569 | 157 | sr_snprintf_ascii(msg, 20, cmd, value); |
d7083042 | 158 | |
16fc7ee2 | 159 | ret = korad_kaxxxxp_send_cmd(serial, msg); |
d7083042 | 160 | devc->req_sent_at = g_get_monotonic_time(); |
8f39d569 | 161 | g_free(msg); |
3f9b48ae FS |
162 | |
163 | g_mutex_unlock(&devc->rw_mutex); | |
d7083042 HV |
164 | |
165 | return ret; | |
166 | } | |
167 | ||
3f9b48ae FS |
168 | SR_PRIV int korad_kaxxxxp_get_value(struct sr_serial_dev_inst *serial, |
169 | int target, struct dev_context *devc) | |
d7083042 | 170 | { |
3f9b48ae FS |
171 | int ret, count; |
172 | char reply[6]; | |
173 | float *value; | |
174 | char status_byte; | |
2e129b8b | 175 | gboolean prev_status; |
d7083042 | 176 | |
3f9b48ae | 177 | g_mutex_lock(&devc->rw_mutex); |
d7083042 HV |
178 | give_device_time_to_process(devc); |
179 | ||
3f9b48ae FS |
180 | value = NULL; |
181 | count = 5; | |
182 | ||
183 | switch (target) { | |
16fc7ee2 | 184 | case KAXXXXP_CURRENT: |
d7083042 | 185 | /* Read current from device. */ |
16fc7ee2 | 186 | ret = korad_kaxxxxp_send_cmd(serial, "IOUT1?"); |
3f9b48ae | 187 | value = &(devc->current); |
d7083042 | 188 | break; |
8da30037 | 189 | case KAXXXXP_CURRENT_LIMIT: |
d7083042 | 190 | /* Read set current from device. */ |
16fc7ee2 | 191 | ret = korad_kaxxxxp_send_cmd(serial, "ISET1?"); |
8da30037 | 192 | value = &(devc->current_limit); |
d7083042 | 193 | break; |
16fc7ee2 | 194 | case KAXXXXP_VOLTAGE: |
d7083042 | 195 | /* Read voltage from device. */ |
16fc7ee2 | 196 | ret = korad_kaxxxxp_send_cmd(serial, "VOUT1?"); |
3f9b48ae | 197 | value = &(devc->voltage); |
d7083042 | 198 | break; |
8da30037 | 199 | case KAXXXXP_VOLTAGE_TARGET: |
d7083042 | 200 | /* Read set voltage from device. */ |
16fc7ee2 | 201 | ret = korad_kaxxxxp_send_cmd(serial, "VSET1?"); |
8da30037 | 202 | value = &(devc->voltage_target); |
d7083042 | 203 | break; |
16fc7ee2 UH |
204 | case KAXXXXP_STATUS: |
205 | case KAXXXXP_OUTPUT: | |
3f9b48ae FS |
206 | case KAXXXXP_OCP: |
207 | case KAXXXXP_OVP: | |
d7083042 | 208 | /* Read status from device. */ |
16fc7ee2 | 209 | ret = korad_kaxxxxp_send_cmd(serial, "STATUS?"); |
3f9b48ae | 210 | count = 1; |
d7083042 HV |
211 | break; |
212 | default: | |
3f9b48ae FS |
213 | sr_err("Don't know how to query %d.", target); |
214 | g_mutex_unlock(&devc->rw_mutex); | |
d7083042 HV |
215 | return SR_ERR; |
216 | } | |
217 | ||
218 | devc->req_sent_at = g_get_monotonic_time(); | |
d7083042 | 219 | |
3f9b48ae FS |
220 | if ((ret = korad_kaxxxxp_read_chars(serial, count, reply)) < 0) { |
221 | g_mutex_unlock(&devc->rw_mutex); | |
d7083042 | 222 | return ret; |
3f9b48ae | 223 | } |
d7083042 | 224 | |
3f9b48ae | 225 | reply[count] = 0; |
d7083042 | 226 | |
3f9b48ae FS |
227 | if (value) { |
228 | sr_atof_ascii((const char *)&reply, value); | |
229 | sr_dbg("value: %f", *value); | |
d7083042 HV |
230 | } else { |
231 | /* We have status reply. */ | |
3f9b48ae | 232 | status_byte = reply[0]; |
2e129b8b FS |
233 | |
234 | /* Constant current channel one. */ | |
235 | prev_status = devc->cc_mode[0]; | |
236 | devc->cc_mode[0] = !(status_byte & (1 << 0)); | |
237 | devc->cc_mode_1_changed = devc->cc_mode[0] != prev_status; | |
238 | /* Constant current channel two. */ | |
239 | prev_status = devc->cc_mode[1]; | |
240 | devc->cc_mode[1] = !(status_byte & (1 << 1)); | |
241 | devc->cc_mode_2_changed = devc->cc_mode[1] != prev_status; | |
242 | ||
d7083042 | 243 | /* |
2e129b8b | 244 | * Tracking: |
d7083042 HV |
245 | * status_byte & ((1 << 2) | (1 << 3)) |
246 | * 00 independent 01 series 11 parallel | |
247 | */ | |
2e129b8b FS |
248 | devc->beep_enabled = status_byte & (1 << 4); |
249 | ||
250 | /* OCP enabled. */ | |
251 | prev_status = devc->ocp_enabled; | |
252 | devc->ocp_enabled = status_byte & (1 << 5); | |
253 | devc->ocp_enabled_changed = devc->ocp_enabled != prev_status; | |
254 | ||
255 | /* Output status. */ | |
256 | prev_status = devc->output_enabled; | |
257 | devc->output_enabled = status_byte & (1 << 6); | |
258 | devc->output_enabled_changed = devc->output_enabled != prev_status; | |
259 | ||
260 | /* OVP enabled, special handling for Velleman LABPS3005 quirk. */ | |
261 | if ((devc->model->model_id == VELLEMAN_LABPS3005D && devc->output_enabled) || | |
262 | devc->model->model_id != VELLEMAN_LABPS3005D) { | |
263 | ||
264 | prev_status = devc->ovp_enabled; | |
265 | devc->ovp_enabled = status_byte & (1 << 7); | |
266 | devc->ovp_enabled_changed = devc->ovp_enabled != prev_status; | |
267 | } | |
268 | ||
d7083042 | 269 | sr_dbg("Status: 0x%02x", status_byte); |
c40ed60f | 270 | sr_spew("Status: CH1: constant %s CH2: constant %s. " |
253d653d FS |
271 | "Tracking would be %s and %s. Output is %s. " |
272 | "OCP is %s, OVP is %s. Device is %s.", | |
d7083042 HV |
273 | (status_byte & (1 << 0)) ? "voltage" : "current", |
274 | (status_byte & (1 << 1)) ? "voltage" : "current", | |
c40ed60f | 275 | (status_byte & (1 << 2)) ? "parallel" : "series", |
d7083042 | 276 | (status_byte & (1 << 3)) ? "tracking" : "independent", |
c40ed60f | 277 | (status_byte & (1 << 6)) ? "enabled" : "disabled", |
253d653d FS |
278 | (status_byte & (1 << 5)) ? "enabled" : "disabled", |
279 | (status_byte & (1 << 7)) ? "enabled" : "disabled", | |
280 | (status_byte & (1 << 4)) ? "beeping" : "silent"); | |
d7083042 | 281 | } |
3f9b48ae | 282 | |
8abdf006 | 283 | /* Read the sixth byte from ISET? BUG workaround. */ |
8da30037 | 284 | if (target == KAXXXXP_CURRENT_LIMIT) |
8abdf006 | 285 | serial_read_blocking(serial, &status_byte, 1, 10); |
3f9b48ae FS |
286 | |
287 | g_mutex_unlock(&devc->rw_mutex); | |
288 | ||
289 | return ret; | |
290 | } | |
291 | ||
292 | SR_PRIV int korad_kaxxxxp_get_all_values(struct sr_serial_dev_inst *serial, | |
293 | struct dev_context *devc) | |
294 | { | |
295 | int ret, target; | |
296 | ||
297 | for (target = KAXXXXP_CURRENT; | |
298 | target <= KAXXXXP_STATUS; target++) { | |
299 | if ((ret = korad_kaxxxxp_get_value(serial, target, devc)) < 0) | |
300 | return ret; | |
301 | } | |
d7083042 HV |
302 | |
303 | return ret; | |
304 | } | |
305 | ||
306 | static void next_measurement(struct dev_context *devc) | |
307 | { | |
3f9b48ae | 308 | switch (devc->acquisition_target) { |
16fc7ee2 | 309 | case KAXXXXP_CURRENT: |
3f9b48ae | 310 | devc->acquisition_target = KAXXXXP_VOLTAGE; |
d7083042 | 311 | break; |
16fc7ee2 | 312 | case KAXXXXP_VOLTAGE: |
3f9b48ae | 313 | devc->acquisition_target = KAXXXXP_STATUS; |
d7083042 | 314 | break; |
16fc7ee2 | 315 | case KAXXXXP_STATUS: |
3f9b48ae | 316 | devc->acquisition_target = KAXXXXP_CURRENT; |
d7083042 HV |
317 | break; |
318 | default: | |
3f9b48ae FS |
319 | devc->acquisition_target = KAXXXXP_CURRENT; |
320 | sr_err("Invalid target for next acquisition."); | |
d7083042 HV |
321 | } |
322 | } | |
323 | ||
16fc7ee2 | 324 | SR_PRIV int korad_kaxxxxp_receive_data(int fd, int revents, void *cb_data) |
e75ee7de | 325 | { |
d7083042 | 326 | struct sr_dev_inst *sdi; |
e75ee7de | 327 | struct dev_context *devc; |
d7083042 HV |
328 | struct sr_serial_dev_inst *serial; |
329 | struct sr_datafeed_packet packet; | |
2e715341 UH |
330 | struct sr_datafeed_analog analog; |
331 | struct sr_analog_encoding encoding; | |
332 | struct sr_analog_meaning meaning; | |
333 | struct sr_analog_spec spec; | |
23165d7b | 334 | GSList *l; |
e75ee7de HV |
335 | |
336 | (void)fd; | |
a7e48f3c | 337 | (void)revents; |
e75ee7de HV |
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 | ||
a7e48f3c FS |
347 | /* Get the value. */ |
348 | korad_kaxxxxp_get_value(serial, devc->acquisition_target, devc); | |
349 | ||
350 | /* Note: digits/spec_digits will be overridden later. */ | |
351 | sr_analog_init(&analog, &encoding, &meaning, &spec, 0); | |
352 | ||
353 | /* Send the value forward. */ | |
354 | packet.type = SR_DF_ANALOG; | |
355 | packet.payload = &analog; | |
356 | analog.num_samples = 1; | |
357 | l = g_slist_copy(sdi->channels); | |
358 | if (devc->acquisition_target == KAXXXXP_CURRENT) { | |
359 | l = g_slist_remove_link(l, g_slist_nth(l, 0)); | |
360 | analog.meaning->channels = l; | |
361 | analog.meaning->mq = SR_MQ_CURRENT; | |
362 | analog.meaning->unit = SR_UNIT_AMPERE; | |
d1a3f3be | 363 | analog.meaning->mqflags = SR_MQFLAG_DC; |
a7e48f3c FS |
364 | analog.encoding->digits = 3; |
365 | analog.spec->spec_digits = 3; | |
366 | analog.data = &devc->current; | |
367 | sr_session_send(sdi, &packet); | |
368 | } else if (devc->acquisition_target == KAXXXXP_VOLTAGE) { | |
369 | l = g_slist_remove_link(l, g_slist_nth(l, 1)); | |
370 | analog.meaning->channels = l; | |
371 | analog.meaning->mq = SR_MQ_VOLTAGE; | |
372 | analog.meaning->unit = SR_UNIT_VOLT; | |
373 | analog.meaning->mqflags = SR_MQFLAG_DC; | |
374 | analog.encoding->digits = 2; | |
375 | analog.spec->spec_digits = 2; | |
376 | analog.data = &devc->voltage; | |
377 | sr_session_send(sdi, &packet); | |
378 | sr_sw_limits_update_samples_read(&devc->limits, 1); | |
2e129b8b FS |
379 | } else if (devc->acquisition_target == KAXXXXP_STATUS) { |
380 | if (devc->cc_mode_1_changed) { | |
381 | sr_session_send_meta(sdi, SR_CONF_REGULATION, | |
382 | g_variant_new_string((devc->cc_mode[0]) ? "CC" : "CV")); | |
383 | devc->cc_mode_1_changed = FALSE; | |
384 | } | |
385 | if (devc->cc_mode_2_changed) { | |
386 | sr_session_send_meta(sdi, SR_CONF_REGULATION, | |
387 | g_variant_new_string((devc->cc_mode[1]) ? "CC" : "CV")); | |
388 | devc->cc_mode_2_changed = FALSE; | |
389 | } | |
390 | if (devc->output_enabled_changed) { | |
391 | sr_session_send_meta(sdi, SR_CONF_ENABLED, | |
392 | g_variant_new_boolean(devc->output_enabled)); | |
393 | devc->output_enabled_changed = FALSE; | |
394 | } | |
395 | if (devc->ocp_enabled_changed) { | |
396 | sr_session_send_meta(sdi, SR_CONF_OVER_CURRENT_PROTECTION_ENABLED, | |
397 | g_variant_new_boolean(devc->ocp_enabled)); | |
398 | devc->ocp_enabled_changed = FALSE; | |
399 | } | |
400 | if (devc->ovp_enabled_changed) { | |
401 | sr_session_send_meta(sdi, SR_CONF_OVER_VOLTAGE_PROTECTION_ENABLED, | |
402 | g_variant_new_boolean(devc->ovp_enabled)); | |
403 | devc->ovp_enabled_changed = FALSE; | |
404 | } | |
d7083042 | 405 | } |
a7e48f3c | 406 | next_measurement(devc); |
d7083042 | 407 | |
3f9b48ae | 408 | if (sr_sw_limits_check(&devc->limits)) |
d2f7c417 | 409 | sr_dev_acquisition_stop(sdi); |
e75ee7de HV |
410 | |
411 | return TRUE; | |
412 | } |