]>
Commit | Line | Data |
---|---|---|
df92e5cf | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
df92e5cf | 3 | * |
f48cef78 UH |
4 | * Copyright (C) 2011 Daniel Ribeiro <drwyrm@gmail.com> |
5 | * Copyright (C) 2012 Renato Caldas <rmsc@fe.up.pt> | |
6 | * Copyright (C) 2013 Lior Elazary <lelazary@yahoo.com> | |
df92e5cf | 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 | ||
6ec6c43b | 22 | #include <config.h> |
df92e5cf | 23 | #include "protocol.h" |
24 | ||
05199c0a | 25 | static const uint32_t drvopts[] = { |
365f04d6 | 26 | SR_CONF_OSCILLOSCOPE, |
1953564a | 27 | SR_CONF_LOGIC_ANALYZER, |
05199c0a UH |
28 | }; |
29 | ||
30 | static const uint32_t devopts[] = { | |
5827f61b BV |
31 | SR_CONF_LIMIT_SAMPLES | SR_CONF_SET, |
32 | SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, | |
33 | SR_CONF_TRIGGER_TYPE | SR_CONF_LIST, | |
34 | SR_CONF_TRIGGER_SLOPE | SR_CONF_SET, | |
35 | SR_CONF_HORIZ_TRIGGERPOS | SR_CONF_SET, | |
36 | SR_CONF_CAPTURE_RATIO | SR_CONF_SET, | |
37 | SR_CONF_RLE | SR_CONF_SET, | |
df92e5cf | 38 | }; |
39 | ||
40 | /* | |
ba7dd8bb | 41 | * Channels are numbered 0 to 7. |
df92e5cf | 42 | * |
43 | * See also: http://www.linkinstruments.com/images/mso19_1113.gif | |
44 | */ | |
0f34cb47 | 45 | static const char *channel_names[] = { |
365f04d6 | 46 | /* Note: DSO needs to be first. */ |
0f34cb47 | 47 | "DSO", "0", "1", "2", "3", "4", "5", "6", "7", |
df92e5cf | 48 | }; |
49 | ||
a9ed6877 BV |
50 | static const uint64_t samplerates[] = { |
51 | SR_HZ(100), | |
52 | SR_MHZ(200), | |
53 | SR_HZ(100), | |
df92e5cf | 54 | }; |
55 | ||
db85496e UH |
56 | static const char *trigger_slopes[2] = { |
57 | "r", "f", | |
58 | }; | |
59 | ||
4f840ce9 | 60 | static GSList *scan(struct sr_dev_driver *di, GSList *options) |
df92e5cf | 61 | { |
00b44ccb UH |
62 | int i; |
63 | GSList *devices = NULL; | |
64 | const char *conn = NULL; | |
65 | const char *serialcomm = NULL; | |
66 | GSList *l; | |
1987b8d6 | 67 | struct sr_config *src; |
00b44ccb | 68 | struct udev *udev; |
fca75cbb | 69 | int chtype; |
df92e5cf | 70 | |
df92e5cf | 71 | for (l = options; l; l = l->next) { |
1987b8d6 BV |
72 | src = l->data; |
73 | switch (src->key) { | |
1953564a | 74 | case SR_CONF_CONN: |
a9ed6877 | 75 | conn = g_variant_get_string(src->data, NULL); |
df92e5cf | 76 | break; |
1953564a | 77 | case SR_CONF_SERIALCOMM: |
06c45a66 | 78 | serialcomm = g_variant_get_string(src->data, NULL); |
df92e5cf | 79 | break; |
80 | } | |
81 | } | |
82 | if (!conn) | |
00b44ccb | 83 | conn = SERIALCONN; |
98fec29e | 84 | if (!serialcomm) |
df92e5cf | 85 | serialcomm = SERIALCOMM; |
86 | ||
00b44ccb | 87 | udev = udev_new(); |
df92e5cf | 88 | if (!udev) { |
89 | sr_err("Failed to initialize udev."); | |
90 | } | |
00b44ccb | 91 | |
df92e5cf | 92 | struct udev_enumerate *enumerate = udev_enumerate_new(udev); |
93 | udev_enumerate_add_match_subsystem(enumerate, "usb-serial"); | |
94 | udev_enumerate_scan_devices(enumerate); | |
95 | struct udev_list_entry *devs = udev_enumerate_get_list_entry(enumerate); | |
96 | struct udev_list_entry *dev_list_entry; | |
00b44ccb UH |
97 | for (dev_list_entry = devs; |
98 | dev_list_entry != NULL; | |
99 | dev_list_entry = udev_list_entry_get_next(dev_list_entry)) { | |
df92e5cf | 100 | const char *syspath = udev_list_entry_get_name(dev_list_entry); |
00b44ccb UH |
101 | struct udev_device *dev = |
102 | udev_device_new_from_syspath(udev, syspath); | |
df92e5cf | 103 | const char *sysname = udev_device_get_sysname(dev); |
00b44ccb UH |
104 | struct udev_device *parent = |
105 | udev_device_get_parent_with_subsystem_devtype(dev, "usb", | |
106 | "usb_device"); | |
df92e5cf | 107 | |
108 | if (!parent) { | |
109 | sr_err("Unable to find parent usb device for %s", | |
110 | sysname); | |
111 | continue; | |
112 | } | |
113 | ||
00b44ccb UH |
114 | const char *idVendor = |
115 | udev_device_get_sysattr_value(parent, "idVendor"); | |
116 | const char *idProduct = | |
117 | udev_device_get_sysattr_value(parent, "idProduct"); | |
df92e5cf | 118 | if (strcmp(USB_VENDOR, idVendor) |
00b44ccb | 119 | || strcmp(USB_PRODUCT, idProduct)) |
df92e5cf | 120 | continue; |
121 | ||
00b44ccb UH |
122 | const char *iSerial = |
123 | udev_device_get_sysattr_value(parent, "serial"); | |
124 | const char *iProduct = | |
125 | udev_device_get_sysattr_value(parent, "product"); | |
df92e5cf | 126 | |
00b44ccb | 127 | char path[32]; |
df92e5cf | 128 | snprintf(path, sizeof(path), "/dev/%s", sysname); |
00b44ccb | 129 | conn = path; |
df92e5cf | 130 | |
131 | size_t s = strcspn(iProduct, " "); | |
00b44ccb UH |
132 | char product[32]; |
133 | char manufacturer[32]; | |
df92e5cf | 134 | if (s > sizeof(product) || |
00b44ccb UH |
135 | strlen(iProduct) - s > sizeof(manufacturer)) { |
136 | sr_err("Could not parse iProduct: %s.", iProduct); | |
df92e5cf | 137 | continue; |
138 | } | |
139 | strncpy(product, iProduct, s); | |
140 | product[s] = 0; | |
4db2aaff | 141 | strcpy(manufacturer, iProduct + s + 1); |
00b44ccb UH |
142 | |
143 | //Create the device context and set its params | |
144 | struct dev_context *devc; | |
f57d8ffe | 145 | devc = g_malloc0(sizeof(struct dev_context)); |
00b44ccb UH |
146 | |
147 | if (mso_parse_serial(iSerial, iProduct, devc) != SR_OK) { | |
148 | sr_err("Invalid iSerial: %s.", iSerial); | |
149 | g_free(devc); | |
150 | return devices; | |
151 | } | |
152 | ||
153 | char hwrev[32]; | |
154 | sprintf(hwrev, "r%d", devc->hwrev); | |
155 | devc->ctlbase1 = 0; | |
156 | devc->protocol_trigger.spimode = 0; | |
157 | for (i = 0; i < 4; i++) { | |
158 | devc->protocol_trigger.word[i] = 0; | |
159 | devc->protocol_trigger.mask[i] = 0xff; | |
160 | } | |
161 | ||
91219afc | 162 | devc->serial = sr_serial_dev_inst_new(conn, serialcomm); |
00b44ccb | 163 | |
aac29cc1 UH |
164 | struct sr_dev_inst *sdi = g_malloc0(sizeof(struct sr_dev_inst)); |
165 | sdi->status = SR_ST_INACTIVE; | |
166 | sdi->vendor = g_strdup(manufacturer); | |
167 | sdi->model = g_strdup(product); | |
168 | sdi->version = g_strdup(hwrev); | |
00b44ccb UH |
169 | sdi->priv = devc; |
170 | ||
07962655 | 171 | for (i = 0; i < ARRAY_SIZE(channel_names); i++) { |
fca75cbb | 172 | chtype = (i == 0) ? SR_CHANNEL_ANALOG : SR_CHANNEL_LOGIC; |
0f34cb47 | 173 | sr_channel_new(sdi, i, chtype, TRUE, channel_names[i]); |
00b44ccb | 174 | } |
00b44ccb | 175 | } |
5a24e89c | 176 | |
15a5bfe4 | 177 | return std_scan_complete(di, devices); |
df92e5cf | 178 | } |
179 | ||
6078d2c9 | 180 | static int dev_open(struct sr_dev_inst *sdi) |
df92e5cf | 181 | { |
00b44ccb | 182 | int ret; |
df92e5cf | 183 | struct dev_context *devc; |
184 | ||
185 | devc = sdi->priv; | |
186 | ||
187 | if (serial_open(devc->serial, SERIAL_RDWR) != SR_OK) | |
188 | return SR_ERR; | |
189 | ||
df92e5cf | 190 | /* FIXME: discard serial buffer */ |
191 | mso_check_trigger(devc->serial, &devc->trigger_state); | |
192 | sr_dbg("Trigger state: 0x%x.", devc->trigger_state); | |
193 | ||
00b44ccb | 194 | ret = mso_reset_adc(sdi); |
df92e5cf | 195 | if (ret != SR_OK) |
196 | return ret; | |
197 | ||
198 | mso_check_trigger(devc->serial, &devc->trigger_state); | |
199 | sr_dbg("Trigger state: 0x%x.", devc->trigger_state); | |
200 | ||
00b44ccb UH |
201 | // ret = mso_reset_fsm(sdi); |
202 | // if (ret != SR_OK) | |
203 | // return ret; | |
204 | // return SR_ERR; | |
df92e5cf | 205 | |
206 | return SR_OK; | |
207 | } | |
208 | ||
dd7a72ea UH |
209 | static int config_get(int key, GVariant **data, |
210 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
df92e5cf | 211 | { |
212 | struct dev_context *devc; | |
213 | ||
53b4680f | 214 | (void)cg; |
8f996b89 | 215 | |
709468ba UH |
216 | if (!sdi) |
217 | return SR_ERR_ARG; | |
218 | ||
219 | devc = sdi->priv; | |
220 | ||
2ea67fc9 | 221 | switch (key) { |
123e1313 | 222 | case SR_CONF_SAMPLERATE: |
709468ba | 223 | *data = g_variant_new_uint64(devc->cur_rate); |
df92e5cf | 224 | break; |
225 | default: | |
bd6fbf62 | 226 | return SR_ERR_NA; |
df92e5cf | 227 | } |
228 | ||
229 | return SR_OK; | |
230 | } | |
231 | ||
dd7a72ea UH |
232 | static int config_set(int key, GVariant *data, |
233 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
df92e5cf | 234 | { |
087a9161 | 235 | struct dev_context *devc; |
ca9b9f48 DE |
236 | uint64_t num_samples; |
237 | const char *slope; | |
00b44ccb | 238 | int trigger_pos; |
a9ed6877 | 239 | double pos; |
00b44ccb | 240 | |
53b4680f | 241 | (void)cg; |
b0baddef | 242 | |
00b44ccb | 243 | devc = sdi->priv; |
087a9161 | 244 | |
2ea67fc9 | 245 | switch (key) { |
1953564a | 246 | case SR_CONF_SAMPLERATE: |
00b44ccb | 247 | // FIXME |
a9ed6877 | 248 | return mso_configure_rate(sdi, g_variant_get_uint64(data)); |
1953564a | 249 | case SR_CONF_LIMIT_SAMPLES: |
a9ed6877 | 250 | num_samples = g_variant_get_uint64(data); |
5952553f | 251 | if (num_samples != 1024) { |
252 | sr_err("Only 1024 samples are supported."); | |
758906aa UH |
253 | return SR_ERR_ARG; |
254 | } | |
a9010323 | 255 | devc->limit_samples = num_samples; |
df92e5cf | 256 | break; |
1953564a | 257 | case SR_CONF_CAPTURE_RATIO: |
00b44ccb | 258 | break; |
1953564a | 259 | case SR_CONF_TRIGGER_SLOPE: |
db85496e | 260 | if ((idx = std_str_idx(data, ARRAY_AND_SIZE(trigger_slopes))) < 0) |
758906aa | 261 | return SR_ERR_ARG; |
db85496e | 262 | devc->trigger_slope = idx; |
00b44ccb | 263 | break; |
1953564a | 264 | case SR_CONF_HORIZ_TRIGGERPOS: |
a9ed6877 | 265 | pos = g_variant_get_double(data); |
00b44ccb UH |
266 | if (pos < 0 || pos > 255) { |
267 | sr_err("Trigger position (%f) should be between 0 and 255.", pos); | |
758906aa | 268 | return SR_ERR_ARG; |
00b44ccb | 269 | } |
a9010323 UH |
270 | trigger_pos = (int)pos; |
271 | devc->trigger_holdoff[0] = trigger_pos & 0xff; | |
df92e5cf | 272 | break; |
1953564a | 273 | case SR_CONF_RLE: |
df92e5cf | 274 | break; |
275 | default: | |
758906aa | 276 | return SR_ERR_NA; |
df92e5cf | 277 | } |
278 | ||
758906aa | 279 | return SR_OK; |
df92e5cf | 280 | } |
281 | ||
dd7a72ea UH |
282 | static int config_list(int key, GVariant **data, |
283 | const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) | |
a1c743fc | 284 | { |
a1c743fc | 285 | switch (key) { |
9a6517d1 | 286 | case SR_CONF_DEVICE_OPTIONS: |
23772462 | 287 | return STD_CONFIG_LIST(key, data, sdi, cg, NO_OPTS, drvopts, devopts); |
a1c743fc | 288 | case SR_CONF_SAMPLERATE: |
53012da6 | 289 | *data = std_gvar_samplerates_steps(ARRAY_AND_SIZE(samplerates)); |
a1c743fc | 290 | break; |
c50277a6 | 291 | case SR_CONF_TRIGGER_TYPE: |
a9ed6877 | 292 | *data = g_variant_new_string(TRIGGER_TYPE); |
c50277a6 | 293 | break; |
a1c743fc | 294 | default: |
bd6fbf62 | 295 | return SR_ERR_NA; |
a1c743fc BV |
296 | } |
297 | ||
298 | return SR_OK; | |
299 | } | |
300 | ||
695dc859 | 301 | static int dev_acquisition_start(const struct sr_dev_inst *sdi) |
df92e5cf | 302 | { |
df92e5cf | 303 | struct dev_context *devc; |
df92e5cf | 304 | int ret = SR_ERR; |
00b44ccb | 305 | |
e73ffd42 | 306 | devc = sdi->priv; |
df92e5cf | 307 | |
ba7dd8bb UH |
308 | if (mso_configure_channels(sdi) != SR_OK) { |
309 | sr_err("Failed to configure channels."); | |
5a24e89c | 310 | return SR_ERR; |
311 | } | |
df92e5cf | 312 | |
df92e5cf | 313 | /* FIXME: No need to do full reconfigure every time */ |
00b44ccb UH |
314 | // ret = mso_reset_fsm(sdi); |
315 | // if (ret != SR_OK) | |
316 | // return ret; | |
df92e5cf | 317 | |
318 | /* FIXME: ACDC Mode */ | |
319 | devc->ctlbase1 &= 0x7f; | |
00b44ccb | 320 | // devc->ctlbase1 |= devc->acdcmode; |
df92e5cf | 321 | |
322 | ret = mso_configure_rate(sdi, devc->cur_rate); | |
323 | if (ret != SR_OK) | |
324 | return ret; | |
325 | ||
326 | /* set dac offset */ | |
327 | ret = mso_dac_out(sdi, devc->dac_offset); | |
328 | if (ret != SR_OK) | |
329 | return ret; | |
330 | ||
331 | ret = mso_configure_threshold_level(sdi); | |
332 | if (ret != SR_OK) | |
333 | return ret; | |
334 | ||
335 | ret = mso_configure_trigger(sdi); | |
336 | if (ret != SR_OK) | |
337 | return ret; | |
338 | ||
df92e5cf | 339 | /* END of config hardware part */ |
00b44ccb UH |
340 | ret = mso_arm(sdi); |
341 | if (ret != SR_OK) | |
342 | return ret; | |
df92e5cf | 343 | |
344 | /* Start acquisition on the device. */ | |
4db2aaff | 345 | mso_check_trigger(devc->serial, &devc->trigger_state); |
346 | ret = mso_check_trigger(devc->serial, NULL); | |
df92e5cf | 347 | if (ret != SR_OK) |
348 | return ret; | |
349 | ||
365f04d6 | 350 | /* Reset trigger state. */ |
351 | devc->trigger_state = 0x00; | |
352 | ||
bee2b016 | 353 | std_session_send_df_header(sdi); |
df92e5cf | 354 | |
ba7dd8bb | 355 | /* Our first channel is analog, the other 8 are of type 'logic'. */ |
365f04d6 | 356 | /* TODO. */ |
357 | ||
102f1239 | 358 | serial_source_add(sdi->session, devc->serial, G_IO_IN, -1, |
695dc859 | 359 | mso_receive_data, sdi); |
df92e5cf | 360 | |
361 | return SR_OK; | |
362 | } | |
363 | ||
695dc859 | 364 | static int dev_acquisition_stop(struct sr_dev_inst *sdi) |
df92e5cf | 365 | { |
df92e5cf | 366 | stop_acquisition(sdi); |
367 | ||
368 | return SR_OK; | |
369 | } | |
370 | ||
dd5c48a6 | 371 | static struct sr_dev_driver link_mso19_driver_info = { |
df92e5cf | 372 | .name = "link-mso19", |
373 | .longname = "Link Instruments MSO-19", | |
374 | .api_version = 1, | |
c2fdcc25 | 375 | .init = std_init, |
700d6b64 | 376 | .cleanup = std_cleanup, |
6078d2c9 | 377 | .scan = scan, |
c01bf34c | 378 | .dev_list = std_dev_list, |
f778bf02 | 379 | .dev_clear = std_dev_clear, |
035a1078 BV |
380 | .config_get = config_get, |
381 | .config_set = config_set, | |
a1c743fc | 382 | .config_list = config_list, |
6078d2c9 | 383 | .dev_open = dev_open, |
bf2c987f | 384 | .dev_close = std_serial_dev_close, |
6078d2c9 UH |
385 | .dev_acquisition_start = dev_acquisition_start, |
386 | .dev_acquisition_stop = dev_acquisition_stop, | |
41812aca | 387 | .context = NULL, |
df92e5cf | 388 | }; |
dd5c48a6 | 389 | SR_REGISTER_DEV_DRIVER(link_mso19_driver_info); |