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