]> sigrok.org Git - libsigrok.git/blame - src/hardware/motech-lps-30x/api.c
drivers: Consistently name per-cg options 'devopts_cg_*'.
[libsigrok.git] / src / 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
6ec6c43b 21#include <config.h>
1c3d002b 22#include <ctype.h>
1c3d002b
MH
23#include <math.h>
24#include <string.h>
41b7bd01 25#include "protocol.h"
05199c0a 26
1c3d002b 27SR_PRIV int lps_read_reply(struct sr_serial_dev_inst *serial, char **buf, int *buflen);
c442ffda
UH
28SR_PRIV int lps_send_va(struct sr_serial_dev_inst *serial, const char *fmt, va_list args);
29SR_PRIV int lps_cmd_ok(struct sr_serial_dev_inst *serial, const char *fmt, ...);
30SR_PRIV int lps_cmd_reply(char *reply, struct sr_serial_dev_inst *serial, const char *fmt, ...);
31SR_PRIV int lps_query_status(struct sr_dev_inst *sdi);
1c3d002b 32
1c3d002b
MH
33#define SERIALCOMM "2400/8n1/dtr=1/rts=1/flow=0"
34
35#define VENDOR_MOTECH "Motech"
36
a0e0bb41 37static const uint32_t scanopts[] = {
1c3d002b
MH
38 SR_CONF_CONN,
39 SR_CONF_SERIALCOMM,
40};
41
55fb76b3 42static const uint32_t drvopts[] = {
55fb76b3
UH
43 SR_CONF_POWER_SUPPLY,
44};
45
f254bc4b 46static const uint32_t devopts[] = {
1c3d002b 47 SR_CONF_CONTINUOUS,
5827f61b
BV
48 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
49 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
7a0b98b5 50 SR_CONF_CHANNEL_CONFIG | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
1c3d002b
MH
51};
52
53/** Hardware capabilities channel 1, 2. */
6b82c3e5 54static const uint32_t devopts_cg_ch12[] = {
7a0b98b5
AJ
55 SR_CONF_VOLTAGE | SR_CONF_GET,
56 SR_CONF_VOLTAGE_TARGET | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
57 SR_CONF_CURRENT | SR_CONF_GET,
58 SR_CONF_CURRENT_LIMIT | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
59 SR_CONF_ENABLED | SR_CONF_GET | SR_CONF_SET,
1c3d002b
MH
60};
61
ca314e06 62/** Hardware capabilities channel 3 (LPS-304/305 only). */
6b82c3e5 63static const uint32_t devopts_cg_ch3[] = {
7a0b98b5
AJ
64 SR_CONF_VOLTAGE | SR_CONF_GET,
65 SR_CONF_ENABLED | SR_CONF_GET | SR_CONF_SET,
1c3d002b
MH
66};
67
68static const char *channel_modes[] = {
69 "Independent",
70 "Track1",
71 "Track2",
72};
73
329733d9 74static const struct lps_modelspec models[] = {
1c3d002b
MH
75 { LPS_UNKNOWN, "Dummy", 0,
76 {
77
78 }
79 },
80 { LPS_301, "LPS-301", 1,
81 {
82 /* Channel 1 */
83 { { 0, 32, 0.01 }, { 0.005, 2, 0.001 } },
84 },
85 },
86 { LPS_302, "LPS-302", 1,
87 {
88 /* Channel 1 */
89 { { 0, 32, 0.01 }, { 0.005, 3, 0.001 } },
90 },
91 },
92 { LPS_303, "LPS-303", 1,
93 {
94 /* Channel 1 */
95 { { 0, 32, 0.01 }, { 0.005, 3, 0.001 } },
96 },
97 },
98 { LPS_304, "LPS-304", 3,
99 {
100 /* Channel 1 */
101 { { 0, 32, 0.01 }, { 0.005, 3, 0.001 } },
102 /* Channel 2 */
103 { { 0, 32, 0.01 }, { 0.005, 3, 0.001 } },
104 /* Channel 3 */
105 { { 5, 5, 0.0 }, { 0.005, 3, 0.001 } },
106 },
107 },
108 { LPS_305, "LPS-305", 3,
109 {
110 /* Channel 1 */
111 { { 0, 32, 0.01 }, { 0.005, 3, 0.001 } },
112 /* Channel 2 */
113 { { 0, 32, 0.01 }, { 0.005, 3, 0.001 } },
114 /* Channel 3 */
115 { { 3.3, 5, 1.7 }, { 0.005, 3, 0.001 } },
116 },
117 },
118};
41b7bd01 119
ca314e06 120/** Send command to device with va_list. */
c442ffda 121SR_PRIV int lps_send_va(struct sr_serial_dev_inst *serial, const char *fmt, va_list args)
41b7bd01 122{
1c3d002b
MH
123 int retc;
124 char auxfmt[LINELEN_MAX];
125 char buf[LINELEN_MAX];
41b7bd01 126
1c3d002b
MH
127 snprintf(auxfmt, sizeof(auxfmt), "%s\r\n", fmt);
128 vsnprintf(buf, sizeof(buf), auxfmt, args);
41b7bd01 129
1c3d002b 130 sr_spew("lps_send_va: \"%s\"", buf);
41b7bd01 131
1a65c5e8
UH
132 retc = serial_write_blocking(serial, buf, strlen(buf),
133 serial_timeout(serial, strlen(buf)));
41b7bd01 134
1c3d002b
MH
135 if (retc < 0)
136 return SR_ERR;
137
138 return SR_OK;
41b7bd01
MH
139}
140
ca314e06 141/** Send command to device. */
c442ffda 142SR_PRIV int lps_send_req(struct sr_serial_dev_inst *serial, const char *fmt, ...)
41b7bd01 143{
1c3d002b
MH
144 int retc;
145 va_list args;
146
147 va_start(args, fmt);
148 retc = lps_send_va(serial, fmt, args);
149 va_end(args);
150
151 return retc;
41b7bd01
MH
152}
153
1c3d002b 154/** Send command and consume simple OK reply. */
c442ffda 155SR_PRIV int lps_cmd_ok(struct sr_serial_dev_inst *serial, const char *fmt, ...)
41b7bd01 156{
1c3d002b
MH
157 int retc;
158 va_list args;
159 char buf[LINELEN_MAX];
c442ffda 160 char *bufptr;
d9251a2c 161 int buflen;
1c3d002b
MH
162
163 /* Send command */
164 va_start(args, fmt);
165 retc = lps_send_va(serial, fmt, args);
166 va_end(args);
167
168 if (retc != SR_OK)
169 return SR_ERR;
170
171 /* Read reply */
172 buf[0] = '\0';
173 bufptr = buf;
174 buflen = sizeof(buf);
175 retc = lps_read_reply(serial, &bufptr, &buflen);
176 if ((retc == SR_OK) && (buflen == 0))
177 return SR_OK;
178
179 return SR_ERR;
41b7bd01
MH
180}
181
d9251a2c
UH
182/**
183 * Send command and read reply string.
184 * @param reply Pointer to buffer of size LINELEN_MAX. Will be NUL-terminated.
1c3d002b 185 */
c442ffda 186SR_PRIV int lps_cmd_reply(char *reply, struct sr_serial_dev_inst *serial, const char *fmt, ...)
41b7bd01 187{
1c3d002b
MH
188 int retc;
189 va_list args;
190 char buf[LINELEN_MAX];
c442ffda 191 char *bufptr;
d9251a2c 192 int buflen;
1c3d002b
MH
193
194 reply[0] = '\0';
195
196 /* Send command */
197 va_start(args, fmt);
198 retc = lps_send_va(serial, fmt, args);
199 va_end(args);
200
201 if (retc != SR_OK)
202 return SR_ERR;
203
204 /* Read reply */
205 buf[0] = '\0';
206 bufptr = buf;
207 buflen = sizeof(buf);
208 retc = lps_read_reply(serial, &bufptr, &buflen);
209 if ((retc == SR_OK) && (buflen > 0)) {
210 strcpy(reply, buf);
211 return SR_OK;
212 }
41b7bd01 213
1c3d002b
MH
214 return SR_ERR;
215}
41b7bd01 216
1c3d002b 217/** Process integer value returned by STATUS command. */
c442ffda 218SR_PRIV int lps_process_status(struct sr_dev_inst *sdi, int stat)
1c3d002b 219{
c442ffda 220 struct dev_context *devc;
1c3d002b
MH
221 int tracking_mode;
222
c50805d5 223 devc = sdi->priv;
1c3d002b
MH
224
225 sr_spew("Status: %d", stat);
226 devc->channel_status[0].cc_mode = (stat & 0x01) != 0;
227 sr_spew("Channel 1 %s mode", devc->channel_status[0].cc_mode?"CC":"CV");
228 if (devc->model->num_channels > 1) {
229 devc->channel_status[1].cc_mode = (stat & 0x02) != 0;
230 sr_spew("Channel 2 %s mode", devc->channel_status[1].cc_mode?"CC":"CV");
231
232 tracking_mode = (stat & 0x0c) >> 2;
233 switch (tracking_mode) {
234 case 0: devc->tracking_mode = 0;
235 break;
236 case 2: devc->tracking_mode = 1;
237 break;
238 case 3: devc->tracking_mode = 2;
239 break;
240 default:
241 sr_err("Illegal channel tracking mode %d!", tracking_mode);
242 devc->tracking_mode = 0;
243 break;
244 }
245
246 sr_spew("Channel tracking: %d", devc->tracking_mode);
247 }
248 devc->channel_status[0].output_enabled = devc->channel_status[1].output_enabled = stat&0x040?TRUE:FALSE;
249 sr_spew("Channel 1%s output: %s", devc->model->num_channels > 1?"+2":"", devc->channel_status[0].output_enabled?"ON":"OFF");
250 if (devc->model->num_channels > 2) {
251 devc->channel_status[2].output_enabled = stat&0x010?TRUE:FALSE;
252 devc->channel_status[2].output_voltage_last = stat&0x020?3.3:5;
253 sr_spew("Channel 3 output: %s, U=%02f V, overload=%d",
254 devc->channel_status[2].output_enabled?"ON":"OFF",
255 devc->channel_status[2].output_voltage_last,
256 stat&0x080?1:0);
257 }
258 sr_spew("Fan=%d, beep=%d, CC output compensated=%d", stat&0x0100?1:0, stat&0x0200?1:0, stat&0x0400?1:0);
41b7bd01
MH
259
260 return SR_OK;
261}
262
1c3d002b 263/** Send STATUS commend and process status string. */
c442ffda 264SR_PRIV int lps_query_status(struct sr_dev_inst *sdi)
41b7bd01 265{
1c3d002b 266 char buf[LINELEN_MAX];
8d522801 267 int stat, ret;
c442ffda 268 struct dev_context *devc;
41b7bd01 269
c50805d5 270 devc = sdi->priv;
41b7bd01 271
1c3d002b 272 devc->req_sent_at = g_get_real_time();
41b7bd01 273
8d522801
UH
274 if ((ret = lps_cmd_reply(buf, sdi->conn, "STATUS")) < 0) {
275 sr_err("%s: Failed to read status: %s.", __func__,
276 sr_strerror(ret));
1c3d002b
MH
277 return SR_ERR;
278 }
279
1f7da0c2 280 if (sr_atoi(buf, &stat) != SR_OK)
1c3d002b
MH
281 return SR_ERR;
282
283 return lps_process_status(sdi, stat);
41b7bd01
MH
284}
285
1c3d002b 286static gint64 calc_timeout_ms(gint64 start_us)
41b7bd01 287{
1c3d002b 288 gint64 result = REQ_TIMEOUT_MS - ((g_get_real_time() - start_us) / 1000);
41b7bd01 289
1c3d002b
MH
290 if (result < 0)
291 return 0;
41b7bd01 292
1c3d002b 293 return result;
41b7bd01
MH
294}
295
d9251a2c
UH
296/**
297 * Read message into buf until "OK" received.
298 *
299 * @retval SR_OK Msg received; buf and buflen contain result, if any except OK.
300 * @retval SR_ERR Error, including timeout.
1c3d002b
MH
301*/
302SR_PRIV int lps_read_reply(struct sr_serial_dev_inst *serial, char **buf, int *buflen)
41b7bd01 303{
1c3d002b
MH
304 int retries;
305 char buf2[LINELEN_MAX];
306 char *buf2ptr;
307 int buf2len;
308 gint64 timeout_start;
309
310 *buf[0] = '\0';
311
312 /* Read one line. It is either a data message or "OK". */
313 timeout_start = g_get_real_time();
314 buf2len = *buflen;
315 /* Up to 5 tries because serial_readline() will consume only one CR or LF per
316 * call, but device sends up to 4 in a row. */
317 for (retries = 0; retries < 5; retries++) {
318 *buflen = buf2len;
319 if (serial_readline(serial, buf, buflen, calc_timeout_ms(timeout_start)) != SR_OK)
320 return SR_ERR;
321 if (!strcmp(*buf, "OK")) { /* We got an OK! */
322 *buf[0] = '\0';
323 *buflen = 0;
324 return SR_OK;
325 }
326 if (*buflen > 0) /* We got a msg! */
327 break;
328 }
41b7bd01 329
1c3d002b
MH
330 /* A data msg is in buf (possibly ERROR), need to consume "OK". */
331 buf2[0] = '\0';
332 buf2ptr = buf2;
333 for (retries = 0; retries < 5; retries++) {
334 buf2len = sizeof(buf2);
335 if (serial_readline(serial, &buf2ptr, &buf2len, calc_timeout_ms(timeout_start)) != SR_OK)
336 return SR_ERR;
337
338 if (!strcmp(buf2ptr, "OK")) { /* We got an OK! */
339 if (!strcmp(*buf, "ERROR")) { /* OK came after msg ERROR! */
340 sr_spew("ERROR found!");
341 *buf[0] = '\0';
342 *buflen = 0;
343 return SR_ERR;
344 }
345 return SR_OK;
346 }
347 }
41b7bd01 348
1c3d002b
MH
349 return SR_ERR; /* Timeout! */
350}
351
ca314e06 352/** Scan for LPS-300 series device. */
1f7da0c2 353static GSList *do_scan(lps_modelid modelid, struct sr_dev_driver *drv, GSList *options)
1c3d002b
MH
354{
355 struct sr_dev_inst *sdi;
1c3d002b
MH
356 struct dev_context *devc;
357 struct sr_serial_dev_inst *serial;
358 struct sr_channel *ch;
359 struct sr_channel_group *cg;
1c3d002b 360 const char *conn, *serialcomm;
8d522801 361 int cnt, ret;
0af636be 362 gchar buf[LINELEN_MAX];
1c3d002b 363 gchar channel[10];
0af636be 364 char *verstr;
1c3d002b
MH
365
366 sdi = NULL;
367 devc = NULL;
368 conn = serialcomm = NULL;
1c3d002b 369
1c3d002b
MH
370 /* Process and check options. */
371 if (sr_serial_extract_options(options, &conn, &serialcomm) != SR_OK)
372 return NULL;
373 if (!serialcomm)
374 serialcomm = SERIALCOMM;
375
376 /* Init serial port. */
91219afc 377 serial = sr_serial_dev_inst_new(conn, serialcomm);
1c3d002b 378
5b980134 379 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
1c3d002b
MH
380 goto exit_err;
381
382 /* Query and verify model string. */
383 serial_flush(serial);
384 if (lps_cmd_reply(buf, serial, "MODEL") != SR_OK)
385 return NULL;
386
387 /* Check model string. */
388 if (strncmp(buf, "LPS-", 4)) {
389 sr_spew("Unknown model code \"%s\"!", buf);
390 return NULL;
391 }
392
393 /* Bug in device FW 1.17, model number is empty, so this can't work with this FW! */
394 if (modelid == LPS_UNKNOWN) {
395 g_strstrip(buf);
396 for (cnt = LPS_301; cnt <= LPS_305; cnt++) {
397 if (!strcmp(buf, models[cnt].modelstr)) {
398 modelid = cnt;
399 break;
400 }
401 }
402 if (modelid == LPS_UNKNOWN) {
403 sr_err("Unable to detect model from model string '%s'!", buf);
404 return NULL;
405 }
41b7bd01
MH
406 }
407
1c3d002b
MH
408 /* Query version */
409 verstr = NULL;
8d522801 410 if ((ret = lps_cmd_reply(buf, serial, "VERSION")) == SR_OK) {
1c3d002b
MH
411 if (strncmp(buf, "Ver-", 4)) {
412 sr_spew("Version string %s not recognized.", buf);
413 goto exit_err;
414 }
415
1c3d002b
MH
416 g_strstrip(buf);
417 verstr = buf + 4;
418 }
d9251a2c 419 else /* Bug in device FW 1.17: Querying version string fails while output is active.
1c3d002b 420 Therefore just print an error message, but do not exit with error. */
8d522801
UH
421 sr_err("Failed to query for hardware version: %s.",
422 sr_strerror(ret));
1c3d002b 423
aac29cc1 424 sdi = g_malloc0(sizeof(struct sr_dev_inst));
0af636be
UH
425 sdi->status = SR_ST_INACTIVE;
426 sdi->vendor = g_strdup(VENDOR_MOTECH);
427 sdi->model = g_strdup(models[modelid].modelstr);
428 sdi->version = g_strdup(verstr);
1c3d002b
MH
429 sdi->inst_type = SR_INST_SERIAL;
430 sdi->conn = serial;
431
432 devc = g_malloc0(sizeof(struct dev_context));
8aafc5e6 433 sr_sw_limits_init(&devc->limits);
1c3d002b 434 devc->model = &models[modelid];
1c3d002b
MH
435
436 sdi->priv = devc;
437
438 /* Setup channels and channel groups. */
439 for (cnt = 0; cnt < models[modelid].num_channels; cnt++) {
440 snprintf(channel, sizeof(channel), "CH%d", cnt + 1);
5e23fcab 441 ch = sr_channel_new(sdi, cnt, SR_CHANNEL_ANALOG, TRUE, channel);
1c3d002b
MH
442
443 devc->channel_status[cnt].info = g_slist_append(NULL, ch);
444
445 cg = g_malloc(sizeof(struct sr_channel_group));
446 snprintf(channel, sizeof(channel), "CG%d", cnt+1);
447 cg->name = g_strdup(channel);
448 cg->priv = NULL;
449 cg->channels = g_slist_append(NULL, ch);
450
451 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
452 }
453
1c3d002b
MH
454 /* Query status */
455 if (lps_query_status(sdi) != SR_OK)
456 goto exit_err;
457
458 serial_close(serial);
1c3d002b 459
43376f33 460 return std_scan_complete(drv, g_slist_append(NULL, sdi));
1c3d002b
MH
461
462exit_err:
463 sr_info("%s: Error!", __func__);
464
04891a99 465 if (serial)
1c3d002b 466 serial_close(serial);
04891a99 467 sr_serial_dev_inst_free(serial);
b1f83103 468 g_free(devc);
4bf93988 469 sr_dev_inst_free(sdi);
1c3d002b
MH
470
471 return NULL;
41b7bd01
MH
472}
473
1c3d002b 474/** Scan for LPS-301 device. */
4f840ce9 475static GSList *scan_lps301(struct sr_dev_driver *di, GSList *options)
1c3d002b 476{
4f840ce9 477 return do_scan(LPS_301, di, options);
1c3d002b
MH
478}
479
53279f13 480static void clear_helper(struct dev_context *devc)
1c3d002b
MH
481{
482 int ch_idx;
483
484 /* Free channel_status.info (list only, data owned by sdi). */
485 for (ch_idx = 0; ch_idx < devc->model->num_channels; ch_idx++)
486 g_slist_free(devc->channel_status[ch_idx].info);
1c3d002b
MH
487}
488
53279f13 489static int dev_clear(const struct sr_dev_driver *di)
1c3d002b 490{
53279f13 491 return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper);
1c3d002b
MH
492}
493
584560f1 494static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
41b7bd01
MH
495 const struct sr_channel_group *cg)
496{
1c3d002b
MH
497 struct dev_context *devc;
498 struct sr_channel *ch;
499 int ch_idx;
500
501 if (!sdi)
502 return SR_ERR_ARG;
503
504 devc = sdi->priv;
505
506 if (!cg) {
1c3d002b
MH
507 switch (key) {
508 case SR_CONF_LIMIT_SAMPLES:
1c3d002b 509 case SR_CONF_LIMIT_MSEC:
8aafc5e6 510 return sr_sw_limits_config_get(&devc->limits, key, data);
7a0b98b5 511 case SR_CONF_CHANNEL_CONFIG:
1c3d002b
MH
512 *data = g_variant_new_string(channel_modes[devc->tracking_mode]);
513 break;
514 default:
515 return SR_ERR_NA;
516 }
517 } else {
518 /* We only ever have one channel per channel group in this driver. */
519 ch = cg->channels->data;
520 ch_idx = ch->index;
ca314e06 521
1c3d002b 522 switch (key) {
7a0b98b5 523 case SR_CONF_VOLTAGE:
1c3d002b
MH
524 *data = g_variant_new_double(devc->channel_status[ch_idx].output_voltage_last);
525 break;
7a0b98b5 526 case SR_CONF_VOLTAGE_TARGET:
1c3d002b
MH
527 *data = g_variant_new_double(devc->channel_status[ch_idx].output_voltage_max);
528 break;
7a0b98b5 529 case SR_CONF_CURRENT:
1c3d002b
MH
530 *data = g_variant_new_double(devc->channel_status[ch_idx].output_current_last);
531 break;
7a0b98b5 532 case SR_CONF_CURRENT_LIMIT:
1c3d002b
MH
533 *data = g_variant_new_double(devc->channel_status[ch_idx].output_current_max);
534 break;
7a0b98b5 535 case SR_CONF_ENABLED:
1c3d002b
MH
536 *data = g_variant_new_boolean(devc->channel_status[ch_idx].output_enabled);
537 break;
538 default:
539 return SR_ERR_NA;
540 }
541 }
41b7bd01 542
1c3d002b
MH
543 return SR_OK;
544}
545
584560f1 546static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
1c3d002b
MH
547 const struct sr_channel_group *cg)
548{
549 struct dev_context *devc;
550 struct sr_channel *ch;
551 gdouble dval;
552 int ch_idx;
553 const char *sval;
554 gboolean bval;
555 int idx;
556 gboolean found;
41b7bd01 557
1c3d002b
MH
558 devc = sdi->priv;
559
419bfb50 560 /* Cannot change settings while acquisition active, would cause a mess with commands.
1c3d002b
MH
561 * Changing this would be possible, but tricky. */
562 if (devc->acq_running)
563 return SR_ERR_NA;
564
565 if (!cg) {
1c3d002b
MH
566 switch (key) {
567 case SR_CONF_LIMIT_MSEC:
1c3d002b 568 case SR_CONF_LIMIT_SAMPLES:
8aafc5e6 569 return sr_sw_limits_config_set(&devc->limits, key, data);
7a0b98b5 570 case SR_CONF_CHANNEL_CONFIG:
1c3d002b
MH
571 sval = g_variant_get_string(data, NULL);
572 found = FALSE;
dcd438ee 573 for (idx = 0; idx < (int)ARRAY_SIZE(channel_modes); idx++) {
1c3d002b
MH
574 if (!strcmp(sval, channel_modes[idx])) {
575 found = TRUE;
576 if (devc->tracking_mode == idx)
577 break; /* Nothing to do! */
578 devc->tracking_mode = idx;
579 if (devc->model->modelid >= LPS_304) /* No use to set anything in the smaller models. */
580 return lps_cmd_ok(sdi->conn, "TRACK%1d", devc->tracking_mode);
581 }
582 if (devc->model->modelid <= LPS_303) /* Only first setting possible for smaller models. */
583 break;
584 }
dcd438ee 585 if (!found)
1c3d002b 586 return SR_ERR_ARG;
1c3d002b
MH
587 break;
588 default:
589 return SR_ERR_NA;
590 }
591 } else {
1c3d002b
MH
592 /* We only ever have one channel per channel group in this driver. */
593 ch = cg->channels->data;
594 ch_idx = ch->index;
595
596 switch (key) {
7a0b98b5 597 case SR_CONF_VOLTAGE_TARGET:
1c3d002b
MH
598 dval = g_variant_get_double(data);
599 if (dval < 0 || dval > devc->model->channels[ch_idx].voltage[1])
600 return SR_ERR_ARG;
601 if (ch_idx == 2) {
602 if (devc->model->modelid < LPS_304)
603 return SR_ERR_ARG;
604
605 if (fabs(dval - 5.000) <= 0.001)
606 dval = 5.0;
607 else if ((devc->model->modelid >= LPS_305) && (fabs(dval - 3.300) <= 0.001))
608 dval = 3.3;
609 else return SR_ERR_ARG;
610 }
611
612 devc->channel_status[ch_idx].output_voltage_max = dval;
613 if (ch_idx == 2)
614 return lps_cmd_ok(sdi->conn, "VDD%1.0f", trunc(dval));
615 else
616 return lps_cmd_ok(sdi->conn, "VSET%d %05.3f", ch_idx+1, dval);
617 break;
7a0b98b5 618 case SR_CONF_CURRENT_LIMIT:
1c3d002b
MH
619 dval = g_variant_get_double(data);
620 if (dval < 0 || dval > devc->model->channels[ch_idx].current[1])
621 return SR_ERR_ARG;
622 if (ch_idx == 2) /* No current setting for CH3. */
623 return SR_ERR_NA;
624 devc->channel_status[ch_idx].output_current_max = dval;
625 return lps_cmd_ok(sdi->conn, "ISET%d %05.4f", ch_idx+1, dval);
626 break;
7a0b98b5 627 case SR_CONF_ENABLED:
1c3d002b
MH
628 bval = g_variant_get_boolean(data);
629 if (bval == devc->channel_status[ch_idx].output_enabled) /* Nothing to do. */
630 break;
631 devc->channel_status[ch_idx].output_enabled = bval;
632 if (ch_idx != 2) { /* Channels 1,2 can be set only together. */
633 devc->channel_status[ch_idx^1].output_enabled = bval;
634 return lps_cmd_ok(sdi->conn, "OUT%1d", (int)bval);
635 } else { /* Channel 3: No command to disable output, set voltage to 0 instead. */
636 if (bval)
637 return lps_cmd_ok(sdi->conn, "VDD%1.0f", devc->channel_status[ch_idx].output_voltage_max);
638 else
639 return lps_cmd_ok(sdi->conn, "VDD0");
640 }
641 break;
642 default:
643 return SR_ERR_NA;
644 }
41b7bd01
MH
645 }
646
1c3d002b 647 return SR_OK;
41b7bd01
MH
648}
649
584560f1 650static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
41b7bd01
MH
651 const struct sr_channel_group *cg)
652{
1c3d002b
MH
653 struct dev_context *devc;
654 struct sr_channel *ch;
655 int ch_idx, i;
656 GVariant *gvar;
657 GVariantBuilder gvb;
41b7bd01 658
e66d1892 659 devc = (sdi) ? sdi->priv : NULL;
1c3d002b 660
0c5f2abc 661 if (!cg) {
1c3d002b 662 switch (key) {
e66d1892 663 case SR_CONF_SCAN_OPTIONS:
1c3d002b 664 case SR_CONF_DEVICE_OPTIONS:
e66d1892 665 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
7a0b98b5 666 case SR_CONF_CHANNEL_CONFIG:
1c3d002b
MH
667 if (devc->model->modelid <= LPS_303) {
668 /* The 1-channel models. */
669 *data = g_variant_new_strv(channel_modes, 1);
670 } else {
671 /* The other models support all modes. */
672 *data = g_variant_new_strv(channel_modes, ARRAY_SIZE(channel_modes));
673 }
674 return SR_OK;
1c3d002b
MH
675 default:
676 return SR_ERR_NA;
677 }
678 }
679
ca314e06 680 /* We only ever have one channel per channel group in this driver. */
1c3d002b
MH
681 ch = cg->channels->data;
682 ch_idx = ch->index;
ca314e06 683
1c3d002b
MH
684 switch (key) {
685 case SR_CONF_DEVICE_OPTIONS:
686 if ((ch_idx == 0) || (ch_idx == 1)) /* CH1, CH2 */
584560f1 687 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
6b82c3e5 688 devopts_cg_ch12, ARRAY_SIZE(devopts_cg_ch12), sizeof(uint32_t));
1c3d002b 689 else /* Must be CH3 */
584560f1 690 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
6b82c3e5 691 devopts_cg_ch3, ARRAY_SIZE(devopts_cg_ch3), sizeof(uint32_t));
1c3d002b 692 break;
7a0b98b5 693 case SR_CONF_VOLTAGE_TARGET:
1c3d002b
MH
694 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
695 /* Min, max, step. */
696 for (i = 0; i < 3; i++) {
697 gvar = g_variant_new_double(devc->model->channels[ch_idx].voltage[i]);
698 g_variant_builder_add_value(&gvb, gvar);
699 }
700 *data = g_variant_builder_end(&gvb);
701 break;
7a0b98b5 702 case SR_CONF_CURRENT_LIMIT:
1c3d002b
MH
703 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
704 /* Min, max, step. */
705 for (i = 0; i < 3; i++) {
706 gvar = g_variant_new_double(devc->model->channels[ch_idx].current[i]);
707 g_variant_builder_add_value(&gvb, gvar);
708 }
709 *data = g_variant_builder_end(&gvb);
710 break;
41b7bd01
MH
711 default:
712 return SR_ERR_NA;
713 }
714
1c3d002b 715 return SR_OK;
41b7bd01
MH
716}
717
695dc859 718static int dev_acquisition_start(const struct sr_dev_inst *sdi)
41b7bd01 719{
1c3d002b
MH
720 struct dev_context *devc;
721 struct sr_serial_dev_inst *serial;
41b7bd01 722
1c3d002b
MH
723 devc = sdi->priv;
724
725 devc->acq_running = TRUE;
726
727 serial = sdi->conn;
102f1239
BV
728 serial_source_add(sdi->session, serial, G_IO_IN, 50,
729 motech_lps_30x_receive_data, (void *)sdi);
bee2b016 730 std_session_send_df_header(sdi);
1c3d002b 731
8aafc5e6 732 sr_sw_limits_acquisition_start(&devc->limits);
1c3d002b
MH
733
734 devc->acq_req = AQ_NONE;
735 /* Do not start polling device here, the read function will do it in 50 ms. */
41b7bd01
MH
736
737 return SR_OK;
738}
739
dd5c48a6 740static struct sr_dev_driver motech_lps_301_driver_info = {
41b7bd01
MH
741 .name = "motech-lps-301",
742 .longname = "Motech LPS-301",
743 .api_version = 1,
c2fdcc25 744 .init = std_init,
700d6b64 745 .cleanup = std_cleanup,
1c3d002b 746 .scan = scan_lps301,
c01bf34c 747 .dev_list = std_dev_list,
53279f13 748 .dev_clear = dev_clear,
41b7bd01
MH
749 .config_get = config_get,
750 .config_set = config_set,
751 .config_list = config_list,
1c3d002b
MH
752 .dev_open = std_serial_dev_open,
753 .dev_close = std_serial_dev_close,
41b7bd01 754 .dev_acquisition_start = dev_acquisition_start,
4b1a9d5d 755 .dev_acquisition_stop = std_serial_dev_acquisition_stop,
41812aca 756 .context = NULL,
41b7bd01 757};
dd5c48a6 758SR_REGISTER_DEV_DRIVER(motech_lps_301_driver_info);