]> sigrok.org Git - libsigrok.git/blob - input/chronovu_la8.c
sr/cli/gtk/qt: s/device/dev/ in many places.
[libsigrok.git] / input / chronovu_la8.c
1 /*
2  * This file is part of the sigrok project.
3  *
4  * Copyright (C) 2011 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>
22 #include <fcntl.h>
23 #include <unistd.h>
24 #include "sigrok.h"
25 #include "sigrok-internal.h"
26
27 #define NUM_PACKETS             2048
28 #define PACKET_SIZE             4096
29 #define DEFAULT_NUM_PROBES      8
30
31 /**
32  * Convert the LA8 'divcount' value to the respective samplerate (in Hz).
33  *
34  * LA8 hardware: sample period = (divcount + 1) * 10ns.
35  * Min. value for divcount: 0x00 (10ns sample period, 100MHz samplerate).
36  * Max. value for divcount: 0xfe (2550ns sample period, 392.15kHz samplerate).
37  *
38  * @param divcount The divcount value as needed by the hardware.
39  *
40  * @return The samplerate in Hz, or 0xffffffffffffffff upon errors.
41  */
42 static uint64_t divcount_to_samplerate(uint8_t divcount)
43 {
44         if (divcount == 0xff)
45                 return 0xffffffffffffffffULL;
46
47         return SR_MHZ(100) / (divcount + 1);
48 }
49
50 static int format_match(const char *filename)
51 {
52         if (!filename) {
53                 sr_err("la8 in: %s: filename was NULL", __func__);
54                 // return SR_ERR; /* FIXME */
55                 return FALSE;
56         }
57
58         if (!g_file_test(filename, G_FILE_TEST_EXISTS)) {
59                 sr_err("la8 in: %s: input file '%s' does not exist",
60                        __func__, filename);
61                 // return SR_ERR; /* FIXME */
62                 return FALSE;
63         }
64
65         if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
66                 sr_err("la8 in: %s: input file '%s' not a regular file",
67                        __func__, filename);
68                 // return SR_ERR; /* FIXME */
69                 return FALSE;
70         }
71
72         /* TODO: Only accept files of length 8MB + 5 bytes. */
73
74         /* TODO: Check for divcount != 0xff. */
75
76         return TRUE;
77 }
78
79 static int init(struct sr_input *in)
80 {
81         int num_probes, i;
82         char name[SR_MAX_PROBENAME_LEN + 1];
83
84         if (in->param && in->param[0]) {
85                 num_probes = strtoul(in->param, NULL, 10);
86                 if (num_probes < 1) {
87                         sr_err("la8 in: %s: strtoul failed", __func__);
88                         return SR_ERR;
89                 }
90         } else {
91                 num_probes = DEFAULT_NUM_PROBES;
92         }
93
94         /* Create a virtual device. */
95         in->vdev = sr_dev_new(NULL, 0);
96
97         for (i = 0; i < num_probes; i++) {
98                 snprintf(name, SR_MAX_PROBENAME_LEN, "%d", i);
99                 /* TODO: Check return value. */
100                 sr_dev_probe_add(in->vdev, name);
101         }
102
103         return SR_OK;
104 }
105
106 static int loadfile(struct sr_input *in, const char *filename)
107 {
108         struct sr_datafeed_header header;
109         struct sr_datafeed_packet packet;
110         struct sr_datafeed_logic logic;
111         uint8_t buf[PACKET_SIZE], divcount;
112         int i, fd, size, num_probes;
113         uint64_t samplerate;
114
115         /* TODO: Use glib functions! GIOChannel, g_fopen, etc. */
116         if ((fd = open(filename, O_RDONLY)) == -1) {
117                 sr_err("la8 in: %s: file open failed", __func__);
118                 return SR_ERR;
119         }
120
121         num_probes = g_slist_length(in->vdev->probes);
122
123         /* Seek to the end of the file, and read the divcount byte. */
124         divcount = 0x00; /* TODO: Don't hardcode! */
125
126         /* Convert the divcount value to a samplerate. */
127         samplerate = divcount_to_samplerate(divcount);
128         if (samplerate == 0xffffffffffffffffULL) {
129                 close(fd); /* FIXME */
130                 return SR_ERR;
131         }
132         sr_dbg("la8 in: %s: samplerate is %" PRIu64, __func__, samplerate);
133
134         /* Send header packet to the session bus. */
135         sr_dbg("la8 in: %s: sending SR_DF_HEADER packet", __func__);
136         packet.type = SR_DF_HEADER;
137         packet.payload = &header;
138         header.feed_version = 1;
139         gettimeofday(&header.starttime, NULL);
140         header.num_logic_probes = num_probes;
141         header.samplerate = samplerate;
142         sr_session_bus(in->vdev, &packet);
143
144         /* TODO: Handle trigger point. */
145
146         /* Send data packets to the session bus. */
147         sr_dbg("la8 in: %s: sending SR_DF_LOGIC data packets", __func__);
148         packet.type = SR_DF_LOGIC;
149         packet.payload = &logic;
150         logic.unitsize = (num_probes + 7) / 8;
151         logic.data = buf;
152
153         /* Send 8MB of total data to the session bus in small chunks. */
154         for (i = 0; i < NUM_PACKETS; i++) {
155                 /* TODO: Handle errors, handle incomplete reads. */
156                 size = read(fd, buf, PACKET_SIZE);
157                 logic.length = PACKET_SIZE;
158                 sr_session_bus(in->vdev, &packet);
159         }
160         close(fd); /* FIXME */
161
162         /* Send end packet to the session bus. */
163         sr_dbg("la8 in: %s: sending SR_DF_END", __func__);
164         packet.type = SR_DF_END;
165         packet.payload = NULL;
166         sr_session_bus(in->vdev, &packet);
167
168         return SR_OK;
169 }
170
171 SR_PRIV struct sr_input_format input_chronovu_la8 = {
172         .id = "chronovu-la8",
173         .description = "ChronoVu LA8",
174         .format_match = format_match,
175         .init = init,
176         .loadfile = loadfile,
177 };