protocol.h 7.0 KB

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