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_kdxxxxp_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_kdxxxxp_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_kdxxxxp_set_value(struct sr_serial_dev_inst *serial,
81 struct dev_context *devc)
87 give_device_time_to_process(devc);
94 sr_err("Can't set measurable parameter.");
96 case KDXXXXP_CURRENT_MAX:
98 value = devc->current_max;
100 case KDXXXXP_VOLTAGE_MAX:
101 cmd = "VSET1:%05.2f";
102 value = devc->voltage_max;
106 value = (devc->output_enabled) ? 1 : 0;
110 value = (devc->beep_enabled) ? 1 : 0;
114 if (devc->program < 1 || devc->program > 5) {
115 sr_err("Only programs 1-5 supported and %d isn't "
116 "between them.", devc->program);
119 value = devc->program;
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;
131 sr_err("Don't know how to set %d.", devc->target);
136 snprintf(msg, 20, cmd, value);
138 ret = korad_kdxxxxp_send_cmd(serial, msg);
139 devc->req_sent_at = g_get_monotonic_time();
140 devc->reply_pending = FALSE;
145 SR_PRIV int korad_kdxxxxp_query_value(struct sr_serial_dev_inst *serial,
146 struct dev_context *devc)
150 give_device_time_to_process(devc);
152 switch(devc->target){
153 case KDXXXXP_CURRENT:
154 /* Read current from device. */
155 ret = korad_kdxxxxp_send_cmd(serial, "IOUT1?");
157 case KDXXXXP_CURRENT_MAX:
158 /* Read set current from device. */
159 ret = korad_kdxxxxp_send_cmd(serial, "ISET1?");
161 case KDXXXXP_VOLTAGE:
162 /* Read voltage from device. */
163 ret = korad_kdxxxxp_send_cmd(serial, "VOUT1?");
165 case KDXXXXP_VOLTAGE_MAX:
166 /* Read set voltage from device. */
167 ret = korad_kdxxxxp_send_cmd(serial, "VSET1?");
171 /* Read status from device. */
172 ret = korad_kdxxxxp_send_cmd(serial, "STATUS?");
175 sr_err("Don't know how to query %d.", devc->target);
179 devc->req_sent_at = g_get_monotonic_time();
180 devc->reply_pending = TRUE;
185 SR_PRIV int korad_kdxxxxp_get_all_values(struct sr_serial_dev_inst *serial,
186 struct dev_context *devc)
190 for (devc->target = KDXXXXP_CURRENT;
191 devc->target <= KDXXXXP_STATUS; devc->target++) {
192 if ((ret = korad_kdxxxxp_query_value(serial, devc)) < 0)
194 if ((ret = korad_kdxxxxp_get_reply(serial, devc)) < 0)
201 SR_PRIV int korad_kdxxxxp_get_reply(struct sr_serial_dev_inst *serial,
202 struct dev_context *devc)
212 switch (devc->target) {
213 case KDXXXXP_CURRENT:
214 /* Read current from device. */
215 target = &(devc->current);
217 case KDXXXXP_CURRENT_MAX:
218 /* Read set current from device. */
219 target = &(devc->current_max);
221 case KDXXXXP_VOLTAGE:
222 /* Read voltage from device. */
223 target = &(devc->voltage);
225 case KDXXXXP_VOLTAGE_MAX:
226 /* Read set voltage from device. */
227 target = &(devc->voltage_max);
231 /* Read status from device. */
235 sr_err("Don't know where to put repply %d.", devc->target);
238 if ((ret = korad_kdxxxxp_read_chars(serial, count, devc->reply)) < 0)
241 devc->reply[count] = 0;
244 /* Handle the strange 'M' */
245 if (devc->reply[0] == 'M') {
246 for (i = 1; i < count; ++i) {
247 devc->reply[i - 1] = devc->reply[i];
249 /* Get the last character */
250 if (( i = korad_kdxxxxp_read_chars(serial, 1,
251 &(devc->reply[count]))) < 0)
254 value = g_ascii_strtod(devc->reply, NULL);
255 *target = (float)value;
256 sr_dbg("value: %f",value);
258 /* We have status reply. */
259 status_byte = devc->reply[0];
260 /* Constant current */
261 devc->cc_mode[0] = !(status_byte & (1 << 0)); /* Channel one */
262 devc->cc_mode[1] = !(status_byte & (1 << 1)); /* Channel two */
265 * status_byte & ((1 << 2) | (1 << 3))
266 * 00 independent 01 series 11 parallel
268 devc->beep_enabled = (1 << 4);
269 /* status_byte & (1 << 5) Unlocked */
271 devc->output_enabled = (status_byte & (1 << 6));
272 sr_dbg("Status: 0x%02x", status_byte);
273 sr_spew("Status: CH1: constant %s CH2: constant %s. Device is "
274 "%s and %s. Buttons are %s. Output is %s ",
275 (status_byte & (1 << 0)) ? "voltage" : "current",
276 (status_byte & (1 << 1)) ? "voltage" : "current",
277 (status_byte & (1 << 3)) ? "tracking" : "independent",
278 (status_byte & (1 << 4)) ? "beeping" : "silent",
279 (status_byte & (1 << 5)) ? "locked" : "unlocked",
280 (status_byte & (1 << 6)) ? "enabled" : "disabled");
283 devc->reply_pending = FALSE;
288 static void next_measurement(struct dev_context *devc)
290 switch (devc->target) {
291 case KDXXXXP_CURRENT:
292 devc->target = KDXXXXP_VOLTAGE;
294 case KDXXXXP_CURRENT_MAX:
295 devc->target = KDXXXXP_CURRENT;
297 case KDXXXXP_VOLTAGE:
298 devc->target = KDXXXXP_STATUS;
300 case KDXXXXP_VOLTAGE_MAX:
301 devc->target = KDXXXXP_CURRENT;
304 devc->target = KDXXXXP_STATUS;
307 devc->target = KDXXXXP_CURRENT;
310 devc->target = KDXXXXP_CURRENT;
314 SR_PRIV int korad_kdxxxxp_receive_data(int fd, int revents, void *cb_data)
316 struct sr_dev_inst *sdi;
317 struct dev_context *devc;
318 struct sr_serial_dev_inst *serial;
319 struct sr_datafeed_packet packet;
320 struct sr_datafeed_analog_old analog;
321 int64_t t, elapsed_us;
325 if (!(sdi = cb_data))
328 if (!(devc = sdi->priv))
333 if (revents == G_IO_IN) {
335 korad_kdxxxxp_get_reply(serial, devc);
337 /* Send the value forward. */
338 packet.type = SR_DF_ANALOG_OLD;
339 packet.payload = &analog;
340 analog.channels = sdi->channels;
341 analog.num_samples = 1;
342 if (devc->target == KDXXXXP_CURRENT) {
343 analog.mq = SR_MQ_CURRENT;
344 analog.unit = SR_UNIT_AMPERE;
346 analog.data = &devc->current;
347 sr_session_send(sdi, &packet);
349 if (devc->target == KDXXXXP_VOLTAGE) {
350 analog.mq = SR_MQ_VOLTAGE;
351 analog.unit = SR_UNIT_VOLT;
352 analog.mqflags = SR_MQFLAG_DC;
353 analog.data = &devc->voltage;
354 sr_session_send(sdi, &packet);
357 next_measurement(devc);
360 if (!devc->reply_pending) {
361 if (korad_kdxxxxp_query_value(serial, devc) < 0)
363 devc->req_sent_at = g_get_monotonic_time();
364 devc->reply_pending = TRUE;
368 if (devc->limit_samples && (devc->num_samples >= devc->limit_samples)) {
369 sr_info("Requested number of samples reached.");
370 sdi->driver->dev_acquisition_stop(sdi, cb_data);
374 if (devc->limit_msec) {
375 t = (g_get_monotonic_time() - devc->starttime) / 1000;
376 if (t > (int64_t)devc->limit_msec) {
377 sr_info("Requested time limit reached.");
378 sdi->driver->dev_acquisition_stop(sdi, cb_data);
383 /* Request next packet, if required. */
384 if (sdi->status == SR_ST_ACTIVE) {
385 if (devc->reply_pending) {
386 elapsed_us = g_get_monotonic_time() - devc->req_sent_at;
387 if (elapsed_us > (REQ_TIMEOUT_MS * 1000))
388 devc->reply_pending = FALSE;