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