]> sigrok.org Git - libsigrok.git/blame - hardware/sysclk-lwla/protocol.h
sysclk-lwla: Limit use of SR_ERR_ARG to user-supplied arguments.
[libsigrok.git] / hardware / sysclk-lwla / protocol.h
CommitLineData
aeaad0b0
DE
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2014 Daniel Elstner <daniel.kitta@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_SYSCLK_LWLA_PROTOCOL_H
21#define LIBSIGROK_HARDWARE_SYSCLK_LWLA_PROTOCOL_H
22
5874e88d
DE
23/* Message logging helpers with subsystem-specific prefix string. */
24#define LOG_PREFIX "sysclk-lwla"
25
26#include "lwla.h"
aeaad0b0
DE
27#include "libsigrok.h"
28#include "libsigrok-internal.h"
5874e88d
DE
29#include <stdint.h>
30#include <glib.h>
aeaad0b0 31
5874e88d
DE
32/* For now, only the LWLA1034 is supported.
33 */
34#define VENDOR_NAME "SysClk"
35#define MODEL_NAME "LWLA1034"
36
37#define USB_VID_PID "2961.6689"
38#define USB_INTERFACE 0
39#define USB_TIMEOUT 3000 /* ms */
40
41#define NUM_PROBES 34
42#define TRIGGER_TYPES "01fr"
43
43db3436
DE
44/* Bit mask covering all 34 channels.
45 */
46#define ALL_CHANNELS_MASK (((uint64_t)1 << NUM_PROBES) - 1)
47
5874e88d
DE
48/** Unit and packet size for the sigrok logic datafeed.
49 */
50#define UNIT_SIZE ((NUM_PROBES + 7) / 8)
2cfd16a3 51#define PACKET_LENGTH 10000 /* units */
5874e88d
DE
52
53/** Size of the acquisition buffer in device memory units.
54 */
55#define MEMORY_DEPTH (256 * 1024) /* 256k x 36 bit */
56
57/** Number of device memory units (36 bit) to read at a time. Slices of 8
58 * consecutive 36-bit words are mapped to 9 32-bit words each, so the chunk
59 * length should be a multiple of 8 to ensure alignment to slice boundaries.
60 *
61 * Experimentation has shown that reading chunks larger than about 1024 bytes
62 * is unreliable. The threshold seems to relate to the buffer size on the FX2
63 * USB chip: The configured endpoint buffer size is 512, and with double or
64 * triple buffering enabled a multiple of 512 bytes can be kept in fly.
65 *
66 * The vendor software limits reads to 120 words (15 slices, 540 bytes) at
67 * a time. So far, it appears safe to increase this to 224 words (28 slices,
68 * 1008 bytes), thus making the most of two 512 byte buffers.
69 */
70#define READ_CHUNK_LEN (28 * 8)
71
72/** Calculate the required buffer size in 16-bit units for reading a given
73 * number of device memory words. Rounded to a multiple of 8 device words.
74 */
75#define LWLA1034_MEMBUF_LEN(count) (((count) + 7) / 8 * 18)
76
77/** Maximum number of 16-bit words sent at a time during acquisition.
78 * Used for allocating the libusb transfer buffer.
79 */
80#define MAX_ACQ_SEND_WORDS 8 /* 5 for memory read request plus stuffing */
81
82/** Maximum number of 16-bit words received at a time during acquisition.
83 * Round to the next multiple of the endpoint buffer size to avoid nasty
84 * transfer overflow conditions on hiccups.
85 */
86#define MAX_ACQ_RECV_WORDS ((READ_CHUNK_LEN / 4 * 9 + 255) / 256 * 256)
87
88/** Maximum length of a register write sequence.
89 */
90#define MAX_REG_WRITE_SEQ_LEN 5
91
92/** Default configured samplerate.
93 */
94#define DEFAULT_SAMPLERATE SR_MHZ(125)
95
29d58767
DE
96/** Maximum configurable sample count limit.
97 */
98#define MAX_LIMIT_SAMPLES (UINT64_C(1) << 48)
99
100/** Maximum configurable capture duration in milliseconds.
101 */
102#define MAX_LIMIT_MSEC (UINT64_C(1) << 32)
103
5874e88d
DE
104/** LWLA clock sources.
105 */
106enum clock_source {
107 CLOCK_SOURCE_NONE,
108 CLOCK_SOURCE_INT,
109 CLOCK_SOURCE_EXT_RISE,
110 CLOCK_SOURCE_EXT_FALL,
111};
112
113/** LWLA device states.
114 */
115enum device_state {
116 STATE_IDLE = 0,
117
118 STATE_START_CAPTURE,
119
120 STATE_STATUS_WAIT,
121 STATE_STATUS_REQUEST,
122 STATE_STATUS_RESPONSE,
123
124 STATE_STOP_CAPTURE,
125
126 STATE_LENGTH_REQUEST,
127 STATE_LENGTH_RESPONSE,
128
129 STATE_READ_PREPARE,
130 STATE_READ_REQUEST,
131 STATE_READ_RESPONSE,
132 STATE_READ_END,
133};
134
135/** LWLA run-length encoding states.
136 */
137enum rle_state {
138 RLE_STATE_DATA,
139 RLE_STATE_LEN
140};
141
142/** LWLA sample acquisition and decompression state.
143 */
144struct acquisition_state {
145 uint64_t sample;
146 uint64_t run_len;
147
29d58767
DE
148 /** Maximum number of samples to process. */
149 uint64_t samples_max;
5874e88d 150 /** Number of samples sent to the session bus. */
29d58767
DE
151 uint64_t samples_done;
152
153 /** Maximum duration of capture, in milliseconds. */
154 uint64_t duration_max;
155 /** Running capture duration since trigger event. */
156 uint64_t duration_now;
5874e88d
DE
157
158 /** Capture memory fill level. */
159 size_t mem_addr_fill;
aeaad0b0 160
5874e88d
DE
161 size_t mem_addr_done;
162 size_t mem_addr_next;
163 size_t mem_addr_stop;
164
2cfd16a3 165 size_t out_index;
5874e88d
DE
166
167 struct libusb_transfer *xfer_in;
168 struct libusb_transfer *xfer_out;
169
170 unsigned int capture_flags;
171
172 enum rle_state rle;
173
29d58767
DE
174 /** Whether to bypass the clock divider. */
175 gboolean bypass_clockdiv;
176
5874e88d
DE
177 /* Payload data buffers for outgoing and incoming transfers. */
178 uint16_t xfer_buf_out[MAX_ACQ_SEND_WORDS];
179 uint16_t xfer_buf_in[MAX_ACQ_RECV_WORDS];
180
181 /* Payload buffer for sigrok logic packets. */
2cfd16a3 182 uint8_t out_packet[PACKET_LENGTH * UNIT_SIZE];
5874e88d
DE
183};
184
185/** Private, per-device-instance driver context.
186 */
aeaad0b0 187struct dev_context {
5874e88d
DE
188 /** The samplerate selected by the user. */
189 uint64_t samplerate;
190
29d58767
DE
191 /** The maximimum sampling duration, in milliseconds. */
192 uint64_t limit_msec;
193
5874e88d
DE
194 /** The maximimum number of samples to acquire. */
195 uint64_t limit_samples;
196
197 /** Channels to use. */
198 uint64_t channel_mask;
199
200 uint64_t trigger_mask;
201 uint64_t trigger_edge_mask;
202 uint64_t trigger_values;
aeaad0b0 203
5874e88d 204 struct acquisition_state *acquisition;
aeaad0b0 205
5874e88d
DE
206 struct regval_pair reg_write_seq[MAX_REG_WRITE_SEQ_LEN];
207 int reg_write_pos;
208 int reg_write_len;
aeaad0b0 209
5874e88d 210 enum device_state state;
aeaad0b0 211
5874e88d
DE
212 /** The currently configured clock source of the device. */
213 enum clock_source cur_clock_source;
214 /** The clock source selected by the user. */
215 enum clock_source selected_clock_source;
216
217 /* Indicates that stopping the acquisition is currently in progress. */
218 gboolean stopping_in_progress;
219
220 /* Indicates whether a transfer failed. */
221 gboolean transfer_error;
aeaad0b0
DE
222};
223
5874e88d
DE
224SR_PRIV struct acquisition_state *lwla_alloc_acquisition_state(void);
225SR_PRIV void lwla_free_acquisition_state(struct acquisition_state *acq);
226
227SR_PRIV int lwla_init_device(const struct sr_dev_inst *sdi);
228SR_PRIV int lwla_set_clock_source(const struct sr_dev_inst *sdi);
229SR_PRIV int lwla_setup_acquisition(const struct sr_dev_inst *sdi);
230SR_PRIV int lwla_start_acquisition(const struct sr_dev_inst *sdi);
231SR_PRIV int lwla_abort_acquisition(const struct sr_dev_inst *sdi);
232
233SR_PRIV int lwla_receive_data(int fd, int revents, void *cb_data);
aeaad0b0 234
5874e88d 235#endif /* !LIBSIGROK_HARDWARE_SYSCLK_LWLA_PROTOCOL_H */