]>
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 "agilent-dmm.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_SET, | |
42 | SR_CONF_LIMIT_MSEC | SR_CONF_SET, | |
43 | }; | |
44 | ||
45 | extern const struct agdmm_job agdmm_jobs_u12xx[]; | |
46 | extern const struct agdmm_recv agdmm_recvs_u123x[]; | |
47 | extern const struct agdmm_recv agdmm_recvs_u124x[]; | |
48 | extern const struct agdmm_recv agdmm_recvs_u125x[]; | |
49 | ||
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 | ||
54 | static const struct agdmm_profile supported_agdmm[] = { | |
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 | ||
59 | { AGILENT_U1241, "U1241A", agdmm_jobs_u12xx, agdmm_recvs_u124x }, | |
60 | { AGILENT_U1242, "U1242A", agdmm_jobs_u12xx, agdmm_recvs_u124x }, | |
61 | { AGILENT_U1241, "U1241B", agdmm_jobs_u12xx, agdmm_recvs_u124x }, | |
62 | { AGILENT_U1242, "U1242B", agdmm_jobs_u12xx, agdmm_recvs_u124x }, | |
63 | ||
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 | |
71 | }; | |
72 | ||
73 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
74 | { | |
75 | struct sr_dev_inst *sdi; | |
76 | struct dev_context *devc; | |
77 | struct sr_config *src; | |
78 | struct sr_serial_dev_inst *serial; | |
79 | GSList *l, *devices; | |
80 | int len, i; | |
81 | const char *conn, *serialcomm; | |
82 | char *buf, **tokens; | |
83 | ||
84 | devices = NULL; | |
85 | conn = serialcomm = NULL; | |
86 | for (l = options; l; l = l->next) { | |
87 | src = l->data; | |
88 | switch (src->key) { | |
89 | case SR_CONF_CONN: | |
90 | conn = g_variant_get_string(src->data, NULL); | |
91 | break; | |
92 | case SR_CONF_SERIALCOMM: | |
93 | serialcomm = g_variant_get_string(src->data, NULL); | |
94 | break; | |
95 | } | |
96 | } | |
97 | if (!conn) | |
98 | return NULL; | |
99 | if (!serialcomm) | |
100 | serialcomm = SERIALCOMM; | |
101 | ||
102 | serial = sr_serial_dev_inst_new(conn, serialcomm); | |
103 | ||
104 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) | |
105 | return NULL; | |
106 | ||
107 | serial_flush(serial); | |
108 | if (serial_write_blocking(serial, "*IDN?\r\n", 7, SERIAL_WRITE_TIMEOUT_MS) < 7) { | |
109 | sr_err("Unable to send identification string."); | |
110 | return NULL; | |
111 | } | |
112 | ||
113 | len = 128; | |
114 | buf = g_malloc(len); | |
115 | serial_readline(serial, &buf, &len, 250); | |
116 | if (!len) | |
117 | return NULL; | |
118 | ||
119 | tokens = g_strsplit(buf, ",", 4); | |
120 | if (!strcmp("Agilent Technologies", tokens[0]) | |
121 | && tokens[1] && tokens[2] && tokens[3]) { | |
122 | for (i = 0; supported_agdmm[i].model; i++) { | |
123 | if (strcmp(supported_agdmm[i].modelname, tokens[1])) | |
124 | continue; | |
125 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
126 | sdi->status = SR_ST_INACTIVE; | |
127 | sdi->vendor = g_strdup("Agilent"); | |
128 | sdi->model = g_strdup(tokens[1]); | |
129 | sdi->version = g_strdup(tokens[3]); | |
130 | devc = g_malloc0(sizeof(struct dev_context)); | |
131 | sr_sw_limits_init(&devc->limits); | |
132 | devc->profile = &supported_agdmm[i]; | |
133 | devc->cur_mq = -1; | |
134 | sdi->inst_type = SR_INST_SERIAL; | |
135 | sdi->conn = serial; | |
136 | sdi->priv = devc; | |
137 | sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "P1"); | |
138 | devices = g_slist_append(devices, sdi); | |
139 | break; | |
140 | } | |
141 | } | |
142 | g_strfreev(tokens); | |
143 | g_free(buf); | |
144 | ||
145 | serial_close(serial); | |
146 | if (!devices) | |
147 | sr_serial_dev_inst_free(serial); | |
148 | ||
149 | return std_scan_complete(di, devices); | |
150 | } | |
151 | ||
152 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, | |
153 | const struct sr_channel_group *cg) | |
154 | { | |
155 | struct dev_context *devc; | |
156 | ||
157 | (void)cg; | |
158 | ||
159 | if (sdi->status != SR_ST_ACTIVE) | |
160 | return SR_ERR_DEV_CLOSED; | |
161 | ||
162 | devc = sdi->priv; | |
163 | ||
164 | return sr_sw_limits_config_set(&devc->limits, key, data); | |
165 | } | |
166 | ||
167 | static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
168 | const struct sr_channel_group *cg) | |
169 | { | |
170 | (void)cg; | |
171 | ||
172 | switch (key) { | |
173 | case SR_CONF_SCAN_OPTIONS: | |
174 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
175 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); | |
176 | break; | |
177 | case SR_CONF_DEVICE_OPTIONS: | |
178 | if (!sdi) | |
179 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
180 | drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t)); | |
181 | else | |
182 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
183 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); | |
184 | break; | |
185 | default: | |
186 | return SR_ERR_NA; | |
187 | } | |
188 | ||
189 | return SR_OK; | |
190 | } | |
191 | ||
192 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
193 | { | |
194 | struct dev_context *devc = sdi->priv; | |
195 | struct sr_serial_dev_inst *serial; | |
196 | ||
197 | if (sdi->status != SR_ST_ACTIVE) | |
198 | return SR_ERR_DEV_CLOSED; | |
199 | ||
200 | sr_sw_limits_acquisition_start(&devc->limits); | |
201 | std_session_send_df_header(sdi, LOG_PREFIX); | |
202 | ||
203 | /* Poll every 100ms, or whenever some data comes in. */ | |
204 | serial = sdi->conn; | |
205 | serial_source_add(sdi->session, serial, G_IO_IN, 100, | |
206 | agdmm_receive_data, (void *)sdi); | |
207 | ||
208 | return SR_OK; | |
209 | } | |
210 | ||
211 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) | |
212 | { | |
213 | return std_serial_dev_acquisition_stop(sdi, std_serial_dev_close, | |
214 | sdi->conn, LOG_PREFIX); | |
215 | } | |
216 | ||
217 | static struct sr_dev_driver agdmm_driver_info = { | |
218 | .name = "agilent-dmm", | |
219 | .longname = "Agilent U12xx series DMMs", | |
220 | .api_version = 1, | |
221 | .init = std_init, | |
222 | .cleanup = std_cleanup, | |
223 | .scan = scan, | |
224 | .dev_list = std_dev_list, | |
225 | .dev_clear = NULL, | |
226 | .config_get = NULL, | |
227 | .config_set = config_set, | |
228 | .config_list = config_list, | |
229 | .dev_open = std_serial_dev_open, | |
230 | .dev_close = std_serial_dev_close, | |
231 | .dev_acquisition_start = dev_acquisition_start, | |
232 | .dev_acquisition_stop = dev_acquisition_stop, | |
233 | .context = NULL, | |
234 | }; | |
235 | SR_REGISTER_DEV_DRIVER(agdmm_driver_info); |