2 * This file is part of the libsigrok project.
4 * Copyright (C) 2019-2020 Gerhard Sittig <gerhard.sittig@gmx.net>
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 static const uint32_t scanopts[] = {
28 static const uint32_t drvopts[] = {
30 SR_CONF_THERMOMETER, /* Supports two temperature probes and diffs. */
33 static const uint32_t devopts[] = {
34 SR_CONF_CONN | SR_CONF_GET,
36 SR_CONF_LIMIT_FRAMES | SR_CONF_GET | SR_CONF_SET,
37 SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
38 SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
39 SR_CONF_DATA_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
40 SR_CONF_DATALOG | SR_CONF_GET | SR_CONF_SET,
41 /* TODO SR_CONF_DATALOG is bool only, how to setup interval/duration? */
42 SR_CONF_MEASURED_QUANTITY | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
43 SR_CONF_RANGE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
46 static const char *channel_names[] = {
47 [UT181A_CH_MAIN] = "P1",
48 [UT181A_CH_AUX1] = "P2",
49 [UT181A_CH_AUX2] = "P3",
50 [UT181A_CH_AUX3] = "P4",
51 [UT181A_CH_BAR] = "bar",
52 #if UT181A_WITH_TIMESTAMP
53 [UT181A_CH_TIME] = "TS",
58 * (Re-)retrieve the list of recordings and their names. These can change
59 * without the driver's being aware, the set is under user control.
61 * TODO Need to re-allocate the list of recording names when a larger
62 * recordings count is seen than previously allocated? This implementation
63 * assumes a known maximum number of recordings, the manual is vague on
66 static int ut181a_update_recordings(const struct sr_dev_inst *sdi)
68 struct dev_context *devc;
69 struct sr_serial_dev_inst *serial;
70 size_t rec_count, rec_idx;
78 ret = ut181a_send_cmd_get_recs_count(serial);
81 ret = ut181a_configure_waitfor(devc, FALSE, 0, 0,
82 FALSE, TRUE, FALSE, FALSE);
85 ret = ut181a_waitfor_response(sdi, 100);
89 rec_count = devc->wait_state.data_value;
90 if (rec_count > ARRAY_SIZE(devc->record_names))
91 rec_count = ARRAY_SIZE(devc->record_names);
92 for (rec_idx = 0; rec_idx < rec_count; rec_idx++) {
93 devc->info.rec_info.rec_idx = rec_idx;
94 ret = ut181a_send_cmd_get_rec_info(serial, rec_idx);
97 ret = ut181a_configure_waitfor(devc,
98 FALSE, CMD_CODE_GET_REC_INFO, 0,
99 FALSE, FALSE, FALSE, FALSE);
102 ret = ut181a_waitfor_response(sdi, 100);
106 devc->record_count = rec_count;
107 devc->data_source_count = DATA_SOURCE_REC_FIRST + devc->record_count;
113 * Retrieve the device's current state. Run monitor mode for some time
114 * until the 'mode' (meter's current function) became available. There
115 * is no other way of querying the meter's current state.
117 static int ut181a_query_initial_state(struct sr_dev_inst *sdi, int timeout_ms)
119 struct dev_context *devc;
120 struct sr_serial_dev_inst *serial;
129 devc->info.meas_head.mode = 0;
130 ret = ut181a_send_cmd_monitor(serial, TRUE);
133 ret = ut181a_configure_waitfor(devc, FALSE, 0, 0,
134 TRUE, FALSE, FALSE, FALSE);
137 deadline = g_get_monotonic_time();
138 deadline += timeout_ms * 1000;
140 ret = ut181a_waitfor_response(sdi, 100);
143 if (devc->info.meas_head.mode)
145 if (g_get_monotonic_time() >= deadline)
148 (void)ut181a_send_cmd_monitor(serial, FALSE);
149 ret = ut181a_configure_waitfor(devc, TRUE, 0, 0,
150 FALSE, FALSE, FALSE, FALSE);
153 (void)ut181a_waitfor_response(sdi, 100);
158 static GSList *scan(struct sr_dev_driver *di, GSList *options)
160 const char *conn, *serialcomm;
161 struct sr_config *src;
163 struct sr_serial_dev_inst *serial;
166 struct sr_dev_inst *sdi;
167 struct dev_context *devc;
171 * Implementor's note:
172 * Do _not_ add a default conn value here. Always expect users to
173 * specify the connection. Never match in the absence of a user spec.
175 * Motivation: There is no way to identify the DMM itself. Neither
176 * are the cable nor its chip unique to the device. They are not even
177 * specific to the series or the vendor. The DMM ships with a generic
178 * CP2110 USB-to-UART bridge. Attempts to auto probe will disturb
179 * other types of devices which may be attached to the probed conn.
181 * On the other hand it's perfectly fine to communicate to the
182 * device and assume that the device model will accept the requests,
183 * once the user specified the connection (and the driver), and thus
184 * instructed this driver to start such activity.
187 serialcomm = "9600/8n1";
188 for (l = options; l; l = l->next) {
192 conn = g_variant_get_string(src->data, NULL);
194 case SR_CONF_SERIALCOMM:
195 serialcomm = g_variant_get_string(src->data, NULL);
203 serial = sr_serial_dev_inst_new(conn, serialcomm);
204 ret = serial_open(serial, SERIAL_RDWR);
205 snprintf(conn_id, sizeof(conn_id), "%s", serial->port);
206 serial_flush(serial);
208 * We cannot identify the device at this point in time.
209 * Successful open shall suffice for now. More activity
210 * will communicate to the device later, after the driver
211 * instance got created. See below for details.
214 serial_close(serial);
215 sr_serial_dev_inst_free(serial);
219 sdi = g_malloc0(sizeof(*sdi));
220 sdi->status = SR_ST_INACTIVE;
221 sdi->vendor = g_strdup("UNI-T");
222 sdi->model = g_strdup("UT181A");
223 sdi->inst_type = SR_INST_SERIAL;
225 sdi->connection_id = g_strdup(conn_id);
226 devc = g_malloc0(sizeof(*devc));
228 sr_sw_limits_init(&devc->limits);
229 for (idx = 0; idx < ARRAY_SIZE(channel_names); idx++) {
230 sr_channel_new(sdi, idx, SR_CHANNEL_ANALOG, TRUE,
235 * Run monitor mode for a while to determine the current state
236 * of the device (which cannot get queried by other means). This
237 * also deals with devices which happen to already be in monitor
238 * mode when we connect to them. As a byproduct this query drains
239 * potentially pending RX data, before getting recording details.
241 devc->disable_feed = 1;
242 ret = ut181a_query_initial_state(sdi, 2000);
244 serial_close(serial);
245 sr_serial_dev_inst_free(serial);
250 * Number of recordings and their names are dynamic and under
251 * the user's control. Prepare for a maximum number of string
252 * labels, and fetch (and re-fetch) their names and current
255 devc->data_source_names[DATA_SOURCE_LIVE] = "Live";
256 devc->data_source_names[DATA_SOURCE_SAVE] = "Save";
257 for (idx = 0; idx < MAX_REC_COUNT; idx++) {
258 ds_idx = DATA_SOURCE_REC_FIRST + idx;
259 devc->data_source_names[ds_idx] = &devc->record_names[idx][0];
261 devc->data_source_count = DATA_SOURCE_REC_FIRST;
262 ret = ut181a_update_recordings(sdi);
263 devc->data_source_count = DATA_SOURCE_REC_FIRST + devc->record_count;
265 serial_close(serial);
266 sr_serial_dev_inst_free(serial);
270 devc->disable_feed = 0;
271 serial_close(serial);
273 devices = g_slist_append(devices, sdi);
275 return std_scan_complete(di, devices);
278 static int config_get(uint32_t key, GVariant **data,
279 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
281 struct dev_context *devc;
282 const struct mqopt_item *mqitem;
291 *data = g_variant_new_string(sdi->connection_id);
293 case SR_CONF_LIMIT_FRAMES:
294 case SR_CONF_LIMIT_SAMPLES:
295 case SR_CONF_LIMIT_MSEC:
298 return sr_sw_limits_config_get(&devc->limits, key, data);
299 case SR_CONF_DATA_SOURCE:
302 *data = g_variant_new_string(devc->data_source_names[devc->data_source]);
304 case SR_CONF_DATALOG:
307 *data = g_variant_new_boolean(devc->is_recording ? TRUE : FALSE);
309 case SR_CONF_MEASURED_QUANTITY:
312 mqitem = ut181a_get_mqitem_from_mode(devc->info.meas_head.mode);
315 arr[0] = g_variant_new_uint32(mqitem->mq);
316 arr[1] = g_variant_new_uint64(mqitem->mqflags);
317 *data = g_variant_new_tuple(arr, ARRAY_SIZE(arr));
322 range = ut181a_get_range_from_packet_bytes(devc);
323 if (!range || !*range)
325 *data = g_variant_new_string(range);
334 static int config_set(uint32_t key, GVariant *data,
335 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
337 struct dev_context *devc;
340 GVariant *tuple_child;
342 enum sr_mqflag mqflags;
352 case SR_CONF_LIMIT_FRAMES:
353 case SR_CONF_LIMIT_SAMPLES:
354 case SR_CONF_LIMIT_MSEC:
357 return sr_sw_limits_config_set(&devc->limits, key, data);
358 case SR_CONF_DATA_SOURCE:
361 /* Prefer data source names for the lookup. */
362 idx = std_str_idx(data, devc->data_source_names, devc->data_source_count);
364 devc->data_source = idx;
368 * Support record number (1-based) as a fallback. The DMM
369 * "supports" ambiguous recording names (keeps offering a
370 * previously stored name for each new recording, neither
371 * automatically increments nor suggests timestamps).
373 if (sr_atoi(g_variant_get_string(data, NULL), &ret) != SR_OK)
378 if (rec_no > devc->record_count)
380 devc->data_source = DATA_SOURCE_REC_FIRST + rec_no - 1;
382 case SR_CONF_DATALOG:
385 on = g_variant_get_boolean(data);
386 sr_err("DIAG: record start/stop %d, currently ENOIMPL", on);
389 * TODO Send command 0x0a (start) or 0x0b (stop). Though
390 * start needs a name (ymd timestamp?) and interval and
391 * duration (arbitrary choice? 1s for 1d?). Or shall this
392 * SET request control "save" items instead? Take one
393 * sample each for every 'datalog=on' request? Combine
394 * limit_samples and limit_msec with datalog to configure
395 * a recording's parameters?
398 case SR_CONF_MEASURED_QUANTITY:
401 tuple_child = g_variant_get_child_value(data, 0);
402 mq = g_variant_get_uint32(tuple_child);
403 g_variant_unref(tuple_child);
404 tuple_child = g_variant_get_child_value(data, 1);
405 mqflags = g_variant_get_uint64(tuple_child);
406 g_variant_unref(tuple_child);
407 mode = ut181a_get_mode_from_mq_flags(mq, mqflags);
410 ret = ut181a_send_cmd_setmode(sdi->conn, mode);
413 ret = ut181a_waitfor_response(sdi->conn, 100);
416 if (devc->info.rsp_head.rsp_type != RSP_TYPE_REPLY_CODE)
418 if (!devc->info.reply_code.ok)
422 range = g_variant_get_string(data, NULL);
423 return ut181a_set_range_from_text(sdi, range);
431 static int config_list(uint32_t key, GVariant **data,
432 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
434 struct dev_context *devc;
437 devc = sdi ? sdi->priv : NULL;
439 case SR_CONF_SCAN_OPTIONS:
440 case SR_CONF_DEVICE_OPTIONS:
441 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
442 case SR_CONF_DATA_SOURCE:
445 ret = ut181a_update_recordings(sdi);
448 *data = g_variant_new_strv(devc->data_source_names, devc->data_source_count);
450 case SR_CONF_MEASURED_QUANTITY:
451 *data = ut181a_get_mq_flags_list();
454 *data = ut181a_get_ranges_list();
463 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
465 struct dev_context *devc;
466 struct sr_serial_dev_inst *serial;
472 serial_flush(serial);
475 * Send an acquisition start command which depends on the
476 * currently selected data source. Enter monitor mode for
477 * Live readings, get saved or recorded data otherwise. The
478 * latter require queries for sample counts, then run chunked
479 * download sequences (single item for Save, set of samples
482 if (devc->data_source == DATA_SOURCE_LIVE) {
483 ret = ut181a_send_cmd_monitor(serial, TRUE);
484 } else if (devc->data_source == DATA_SOURCE_SAVE) {
486 * There is only one sequence of saved measurements in
487 * the device, but its length is yet unknown. Determine
488 * the number of saved items, and initiate the reception
489 * of the first value. Completion of data reception will
490 * drive subsequent progress.
492 ret = ut181a_send_cmd_get_save_count(serial);
495 ret = ut181a_configure_waitfor(devc, FALSE, 0, 0,
496 FALSE, FALSE, TRUE, FALSE);
499 ret = ut181a_waitfor_response(sdi, 200);
502 devc->info.save_info.save_count = devc->wait_state.data_value;
503 devc->info.save_info.save_idx = 0;
504 ret = ut181a_send_cmd_get_saved_value(serial, 0);
505 } else if (devc->data_source >= DATA_SOURCE_REC_FIRST) {
507 * When we get here, the data source got selected, which
508 * includes an update of the device's list of recordings.
509 * So the index should be good, just the number of samples
510 * in that recording is yet unknown. Get the sample count
511 * and initiate the reception of the first chunk, completed
512 * reception of a chunk advances through the sequence.
514 rec_idx = devc->data_source - DATA_SOURCE_REC_FIRST;
515 if (rec_idx >= devc->record_count)
517 devc->info.rec_info.rec_count = devc->record_count;
518 devc->info.rec_info.rec_idx = rec_idx;
519 devc->info.rec_info.auto_next = 0;
520 devc->info.rec_info.auto_feed = 1;
521 ret = ut181a_send_cmd_get_rec_info(serial, rec_idx);
524 ret = ut181a_configure_waitfor(devc,
525 FALSE, CMD_CODE_GET_REC_INFO, 0,
526 FALSE, FALSE, FALSE, FALSE);
529 ret = ut181a_waitfor_response(sdi, 200);
532 devc->info.rec_data.samples_total = devc->wait_state.data_value;
533 devc->info.rec_data.samples_curr = 0;
534 ret = ut181a_send_cmd_get_rec_samples(serial, rec_idx, 0);
539 sr_sw_limits_acquisition_start(&devc->limits);
540 devc->recv_count = 0;
541 std_session_send_df_header(sdi);
543 serial_source_add(sdi->session, serial, G_IO_IN, 10,
544 ut181a_handle_events, (void *)sdi);
549 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
552 sdi->status = SR_ST_STOPPING;
553 /* Initiate stop here. Activity happens in ut181a_handle_events(). */
558 static struct sr_dev_driver uni_t_ut181a_driver_info = {
559 .name = "uni-t-ut181a",
560 .longname = "UNI-T UT181A",
563 .cleanup = std_cleanup,
565 .dev_list = std_dev_list,
566 .dev_clear = std_dev_clear,
567 .config_get = config_get,
568 .config_set = config_set,
569 .config_list = config_list,
570 .dev_open = std_serial_dev_open,
571 .dev_close = std_serial_dev_close,
572 .dev_acquisition_start = dev_acquisition_start,
573 .dev_acquisition_stop = dev_acquisition_stop,
576 SR_REGISTER_DEV_DRIVER(uni_t_ut181a_driver_info);