]> sigrok.org Git - libsigrok.git/blob - src/hardware/siglent-sds/protocol.h
siglent-sds: Fixed samplerate and memory depth calculation
[libsigrok.git] / src / hardware / siglent-sds / protocol.h
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2018 mhooijboer <marchelh@gmail.com>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #ifndef LIBSIGROK_HARDWARE_SIGLENT_SDS_PROTOCOL_H
21 #define LIBSIGROK_HARDWARE_SIGLENT_SDS_PROTOCOL_H
22
23 #include <stdbool.h>
24 #include <stdint.h>
25 #include <glib.h>
26 #include <libsigrok/libsigrok.h>
27 #include "libsigrok-internal.h"
28
29 #define LOG_PREFIX "siglent-sds"
30
31 /* Size of acquisition buffers */
32 //#define ACQ_BUFFER_SIZE (6000000)
33 #define ACQ_BUFFER_SIZE (18000000)
34
35 #define SIGLENT_HEADER_SIZE 351
36
37 /* Maximum number of samples to retrieve at once. */
38 #define ACQ_BLOCK_SIZE (30 * 1000)
39
40 #define MAX_ANALOG_CHANNELS 4
41 #define MAX_DIGITAL_CHANNELS 16
42
43 #define DEVICE_STATE_STOPPED  0     /* Scope is in stopped state */
44 #define DEVICE_STATE_DATA_ACQ 1     /* A new signal has been acquired */
45 #define DEVICE_STATE_TRIG_RDY 8192  /* Trigger is ready */
46
47 enum protocol_version {
48         SPO_MODEL,
49         NON_SPO_MODEL,
50 };
51
52 enum data_source {
53         DATA_SOURCE_SCREEN,
54         DATA_SOURCE_HISTORY,
55 };
56
57 struct siglent_sds_vendor {
58         const char *name;
59         const char *full_name;
60 };
61
62 struct siglent_sds_series {
63         const struct siglent_sds_vendor *vendor;
64         const char *name;
65         enum protocol_version protocol;
66         uint64_t max_timebase[2];
67         uint64_t min_vdiv[2];
68         int num_horizontal_divs;
69         int num_vertical_divs;
70         int buffer_samples;
71 };
72
73 struct siglent_sds_model {
74         const struct siglent_sds_series *series;
75         const char *name;
76         uint64_t min_timebase[2];
77         unsigned int analog_channels;
78         bool has_digital;
79         unsigned int digital_channels;
80 };
81
82 enum wait_events {
83         WAIT_NONE,      /* Don't wait */
84         WAIT_TRIGGER,   /* Wait for trigger */
85         WAIT_BLOCK,     /* Wait for block data (only when reading sample mem) */
86         WAIT_STOP,      /* Wait for scope stopping (only single shots) */
87 };
88
89 struct dev_context {
90         /* Device model */
91         const struct siglent_sds_model *model;
92
93         /* Device properties */
94         const uint64_t (*timebases)[2];
95         uint64_t num_timebases;
96         const uint64_t (*vdivs)[2];
97         uint64_t num_vdivs;
98
99         /* Channel groups */
100         struct sr_channel_group **analog_groups;
101         struct sr_channel_group *digital_group;
102
103         /* Acquisition settings */
104         GSList *enabled_channels;
105         uint64_t limit_frames;
106         uint64_t average_samples;
107         gboolean average_enabled;
108         enum data_source data_source;
109         uint64_t analog_frame_size;
110         uint64_t digital_frame_size;
111         uint64_t num_samples;
112         uint64_t memory_depth;
113         long block_header_size;
114         float samplerate;
115
116         /* Device settings */
117         gboolean analog_channels[MAX_ANALOG_CHANNELS];
118         gboolean digital_channels[MAX_DIGITAL_CHANNELS];
119         gboolean la_enabled;
120         float timebase;
121         float attenuation[MAX_ANALOG_CHANNELS];
122         float vdiv[MAX_ANALOG_CHANNELS];
123         int vert_reference[MAX_ANALOG_CHANNELS];
124         float vert_offset[MAX_ANALOG_CHANNELS];
125         char *trigger_source;
126         float horiz_triggerpos;
127         char *trigger_slope;
128         float trigger_level;
129         char *coupling[MAX_ANALOG_CHANNELS];
130
131         /* Operational state */
132
133         /* Number of frames received in total. */
134         uint64_t num_frames;
135         /* GSList entry for the current channel. */
136         GSList *channel_entry;
137         /* Number of bytes received for current channel. */
138         uint64_t num_channel_bytes;
139         /* Number of bytes of block header read. */
140         uint64_t num_header_bytes;
141         /* Number of data blocks bytes already read. */
142         uint64_t num_block_bytes;
143         /* Number of data blocks read. */
144         int num_block_read;
145         /* What to wait for in *_receive. */
146         enum wait_events wait_event;
147         /* Trigger/block copying/stop waiting status. */
148         int wait_status;
149         /* Acq buffers used for reading from the scope and sending data to app. */
150         unsigned char *buffer;
151         float *data;
152 };
153
154 SR_PRIV int siglent_sds_config_set(const struct sr_dev_inst *sdi,
155         const char *format, ...);
156 SR_PRIV int siglent_sds_capture_start(const struct sr_dev_inst *sdi);
157 SR_PRIV int siglent_sds_channel_start(const struct sr_dev_inst *sdi);
158 SR_PRIV int siglent_sds_receive(int fd, int revents, void *cb_data);
159 SR_PRIV int siglent_sds_get_dev_cfg(const struct sr_dev_inst *sdi);
160 SR_PRIV int siglent_sds_get_dev_cfg_vertical(const struct sr_dev_inst *sdi);
161 SR_PRIV int siglent_sds_get_dev_cfg_horizontal(const struct sr_dev_inst *sdi);
162
163 #endif