]> sigrok.org Git - libsigrok.git/blame - src/hardware/hantek-4032l/protocol.h
hantek-4032l: Add support for external clocks.
[libsigrok.git] / src / hardware / hantek-4032l / protocol.h
CommitLineData
6a25fa42
AZ
1/*
2 * This file is part of the libsigrok project.
3 *
5089a143
AZ
4 * Copyright (C) 2016 Andreas Zschunke <andreas.zschunke@gmx.net>
5 * Copyright (C) 2017 Andrej Valek <andy@skyrain.eu>
6 * Copyright (C) 2017 Uwe Hermann <uwe@hermann-uwe.de>
6a25fa42
AZ
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22#ifndef LIBSIGROK_HARDWARE_HANTEK_4032L_PROTOCOL_H
23#define LIBSIGROK_HARDWARE_HANTEK_4032L_PROTOCOL_H
24
25#include <stdint.h>
26#include <glib.h>
5089a143 27#include <string.h>
6a25fa42
AZ
28#include <libsigrok/libsigrok.h>
29#include "libsigrok-internal.h"
30
31#define LOG_PREFIX "hantek-4032l"
32
5089a143
AZ
33#define H4032L_USB_VENDOR 0x04b5
34#define H4032L_USB_PRODUCT 0x4032
35
2958315d
AV
36#define H4032L_DATA_BUFFER_SIZE (2 * 1024)
37#define H4032L_DATA_TRANSFER_MAX_NUM 32
38
4b75f84c
AV
39#define H4043L_NUM_SAMPLES_MIN (2 * 1024)
40#define H4032L_NUM_SAMPLES_MAX (64 * 1024 * 1024)
41
5089a143
AZ
42#define H4032L_CMD_PKT_MAGIC 0x017f
43#define H4032L_STATUS_PACKET_MAGIC 0x2B1A037F
44#define H4032L_START_PACKET_MAGIC 0x2B1A027F
45#define H4032L_END_PACKET_MAGIC 0x4D3C037F
46
f49065c6
AV
47enum h4032l_clock_edge_type {
48 H4032L_CLOCK_EDGE_TYPE_RISE,
49 H4032L_CLOCK_EDGE_TYPE_FALL,
50 H4032L_CLOCK_EDGE_TYPE_BOTH
51};
52
53enum h4032l_ext_clock_source {
54 H4032L_EXT_CLOCK_SOURCE_CHANNEL_A,
55 H4032L_EXT_CLOCK_SOURCE_CHANNEL_B
56};
57
58enum h4032l_clock_edge_type_channel {
59 H4032L_CLOCK_EDGE_TYPE_RISE_A = 0x24,
60 H4032L_CLOCK_EDGE_TYPE_RISE_B,
61 H4032L_CLOCK_EDGE_TYPE_BOTH_A,
62 H4032L_CLOCK_EDGE_TYPE_BOTH_B,
63 H4032L_CLOCK_EDGE_TYPE_FALL_A,
64 H4032L_CLOCK_EDGE_TYPE_FALL_B
65};
66
5089a143 67enum h4032l_trigger_edge_type {
28f2d07f 68 H4032L_TRIGGER_EDGE_TYPE_RISE,
5089a143
AZ
69 H4032L_TRIGGER_EDGE_TYPE_FALL,
70 H4032L_TRIGGER_EDGE_TYPE_TOGGLE,
71 H4032L_TRIGGER_EDGE_TYPE_DISABLED
72};
73
74enum h4032l_trigger_data_range_type {
28f2d07f 75 H4032L_TRIGGER_DATA_RANGE_TYPE_MAX,
5089a143
AZ
76 H4032L_TRIGGER_DATA_RANGE_TYPE_MIN_OR_MAX,
77 H4032L_TRIGGER_DATA_RANGE_TYPE_OUT_OF_RANGE,
78 H4032L_TRIGGER_DATA_RANGE_TYPE_WITHIN_RANGE
79};
80
81enum h4032l_trigger_time_range_type {
28f2d07f 82 H4032L_TRIGGER_TIME_RANGE_TYPE_MAX,
5089a143
AZ
83 H4032L_TRIGGER_TIME_RANGE_TYPE_MIN_OR_MAX,
84 H4032L_TRIGGER_TIME_RANGE_TYPE_OUT_OF_RANGE,
85 H4032L_TRIGGER_TIME_RANGE_TYPE_WITHIN_RANGE
86};
6a25fa42 87
5089a143 88enum h4032l_trigger_data_selection {
28f2d07f 89 H4032L_TRIGGER_DATA_SELECTION_NEXT,
5089a143
AZ
90 H4032L_TRIGGER_DATA_SELECTION_CURRENT,
91 H4032L_TRIGGER_DATA_SELECTION_PREV
92};
6a25fa42 93
5089a143
AZ
94enum h4032l_status {
95 H4032L_STATUS_IDLE,
96 H4032L_STATUS_CMD_CONFIGURE,
97 H4032L_STATUS_CMD_STATUS,
98 H4032L_STATUS_RESPONSE_STATUS,
99 H4032L_STATUS_RESPONSE_STATUS_RETRY,
100 H4032L_STATUS_RESPONSE_STATUS_CONTINUE,
101 H4032L_STATUS_CMD_GET,
102 H4032L_STATUS_FIRST_TRANSFER,
28f2d07f 103 H4032L_STATUS_TRANSFER
5089a143 104};
6a25fa42 105
e80e1858
AV
106#pragma pack(push,2)
107struct h4032l_trigger {
5089a143
AZ
108 struct {
109 uint32_t edge_signal:5;
110 uint32_t edge_type:2;
111 uint32_t :1;
112 uint32_t data_range_type:2;
113 uint32_t time_range_type:2;
114 uint32_t data_range_enabled:1;
115 uint32_t time_range_enabled:1;
116 uint32_t :2;
117 uint32_t data_sel:2;
118 uint32_t combined_enabled:1;
119 } flags;
120 uint32_t data_range_min;
121 uint32_t data_range_max;
122 uint32_t time_range_min;
123 uint32_t time_range_max;
124 uint32_t data_range_mask;
125 uint32_t combine_mask;
126 uint32_t combine_data;
127};
6a25fa42 128
e80e1858 129struct h4032l_cmd_pkt {
5089a143
AZ
130 uint16_t magic; /* 0x017f */
131 uint8_t sample_rate;
132 struct {
133 uint8_t enable_trigger1:1;
134 uint8_t enable_trigger2:1;
135 uint8_t trigger_and_logic:1;
136 } trig_flags;
137 uint16_t pwm_a;
138 uint16_t pwm_b;
139 uint16_t reserved;
140 uint32_t sample_size; /* Sample depth in bits per channel, 2k-64M, must be multiple of 512. */
141 uint32_t pre_trigger_size; /* Pretrigger buffer depth in bits, must be < sample_size. */
142 struct h4032l_trigger trigger[2];
143 uint16_t cmd;
144};
e80e1858 145#pragma pack(pop)
5089a143
AZ
146
147struct dev_context {
148 enum h4032l_status status;
f49065c6 149 uint64_t sample_rate;
3dc976fe 150 unsigned int sent_samples;
2958315d 151 int submitted_transfers;
5089a143 152 uint32_t remaining_samples;
a5b9880e 153 gboolean acq_aborted;
5089a143 154 struct h4032l_cmd_pkt cmd_pkt;
2958315d
AV
155 unsigned int num_transfers;
156 struct libusb_transfer **transfers;
5089a143
AZ
157 uint8_t buffer[512];
158 uint64_t capture_ratio;
3dc976fe 159 uint32_t trigger_pos;
f49065c6
AV
160 gboolean external_clock;
161 enum h4032l_ext_clock_source external_clock_source;
162 enum h4032l_clock_edge_type clock_edge;
2a801861 163 double cur_threshold[2];
7a7afc00 164 uint32_t fpga_version;
6a25fa42
AZ
165};
166
5089a143
AZ
167SR_PRIV int h4032l_receive_data(int fd, int revents, void *cb_data);
168SR_PRIV uint16_t h4032l_voltage2pwm(double voltage);
169SR_PRIV void LIBUSB_CALL h4032l_usb_callback(struct libusb_transfer *transfer);
43d86035
AV
170SR_PRIV void LIBUSB_CALL h4032l_data_transfer_callback(struct libusb_transfer *transfer);
171SR_PRIV int h4032l_start_data_transfers(const struct sr_dev_inst *sdi);
5089a143 172SR_PRIV int h4032l_start(const struct sr_dev_inst *sdi);
2958315d 173SR_PRIV int h4032l_stop(struct sr_dev_inst *sdi);
5089a143 174SR_PRIV int h4032l_dev_open(struct sr_dev_inst *sdi);
7a7afc00 175SR_PRIV int h4032l_get_fpga_version(const struct sr_dev_inst *sdi);
6a25fa42
AZ
176
177#endif