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 %ld 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 value = g_ascii_strtod(devc->reply, NULL);
245 *target = (float)value;
246 sr_dbg("value: %f",value);
248 /* We have status reply. */
249 status_byte = devc->reply[0];
250 /* Constant current */
251 devc->cc_mode[0] = !(status_byte & (1 << 0)); /* Channel one */
252 devc->cc_mode[1] = !(status_byte & (1 << 1)); /* Channel two */
255 * status_byte & ((1 << 2) | (1 << 3))
256 * 00 independent 01 series 11 parallel
258 devc->beep_enabled = (1 << 4);
259 /* status_byte & (1 << 5) Unlocked */
261 devc->output_enabled = (status_byte & (1 << 6));
262 sr_dbg("Status: 0x%02x", status_byte);
263 sr_spew("Status: CH1: constant %s CH2: constant %s. Device is "
264 "%s and %s. Buttons are %s. Output is %s ",
265 (status_byte & (1 << 0)) ? "voltage" : "current",
266 (status_byte & (1 << 1)) ? "voltage" : "current",
267 (status_byte & (1 << 3)) ? "tracking" : "independent",
268 (status_byte & (1 << 4)) ? "beeping" : "silent",
269 (status_byte & (1 << 5)) ? "locked" : "unlocked",
270 (status_byte & (1 << 6)) ? "enabled" : "disabled");
273 devc->reply_pending = FALSE;
278 static void next_measurement(struct dev_context *devc)
280 switch (devc->target) {
281 case KDXXXXP_CURRENT:
282 devc->target = KDXXXXP_VOLTAGE;
284 case KDXXXXP_CURRENT_MAX:
285 devc->target = KDXXXXP_CURRENT;
287 case KDXXXXP_VOLTAGE:
288 devc->target = KDXXXXP_STATUS;
290 case KDXXXXP_VOLTAGE_MAX:
291 devc->target = KDXXXXP_CURRENT;
294 devc->target = KDXXXXP_STATUS;
297 devc->target = KDXXXXP_CURRENT;
300 devc->target = KDXXXXP_CURRENT;
304 SR_PRIV int korad_kdxxxxp_receive_data(int fd, int revents, void *cb_data)
306 struct sr_dev_inst *sdi;
307 struct dev_context *devc;
308 struct sr_serial_dev_inst *serial;
309 struct sr_datafeed_packet packet;
310 struct sr_datafeed_analog analog;
311 int64_t t, elapsed_us;
315 if (!(sdi = cb_data))
318 if (!(devc = sdi->priv))
323 if (revents == G_IO_IN) {
325 korad_kdxxxxp_get_reply(serial, devc);
327 /* Send the value forward. */
328 packet.type = SR_DF_ANALOG;
329 packet.payload = &analog;
330 analog.channels = sdi->channels;
331 analog.num_samples = 1;
332 if (devc->target == KDXXXXP_CURRENT) {
333 analog.mq = SR_MQ_CURRENT;
334 analog.unit = SR_UNIT_AMPERE;
336 analog.data = &devc->current;
337 sr_session_send(sdi, &packet);
339 if (devc->target == KDXXXXP_VOLTAGE) {
340 analog.mq = SR_MQ_VOLTAGE;
341 analog.unit = SR_UNIT_VOLT;
342 analog.mqflags = SR_MQFLAG_DC;
343 analog.data = &devc->voltage;
344 sr_session_send(sdi, &packet);
347 next_measurement(devc);
350 if (!devc->reply_pending) {
351 if (korad_kdxxxxp_query_value(serial, devc) < 0)
353 devc->req_sent_at = g_get_monotonic_time();
354 devc->reply_pending = TRUE;
358 if (devc->limit_samples && (devc->num_samples >= devc->limit_samples)) {
359 sr_info("Requested number of samples reached.");
360 sdi->driver->dev_acquisition_stop(sdi, cb_data);
364 if (devc->limit_msec) {
365 t = (g_get_monotonic_time() - devc->starttime) / 1000;
366 if (t > (int64_t)devc->limit_msec) {
367 sr_info("Requested time limit reached.");
368 sdi->driver->dev_acquisition_stop(sdi, cb_data);
373 /* Request next packet, if required. */
374 if (sdi->status == SR_ST_ACTIVE) {
375 if (devc->reply_pending) {
376 elapsed_us = g_get_monotonic_time() - devc->req_sent_at;
377 if (elapsed_us > (REQ_TIMEOUT_MS * 1000))
378 devc->reply_pending = FALSE;