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