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