]>
Commit | Line | Data |
---|---|---|
3ba56876 | 1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2010-2012 Håvard Espeland <gus@ping.uio.no>, | |
5 | * Copyright (C) 2010 Martin Stensgård <mastensg@ping.uio.no> | |
6 | * Copyright (C) 2010 Carl Henrik Lunde <chlunde@ping.uio.no> | |
7 | * | |
8 | * This program is free software: you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation, either version 3 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
20 | */ | |
21 | ||
22 | /* | |
23 | * ASIX SIGMA/SIGMA2 logic analyzer driver | |
24 | */ | |
25 | ||
26 | #include <config.h> | |
27 | #include "protocol.h" | |
28 | ||
3ba56876 | 29 | /* |
30 | * Channel numbers seem to go from 1-16, according to this image: | |
31 | * http://tools.asix.net/img/sigma_sigmacab_pins_720.jpg | |
32 | * (the cable has two additional GND pins, and a TI and TO pin) | |
33 | */ | |
34 | static const char *channel_names[] = { | |
35 | "1", "2", "3", "4", "5", "6", "7", "8", | |
36 | "9", "10", "11", "12", "13", "14", "15", "16", | |
37 | }; | |
38 | ||
39 | static const uint32_t drvopts[] = { | |
40 | SR_CONF_LOGIC_ANALYZER, | |
41 | }; | |
42 | ||
43 | static const uint32_t devopts[] = { | |
44 | SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET, | |
2f7e529c | 45 | SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, |
3ba56876 | 46 | SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, |
de3f7acb | 47 | #if ASIX_SIGMA_WITH_TRIGGER |
3ba56876 | 48 | SR_CONF_TRIGGER_MATCH | SR_CONF_LIST, |
49 | SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET, | |
de3f7acb | 50 | #endif |
3ba56876 | 51 | }; |
52 | ||
53 | static const int32_t trigger_matches[] = { | |
54 | SR_TRIGGER_ZERO, | |
55 | SR_TRIGGER_ONE, | |
56 | SR_TRIGGER_RISING, | |
57 | SR_TRIGGER_FALLING, | |
58 | }; | |
59 | ||
60 | ||
61 | static int dev_clear(const struct sr_dev_driver *di) | |
62 | { | |
63 | return std_dev_clear(di, sigma_clear_helper); | |
64 | } | |
65 | ||
3ba56876 | 66 | static GSList *scan(struct sr_dev_driver *di, GSList *options) |
67 | { | |
68 | struct sr_dev_inst *sdi; | |
3ba56876 | 69 | struct dev_context *devc; |
3ba56876 | 70 | struct ftdi_device_list *devlist; |
71 | char serial_txt[10]; | |
72 | uint32_t serial; | |
73 | int ret; | |
74 | unsigned int i; | |
75 | ||
76 | (void)options; | |
77 | ||
3ba56876 | 78 | devc = g_malloc0(sizeof(struct dev_context)); |
79 | ||
80 | ftdi_init(&devc->ftdic); | |
81 | ||
82 | /* Look for SIGMAs. */ | |
83 | ||
84 | if ((ret = ftdi_usb_find_all(&devc->ftdic, &devlist, | |
85 | USB_VENDOR, USB_PRODUCT)) <= 0) { | |
86 | if (ret < 0) | |
87 | sr_err("ftdi_usb_find_all(): %d", ret); | |
88 | goto free; | |
89 | } | |
90 | ||
91 | /* Make sure it's a version 1 or 2 SIGMA. */ | |
92 | ftdi_usb_get_strings(&devc->ftdic, devlist->dev, NULL, 0, NULL, 0, | |
93 | serial_txt, sizeof(serial_txt)); | |
94 | sscanf(serial_txt, "%x", &serial); | |
95 | ||
96 | if (serial < 0xa6010000 || serial > 0xa602ffff) { | |
97 | sr_err("Only SIGMA and SIGMA2 are supported " | |
98 | "in this version of libsigrok."); | |
99 | goto free; | |
100 | } | |
101 | ||
102 | sr_info("Found ASIX SIGMA - Serial: %s", serial_txt); | |
103 | ||
104 | devc->cur_samplerate = samplerates[0]; | |
105 | devc->period_ps = 0; | |
106 | devc->limit_msec = 0; | |
2f7e529c | 107 | devc->limit_samples = 0; |
3ba56876 | 108 | devc->cur_firmware = -1; |
109 | devc->num_channels = 0; | |
110 | devc->samples_per_event = 0; | |
111 | devc->capture_ratio = 50; | |
112 | devc->use_triggers = 0; | |
113 | ||
114 | /* Register SIGMA device. */ | |
115 | sdi = g_malloc0(sizeof(struct sr_dev_inst)); | |
116 | sdi->status = SR_ST_INITIALIZING; | |
117 | sdi->vendor = g_strdup(USB_VENDOR_NAME); | |
118 | sdi->model = g_strdup(USB_MODEL_NAME); | |
3ba56876 | 119 | |
120 | for (i = 0; i < ARRAY_SIZE(channel_names); i++) | |
121 | sr_channel_new(sdi, i, SR_CHANNEL_LOGIC, TRUE, channel_names[i]); | |
122 | ||
3ba56876 | 123 | sdi->priv = devc; |
124 | ||
125 | /* We will open the device again when we need it. */ | |
126 | ftdi_list_free(&devlist); | |
127 | ||
43376f33 | 128 | return std_scan_complete(di, g_slist_append(NULL, sdi)); |
3ba56876 | 129 | |
130 | free: | |
131 | ftdi_deinit(&devc->ftdic); | |
132 | g_free(devc); | |
133 | return NULL; | |
134 | } | |
135 | ||
3ba56876 | 136 | static int dev_open(struct sr_dev_inst *sdi) |
137 | { | |
138 | struct dev_context *devc; | |
139 | int ret; | |
140 | ||
141 | devc = sdi->priv; | |
142 | ||
143 | /* Make sure it's an ASIX SIGMA. */ | |
144 | if ((ret = ftdi_usb_open_desc(&devc->ftdic, | |
145 | USB_VENDOR, USB_PRODUCT, USB_DESCRIPTION, NULL)) < 0) { | |
146 | ||
147 | sr_err("ftdi_usb_open failed: %s", | |
148 | ftdi_get_error_string(&devc->ftdic)); | |
149 | ||
150 | return 0; | |
151 | } | |
152 | ||
153 | sdi->status = SR_ST_ACTIVE; | |
154 | ||
155 | return SR_OK; | |
156 | } | |
157 | ||
158 | static int dev_close(struct sr_dev_inst *sdi) | |
159 | { | |
160 | struct dev_context *devc; | |
161 | ||
162 | devc = sdi->priv; | |
163 | ||
164 | /* TODO */ | |
165 | if (sdi->status == SR_ST_ACTIVE) | |
166 | ftdi_usb_close(&devc->ftdic); | |
167 | ||
168 | sdi->status = SR_ST_INACTIVE; | |
169 | ||
170 | return SR_OK; | |
171 | } | |
172 | ||
3ba56876 | 173 | static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, |
174 | const struct sr_channel_group *cg) | |
175 | { | |
176 | struct dev_context *devc; | |
177 | ||
178 | (void)cg; | |
179 | ||
180 | if (!sdi) | |
181 | return SR_ERR; | |
182 | devc = sdi->priv; | |
183 | ||
184 | switch (key) { | |
185 | case SR_CONF_SAMPLERATE: | |
186 | *data = g_variant_new_uint64(devc->cur_samplerate); | |
187 | break; | |
188 | case SR_CONF_LIMIT_MSEC: | |
189 | *data = g_variant_new_uint64(devc->limit_msec); | |
190 | break; | |
2f7e529c GS |
191 | case SR_CONF_LIMIT_SAMPLES: |
192 | *data = g_variant_new_uint64(devc->limit_samples); | |
193 | break; | |
de3f7acb | 194 | #if ASIX_SIGMA_WITH_TRIGGER |
3ba56876 | 195 | case SR_CONF_CAPTURE_RATIO: |
196 | *data = g_variant_new_uint64(devc->capture_ratio); | |
197 | break; | |
de3f7acb | 198 | #endif |
3ba56876 | 199 | default: |
200 | return SR_ERR_NA; | |
201 | } | |
202 | ||
203 | return SR_OK; | |
204 | } | |
205 | ||
206 | static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, | |
207 | const struct sr_channel_group *cg) | |
208 | { | |
209 | struct dev_context *devc; | |
210 | uint64_t tmp; | |
211 | int ret; | |
212 | ||
213 | (void)cg; | |
214 | ||
215 | if (sdi->status != SR_ST_ACTIVE) | |
216 | return SR_ERR_DEV_CLOSED; | |
217 | ||
218 | devc = sdi->priv; | |
219 | ||
220 | ret = SR_OK; | |
221 | switch (key) { | |
222 | case SR_CONF_SAMPLERATE: | |
223 | ret = sigma_set_samplerate(sdi, g_variant_get_uint64(data)); | |
224 | break; | |
225 | case SR_CONF_LIMIT_MSEC: | |
226 | tmp = g_variant_get_uint64(data); | |
227 | if (tmp > 0) | |
228 | devc->limit_msec = g_variant_get_uint64(data); | |
229 | else | |
230 | ret = SR_ERR; | |
231 | break; | |
232 | case SR_CONF_LIMIT_SAMPLES: | |
233 | tmp = g_variant_get_uint64(data); | |
2f7e529c | 234 | devc->limit_samples = tmp; |
9a0a606a | 235 | devc->limit_msec = sigma_limit_samples_to_msec(devc, tmp); |
3ba56876 | 236 | break; |
de3f7acb | 237 | #if ASIX_SIGMA_WITH_TRIGGER |
3ba56876 | 238 | case SR_CONF_CAPTURE_RATIO: |
239 | tmp = g_variant_get_uint64(data); | |
de3f7acb GS |
240 | if (tmp > 100) |
241 | return SR_ERR; | |
242 | devc->capture_ratio = tmp; | |
3ba56876 | 243 | break; |
de3f7acb | 244 | #endif |
3ba56876 | 245 | default: |
246 | ret = SR_ERR_NA; | |
247 | } | |
248 | ||
249 | return ret; | |
250 | } | |
251 | ||
252 | static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, | |
253 | const struct sr_channel_group *cg) | |
254 | { | |
255 | GVariant *gvar; | |
256 | GVariantBuilder gvb; | |
257 | ||
258 | (void)cg; | |
259 | ||
260 | switch (key) { | |
261 | case SR_CONF_DEVICE_OPTIONS: | |
262 | if (!sdi) | |
263 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
264 | drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t)); | |
265 | else | |
266 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, | |
267 | devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); | |
268 | break; | |
269 | case SR_CONF_SAMPLERATE: | |
270 | g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}")); | |
271 | gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), samplerates, | |
4154a516 | 272 | samplerates_count, sizeof(samplerates[0])); |
3ba56876 | 273 | g_variant_builder_add(&gvb, "{sv}", "samplerates", gvar); |
274 | *data = g_variant_builder_end(&gvb); | |
275 | break; | |
de3f7acb | 276 | #if ASIX_SIGMA_WITH_TRIGGER |
3ba56876 | 277 | case SR_CONF_TRIGGER_MATCH: |
278 | *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, | |
279 | trigger_matches, ARRAY_SIZE(trigger_matches), | |
280 | sizeof(int32_t)); | |
281 | break; | |
de3f7acb | 282 | #endif |
3ba56876 | 283 | default: |
284 | return SR_ERR_NA; | |
285 | } | |
286 | ||
287 | return SR_OK; | |
288 | } | |
289 | ||
695dc859 | 290 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) |
3ba56876 | 291 | { |
292 | struct dev_context *devc; | |
293 | struct clockselect_50 clockselect; | |
294 | int frac, triggerpin, ret; | |
f06fb3e9 | 295 | uint8_t triggerselect; |
3ba56876 | 296 | struct triggerinout triggerinout_conf; |
297 | struct triggerlut lut; | |
22f64ed8 | 298 | uint8_t regval; |
3ba56876 | 299 | |
300 | if (sdi->status != SR_ST_ACTIVE) | |
301 | return SR_ERR_DEV_CLOSED; | |
302 | ||
303 | devc = sdi->priv; | |
304 | ||
305 | if (sigma_convert_trigger(sdi) != SR_OK) { | |
306 | sr_err("Failed to configure triggers."); | |
307 | return SR_ERR; | |
308 | } | |
309 | ||
310 | /* If the samplerate has not been set, default to 200 kHz. */ | |
311 | if (devc->cur_firmware == -1) { | |
312 | if ((ret = sigma_set_samplerate(sdi, SR_KHZ(200))) != SR_OK) | |
313 | return ret; | |
314 | } | |
315 | ||
316 | /* Enter trigger programming mode. */ | |
317 | sigma_set_register(WRITE_TRIGGER_SELECT1, 0x20, devc); | |
318 | ||
f06fb3e9 | 319 | triggerselect = 0; |
3ba56876 | 320 | if (devc->cur_samplerate >= SR_MHZ(100)) { |
f06fb3e9 | 321 | /* 100 and 200 MHz mode. */ |
3ba56876 | 322 | sigma_set_register(WRITE_TRIGGER_SELECT1, 0x81, devc); |
323 | ||
324 | /* Find which pin to trigger on from mask. */ | |
0a1f7b09 | 325 | for (triggerpin = 0; triggerpin < 8; triggerpin++) |
3ba56876 | 326 | if ((devc->trigger.risingmask | devc->trigger.fallingmask) & |
327 | (1 << triggerpin)) | |
328 | break; | |
329 | ||
330 | /* Set trigger pin and light LED on trigger. */ | |
331 | triggerselect = (1 << LEDSEL1) | (triggerpin & 0x7); | |
332 | ||
333 | /* Default rising edge. */ | |
334 | if (devc->trigger.fallingmask) | |
335 | triggerselect |= 1 << 3; | |
336 | ||
3ba56876 | 337 | } else if (devc->cur_samplerate <= SR_MHZ(50)) { |
f06fb3e9 | 338 | /* All other modes. */ |
3ba56876 | 339 | sigma_build_basic_trigger(&lut, devc); |
340 | ||
341 | sigma_write_trigger_lut(&lut, devc); | |
342 | ||
343 | triggerselect = (1 << LEDSEL1) | (1 << LEDSEL0); | |
344 | } | |
345 | ||
346 | /* Setup trigger in and out pins to default values. */ | |
347 | memset(&triggerinout_conf, 0, sizeof(struct triggerinout)); | |
348 | triggerinout_conf.trgout_bytrigger = 1; | |
349 | triggerinout_conf.trgout_enable = 1; | |
350 | ||
351 | sigma_write_register(WRITE_TRIGGER_OPTION, | |
352 | (uint8_t *) &triggerinout_conf, | |
353 | sizeof(struct triggerinout), devc); | |
354 | ||
355 | /* Go back to normal mode. */ | |
356 | sigma_set_register(WRITE_TRIGGER_SELECT1, triggerselect, devc); | |
357 | ||
358 | /* Set clock select register. */ | |
359 | if (devc->cur_samplerate == SR_MHZ(200)) | |
360 | /* Enable 4 channels. */ | |
361 | sigma_set_register(WRITE_CLOCK_SELECT, 0xf0, devc); | |
362 | else if (devc->cur_samplerate == SR_MHZ(100)) | |
363 | /* Enable 8 channels. */ | |
364 | sigma_set_register(WRITE_CLOCK_SELECT, 0x00, devc); | |
365 | else { | |
366 | /* | |
367 | * 50 MHz mode (or fraction thereof). Any fraction down to | |
368 | * 50 MHz / 256 can be used, but is not supported by sigrok API. | |
369 | */ | |
370 | frac = SR_MHZ(50) / devc->cur_samplerate - 1; | |
371 | ||
372 | clockselect.async = 0; | |
373 | clockselect.fraction = frac; | |
374 | clockselect.disabled_channels = 0; | |
375 | ||
376 | sigma_write_register(WRITE_CLOCK_SELECT, | |
377 | (uint8_t *) &clockselect, | |
378 | sizeof(clockselect), devc); | |
379 | } | |
380 | ||
381 | /* Setup maximum post trigger time. */ | |
382 | sigma_set_register(WRITE_POST_TRIGGER, | |
383 | (devc->capture_ratio * 255) / 100, devc); | |
384 | ||
385 | /* Start acqusition. */ | |
386 | gettimeofday(&devc->start_tv, 0); | |
22f64ed8 GS |
387 | regval = WMR_TRGRES | WMR_SDRAMWRITEEN; |
388 | #if ASIX_SIGMA_WITH_TRIGGER | |
389 | regval |= WMR_TRGEN; | |
390 | #endif | |
391 | sigma_set_register(WRITE_MODE, regval, devc); | |
3ba56876 | 392 | |
bee2b016 | 393 | std_session_send_df_header(sdi); |
3ba56876 | 394 | |
395 | /* Add capture source. */ | |
396 | sr_session_source_add(sdi->session, -1, 0, 10, sigma_receive_data, (void *)sdi); | |
397 | ||
398 | devc->state.state = SIGMA_CAPTURE; | |
399 | ||
400 | return SR_OK; | |
401 | } | |
402 | ||
695dc859 | 403 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) |
3ba56876 | 404 | { |
405 | struct dev_context *devc; | |
406 | ||
3ba56876 | 407 | devc = sdi->priv; |
408 | devc->state.state = SIGMA_IDLE; | |
409 | ||
410 | sr_session_source_remove(sdi->session, -1); | |
411 | ||
412 | return SR_OK; | |
413 | } | |
414 | ||
dd5c48a6 | 415 | static struct sr_dev_driver asix_sigma_driver_info = { |
3ba56876 | 416 | .name = "asix-sigma", |
417 | .longname = "ASIX SIGMA/SIGMA2", | |
418 | .api_version = 1, | |
c2fdcc25 | 419 | .init = std_init, |
700d6b64 | 420 | .cleanup = std_cleanup, |
3ba56876 | 421 | .scan = scan, |
c01bf34c | 422 | .dev_list = std_dev_list, |
3ba56876 | 423 | .dev_clear = dev_clear, |
424 | .config_get = config_get, | |
425 | .config_set = config_set, | |
426 | .config_list = config_list, | |
427 | .dev_open = dev_open, | |
428 | .dev_close = dev_close, | |
429 | .dev_acquisition_start = dev_acquisition_start, | |
430 | .dev_acquisition_stop = dev_acquisition_stop, | |
431 | .context = NULL, | |
432 | }; | |
dd5c48a6 | 433 | SR_REGISTER_DEV_DRIVER(asix_sigma_driver_info); |