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 if (devc->acquisition_running)
325 case SR_CONF_OVER_CURRENT_PROTECTION:
326 bval = g_variant_get_boolean(data);
327 if (bval == devc->over_current_protection_set)
330 devc->over_current_protection_set = bval;
331 if (devc->acquisition_running)
338 /* Probe group specified: per-channel options. */
339 /* We only ever have one channel per probe group in this driver. */
340 probe = probe_group->probes->data;
341 channel = probe->index;
344 case SR_CONF_OUTPUT_VOLTAGE_MAX:
345 dval = g_variant_get_double(data);
346 if (dval < 0 || dval > devc->model->channels[channel].voltage[1])
348 devc->config[channel].output_voltage_max = dval;
349 if (devc->acquisition_running)
352 case SR_CONF_OUTPUT_CURRENT_MAX:
353 dval = g_variant_get_double(data);
354 if (dval < 0 || dval > devc->model->channels[channel].current[1])
356 devc->config[channel].output_current_max = dval;
357 if (devc->acquisition_running)
360 case SR_CONF_OUTPUT_ENABLED:
361 bval = g_variant_get_boolean(data);
362 if (bval == devc->config[channel].output_enabled_set)
365 devc->config[channel].output_enabled_set = bval;
366 if (devc->acquisition_running)
378 static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
379 const struct sr_probe_group *probe_group)
381 struct dev_context *devc;
382 struct sr_probe *probe;
387 /* Always available, even without sdi. */
388 if (key == SR_CONF_SCAN_OPTIONS) {
389 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
390 hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
395 if (key == SR_CONF_DEVICE_OPTIONS) {
396 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
397 devopts, ARRAY_SIZE(devopts), sizeof(int32_t));
401 /* Everything else needs an sdi. */
409 /* No probe group: global options. */
411 case SR_CONF_DEVICE_OPTIONS:
412 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
413 devopts_sdi, ARRAY_SIZE(devopts_sdi), sizeof(int32_t));
415 case SR_CONF_OUTPUT_CHANNEL:
416 if (devc->model->channel_modes == CHANMODE_INDEPENDENT) {
417 /* The 1-channel models. */
418 *data = g_variant_new_strv(channel_modes, 1);
420 /* The other models support all modes. */
421 *data = g_variant_new_strv(channel_modes, ARRAY_SIZE(channel_modes));
428 /* Probe group specified: per-channel options. */
431 /* We only ever have one channel per probe group in this driver. */
432 probe = probe_group->probes->data;
433 channel = probe->index;
436 case SR_CONF_DEVICE_OPTIONS:
437 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
438 devopts_pg, ARRAY_SIZE(devopts_pg), sizeof(int32_t));
440 case SR_CONF_OUTPUT_VOLTAGE_MAX:
441 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
442 /* Min, max, step. */
443 for (i = 0; i < 3; i++) {
444 gvar = g_variant_new_double(devc->model->channels[channel].voltage[i]);
445 g_variant_builder_add_value(&gvb, gvar);
447 *data = g_variant_builder_end(&gvb);
449 case SR_CONF_OUTPUT_CURRENT_MAX:
450 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
451 /* Min, max, step. */
452 for (i = 0; i < 3; i++) {
453 gvar = g_variant_new_double(devc->model->channels[channel].current[i]);
454 g_variant_builder_add_value(&gvb, gvar);
456 *data = g_variant_builder_end(&gvb);
466 static int dev_acquisition_start(const struct sr_dev_inst *sdi,
469 struct dev_context *devc;
470 struct sr_serial_dev_inst *serial;
471 uint8_t packet[PACKET_SIZE];
475 if (sdi->status != SR_ST_ACTIVE)
476 return SR_ERR_DEV_CLOSED;
479 memset(devc->packet, 0x44, PACKET_SIZE);
480 devc->packet_size = 0;
482 devc->acquisition_running = TRUE;
485 serial_source_add(serial, G_IO_IN, 50, atten_pps3xxx_receive_data, (void *)sdi);
486 std_session_send_df_header(cb_data, LOG_PREFIX);
488 /* Send a "probe" configuration packet now. */
489 memset(packet, 0, PACKET_SIZE);
492 send_packet(sdi, packet);
497 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
499 struct dev_context *devc;
503 if (sdi->status != SR_ST_ACTIVE)
504 return SR_ERR_DEV_CLOSED;
507 devc->acquisition_running = FALSE;
512 SR_PRIV struct sr_dev_driver atten_pps3203_driver_info = {
513 .name = "atten-pps3203",
514 .longname = "Atten PPS3203T-3S",
519 .dev_list = dev_list,
520 .dev_clear = dev_clear,
521 .config_get = config_get,
522 .config_set = config_set,
523 .config_list = config_list,
524 .dev_open = std_serial_dev_open,
525 .dev_close = std_serial_dev_close,
526 .dev_acquisition_start = dev_acquisition_start,
527 .dev_acquisition_stop = dev_acquisition_stop,