]>
Commit | Line | Data |
---|---|---|
8f4e922f UH |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2014 Uwe Hermann <uwe@hermann-uwe.de> | |
25abc8dd | 5 | * Copyright (C) 2014 Matthias Heidbrink <m-sigrok@heidbrink.biz> |
8f4e922f UH |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation; either version 2 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
20 | */ | |
21 | ||
22 | #include "protocol.h" | |
23 | ||
b5e92647 UH |
24 | static const int32_t hwopts[] = { |
25 | SR_CONF_CONN, | |
26 | SR_CONF_SERIALCOMM, | |
27 | }; | |
28 | ||
29 | static const int32_t devopts[] = { | |
30 | SR_CONF_POWER_SUPPLY, | |
31 | SR_CONF_LIMIT_SAMPLES, | |
32 | SR_CONF_LIMIT_MSEC, | |
33 | SR_CONF_CONTINUOUS, | |
34 | SR_CONF_OUTPUT_VOLTAGE, | |
35 | SR_CONF_OUTPUT_CURRENT, | |
36 | }; | |
37 | ||
38 | /* Note: All models have one power supply output only. */ | |
39 | static struct hcs_model models[] = { | |
25abc8dd MH |
40 | { MANSON_HCS_3100, "HCS-3100", "3100", { 1, 18, 0.1 }, { 0, 10, 0.10 } }, |
41 | { MANSON_HCS_3102, "HCS-3102", "3102", { 1, 36, 0.1 }, { 0, 5, 0.01 } }, | |
42 | { MANSON_HCS_3104, "HCS-3104", "3104", { 1, 60, 0.1 }, { 0, 2.5, 0.01 } }, | |
43 | { MANSON_HCS_3150, "HCS-3150", "3150", { 1, 18, 0.1 }, { 0, 15, 0.10 } }, | |
44 | { MANSON_HCS_3200, "HCS-3200", "3200", { 1, 18, 0.1 }, { 0, 20, 0.10 } }, | |
45 | { MANSON_HCS_3202, "HCS-3202", "3202", { 1, 36, 0.1 }, { 0, 10, 0.10 } }, | |
46 | { MANSON_HCS_3204, "HCS-3204", "3204", { 1, 60, 0.1 }, { 0, 5, 0.01 } }, | |
47 | { MANSON_HCS_3300, "HCS-3300-USB", "3300", { 1, 16, 0.1 }, { 0, 30, 0.10 } }, | |
48 | { MANSON_HCS_3302, "HCS-3302-USB", "3302", { 1, 32, 0.1 }, { 0, 15, 0.10 } }, | |
49 | { MANSON_HCS_3304, "HCS-3304-USB", "3304", { 1, 60, 0.1 }, { 0, 8, 0.10 } }, | |
50 | { MANSON_HCS_3400, "HCS-3400-USB", "3400", { 1, 16, 0.1 }, { 0, 40, 0.10 } }, | |
51 | { MANSON_HCS_3402, "HCS-3402-USB", "3402", { 1, 32, 0.1 }, { 0, 20, 0.10 } }, | |
52 | { MANSON_HCS_3404, "HCS-3404-USB", "3404", { 1, 60, 0.1 }, { 0, 10, 0.10 } }, | |
53 | { MANSON_HCS_3600, "HCS-3600-USB", "3600", { 1, 16, 0.1 }, { 0, 60, 0.10 } }, | |
54 | { MANSON_HCS_3602, "HCS-3602-USB", "3602", { 1, 32, 0.1 }, { 0, 30, 0.10 } }, | |
55 | { MANSON_HCS_3604, "HCS-3604-USB", "3604", { 1, 60, 0.1 }, { 0, 15, 0.10 } }, | |
56 | { 0, NULL, NULL, { 0, 0, 0 }, { 0, 0, 0 }, }, | |
b5e92647 UH |
57 | }; |
58 | ||
8f4e922f UH |
59 | SR_PRIV struct sr_dev_driver manson_hcs_3xxx_driver_info; |
60 | static struct sr_dev_driver *di = &manson_hcs_3xxx_driver_info; | |
61 | ||
b5e92647 UH |
62 | static int dev_clear(void) |
63 | { | |
64 | return std_dev_clear(di, NULL); | |
65 | } | |
66 | ||
8f4e922f UH |
67 | static int init(struct sr_context *sr_ctx) |
68 | { | |
69 | return std_init(sr_ctx, di, LOG_PREFIX); | |
70 | } | |
71 | ||
72 | static GSList *scan(GSList *options) | |
73 | { | |
b5e92647 | 74 | int i, model_id; |
8f4e922f | 75 | struct drv_context *drvc; |
b5e92647 UH |
76 | struct dev_context *devc; |
77 | struct sr_dev_inst *sdi; | |
78 | struct sr_config *src; | |
79 | struct sr_channel *ch; | |
80 | GSList *devices, *l; | |
81 | const char *conn, *serialcomm; | |
82 | struct sr_serial_dev_inst *serial; | |
83 | char reply[50], **tokens; | |
8f4e922f | 84 | |
8f4e922f UH |
85 | drvc = di->priv; |
86 | drvc->instances = NULL; | |
b5e92647 UH |
87 | devices = NULL; |
88 | conn = NULL; | |
89 | serialcomm = NULL; | |
90 | ||
91 | for (l = options; l; l = l->next) { | |
92 | src = l->data; | |
93 | switch (src->key) { | |
94 | case SR_CONF_CONN: | |
95 | conn = g_variant_get_string(src->data, NULL); | |
96 | break; | |
97 | case SR_CONF_SERIALCOMM: | |
98 | serialcomm = g_variant_get_string(src->data, NULL); | |
99 | break; | |
100 | default: | |
101 | sr_err("Unknown option %d, skipping.", src->key); | |
102 | break; | |
103 | } | |
104 | } | |
8f4e922f | 105 | |
b5e92647 UH |
106 | if (!conn) |
107 | return NULL; | |
108 | if (!serialcomm) | |
109 | serialcomm = "9600/8n1"; | |
8f4e922f | 110 | |
b5e92647 UH |
111 | if (!(serial = sr_serial_dev_inst_new(conn, serialcomm))) |
112 | return NULL; | |
8f4e922f | 113 | |
b5e92647 UH |
114 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) |
115 | return NULL; | |
8f4e922f | 116 | |
b5e92647 | 117 | serial_flush(serial); |
8f4e922f | 118 | |
b5e92647 | 119 | sr_info("Probing serial port %s.", conn); |
8f4e922f | 120 | |
b5e92647 UH |
121 | /* Get the device model. */ |
122 | memset(&reply, 0, sizeof(reply)); | |
123 | serial_write_blocking(serial, "GMOD\r", 5); | |
124 | serial_read_blocking(serial, &reply, 8); | |
125 | tokens = g_strsplit((const gchar *)&reply, "\r", 2); | |
8f4e922f | 126 | |
b5e92647 | 127 | model_id = -1; |
25abc8dd MH |
128 | for (i = 0; models[i].id != NULL; i++) { |
129 | if (!strcmp(models[i].id, tokens[0])) | |
b5e92647 UH |
130 | model_id = i; |
131 | } | |
132 | if (model_id < 0) { | |
25abc8dd | 133 | sr_err("Unknown model id '%s' detected, aborting.", tokens[0]); |
b5e92647 UH |
134 | return NULL; |
135 | } | |
136 | ||
137 | if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Manson", | |
138 | models[model_id].name, NULL))) { | |
139 | sr_err("Failed to create device instance."); | |
140 | return NULL; | |
141 | } | |
8f4e922f | 142 | |
b5e92647 | 143 | g_strfreev(tokens); |
8f4e922f | 144 | |
b5e92647 UH |
145 | sdi->inst_type = SR_INST_SERIAL; |
146 | sdi->conn = serial; | |
147 | sdi->driver = di; | |
148 | ||
149 | if (!(ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "CH1"))) { | |
150 | sr_err("Failed to create channel."); | |
151 | return NULL; | |
152 | } | |
153 | sdi->channels = g_slist_append(sdi->channels, ch); | |
154 | ||
155 | devc = g_malloc0(sizeof(struct dev_context)); | |
156 | devc->model = &models[model_id]; | |
157 | ||
158 | sdi->priv = devc; | |
159 | ||
160 | drvc->instances = g_slist_append(drvc->instances, sdi); | |
161 | devices = g_slist_append(devices, sdi); | |
162 | ||
163 | serial_close(serial); | |
164 | if (!devices) | |
165 | sr_serial_dev_inst_free(serial); | |
166 | ||
167 | return devices; | |
168 | } | |
169 | ||
170 | static GSList *dev_list(void) | |
171 | { | |
172 | return ((struct drv_context *)(di->priv))->instances; | |
8f4e922f UH |
173 | } |
174 | ||
175 | static int cleanup(void) | |
176 | { | |
177 | return dev_clear(); | |
178 | } | |
179 | ||
180 | static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi, | |
181 | const struct sr_channel_group *cg) | |
182 | { | |
b5e92647 | 183 | struct dev_context *devc; |
8f4e922f | 184 | |
8f4e922f UH |
185 | (void)cg; |
186 | ||
b5e92647 UH |
187 | if (!sdi) |
188 | return SR_ERR_ARG; | |
189 | ||
190 | devc = sdi->priv; | |
191 | ||
8f4e922f | 192 | switch (key) { |
b5e92647 UH |
193 | case SR_CONF_LIMIT_SAMPLES: |
194 | *data = g_variant_new_uint64(devc->limit_samples); | |
195 | break; | |
196 | case SR_CONF_LIMIT_MSEC: | |
197 | *data = g_variant_new_uint64(devc->limit_msec); | |
198 | break; | |
199 | case SR_CONF_OUTPUT_VOLTAGE: | |
200 | *data = g_variant_new_double(devc->voltage); | |
201 | break; | |
202 | case SR_CONF_OUTPUT_CURRENT: | |
203 | *data = g_variant_new_double(devc->current); | |
204 | break; | |
8f4e922f UH |
205 | default: |
206 | return SR_ERR_NA; | |
207 | } | |
208 | ||
b5e92647 | 209 | return SR_OK; |
8f4e922f UH |
210 | } |
211 | ||
212 | static int config_set(int key, GVariant *data, const struct sr_dev_inst *sdi, | |
213 | const struct sr_channel_group *cg) | |
214 | { | |
b5e92647 | 215 | struct dev_context *devc; |
8f4e922f | 216 | |
8f4e922f UH |
217 | (void)cg; |
218 | ||
219 | if (sdi->status != SR_ST_ACTIVE) | |
220 | return SR_ERR_DEV_CLOSED; | |
221 | ||
b5e92647 UH |
222 | devc = sdi->priv; |
223 | ||
8f4e922f | 224 | switch (key) { |
b5e92647 UH |
225 | case SR_CONF_LIMIT_MSEC: |
226 | if (g_variant_get_uint64(data) == 0) | |
227 | return SR_ERR_ARG; | |
228 | devc->limit_msec = g_variant_get_uint64(data); | |
229 | break; | |
230 | case SR_CONF_LIMIT_SAMPLES: | |
231 | if (g_variant_get_uint64(data) == 0) | |
232 | return SR_ERR_ARG; | |
233 | devc->limit_samples = g_variant_get_uint64(data); | |
234 | break; | |
8f4e922f | 235 | default: |
b5e92647 | 236 | return SR_ERR_NA; |
8f4e922f UH |
237 | } |
238 | ||
b5e92647 | 239 | return SR_OK; |
8f4e922f UH |
240 | } |
241 | ||
242 | static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi, | |
243 | const struct sr_channel_group *cg) | |
244 | { | |
8f4e922f | 245 | (void)sdi; |
8f4e922f UH |
246 | (void)cg; |
247 | ||
8f4e922f | 248 | switch (key) { |
b5e92647 UH |
249 | case SR_CONF_SCAN_OPTIONS: |
250 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
251 | hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t)); | |
252 | break; | |
253 | case SR_CONF_DEVICE_OPTIONS: | |
254 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
255 | devopts, ARRAY_SIZE(devopts), sizeof(int32_t)); | |
256 | break; | |
8f4e922f UH |
257 | default: |
258 | return SR_ERR_NA; | |
259 | } | |
260 | ||
b5e92647 | 261 | return SR_OK; |
8f4e922f UH |
262 | } |
263 | ||
b5e92647 | 264 | static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data) |
8f4e922f | 265 | { |
b5e92647 UH |
266 | struct dev_context *devc; |
267 | struct sr_serial_dev_inst *serial; | |
8f4e922f UH |
268 | |
269 | if (sdi->status != SR_ST_ACTIVE) | |
270 | return SR_ERR_DEV_CLOSED; | |
271 | ||
b5e92647 UH |
272 | devc = sdi->priv; |
273 | devc->cb_data = cb_data; | |
274 | ||
275 | /* Send header packet to the session bus. */ | |
276 | std_session_send_df_header(cb_data, LOG_PREFIX); | |
277 | ||
278 | devc->starttime = g_get_monotonic_time(); | |
279 | devc->num_samples = 0; | |
280 | devc->reply_pending = FALSE; | |
281 | devc->req_sent_at = 0; | |
282 | ||
283 | /* Poll every 10ms, or whenever some data comes in. */ | |
284 | serial = sdi->conn; | |
285 | serial_source_add(serial, G_IO_IN, 10, hcs_receive_data, (void *)sdi); | |
286 | ||
8f4e922f UH |
287 | return SR_OK; |
288 | } | |
289 | ||
290 | static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) | |
291 | { | |
b5e92647 UH |
292 | return std_serial_dev_acquisition_stop(sdi, cb_data, |
293 | std_serial_dev_close, sdi->conn, LOG_PREFIX); | |
8f4e922f UH |
294 | } |
295 | ||
296 | SR_PRIV struct sr_dev_driver manson_hcs_3xxx_driver_info = { | |
297 | .name = "manson-hcs-3xxx", | |
298 | .longname = "Manson HCS-3xxx", | |
299 | .api_version = 1, | |
300 | .init = init, | |
301 | .cleanup = cleanup, | |
302 | .scan = scan, | |
303 | .dev_list = dev_list, | |
304 | .dev_clear = dev_clear, | |
305 | .config_get = config_get, | |
306 | .config_set = config_set, | |
307 | .config_list = config_list, | |
b5e92647 UH |
308 | .dev_open = std_serial_dev_open, |
309 | .dev_close = std_serial_dev_close, | |
8f4e922f UH |
310 | .dev_acquisition_start = dev_acquisition_start, |
311 | .dev_acquisition_stop = dev_acquisition_stop, | |
312 | .priv = NULL, | |
313 | }; |