]>
Commit | Line | Data |
---|---|---|
eb1f1eb4 UH |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
4 | * Copyright (C) 2012 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 | ||
21 | #include <stdlib.h> | |
163f0523 UH |
22 | #include <string.h> |
23 | #include <fcntl.h> | |
45c59c8b BV |
24 | #include "libsigrok.h" |
25 | #include "libsigrok-internal.h" | |
eb1f1eb4 UH |
26 | |
27 | #define MIN_NUM_SAMPLES 1 | |
28 | ||
29 | struct context { | |
30 | uint64_t limit_samples; | |
31 | uint64_t limit_msec; /* TODO: Implement. */ | |
32 | uint64_t num_samples; | |
33 | void *session_dev_id; | |
163f0523 UH |
34 | struct sr_serial_dev_inst *serial; |
35 | uint8_t bytes[14 + 1]; | |
36 | int byte_counter; | |
37 | gboolean synchronized; | |
38 | gboolean got_14_bytes; | |
eb1f1eb4 UH |
39 | }; |
40 | ||
41 | static const int hwcaps[] = { | |
42 | SR_HWCAP_OSCILLOSCOPE, | |
43 | SR_HWCAP_LIMIT_SAMPLES, | |
44 | // SR_HWCAP_LIMIT_MSEC, | |
45 | // SR_HWCAP_CONTINUOUS, | |
46 | 0, | |
47 | }; | |
48 | ||
49 | static const char *probe_names[] = { | |
50 | "Probe", | |
51 | NULL, | |
52 | }; | |
53 | ||
54 | static GSList *dev_insts = NULL; | |
55 | ||
56 | /* Function prototypes. */ | |
57 | static int hw_dev_acquisition_stop(int dev_index, void *cb_data); | |
58 | ||
59 | static int hw_init(const char *devinfo) | |
60 | { | |
61 | struct sr_dev_inst *sdi; | |
62 | struct context *ctx; | |
63 | int devcnt = 0; | |
64 | ||
65 | /* Avoid compiler warnings. */ | |
66 | (void)devinfo; /* TODO: This specifies the serial port to use. */ | |
67 | ||
68 | if (!(ctx = g_try_malloc0(sizeof(struct context)))) { | |
69 | sr_err("va18b: %s: ctx malloc failed.", __func__); | |
70 | return 0; | |
71 | } | |
72 | ||
73 | ctx->limit_samples = 0; | |
74 | ctx->limit_msec = 0; | |
75 | ctx->num_samples = 0; | |
76 | ctx->session_dev_id = NULL; | |
163f0523 UH |
77 | ctx->serial = NULL; |
78 | memset(ctx->bytes, 0x00, 14 + 1); | |
79 | ctx->byte_counter = 0; | |
80 | ctx->synchronized = FALSE; | |
81 | ctx->got_14_bytes = FALSE; | |
eb1f1eb4 UH |
82 | |
83 | if (!(sdi = sr_dev_inst_new(devcnt, SR_ST_ACTIVE, | |
84 | "Mastech", "VA18B", ""))) { | |
85 | sr_err("va18b: %s: sr_dev_inst_new returned NULL.", __func__); | |
86 | return 0; | |
87 | } | |
88 | ||
89 | sdi->priv = ctx; | |
163f0523 UH |
90 | |
91 | /* TODO: Don't hardcode serial port. */ | |
92 | ctx->serial = sr_serial_dev_inst_new("/dev/ttyUSB0", -1); | |
93 | ||
eb1f1eb4 UH |
94 | dev_insts = g_slist_append(dev_insts, sdi); |
95 | ||
96 | devcnt = 1; | |
97 | ||
98 | return devcnt; | |
99 | } | |
100 | ||
101 | static int hw_dev_open(int dev_index) | |
102 | { | |
163f0523 UH |
103 | struct sr_dev_inst *sdi; |
104 | struct context *ctx; | |
105 | ||
106 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) { | |
107 | sr_err("va18b: %s: sdi was NULL.", __func__); | |
108 | return SR_ERR_BUG; | |
109 | } | |
110 | ||
111 | if (!(ctx = sdi->priv)) { | |
112 | sr_err("va18b: %s: sdi->priv was NULL.", __func__); | |
113 | return SR_ERR_BUG; | |
114 | } | |
eb1f1eb4 | 115 | |
163f0523 UH |
116 | sr_dbg("va18b: %s: Opening serial port '%s'.", __func__, |
117 | ctx->serial->port); | |
118 | ||
119 | /* TODO: Check for != NULL. */ | |
120 | ||
121 | /* TODO: O_NONBLOCK? */ | |
122 | ctx->serial->fd = serial_open(ctx->serial->port, O_RDWR | O_NONBLOCK); | |
123 | if (ctx->serial->fd == -1) { | |
124 | sr_err("va18b: %s: Couldn't open serial port '%s'.", __func__, | |
125 | ctx->serial->port); | |
126 | return SR_ERR; | |
127 | } | |
128 | ||
129 | serial_set_params(ctx->serial->fd, 2400, 8, 0, 1, 2 /* TODO */); | |
eb1f1eb4 UH |
130 | |
131 | return SR_OK; | |
132 | } | |
133 | ||
134 | static int hw_dev_close(int dev_index) | |
135 | { | |
163f0523 UH |
136 | struct sr_dev_inst *sdi; |
137 | struct context *ctx; | |
138 | ||
139 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) { | |
140 | sr_err("va18b: %s: sdi was NULL.", __func__); | |
141 | return SR_ERR_BUG; | |
142 | } | |
143 | ||
144 | if (!(ctx = sdi->priv)) { | |
145 | sr_err("va18b: %s: sdi->priv was NULL.", __func__); | |
146 | return SR_ERR_BUG; | |
147 | } | |
eb1f1eb4 | 148 | |
163f0523 UH |
149 | /* TODO: Check for != NULL. */ |
150 | ||
151 | if (ctx->serial->fd != -1) { | |
152 | serial_close(ctx->serial->fd); | |
153 | ctx->serial->fd = -1; | |
154 | sdi->status = SR_ST_INACTIVE; | |
155 | } | |
eb1f1eb4 UH |
156 | |
157 | return SR_OK; | |
158 | } | |
159 | ||
160 | static int hw_cleanup(void) | |
161 | { | |
162 | GSList *l; | |
163 | struct sr_dev_inst *sdi; | |
164 | struct context *ctx; | |
165 | ||
166 | /* Properly close and free all devices. */ | |
167 | for (l = dev_insts; l; l = l->next) { | |
168 | if (!(sdi = l->data)) { | |
169 | /* Log error, but continue cleaning up the rest. */ | |
170 | sr_err("va18b: %s: sdi was NULL, continuing.", | |
171 | __func__); | |
172 | continue; | |
173 | } | |
174 | if (!(ctx = sdi->priv)) { | |
175 | /* Log error, but continue cleaning up the rest. */ | |
176 | sr_err("va18b: %s: sdi->priv was NULL, continuing.", | |
177 | __func__); | |
178 | continue; | |
179 | } | |
180 | ||
163f0523 UH |
181 | /* TODO: Check for serial != NULL. */ |
182 | if (ctx->serial->fd != -1) | |
183 | serial_close(ctx->serial->fd); | |
184 | sr_serial_dev_inst_free(ctx->serial); | |
eb1f1eb4 UH |
185 | |
186 | sr_dev_inst_free(sdi); | |
187 | } | |
188 | ||
189 | g_slist_free(dev_insts); | |
190 | dev_insts = NULL; | |
191 | ||
192 | return SR_OK; | |
193 | } | |
194 | ||
195 | static const void *hw_dev_info_get(int dev_index, int dev_info_id) | |
196 | { | |
197 | struct sr_dev_inst *sdi; | |
198 | struct context *ctx; | |
199 | const void *info; | |
200 | uint64_t tmp; | |
201 | int tmpint; | |
202 | ||
203 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) { | |
204 | sr_err("va18b: %s: sdi was NULL.", __func__); | |
205 | return NULL; | |
206 | } | |
207 | ||
208 | if (!(ctx = sdi->priv)) { | |
209 | sr_err("va18b: %s: sdi->priv was NULL.", __func__); | |
210 | return NULL; | |
211 | } | |
212 | ||
213 | sr_spew("va18b: %s: dev_index %d, dev_info_id %d.", __func__, | |
214 | dev_index, dev_info_id); | |
215 | ||
216 | switch (dev_info_id) { | |
217 | case SR_DI_INST: | |
218 | info = sdi; | |
219 | sr_spew("va18b: %s: Returning sdi.", __func__); | |
220 | break; | |
221 | case SR_DI_NUM_PROBES: | |
222 | tmpint = 1; | |
223 | info = (int *)tmpint; | |
224 | sr_spew("va18b: %s: Returning number of probes: 1.", __func__); | |
225 | break; | |
226 | case SR_DI_PROBE_NAMES: | |
227 | info = probe_names; | |
228 | sr_spew("va18b: %s: Returning probenames.", __func__); | |
229 | break; | |
230 | case SR_DI_CUR_SAMPLERATE: | |
231 | /* FIXME */ | |
232 | tmp = 1; | |
233 | info = (uint64_t *)&tmp; | |
234 | sr_spew("va18b: %s: Returning samplerate: %" PRIu64 "Hz.", | |
235 | __func__, tmp); | |
236 | break; | |
237 | default: | |
238 | /* Unknown device info ID, return NULL. */ | |
239 | sr_err("va18b: %s: Unknown device info ID: %d.", | |
240 | __func__, dev_info_id); | |
241 | info = NULL; | |
242 | break; | |
243 | } | |
244 | ||
245 | return info; | |
246 | } | |
247 | ||
248 | static int hw_dev_status_get(int dev_index) | |
249 | { | |
250 | struct sr_dev_inst *sdi; | |
251 | ||
252 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) { | |
253 | sr_err("va18b: %s: sdi was NULL, device not found.", __func__); | |
254 | return SR_ST_NOT_FOUND; | |
255 | } | |
256 | ||
257 | sr_dbg("va18b: Returning status: %d.", sdi->status); | |
258 | ||
259 | return sdi->status; | |
260 | } | |
261 | ||
262 | static const int *hw_hwcap_get_all(void) | |
263 | { | |
264 | sr_spew("va18b: Returning list of device capabilities."); | |
265 | ||
266 | return hwcaps; | |
267 | } | |
268 | ||
269 | static int hw_dev_config_set(int dev_index, int hwcap, const void *value) | |
270 | { | |
271 | struct sr_dev_inst *sdi; | |
272 | struct context *ctx; | |
273 | ||
274 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) { | |
275 | sr_err("va18b: %s: sdi was NULL.", __func__); | |
276 | return SR_ERR_BUG; | |
277 | } | |
278 | ||
279 | if (!(ctx = sdi->priv)) { | |
280 | sr_err("va18b: %s: sdi->priv was NULL.", __func__); | |
281 | return SR_ERR_BUG; | |
282 | } | |
283 | ||
284 | sr_spew("va18b: %s: dev_index %d, hwcap %d.", __func__, | |
285 | dev_index, hwcap); | |
286 | ||
287 | switch (hwcap) { | |
288 | case SR_HWCAP_PROBECONFIG: | |
289 | /* TODO: Required? */ | |
290 | break; | |
291 | case SR_HWCAP_LIMIT_MSEC: | |
292 | if (*(const uint64_t *)value == 0) { | |
293 | sr_err("va18b: %s: LIMIT_MSEC can't be 0.", __func__); | |
294 | return SR_ERR; | |
295 | } | |
296 | ctx->limit_msec = *(const uint64_t *)value; | |
297 | sr_dbg("va18b: Setting LIMIT_MSEC to %" PRIu64 ".", | |
298 | ctx->limit_msec); | |
299 | break; | |
300 | case SR_HWCAP_LIMIT_SAMPLES: | |
301 | if (*(const uint64_t *)value < MIN_NUM_SAMPLES) { | |
302 | sr_err("va18b: %s: LIMIT_SAMPLES too small.", __func__); | |
303 | return SR_ERR; | |
304 | } | |
305 | ctx->limit_samples = *(const uint64_t *)value; | |
306 | sr_dbg("va18b: Setting LIMIT_SAMPLES to %" PRIu64 ".", | |
307 | ctx->limit_samples); | |
308 | break; | |
309 | default: | |
310 | /* Unknown capability, return SR_ERR. */ | |
311 | sr_err("va18b: %s: Unknown capability: %d.", __func__, hwcap); | |
312 | return SR_ERR; | |
313 | break; | |
314 | } | |
315 | ||
316 | return SR_OK; | |
317 | } | |
318 | ||
163f0523 UH |
319 | static uint8_t get_digit(uint8_t b1, uint8_t b2) |
320 | { | |
321 | uint8_t b; | |
322 | ||
323 | b = ((b1 & 0x0f) << 4) | (b2 & 0x0f); | |
324 | printf("0x%02x (b1: 0x%02x, b2: 0x%02x)\n", b, b1, b2); | |
325 | ||
326 | if (b == 0x7d) { | |
327 | printf("ret = 8\n"); | |
328 | return 0; | |
329 | } | |
330 | else if (b == 0x05) | |
331 | return 1; | |
332 | else if (b == 0x5b) | |
333 | return 2; | |
334 | else if (b == 0x1f) | |
335 | return 3; | |
336 | else if (b == 0x27) | |
337 | return 4; | |
338 | else if (b == 0x3e) | |
339 | return 5; | |
340 | else if (b == 0x7e) | |
341 | return 6; | |
342 | else if (b == 0x15) | |
343 | return 7; | |
344 | else if (b == 0x7f) | |
345 | return 8; | |
346 | else if (b == 0x3f) | |
347 | return 9; | |
348 | else | |
349 | return 0xff; /* Error */ | |
350 | ||
351 | } | |
352 | ||
eb1f1eb4 UH |
353 | static int receive_data(int fd, int revents, void *cb_data) |
354 | { | |
355 | struct sr_dev_inst *sdi; | |
356 | struct sr_datafeed_packet packet; | |
357 | struct sr_datafeed_analog analog; | |
358 | struct context *ctx; | |
359 | int num_probes; | |
163f0523 | 360 | uint8_t b, left_nibble, right_nibble; |
eb1f1eb4 UH |
361 | |
362 | if (!(sdi = cb_data)) { | |
363 | sr_err("va18b: %s: cb_data was NULL.", __func__); | |
364 | return FALSE; | |
365 | } | |
366 | ||
367 | if (!(ctx = sdi->priv)) { | |
368 | sr_err("va18b: %s: sdi->priv was NULL.", __func__); | |
369 | return FALSE; | |
370 | } | |
371 | ||
163f0523 UH |
372 | if (revents != G_IO_IN) { |
373 | sr_err("va18b: %s: No data?", __func__); | |
374 | return FALSE; | |
375 | } | |
376 | ||
377 | if (serial_read(fd, &b, 1) != 1) { | |
378 | sr_err("va18b: %s: Could not read a byte from serial port.", | |
379 | __func__); | |
380 | return FALSE; | |
381 | } | |
382 | ||
383 | left_nibble = ((b & 0xf0) >> 4); | |
384 | ||
385 | /* Upon starting, we wait until we're at byte 1. */ | |
386 | if (ctx->synchronized == FALSE && left_nibble != 1) { | |
387 | sr_spew("va18b: Waiting for byte #1 in order to synchronize " | |
388 | "(got byte #%d).", left_nibble); | |
389 | return TRUE; // FALSE? | |
390 | } else if (ctx->synchronized == FALSE && left_nibble == 1) { | |
391 | sr_dbg("va18b: Successfully synchronized to data stream."); | |
392 | ctx->synchronized = TRUE; | |
393 | ctx->byte_counter = 1; | |
394 | } | |
395 | ||
396 | /* TODO: Check for left_nibble in (1, 14). */ | |
397 | ||
398 | ctx->bytes[ctx->byte_counter++] = b; | |
399 | ||
400 | /// sr_dbg("va18b: bc: %d", ctx->byte_counter); | |
401 | if (ctx->byte_counter == 14 + 1) { | |
402 | sr_dbg("va18b: Received all 14 bytes for this packet."); | |
403 | ctx->got_14_bytes = TRUE; | |
404 | } else { | |
405 | sr_spew("va18b: Didn't receive all 14 bytes, yet."); | |
406 | return TRUE; // FALSE? | |
407 | } | |
408 | ||
409 | printf("0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x " | |
410 | "0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x\n", | |
411 | ctx->bytes[1], ctx->bytes[2], ctx->bytes[3], ctx->bytes[4], | |
412 | ctx->bytes[5], ctx->bytes[6], ctx->bytes[7], ctx->bytes[8], | |
413 | ctx->bytes[9], ctx->bytes[10], ctx->bytes[11], ctx->bytes[12], | |
414 | ctx->bytes[13], ctx->bytes[14]); | |
415 | ||
416 | printf("0x%02x %d %d %d\n", | |
417 | get_digit(ctx->bytes[2], ctx->bytes[3]), | |
418 | get_digit(ctx->bytes[4], ctx->bytes[5]), | |
419 | get_digit(ctx->bytes[6], ctx->bytes[7]), | |
420 | get_digit(ctx->bytes[8], ctx->bytes[9])); | |
421 | ||
422 | ctx->got_14_bytes = FALSE; | |
423 | ctx->byte_counter = 0; | |
424 | ||
eb1f1eb4 UH |
425 | sr_dbg("va18b: Sending SR_DF_ANALOG packet with 1 sample."); |
426 | /* TODO: timestamp. */ | |
427 | num_probes = 1; | |
428 | packet.type = SR_DF_ANALOG; | |
429 | packet.payload = &analog; | |
430 | analog.num_samples = 1; | |
431 | analog.mq = SR_MQ_VOLTAGE; | |
432 | analog.unit = SR_UNIT_VOLT; | |
433 | /* TODO: Check alloc return value. */ | |
434 | analog.data = g_try_malloc(analog.num_samples * sizeof(float) * num_probes); | |
163f0523 UH |
435 | // analog.data[0] = rand() % 42; /* Transmit dummy data for now. */ |
436 | analog.data[0] = b; /* Transmit dummy data for now. */ | |
eb1f1eb4 UH |
437 | sr_session_send(ctx->session_dev_id, &packet); |
438 | ||
439 | ctx->num_samples++; | |
440 | ||
441 | /* Support for SR_HWCAP_LIMIT_SAMPLES. */ | |
442 | if (ctx->limit_samples > 0 && ctx->num_samples > ctx->limit_samples) { | |
443 | hw_dev_acquisition_stop(0 /* FIXME? */, cb_data); | |
444 | return FALSE; | |
445 | } | |
446 | ||
447 | return TRUE; | |
448 | } | |
449 | ||
450 | static int hw_dev_acquisition_start(int dev_index, void *cb_data) | |
451 | { | |
452 | struct sr_datafeed_packet packet; | |
453 | struct sr_datafeed_header header; | |
454 | struct sr_datafeed_meta_analog meta; | |
455 | struct sr_dev_inst *sdi; | |
456 | struct context *ctx; | |
457 | ||
458 | if (!(sdi = sr_dev_inst_get(dev_insts, dev_index))) { | |
459 | sr_err("va18b: %s: sdi was NULL.", __func__); | |
460 | return SR_ERR_BUG; | |
461 | } | |
462 | ||
463 | if (!(ctx = sdi->priv)) { | |
464 | sr_err("va18b: %s: sdi->priv was NULL.", __func__); | |
465 | return SR_ERR_BUG; | |
466 | } | |
467 | ||
468 | sr_dbg("va18b: Starting acquisition."); | |
469 | ||
470 | ctx->session_dev_id = cb_data; | |
471 | ||
472 | /* Send header packet to the session bus. */ | |
473 | sr_dbg("va18b: Sending SR_DF_HEADER."); | |
474 | packet.type = SR_DF_HEADER; | |
475 | packet.payload = (uint8_t *)&header; | |
476 | header.feed_version = 1; | |
477 | gettimeofday(&header.starttime, NULL); | |
478 | sr_session_send(ctx->session_dev_id, &packet); | |
479 | ||
480 | /* Send metadata about the SR_DF_ANALOG packets to come. */ | |
481 | sr_dbg("va18b: Sending SR_DF_META_ANALOG."); | |
482 | packet.type = SR_DF_META_ANALOG; | |
483 | packet.payload = &meta; | |
484 | meta.num_probes = 1; | |
485 | sr_session_send(ctx->session_dev_id, &packet); | |
486 | ||
487 | /* Hook up a dummy handler to receive data from the device. */ | |
163f0523 UH |
488 | // sr_source_add(-1, G_IO_IN, 0, receive_data, sdi); |
489 | sr_source_add(ctx->serial->fd, G_IO_IN, -1, receive_data, sdi); | |
eb1f1eb4 UH |
490 | |
491 | return SR_OK; | |
492 | } | |
493 | ||
494 | static int hw_dev_acquisition_stop(int dev_index, void *cb_data) | |
495 | { | |
496 | struct sr_datafeed_packet packet; | |
497 | ||
498 | /* Avoid compiler warnings. */ | |
499 | (void)dev_index; | |
500 | ||
501 | sr_dbg("va18b: Stopping acquisition."); | |
502 | ||
503 | /* Send end packet to the session bus. */ | |
504 | sr_dbg("va18b: Sending SR_DF_END."); | |
505 | packet.type = SR_DF_END; | |
506 | sr_session_send(cb_data, &packet); | |
507 | ||
508 | return SR_OK; | |
509 | } | |
510 | ||
511 | SR_PRIV struct sr_dev_driver mastech_va18b_driver_info = { | |
512 | .name = "mastech-va18b", | |
513 | .longname = "Mastech VA18B", | |
514 | .api_version = 1, | |
515 | .init = hw_init, | |
516 | .cleanup = hw_cleanup, | |
517 | .dev_open = hw_dev_open, | |
518 | .dev_close = hw_dev_close, | |
519 | .dev_info_get = hw_dev_info_get, | |
520 | .dev_status_get = hw_dev_status_get, | |
521 | .hwcap_get_all = hw_hwcap_get_all, | |
522 | .dev_config_set = hw_dev_config_set, | |
523 | .dev_acquisition_start = hw_dev_acquisition_start, | |
524 | .dev_acquisition_stop = hw_dev_acquisition_stop, | |
525 | }; |