]>
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 UH |
27 | #include "libsigrok-internal.h" |
28 | #include "agilent-dmm.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[] = { | |
40 | SR_CONF_CONTINUOUS | SR_CONF_SET, | |
5827f61b BV |
41 | SR_CONF_LIMIT_SAMPLES | SR_CONF_SET, |
42 | SR_CONF_LIMIT_MSEC | SR_CONF_SET, | |
e93cdf42 BV |
43 | }; |
44 | ||
f857bd92 | 45 | extern const struct agdmm_job agdmm_jobs_u12xx[]; |
8c0152f2 | 46 | extern const struct agdmm_recv agdmm_recvs_u123x[]; |
4edba404 | 47 | extern const struct agdmm_recv agdmm_recvs_u124x[]; |
8c0152f2 | 48 | extern const struct agdmm_recv agdmm_recvs_u125x[]; |
e93cdf42 | 49 | |
74ac7d7f BV |
50 | /* This works on all the Agilent U12xxA series, although the |
51 | * U127xA can apparently also run at 19200/8n1. */ | |
52 | #define SERIALCOMM "9600/8n1" | |
53 | ||
e93cdf42 | 54 | static const struct agdmm_profile supported_agdmm[] = { |
f857bd92 BV |
55 | { AGILENT_U1231, "U1231A", agdmm_jobs_u12xx, agdmm_recvs_u123x }, |
56 | { AGILENT_U1232, "U1232A", agdmm_jobs_u12xx, agdmm_recvs_u123x }, | |
57 | { AGILENT_U1233, "U1233A", agdmm_jobs_u12xx, agdmm_recvs_u123x }, | |
58 | ||
4edba404 BV |
59 | { AGILENT_U1241, "U1241A", agdmm_jobs_u12xx, agdmm_recvs_u124x }, |
60 | { AGILENT_U1242, "U1242A", agdmm_jobs_u12xx, agdmm_recvs_u124x }, | |
ee2bcdfc BV |
61 | { AGILENT_U1241, "U1241B", agdmm_jobs_u12xx, agdmm_recvs_u124x }, |
62 | { AGILENT_U1242, "U1242B", agdmm_jobs_u12xx, agdmm_recvs_u124x }, | |
4edba404 | 63 | |
f857bd92 BV |
64 | { AGILENT_U1251, "U1251A", agdmm_jobs_u12xx, agdmm_recvs_u125x }, |
65 | { AGILENT_U1252, "U1252A", agdmm_jobs_u12xx, agdmm_recvs_u125x }, | |
66 | { AGILENT_U1253, "U1253A", agdmm_jobs_u12xx, agdmm_recvs_u125x }, | |
67 | { AGILENT_U1251, "U1251B", agdmm_jobs_u12xx, agdmm_recvs_u125x }, | |
68 | { AGILENT_U1252, "U1252B", agdmm_jobs_u12xx, agdmm_recvs_u125x }, | |
69 | { AGILENT_U1253, "U1253B", agdmm_jobs_u12xx, agdmm_recvs_u125x }, | |
70 | ALL_ZERO | |
e93cdf42 BV |
71 | }; |
72 | ||
73 | SR_PRIV struct sr_dev_driver agdmm_driver_info; | |
e93cdf42 | 74 | |
4f840ce9 | 75 | static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx) |
e93cdf42 | 76 | { |
f6beaac5 | 77 | return std_init(sr_ctx, di, LOG_PREFIX); |
e93cdf42 BV |
78 | } |
79 | ||
4f840ce9 | 80 | static GSList *scan(struct sr_dev_driver *di, GSList *options) |
e93cdf42 BV |
81 | { |
82 | struct sr_dev_inst *sdi; | |
83 | struct drv_context *drvc; | |
84 | struct dev_context *devc; | |
1987b8d6 | 85 | struct sr_config *src; |
109a3ba4 | 86 | struct sr_serial_dev_inst *serial; |
e93cdf42 | 87 | GSList *l, *devices; |
109a3ba4 | 88 | int len, i; |
e93cdf42 BV |
89 | const char *conn, *serialcomm; |
90 | char *buf, **tokens; | |
91 | ||
41812aca | 92 | drvc = di->context; |
e93cdf42 BV |
93 | drvc->instances = NULL; |
94 | ||
95 | devices = NULL; | |
96 | conn = serialcomm = NULL; | |
97 | for (l = options; l; l = l->next) { | |
1987b8d6 BV |
98 | src = l->data; |
99 | switch (src->key) { | |
1953564a | 100 | case SR_CONF_CONN: |
e44ac12a | 101 | conn = g_variant_get_string(src->data, NULL); |
e93cdf42 | 102 | break; |
1953564a | 103 | case SR_CONF_SERIALCOMM: |
e44ac12a | 104 | serialcomm = g_variant_get_string(src->data, NULL); |
e93cdf42 BV |
105 | break; |
106 | } | |
107 | } | |
74ac7d7f | 108 | if (!conn) |
e93cdf42 | 109 | return NULL; |
109a3ba4 BV |
110 | if (!serialcomm) |
111 | serialcomm = SERIALCOMM; | |
e93cdf42 | 112 | |
91219afc | 113 | serial = sr_serial_dev_inst_new(conn, serialcomm); |
e93cdf42 | 114 | |
64c536be | 115 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) |
e93cdf42 | 116 | return NULL; |
e93cdf42 | 117 | |
109a3ba4 | 118 | serial_flush(serial); |
95779b43 | 119 | if (serial_write_blocking(serial, "*IDN?\r\n", 7, SERIAL_WRITE_TIMEOUT_MS) < 7) { |
081c214e | 120 | sr_err("Unable to send identification string."); |
e93cdf42 BV |
121 | return NULL; |
122 | } | |
123 | ||
124 | len = 128; | |
a95f142e | 125 | buf = g_malloc(len); |
ee2bcdfc | 126 | serial_readline(serial, &buf, &len, 250); |
e93cdf42 BV |
127 | if (!len) |
128 | return NULL; | |
129 | ||
130 | tokens = g_strsplit(buf, ",", 4); | |
131 | if (!strcmp("Agilent Technologies", tokens[0]) | |
fbf07e02 | 132 | && tokens[1] && tokens[2] && tokens[3]) { |
e93cdf42 BV |
133 | for (i = 0; supported_agdmm[i].model; i++) { |
134 | if (strcmp(supported_agdmm[i].modelname, tokens[1])) | |
135 | continue; | |
aac29cc1 | 136 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); |
0af636be UH |
137 | sdi->status = SR_ST_INACTIVE; |
138 | sdi->vendor = g_strdup("Agilent"); | |
139 | sdi->model = g_strdup(tokens[1]); | |
140 | sdi->version = g_strdup(tokens[3]); | |
f57d8ffe | 141 | devc = g_malloc0(sizeof(struct dev_context)); |
e93cdf42 | 142 | devc->profile = &supported_agdmm[i]; |
e93cdf42 | 143 | devc->cur_mq = -1; |
fb3a1505 BV |
144 | sdi->inst_type = SR_INST_SERIAL; |
145 | sdi->conn = serial; | |
e93cdf42 BV |
146 | sdi->priv = devc; |
147 | sdi->driver = di; | |
5e23fcab | 148 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1"); |
e93cdf42 BV |
149 | drvc->instances = g_slist_append(drvc->instances, sdi); |
150 | devices = g_slist_append(devices, sdi); | |
151 | break; | |
152 | } | |
153 | } | |
154 | g_strfreev(tokens); | |
155 | g_free(buf); | |
156 | ||
109a3ba4 BV |
157 | serial_close(serial); |
158 | if (!devices) | |
159 | sr_serial_dev_inst_free(serial); | |
e93cdf42 BV |
160 | |
161 | return devices; | |
162 | } | |
163 | ||
4f840ce9 | 164 | static GSList *dev_list(const struct sr_dev_driver *di) |
e93cdf42 | 165 | { |
41812aca | 166 | return ((struct drv_context *)(di->context))->instances; |
e93cdf42 BV |
167 | } |
168 | ||
4f840ce9 | 169 | static int cleanup(const struct sr_dev_driver *di) |
e93cdf42 | 170 | { |
a6630742 | 171 | return std_dev_clear(di, NULL); |
e93cdf42 BV |
172 | } |
173 | ||
584560f1 | 174 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, |
53b4680f | 175 | const struct sr_channel_group *cg) |
e93cdf42 BV |
176 | { |
177 | struct dev_context *devc; | |
178 | ||
53b4680f | 179 | (void)cg; |
8f996b89 | 180 | |
e93cdf42 | 181 | if (sdi->status != SR_ST_ACTIVE) |
e73ffd42 | 182 | return SR_ERR_DEV_CLOSED; |
e93cdf42 BV |
183 | |
184 | if (!(devc = sdi->priv)) { | |
38d326e8 | 185 | sr_err("sdi->priv was NULL."); |
e93cdf42 BV |
186 | return SR_ERR_BUG; |
187 | } | |
188 | ||
584560f1 | 189 | switch (key) { |
1953564a | 190 | case SR_CONF_LIMIT_MSEC: |
e93cdf42 | 191 | /* TODO: not yet implemented */ |
e44ac12a | 192 | devc->limit_msec = g_variant_get_uint64(data); |
e93cdf42 | 193 | break; |
1953564a | 194 | case SR_CONF_LIMIT_SAMPLES: |
e44ac12a | 195 | devc->limit_samples = g_variant_get_uint64(data); |
e93cdf42 BV |
196 | break; |
197 | default: | |
bd6fbf62 | 198 | return SR_ERR_NA; |
e93cdf42 BV |
199 | } |
200 | ||
201 | return SR_OK; | |
202 | } | |
203 | ||
584560f1 | 204 | static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, |
53b4680f | 205 | const struct sr_channel_group *cg) |
a1c743fc | 206 | { |
a1c743fc | 207 | (void)sdi; |
53b4680f | 208 | (void)cg; |
a1c743fc BV |
209 | |
210 | switch (key) { | |
0d485e30 | 211 | case SR_CONF_SCAN_OPTIONS: |
584560f1 | 212 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, |
a0e0bb41 | 213 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); |
0d485e30 | 214 | break; |
9a6517d1 | 215 | case SR_CONF_DEVICE_OPTIONS: |
ce4bd052 BV |
216 | if (!sdi) |
217 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
f9dada0b | 218 | drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t)); |
ce4bd052 BV |
219 | else |
220 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
f9dada0b | 221 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); |
9a6517d1 | 222 | break; |
a1c743fc | 223 | default: |
bd6fbf62 | 224 | return SR_ERR_NA; |
a1c743fc BV |
225 | } |
226 | ||
227 | return SR_OK; | |
228 | } | |
229 | ||
6078d2c9 | 230 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) |
e93cdf42 | 231 | { |
e93cdf42 | 232 | struct dev_context *devc; |
fb3a1505 | 233 | struct sr_serial_dev_inst *serial; |
e93cdf42 | 234 | |
e73ffd42 BV |
235 | if (sdi->status != SR_ST_ACTIVE) |
236 | return SR_ERR_DEV_CLOSED; | |
237 | ||
e93cdf42 | 238 | if (!(devc = sdi->priv)) { |
38d326e8 | 239 | sr_err("sdi->priv was NULL."); |
e93cdf42 BV |
240 | return SR_ERR_BUG; |
241 | } | |
242 | ||
e93cdf42 BV |
243 | devc->cb_data = cb_data; |
244 | ||
245 | /* Send header packet to the session bus. */ | |
29a27196 | 246 | std_session_send_df_header(cb_data, LOG_PREFIX); |
e93cdf42 | 247 | |
e93cdf42 | 248 | /* Poll every 100ms, or whenever some data comes in. */ |
fb3a1505 | 249 | serial = sdi->conn; |
102f1239 BV |
250 | serial_source_add(sdi->session, serial, G_IO_IN, 100, |
251 | agdmm_receive_data, (void *)sdi); | |
e93cdf42 BV |
252 | |
253 | return SR_OK; | |
254 | } | |
255 | ||
6078d2c9 | 256 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) |
e93cdf42 | 257 | { |
d43b0908 | 258 | return std_serial_dev_acquisition_stop(sdi, cb_data, std_serial_dev_close, |
29a27196 | 259 | sdi->conn, LOG_PREFIX); |
e93cdf42 BV |
260 | } |
261 | ||
262 | SR_PRIV struct sr_dev_driver agdmm_driver_info = { | |
263 | .name = "agilent-dmm", | |
264 | .longname = "Agilent U12xx series DMMs", | |
265 | .api_version = 1, | |
6078d2c9 UH |
266 | .init = init, |
267 | .cleanup = cleanup, | |
268 | .scan = scan, | |
269 | .dev_list = dev_list, | |
a6630742 | 270 | .dev_clear = NULL, |
6fab7b8f | 271 | .config_get = NULL, |
035a1078 | 272 | .config_set = config_set, |
a1c743fc | 273 | .config_list = config_list, |
854434de | 274 | .dev_open = std_serial_dev_open, |
bf2c987f | 275 | .dev_close = std_serial_dev_close, |
6078d2c9 UH |
276 | .dev_acquisition_start = dev_acquisition_start, |
277 | .dev_acquisition_stop = dev_acquisition_stop, | |
41812aca | 278 | .context = NULL, |
e93cdf42 | 279 | }; |