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