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