]>
Commit | Line | Data |
---|---|---|
4433145f UH |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de> | |
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 2 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, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
6ec6c43b | 21 | #include <config.h> |
4433145f UH |
22 | #include "protocol.h" |
23 | ||
a0e0bb41 | 24 | static const uint32_t scanopts[] = { |
4a8bbed7 UH |
25 | SR_CONF_CONN, |
26 | SR_CONF_SERIALCOMM, | |
27 | }; | |
28 | ||
6685e9a6 | 29 | static const uint32_t drvopts[] = { |
4a8bbed7 | 30 | SR_CONF_THERMOMETER, |
6685e9a6 UH |
31 | }; |
32 | ||
33 | static const uint32_t devopts[] = { | |
e91bb0a6 | 34 | SR_CONF_CONTINUOUS, |
5827f61b BV |
35 | SR_CONF_LIMIT_SAMPLES | SR_CONF_SET, |
36 | SR_CONF_LIMIT_MSEC | SR_CONF_SET, | |
4a8bbed7 UH |
37 | }; |
38 | ||
ba7dd8bb | 39 | static const char *channel_names[] = { |
4a8bbed7 | 40 | "T1", "T2", "T3", "T4", |
4a8bbed7 UH |
41 | }; |
42 | ||
43 | SR_PRIV struct sr_dev_driver center_309_driver_info; | |
44 | SR_PRIV struct sr_dev_driver voltcraft_k204_driver_info; | |
45 | ||
46 | SR_PRIV const struct center_dev_info center_devs[] = { | |
47 | { | |
48 | "Center", "309", "9600/8n1", 4, 32000, 45, | |
49 | center_3xx_packet_valid, | |
50 | ¢er_309_driver_info, receive_data_CENTER_309, | |
51 | }, | |
52 | { | |
53 | "Voltcraft", "K204", "9600/8n1", 4, 32000, 45, | |
54 | center_3xx_packet_valid, | |
55 | &voltcraft_k204_driver_info, receive_data_VOLTCRAFT_K204, | |
56 | }, | |
57 | }; | |
4433145f | 58 | |
4a8bbed7 | 59 | static int init(struct sr_context *sr_ctx, int idx) |
4433145f | 60 | { |
c45c32ce | 61 | return std_init(center_devs[idx].di, sr_ctx); |
4a8bbed7 UH |
62 | } |
63 | ||
64 | static GSList *center_scan(const char *conn, const char *serialcomm, int idx) | |
65 | { | |
66 | int i; | |
67 | struct sr_dev_inst *sdi; | |
4433145f | 68 | struct drv_context *drvc; |
4a8bbed7 | 69 | struct dev_context *devc; |
4a8bbed7 | 70 | struct sr_serial_dev_inst *serial; |
4433145f UH |
71 | GSList *devices; |
72 | ||
91219afc | 73 | serial = sr_serial_dev_inst_new(conn, serialcomm); |
4a8bbed7 | 74 | |
258a2313 | 75 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) |
4a8bbed7 | 76 | return NULL; |
4433145f | 77 | |
41812aca | 78 | drvc = center_devs[idx].di->context; |
4433145f | 79 | devices = NULL; |
4a8bbed7 UH |
80 | serial_flush(serial); |
81 | ||
82 | sr_info("Found device on port %s.", conn); | |
83 | ||
aac29cc1 | 84 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); |
0af636be UH |
85 | sdi->status = SR_ST_INACTIVE; |
86 | sdi->vendor = g_strdup(center_devs[idx].vendor); | |
87 | sdi->model = g_strdup(center_devs[idx].device); | |
f57d8ffe | 88 | devc = g_malloc0(sizeof(struct dev_context)); |
4a8bbed7 UH |
89 | sdi->inst_type = SR_INST_SERIAL; |
90 | sdi->conn = serial; | |
4a8bbed7 UH |
91 | sdi->priv = devc; |
92 | sdi->driver = center_devs[idx].di; | |
93 | ||
0f34cb47 UH |
94 | for (i = 0; i < center_devs[idx].num_channels; i++) |
95 | sr_channel_new(sdi, i, SR_CHANNEL_ANALOG, TRUE, channel_names[i]); | |
4a8bbed7 UH |
96 | |
97 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
98 | devices = g_slist_append(devices, sdi); | |
99 | ||
4a8bbed7 | 100 | serial_close(serial); |
4433145f UH |
101 | |
102 | return devices; | |
103 | } | |
104 | ||
4a8bbed7 | 105 | static GSList *scan(GSList *options, int idx) |
4433145f | 106 | { |
4a8bbed7 UH |
107 | struct sr_config *src; |
108 | GSList *l, *devices; | |
109 | const char *conn, *serialcomm; | |
110 | ||
111 | conn = serialcomm = NULL; | |
112 | for (l = options; l; l = l->next) { | |
113 | src = l->data; | |
114 | switch (src->key) { | |
115 | case SR_CONF_CONN: | |
116 | conn = g_variant_get_string(src->data, NULL); | |
117 | break; | |
118 | case SR_CONF_SERIALCOMM: | |
119 | serialcomm = g_variant_get_string(src->data, NULL); | |
120 | break; | |
121 | } | |
122 | } | |
123 | if (!conn) | |
124 | return NULL; | |
125 | ||
126 | if (serialcomm) { | |
127 | /* Use the provided comm specs. */ | |
128 | devices = center_scan(conn, serialcomm, idx); | |
129 | } else { | |
130 | /* Try the default. */ | |
131 | devices = center_scan(conn, center_devs[idx].conn, idx); | |
132 | } | |
133 | ||
134 | return devices; | |
4433145f UH |
135 | } |
136 | ||
584560f1 | 137 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, |
53b4680f | 138 | const struct sr_channel_group *cg) |
4433145f | 139 | { |
4a8bbed7 | 140 | struct dev_context *devc; |
4433145f | 141 | |
53b4680f | 142 | (void)cg; |
d3c74a6f | 143 | |
4433145f UH |
144 | if (sdi->status != SR_ST_ACTIVE) |
145 | return SR_ERR_DEV_CLOSED; | |
146 | ||
4a8bbed7 UH |
147 | devc = sdi->priv; |
148 | ||
838f6906 | 149 | return sr_sw_limits_config_set(&devc->sw_limits, key, data); |
4433145f UH |
150 | } |
151 | ||
584560f1 | 152 | static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, |
53b4680f | 153 | const struct sr_channel_group *cg) |
4433145f | 154 | { |
53b4680f | 155 | (void)cg; |
4433145f | 156 | |
4433145f | 157 | switch (key) { |
4a8bbed7 | 158 | case SR_CONF_SCAN_OPTIONS: |
584560f1 | 159 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, |
a0e0bb41 | 160 | scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t)); |
4a8bbed7 UH |
161 | break; |
162 | case SR_CONF_DEVICE_OPTIONS: | |
6685e9a6 UH |
163 | if (!sdi) |
164 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
165 | drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t)); | |
166 | else | |
167 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
f254bc4b | 168 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); |
4a8bbed7 | 169 | break; |
4433145f UH |
170 | default: |
171 | return SR_ERR_NA; | |
172 | } | |
173 | ||
4a8bbed7 | 174 | return SR_OK; |
4433145f UH |
175 | } |
176 | ||
695dc859 | 177 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, int idx) |
4433145f | 178 | { |
4a8bbed7 UH |
179 | struct dev_context *devc; |
180 | struct sr_serial_dev_inst *serial; | |
4433145f UH |
181 | |
182 | if (sdi->status != SR_ST_ACTIVE) | |
183 | return SR_ERR_DEV_CLOSED; | |
184 | ||
4a8bbed7 | 185 | devc = sdi->priv; |
838f6906 LPC |
186 | |
187 | sr_sw_limits_acquisition_start(&devc->sw_limits); | |
4a8bbed7 | 188 | |
695dc859 | 189 | std_session_send_df_header(sdi, LOG_PREFIX); |
4a8bbed7 UH |
190 | |
191 | /* Poll every 500ms, or whenever some data comes in. */ | |
192 | serial = sdi->conn; | |
102f1239 | 193 | serial_source_add(sdi->session, serial, G_IO_IN, 500, |
4a8bbed7 UH |
194 | center_devs[idx].receive_data, (void *)sdi); |
195 | ||
4433145f UH |
196 | return SR_OK; |
197 | } | |
198 | ||
695dc859 | 199 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) |
4433145f | 200 | { |
6525d819 | 201 | return std_serial_dev_acquisition_stop(sdi, |
d43b0908 | 202 | std_serial_dev_close, sdi->conn, LOG_PREFIX); |
4433145f UH |
203 | } |
204 | ||
4a8bbed7 UH |
205 | /* Driver-specific API function wrappers */ |
206 | #define HW_INIT(X) \ | |
4f840ce9 | 207 | static int init_##X(struct sr_dev_driver *d, \ |
1a863916 | 208 | struct sr_context *sr_ctx) { (void)d; return init(sr_ctx, X); } |
4a8bbed7 | 209 | #define HW_SCAN(X) \ |
4f840ce9 | 210 | static GSList *scan_##X(struct sr_dev_driver *d, GSList *options) { \ |
1a863916 | 211 | (void)d; return scan(options, X); } |
4a8bbed7 | 212 | #define HW_DEV_ACQUISITION_START(X) \ |
695dc859 UH |
213 | static int dev_acquisition_start_##X(const struct sr_dev_inst *sdi \ |
214 | ) { return dev_acquisition_start(sdi, X); } | |
4a8bbed7 UH |
215 | |
216 | /* Driver structs and API function wrappers */ | |
217 | #define DRV(ID, ID_UPPER, NAME, LONGNAME) \ | |
218 | HW_INIT(ID_UPPER) \ | |
4a8bbed7 | 219 | HW_SCAN(ID_UPPER) \ |
4a8bbed7 UH |
220 | HW_DEV_ACQUISITION_START(ID_UPPER) \ |
221 | SR_PRIV struct sr_dev_driver ID##_driver_info = { \ | |
222 | .name = NAME, \ | |
223 | .longname = LONGNAME, \ | |
224 | .api_version = 1, \ | |
225 | .init = init_##ID_UPPER, \ | |
700d6b64 | 226 | .cleanup = std_cleanup, \ |
4a8bbed7 | 227 | .scan = scan_##ID_UPPER, \ |
c01bf34c | 228 | .dev_list = std_dev_list, \ |
4a8bbed7 UH |
229 | .config_get = NULL, \ |
230 | .config_set = config_set, \ | |
231 | .config_list = config_list, \ | |
854434de | 232 | .dev_open = std_serial_dev_open, \ |
bf2c987f | 233 | .dev_close = std_serial_dev_close, \ |
4a8bbed7 UH |
234 | .dev_acquisition_start = dev_acquisition_start_##ID_UPPER, \ |
235 | .dev_acquisition_stop = dev_acquisition_stop, \ | |
41812aca | 236 | .context = NULL, \ |
4433145f | 237 | }; |
4a8bbed7 UH |
238 | |
239 | DRV(center_309, CENTER_309, "center-309", "Center 309") | |
240 | DRV(voltcraft_k204, VOLTCRAFT_K204, "voltcraft-k204", "Voltcraft K204") |