]>
Commit | Line | Data |
---|---|---|
e93cdf42 | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
e93cdf42 BV |
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 | ||
6ec6c43b | 20 | #include <config.h> |
e93cdf42 | 21 | #include <glib.h> |
e93cdf42 BV |
22 | #include <sys/types.h> |
23 | #include <sys/stat.h> | |
24 | #include <fcntl.h> | |
25 | #include <string.h> | |
c1aae900 | 26 | #include <libsigrok/libsigrok.h> |
6f22a8ef | 27 | #include "libsigrok-internal.h" |
6cf1a87b | 28 | #include "protocol.h" |
e93cdf42 | 29 | |
a0e0bb41 | 30 | static const uint32_t scanopts[] = { |
1953564a BV |
31 | SR_CONF_CONN, |
32 | SR_CONF_SERIALCOMM, | |
e93cdf42 BV |
33 | }; |
34 | ||
f9dada0b | 35 | static const uint32_t drvopts[] = { |
1953564a | 36 | SR_CONF_MULTIMETER, |
f9dada0b BV |
37 | }; |
38 | ||
39 | static const uint32_t devopts[] = { | |
e91bb0a6 | 40 | SR_CONF_CONTINUOUS, |
6ace179e AJ |
41 | SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, |
42 | SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET, | |
b907d62f | 43 | SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, |
63bb11ba | 44 | SR_CONF_DATA_SOURCE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, |
b907d62f AJ |
45 | }; |
46 | ||
47 | static const uint64_t samplerates[] = { | |
48 | SR_HZ(1), | |
49 | SR_HZ(20), | |
50 | SR_HZ(1), | |
e93cdf42 BV |
51 | }; |
52 | ||
63bb11ba | 53 | static const char *data_sources[] = { |
f8195cb2 | 54 | "Live", "Log-Hand", "Log-Trig", "Log-Auto", "Log-Export", |
63bb11ba AJ |
55 | }; |
56 | ||
57 | extern const struct agdmm_job agdmm_jobs_live[]; | |
58 | extern const struct agdmm_job agdmm_jobs_log[]; | |
8c0152f2 | 59 | extern const struct agdmm_recv agdmm_recvs_u123x[]; |
4edba404 | 60 | extern const struct agdmm_recv agdmm_recvs_u124x[]; |
0ceb7038 | 61 | extern const struct agdmm_recv agdmm_recvs_u124xc[]; |
8c0152f2 | 62 | extern const struct agdmm_recv agdmm_recvs_u125x[]; |
43185ed3 | 63 | extern const struct agdmm_recv agdmm_recvs_u128x[]; |
e93cdf42 | 64 | |
74ac7d7f BV |
65 | /* This works on all the Agilent U12xxA series, although the |
66 | * U127xA can apparently also run at 19200/8n1. */ | |
67 | #define SERIALCOMM "9600/8n1" | |
68 | ||
e93cdf42 | 69 | static const struct agdmm_profile supported_agdmm[] = { |
63bb11ba AJ |
70 | { AGILENT_U1231, "U1231A", 1, agdmm_jobs_live, NULL, agdmm_recvs_u123x }, |
71 | { AGILENT_U1232, "U1232A", 1, agdmm_jobs_live, NULL, agdmm_recvs_u123x }, | |
72 | { AGILENT_U1233, "U1233A", 1, agdmm_jobs_live, NULL, agdmm_recvs_u123x }, | |
73 | ||
74 | { AGILENT_U1241, "U1241A", 2, agdmm_jobs_live, NULL, agdmm_recvs_u124x }, | |
75 | { AGILENT_U1242, "U1242A", 2, agdmm_jobs_live, NULL, agdmm_recvs_u124x }, | |
76 | { AGILENT_U1241, "U1241B", 2, agdmm_jobs_live, NULL, agdmm_recvs_u124x }, | |
77 | { AGILENT_U1242, "U1242B", 2, agdmm_jobs_live, NULL, agdmm_recvs_u124x }, | |
78 | ||
0ceb7038 AJ |
79 | { KEYSIGHT_U1241C, "U1241C", 2, agdmm_jobs_live, agdmm_jobs_log, agdmm_recvs_u124xc }, |
80 | { KEYSIGHT_U1242C, "U1242C", 2, agdmm_jobs_live, agdmm_jobs_log, agdmm_recvs_u124xc }, | |
81 | ||
63bb11ba AJ |
82 | { AGILENT_U1251, "U1251A", 3, agdmm_jobs_live, NULL, agdmm_recvs_u125x }, |
83 | { AGILENT_U1252, "U1252A", 3, agdmm_jobs_live, NULL, agdmm_recvs_u125x }, | |
84 | { AGILENT_U1253, "U1253A", 3, agdmm_jobs_live, NULL, agdmm_recvs_u125x }, | |
85 | { AGILENT_U1251, "U1251B", 3, agdmm_jobs_live, NULL, agdmm_recvs_u125x }, | |
86 | { AGILENT_U1252, "U1252B", 3, agdmm_jobs_live, NULL, agdmm_recvs_u125x }, | |
87 | { AGILENT_U1253, "U1253B", 3, agdmm_jobs_live, NULL, agdmm_recvs_u125x }, | |
88 | ||
89 | { KEYSIGHT_U1281, "U1281A", 3, agdmm_jobs_live, agdmm_jobs_log, agdmm_recvs_u128x }, | |
90 | { KEYSIGHT_U1282, "U1282A", 3, agdmm_jobs_live, agdmm_jobs_log, agdmm_recvs_u128x }, | |
f857bd92 | 91 | ALL_ZERO |
e93cdf42 BV |
92 | }; |
93 | ||
4f840ce9 | 94 | static GSList *scan(struct sr_dev_driver *di, GSList *options) |
e93cdf42 BV |
95 | { |
96 | struct sr_dev_inst *sdi; | |
e93cdf42 | 97 | struct dev_context *devc; |
1987b8d6 | 98 | struct sr_config *src; |
109a3ba4 | 99 | struct sr_serial_dev_inst *serial; |
e93cdf42 | 100 | GSList *l, *devices; |
109a3ba4 | 101 | int len, i; |
e93cdf42 BV |
102 | const char *conn, *serialcomm; |
103 | char *buf, **tokens; | |
104 | ||
e93cdf42 BV |
105 | devices = NULL; |
106 | conn = serialcomm = NULL; | |
107 | for (l = options; l; l = l->next) { | |
1987b8d6 BV |
108 | src = l->data; |
109 | switch (src->key) { | |
1953564a | 110 | case SR_CONF_CONN: |
e44ac12a | 111 | conn = g_variant_get_string(src->data, NULL); |
e93cdf42 | 112 | break; |
1953564a | 113 | case SR_CONF_SERIALCOMM: |
e44ac12a | 114 | serialcomm = g_variant_get_string(src->data, NULL); |
e93cdf42 BV |
115 | break; |
116 | } | |
117 | } | |
74ac7d7f | 118 | if (!conn) |
e93cdf42 | 119 | return NULL; |
109a3ba4 BV |
120 | if (!serialcomm) |
121 | serialcomm = SERIALCOMM; | |
e93cdf42 | 122 | |
91219afc | 123 | serial = sr_serial_dev_inst_new(conn, serialcomm); |
e93cdf42 | 124 | |
64c536be | 125 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) |
e93cdf42 | 126 | return NULL; |
e93cdf42 | 127 | |
109a3ba4 | 128 | serial_flush(serial); |
95779b43 | 129 | if (serial_write_blocking(serial, "*IDN?\r\n", 7, SERIAL_WRITE_TIMEOUT_MS) < 7) { |
081c214e | 130 | sr_err("Unable to send identification string."); |
e93cdf42 BV |
131 | return NULL; |
132 | } | |
133 | ||
134 | len = 128; | |
a95f142e | 135 | buf = g_malloc(len); |
ee2bcdfc | 136 | serial_readline(serial, &buf, &len, 250); |
e93cdf42 BV |
137 | if (!len) |
138 | return NULL; | |
139 | ||
140 | tokens = g_strsplit(buf, ",", 4); | |
9ae544b6 AJ |
141 | if ((!strcmp("Agilent Technologies", tokens[0]) || |
142 | !strcmp("Keysight Technologies", tokens[0])) | |
fbf07e02 | 143 | && tokens[1] && tokens[2] && tokens[3]) { |
e93cdf42 BV |
144 | for (i = 0; supported_agdmm[i].model; i++) { |
145 | if (strcmp(supported_agdmm[i].modelname, tokens[1])) | |
146 | continue; | |
aac29cc1 | 147 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); |
0af636be | 148 | sdi->status = SR_ST_INACTIVE; |
9ae544b6 | 149 | sdi->vendor = g_strdup(tokens[0][0] == 'A' ? "Agilent" : "Keysight"); |
0af636be UH |
150 | sdi->model = g_strdup(tokens[1]); |
151 | sdi->version = g_strdup(tokens[3]); | |
f57d8ffe | 152 | devc = g_malloc0(sizeof(struct dev_context)); |
5b6829ea | 153 | sr_sw_limits_init(&devc->limits); |
e93cdf42 | 154 | devc->profile = &supported_agdmm[i]; |
63bb11ba | 155 | devc->data_source = DEFAULT_DATA_SOURCE; |
b907d62f | 156 | devc->cur_samplerate = 5; |
d822726d AJ |
157 | if (supported_agdmm[i].nb_channels > 1) { |
158 | int temp_chan = supported_agdmm[i].nb_channels - 1; | |
159 | devc->cur_mq[temp_chan] = SR_MQ_TEMPERATURE; | |
160 | devc->cur_unit[temp_chan] = SR_UNIT_CELSIUS; | |
161 | devc->cur_digits[temp_chan] = 1; | |
162 | devc->cur_encoding[temp_chan] = 2; | |
163 | } | |
fb3a1505 BV |
164 | sdi->inst_type = SR_INST_SERIAL; |
165 | sdi->conn = serial; | |
e93cdf42 | 166 | sdi->priv = devc; |
5e23fcab | 167 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1"); |
d822726d AJ |
168 | if (supported_agdmm[i].nb_channels > 1) |
169 | sr_channel_new(sdi, 1, SR_CHANNEL_ANALOG, TRUE, "P2"); | |
170 | if (supported_agdmm[i].nb_channels > 2) | |
171 | sr_channel_new(sdi, 2, SR_CHANNEL_ANALOG, TRUE, "P3"); | |
e93cdf42 BV |
172 | devices = g_slist_append(devices, sdi); |
173 | break; | |
174 | } | |
175 | } | |
176 | g_strfreev(tokens); | |
177 | g_free(buf); | |
178 | ||
109a3ba4 BV |
179 | serial_close(serial); |
180 | if (!devices) | |
181 | sr_serial_dev_inst_free(serial); | |
e93cdf42 | 182 | |
15a5bfe4 | 183 | return std_scan_complete(di, devices); |
e93cdf42 BV |
184 | } |
185 | ||
dd7a72ea UH |
186 | static int config_get(uint32_t key, GVariant **data, |
187 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
6ace179e AJ |
188 | { |
189 | struct dev_context *devc; | |
6ace179e AJ |
190 | |
191 | (void)cg; | |
192 | ||
193 | devc = sdi->priv; | |
194 | ||
6ace179e | 195 | switch (key) { |
b907d62f AJ |
196 | case SR_CONF_SAMPLERATE: |
197 | *data = g_variant_new_uint64(devc->cur_samplerate); | |
198 | break; | |
6ace179e AJ |
199 | case SR_CONF_LIMIT_SAMPLES: |
200 | case SR_CONF_LIMIT_MSEC: | |
758906aa | 201 | return sr_sw_limits_config_get(&devc->limits, key, data); |
63bb11ba AJ |
202 | case SR_CONF_DATA_SOURCE: |
203 | *data = g_variant_new_string(data_sources[devc->data_source]); | |
204 | break; | |
6ace179e AJ |
205 | default: |
206 | return SR_ERR_NA; | |
207 | } | |
208 | ||
758906aa | 209 | return SR_OK; |
6ace179e AJ |
210 | } |
211 | ||
dd7a72ea UH |
212 | static int config_set(uint32_t key, GVariant *data, |
213 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
e93cdf42 BV |
214 | { |
215 | struct dev_context *devc; | |
b907d62f | 216 | uint64_t samplerate; |
758906aa | 217 | int idx; |
e93cdf42 | 218 | |
53b4680f | 219 | (void)cg; |
8f996b89 | 220 | |
b0baddef | 221 | devc = sdi->priv; |
e93cdf42 | 222 | |
6ace179e | 223 | switch (key) { |
b907d62f AJ |
224 | case SR_CONF_SAMPLERATE: |
225 | samplerate = g_variant_get_uint64(data); | |
226 | if (samplerate < samplerates[0] || samplerate > samplerates[1]) | |
758906aa UH |
227 | return SR_ERR_ARG; |
228 | devc->cur_samplerate = g_variant_get_uint64(data); | |
b907d62f | 229 | break; |
6ace179e AJ |
230 | case SR_CONF_LIMIT_SAMPLES: |
231 | case SR_CONF_LIMIT_MSEC: | |
758906aa | 232 | return sr_sw_limits_config_set(&devc->limits, key, data); |
697fb6dd UH |
233 | case SR_CONF_DATA_SOURCE: |
234 | if ((idx = std_str_idx(data, ARRAY_AND_SIZE(data_sources))) < 0) | |
235 | return SR_ERR_ARG; | |
236 | devc->data_source = idx; | |
63bb11ba | 237 | break; |
6ace179e | 238 | default: |
758906aa | 239 | return SR_ERR_NA; |
6ace179e AJ |
240 | } |
241 | ||
758906aa | 242 | return SR_OK; |
e93cdf42 BV |
243 | } |
244 | ||
dd7a72ea UH |
245 | static int config_list(uint32_t key, GVariant **data, |
246 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
a1c743fc | 247 | { |
6ace179e | 248 | switch (key) { |
e66d1892 | 249 | case SR_CONF_SCAN_OPTIONS: |
9a6517d1 | 250 | case SR_CONF_DEVICE_OPTIONS: |
e66d1892 | 251 | return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); |
b907d62f | 252 | case SR_CONF_SAMPLERATE: |
53012da6 | 253 | *data = std_gvar_samplerates_steps(ARRAY_AND_SIZE(samplerates)); |
b907d62f | 254 | break; |
63bb11ba | 255 | case SR_CONF_DATA_SOURCE: |
53012da6 | 256 | *data = g_variant_new_strv(ARRAY_AND_SIZE(data_sources)); |
63bb11ba | 257 | break; |
a1c743fc | 258 | default: |
bd6fbf62 | 259 | return SR_ERR_NA; |
a1c743fc BV |
260 | } |
261 | ||
262 | return SR_OK; | |
263 | } | |
264 | ||
695dc859 | 265 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) |
e93cdf42 | 266 | { |
5b6829ea | 267 | struct dev_context *devc = sdi->priv; |
fb3a1505 | 268 | struct sr_serial_dev_inst *serial; |
e93cdf42 | 269 | |
d822726d AJ |
270 | devc->cur_channel = sr_next_enabled_channel(sdi, NULL); |
271 | devc->cur_conf = sr_next_enabled_channel(sdi, NULL); | |
63bb11ba | 272 | devc->cur_sample = 1; |
d822726d AJ |
273 | devc->cur_mq[0] = -1; |
274 | if (devc->profile->nb_channels > 2) | |
275 | devc->cur_mq[1] = -1; | |
276 | ||
63bb11ba AJ |
277 | if (devc->data_source == DATA_SOURCE_LIVE) { |
278 | devc->jobs = devc->profile->jobs_live; | |
279 | } else { | |
280 | devc->jobs = devc->profile->jobs_log; | |
281 | if (!devc->jobs) { | |
282 | sr_err("Log data source is not implemented for this model."); | |
283 | return SR_ERR_NA; | |
284 | } | |
285 | if (!((struct sr_channel *)sdi->channels->data)->enabled) { | |
286 | sr_err("Log data is only available for channel P1."); | |
287 | return SR_ERR_NA; | |
288 | } | |
289 | } | |
290 | ||
5b6829ea | 291 | sr_sw_limits_acquisition_start(&devc->limits); |
bee2b016 | 292 | std_session_send_df_header(sdi); |
e93cdf42 | 293 | |
fb3a1505 | 294 | serial = sdi->conn; |
b907d62f | 295 | serial_source_add(sdi->session, serial, G_IO_IN, 10, |
102f1239 | 296 | agdmm_receive_data, (void *)sdi); |
e93cdf42 BV |
297 | |
298 | return SR_OK; | |
299 | } | |
300 | ||
dd5c48a6 | 301 | static struct sr_dev_driver agdmm_driver_info = { |
e93cdf42 BV |
302 | .name = "agilent-dmm", |
303 | .longname = "Agilent U12xx series DMMs", | |
304 | .api_version = 1, | |
c2fdcc25 | 305 | .init = std_init, |
700d6b64 | 306 | .cleanup = std_cleanup, |
6078d2c9 | 307 | .scan = scan, |
c01bf34c | 308 | .dev_list = std_dev_list, |
f778bf02 | 309 | .dev_clear = std_dev_clear, |
6ace179e | 310 | .config_get = config_get, |
035a1078 | 311 | .config_set = config_set, |
a1c743fc | 312 | .config_list = config_list, |
854434de | 313 | .dev_open = std_serial_dev_open, |
bf2c987f | 314 | .dev_close = std_serial_dev_close, |
6078d2c9 | 315 | .dev_acquisition_start = dev_acquisition_start, |
4b1a9d5d | 316 | .dev_acquisition_stop = std_serial_dev_acquisition_stop, |
41812aca | 317 | .context = NULL, |
e93cdf42 | 318 | }; |
dd5c48a6 | 319 | SR_REGISTER_DEV_DRIVER(agdmm_driver_info); |