]> sigrok.org Git - libsigrok.git/blob - src/hardware/kingst-la2016/protocol.h
kingst-la2016: Upload firmware to correct USB configuration 1
[libsigrok.git] / src / hardware / kingst-la2016 / protocol.h
1 /*
2  * This file is part of the libsigrok project.
3  *
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>
8  *
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.
13  *
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.
18  *
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/>.
21  */
22
23 #ifndef LIBSIGROK_HARDWARE_KINGST_LA2016_PROTOCOL_H
24 #define LIBSIGROK_HARDWARE_KINGST_LA2016_PROTOCOL_H
25
26 #include <stdint.h>
27 #include <glib.h>
28 #include <libsigrok/libsigrok.h>
29 #include "libsigrok-internal.h"
30
31 #define LOG_PREFIX "kingst-la2016"
32
33 /* device is little endian */
34
35 #define LA2016_VID              0x77a1
36 #define LA2016_PID              0x01a2
37 #define USB_INTERFACE           0
38 #define USB_CONFIGURATION       1
39
40 #define LA2016_BULK_MAX         8388608
41
42 #define MAX_RENUM_DELAY_MS      3000
43 #define DEFAULT_TIMEOUT_MS      200
44
45 #define LA2016_THR_VOLTAGE_MIN  0.40
46 #define LA2016_THR_VOLTAGE_MAX  4.00
47
48 #define LA2016_NUM_SAMPLES_MIN  256
49 #define LA2016_NUM_SAMPLES_MAX  (10UL * 1000 * 1000 * 1000)
50
51 typedef struct pwm_setting_dev {
52         uint32_t period;
53         uint32_t duty;
54 } __attribute__((__packed__)) pwm_setting_dev_t;
55
56 typedef struct trigger_cfg {
57         uint32_t channels;
58         uint32_t enabled;
59         uint32_t level;
60         uint32_t high_or_falling;
61 } __attribute__((__packed__)) trigger_cfg_t;
62
63 typedef struct sample_config {
64         uint32_t sample_depth;
65         uint32_t psa;
66         uint16_t u1;
67         uint32_t u2;
68         uint16_t clock_divisor;
69 } __attribute__((__packed__)) sample_config_t;
70
71 typedef struct capture_info {
72         uint32_t n_rep_packets;
73         uint32_t n_rep_packets_before_trigger;
74         uint32_t write_pos;
75 } __attribute__((__packed__)) capture_info_t;
76
77 typedef struct acq_packet {
78         uint16_t state;
79         uint8_t repetitions;
80 } __attribute__((__packed__)) acq_packet_t;
81
82 typedef struct transfer_packet {
83         acq_packet_t packet[5];
84         uint8_t seq;
85 } __attribute__((__packed__)) transfer_packet_t;
86
87 typedef struct pwm_setting {
88         uint8_t enabled;
89         float freq;
90         float duty;
91         pwm_setting_dev_t dev;
92 } pwm_setting_t;
93
94 struct dev_context {
95         struct sr_context *ctx;
96
97         int64_t fw_updated;
98         pwm_setting_t pwm_setting[2];
99         unsigned int threshold_voltage_idx;
100         float threshold_voltage;
101         uint64_t cur_samplerate;
102         uint64_t limit_samples;
103         uint64_t capture_ratio;
104         uint16_t cur_channels;
105         int num_channels;
106
107         uint32_t bitstream_size;
108
109         /* derived stuff */
110         uint64_t pre_trigger_size;
111
112         /* state after sampling */
113         int had_triggers_configured;
114         int have_trigger;
115         int transfer_finished;
116         capture_info_t info;
117         unsigned int n_transfer_packets_to_read; /* each with 5 acq packets */
118         unsigned int n_bytes_to_read;
119         unsigned int n_reps_until_trigger;
120         unsigned int reading_behind_trigger;
121         uint64_t total_samples;
122         uint32_t read_pos;
123
124         unsigned int convbuffer_size;
125         uint8_t *convbuffer;
126         struct libusb_transfer *transfer;
127 };
128
129 SR_PRIV int la2016_upload_firmware(struct sr_context *sr_ctx, libusb_device *dev, uint16_t product_id);
130 SR_PRIV int la2016_setup_acquisition(const struct sr_dev_inst *sdi);
131 SR_PRIV int la2016_start_acquisition(const struct sr_dev_inst *sdi);
132 SR_PRIV int la2016_stop_acquisition(const struct sr_dev_inst *sdi);
133 SR_PRIV int la2016_abort_acquisition(const struct sr_dev_inst *sdi);
134 SR_PRIV int la2016_has_triggered(const struct sr_dev_inst *sdi);
135 SR_PRIV int la2016_start_retrieval(const struct sr_dev_inst *sdi, libusb_transfer_cb_fn cb);
136 SR_PRIV int la2016_init_device(const struct sr_dev_inst *sdi);
137 SR_PRIV int la2016_deinit_device(const struct sr_dev_inst *sdi);
138
139 #ifndef WORDS_BIGENDIAN
140 /* this host is big-endian, need to swap from/to device inplace */
141 #define inplace_WL32(obj) do { uint32_t tmp = obj; WL32(&(obj), tmp); } while (0)
142 #define inplace_RL32(obj) obj = RL32(&(obj))
143 #define inplace_WL16(obj) do { uint16_t tmp = obj; WL16(&(obj), tmp); } while (0)
144 #define inplace_RL16(obj) obj = RL16(&(obj))
145
146 #define pwm_setting_dev_le(obj) do {            \
147                 inplace_WL32((obj).period);     \
148                 inplace_WL32((obj).duty);       \
149         } while (0)
150 #define trigger_cfg_le(obj) do {                        \
151                 inplace_WL32((obj).channels);           \
152                 inplace_WL32((obj).enabled);            \
153                 inplace_WL32((obj).level);              \
154                 inplace_WL32((obj).high_or_falling);    \
155         } while (0)
156 #define sample_config_le(obj) do {                      \
157                 inplace_WL32((obj).sample_depth);       \
158                 inplace_WL32((obj).psa);                \
159                 inplace_WL16((obj).u1);                 \
160                 inplace_WL32((obj).u2);                 \
161                 inplace_WL16((obj).clock_divisor);      \
162         } while (0)
163
164 #define capture_info_host(obj) do {                                     \
165                 inplace_RL32((obj).n_rep_packets);                      \
166                 inplace_RL32((obj).n_rep_packets_before_trigger);       \
167                 inplace_RL32((obj).write_pos);                          \
168         } while (0)
169 #define acq_packet_host(obj)                    \
170         inplace_RL16((obj).state)
171 #define transfer_packet_host(obj) do {                  \
172                 acq_packet_host((obj).packet[0]);       \
173                 acq_packet_host((obj).packet[1]);       \
174                 acq_packet_host((obj).packet[2]);       \
175                 acq_packet_host((obj).packet[3]);       \
176                 acq_packet_host((obj).packet[4]);       \
177         } while (0)
178
179 #else
180 /* this host is little-endian, same as device */
181 #define pwm_setting_dev_le(obj)   (void)obj
182 #define trigger_cfg_le(obj)       (void)obj
183 #define sample_config_le(obj)     (void)obj
184
185 #define capture_info_host(obj)    (void)obj
186 #define acq_packet_host(obj)      (void)obj
187 #define transfer_packet_host(obj) (void)obj
188 #endif
189
190 #endif