2 * This file is part of the sigrok project.
4 * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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
27 #define NUM_TRIGGER_STAGES 4
28 #define TRIGGER_TYPES "01"
30 /* Software trigger implementation: positive values indicate trigger stage. */
31 #define TRIGGER_FIRED -1
33 static int capabilities[] = {
39 /* Random selection of samplerates this "device" shall support. */
40 static uint64_t supported_samplerates[] = {
50 static struct samplerates samplerates = {
54 supported_samplerates,
57 /* TODO: All of these should go in a device-specific struct. */
58 static uint64_t cur_samplerate = 0;
59 static uint64_t limit_samples = 0;
60 // static uint8_t probe_mask = 0;
61 // static uint8_t trigger_mask[NUM_TRIGGER_STAGES] = { 0 };
62 // static uint8_t trigger_value[NUM_TRIGGER_STAGES] = { 0 };
63 // static uint8_t trigger_buffer[NUM_TRIGGER_STAGES] = { 0 };
65 // static int trigger_stage = TRIGGER_FIRED;
67 static int hw_set_configuration(int device_index, int capability, void *value);
68 static void hw_stop_acquisition(int device_index, gpointer session_device_id);
71 static int configure_probes(GSList *probes)
75 int probe_bit, stage, i;
79 for (i = 0; i < NUM_TRIGGER_STAGES; i++) {
85 for (l = probes; l; l = l->next) {
86 probe = (struct probe *)l->data;
87 if (!(probe->enabled))
89 probe_bit = 1 << (probe->index - 1);
90 probe_mask |= probe_bit;
91 if (!(probe->trigger))
95 for (tc = probe->trigger; *tc; tc++) {
96 trigger_mask[stage] |= probe_bit;
98 trigger_value[stage] |= probe_bit;
100 if (stage > NUM_TRIGGER_STAGES)
107 * We didn't configure any triggers, make sure acquisition
108 * doesn't wait for any.
110 trigger_stage = TRIGGER_FIRED;
118 static int hw_init(char *deviceinfo)
120 /* Avoid compiler warning. */
121 deviceinfo = deviceinfo;
123 /* Nothing needed so far. */
124 return 1; /* FIXME? */
127 static int hw_opendev(int device_index)
129 /* Avoid compiler warning. */
130 device_index = device_index;
132 /* Nothing needed so far. */
136 static void hw_closedev(int device_index)
138 /* Avoid compiler warning. */
139 device_index = device_index;
141 /* Nothing needed so far. */
144 static void hw_cleanup(void)
146 /* Nothing needed so far. */
149 static void *hw_get_device_info(int device_index, int device_info_id)
153 /* Avoid compiler warning. */
154 device_index = device_index;
156 switch (device_info_id) {
162 info = GINT_TO_POINTER(NUM_PROBES);
167 case DI_TRIGGER_TYPES:
168 info = TRIGGER_TYPES;
170 case DI_CUR_SAMPLERATE:
171 info = &cur_samplerate;
178 static int hw_get_status(int device_index)
180 /* Avoid compiler warning. */
181 device_index = device_index;
183 return 0; /* FIXME */
186 static int *hw_get_capabilities(void)
191 static int hw_set_configuration(int device_index, int capability, void *value)
195 /* Avoid compiler warning. */
196 device_index = device_index;
198 if (capability == HWCAP_SAMPLERATE) {
199 cur_samplerate = *(uint64_t *)value;
201 } else if (capability == HWCAP_PROBECONFIG) {
202 // ret = configure_probes((GSList *) value);
204 } else if (capability == HWCAP_LIMIT_SAMPLES) {
205 limit_samples = strtoull(value, NULL, 10);
214 static int hw_start_acquisition(int device_index, gpointer session_device_id)
216 struct datafeed_packet *packet;
217 struct datafeed_header *header;
220 /* Avoid compiler warning. */
221 device_index = device_index;
223 packet = malloc(sizeof(struct datafeed_packet));
224 header = malloc(sizeof(struct datafeed_header));
226 if (!packet || !header || !buf)
227 return SIGROK_ERR_MALLOC;
230 memset(buf, 0x55, 2048);
232 packet->type = DF_HEADER;
233 packet->length = sizeof(struct datafeed_header);
234 packet->payload = (unsigned char *)header;
235 header->feed_version = 1;
236 gettimeofday(&header->starttime, NULL);
237 header->samplerate = cur_samplerate;
238 header->protocol_id = PROTO_RAW;
239 header->num_probes = NUM_PROBES;
240 session_bus(session_device_id, packet);
248 void receive_transfer(struct libusb_transfer *transfer)
250 static int num_samples = 0;
251 static int empty_transfer_count = 0;
252 struct datafeed_packet packet;
254 int cur_buflen, trigger_offset, i;
255 unsigned char *cur_buf, *new_buf;
257 g_message("receive_transfer(): status %d received %d bytes",
258 transfer->status, transfer->actual_length);
260 /* Save incoming transfer before reusing the transfer struct. */
261 cur_buf = transfer->buffer;
262 cur_buflen = transfer->actual_length;
263 user_data = transfer->user_data;
265 /* Fire off a new request. */
266 new_buf = g_malloc(4096);
267 transfer->buffer = new_buf;
268 transfer->length = 4096;
269 if (libusb_submit_transfer(transfer) != 0) {
270 /* TODO: Stop session? */
275 if (trigger_stage >= 0) {
276 for (i = 0; i < cur_buflen; i++) {
277 trigger_helper(i, cur_buf, &packet, user_data,
282 if (trigger_stage == TRIGGER_FIRED) {
283 /* Send the incoming transfer to the session bus. */
284 packet.type = DF_LOGIC8;
285 packet.length = cur_buflen - trigger_offset;
286 packet.payload = cur_buf + trigger_offset;
287 session_bus(user_data, &packet);
290 num_samples += cur_buflen;
291 if ((unsigned int)num_samples > limit_samples) {
292 hw_stop_acquisition(-1, user_data);
296 * TODO: Buffer pre-trigger data in capture
297 * ratio-sized buffer.
303 /* This stops acquisition on ALL devices, ignoring device_index. */
304 static void hw_stop_acquisition(int device_index, gpointer session_device_id)
306 struct datafeed_packet packet;
309 device_index = device_index;
311 packet.type = DF_END;
312 session_bus(session_device_id, &packet);
314 /// receive_transfer(NULL);
316 /* TODO: Need to cancel and free any queued up transfers. */
319 struct device_plugin demo_plugin_info = {
329 hw_set_configuration,
330 hw_start_acquisition,