2 * This file is part of the libsigrok project.
4 * Copyright (C) 2013 Marc Schink <sigrok-dev@marcschink.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 3 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, see <http://www.gnu.org/licenses/>.
20 #ifndef LIBSIGROK_HARDWARE_IKALOGIC_SCANALOGIC2_PROTOCOL_H
21 #define LIBSIGROK_HARDWARE_IKALOGIC_SCANALOGIC2_PROTOCOL_H
27 #include "libsigrok.h"
28 #include "libsigrok-internal.h"
30 /* Message logging helpers with subsystem-specific prefix string. */
31 #define LOG_PREFIX "ikalogic-scanalogic2: "
32 #define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args)
33 #define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args)
34 #define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args)
35 #define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args)
36 #define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args)
37 #define sr_err(s, args...) sr_err(LOG_PREFIX s, ## args)
39 #define VENDOR_NAME "IKALOGIC"
40 #define MODEL_NAME "Scanalogic-2"
42 #define USB_VID_PID "20a0.4123"
43 #define USB_INTERFACE 0
44 #define USB_TIMEOUT 5000
46 #define USB_REQUEST_TYPE_IN (LIBUSB_REQUEST_TYPE_CLASS | \
47 LIBUSB_RECIPIENT_INTERFACE | LIBUSB_ENDPOINT_IN)
49 #define USB_REQUEST_TYPE_OUT (LIBUSB_REQUEST_TYPE_CLASS | \
50 LIBUSB_RECIPIENT_INTERFACE | LIBUSB_ENDPOINT_OUT)
52 #define USB_HID_SET_REPORT 0x09
53 #define USB_HID_REPORT_TYPE_FEATURE 0x300
55 #define NUM_SAMPLERATES 11
58 #define TRIGGER_TYPES "rfc"
61 * Number of sample bytes and samples the device can acquire. Note that the
62 * vendor software can acquire 32736 sample bytes only but the device is capable
63 * to acquire up to 32766 sample bytes.
65 #define MAX_DEV_SAMPLE_BYTES 32766
66 #define MAX_DEV_SAMPLES (MAX_INT_SAMPLE_BYTES * 8)
68 /* Number of sample bytes and samples the driver can acquire. */
69 #define MAX_SAMPLE_BYTES (MAX_DEV_SAMPLE_BYTES - 1)
70 #define MAX_SAMPLES (MAX_SAMPLE_BYTES * 8)
72 /* Maximum time that the trigger can be delayed in milliseconds. */
73 #define MAX_AFTER_TRIGGER_DELAY 65000
75 #define PACKET_LENGTH 128
77 /* Number of sample bytes per packet where a sample byte contains 8 samples. */
78 #define PACKET_NUM_SAMPLE_BYTES 124
80 /* Number of samples per packet. */
81 #define PACKET_NUM_SAMPLES (PACKET_NUM_SAMPLE_BYTES * 8)
83 #define DEFAULT_SAMPLERATE SR_KHZ(1.25)
86 * Time interval between the last status of available data received and the
87 * moment when the next status request will be sent in microseconds.
89 #define WAIT_DATA_READY_INTERVAL 1500000
91 #define CMD_SAMPLE 0x01
92 #define CMD_RESET 0x02
96 #define TRIGGER_CHANNEL_ALL 0x00
97 #define TRIGGER_CHANNEL_0 0x01
98 #define TRIGGER_CHANNEL_1 0x02
99 #define TRIGGER_CHANNEL_2 0x03
101 #define TRIGGER_TYPE_NEGEDGE 0x00
102 #define TRIGGER_TYPE_POSEDGE 0x01
103 #define TRIGGER_TYPE_ANYEDGE 0x02
104 #define TRIGGER_TYPE_NONE 0x03
106 #define STATUS_DATA_READY 0x60
107 #define STATUS_WAITING_FOR_TRIGGER 0x61
108 #define STATUS_SAMPLING 0x62
109 #define STATUS_DEVICE_READY 0x63
112 /* Serial number of the device. */
115 /* Major version of the firmware. */
116 uint8_t fw_ver_major;
118 /* Minor version of the firmware. */
119 uint8_t fw_ver_minor;
125 STATE_WAIT_DATA_READY,
127 STATE_RESET_AND_IDLE,
128 STATE_WAIT_DEVICE_READY
131 /** Private, per-device-instance driver context. */
133 /* Current selected samplerate. */
136 /* Device specific identifier for the current samplerate. */
137 uint8_t samplerate_id;
139 /* Current sampling limit. */
140 uint64_t limit_samples;
142 /* Calculated number of pre-trigger samples. */
143 uint64_t pre_trigger_samples;
145 /* Number of pre- and post-trigger sample bytes to acquire. */
146 uint16_t pre_trigger_bytes;
147 uint16_t post_trigger_bytes;
149 /* Device specific settings for the trigger. */
150 uint8_t trigger_channel;
151 uint8_t trigger_type;
153 unsigned int capture_ratio;
155 /* Time that the trigger will be delayed in milliseconds. */
156 uint16_t after_trigger_delay;
160 /* Array to provide an index based access to all probes. */
161 const struct sr_probe *probes[NUM_PROBES];
163 unsigned int num_usbfd;
166 struct libusb_transfer *xfer_in, *xfer_out;
169 * Buffer to store setup and payload data for incoming and outgoing
172 uint8_t xfer_buf_in[LIBUSB_CONTROL_SETUP_SIZE + PACKET_LENGTH];
173 uint8_t xfer_buf_out[LIBUSB_CONTROL_SETUP_SIZE + PACKET_LENGTH];
175 /* Pointers to the payload of incoming and outgoing transfers. */
176 uint8_t *xfer_data_in, *xfer_data_out;
178 /* Current state of the state machine */
181 /* Next state of the state machine. */
182 unsigned int next_state;
185 * Locking variable to ensure that no status about available data will
186 * be requested until the last status was received.
188 gboolean wait_data_ready_locked;
191 * Time when the last response about the status of available data was
194 int64_t wait_data_ready_time;
197 * Indicates that stopping of the acquisition is currently in progress.
199 gboolean stopping_in_progress;
202 * Buffer which contains the samples received from the device for each
203 * channel except the last one. The samples of the last channel will be
204 * processed directly after they will be received.
206 uint8_t sample_buffer[NUM_PROBES - 1][MAX_DEV_SAMPLE_BYTES];
208 /* Expected number of sample packets for each channel. */
209 uint16_t num_sample_packets;
211 /* Number of samples already processed. */
212 uint64_t samples_processed;
214 /* Sample packet number that is currently processed. */
215 uint16_t sample_packet;
217 /* Channel number that is currently processed. */
220 /* Number of enabled probes. */
221 unsigned int num_enabled_probes;
223 /* Array to provide a sequential access to all enabled probe indices. */
224 uint8_t probe_map[NUM_PROBES];
226 /* Indicates whether a transfer failed. */
227 gboolean transfer_error;
230 SR_PRIV int ikalogic_scanalogic2_receive_data(int fd, int revents,
232 SR_PRIV void ikalogic_scanalogic2_receive_transfer_in(
233 struct libusb_transfer *transfer);
234 SR_PRIV void ikalogic_scanalogic2_receive_transfer_out(
235 struct libusb_transfer *transfer);
236 SR_PRIV int ikalogic_scanalogic2_set_samplerate(const struct sr_dev_inst *sdi,
237 uint64_t samplerate);
238 SR_PRIV int ikalogic_scanalogic2_set_limit_samples(
239 const struct sr_dev_inst *sdi, uint64_t limit_samples);
240 SR_PRIV void ikalogic_scanalogic2_configure_trigger(
241 const struct sr_dev_inst *sdi);
242 SR_PRIV int ikalogic_scanalogic2_set_capture_ratio(
243 const struct sr_dev_inst *sdi, uint64_t capture_ratio);
244 SR_PRIV int ikalogic_scanalogic2_set_after_trigger_delay(
245 const struct sr_dev_inst *sdi, uint64_t after_trigger_delay);
246 SR_PRIV void ikalogic_scanalogic2_calculate_trigger_samples(
247 const struct sr_dev_inst *sdi);
248 SR_PRIV int ikalogic_scanalogic2_get_device_info(struct sr_usb_dev_inst usb,
249 struct device_info *dev_info);
250 SR_PRIV int ikalogic_scanalogic2_transfer_in(libusb_device_handle *dev_handle,
252 SR_PRIV int ikalogic_scanalogic2_transfer_out(libusb_device_handle *dev_handle,