2 * This file is part of the libsigrok project.
4 * Copyright (C) 2020 Florian Schmidt <schmidt_florian@gmx.de>
5 * Copyright (C) 2013 Marcus Comstedt <marcus@mc.pp.se>
6 * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
7 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #ifndef LIBSIGROK_HARDWARE_KINGST_LA2016_PROTOCOL_H
24 #define LIBSIGROK_HARDWARE_KINGST_LA2016_PROTOCOL_H
28 #include <libsigrok/libsigrok.h>
29 #include "libsigrok-internal.h"
31 #define LOG_PREFIX "kingst-la2016"
33 /* device is little endian */
35 #define LA2016_VID 0x77a1
36 #define LA2016_PID 0x01a2
37 #define USB_INTERFACE 0
39 #define LA2016_BULK_MAX 8388608
41 #define MAX_RENUM_DELAY_MS 3000
42 #define DEFAULT_TIMEOUT_MS 200
44 #define LA2016_THR_VOLTAGE_MIN 0.40
45 #define LA2016_THR_VOLTAGE_MAX 4.00
47 #define LA2016_NUM_SAMPLES_MIN 256
48 #define LA2016_NUM_SAMPLES_MAX (10UL * 1000 * 1000 * 1000)
50 typedef struct pwm_setting_dev {
53 } __attribute__((__packed__)) pwm_setting_dev_t;
55 typedef struct trigger_cfg {
59 uint32_t high_or_falling;
60 } __attribute__((__packed__)) trigger_cfg_t;
62 typedef struct sample_config {
63 uint32_t sample_depth;
67 uint16_t clock_divisor;
68 } __attribute__((__packed__)) sample_config_t;
70 typedef struct capture_info {
71 uint32_t n_rep_packets;
72 uint32_t n_rep_packets_before_trigger;
74 } __attribute__((__packed__)) capture_info_t;
76 typedef struct acq_packet {
79 } __attribute__((__packed__)) acq_packet_t;
81 typedef struct transfer_packet {
82 acq_packet_t packet[5];
84 } __attribute__((__packed__)) transfer_packet_t;
86 typedef struct pwm_setting {
90 pwm_setting_dev_t dev;
94 struct sr_context *ctx;
97 pwm_setting_t pwm_setting[2];
98 unsigned int threshold_voltage_idx;
99 float threshold_voltage;
100 uint64_t cur_samplerate;
101 uint64_t limit_samples;
102 uint64_t capture_ratio;
103 uint16_t cur_channels;
107 uint64_t pre_trigger_size;
109 /* state after sampling */
110 int had_triggers_configured;
112 int transfer_finished;
114 unsigned int n_transfer_packets_to_read; /* each with 5 acq packets */
115 unsigned int n_bytes_to_read;
116 unsigned int n_reps_until_trigger;
117 unsigned int reading_behind_trigger;
118 uint64_t total_samples;
121 unsigned int convbuffer_size;
123 struct libusb_transfer *transfer;
126 SR_PRIV int la2016_upload_firmware(struct sr_context *sr_ctx, libusb_device *dev, uint16_t product_id);
127 SR_PRIV int la2016_setup_acquisition(const struct sr_dev_inst *sdi);
128 SR_PRIV int la2016_start_acquisition(const struct sr_dev_inst *sdi);
129 SR_PRIV int la2016_stop_acquisition(const struct sr_dev_inst *sdi);
130 SR_PRIV int la2016_abort_acquisition(const struct sr_dev_inst *sdi);
131 SR_PRIV int la2016_has_triggered(const struct sr_dev_inst *sdi);
132 SR_PRIV int la2016_start_retrieval(const struct sr_dev_inst *sdi, libusb_transfer_cb_fn cb);
133 SR_PRIV int la2016_init_device(const struct sr_dev_inst *sdi);
134 SR_PRIV int la2016_deinit_device(const struct sr_dev_inst *sdi);
136 #ifndef WORDS_BIGENDIAN
137 /* this host is big-endian, need to swap from/to device inplace */
138 #define inplace_WL32(obj) do { uint32_t tmp = obj; WL32(&(obj), tmp); } while (0)
139 #define inplace_RL32(obj) obj = RL32(&(obj))
140 #define inplace_WL16(obj) do { uint16_t tmp = obj; WL16(&(obj), tmp); } while (0)
141 #define inplace_RL16(obj) obj = RL16(&(obj))
143 #define pwm_setting_dev_le(obj) do { \
144 inplace_WL32((obj).period); \
145 inplace_WL32((obj).duty); \
147 #define trigger_cfg_le(obj) do { \
148 inplace_WL32((obj).channels); \
149 inplace_WL32((obj).enabled); \
150 inplace_WL32((obj).level); \
151 inplace_WL32((obj).high_or_falling); \
153 #define sample_config_le(obj) do { \
154 inplace_WL32((obj).sample_depth); \
155 inplace_WL32((obj).psa); \
156 inplace_WL16((obj).u1); \
157 inplace_WL32((obj).u2); \
158 inplace_WL16((obj).clock_divisor); \
161 #define capture_info_host(obj) do { \
162 inplace_RL32((obj).n_rep_packets); \
163 inplace_RL32((obj).n_rep_packets_before_trigger); \
164 inplace_RL32((obj).write_pos); \
166 #define acq_packet_host(obj) \
167 inplace_RL16((obj).state)
168 #define transfer_packet_host(obj) do { \
169 acq_packet_host((obj).packet[0]); \
170 acq_packet_host((obj).packet[1]); \
171 acq_packet_host((obj).packet[2]); \
172 acq_packet_host((obj).packet[3]); \
173 acq_packet_host((obj).packet[4]); \
177 /* this host is little-endian, same as device */
178 #define pwm_setting_dev_le(obj) (void)obj
179 #define trigger_cfg_le(obj) (void)obj
180 #define sample_config_le(obj) (void)obj
182 #define capture_info_host(obj) (void)obj
183 #define acq_packet_host(obj) (void)obj
184 #define transfer_packet_host(obj) (void)obj