]>
Commit | Line | Data |
---|---|---|
b872ab7d BS |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2018 Bastian Schmitz <bastian.schmitz@udo.edu> | |
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 <string.h> | |
22 | #include "protocol.h" | |
23 | ||
c329b788 RA |
24 | #define IDN_RETRIES 3 /* at least 2 */ |
25 | ||
b872ab7d BS |
26 | static const uint32_t scanopts[] = { |
27 | SR_CONF_CONN, | |
28 | SR_CONF_SERIALCOMM, | |
29 | }; | |
30 | ||
31 | static const uint32_t drvopts[] = { | |
32 | SR_CONF_POWER_SUPPLY, | |
33 | }; | |
34 | ||
35 | static const uint32_t devopts[] = { | |
36 | SR_CONF_CONTINUOUS, | |
37 | SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, | |
38 | SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET, | |
39 | SR_CONF_CHANNEL_CONFIG | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
40 | SR_CONF_ENABLED | SR_CONF_GET | SR_CONF_SET, | |
41 | }; | |
42 | ||
43 | static const uint32_t devopts_cg[] = { | |
44 | SR_CONF_VOLTAGE | SR_CONF_GET, | |
45 | SR_CONF_VOLTAGE_TARGET | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
46 | SR_CONF_CURRENT | SR_CONF_GET, | |
47 | SR_CONF_CURRENT_LIMIT | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
48 | }; | |
49 | ||
50 | static const char *channel_modes[] = { | |
51 | "Independent", | |
52 | }; | |
53 | ||
c93c014f RA |
54 | static const char *gpd_serialcomms[] = { |
55 | "9600/8n1", | |
56 | "57600/8n1", | |
57 | "115200/8n1" | |
58 | }; | |
59 | ||
b872ab7d BS |
60 | static const struct gpd_model models[] = { |
61 | { GPD_2303S, "GPD-2303S", | |
62 | CHANMODE_INDEPENDENT, | |
63 | 2, | |
64 | { | |
65 | /* Channel 1 */ | |
66 | { { 0, 30, 0.001 }, { 0, 3, 0.001 } }, | |
67 | /* Channel 2 */ | |
68 | { { 0, 30, 0.001 }, { 0, 3, 0.001 } }, | |
69 | }, | |
70 | }, | |
3b316fdc TK |
71 | { GPD_3303S, "GPD-3303S", |
72 | CHANMODE_INDEPENDENT, | |
73 | 2, | |
74 | { | |
75 | /* Channel 1 */ | |
76 | { { 0, 32, 0.001 }, { 0, 3.2, 0.001 } }, | |
77 | /* Channel 2 */ | |
78 | { { 0, 32, 0.001 }, { 0, 3.2, 0.001 } }, | |
79 | }, | |
80 | }, | |
b872ab7d BS |
81 | }; |
82 | ||
83 | static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
84 | { | |
c93c014f | 85 | const char *conn, *serialcomm, **serialcomms; |
b872ab7d BS |
86 | const struct gpd_model *model; |
87 | const struct sr_config *src; | |
88 | struct sr_channel *ch; | |
89 | struct sr_channel_group *cg; | |
90 | GSList *l; | |
91 | struct sr_serial_dev_inst *serial; | |
92 | struct sr_dev_inst *sdi; | |
13726d30 | 93 | char reply[100]; |
c93c014f | 94 | unsigned int i, b, serialcomms_count; |
b872ab7d BS |
95 | struct dev_context *devc; |
96 | char channel[10]; | |
97 | GRegex *regex; | |
98 | GMatchInfo *match_info; | |
99 | unsigned int cc_cv_ch1, cc_cv_ch2, track1, track2, beep, baud1, baud2; | |
100 | ||
101 | serial = NULL; | |
102 | match_info = NULL; | |
103 | regex = NULL; | |
104 | conn = NULL; | |
105 | serialcomm = NULL; | |
106 | ||
107 | for (l = options; l; l = l->next) { | |
108 | src = l->data; | |
109 | switch (src->key) { | |
110 | case SR_CONF_CONN: | |
111 | conn = g_variant_get_string(src->data, NULL); | |
112 | break; | |
113 | case SR_CONF_SERIALCOMM: | |
114 | serialcomm = g_variant_get_string(src->data, NULL); | |
115 | break; | |
116 | } | |
117 | } | |
118 | ||
119 | if (!conn) | |
120 | return NULL; | |
c93c014f RA |
121 | if (serialcomm) { |
122 | serialcomms = &serialcomm; | |
123 | serialcomms_count = 1; | |
124 | } else { | |
125 | serialcomms = gpd_serialcomms; | |
126 | serialcomms_count = sizeof(gpd_serialcomms) / sizeof(gpd_serialcomms[0]); | |
b872ab7d | 127 | } |
b872ab7d | 128 | |
c93c014f RA |
129 | for( b = 0; b < serialcomms_count; b++) { |
130 | serialcomm = serialcomms[b]; | |
131 | sr_info("Probing serial port %s @ %s", conn, serialcomm); | |
132 | serial = sr_serial_dev_inst_new(conn, serialcomm); | |
133 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) | |
134 | continue; | |
b872ab7d | 135 | |
c93c014f RA |
136 | /* |
137 | * Problem: we need to clear the GPD receive buffer before we | |
138 | * can expect it to process commands correctly. | |
139 | * | |
140 | * Do not just send a newline, since that may cause it to | |
141 | * execute a currently buffered command. | |
142 | * | |
143 | * Solution: Send identification request a few times. | |
144 | * The first should corrupt any previous buffered command if present | |
145 | * and respond with "Invalid Character." or respond directly with | |
146 | * an identification string. | |
147 | */ | |
148 | for (i = 0; i < IDN_RETRIES; i++) { | |
149 | /* Request the GPD to identify itself */ | |
150 | gpd_send_cmd(serial, "*IDN?\n"); | |
151 | if (gpd_receive_reply(serial, reply, sizeof(reply)) == SR_OK) { | |
152 | if (0 == strncmp(reply, "GW INSTEK", 9)) { | |
153 | break; | |
154 | } | |
155 | } | |
b872ab7d | 156 | } |
c93c014f RA |
157 | if (i == IDN_RETRIES) { |
158 | sr_err("Device did not reply to identification request."); | |
159 | serial_flush(serial); | |
13726d30 TK |
160 | goto error; |
161 | } | |
b872ab7d | 162 | |
c93c014f RA |
163 | /* |
164 | * Returned identification string is for example: | |
165 | * "GW INSTEK,GPD-2303S,SN:ER915277,V2.10" | |
166 | */ | |
167 | regex = g_regex_new("GW INSTEK,(.+),SN:(.+),(V.+)", 0, 0, NULL); | |
168 | if (!g_regex_match(regex, reply, 0, &match_info)) { | |
169 | sr_err("Unsupported model '%s'.", reply); | |
b872ab7d BS |
170 | goto error; |
171 | } | |
172 | ||
c93c014f RA |
173 | model = NULL; |
174 | for (i = 0; i < ARRAY_SIZE(models); i++) { | |
175 | if (!strcmp(g_match_info_fetch(match_info, 1), models[i].name)) { | |
176 | model = &models[i]; | |
177 | break; | |
178 | } | |
b872ab7d | 179 | } |
c93c014f RA |
180 | if (!model) { |
181 | sr_err("Unsupported model '%s'.", reply); | |
b872ab7d BS |
182 | goto error; |
183 | } | |
c93c014f RA |
184 | |
185 | sr_info("Detected model '%s'.", model->name); | |
186 | ||
187 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
188 | sdi->status = SR_ST_INACTIVE; | |
189 | sdi->vendor = g_strdup("GW Instek"); | |
190 | sdi->model = g_strdup(model->name); | |
191 | sdi->inst_type = SR_INST_SERIAL; | |
192 | sdi->conn = serial; | |
193 | ||
194 | for (i = 0; i < model->num_channels; i++) { | |
195 | snprintf(channel, sizeof(channel), "CH%d", i + 1); | |
196 | ch = sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE, channel); | |
d810901a | 197 | cg = sr_channel_group_new(sdi, channel, NULL); |
c93c014f | 198 | cg->channels = g_slist_append(NULL, ch); |
c93c014f RA |
199 | } |
200 | ||
201 | devc = g_malloc0(sizeof(struct dev_context)); | |
202 | sr_sw_limits_init(&devc->limits); | |
203 | devc->model = model; | |
204 | devc->config = g_malloc0(sizeof(struct per_channel_config) | |
205 | * model->num_channels); | |
206 | sdi->priv = devc; | |
207 | ||
208 | serial_flush(serial); | |
209 | gpd_send_cmd(serial, "STATUS?\n"); | |
b872ab7d | 210 | gpd_receive_reply(serial, reply, sizeof(reply)); |
c93c014f RA |
211 | |
212 | if (sscanf(reply, "%1u%1u%1u%1u%1u%1u%1u%1u", &cc_cv_ch1, | |
213 | &cc_cv_ch2, &track1, &track2, &beep, | |
214 | &devc->output_enabled, &baud1, &baud2) != 8) { | |
215 | /* old firmware (< 2.00?) responds with different format */ | |
216 | if (sscanf(reply, "%1u %1u %1u %1u %1u X %1u X", &cc_cv_ch1, | |
217 | &cc_cv_ch2, &track1, &track2, &beep, | |
218 | &devc->output_enabled) != 6) { | |
219 | sr_err("Invalid reply to STATUS: '%s'.", reply); | |
220 | goto error; | |
221 | } | |
222 | /* ignore remaining two lines of status message */ | |
223 | gpd_receive_reply(serial, reply, sizeof(reply)); | |
224 | gpd_receive_reply(serial, reply, sizeof(reply)); | |
b872ab7d | 225 | } |
b872ab7d | 226 | |
c93c014f RA |
227 | for (i = 0; i < model->num_channels; i++) { |
228 | gpd_send_cmd(serial, "ISET%d?\n", i + 1); | |
229 | gpd_receive_reply(serial, reply, sizeof(reply)); | |
230 | if (sscanf(reply, "%f", &devc->config[i].output_current_max) != 1) { | |
231 | sr_err("Invalid reply to ISETn?: '%s'.", reply); | |
232 | goto error; | |
233 | } | |
b872ab7d | 234 | |
c93c014f RA |
235 | gpd_send_cmd(serial, "VSET%d?\n", i + 1); |
236 | gpd_receive_reply(serial, reply, sizeof(reply)); | |
237 | if (sscanf(reply, "%f", &devc->config[i].output_voltage_max) != 1) { | |
238 | sr_err("Invalid reply to VSETn?: '%s'.", reply); | |
239 | goto error; | |
240 | } | |
241 | gpd_send_cmd(serial, "IOUT%d?\n", i + 1); | |
242 | gpd_receive_reply(serial, reply, sizeof(reply)); | |
243 | if (sscanf(reply, "%f", &devc->config[i].output_current_last) != 1) { | |
244 | sr_err("Invalid reply to IOUTn?: '%s'.", reply); | |
245 | goto error; | |
246 | } | |
247 | gpd_send_cmd(serial, "VOUT%d?\n", i + 1); | |
248 | gpd_receive_reply(serial, reply, sizeof(reply)); | |
249 | if (sscanf(reply, "%f", &devc->config[i].output_voltage_last) != 1) { | |
250 | sr_err("Invalid reply to VOUTn?: '%s'.", reply); | |
251 | goto error; | |
252 | } | |
253 | } | |
b872ab7d | 254 | |
c93c014f RA |
255 | return std_scan_complete(di, g_slist_append(NULL, sdi)); |
256 | ||
257 | error: | |
258 | if (match_info) | |
259 | g_match_info_free(match_info); | |
260 | if (regex) | |
261 | g_regex_unref(regex); | |
262 | if (serial) | |
263 | serial_close(serial); | |
264 | } | |
b872ab7d BS |
265 | |
266 | return NULL; | |
267 | } | |
268 | ||
269 | static int config_get(uint32_t key, GVariant **data, | |
270 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
271 | { | |
5ff77241 | 272 | int channel; |
b872ab7d BS |
273 | const struct dev_context *devc; |
274 | const struct sr_channel *ch; | |
b872ab7d BS |
275 | |
276 | if (!sdi) | |
277 | return SR_ERR_ARG; | |
278 | ||
279 | devc = sdi->priv; | |
280 | ||
281 | if (!cg) { | |
282 | switch (key) { | |
b81cfbc3 TK |
283 | case SR_CONF_LIMIT_SAMPLES: |
284 | case SR_CONF_LIMIT_MSEC: | |
285 | return sr_sw_limits_config_get(&devc->limits, key, data); | |
b872ab7d BS |
286 | case SR_CONF_CHANNEL_CONFIG: |
287 | *data = g_variant_new_string( | |
288 | channel_modes[devc->channel_mode]); | |
289 | break; | |
290 | case SR_CONF_ENABLED: | |
291 | *data = g_variant_new_boolean(devc->output_enabled); | |
292 | break; | |
293 | default: | |
294 | return SR_ERR_NA; | |
295 | } | |
296 | } else { | |
297 | ch = cg->channels->data; | |
298 | channel = ch->index; | |
b872ab7d BS |
299 | switch (key) { |
300 | case SR_CONF_VOLTAGE: | |
301 | *data = g_variant_new_double( | |
302 | devc->config[channel].output_voltage_last); | |
303 | break; | |
304 | case SR_CONF_VOLTAGE_TARGET: | |
305 | *data = g_variant_new_double( | |
306 | devc->config[channel].output_voltage_max); | |
307 | break; | |
308 | case SR_CONF_CURRENT: | |
309 | *data = g_variant_new_double( | |
310 | devc->config[channel].output_current_last); | |
311 | break; | |
312 | case SR_CONF_CURRENT_LIMIT: | |
313 | *data = g_variant_new_double( | |
314 | devc->config[channel].output_current_max); | |
315 | break; | |
316 | default: | |
317 | return SR_ERR_NA; | |
318 | } | |
319 | } | |
320 | ||
5ff77241 | 321 | return SR_OK; |
b872ab7d BS |
322 | } |
323 | ||
324 | static int config_set(uint32_t key, GVariant *data, | |
325 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
326 | { | |
327 | int ret, channel; | |
328 | const struct sr_channel *ch; | |
329 | double dval; | |
330 | gboolean bval; | |
331 | struct dev_context *devc; | |
332 | ||
333 | devc = sdi->priv; | |
b872ab7d BS |
334 | |
335 | ret = SR_OK; | |
336 | ||
337 | switch (key) { | |
338 | case SR_CONF_LIMIT_MSEC: | |
339 | case SR_CONF_LIMIT_SAMPLES: | |
340 | return sr_sw_limits_config_set(&devc->limits, key, data); | |
341 | case SR_CONF_ENABLED: | |
342 | bval = g_variant_get_boolean(data); | |
343 | gpd_send_cmd(sdi->conn, "OUT%c\n", bval ? '1' : '0'); | |
344 | devc->output_enabled = bval; | |
345 | break; | |
346 | case SR_CONF_VOLTAGE_TARGET: | |
347 | ch = cg->channels->data; | |
348 | channel = ch->index; | |
349 | dval = g_variant_get_double(data); | |
350 | if (dval < devc->model->channels[channel].voltage[0] | |
351 | || dval > devc->model->channels[channel].voltage[1]) | |
352 | return SR_ERR_ARG; | |
353 | gpd_send_cmd(sdi->conn, "VSET%d:%05.3lf\n", channel + 1, dval); | |
354 | devc->config[channel].output_voltage_max = dval; | |
355 | break; | |
356 | case SR_CONF_CURRENT_LIMIT: | |
357 | ch = cg->channels->data; | |
358 | channel = ch->index; | |
359 | dval = g_variant_get_double(data); | |
360 | if (dval < devc->model->channels[channel].current[0] | |
361 | || dval > devc->model->channels[channel].current[1]) | |
362 | return SR_ERR_ARG; | |
363 | gpd_send_cmd(sdi->conn, "ISET%d:%05.3lf\n", channel + 1, dval); | |
364 | devc->config[channel].output_current_max = dval; | |
365 | break; | |
366 | default: | |
367 | ret = SR_ERR_NA; | |
368 | break; | |
369 | } | |
370 | ||
371 | return ret; | |
372 | } | |
373 | ||
374 | static int config_list(uint32_t key, GVariant **data, | |
375 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
376 | { | |
377 | const struct dev_context *devc; | |
378 | const struct sr_channel *ch; | |
379 | int channel; | |
380 | ||
381 | devc = (sdi) ? sdi->priv : NULL; | |
382 | ||
383 | if (!cg) { | |
384 | switch (key) { | |
385 | case SR_CONF_SCAN_OPTIONS: | |
386 | case SR_CONF_DEVICE_OPTIONS: | |
387 | return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, | |
388 | drvopts, devopts); | |
389 | case SR_CONF_CHANNEL_CONFIG: | |
390 | *data = g_variant_new_strv(ARRAY_AND_SIZE(channel_modes)); | |
391 | break; | |
392 | default: | |
393 | return SR_ERR_NA; | |
394 | } | |
395 | } else { | |
396 | ch = cg->channels->data; | |
397 | channel = ch->index; | |
398 | ||
399 | switch (key) { | |
400 | case SR_CONF_DEVICE_OPTIONS: | |
401 | *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg)); | |
402 | break; | |
403 | case SR_CONF_VOLTAGE_TARGET: | |
404 | *data = std_gvar_min_max_step_array( | |
405 | devc->model->channels[channel].voltage); | |
406 | break; | |
407 | case SR_CONF_CURRENT_LIMIT: | |
408 | *data = std_gvar_min_max_step_array( | |
409 | devc->model->channels[channel].current); | |
410 | break; | |
411 | default: | |
412 | return SR_ERR_NA; | |
413 | } | |
414 | } | |
415 | ||
416 | return SR_OK; | |
417 | } | |
418 | ||
419 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) | |
420 | { | |
421 | struct dev_context *devc; | |
422 | struct sr_serial_dev_inst *serial; | |
423 | ||
b872ab7d BS |
424 | devc = sdi->priv; |
425 | ||
426 | sr_sw_limits_acquisition_start(&devc->limits); | |
427 | std_session_send_df_header(sdi); | |
428 | ||
429 | devc->reply_pending = FALSE; | |
430 | devc->req_sent_at = 0; | |
431 | serial = sdi->conn; | |
432 | serial_source_add(sdi->session, serial, G_IO_IN, 100, | |
433 | gpd_receive_data, (void *)sdi); | |
434 | ||
435 | return SR_OK; | |
436 | } | |
437 | ||
f1e82915 | 438 | static struct sr_dev_driver gwinstek_gpd_driver_info = { |
b872ab7d BS |
439 | .name = "gwinstek-gpd", |
440 | .longname = "GW Instek GPD", | |
441 | .api_version = 1, | |
442 | .init = std_init, | |
443 | .cleanup = std_cleanup, | |
444 | .scan = scan, | |
445 | .dev_list = std_dev_list, | |
446 | .dev_clear = std_dev_clear, | |
447 | .config_get = config_get, | |
448 | .config_set = config_set, | |
449 | .config_list = config_list, | |
450 | .dev_open = std_serial_dev_open, | |
451 | .dev_close = std_serial_dev_close, | |
452 | .dev_acquisition_start = dev_acquisition_start, | |
453 | .dev_acquisition_stop = std_serial_dev_acquisition_stop, | |
454 | .context = NULL, | |
455 | }; | |
456 | SR_REGISTER_DEV_DRIVER(gwinstek_gpd_driver_info); |