]>
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", "Log-Hand", "Log-Trig", "Log-Auto", "Log-Export", | |
55 | }; | |
56 | ||
57 | extern const struct agdmm_job agdmm_jobs_live[]; | |
58 | extern const struct agdmm_job agdmm_jobs_log[]; | |
59 | extern const struct agdmm_recv agdmm_recvs_u123x[]; | |
60 | extern const struct agdmm_recv agdmm_recvs_u124x[]; | |
61 | extern const struct agdmm_recv agdmm_recvs_u124xc[]; | |
62 | extern const struct agdmm_recv agdmm_recvs_u125x[]; | |
63 | extern const struct agdmm_recv agdmm_recvs_u127x[]; | |
64 | extern const struct agdmm_recv agdmm_recvs_u128x[]; | |
65 | ||
66 | /* This works on all the Agilent U12xxA series, although the | |
67 | * U127xA can apparently also run at 19200/8n1. */ | |
68 | #define SERIALCOMM "9600/8n1" | |
69 | ||
70 | static const struct agdmm_profile supported_agdmm[] = { | |
71 | { AGILENT_U1231, "U1231A", 1, agdmm_jobs_live, NULL, agdmm_recvs_u123x }, | |
72 | { AGILENT_U1232, "U1232A", 1, agdmm_jobs_live, NULL, agdmm_recvs_u123x }, | |
73 | { AGILENT_U1233, "U1233A", 1, agdmm_jobs_live, NULL, agdmm_recvs_u123x }, | |
74 | ||
75 | { AGILENT_U1241, "U1241A", 2, agdmm_jobs_live, NULL, agdmm_recvs_u124x }, | |
76 | { AGILENT_U1242, "U1242A", 2, agdmm_jobs_live, NULL, agdmm_recvs_u124x }, | |
77 | { AGILENT_U1241, "U1241B", 2, agdmm_jobs_live, NULL, agdmm_recvs_u124x }, | |
78 | { AGILENT_U1242, "U1242B", 2, agdmm_jobs_live, NULL, agdmm_recvs_u124x }, | |
79 | ||
80 | { KEYSIGHT_U1241C, "U1241C", 2, agdmm_jobs_live, agdmm_jobs_log, agdmm_recvs_u124xc }, | |
81 | { KEYSIGHT_U1242C, "U1242C", 2, agdmm_jobs_live, agdmm_jobs_log, agdmm_recvs_u124xc }, | |
82 | ||
83 | { AGILENT_U1251, "U1251A", 3, agdmm_jobs_live, NULL, agdmm_recvs_u125x }, | |
84 | { AGILENT_U1252, "U1252A", 3, agdmm_jobs_live, NULL, agdmm_recvs_u125x }, | |
85 | { AGILENT_U1253, "U1253A", 3, agdmm_jobs_live, NULL, agdmm_recvs_u125x }, | |
86 | { AGILENT_U1251, "U1251B", 3, agdmm_jobs_live, NULL, agdmm_recvs_u125x }, | |
87 | { AGILENT_U1252, "U1252B", 3, agdmm_jobs_live, NULL, agdmm_recvs_u125x }, | |
88 | { AGILENT_U1253, "U1253B", 3, agdmm_jobs_live, NULL, agdmm_recvs_u125x }, | |
89 | ||
90 | { AGILENT_U1271, "U1271A", 3, agdmm_jobs_live, NULL, agdmm_recvs_u127x }, | |
91 | { AGILENT_U1272, "U1272A", 3, agdmm_jobs_live, NULL, agdmm_recvs_u127x }, | |
92 | { AGILENT_U1273, "U1273A", 3, agdmm_jobs_live, NULL, agdmm_recvs_u127x }, | |
93 | { AGILENT_U1273AX, "U1273AX", 3, agdmm_jobs_live, NULL, agdmm_recvs_u127x }, | |
94 | ||
95 | { KEYSIGHT_U1281, "U1281A", 3, agdmm_jobs_live, agdmm_jobs_log, agdmm_recvs_u128x }, | |
96 | { KEYSIGHT_U1282, "U1282A", 3, agdmm_jobs_live, agdmm_jobs_log, agdmm_recvs_u128x }, | |
97 | ALL_ZERO | |
98 | }; | |
99 | ||
100 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
101 | { | |
102 | struct sr_dev_inst *sdi; | |
103 | struct dev_context *devc; | |
104 | struct sr_config *src; | |
105 | struct sr_serial_dev_inst *serial; | |
106 | GSList *l, *devices; | |
107 | int len, i; | |
108 | const char *conn, *serialcomm; | |
109 | char *buf, **tokens; | |
110 | ||
111 | devices = NULL; | |
112 | conn = serialcomm = NULL; | |
113 | for (l = options; l; l = l->next) { | |
114 | src = l->data; | |
115 | switch (src->key) { | |
116 | case SR_CONF_CONN: | |
117 | conn = g_variant_get_string(src->data, NULL); | |
118 | break; | |
119 | case SR_CONF_SERIALCOMM: | |
120 | serialcomm = g_variant_get_string(src->data, NULL); | |
121 | break; | |
122 | } | |
123 | } | |
124 | if (!conn) | |
125 | return NULL; | |
126 | if (!serialcomm) | |
127 | serialcomm = SERIALCOMM; | |
128 | ||
129 | serial = sr_serial_dev_inst_new(conn, serialcomm); | |
130 | ||
131 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) | |
132 | return NULL; | |
133 | ||
134 | serial_flush(serial); | |
135 | if (serial_write_blocking(serial, "*IDN?\r\n", 7, SERIAL_WRITE_TIMEOUT_MS) < 7) { | |
136 | sr_err("Unable to send identification string."); | |
137 | return NULL; | |
138 | } | |
139 | ||
140 | len = 128; | |
141 | buf = g_malloc(len); | |
142 | serial_readline(serial, &buf, &len, 250); | |
143 | if (!len) | |
144 | return NULL; | |
145 | ||
146 | tokens = g_strsplit(buf, ",", 4); | |
147 | if ((!strcmp("Agilent Technologies", tokens[0]) || | |
148 | !strcmp("Keysight Technologies", tokens[0])) | |
149 | && tokens[1] && tokens[2] && tokens[3]) { | |
150 | for (i = 0; supported_agdmm[i].model; i++) { | |
151 | if (strcmp(supported_agdmm[i].modelname, tokens[1])) | |
152 | continue; | |
153 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
154 | sdi->status = SR_ST_INACTIVE; | |
155 | sdi->vendor = g_strdup(tokens[0][0] == 'A' ? "Agilent" : "Keysight"); | |
156 | sdi->model = g_strdup(tokens[1]); | |
157 | sdi->version = g_strdup(tokens[3]); | |
158 | devc = g_malloc0(sizeof(struct dev_context)); | |
159 | sr_sw_limits_init(&devc->limits); | |
160 | devc->profile = &supported_agdmm[i]; | |
161 | devc->data_source = DEFAULT_DATA_SOURCE; | |
162 | devc->cur_samplerate = 5; | |
163 | if (supported_agdmm[i].nb_channels > 1) { | |
164 | int temp_chan = supported_agdmm[i].nb_channels - 1; | |
165 | devc->cur_mq[temp_chan] = SR_MQ_TEMPERATURE; | |
166 | devc->cur_unit[temp_chan] = SR_UNIT_CELSIUS; | |
167 | devc->cur_digits[temp_chan] = 1; | |
168 | devc->cur_encoding[temp_chan] = 2; | |
169 | } | |
170 | sdi->inst_type = SR_INST_SERIAL; | |
171 | sdi->conn = serial; | |
172 | sdi->priv = devc; | |
173 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1"); | |
174 | if (supported_agdmm[i].nb_channels > 1) | |
175 | sr_channel_new(sdi, 1, SR_CHANNEL_ANALOG, TRUE, "P2"); | |
176 | if (supported_agdmm[i].nb_channels > 2) | |
177 | sr_channel_new(sdi, 2, SR_CHANNEL_ANALOG, TRUE, "P3"); | |
178 | devices = g_slist_append(devices, sdi); | |
179 | break; | |
180 | } | |
181 | } | |
182 | g_strfreev(tokens); | |
183 | g_free(buf); | |
184 | ||
185 | serial_close(serial); | |
186 | if (!devices) | |
187 | sr_serial_dev_inst_free(serial); | |
188 | ||
189 | return std_scan_complete(di, devices); | |
190 | } | |
191 | ||
192 | static int config_get(uint32_t key, GVariant **data, | |
193 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
194 | { | |
195 | struct dev_context *devc; | |
196 | ||
197 | (void)cg; | |
198 | ||
199 | devc = sdi->priv; | |
200 | ||
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 | return sr_sw_limits_config_get(&devc->limits, key, data); | |
208 | case SR_CONF_DATA_SOURCE: | |
209 | *data = g_variant_new_string(data_sources[devc->data_source]); | |
210 | break; | |
211 | default: | |
212 | return SR_ERR_NA; | |
213 | } | |
214 | ||
215 | return SR_OK; | |
216 | } | |
217 | ||
218 | static int config_set(uint32_t key, GVariant *data, | |
219 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
220 | { | |
221 | struct dev_context *devc; | |
222 | uint64_t samplerate; | |
223 | int idx; | |
224 | ||
225 | (void)cg; | |
226 | ||
227 | devc = sdi->priv; | |
228 | ||
229 | switch (key) { | |
230 | case SR_CONF_SAMPLERATE: | |
231 | samplerate = g_variant_get_uint64(data); | |
232 | if (samplerate < samplerates[0] || samplerate > samplerates[1]) | |
233 | return SR_ERR_ARG; | |
234 | devc->cur_samplerate = g_variant_get_uint64(data); | |
235 | break; | |
236 | case SR_CONF_LIMIT_SAMPLES: | |
237 | case SR_CONF_LIMIT_MSEC: | |
238 | return sr_sw_limits_config_set(&devc->limits, key, data); | |
239 | case SR_CONF_DATA_SOURCE: | |
240 | if ((idx = std_str_idx(data, ARRAY_AND_SIZE(data_sources))) < 0) | |
241 | return SR_ERR_ARG; | |
242 | devc->data_source = idx; | |
243 | break; | |
244 | default: | |
245 | return SR_ERR_NA; | |
246 | } | |
247 | ||
248 | return SR_OK; | |
249 | } | |
250 | ||
251 | static int config_list(uint32_t key, GVariant **data, | |
252 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
253 | { | |
254 | switch (key) { | |
255 | case SR_CONF_SCAN_OPTIONS: | |
256 | case SR_CONF_DEVICE_OPTIONS: | |
257 | return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); | |
258 | case SR_CONF_SAMPLERATE: | |
259 | *data = std_gvar_samplerates_steps(ARRAY_AND_SIZE(samplerates)); | |
260 | break; | |
261 | case SR_CONF_DATA_SOURCE: | |
262 | *data = g_variant_new_strv(ARRAY_AND_SIZE(data_sources)); | |
263 | break; | |
264 | default: | |
265 | return SR_ERR_NA; | |
266 | } | |
267 | ||
268 | return SR_OK; | |
269 | } | |
270 | ||
271 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
272 | { | |
273 | struct dev_context *devc = sdi->priv; | |
274 | struct sr_serial_dev_inst *serial; | |
275 | ||
276 | devc->cur_channel = sr_next_enabled_channel(sdi, NULL); | |
277 | devc->cur_conf = sr_next_enabled_channel(sdi, NULL); | |
278 | devc->cur_sample = 1; | |
279 | devc->cur_mq[0] = -1; | |
280 | if (devc->profile->nb_channels > 2) | |
281 | devc->cur_mq[1] = -1; | |
282 | ||
283 | if (devc->data_source == DATA_SOURCE_LIVE) { | |
284 | devc->jobs = devc->profile->jobs_live; | |
285 | } else { | |
286 | devc->jobs = devc->profile->jobs_log; | |
287 | if (!devc->jobs) { | |
288 | sr_err("Log data source is not implemented for this model."); | |
289 | return SR_ERR_NA; | |
290 | } | |
291 | if (!((struct sr_channel *)sdi->channels->data)->enabled) { | |
292 | sr_err("Log data is only available for channel P1."); | |
293 | return SR_ERR_NA; | |
294 | } | |
295 | } | |
296 | ||
297 | sr_sw_limits_acquisition_start(&devc->limits); | |
298 | std_session_send_df_header(sdi); | |
299 | ||
300 | serial = sdi->conn; | |
301 | serial_source_add(sdi->session, serial, G_IO_IN, 10, | |
302 | agdmm_receive_data, (void *)sdi); | |
303 | ||
304 | return SR_OK; | |
305 | } | |
306 | ||
307 | static struct sr_dev_driver agdmm_driver_info = { | |
308 | .name = "agilent-dmm", | |
309 | .longname = "Agilent U12xx series DMMs", | |
310 | .api_version = 1, | |
311 | .init = std_init, | |
312 | .cleanup = std_cleanup, | |
313 | .scan = scan, | |
314 | .dev_list = std_dev_list, | |
315 | .dev_clear = std_dev_clear, | |
316 | .config_get = config_get, | |
317 | .config_set = config_set, | |
318 | .config_list = config_list, | |
319 | .dev_open = std_serial_dev_open, | |
320 | .dev_close = std_serial_dev_close, | |
321 | .dev_acquisition_start = dev_acquisition_start, | |
322 | .dev_acquisition_stop = std_serial_dev_acquisition_stop, | |
323 | .context = NULL, | |
324 | }; | |
325 | SR_REGISTER_DEV_DRIVER(agdmm_driver_info); |