]> sigrok.org Git - libsigrok.git/blob - src/hardware/kingst-la2016/protocol.h
kingst-la2016: tested with idVendor=77a1, idProduct=01a2
[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
39 #define LA2016_BULK_MAX         8388608
40
41 #define MAX_RENUM_DELAY_MS      3000
42 #define DEFAULT_TIMEOUT_MS      200
43
44 #define LA2016_THR_VOLTAGE_MIN  0.40
45 #define LA2016_THR_VOLTAGE_MAX  4.00
46
47 #define LA2016_NUM_SAMPLES_MIN  256
48 #define LA2016_NUM_SAMPLES_MAX  (10UL * 1000 * 1000 * 1000)
49
50 typedef struct pwm_setting_dev {
51         uint32_t period;
52         uint32_t duty;
53 } __attribute__((__packed__)) pwm_setting_dev_t;
54
55 typedef struct trigger_cfg {
56         uint32_t channels;
57         uint32_t enabled;
58         uint32_t level;
59         uint32_t high_or_falling;
60 } __attribute__((__packed__)) trigger_cfg_t;
61
62 typedef struct sample_config {
63         uint32_t sample_depth;
64         uint32_t psa;
65         uint16_t u1;
66         uint32_t u2;
67         uint16_t clock_divisor;
68 } __attribute__((__packed__)) sample_config_t;
69
70 typedef struct capture_info {
71         uint32_t n_rep_packets;
72         uint32_t n_rep_packets_before_trigger;
73         uint32_t write_pos;
74 } __attribute__((__packed__)) capture_info_t;
75
76 typedef struct acq_packet {
77         uint16_t state;
78         uint8_t repetitions;
79 } __attribute__((__packed__)) acq_packet_t;
80
81 typedef struct transfer_packet {
82         acq_packet_t packet[5];
83         uint8_t seq;
84 } __attribute__((__packed__)) transfer_packet_t;
85
86 typedef struct pwm_setting {
87         uint8_t enabled;
88         float freq;
89         float duty;
90         pwm_setting_dev_t dev;
91 } pwm_setting_t;
92
93 struct dev_context {
94         struct sr_context *ctx;
95
96         int64_t fw_updated;
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;
104         int num_channels;
105
106         /* derived stuff */
107         uint64_t pre_trigger_size;
108
109         /* state after sampling */
110         int had_triggers_configured;
111         int have_trigger;
112         int transfer_finished;
113         capture_info_t info;
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;
119         uint32_t read_pos;
120
121         unsigned int convbuffer_size;
122         uint8_t *convbuffer;
123         struct libusb_transfer *transfer;
124 };
125
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);
135
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))
142
143 #define pwm_setting_dev_le(obj) do {            \
144                 inplace_WL32((obj).period);     \
145                 inplace_WL32((obj).duty);       \
146         } while (0)
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);    \
152         } while (0)
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);      \
159         } while (0)
160
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);                          \
165         } while (0)
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]);       \
174         } while (0)
175
176 #else
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
181
182 #define capture_info_host(obj)    (void)obj
183 #define acq_packet_host(obj)      (void)obj
184 #define transfer_packet_host(obj) (void)obj
185 #endif
186
187 #endif