2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
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 2 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, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 static const uint32_t scanopts[] = {
29 static const uint32_t drvopts[] = {
33 static const uint32_t devopts[] = {
34 SR_CONF_CONTINUOUS | SR_CONF_SET,
35 SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
36 SR_CONF_LIMIT_MSEC | SR_CONF_SET,
39 static const char *channel_names[] = {
40 "T1", "T2", "T3", "T4",
43 SR_PRIV struct sr_dev_driver center_309_driver_info;
44 SR_PRIV struct sr_dev_driver voltcraft_k204_driver_info;
46 SR_PRIV const struct center_dev_info center_devs[] = {
48 "Center", "309", "9600/8n1", 4, 32000, 45,
49 center_3xx_packet_valid,
50 ¢er_309_driver_info, receive_data_CENTER_309,
53 "Voltcraft", "K204", "9600/8n1", 4, 32000, 45,
54 center_3xx_packet_valid,
55 &voltcraft_k204_driver_info, receive_data_VOLTCRAFT_K204,
59 static int init(struct sr_context *sr_ctx, int idx)
61 return std_init(sr_ctx, center_devs[idx].di, LOG_PREFIX);
64 static GSList *center_scan(const char *conn, const char *serialcomm, int idx)
67 struct sr_dev_inst *sdi;
68 struct drv_context *drvc;
69 struct dev_context *devc;
70 struct sr_serial_dev_inst *serial;
73 serial = sr_serial_dev_inst_new(conn, serialcomm);
75 if (serial_open(serial, SERIAL_RDWR) != SR_OK)
78 drvc = center_devs[idx].di->context;
82 sr_info("Found device on port %s.", conn);
84 sdi = g_malloc0(sizeof(struct sr_dev_inst));
85 sdi->status = SR_ST_INACTIVE;
86 sdi->vendor = g_strdup(center_devs[idx].vendor);
87 sdi->model = g_strdup(center_devs[idx].device);
88 devc = g_malloc0(sizeof(struct dev_context));
89 sdi->inst_type = SR_INST_SERIAL;
92 sdi->driver = center_devs[idx].di;
94 for (i = 0; i < center_devs[idx].num_channels; i++)
95 sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE, channel_names[i]);
97 drvc->instances = g_slist_append(drvc->instances, sdi);
98 devices = g_slist_append(devices, sdi);
100 serial_close(serial);
105 static GSList *scan(GSList *options, int idx)
107 struct sr_config *src;
109 const char *conn, *serialcomm;
111 conn = serialcomm = NULL;
112 for (l = options; l; l = l->next) {
116 conn = g_variant_get_string(src->data, NULL);
118 case SR_CONF_SERIALCOMM:
119 serialcomm = g_variant_get_string(src->data, NULL);
127 /* Use the provided comm specs. */
128 devices = center_scan(conn, serialcomm, idx);
130 /* Try the default. */
131 devices = center_scan(conn, center_devs[idx].conn, idx);
137 static GSList *dev_list(int idx)
139 return ((struct drv_context *)(center_devs[idx].di->context))->instances;
142 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
143 const struct sr_channel_group *cg)
145 struct dev_context *devc;
149 if (sdi->status != SR_ST_ACTIVE)
150 return SR_ERR_DEV_CLOSED;
155 case SR_CONF_LIMIT_SAMPLES:
156 if (g_variant_get_uint64(data) == 0)
158 devc->limit_samples = g_variant_get_uint64(data);
160 case SR_CONF_LIMIT_MSEC:
161 if (g_variant_get_uint64(data) == 0)
163 devc->limit_msec = g_variant_get_uint64(data);
172 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
173 const struct sr_channel_group *cg)
178 case SR_CONF_SCAN_OPTIONS:
179 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
180 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
182 case SR_CONF_DEVICE_OPTIONS:
184 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
185 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
187 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
188 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
197 static int dev_acquisition_start(const struct sr_dev_inst *sdi, int idx)
199 struct dev_context *devc;
200 struct sr_serial_dev_inst *serial;
202 if (sdi->status != SR_ST_ACTIVE)
203 return SR_ERR_DEV_CLOSED;
206 devc->num_samples = 0;
207 devc->starttime = g_get_monotonic_time();
209 std_session_send_df_header(sdi, LOG_PREFIX);
211 /* Poll every 500ms, or whenever some data comes in. */
213 serial_source_add(sdi->session, serial, G_IO_IN, 500,
214 center_devs[idx].receive_data, (void *)sdi);
219 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
221 return std_serial_dev_acquisition_stop(sdi,
222 std_serial_dev_close, sdi->conn, LOG_PREFIX);
225 /* Driver-specific API function wrappers */
227 static int init_##X(struct sr_dev_driver *d, \
228 struct sr_context *sr_ctx) { (void)d; return init(sr_ctx, X); }
230 static GSList *scan_##X(struct sr_dev_driver *d, GSList *options) { \
231 (void)d; return scan(options, X); }
232 #define HW_DEV_LIST(X) \
233 static GSList *dev_list_##X(const struct sr_dev_driver *d) { \
234 (void)d; return dev_list(X); }
235 #define HW_DEV_ACQUISITION_START(X) \
236 static int dev_acquisition_start_##X(const struct sr_dev_inst *sdi \
237 ) { return dev_acquisition_start(sdi, X); }
239 /* Driver structs and API function wrappers */
240 #define DRV(ID, ID_UPPER, NAME, LONGNAME) \
243 HW_DEV_LIST(ID_UPPER) \
244 HW_DEV_ACQUISITION_START(ID_UPPER) \
245 SR_PRIV struct sr_dev_driver ID##_driver_info = { \
247 .longname = LONGNAME, \
249 .init = init_##ID_UPPER, \
250 .cleanup = std_cleanup, \
251 .scan = scan_##ID_UPPER, \
252 .dev_list = dev_list_##ID_UPPER, \
253 .config_get = NULL, \
254 .config_set = config_set, \
255 .config_list = config_list, \
256 .dev_open = std_serial_dev_open, \
257 .dev_close = std_serial_dev_close, \
258 .dev_acquisition_start = dev_acquisition_start_##ID_UPPER, \
259 .dev_acquisition_stop = dev_acquisition_stop, \
263 DRV(center_309, CENTER_309, "center-309", "Center 309")
264 DRV(voltcraft_k204, VOLTCRAFT_K204, "voltcraft-k204", "Voltcraft K204")