]> sigrok.org Git - libsigrok.git/blame - src/hardware/ikalogic-scanalogic2/protocol.h
windows: Fix various compiler warnings.
[libsigrok.git] / src / hardware / ikalogic-scanalogic2 / protocol.h
CommitLineData
16e76bae
MS
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013 Marc Schink <sigrok-dev@marcschink.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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20#ifndef LIBSIGROK_HARDWARE_IKALOGIC_SCANALOGIC2_PROTOCOL_H
21#define LIBSIGROK_HARDWARE_IKALOGIC_SCANALOGIC2_PROTOCOL_H
22
e52e712d
MS
23#include <stdlib.h>
24#include <string.h>
16e76bae
MS
25#include <stdint.h>
26#include <glib.h>
27#include "libsigrok.h"
28#include "libsigrok-internal.h"
29
3544f848 30#define LOG_PREFIX "ikalogic-scanalogic2"
16e76bae 31
e52e712d
MS
32#define VENDOR_NAME "IKALOGIC"
33#define MODEL_NAME "Scanalogic-2"
34
35#define USB_VID_PID "20a0.4123"
36#define USB_INTERFACE 0
1a46cc62 37#define USB_TIMEOUT_MS (5 * 1000)
e52e712d
MS
38
39#define USB_REQUEST_TYPE_IN (LIBUSB_REQUEST_TYPE_CLASS | \
40 LIBUSB_RECIPIENT_INTERFACE | LIBUSB_ENDPOINT_IN)
41
42#define USB_REQUEST_TYPE_OUT (LIBUSB_REQUEST_TYPE_CLASS | \
43 LIBUSB_RECIPIENT_INTERFACE | LIBUSB_ENDPOINT_OUT)
44
fb8d593c 45#define USB_HID_GET_REPORT 0x01
e52e712d
MS
46#define USB_HID_SET_REPORT 0x09
47#define USB_HID_REPORT_TYPE_FEATURE 0x300
48
49#define NUM_SAMPLERATES 11
3f239f08 50#define NUM_CHANNELS 4
e52e712d 51
e52e712d
MS
52/*
53 * Number of sample bytes and samples the device can acquire. Note that the
54 * vendor software can acquire 32736 sample bytes only but the device is capable
55 * to acquire up to 32766 sample bytes.
56 */
57#define MAX_DEV_SAMPLE_BYTES 32766
58#define MAX_DEV_SAMPLES (MAX_INT_SAMPLE_BYTES * 8)
59
60/* Number of sample bytes and samples the driver can acquire. */
61#define MAX_SAMPLE_BYTES (MAX_DEV_SAMPLE_BYTES - 1)
62#define MAX_SAMPLES (MAX_SAMPLE_BYTES * 8)
63
64/* Maximum time that the trigger can be delayed in milliseconds. */
65#define MAX_AFTER_TRIGGER_DELAY 65000
66
67#define PACKET_LENGTH 128
68
69/* Number of sample bytes per packet where a sample byte contains 8 samples. */
70#define PACKET_NUM_SAMPLE_BYTES 124
71
72/* Number of samples per packet. */
73#define PACKET_NUM_SAMPLES (PACKET_NUM_SAMPLE_BYTES * 8)
74
75#define DEFAULT_SAMPLERATE SR_KHZ(1.25)
76
77/*
78 * Time interval between the last status of available data received and the
79 * moment when the next status request will be sent in microseconds.
80 */
81#define WAIT_DATA_READY_INTERVAL 1500000
82
83#define CMD_SAMPLE 0x01
84#define CMD_RESET 0x02
85#define CMD_IDLE 0x07
86#define CMD_INFO 0x0a
87
88#define TRIGGER_CHANNEL_ALL 0x00
89#define TRIGGER_CHANNEL_0 0x01
90#define TRIGGER_CHANNEL_1 0x02
91#define TRIGGER_CHANNEL_2 0x03
92
93#define TRIGGER_TYPE_NEGEDGE 0x00
94#define TRIGGER_TYPE_POSEDGE 0x01
95#define TRIGGER_TYPE_ANYEDGE 0x02
96#define TRIGGER_TYPE_NONE 0x03
97
98#define STATUS_DATA_READY 0x60
99#define STATUS_WAITING_FOR_TRIGGER 0x61
100#define STATUS_SAMPLING 0x62
101#define STATUS_DEVICE_READY 0x63
102
103struct device_info {
104 /* Serial number of the device. */
105 uint32_t serial;
106
107 /* Major version of the firmware. */
108 uint8_t fw_ver_major;
109
110 /* Minor version of the firmware. */
111 uint8_t fw_ver_minor;
112};
113
114enum {
115 STATE_IDLE = 0,
116 STATE_SAMPLE,
117 STATE_WAIT_DATA_READY,
118 STATE_RECEIVE_DATA,
119 STATE_RESET_AND_IDLE,
120 STATE_WAIT_DEVICE_READY
121};
122
16e76bae
MS
123/** Private, per-device-instance driver context. */
124struct dev_context {
e52e712d
MS
125 /* Current selected samplerate. */
126 uint64_t samplerate;
127
128 /* Device specific identifier for the current samplerate. */
129 uint8_t samplerate_id;
130
131 /* Current sampling limit. */
132 uint64_t limit_samples;
133
134 /* Calculated number of pre-trigger samples. */
135 uint64_t pre_trigger_samples;
136
137 /* Number of pre- and post-trigger sample bytes to acquire. */
138 uint16_t pre_trigger_bytes;
139 uint16_t post_trigger_bytes;
140
141 /* Device specific settings for the trigger. */
142 uint8_t trigger_channel;
143 uint8_t trigger_type;
144
145 unsigned int capture_ratio;
146
147 /* Time that the trigger will be delayed in milliseconds. */
148 uint16_t after_trigger_delay;
149
150 void *cb_data;
151
ba7dd8bb 152 /* Array to provide an index based access to all channels. */
3f239f08 153 const struct sr_channel *channels[NUM_CHANNELS];
e52e712d 154
e52e712d
MS
155 struct libusb_transfer *xfer_in, *xfer_out;
156
157 /*
158 * Buffer to store setup and payload data for incoming and outgoing
159 * transfers.
160 */
161 uint8_t xfer_buf_in[LIBUSB_CONTROL_SETUP_SIZE + PACKET_LENGTH];
162 uint8_t xfer_buf_out[LIBUSB_CONTROL_SETUP_SIZE + PACKET_LENGTH];
163
164 /* Pointers to the payload of incoming and outgoing transfers. */
165 uint8_t *xfer_data_in, *xfer_data_out;
166
167 /* Current state of the state machine */
168 unsigned int state;
169
170 /* Next state of the state machine. */
171 unsigned int next_state;
172
173 /*
174 * Locking variable to ensure that no status about available data will
175 * be requested until the last status was received.
176 */
177 gboolean wait_data_ready_locked;
178
179 /*
180 * Time when the last response about the status of available data was
181 * received.
182 */
183 int64_t wait_data_ready_time;
184
185 /*
186 * Indicates that stopping of the acquisition is currently in progress.
187 */
188 gboolean stopping_in_progress;
189
190 /*
191 * Buffer which contains the samples received from the device for each
192 * channel except the last one. The samples of the last channel will be
193 * processed directly after they will be received.
194 */
3f239f08 195 uint8_t sample_buffer[NUM_CHANNELS - 1][MAX_DEV_SAMPLE_BYTES];
e52e712d
MS
196
197 /* Expected number of sample packets for each channel. */
198 uint16_t num_sample_packets;
199
200 /* Number of samples already processed. */
201 uint64_t samples_processed;
202
203 /* Sample packet number that is currently processed. */
204 uint16_t sample_packet;
16e76bae 205
e52e712d
MS
206 /* Channel number that is currently processed. */
207 uint8_t channel;
16e76bae 208
ba7dd8bb
UH
209 /* Number of enabled channels. */
210 unsigned int num_enabled_channels;
16e76bae 211
ba7dd8bb 212 /* Array to provide a sequential access to all enabled channel indices. */
3f239f08 213 uint8_t channel_map[NUM_CHANNELS];
16e76bae 214
e52e712d
MS
215 /* Indicates whether a transfer failed. */
216 gboolean transfer_error;
16e76bae
MS
217};
218
79914b3a 219SR_PRIV int ikalogic_scanalogic2_receive_data(int fd, int revents, void *cb_data);
55462b8b
UH
220SR_PRIV void LIBUSB_CALL sl2_receive_transfer_in(struct libusb_transfer *transfer);
221SR_PRIV void LIBUSB_CALL sl2_receive_transfer_out(struct libusb_transfer *transfer);
79914b3a 222SR_PRIV int sl2_set_samplerate(const struct sr_dev_inst *sdi,
e52e712d 223 uint64_t samplerate);
79914b3a
UH
224SR_PRIV int sl2_set_limit_samples(const struct sr_dev_inst *sdi,
225 uint64_t limit_samples);
02d5c0d8 226SR_PRIV int sl2_convert_trigger(const struct sr_dev_inst *sdi);
79914b3a
UH
227SR_PRIV int sl2_set_capture_ratio(const struct sr_dev_inst *sdi,
228 uint64_t capture_ratio);
229SR_PRIV int sl2_set_after_trigger_delay(const struct sr_dev_inst *sdi,
230 uint64_t after_trigger_delay);
231SR_PRIV void sl2_calculate_trigger_samples(const struct sr_dev_inst *sdi);
232SR_PRIV int sl2_get_device_info(struct sr_usb_dev_inst usb,
e52e712d 233 struct device_info *dev_info);
79914b3a
UH
234SR_PRIV int sl2_transfer_in(libusb_device_handle *dev_handle, uint8_t *data);
235SR_PRIV int sl2_transfer_out(libusb_device_handle *dev_handle, uint8_t *data);
16e76bae
MS
236
237#endif