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