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