]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2012 Bert Vermeulen <bert@biot.com> | |
5 | * | |
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. | |
10 | * | |
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. | |
15 | * | |
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/>. | |
18 | */ | |
19 | ||
20 | #include <config.h> | |
21 | #include <glib.h> | |
22 | #include <sys/types.h> | |
23 | #include <sys/stat.h> | |
24 | #include <fcntl.h> | |
25 | #include <string.h> | |
26 | #include <libsigrok/libsigrok.h> | |
27 | #include "libsigrok-internal.h" | |
28 | #include "protocol.h" | |
29 | ||
30 | static const uint32_t scanopts[] = { | |
31 | SR_CONF_CONN, | |
32 | SR_CONF_SERIALCOMM, | |
33 | }; | |
34 | ||
35 | static const uint32_t drvopts[] = { | |
36 | SR_CONF_MULTIMETER, | |
37 | }; | |
38 | ||
39 | static const uint32_t devopts[] = { | |
40 | SR_CONF_CONTINUOUS, | |
41 | SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, | |
42 | SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET, | |
43 | SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
44 | SR_CONF_DATA_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
45 | }; | |
46 | ||
47 | static const uint64_t samplerates[] = { | |
48 | SR_HZ(1), | |
49 | SR_HZ(20), | |
50 | SR_HZ(1), | |
51 | }; | |
52 | ||
53 | static const char *data_sources[] = { | |
54 | "Live", | |
55 | "Log-Hand", | |
56 | "Log-Trig", | |
57 | "Log-Auto", | |
58 | "Log-Export", | |
59 | }; | |
60 | ||
61 | extern const struct agdmm_job agdmm_jobs_live[]; | |
62 | extern const struct agdmm_job agdmm_jobs_log[]; | |
63 | extern const struct agdmm_recv agdmm_recvs_u123x[]; | |
64 | extern const struct agdmm_recv agdmm_recvs_u124x[]; | |
65 | extern const struct agdmm_recv agdmm_recvs_u124xc[]; | |
66 | extern const struct agdmm_recv agdmm_recvs_u125x[]; | |
67 | extern const struct agdmm_recv agdmm_recvs_u128x[]; | |
68 | ||
69 | /* This works on all the Agilent U12xxA series, although the | |
70 | * U127xA can apparently also run at 19200/8n1. */ | |
71 | #define SERIALCOMM "9600/8n1" | |
72 | ||
73 | static const struct agdmm_profile supported_agdmm[] = { | |
74 | { AGILENT_U1231, "U1231A", 1, agdmm_jobs_live, NULL, agdmm_recvs_u123x }, | |
75 | { AGILENT_U1232, "U1232A", 1, agdmm_jobs_live, NULL, agdmm_recvs_u123x }, | |
76 | { AGILENT_U1233, "U1233A", 1, agdmm_jobs_live, NULL, agdmm_recvs_u123x }, | |
77 | ||
78 | { AGILENT_U1241, "U1241A", 2, agdmm_jobs_live, NULL, agdmm_recvs_u124x }, | |
79 | { AGILENT_U1242, "U1242A", 2, agdmm_jobs_live, NULL, agdmm_recvs_u124x }, | |
80 | { AGILENT_U1241, "U1241B", 2, agdmm_jobs_live, NULL, agdmm_recvs_u124x }, | |
81 | { AGILENT_U1242, "U1242B", 2, agdmm_jobs_live, NULL, agdmm_recvs_u124x }, | |
82 | ||
83 | { KEYSIGHT_U1241C, "U1241C", 2, agdmm_jobs_live, agdmm_jobs_log, agdmm_recvs_u124xc }, | |
84 | { KEYSIGHT_U1242C, "U1242C", 2, agdmm_jobs_live, agdmm_jobs_log, agdmm_recvs_u124xc }, | |
85 | ||
86 | { AGILENT_U1251, "U1251A", 3, agdmm_jobs_live, NULL, agdmm_recvs_u125x }, | |
87 | { AGILENT_U1252, "U1252A", 3, agdmm_jobs_live, NULL, agdmm_recvs_u125x }, | |
88 | { AGILENT_U1253, "U1253A", 3, agdmm_jobs_live, NULL, agdmm_recvs_u125x }, | |
89 | { AGILENT_U1251, "U1251B", 3, agdmm_jobs_live, NULL, agdmm_recvs_u125x }, | |
90 | { AGILENT_U1252, "U1252B", 3, agdmm_jobs_live, NULL, agdmm_recvs_u125x }, | |
91 | { AGILENT_U1253, "U1253B", 3, agdmm_jobs_live, NULL, agdmm_recvs_u125x }, | |
92 | ||
93 | { KEYSIGHT_U1281, "U1281A", 3, agdmm_jobs_live, agdmm_jobs_log, agdmm_recvs_u128x }, | |
94 | { KEYSIGHT_U1282, "U1282A", 3, agdmm_jobs_live, agdmm_jobs_log, agdmm_recvs_u128x }, | |
95 | ALL_ZERO | |
96 | }; | |
97 | ||
98 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
99 | { | |
100 | struct sr_dev_inst *sdi; | |
101 | struct dev_context *devc; | |
102 | struct sr_config *src; | |
103 | struct sr_serial_dev_inst *serial; | |
104 | GSList *l, *devices; | |
105 | int len, i; | |
106 | const char *conn, *serialcomm; | |
107 | char *buf, **tokens; | |
108 | ||
109 | devices = NULL; | |
110 | conn = serialcomm = NULL; | |
111 | for (l = options; l; l = l->next) { | |
112 | src = l->data; | |
113 | switch (src->key) { | |
114 | case SR_CONF_CONN: | |
115 | conn = g_variant_get_string(src->data, NULL); | |
116 | break; | |
117 | case SR_CONF_SERIALCOMM: | |
118 | serialcomm = g_variant_get_string(src->data, NULL); | |
119 | break; | |
120 | } | |
121 | } | |
122 | if (!conn) | |
123 | return NULL; | |
124 | if (!serialcomm) | |
125 | serialcomm = SERIALCOMM; | |
126 | ||
127 | serial = sr_serial_dev_inst_new(conn, serialcomm); | |
128 | ||
129 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) | |
130 | return NULL; | |
131 | ||
132 | serial_flush(serial); | |
133 | if (serial_write_blocking(serial, "*IDN?\r\n", 7, SERIAL_WRITE_TIMEOUT_MS) < 7) { | |
134 | sr_err("Unable to send identification string."); | |
135 | return NULL; | |
136 | } | |
137 | ||
138 | len = 128; | |
139 | buf = g_malloc(len); | |
140 | serial_readline(serial, &buf, &len, 250); | |
141 | if (!len) | |
142 | return NULL; | |
143 | ||
144 | tokens = g_strsplit(buf, ",", 4); | |
145 | if ((!strcmp("Agilent Technologies", tokens[0]) || | |
146 | !strcmp("Keysight Technologies", tokens[0])) | |
147 | && tokens[1] && tokens[2] && tokens[3]) { | |
148 | for (i = 0; supported_agdmm[i].model; i++) { | |
149 | if (strcmp(supported_agdmm[i].modelname, tokens[1])) | |
150 | continue; | |
151 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
152 | sdi->status = SR_ST_INACTIVE; | |
153 | sdi->vendor = g_strdup(tokens[0][0] == 'A' ? "Agilent" : "Keysight"); | |
154 | sdi->model = g_strdup(tokens[1]); | |
155 | sdi->version = g_strdup(tokens[3]); | |
156 | devc = g_malloc0(sizeof(struct dev_context)); | |
157 | sr_sw_limits_init(&devc->limits); | |
158 | devc->profile = &supported_agdmm[i]; | |
159 | devc->data_source = DEFAULT_DATA_SOURCE; | |
160 | devc->cur_samplerate = 5; | |
161 | if (supported_agdmm[i].nb_channels > 1) { | |
162 | int temp_chan = supported_agdmm[i].nb_channels - 1; | |
163 | devc->cur_mq[temp_chan] = SR_MQ_TEMPERATURE; | |
164 | devc->cur_unit[temp_chan] = SR_UNIT_CELSIUS; | |
165 | devc->cur_digits[temp_chan] = 1; | |
166 | devc->cur_encoding[temp_chan] = 2; | |
167 | } | |
168 | sdi->inst_type = SR_INST_SERIAL; | |
169 | sdi->conn = serial; | |
170 | sdi->priv = devc; | |
171 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1"); | |
172 | if (supported_agdmm[i].nb_channels > 1) | |
173 | sr_channel_new(sdi, 1, SR_CHANNEL_ANALOG, TRUE, "P2"); | |
174 | if (supported_agdmm[i].nb_channels > 2) | |
175 | sr_channel_new(sdi, 2, SR_CHANNEL_ANALOG, TRUE, "P3"); | |
176 | devices = g_slist_append(devices, sdi); | |
177 | break; | |
178 | } | |
179 | } | |
180 | g_strfreev(tokens); | |
181 | g_free(buf); | |
182 | ||
183 | serial_close(serial); | |
184 | if (!devices) | |
185 | sr_serial_dev_inst_free(serial); | |
186 | ||
187 | return std_scan_complete(di, devices); | |
188 | } | |
189 | ||
190 | static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
191 | const struct sr_channel_group *cg) | |
192 | { | |
193 | struct dev_context *devc; | |
194 | int ret; | |
195 | ||
196 | (void)cg; | |
197 | ||
198 | devc = sdi->priv; | |
199 | ||
200 | ret = SR_OK; | |
201 | switch (key) { | |
202 | case SR_CONF_SAMPLERATE: | |
203 | *data = g_variant_new_uint64(devc->cur_samplerate); | |
204 | break; | |
205 | case SR_CONF_LIMIT_SAMPLES: | |
206 | case SR_CONF_LIMIT_MSEC: | |
207 | ret = sr_sw_limits_config_get(&devc->limits, key, data); | |
208 | break; | |
209 | case SR_CONF_DATA_SOURCE: | |
210 | *data = g_variant_new_string(data_sources[devc->data_source]); | |
211 | break; | |
212 | default: | |
213 | return SR_ERR_NA; | |
214 | } | |
215 | ||
216 | return ret; | |
217 | } | |
218 | ||
219 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, | |
220 | const struct sr_channel_group *cg) | |
221 | { | |
222 | struct dev_context *devc; | |
223 | uint64_t samplerate; | |
224 | const char *tmp_str; | |
225 | unsigned int i; | |
226 | int ret; | |
227 | ||
228 | (void)cg; | |
229 | ||
230 | devc = sdi->priv; | |
231 | ||
232 | ret = SR_OK; | |
233 | switch (key) { | |
234 | case SR_CONF_SAMPLERATE: | |
235 | samplerate = g_variant_get_uint64(data); | |
236 | if (samplerate < samplerates[0] || samplerate > samplerates[1]) | |
237 | ret = SR_ERR_ARG; | |
238 | else | |
239 | devc->cur_samplerate = g_variant_get_uint64(data); | |
240 | break; | |
241 | case SR_CONF_LIMIT_SAMPLES: | |
242 | case SR_CONF_LIMIT_MSEC: | |
243 | ret = sr_sw_limits_config_set(&devc->limits, key, data); | |
244 | break; | |
245 | case SR_CONF_DATA_SOURCE: { | |
246 | tmp_str = g_variant_get_string(data, NULL); | |
247 | for (i = 0; i < ARRAY_SIZE(data_sources); i++) | |
248 | if (!strcmp(tmp_str, data_sources[i])) { | |
249 | devc->data_source = i; | |
250 | break; | |
251 | } | |
252 | if (i == ARRAY_SIZE(data_sources)) | |
253 | return SR_ERR; | |
254 | break; | |
255 | } | |
256 | default: | |
257 | ret = SR_ERR_NA; | |
258 | } | |
259 | ||
260 | return ret; | |
261 | } | |
262 | ||
263 | static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
264 | const struct sr_channel_group *cg) | |
265 | { | |
266 | GVariant *gvar; | |
267 | GVariantBuilder gvb; | |
268 | ||
269 | if (key == SR_CONF_SCAN_OPTIONS) { | |
270 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
271 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); | |
272 | return SR_OK; | |
273 | } | |
274 | ||
275 | if (key == SR_CONF_DEVICE_OPTIONS && !sdi) { | |
276 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
277 | drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t)); | |
278 | return SR_OK; | |
279 | } | |
280 | ||
281 | if (!sdi || cg) | |
282 | return SR_ERR_ARG; | |
283 | ||
284 | switch (key) { | |
285 | case SR_CONF_DEVICE_OPTIONS: | |
286 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
287 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); | |
288 | break; | |
289 | case SR_CONF_SAMPLERATE: | |
290 | g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}")); | |
291 | gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), samplerates, | |
292 | ARRAY_SIZE(samplerates), sizeof(uint64_t)); | |
293 | g_variant_builder_add(&gvb, "{sv}", "samplerate-steps", gvar); | |
294 | *data = g_variant_builder_end(&gvb); | |
295 | break; | |
296 | case SR_CONF_DATA_SOURCE: | |
297 | *data = g_variant_new_strv(data_sources, ARRAY_SIZE(data_sources)); | |
298 | break; | |
299 | default: | |
300 | return SR_ERR_NA; | |
301 | } | |
302 | ||
303 | return SR_OK; | |
304 | } | |
305 | ||
306 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
307 | { | |
308 | struct dev_context *devc = sdi->priv; | |
309 | struct sr_serial_dev_inst *serial; | |
310 | ||
311 | devc->cur_channel = sr_next_enabled_channel(sdi, NULL); | |
312 | devc->cur_conf = sr_next_enabled_channel(sdi, NULL); | |
313 | devc->cur_sample = 1; | |
314 | devc->cur_mq[0] = -1; | |
315 | if (devc->profile->nb_channels > 2) | |
316 | devc->cur_mq[1] = -1; | |
317 | ||
318 | if (devc->data_source == DATA_SOURCE_LIVE) { | |
319 | devc->jobs = devc->profile->jobs_live; | |
320 | } else { | |
321 | devc->jobs = devc->profile->jobs_log; | |
322 | if (!devc->jobs) { | |
323 | sr_err("Log data source is not implemented for this model."); | |
324 | return SR_ERR_NA; | |
325 | } | |
326 | if (!((struct sr_channel *)sdi->channels->data)->enabled) { | |
327 | sr_err("Log data is only available for channel P1."); | |
328 | return SR_ERR_NA; | |
329 | } | |
330 | } | |
331 | ||
332 | sr_sw_limits_acquisition_start(&devc->limits); | |
333 | std_session_send_df_header(sdi); | |
334 | ||
335 | /* Poll every 10ms, or whenever some data comes in. */ | |
336 | serial = sdi->conn; | |
337 | serial_source_add(sdi->session, serial, G_IO_IN, 10, | |
338 | agdmm_receive_data, (void *)sdi); | |
339 | ||
340 | return SR_OK; | |
341 | } | |
342 | ||
343 | static struct sr_dev_driver agdmm_driver_info = { | |
344 | .name = "agilent-dmm", | |
345 | .longname = "Agilent U12xx series DMMs", | |
346 | .api_version = 1, | |
347 | .init = std_init, | |
348 | .cleanup = std_cleanup, | |
349 | .scan = scan, | |
350 | .dev_list = std_dev_list, | |
351 | .dev_clear = std_dev_clear, | |
352 | .config_get = config_get, | |
353 | .config_set = config_set, | |
354 | .config_list = config_list, | |
355 | .dev_open = std_serial_dev_open, | |
356 | .dev_close = std_serial_dev_close, | |
357 | .dev_acquisition_start = dev_acquisition_start, | |
358 | .dev_acquisition_stop = std_serial_dev_acquisition_stop, | |
359 | .context = NULL, | |
360 | }; | |
361 | SR_REGISTER_DEV_DRIVER(agdmm_driver_info); |