]> sigrok.org Git - libsigrok.git/blob - src/hardware/kingst-la2016/protocol.h
43c079d68323a7ead46442fcacac08d70225528b
[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         uint32_t bitstream_size;
107
108         /* derived stuff */
109         uint64_t pre_trigger_size;
110
111         /* state after sampling */
112         int had_triggers_configured;
113         int have_trigger;
114         int transfer_finished;
115         capture_info_t info;
116         unsigned int n_transfer_packets_to_read; /* each with 5 acq packets */
117         unsigned int n_bytes_to_read;
118         unsigned int n_reps_until_trigger;
119         unsigned int reading_behind_trigger;
120         uint64_t total_samples;
121         uint32_t read_pos;
122
123         unsigned int convbuffer_size;
124         uint8_t *convbuffer;
125         struct libusb_transfer *transfer;
126 };
127
128 SR_PRIV int la2016_upload_firmware(struct sr_context *sr_ctx, libusb_device *dev, uint16_t product_id);
129 SR_PRIV int la2016_setup_acquisition(const struct sr_dev_inst *sdi);
130 SR_PRIV int la2016_start_acquisition(const struct sr_dev_inst *sdi);
131 SR_PRIV int la2016_stop_acquisition(const struct sr_dev_inst *sdi);
132 SR_PRIV int la2016_abort_acquisition(const struct sr_dev_inst *sdi);
133 SR_PRIV int la2016_has_triggered(const struct sr_dev_inst *sdi);
134 SR_PRIV int la2016_start_retrieval(const struct sr_dev_inst *sdi, libusb_transfer_cb_fn cb);
135 SR_PRIV int la2016_init_device(const struct sr_dev_inst *sdi);
136 SR_PRIV int la2016_deinit_device(const struct sr_dev_inst *sdi);
137
138 #ifndef WORDS_BIGENDIAN
139 /* this host is big-endian, need to swap from/to device inplace */
140 #define inplace_WL32(obj) do { uint32_t tmp = obj; WL32(&(obj), tmp); } while (0)
141 #define inplace_RL32(obj) obj = RL32(&(obj))
142 #define inplace_WL16(obj) do { uint16_t tmp = obj; WL16(&(obj), tmp); } while (0)
143 #define inplace_RL16(obj) obj = RL16(&(obj))
144
145 #define pwm_setting_dev_le(obj) do {            \
146                 inplace_WL32((obj).period);     \
147                 inplace_WL32((obj).duty);       \
148         } while (0)
149 #define trigger_cfg_le(obj) do {                        \
150                 inplace_WL32((obj).channels);           \
151                 inplace_WL32((obj).enabled);            \
152                 inplace_WL32((obj).level);              \
153                 inplace_WL32((obj).high_or_falling);    \
154         } while (0)
155 #define sample_config_le(obj) do {                      \
156                 inplace_WL32((obj).sample_depth);       \
157                 inplace_WL32((obj).psa);                \
158                 inplace_WL16((obj).u1);                 \
159                 inplace_WL32((obj).u2);                 \
160                 inplace_WL16((obj).clock_divisor);      \
161         } while (0)
162
163 #define capture_info_host(obj) do {                                     \
164                 inplace_RL32((obj).n_rep_packets);                      \
165                 inplace_RL32((obj).n_rep_packets_before_trigger);       \
166                 inplace_RL32((obj).write_pos);                          \
167         } while (0)
168 #define acq_packet_host(obj)                    \
169         inplace_RL16((obj).state)
170 #define transfer_packet_host(obj) do {                  \
171                 acq_packet_host((obj).packet[0]);       \
172                 acq_packet_host((obj).packet[1]);       \
173                 acq_packet_host((obj).packet[2]);       \
174                 acq_packet_host((obj).packet[3]);       \
175                 acq_packet_host((obj).packet[4]);       \
176         } while (0)
177
178 #else
179 /* this host is little-endian, same as device */
180 #define pwm_setting_dev_le(obj)   (void)obj
181 #define trigger_cfg_le(obj)       (void)obj
182 #define sample_config_le(obj)     (void)obj
183
184 #define capture_info_host(obj)    (void)obj
185 #define acq_packet_host(obj)      (void)obj
186 #define transfer_packet_host(obj) (void)obj
187 #endif
188
189 #endif