]> sigrok.org Git - libsigrok.git/blame - hardware/motech-lps-30x/api.c
motech-lps-30x: Implemented driver.
[libsigrok.git] / hardware / motech-lps-30x / api.c
CommitLineData
41b7bd01
MH
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2014 Matthias Heidbrink <m-sigrok@heidbrink.biz>
1c3d002b 5 * Copyright (C) 2014 Bert Vermeulen <bert@biot.com> (code from atten-pps3xxx)
41b7bd01
MH
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
1c3d002b
MH
21/** @file
22 * <em>Motech LPS-30x series</em> power supply driver
23 * @internal
24 */
25
26#include <ctype.h>
27#include <errno.h>
28#include <math.h>
29#include <string.h>
30
41b7bd01
MH
31#include "protocol.h"
32
1c3d002b 33/* Forward declarations */
41b7bd01 34SR_PRIV struct sr_dev_driver motech_lps_301_driver_info;
1c3d002b
MH
35SR_PRIV int lps_read_reply(struct sr_serial_dev_inst *serial, char **buf, int *buflen);
36SR_PRIV int lps_send_va(struct sr_serial_dev_inst *serial, const char* fmt, va_list args);
37SR_PRIV int lps_cmd_ok(struct sr_serial_dev_inst *serial, const char* fmt, ...);
38SR_PRIV int lps_cmd_reply(char* reply, struct sr_serial_dev_inst *serial, const char* fmt, ...);
39SR_PRIV int lps_query_status(struct sr_dev_inst* sdi);
40
41/* Serial communication parameters */
42#define SERIALCOMM "2400/8n1/dtr=1/rts=1/flow=0"
43
44#define VENDOR_MOTECH "Motech"
45
46/** Driver scanning options. */
47static const int32_t hwopts[] = {
48 SR_CONF_CONN,
49 SR_CONF_SERIALCOMM,
50};
51
52/** Hardware capabilities generic. */
53static const int32_t hwcaps[] = {
54 /* Device class */
55 SR_CONF_POWER_SUPPLY,
56 /* Aquisition modes. */
57 SR_CONF_LIMIT_SAMPLES,
58 SR_CONF_LIMIT_MSEC,
59 SR_CONF_CONTINUOUS,
60 /* Device configuration */
61 SR_CONF_OUTPUT_CHANNEL,
62};
63
64/** Hardware capabilities channel 1, 2. */
65static const int32_t hwcaps_ch12[] = {
66 SR_CONF_OUTPUT_VOLTAGE,
67 SR_CONF_OUTPUT_VOLTAGE_MAX,
68 SR_CONF_OUTPUT_CURRENT,
69 SR_CONF_OUTPUT_CURRENT_MAX,
70 SR_CONF_OUTPUT_ENABLED,
71};
72
73/** Hardware capabilities channel 3. (LPS-304/305 only). */
74static const int32_t hwcaps_ch3[] = {
75 SR_CONF_OUTPUT_VOLTAGE,
76 SR_CONF_OUTPUT_ENABLED,
77};
78
79static const char *channel_modes[] = {
80 "Independent",
81 "Track1",
82 "Track2",
83};
84
85static struct lps_modelspec models[] = {
86 { LPS_UNKNOWN, "Dummy", 0,
87 {
88
89 }
90 },
91 { LPS_301, "LPS-301", 1,
92 {
93 /* Channel 1 */
94 { { 0, 32, 0.01 }, { 0.005, 2, 0.001 } },
95 },
96 },
97 { LPS_302, "LPS-302", 1,
98 {
99 /* Channel 1 */
100 { { 0, 32, 0.01 }, { 0.005, 3, 0.001 } },
101 },
102 },
103 { LPS_303, "LPS-303", 1,
104 {
105 /* Channel 1 */
106 { { 0, 32, 0.01 }, { 0.005, 3, 0.001 } },
107 },
108 },
109 { LPS_304, "LPS-304", 3,
110 {
111 /* Channel 1 */
112 { { 0, 32, 0.01 }, { 0.005, 3, 0.001 } },
113 /* Channel 2 */
114 { { 0, 32, 0.01 }, { 0.005, 3, 0.001 } },
115 /* Channel 3 */
116 { { 5, 5, 0.0 }, { 0.005, 3, 0.001 } },
117 },
118 },
119 { LPS_305, "LPS-305", 3,
120 {
121 /* Channel 1 */
122 { { 0, 32, 0.01 }, { 0.005, 3, 0.001 } },
123 /* Channel 2 */
124 { { 0, 32, 0.01 }, { 0.005, 3, 0.001 } },
125 /* Channel 3 */
126 { { 3.3, 5, 1.7 }, { 0.005, 3, 0.001 } },
127 },
128 },
129};
41b7bd01 130
1c3d002b 131static int init_lps301(struct sr_context *sr_ctx)
41b7bd01 132{
1c3d002b 133 return std_init(sr_ctx, &motech_lps_301_driver_info, LOG_PREFIX);
41b7bd01
MH
134}
135
1c3d002b
MH
136/** Send command to device with va_list.
137 */
138SR_PRIV int lps_send_va(struct sr_serial_dev_inst *serial, const char* fmt, va_list args)
41b7bd01 139{
1c3d002b
MH
140 int retc;
141 char auxfmt[LINELEN_MAX];
142 char buf[LINELEN_MAX];
41b7bd01 143
1c3d002b
MH
144 snprintf(auxfmt, sizeof(auxfmt), "%s\r\n", fmt);
145 vsnprintf(buf, sizeof(buf), auxfmt, args);
41b7bd01 146
1c3d002b 147 sr_spew("lps_send_va: \"%s\"", buf);
41b7bd01 148
1c3d002b 149 retc = serial_write_nonblocking(serial, buf, strlen(buf));
41b7bd01 150
1c3d002b
MH
151 if (retc < 0)
152 return SR_ERR;
153
154 return SR_OK;
41b7bd01
MH
155}
156
1c3d002b
MH
157/** Send command to device.
158 */
159SR_PRIV int lps_send_req(struct sr_serial_dev_inst *serial, const char* fmt, ...)
41b7bd01 160{
1c3d002b
MH
161 int retc;
162 va_list args;
163
164 va_start(args, fmt);
165 retc = lps_send_va(serial, fmt, args);
166 va_end(args);
167
168 return retc;
41b7bd01
MH
169}
170
1c3d002b
MH
171/** Send command and consume simple OK reply. */
172SR_PRIV int lps_cmd_ok(struct sr_serial_dev_inst *serial, const char* fmt, ...)
41b7bd01 173{
1c3d002b
MH
174 int retc;
175 va_list args;
176 char buf[LINELEN_MAX];
177 char* bufptr;
178 int buflen;
179
180 /* Send command */
181 va_start(args, fmt);
182 retc = lps_send_va(serial, fmt, args);
183 va_end(args);
184
185 if (retc != SR_OK)
186 return SR_ERR;
187
188 /* Read reply */
189 buf[0] = '\0';
190 bufptr = buf;
191 buflen = sizeof(buf);
192 retc = lps_read_reply(serial, &bufptr, &buflen);
193 if ((retc == SR_OK) && (buflen == 0))
194 return SR_OK;
195
196 return SR_ERR;
41b7bd01
MH
197}
198
1c3d002b
MH
199/** Send command and read reply string.
200 * \param reply Pointer to buffer of size LINELEN_MAX. Will be NUL-terminated.
201 */
202SR_PRIV int lps_cmd_reply(char* reply, struct sr_serial_dev_inst *serial, const char* fmt, ...)
41b7bd01 203{
1c3d002b
MH
204 int retc;
205 va_list args;
206 char buf[LINELEN_MAX];
207 char* bufptr;
208 int buflen;
209
210 reply[0] = '\0';
211
212 /* Send command */
213 va_start(args, fmt);
214 retc = lps_send_va(serial, fmt, args);
215 va_end(args);
216
217 if (retc != SR_OK)
218 return SR_ERR;
219
220 /* Read reply */
221 buf[0] = '\0';
222 bufptr = buf;
223 buflen = sizeof(buf);
224 retc = lps_read_reply(serial, &bufptr, &buflen);
225 if ((retc == SR_OK) && (buflen > 0)) {
226 strcpy(reply, buf);
227 return SR_OK;
228 }
41b7bd01 229
1c3d002b
MH
230 return SR_ERR;
231}
41b7bd01 232
1c3d002b
MH
233/** Process integer value returned by STATUS command. */
234SR_PRIV int lps_process_status(struct sr_dev_inst* sdi, int stat)
235{
236 struct dev_context* devc;
237 int tracking_mode;
238
239 devc = (struct dev_context*)sdi->priv;
240
241 sr_spew("Status: %d", stat);
242 devc->channel_status[0].cc_mode = (stat & 0x01) != 0;
243 sr_spew("Channel 1 %s mode", devc->channel_status[0].cc_mode?"CC":"CV");
244 if (devc->model->num_channels > 1) {
245 devc->channel_status[1].cc_mode = (stat & 0x02) != 0;
246 sr_spew("Channel 2 %s mode", devc->channel_status[1].cc_mode?"CC":"CV");
247
248 tracking_mode = (stat & 0x0c) >> 2;
249 switch (tracking_mode) {
250 case 0: devc->tracking_mode = 0;
251 break;
252 case 2: devc->tracking_mode = 1;
253 break;
254 case 3: devc->tracking_mode = 2;
255 break;
256 default:
257 sr_err("Illegal channel tracking mode %d!", tracking_mode);
258 devc->tracking_mode = 0;
259 break;
260 }
261
262 sr_spew("Channel tracking: %d", devc->tracking_mode);
263 }
264 devc->channel_status[0].output_enabled = devc->channel_status[1].output_enabled = stat&0x040?TRUE:FALSE;
265 sr_spew("Channel 1%s output: %s", devc->model->num_channels > 1?"+2":"", devc->channel_status[0].output_enabled?"ON":"OFF");
266 if (devc->model->num_channels > 2) {
267 devc->channel_status[2].output_enabled = stat&0x010?TRUE:FALSE;
268 devc->channel_status[2].output_voltage_last = stat&0x020?3.3:5;
269 sr_spew("Channel 3 output: %s, U=%02f V, overload=%d",
270 devc->channel_status[2].output_enabled?"ON":"OFF",
271 devc->channel_status[2].output_voltage_last,
272 stat&0x080?1:0);
273 }
274 sr_spew("Fan=%d, beep=%d, CC output compensated=%d", stat&0x0100?1:0, stat&0x0200?1:0, stat&0x0400?1:0);
41b7bd01
MH
275
276 return SR_OK;
277}
278
1c3d002b
MH
279/** Send STATUS commend and process status string. */
280SR_PRIV int lps_query_status(struct sr_dev_inst* sdi)
41b7bd01 281{
1c3d002b
MH
282 char buf[LINELEN_MAX];
283 int stat;
284 const char* ptr;
285 struct dev_context* devc;
41b7bd01 286
1c3d002b 287 devc = (struct dev_context*)sdi->priv;
41b7bd01 288
1c3d002b 289 devc->req_sent_at = g_get_real_time();
41b7bd01 290
1c3d002b
MH
291 if (lps_cmd_reply(buf, sdi->conn, "STATUS") < 0) {
292 sr_err("%s: Failed to read status: %d %s", __func__, errno, strerror(errno));
293 return SR_ERR;
294 }
295
296 /* Jump across leading zeros to prevent sr_atoi() believing this to be octal. */
297 for (ptr = buf; isspace(*buf) || (*ptr == '0'); ptr++)
298 ;
299
300 if (sr_atoi(ptr, &stat) != SR_OK)
301 return SR_ERR;
302
303 return lps_process_status(sdi, stat);
41b7bd01
MH
304}
305
1c3d002b 306static gint64 calc_timeout_ms(gint64 start_us)
41b7bd01 307{
1c3d002b 308 gint64 result = REQ_TIMEOUT_MS - ((g_get_real_time() - start_us) / 1000);
41b7bd01 309
1c3d002b
MH
310 if (result < 0)
311 return 0;
41b7bd01 312
1c3d002b 313 return result;
41b7bd01
MH
314}
315
1c3d002b
MH
316/** Read message into buf until "OK" received.
317 * \retval SR_OK Msg received; buf and buflen contain result, if any except OK.
318 * \retval SR_ERR Error, including timeout.
319*/
320SR_PRIV int lps_read_reply(struct sr_serial_dev_inst *serial, char **buf, int *buflen)
41b7bd01 321{
1c3d002b
MH
322 int retries;
323 char buf2[LINELEN_MAX];
324 char *buf2ptr;
325 int buf2len;
326 gint64 timeout_start;
327
328 *buf[0] = '\0';
329
330 /* Read one line. It is either a data message or "OK". */
331 timeout_start = g_get_real_time();
332 buf2len = *buflen;
333 /* Up to 5 tries because serial_readline() will consume only one CR or LF per
334 * call, but device sends up to 4 in a row. */
335 for (retries = 0; retries < 5; retries++) {
336 *buflen = buf2len;
337 if (serial_readline(serial, buf, buflen, calc_timeout_ms(timeout_start)) != SR_OK)
338 return SR_ERR;
339 if (!strcmp(*buf, "OK")) { /* We got an OK! */
340 *buf[0] = '\0';
341 *buflen = 0;
342 return SR_OK;
343 }
344 if (*buflen > 0) /* We got a msg! */
345 break;
346 }
41b7bd01 347
1c3d002b
MH
348 /* A data msg is in buf (possibly ERROR), need to consume "OK". */
349 buf2[0] = '\0';
350 buf2ptr = buf2;
351 for (retries = 0; retries < 5; retries++) {
352 buf2len = sizeof(buf2);
353 if (serial_readline(serial, &buf2ptr, &buf2len, calc_timeout_ms(timeout_start)) != SR_OK)
354 return SR_ERR;
355
356 if (!strcmp(buf2ptr, "OK")) { /* We got an OK! */
357 if (!strcmp(*buf, "ERROR")) { /* OK came after msg ERROR! */
358 sr_spew("ERROR found!");
359 *buf[0] = '\0';
360 *buflen = 0;
361 return SR_ERR;
362 }
363 return SR_OK;
364 }
365 }
41b7bd01 366
1c3d002b
MH
367 return SR_ERR; /* Timeout! */
368}
369
370/** Scan for LPS-300 series device.
371 */
372static GSList *doScan(lps_modelid modelid, struct sr_dev_driver *drv, GSList *options)
373{
374 struct sr_dev_inst *sdi;
375 struct drv_context *drvc;
376 struct dev_context *devc;
377 struct sr_serial_dev_inst *serial;
378 struct sr_channel *ch;
379 struct sr_channel_group *cg;
380 GSList *devices;
381 const char *conn, *serialcomm;
382 int cnt;
383 gchar buf[LINELEN_MAX];
384 gchar channel[10];
385 char* verstr;
386
387 sdi = NULL;
388 devc = NULL;
389 conn = serialcomm = NULL;
390 devices = NULL;
391
392 drvc = drv->priv;
393 drvc->instances = NULL;
394
395 sr_spew("scan() called!");
396
397 /* Process and check options. */
398 if (sr_serial_extract_options(options, &conn, &serialcomm) != SR_OK)
399 return NULL;
400 if (!serialcomm)
401 serialcomm = SERIALCOMM;
402
403 /* Init serial port. */
404 if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
405 return NULL;
406
407 if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
408 goto exit_err;
409
410 /* Query and verify model string. */
411 serial_flush(serial);
412 if (lps_cmd_reply(buf, serial, "MODEL") != SR_OK)
413 return NULL;
414
415 /* Check model string. */
416 if (strncmp(buf, "LPS-", 4)) {
417 sr_spew("Unknown model code \"%s\"!", buf);
418 return NULL;
419 }
420
421 /* Bug in device FW 1.17, model number is empty, so this can't work with this FW! */
422 if (modelid == LPS_UNKNOWN) {
423 g_strstrip(buf);
424 for (cnt = LPS_301; cnt <= LPS_305; cnt++) {
425 if (!strcmp(buf, models[cnt].modelstr)) {
426 modelid = cnt;
427 break;
428 }
429 }
430 if (modelid == LPS_UNKNOWN) {
431 sr_err("Unable to detect model from model string '%s'!", buf);
432 return NULL;
433 }
41b7bd01
MH
434 }
435
1c3d002b
MH
436 /* Query version */
437 verstr = NULL;
438 if (lps_cmd_reply(buf, serial, "VERSION") == SR_OK) {
439 if (strncmp(buf, "Ver-", 4)) {
440 sr_spew("Version string %s not recognized.", buf);
441 goto exit_err;
442 }
443
444
445 g_strstrip(buf);
446 verstr = buf + 4;
447 }
448 else /* Bug in device FW 1.17: Quering version string fails while output is active.
449 Therefore just print an error message, but do not exit with error. */
450 sr_err("Failed to query for hardware version: %d %s", errno, strerror(errno));
451
452 sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, VENDOR_MOTECH, models[modelid].modelstr, verstr);
453 sdi->driver = drv;
454 sdi->inst_type = SR_INST_SERIAL;
455 sdi->conn = serial;
456
457 devc = g_malloc0(sizeof(struct dev_context));
458 devc->model = &models[modelid];
459 devc->limit_samples = 0;
460 devc->limit_msec = 0;
461 devc->num_samples = 0;
462 devc->elapsed_msec = g_timer_new();
463
464 sdi->priv = devc;
465
466 /* Setup channels and channel groups. */
467 for (cnt = 0; cnt < models[modelid].num_channels; cnt++) {
468 snprintf(channel, sizeof(channel), "CH%d", cnt + 1);
469 ch = sr_channel_new(cnt, SR_CHANNEL_ANALOG, TRUE, channel);
470 sdi->channels = g_slist_append(sdi->channels, ch);
471
472 devc->channel_status[cnt].info = g_slist_append(NULL, ch);
473
474 cg = g_malloc(sizeof(struct sr_channel_group));
475 snprintf(channel, sizeof(channel), "CG%d", cnt+1);
476 cg->name = g_strdup(channel);
477 cg->priv = NULL;
478 cg->channels = g_slist_append(NULL, ch);
479
480 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
481 }
482
483 drvc->instances = g_slist_append(drvc->instances, sdi);
484 devices = g_slist_append(devices, sdi);
485
486 /* Query status */
487 if (lps_query_status(sdi) != SR_OK)
488 goto exit_err;
489
490 serial_close(serial);
491 if (!devices)
492 sr_serial_dev_inst_free(serial);
493
494 return devices;
495
496exit_err:
497 sr_info("%s: Error!", __func__);
498
499 if (serial) {
500 serial_close(serial);
501 sr_serial_dev_inst_free(serial);
502 }
503 if (devc)
504 g_free(devc);
505 if (sdi)
506 sr_dev_inst_free(sdi);
507
508 return NULL;
41b7bd01
MH
509}
510
1c3d002b
MH
511/** Scan for LPS-301 device. */
512static GSList *scan_lps301(GSList *options)
513{
514 return doScan(LPS_301, &motech_lps_301_driver_info, options);
515}
516
517static GSList *doDevList(struct sr_dev_driver *drv)
518{
519 return ((struct drv_context *)(drv->priv))->instances;
520}
521
522static GSList *dev_list_lps301(void)
523{
524 return doDevList(&motech_lps_301_driver_info);
525}
526
527static void dev_clear_private(struct dev_context* devc)
528{
529 int ch_idx;
530
531 /* Free channel_status.info (list only, data owned by sdi). */
532 for (ch_idx = 0; ch_idx < devc->model->num_channels; ch_idx++)
533 g_slist_free(devc->channel_status[ch_idx].info);
534
535 g_timer_destroy(devc->elapsed_msec);
536}
537
538static int dev_clear_lps301(void)
539{
540 return std_dev_clear(&motech_lps_301_driver_info, (std_dev_clear_callback)dev_clear_private);
541}
542
543static int cleanup(void)
544{
545 return dev_clear_lps301();
546}
547
548static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
41b7bd01
MH
549 const struct sr_channel_group *cg)
550{
1c3d002b
MH
551 struct dev_context *devc;
552 struct sr_channel *ch;
553 int ch_idx;
554
555 if (!sdi)
556 return SR_ERR_ARG;
557
558 devc = sdi->priv;
559
560 if (!cg) {
561 /* No channel group: global options. */
562 switch (key) {
563 case SR_CONF_LIMIT_SAMPLES:
564 *data = g_variant_new_uint64(devc->limit_samples);
565 break;
566 case SR_CONF_LIMIT_MSEC:
567 *data = g_variant_new_uint64(devc->limit_msec);
568 break;
569 case SR_CONF_OUTPUT_CHANNEL:
570 *data = g_variant_new_string(channel_modes[devc->tracking_mode]);
571 break;
572 default:
573 return SR_ERR_NA;
574 }
575 } else {
576 /* We only ever have one channel per channel group in this driver. */
577 ch = cg->channels->data;
578 ch_idx = ch->index;
579 switch (key) {
580 case SR_CONF_OUTPUT_VOLTAGE:
581 *data = g_variant_new_double(devc->channel_status[ch_idx].output_voltage_last);
582 break;
583 case SR_CONF_OUTPUT_VOLTAGE_MAX:
584 *data = g_variant_new_double(devc->channel_status[ch_idx].output_voltage_max);
585 break;
586 case SR_CONF_OUTPUT_CURRENT:
587 *data = g_variant_new_double(devc->channel_status[ch_idx].output_current_last);
588 break;
589 case SR_CONF_OUTPUT_CURRENT_MAX:
590 *data = g_variant_new_double(devc->channel_status[ch_idx].output_current_max);
591 break;
592 case SR_CONF_OUTPUT_ENABLED:
593 *data = g_variant_new_boolean(devc->channel_status[ch_idx].output_enabled);
594 break;
595 default:
596 return SR_ERR_NA;
597 }
598 }
41b7bd01 599
1c3d002b
MH
600 return SR_OK;
601}
602
603static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
604 const struct sr_channel_group *cg)
605{
606 struct dev_context *devc;
607 struct sr_channel *ch;
608 gdouble dval;
609 int ch_idx;
610 const char *sval;
611 gboolean bval;
612 int idx;
613 gboolean found;
41b7bd01
MH
614
615 if (sdi->status != SR_ST_ACTIVE)
616 return SR_ERR_DEV_CLOSED;
617
1c3d002b
MH
618 devc = sdi->priv;
619
620 /* Cannot change settings while aquisition active, would cause a mess with commands.
621 * Changing this would be possible, but tricky. */
622 if (devc->acq_running)
623 return SR_ERR_NA;
624
625 if (!cg) {
626 /* No channel group: global options. */
627 switch (key) {
628 case SR_CONF_LIMIT_MSEC:
629 if (g_variant_get_uint64(data) == 0) {
630 sr_err("LIMIT_MSEC can't be 0.");
631 return SR_ERR;
632 }
633 devc->limit_msec = g_variant_get_uint64(data);
634 sr_dbg("Setting time limit to %" PRIu64 "ms.",
635 devc->limit_msec);
636 break;
637 case SR_CONF_LIMIT_SAMPLES:
638 devc->limit_samples = g_variant_get_uint64(data);
639 sr_dbg("Setting sample limit to %" PRIu64 ".",
640 devc->limit_samples);
641 break;
642 case SR_CONF_OUTPUT_CHANNEL:
643 sval = g_variant_get_string(data, NULL);
644 found = FALSE;
645 for (idx = 0; idx < (int)ARRAY_SIZE(channel_modes); idx++)
646 {
647 if (!strcmp(sval, channel_modes[idx])) {
648 found = TRUE;
649 if (devc->tracking_mode == idx)
650 break; /* Nothing to do! */
651 devc->tracking_mode = idx;
652 if (devc->model->modelid >= LPS_304) /* No use to set anything in the smaller models. */
653 return lps_cmd_ok(sdi->conn, "TRACK%1d", devc->tracking_mode);
654 }
655 if (devc->model->modelid <= LPS_303) /* Only first setting possible for smaller models. */
656 break;
657 }
658 if (!found) {
659 return SR_ERR_ARG;
660 }
661 break;
662 default:
663 return SR_ERR_NA;
664 }
665 } else {
666 /* Channel group specified: per-channel options. */
667 /* We only ever have one channel per channel group in this driver. */
668 ch = cg->channels->data;
669 ch_idx = ch->index;
670
671 switch (key) {
672 case SR_CONF_OUTPUT_VOLTAGE_MAX:
673 dval = g_variant_get_double(data);
674 if (dval < 0 || dval > devc->model->channels[ch_idx].voltage[1])
675 return SR_ERR_ARG;
676 if (ch_idx == 2) {
677 if (devc->model->modelid < LPS_304)
678 return SR_ERR_ARG;
679
680 if (fabs(dval - 5.000) <= 0.001)
681 dval = 5.0;
682 else if ((devc->model->modelid >= LPS_305) && (fabs(dval - 3.300) <= 0.001))
683 dval = 3.3;
684 else return SR_ERR_ARG;
685 }
686
687 devc->channel_status[ch_idx].output_voltage_max = dval;
688 if (ch_idx == 2)
689 return lps_cmd_ok(sdi->conn, "VDD%1.0f", trunc(dval));
690 else
691 return lps_cmd_ok(sdi->conn, "VSET%d %05.3f", ch_idx+1, dval);
692 break;
693 case SR_CONF_OUTPUT_CURRENT_MAX:
694 dval = g_variant_get_double(data);
695 if (dval < 0 || dval > devc->model->channels[ch_idx].current[1])
696 return SR_ERR_ARG;
697 if (ch_idx == 2) /* No current setting for CH3. */
698 return SR_ERR_NA;
699 devc->channel_status[ch_idx].output_current_max = dval;
700 return lps_cmd_ok(sdi->conn, "ISET%d %05.4f", ch_idx+1, dval);
701 break;
702 case SR_CONF_OUTPUT_ENABLED:
703 bval = g_variant_get_boolean(data);
704 if (bval == devc->channel_status[ch_idx].output_enabled) /* Nothing to do. */
705 break;
706 devc->channel_status[ch_idx].output_enabled = bval;
707 if (ch_idx != 2) { /* Channels 1,2 can be set only together. */
708 devc->channel_status[ch_idx^1].output_enabled = bval;
709 return lps_cmd_ok(sdi->conn, "OUT%1d", (int)bval);
710 } else { /* Channel 3: No command to disable output, set voltage to 0 instead. */
711 if (bval)
712 return lps_cmd_ok(sdi->conn, "VDD%1.0f", devc->channel_status[ch_idx].output_voltage_max);
713 else
714 return lps_cmd_ok(sdi->conn, "VDD0");
715 }
716 break;
717 default:
718 return SR_ERR_NA;
719 }
41b7bd01
MH
720 }
721
1c3d002b 722 return SR_OK;
41b7bd01
MH
723}
724
725static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
726 const struct sr_channel_group *cg)
727{
1c3d002b
MH
728 struct dev_context *devc;
729 struct sr_channel *ch;
730 int ch_idx, i;
731 GVariant *gvar;
732 GVariantBuilder gvb;
41b7bd01 733
41b7bd01 734 (void)data;
41b7bd01 735
1c3d002b 736 /* Driver options, no device instance necessary. */
41b7bd01 737 switch (key) {
1c3d002b
MH
738 case SR_CONF_SCAN_OPTIONS:
739 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
740 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
741 return SR_OK;
742 default:
743 if (sdi == NULL)
744 return SR_ERR_ARG;
745
746 devc = sdi->priv;
747 }
748
749 /* Device options, independant from channel groups. */
750 if (cg == NULL) {
751 switch (key) {
752 case SR_CONF_DEVICE_OPTIONS:
753 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
754 hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
755 return SR_OK;
756 case SR_CONF_OUTPUT_CHANNEL:
757 if (devc->model->modelid <= LPS_303) {
758 /* The 1-channel models. */
759 *data = g_variant_new_strv(channel_modes, 1);
760 } else {
761 /* The other models support all modes. */
762 *data = g_variant_new_strv(channel_modes, ARRAY_SIZE(channel_modes));
763 }
764 return SR_OK;
765 break;
766 default:
767 return SR_ERR_NA;
768 }
769 }
770
771 /* Device options, depending on channel groups. */
772 ch = cg->channels->data;
773 ch_idx = ch->index;
774 switch (key) {
775 case SR_CONF_DEVICE_OPTIONS:
776 if ((ch_idx == 0) || (ch_idx == 1)) /* CH1, CH2 */
777 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
778 hwcaps_ch12, ARRAY_SIZE(hwcaps_ch12), sizeof(int32_t));
779 else /* Must be CH3 */
780 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
781 hwcaps_ch3, ARRAY_SIZE(hwcaps_ch3), sizeof(int32_t));
782 break;
783 case SR_CONF_OUTPUT_VOLTAGE_MAX:
784 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
785 /* Min, max, step. */
786 for (i = 0; i < 3; i++) {
787 gvar = g_variant_new_double(devc->model->channels[ch_idx].voltage[i]);
788 g_variant_builder_add_value(&gvb, gvar);
789 }
790 *data = g_variant_builder_end(&gvb);
791 break;
792 case SR_CONF_OUTPUT_CURRENT_MAX:
793 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
794 /* Min, max, step. */
795 for (i = 0; i < 3; i++) {
796 gvar = g_variant_new_double(devc->model->channels[ch_idx].current[i]);
797 g_variant_builder_add_value(&gvb, gvar);
798 }
799 *data = g_variant_builder_end(&gvb);
800 break;
41b7bd01
MH
801 default:
802 return SR_ERR_NA;
803 }
804
1c3d002b 805 return SR_OK;
41b7bd01
MH
806}
807
808static int dev_acquisition_start(const struct sr_dev_inst *sdi,
809 void *cb_data)
810{
1c3d002b
MH
811 struct dev_context *devc;
812 struct sr_serial_dev_inst *serial;
41b7bd01
MH
813
814 if (sdi->status != SR_ST_ACTIVE)
815 return SR_ERR_DEV_CLOSED;
816
1c3d002b
MH
817 devc = sdi->priv;
818
819 devc->acq_running = TRUE;
820
821 serial = sdi->conn;
822 serial_source_add(serial, G_IO_IN, 50, motech_lps_30x_receive_data, (void *)sdi);
823 std_session_send_df_header(cb_data, LOG_PREFIX);
824
825 /* Start timer, if required. */
826 if (devc->limit_msec)
827 g_timer_start(devc->elapsed_msec);
828
829 devc->acq_req = AQ_NONE;
830 /* Do not start polling device here, the read function will do it in 50 ms. */
41b7bd01
MH
831
832 return SR_OK;
833}
834
835static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
836{
1c3d002b 837 struct dev_context *devc;
41b7bd01 838
1c3d002b
MH
839 /* Stop timer, if required. */
840 if (sdi && (devc = sdi->priv) && devc->limit_msec)
841 g_timer_stop(devc->elapsed_msec);
41b7bd01 842
1c3d002b
MH
843 return std_serial_dev_acquisition_stop(sdi, cb_data, std_serial_dev_close,
844 sdi->conn, LOG_PREFIX);
41b7bd01
MH
845}
846
847SR_PRIV struct sr_dev_driver motech_lps_301_driver_info = {
848 .name = "motech-lps-301",
849 .longname = "Motech LPS-301",
850 .api_version = 1,
1c3d002b 851 .init = init_lps301,
41b7bd01 852 .cleanup = cleanup,
1c3d002b
MH
853 .scan = scan_lps301,
854 .dev_list = dev_list_lps301,
855 .dev_clear = dev_clear_lps301,
41b7bd01
MH
856 .config_get = config_get,
857 .config_set = config_set,
858 .config_list = config_list,
1c3d002b
MH
859 .dev_open = std_serial_dev_open,
860 .dev_close = std_serial_dev_close,
41b7bd01
MH
861 .dev_acquisition_start = dev_acquisition_start,
862 .dev_acquisition_stop = dev_acquisition_stop,
863 .priv = NULL,
864};