2 * This file is part of the libsigrok project.
4 * Copyright (C) 2014 Bert Vermeulen <bert@biot.com>
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.
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.
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/>.
25 * The default serial communication settings on the device are 9600
26 * baud, 9 data bits. The 9th bit isn't actually used, and the vendor
27 * software uses Mark parity to absorb the extra bit.
29 * Since 9 data bits is not a standard available in POSIX, we use two
30 * stop bits to skip over the extra bit instead.
32 #define SERIALCOMM "9600/8n2"
34 static const int32_t hwopts[] = {
39 static const int32_t devopts[] = {
44 static const int32_t devopts_sdi[] = {
47 SR_CONF_OUTPUT_CHANNEL,
48 SR_CONF_OVER_CURRENT_PROTECTION,
51 static const int32_t devopts_pg[] = {
54 SR_CONF_OUTPUT_VOLTAGE,
55 SR_CONF_OUTPUT_VOLTAGE_MAX,
56 SR_CONF_OUTPUT_CURRENT,
57 SR_CONF_OUTPUT_CURRENT_MAX,
58 SR_CONF_OUTPUT_ENABLED,
61 static const char *channel_modes[] = {
67 static struct pps_model models[] = {
68 { PPS_3203T_3S, "PPS3203T-3S",
69 CHANMODE_INDEPENDENT | CHANMODE_SERIES | CHANMODE_PARALLEL,
73 { { 0, 32, 0.01 }, { 0, 3, 0.001 } },
75 { { 0, 32, 0.01 }, { 0, 3, 0.001 } },
77 { { 0, 6, 0.01 }, { 0, 3, 0.001 } },
83 SR_PRIV struct sr_dev_driver atten_pps3203_driver_info;
84 static struct sr_dev_driver *di = &atten_pps3203_driver_info;
86 static int init(struct sr_context *sr_ctx)
88 return std_init(sr_ctx, di, LOG_PREFIX);
91 static GSList *scan(GSList *options, int modelid)
93 struct sr_dev_inst *sdi;
94 struct drv_context *drvc;
95 struct dev_context *devc;
96 struct sr_config *src;
97 struct sr_probe *probe;
98 struct sr_probe_group *pg;
99 struct sr_serial_dev_inst *serial;
101 struct pps_model *model;
102 uint8_t packet[PACKET_SIZE];
105 const char *conn, *serialcomm;
110 drvc->instances = NULL;
112 conn = serialcomm = NULL;
113 for (l = options; l; l = l->next) {
117 conn = g_variant_get_string(src->data, NULL);
119 case SR_CONF_SERIALCOMM:
120 serialcomm = g_variant_get_string(src->data, NULL);
127 serialcomm = SERIALCOMM;
129 if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
132 if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
134 serial_flush(serial);
136 /* This is how the vendor software probes for hardware. */
137 memset(packet, 0, PACKET_SIZE);
140 if (serial_write(serial, packet, PACKET_SIZE) == -1) {
141 sr_err("Unable to write while probing for hardware: %s",
145 /* The device responds with a 24-byte packet when it receives a packet.
146 * At 9600 baud, 300ms is long enough for it to have arrived. */
147 g_usleep(300 * 1000);
148 memset(packet, 0, PACKET_SIZE);
149 if ((ret = serial_read_nonblocking(serial, packet, PACKET_SIZE)) < 0) {
150 sr_err("Unable to read while probing for hardware: %s",
154 if (ret != PACKET_SIZE || packet[0] != 0xaa || packet[1] != 0xaa) {
155 /* Doesn't look like an Atten PPS. */
160 for (i = 0; i < ARRAY_SIZE(models); i++) {
161 if (models[i].modelid == modelid) {
167 sr_err("Unknown modelid %d", modelid);
171 sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Atten", model->name, NULL);
173 sdi->inst_type = SR_INST_SERIAL;
175 for (i = 0; i < MAX_CHANNELS; i++) {
176 snprintf(channel, 10, "CH%d", i + 1);
177 probe = sr_probe_new(i, SR_PROBE_ANALOG, TRUE, channel);
178 sdi->probes = g_slist_append(sdi->probes, probe);
179 pg = g_malloc(sizeof(struct sr_probe_group));
180 pg->name = g_strdup(channel);
181 pg->probes = g_slist_append(NULL, probe);
183 sdi->probe_groups = g_slist_append(sdi->probe_groups, pg);
186 devc = g_malloc0(sizeof(struct dev_context));
188 devc->config = g_malloc0(sizeof(struct per_channel_config) * model->num_channels);
190 drvc->instances = g_slist_append(drvc->instances, sdi);
191 devices = g_slist_append(devices, sdi);
193 serial_close(serial);
195 sr_serial_dev_inst_free(serial);
200 static GSList *scan_3203(GSList *options)
202 return scan(options, PPS_3203T_3S);
205 static GSList *dev_list(void)
207 return ((struct drv_context *)(di->priv))->instances;
210 static int dev_clear(void)
212 return std_dev_clear(di, NULL);
215 static int cleanup(void)
220 static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
221 const struct sr_probe_group *probe_group)
223 struct dev_context *devc;
224 struct sr_probe *probe;
234 /* No probe group: global options. */
236 case SR_CONF_OUTPUT_CHANNEL:
237 *data = g_variant_new_string(channel_modes[devc->channel_mode - 1]);
239 case SR_CONF_OVER_CURRENT_PROTECTION:
240 *data = g_variant_new_boolean(devc->over_current_protection);
246 /* We only ever have one channel per probe group in this driver. */
247 probe = probe_group->probes->data;
248 channel = probe->index;
251 case SR_CONF_OUTPUT_VOLTAGE:
252 *data = g_variant_new_double(devc->config[channel].output_voltage_last);
254 case SR_CONF_OUTPUT_VOLTAGE_MAX:
255 *data = g_variant_new_double(devc->config[channel].output_voltage_max);
257 case SR_CONF_OUTPUT_CURRENT:
258 *data = g_variant_new_double(devc->config[channel].output_current_last);
260 case SR_CONF_OUTPUT_CURRENT_MAX:
261 *data = g_variant_new_double(devc->config[channel].output_current_max);
263 case SR_CONF_OUTPUT_ENABLED:
264 *data = g_variant_new_boolean(devc->config[channel].output_enabled);
274 static int find_str(const char *str, const char **strings, int array_size)
279 for (i = 0; i < array_size; i++) {
280 if (!strcmp(str, strings[i])) {
289 static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi,
290 const struct sr_probe_group *probe_group)
292 struct dev_context *devc;
293 struct sr_probe *probe;
295 int channel, ret, ival;
299 if (sdi->status != SR_ST_ACTIVE)
300 return SR_ERR_DEV_CLOSED;
305 /* No probe group: global options. */
307 case SR_CONF_OUTPUT_CHANNEL:
308 sval = g_variant_get_string(data, NULL);
309 if ((ival = find_str(sval, channel_modes,
310 ARRAY_SIZE(channel_modes))) == -1) {
314 if (devc->model->channel_modes && (1 << ival) == 0) {
315 /* Not supported on this model. */
318 if (ival == devc->channel_mode_set)
321 devc->channel_mode_set = ival;
322 devc->config_dirty = TRUE;
324 case SR_CONF_OVER_CURRENT_PROTECTION:
325 bval = g_variant_get_boolean(data);
326 if (bval == devc->over_current_protection_set)
329 devc->over_current_protection_set = bval;
330 devc->config_dirty = TRUE;
336 /* Probe group specified: per-channel options. */
337 /* We only ever have one channel per probe group in this driver. */
338 probe = probe_group->probes->data;
339 channel = probe->index;
342 case SR_CONF_OUTPUT_VOLTAGE_MAX:
343 dval = g_variant_get_double(data);
344 if (dval < 0 || dval > devc->model->channels[channel].voltage[1])
346 devc->config[channel].output_voltage_max = dval;
347 devc->config_dirty = TRUE;
349 case SR_CONF_OUTPUT_CURRENT_MAX:
350 dval = g_variant_get_double(data);
351 if (dval < 0 || dval > devc->model->channels[channel].current[1])
353 devc->config[channel].output_current_max = dval;
354 devc->config_dirty = TRUE;
356 case SR_CONF_OUTPUT_ENABLED:
357 bval = g_variant_get_boolean(data);
358 if (bval == devc->config[channel].output_enabled_set)
361 devc->config[channel].output_enabled_set = bval;
362 devc->config_dirty = TRUE;
373 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
374 const struct sr_probe_group *probe_group)
376 struct dev_context *devc;
377 struct sr_probe *probe;
382 /* Always available, even without sdi. */
383 if (key == SR_CONF_SCAN_OPTIONS) {
384 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
385 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
390 if (key == SR_CONF_DEVICE_OPTIONS) {
391 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
392 devopts, ARRAY_SIZE(devopts), sizeof(int32_t));
396 /* Everything else needs an sdi. */
404 /* No probe group: global options. */
406 case SR_CONF_DEVICE_OPTIONS:
407 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
408 devopts_sdi, ARRAY_SIZE(devopts_sdi), sizeof(int32_t));
410 case SR_CONF_OUTPUT_CHANNEL:
411 if (devc->model->channel_modes == CHANMODE_INDEPENDENT) {
412 /* The 1-channel models. */
413 *data = g_variant_new_strv(channel_modes, 1);
415 /* The other models support all modes. */
416 *data = g_variant_new_strv(channel_modes, ARRAY_SIZE(channel_modes));
423 /* Probe group specified: per-channel options. */
426 /* We only ever have one channel per probe group in this driver. */
427 probe = probe_group->probes->data;
428 channel = probe->index;
431 case SR_CONF_DEVICE_OPTIONS:
432 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
433 devopts_pg, ARRAY_SIZE(devopts_pg), sizeof(int32_t));
435 case SR_CONF_OUTPUT_VOLTAGE_MAX:
436 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
437 /* Min, max, step. */
438 for (i = 0; i < 3; i++) {
439 gvar = g_variant_new_double(devc->model->channels[channel].voltage[i]);
440 g_variant_builder_add_value(&gvb, gvar);
442 *data = g_variant_builder_end(&gvb);
444 case SR_CONF_OUTPUT_CURRENT_MAX:
445 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
446 /* Min, max, step. */
447 for (i = 0; i < 3; i++) {
448 gvar = g_variant_new_double(devc->model->channels[channel].current[i]);
449 g_variant_builder_add_value(&gvb, gvar);
451 *data = g_variant_builder_end(&gvb);
461 static int dev_close(struct sr_dev_inst *sdi)
463 struct dev_context *devc;
466 if (devc->config_dirty)
467 /* Some configuration changes were queued up but didn't
468 * get sent to the device, likely because we were never
469 * in acquisition mode. Send them out now. */
472 return std_serial_dev_close(sdi);
475 static int dev_acquisition_start(const struct sr_dev_inst *sdi,
478 struct dev_context *devc;
479 struct sr_serial_dev_inst *serial;
480 uint8_t packet[PACKET_SIZE];
484 if (sdi->status != SR_ST_ACTIVE)
485 return SR_ERR_DEV_CLOSED;
488 memset(devc->packet, 0x44, PACKET_SIZE);
489 devc->packet_size = 0;
491 devc->acquisition_running = TRUE;
494 serial_source_add(serial, G_IO_IN, 50, atten_pps3xxx_receive_data, (void *)sdi);
495 std_session_send_df_header(cb_data, LOG_PREFIX);
497 /* Send a "probe" configuration packet now. */
498 memset(packet, 0, PACKET_SIZE);
501 send_packet(sdi, packet);
506 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
508 struct dev_context *devc;
512 if (sdi->status != SR_ST_ACTIVE)
513 return SR_ERR_DEV_CLOSED;
516 devc->acquisition_running = FALSE;
521 SR_PRIV struct sr_dev_driver atten_pps3203_driver_info = {
522 .name = "atten-pps3203",
523 .longname = "Atten PPS3203T-3S",
528 .dev_list = dev_list,
529 .dev_clear = dev_clear,
530 .config_get = config_get,
531 .config_set = config_set,
532 .config_list = config_list,
533 .dev_open = std_serial_dev_open,
534 .dev_close = dev_close,
535 .dev_acquisition_start = dev_acquisition_start,
536 .dev_acquisition_stop = dev_acquisition_stop,