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);
207 * We cannot identify the device at this point in time.
208 * Successful open shall suffice for now. More activity
209 * will communicate to the device later, after the driver
210 * instance got created. See below for details.
213 serial_close(serial);
214 sr_serial_dev_inst_free(serial);
218 sdi = g_malloc0(sizeof(*sdi));
219 sdi->status = SR_ST_INACTIVE;
220 sdi->vendor = g_strdup("UNI-T");
221 sdi->model = g_strdup("UT181A");
222 sdi->inst_type = SR_INST_SERIAL;
224 sdi->connection_id = g_strdup(conn_id);
225 devc = g_malloc0(sizeof(*devc));
227 sr_sw_limits_init(&devc->limits);
228 for (idx = 0; idx < ARRAY_SIZE(channel_names); idx++) {
229 sr_channel_new(sdi, idx, SR_CHANNEL_ANALOG, TRUE,
234 * Run monitor mode for a while to determine the current state
235 * of the device (which cannot get queried by other means). This
236 * also deals with devices which happen to already be in monitor
237 * mode when we connect to them. As a byproduct this query drains
238 * potentially pending RX data, before getting recording details.
240 devc->disable_feed = 1;
241 ret = ut181a_query_initial_state(sdi, 2000);
243 serial_close(serial);
244 sr_serial_dev_inst_free(serial);
249 * Number of recordings and their names are dynamic and under
250 * the user's control. Prepare for a maximum number of string
251 * labels, and fetch (and re-fetch) their names and current
254 devc->data_source_names[DATA_SOURCE_LIVE] = "Live";
255 devc->data_source_names[DATA_SOURCE_SAVE] = "Save";
256 for (idx = 0; idx < MAX_REC_COUNT; idx++) {
257 ds_idx = DATA_SOURCE_REC_FIRST + idx;
258 devc->data_source_names[ds_idx] = &devc->record_names[idx][0];
260 devc->data_source_count = DATA_SOURCE_REC_FIRST;
261 ret = ut181a_update_recordings(sdi);
262 devc->data_source_count = DATA_SOURCE_REC_FIRST + devc->record_count;
264 serial_close(serial);
265 sr_serial_dev_inst_free(serial);
269 devc->disable_feed = 0;
270 serial_close(serial);
272 devices = g_slist_append(devices, sdi);
274 return std_scan_complete(di, devices);
277 static int config_get(uint32_t key, GVariant **data,
278 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
280 struct dev_context *devc;
281 const struct mqopt_item *mqitem;
290 *data = g_variant_new_string(sdi->connection_id);
292 case SR_CONF_LIMIT_FRAMES:
293 case SR_CONF_LIMIT_SAMPLES:
294 case SR_CONF_LIMIT_MSEC:
297 return sr_sw_limits_config_get(&devc->limits, key, data);
298 case SR_CONF_DATA_SOURCE:
301 *data = g_variant_new_string(devc->data_source_names[devc->data_source]);
303 case SR_CONF_DATALOG:
306 *data = g_variant_new_boolean(devc->is_recording ? TRUE : FALSE);
308 case SR_CONF_MEASURED_QUANTITY:
311 mqitem = ut181a_get_mqitem_from_mode(devc->info.meas_head.mode);
314 arr[0] = g_variant_new_uint32(mqitem->mq);
315 arr[1] = g_variant_new_uint64(mqitem->mqflags);
316 *data = g_variant_new_tuple(arr, ARRAY_SIZE(arr));
321 range = ut181a_get_range_from_packet_bytes(devc);
322 if (!range || !*range)
324 *data = g_variant_new_string(range);
333 static int config_set(uint32_t key, GVariant *data,
334 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
336 struct dev_context *devc;
339 GVariant *tuple_child;
341 enum sr_mqflag mqflags;
351 case SR_CONF_LIMIT_FRAMES:
352 case SR_CONF_LIMIT_SAMPLES:
353 case SR_CONF_LIMIT_MSEC:
356 return sr_sw_limits_config_set(&devc->limits, key, data);
357 case SR_CONF_DATA_SOURCE:
360 /* Prefer data source names for the lookup. */
361 idx = std_str_idx(data, devc->data_source_names, devc->data_source_count);
363 devc->data_source = idx;
367 * Support record number (1-based) as a fallback. The DMM
368 * "supports" ambiguous recording names (keeps offering a
369 * previously stored name for each new recording, neither
370 * automatically increments nor suggests timestamps).
372 if (sr_atoi(g_variant_get_string(data, NULL), &ret) != SR_OK)
377 if (rec_no > devc->record_count)
379 devc->data_source = DATA_SOURCE_REC_FIRST + rec_no - 1;
381 case SR_CONF_DATALOG:
384 on = g_variant_get_boolean(data);
385 sr_err("DIAG: record start/stop %d, currently ENOIMPL", on);
388 * TODO Send command 0x0a (start) or 0x0b (stop). Though
389 * start needs a name (ymd timestamp?) and interval and
390 * duration (arbitrary choice? 1s for 1d?). Or shall this
391 * SET request control "save" items instead? Take one
392 * sample each for every 'datalog=on' request? Combine
393 * limit_samples and limit_msec with datalog to configure
394 * a recording's parameters?
397 case SR_CONF_MEASURED_QUANTITY:
400 tuple_child = g_variant_get_child_value(data, 0);
401 mq = g_variant_get_uint32(tuple_child);
402 g_variant_unref(tuple_child);
403 tuple_child = g_variant_get_child_value(data, 1);
404 mqflags = g_variant_get_uint64(tuple_child);
405 g_variant_unref(tuple_child);
406 mode = ut181a_get_mode_from_mq_flags(mq, mqflags);
409 ret = ut181a_send_cmd_setmode(sdi->conn, mode);
412 ret = ut181a_waitfor_response(sdi->conn, 100);
415 if (devc->info.rsp_head.rsp_type != RSP_TYPE_REPLY_CODE)
417 if (!devc->info.reply_code.ok)
421 range = g_variant_get_string(data, NULL);
422 return ut181a_set_range_from_text(sdi, range);
430 static int config_list(uint32_t key, GVariant **data,
431 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
433 struct dev_context *devc;
436 devc = sdi ? sdi->priv : NULL;
438 case SR_CONF_SCAN_OPTIONS:
439 case SR_CONF_DEVICE_OPTIONS:
440 return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts);
441 case SR_CONF_DATA_SOURCE:
444 ret = ut181a_update_recordings(sdi);
447 *data = g_variant_new_strv(devc->data_source_names, devc->data_source_count);
449 case SR_CONF_MEASURED_QUANTITY:
450 *data = ut181a_get_mq_flags_list();
453 *data = ut181a_get_ranges_list();
462 static int dev_acquisition_start(const struct sr_dev_inst *sdi)
464 struct dev_context *devc;
465 struct sr_serial_dev_inst *serial;
471 serial_flush(serial);
474 * Send an acquisition start command which depends on the
475 * currently selected data source. Enter monitor mode for
476 * Live readings, get saved or recorded data otherwise. The
477 * latter require queries for sample counts, then run chunked
478 * download sequences (single item for Save, set of samples
481 if (devc->data_source == DATA_SOURCE_LIVE) {
482 ret = ut181a_send_cmd_monitor(serial, TRUE);
483 } else if (devc->data_source == DATA_SOURCE_SAVE) {
485 * There is only one sequence of saved measurements in
486 * the device, but its length is yet unknown. Determine
487 * the number of saved items, and initiate the reception
488 * of the first value. Completion of data reception will
489 * drive subsequent progress.
491 ret = ut181a_send_cmd_get_save_count(serial);
494 ret = ut181a_configure_waitfor(devc, FALSE, 0, 0,
495 FALSE, FALSE, TRUE, FALSE);
498 ret = ut181a_waitfor_response(sdi, 200);
501 devc->info.save_info.save_count = devc->wait_state.data_value;
502 devc->info.save_info.save_idx = 0;
503 ret = ut181a_send_cmd_get_saved_value(serial, 0);
504 } else if (devc->data_source >= DATA_SOURCE_REC_FIRST) {
506 * When we get here, the data source got selected, which
507 * includes an update of the device's list of recordings.
508 * So the index should be good, just the number of samples
509 * in that recording is yet unknown. Get the sample count
510 * and initiate the reception of the first chunk, completed
511 * reception of a chunk advances through the sequence.
513 rec_idx = devc->data_source - DATA_SOURCE_REC_FIRST;
514 if (rec_idx >= devc->record_count)
516 devc->info.rec_info.rec_count = devc->record_count;
517 devc->info.rec_info.rec_idx = rec_idx;
518 devc->info.rec_info.auto_next = 0;
519 devc->info.rec_info.auto_feed = 1;
520 ret = ut181a_send_cmd_get_rec_info(serial, rec_idx);
523 ret = ut181a_configure_waitfor(devc,
524 FALSE, CMD_CODE_GET_REC_INFO, 0,
525 FALSE, FALSE, FALSE, FALSE);
528 ret = ut181a_waitfor_response(sdi, 200);
531 devc->info.rec_data.samples_total = devc->wait_state.data_value;
532 devc->info.rec_data.samples_curr = 0;
533 ret = ut181a_send_cmd_get_rec_samples(serial, rec_idx, 0);
535 sr_err("Unhandled data source %d, programming error?",
536 (int)devc->data_source);
542 sr_sw_limits_acquisition_start(&devc->limits);
543 devc->recv_count = 0;
544 std_session_send_df_header(sdi);
546 serial_source_add(sdi->session, serial, G_IO_IN, 10,
547 ut181a_handle_events, (void *)sdi);
552 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
555 sdi->status = SR_ST_STOPPING;
556 /* Initiate stop here. Activity happens in ut181a_handle_events(). */
561 static struct sr_dev_driver uni_t_ut181a_driver_info = {
562 .name = "uni-t-ut181a",
563 .longname = "UNI-T UT181A",
566 .cleanup = std_cleanup,
568 .dev_list = std_dev_list,
569 .dev_clear = std_dev_clear,
570 .config_get = config_get,
571 .config_set = config_set,
572 .config_list = config_list,
573 .dev_open = std_serial_dev_open,
574 .dev_close = std_serial_dev_close,
575 .dev_acquisition_start = dev_acquisition_start,
576 .dev_acquisition_stop = dev_acquisition_stop,
579 SR_REGISTER_DEV_DRIVER(uni_t_ut181a_driver_info);