2 * This file is part of the libsigrok project.
4 * Copyright (C) 2015 Hannu Vuolasaho <vuokkosetae@gmail.com>
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.
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.
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/>.
23 #define REQ_TIMEOUT_MS 500
24 #define DEVICE_PROCESSING_TIME_MS 80
26 SR_PRIV int korad_kaxxxxp_send_cmd(struct sr_serial_dev_inst *serial,
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);
40 SR_PRIV int korad_kaxxxxp_read_chars(struct sr_serial_dev_inst *serial,
43 int ret, received, turns;
49 if ((ret = serial_read_blocking(serial, buf + received,
51 serial_timeout(serial, count))) < 0) {
52 sr_err("Error %d reading %d bytes from device.",
58 } while ((received < count) && (turns < 100));
62 sr_spew("Received: '%s'.", buf);
67 static void give_device_time_to_process(struct dev_context *devc)
69 int64_t sleeping_time;
71 sleeping_time = devc->req_sent_at + (DEVICE_PROCESSING_TIME_MS * 1000);
72 sleeping_time -= g_get_monotonic_time();
74 if (sleeping_time > 0) {
75 g_usleep(sleeping_time);
76 sr_spew("Sleeping for processing %" PRIi64 " usec", sleeping_time);
80 SR_PRIV int korad_kaxxxxp_set_value(struct sr_serial_dev_inst *serial,
81 struct dev_context *devc)
88 give_device_time_to_process(devc);
91 switch (devc->target) {
95 sr_err("Can't set measurable parameter.");
97 case KAXXXXP_CURRENT_MAX:
99 value = devc->current_max;
101 case KAXXXXP_VOLTAGE_MAX:
102 cmd = "VSET1:%05.2f";
103 value = devc->voltage_max;
107 value = (devc->output_enabled) ? 1 : 0;
111 value = (devc->beep_enabled) ? 1 : 0;
115 value = (devc->ocp_enabled) ? 1 : 0;
119 value = (devc->ovp_enabled) ? 1 : 0;
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);
128 value = devc->program;
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);
137 value = devc->program;
140 sr_err("Don't know how to set %d.", devc->target);
145 snprintf(msg, 20, cmd, value);
147 ret = korad_kaxxxxp_send_cmd(serial, msg);
148 devc->req_sent_at = g_get_monotonic_time();
149 devc->reply_pending = FALSE;
154 SR_PRIV int korad_kaxxxxp_query_value(struct sr_serial_dev_inst *serial,
155 struct dev_context *devc)
159 give_device_time_to_process(devc);
161 switch (devc->target) {
162 case KAXXXXP_CURRENT:
163 /* Read current from device. */
164 ret = korad_kaxxxxp_send_cmd(serial, "IOUT1?");
166 case KAXXXXP_CURRENT_MAX:
167 /* Read set current from device. */
168 ret = korad_kaxxxxp_send_cmd(serial, "ISET1?");
170 case KAXXXXP_VOLTAGE:
171 /* Read voltage from device. */
172 ret = korad_kaxxxxp_send_cmd(serial, "VOUT1?");
174 case KAXXXXP_VOLTAGE_MAX:
175 /* Read set voltage from device. */
176 ret = korad_kaxxxxp_send_cmd(serial, "VSET1?");
180 /* Read status from device. */
181 ret = korad_kaxxxxp_send_cmd(serial, "STATUS?");
184 sr_err("Don't know how to query %d.", devc->target);
188 devc->req_sent_at = g_get_monotonic_time();
189 devc->reply_pending = TRUE;
194 SR_PRIV int korad_kaxxxxp_get_all_values(struct sr_serial_dev_inst *serial,
195 struct dev_context *devc)
199 for (devc->target = KAXXXXP_CURRENT;
200 devc->target <= KAXXXXP_STATUS; devc->target++) {
201 if ((ret = korad_kaxxxxp_query_value(serial, devc)) < 0)
203 if ((ret = korad_kaxxxxp_get_reply(serial, devc)) < 0)
210 SR_PRIV int korad_kaxxxxp_get_reply(struct sr_serial_dev_inst *serial,
211 struct dev_context *devc)
221 switch (devc->target) {
222 case KAXXXXP_CURRENT:
223 /* Read current from device. */
224 target = &(devc->current);
226 case KAXXXXP_CURRENT_MAX:
227 /* Read set current from device. */
228 target = &(devc->current_max);
230 case KAXXXXP_VOLTAGE:
231 /* Read voltage from device. */
232 target = &(devc->voltage);
234 case KAXXXXP_VOLTAGE_MAX:
235 /* Read set voltage from device. */
236 target = &(devc->voltage_max);
240 /* Read status from device. */
244 sr_err("Don't know where to put repply %d.", devc->target);
247 if ((ret = korad_kaxxxxp_read_chars(serial, count, devc->reply)) < 0)
250 devc->reply[count] = 0;
253 value = g_ascii_strtod(devc->reply, NULL);
254 *target = (float)value;
255 sr_dbg("value: %f",value);
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 */
264 * status_byte & ((1 << 2) | (1 << 3))
265 * 00 independent 01 series 11 parallel
267 devc->beep_enabled = (1 << 4);
268 devc->ocp_enabled = (status_byte & (1 << 5));
269 devc->output_enabled = (status_byte & (1 << 6));
270 /* Velleman LABPS3005 quirk */
271 if (devc->output_enabled)
272 devc->ovp_enabled = (status_byte & (1 << 7));
273 sr_dbg("Status: 0x%02x", status_byte);
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.",
278 (status_byte & (1 << 0)) ? "voltage" : "current",
279 (status_byte & (1 << 1)) ? "voltage" : "current",
280 (status_byte & (1 << 2)) ? "parallel" : "series",
281 (status_byte & (1 << 3)) ? "tracking" : "independent",
282 (status_byte & (1 << 4)) ? "beeping" : "silent",
283 (status_byte & (1 << 5)) ? "locked" : "unlocked",
284 (status_byte & (1 << 6)) ? "enabled" : "disabled",
285 (status_byte & (1 << 7)) ? "true" : "false");
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);
290 devc->reply_pending = FALSE;
295 static void next_measurement(struct dev_context *devc)
297 switch (devc->target) {
298 case KAXXXXP_CURRENT:
299 devc->target = KAXXXXP_VOLTAGE;
301 case KAXXXXP_CURRENT_MAX:
302 devc->target = KAXXXXP_CURRENT;
304 case KAXXXXP_VOLTAGE:
305 devc->target = KAXXXXP_STATUS;
307 case KAXXXXP_VOLTAGE_MAX:
308 devc->target = KAXXXXP_CURRENT;
310 /* Read back what was set. */
315 devc->target = KAXXXXP_STATUS;
318 devc->target = KAXXXXP_CURRENT;
321 devc->target = KAXXXXP_CURRENT;
325 SR_PRIV int korad_kaxxxxp_receive_data(int fd, int revents, void *cb_data)
327 struct sr_dev_inst *sdi;
328 struct dev_context *devc;
329 struct sr_serial_dev_inst *serial;
330 struct sr_datafeed_packet packet;
331 struct sr_datafeed_analog analog;
332 struct sr_analog_encoding encoding;
333 struct sr_analog_meaning meaning;
334 struct sr_analog_spec spec;
339 if (!(sdi = cb_data))
342 if (!(devc = sdi->priv))
347 if (revents == G_IO_IN) {
349 korad_kaxxxxp_get_reply(serial, devc);
351 sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
353 /* Send the value forward. */
354 packet.type = SR_DF_ANALOG;
355 packet.payload = &analog;
356 analog.meaning->channels = sdi->channels;
357 analog.num_samples = 1;
358 if (devc->target == KAXXXXP_CURRENT) {
359 analog.meaning->mq = SR_MQ_CURRENT;
360 analog.meaning->unit = SR_UNIT_AMPERE;
361 analog.meaning->mqflags = 0;
362 analog.data = &devc->current;
363 sr_session_send(sdi, &packet);
365 if (devc->target == KAXXXXP_VOLTAGE) {
366 analog.meaning->mq = SR_MQ_VOLTAGE;
367 analog.meaning->unit = SR_UNIT_VOLT;
368 analog.meaning->mqflags = SR_MQFLAG_DC;
369 analog.data = &devc->voltage;
370 sr_session_send(sdi, &packet);
371 sr_sw_limits_update_samples_read(&devc->limits, 1);
373 next_measurement(devc);
376 if (!devc->reply_pending) {
377 if (korad_kaxxxxp_query_value(serial, devc) < 0)
379 devc->req_sent_at = g_get_monotonic_time();
380 devc->reply_pending = TRUE;
384 if (sr_sw_limits_check(&devc->limits)) {
385 sdi->driver->dev_acquisition_stop(sdi);
389 /* Request next packet, if required. */
390 if (sdi->status == SR_ST_ACTIVE) {
391 if (devc->reply_pending) {
392 elapsed_us = g_get_monotonic_time() - devc->req_sent_at;
393 if (elapsed_us > (REQ_TIMEOUT_MS * 1000))
394 devc->reply_pending = FALSE;