1213 lines
46 KiB
C
1213 lines
46 KiB
C
/* SPDX-License-Identifier: GPL-3.0-or-later */
|
|
#include <gio/gio.h>
|
|
#include <gio/gnetworking.h>
|
|
#include <string.h>
|
|
|
|
#include "libsigrok.h"
|
|
#include "libsigrok-internal.h"
|
|
#include "../../log.h"
|
|
#include "zynq_la.h"
|
|
#include "zynq_la_stream.h"
|
|
#include "zynq_la_pl.h"
|
|
#include "zynq_record.h"
|
|
|
|
#define CHANNELS 8
|
|
#define SAMPLE_GROUP 64
|
|
#define STREAM_DEPTH (64u * 1024u * 1024u)
|
|
#define RECORD_SECONDS 3600
|
|
#define IO_TIMEOUT (5 * G_TIME_SPAN_SECOND)
|
|
#define SNAPSHOT_TIMEOUT (65 * G_TIME_SPAN_SECOND)
|
|
#define STREAM_MIN_DIVIDER 10
|
|
#define PL_HEARTBEAT (250 * G_TIME_SPAN_MILLISECOND)
|
|
#define PL_REORDER_BLOCKS 16384u
|
|
#define PL_SOCKET_BUFFER (32 * 1024 * 1024)
|
|
|
|
enum receive_state { REPLY, FRAME_HEADER, SAMPLES };
|
|
|
|
struct pl_sample_block {
|
|
gboolean ready;
|
|
uint8_t data[ZLP_BLOCK];
|
|
};
|
|
|
|
struct zynq_device {
|
|
gboolean stream;
|
|
gboolean pl;
|
|
gboolean disk_buffer;
|
|
gboolean loop;
|
|
gboolean source_active;
|
|
uint8_t max_height;
|
|
uint32_t clock;
|
|
uint32_t divider;
|
|
uint64_t limit;
|
|
GSocketConnection *connection;
|
|
GSocket *datagrams;
|
|
uint32_t nonce;
|
|
gint64 keepalive;
|
|
gboolean pl_started;
|
|
size_t pl_count;
|
|
unsigned pl_pending;
|
|
uint32_t pl_magic;
|
|
uint32_t pl_stream_magic, pl_snapshot_magic;
|
|
gboolean pl_packed, pl_rle_available;
|
|
uint32_t stages[TriggerStages][4];
|
|
unsigned stage_count, stage_sent, pretrigger;
|
|
gboolean serial_trigger, external_trigger;
|
|
uint8_t pl_expand[16];
|
|
struct pl_sample_block *pl_reorder;
|
|
char *record_file, *active_record, *last_recording;
|
|
char *last_error;
|
|
uint64_t record_samples, delivered;
|
|
struct zynq_recorder *recorder;
|
|
|
|
enum receive_state state;
|
|
gint64 deadline;
|
|
size_t received;
|
|
size_t wanted;
|
|
uint32_t sequence;
|
|
gint progress;
|
|
gint triggered;
|
|
gint recorded_tenths;
|
|
unsigned enabled_mask;
|
|
unsigned level_mask, level_value;
|
|
unsigned edge_mask, edge_value, any_edge_mask;
|
|
uint8_t previous;
|
|
gboolean previous_valid;
|
|
size_t pending_count;
|
|
uint8_t pending[SAMPLE_GROUP];
|
|
uint8_t input[ZLS_BLOCK];
|
|
uint8_t output[ZLS_BLOCK];
|
|
};
|
|
|
|
static const uint32_t dividers[] = {
|
|
50000, 20000, 10000, 5000, 2000, 1000, 500, 200, 100, 50, 40, 25,
|
|
20, 14, 10, 7, 5, 4, 3, 2, 1
|
|
};
|
|
static const char *const max_heights[] = {"1X", "2X", "3X", "4X", "5X"};
|
|
|
|
static uint64_t max_samples(const struct zynq_device *device)
|
|
{
|
|
return device->stream ? STREAM_DEPTH : ZLA_MAX_SAMPLES;
|
|
}
|
|
|
|
static uint32_t word(const uint8_t *data, size_t index)
|
|
{
|
|
uint32_t value;
|
|
memcpy(&value, data + index * sizeof(value), sizeof(value));
|
|
return GUINT32_FROM_LE(value);
|
|
}
|
|
|
|
static GSocket *connect_udp(const char *address)
|
|
{
|
|
GInetAddress *ip = g_inet_address_new_from_string(address);
|
|
GSocketAddress *peer = g_inet_socket_address_new(ip, ZLP_PORT);
|
|
g_object_unref(ip);
|
|
GSocket *socket = g_socket_new(G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_DATAGRAM,
|
|
G_SOCKET_PROTOCOL_UDP, NULL);
|
|
if (socket && !g_socket_connect(socket, peer, NULL, NULL))
|
|
g_clear_object(&socket);
|
|
g_object_unref(peer);
|
|
|
|
if (socket) {
|
|
// Buffer bursts while the host schedules the receiver at 100 MB/s.
|
|
g_socket_set_option(socket, SOL_SOCKET, SO_RCVBUF, PL_SOCKET_BUFFER, NULL);
|
|
g_socket_set_blocking(socket, FALSE);
|
|
}
|
|
return socket;
|
|
}
|
|
|
|
static gboolean send_udp(GSocket *socket, uint32_t magic, uint32_t op, uint32_t nonce, uint32_t value)
|
|
{
|
|
uint32_t request[] = {GUINT32_TO_LE(magic), GUINT32_TO_LE(op),
|
|
GUINT32_TO_LE(nonce), GUINT32_TO_LE(value)};
|
|
return g_socket_send(socket, (const char *)request, sizeof(request), NULL, NULL) ==
|
|
sizeof(request);
|
|
}
|
|
|
|
static uint32_t probe_pl(const char *address, uint32_t magic)
|
|
{
|
|
GSocket *socket = connect_udp(address);
|
|
if (!socket)
|
|
return 0;
|
|
|
|
uint32_t nonce = g_random_int() | 1u;
|
|
uint32_t clock = 0;
|
|
for (unsigned attempt = 0; attempt < 2 && !clock; attempt++) {
|
|
if (!send_udp(socket, magic, ZLP_PROBE, nonce, 0) ||
|
|
!g_socket_condition_timed_wait(socket, G_IO_IN,
|
|
(magic == ZLR_MAGIC ? 100 : 450) * G_TIME_SPAN_MILLISECOND, NULL, NULL))
|
|
continue;
|
|
|
|
uint8_t response[16];
|
|
gssize size = g_socket_receive(socket, (char *)response, sizeof(response), NULL, NULL);
|
|
if (size != sizeof(response) || word(response, 0) != magic ||
|
|
word(response, 1) != ZLP_INFO || word(response, 2) != nonce)
|
|
continue;
|
|
uint32_t reported = word(response, 3);
|
|
if ((magic != ZLP_MAGIC && reported == ZLP2_CLOCK_HZ) ||
|
|
(magic == ZLP_MAGIC && (reported == ZLP_CLOCK_HZ || reported == ZLP_LEGACY_CLOCK_HZ)))
|
|
clock = reported;
|
|
}
|
|
g_object_unref(socket);
|
|
return clock;
|
|
}
|
|
|
|
static unsigned channel_mask(const struct sr_dev_inst *sdi)
|
|
{
|
|
unsigned mask = 0;
|
|
for (const GSList *entry = sdi->channels; entry; entry = entry->next) {
|
|
const struct sr_channel *ch = entry->data;
|
|
if (ch->enabled)
|
|
mask |= 1u << ch->index;
|
|
}
|
|
return mask;
|
|
}
|
|
|
|
static gboolean pack_channels(unsigned mask)
|
|
{
|
|
unsigned count = 0;
|
|
for (unsigned channel = 0; channel < CHANNELS; channel++)
|
|
count += (mask >> channel) & 1u;
|
|
return count > 0 && count <= 4;
|
|
}
|
|
|
|
static unsigned min_divider(const struct sr_dev_inst *sdi)
|
|
{
|
|
const struct zynq_device *device = sdi->priv;
|
|
if (device->pl_magic == ZLP2_MAGIC || device->pl_magic == ZLR_MAGIC)
|
|
return device->pl_rle_available || pack_channels(channel_mask(sdi)) ? 1 : 2;
|
|
if (device->pl)
|
|
return device->clock == ZLP_LEGACY_CLOCK_HZ ? ZLP_LEGACY_MIN_DIVIDER : ZLP_MIN_DIVIDER;
|
|
return device->stream ? STREAM_MIN_DIVIDER : 1;
|
|
}
|
|
|
|
static GSocketConnection *connect_to(const char *address, unsigned port)
|
|
{
|
|
GSocketClient *client = g_socket_client_new();
|
|
g_socket_client_set_enable_proxy(client, FALSE);
|
|
g_socket_client_set_timeout(client, 2);
|
|
GSocketConnection *connection =
|
|
g_socket_client_connect_to_host(client, address, port, NULL, NULL);
|
|
g_object_unref(client);
|
|
return connection;
|
|
}
|
|
|
|
static gboolean send_request(GSocketConnection *connection, uint32_t *request, size_t count)
|
|
{
|
|
for (size_t i = 0; i < count; i++)
|
|
request[i] = GUINT32_TO_LE(request[i]);
|
|
|
|
return g_output_stream_write_all(
|
|
g_io_stream_get_output_stream(G_IO_STREAM(connection)),
|
|
request, count * sizeof(*request), NULL, NULL, NULL);
|
|
}
|
|
|
|
static uint32_t probe_clock(const char *address, gboolean stream)
|
|
{
|
|
GSocketConnection *connection = connect_to(address, stream ? ZLS_PORT : ZLA_PORT);
|
|
if (!connection)
|
|
return 0;
|
|
|
|
/* An invalid divider/count returns metadata without arming either engine. */
|
|
uint32_t request[11] = {stream ? ZLS_MAGIC : ZLA_REQUEST, 1};
|
|
uint8_t reply[32];
|
|
gsize bytes = 0;
|
|
gboolean ok = send_request(connection, request, stream ? 4 : 11) &&
|
|
g_input_stream_read_all(g_io_stream_get_input_stream(G_IO_STREAM(connection)),
|
|
reply, sizeof(reply), &bytes, NULL, NULL) && bytes == sizeof(reply) &&
|
|
word(reply, 0) == (stream ? ZLS_MAGIC : ZLA_MAGIC) &&
|
|
word(reply, 1) == 1 && word(reply, 2) == 1 && word(reply, 6) == CHANNELS &&
|
|
word(reply, 5) == (stream ? ZLS_BLOCK : 0) && word(reply, 7) == (stream ? 0 : 1);
|
|
g_object_unref(connection);
|
|
return ok ? word(reply, 3) : 0;
|
|
}
|
|
|
|
static int config_get(int key, GVariant **data, const struct sr_dev_inst *sdi,
|
|
const struct sr_channel *ch, const struct sr_channel_group *cg)
|
|
{
|
|
(void)ch;
|
|
(void)cg;
|
|
const struct zynq_device *device = sdi->priv;
|
|
switch (key) {
|
|
case SR_CONF_DEVICE_MODE:
|
|
*data = g_variant_new_int16(LOGIC);
|
|
break;
|
|
case SR_CONF_OPERATION_MODE:
|
|
*data = g_variant_new_int16(device->disk_buffer ? LO_OP_BUFFER :
|
|
device->stream ? LO_OP_STREAM : device->pl_stream_magic ? LO_OP_ZYNQ_SNAPSHOT : LO_OP_BUFFER);
|
|
break;
|
|
case SR_CONF_MAX_HEIGHT:
|
|
*data = g_variant_new_string(max_heights[device->max_height]);
|
|
break;
|
|
case SR_CONF_MAX_HEIGHT_VALUE:
|
|
*data = g_variant_new_byte(device->max_height);
|
|
break;
|
|
case SR_CONF_SAMPLERATE:
|
|
*data = g_variant_new_uint64(device->clock / MAX(device->divider, min_divider(sdi)));
|
|
break;
|
|
case SR_CONF_LIMIT_SAMPLES:
|
|
*data = g_variant_new_uint64(device->limit);
|
|
break;
|
|
case SR_CONF_HW_DEPTH:
|
|
*data = g_variant_new_uint64(max_samples(device));
|
|
break;
|
|
case SR_CONF_STREAM:
|
|
*data = g_variant_new_boolean(device->stream && !device->disk_buffer);
|
|
break;
|
|
case SR_CONF_ZYNQ_RECORD_FILE:
|
|
if (!device->pl || !device->stream)
|
|
return SR_ERR_NA;
|
|
*data = g_variant_new_string(device->record_file ? device->record_file : "");
|
|
break;
|
|
case SR_CONF_ZYNQ_RECORD_SAMPLES:
|
|
if (!device->pl || !device->stream)
|
|
return SR_ERR_NA;
|
|
*data = g_variant_new_uint64(device->record_samples);
|
|
break;
|
|
case SR_CONF_ZYNQ_LAST_ERROR:
|
|
*data = g_variant_new_string(device->last_error ? device->last_error : "");
|
|
break;
|
|
case SR_CONF_ZYNQ_ADVANCED:
|
|
*data = g_variant_new_boolean(device->pl_magic == ZLT_MAGIC);
|
|
break;
|
|
case SR_CONF_ZYNQ_DISK_BUFFER:
|
|
*data = g_variant_new_boolean(device->disk_buffer);
|
|
break;
|
|
case SR_CONF_ZYNQ_RECORD_TENTHS:
|
|
*data = g_variant_new_uint64(g_atomic_int_get(&device->recorded_tenths));
|
|
break;
|
|
case SR_CONF_ZYNQ_LIVE_RECORDING:
|
|
*data = g_variant_new_boolean(device->recorder && !device->disk_buffer);
|
|
break;
|
|
case SR_CONF_ZYNQ_LAST_RECORDING:
|
|
*data = g_variant_new_string(device->last_recording ? device->last_recording : "");
|
|
break;
|
|
case SR_CONF_TOTAL_CH_NUM:
|
|
case SR_CONF_VLD_CH_NUM:
|
|
*data = g_variant_new_int16(CHANNELS);
|
|
break;
|
|
case SR_CONF_RLE_SUPPORT:
|
|
case SR_CONF_TEST:
|
|
case SR_CONF_WAIT_UPLOAD:
|
|
*data = g_variant_new_boolean(FALSE);
|
|
break;
|
|
default:
|
|
return SR_ERR_NA;
|
|
}
|
|
return SR_OK;
|
|
}
|
|
|
|
static int config_set(int key, GVariant *data, struct sr_dev_inst *sdi,
|
|
struct sr_channel *ch, struct sr_channel_group *cg)
|
|
{
|
|
(void)ch;
|
|
(void)cg;
|
|
struct zynq_device *device = sdi->priv;
|
|
switch (key) {
|
|
case SR_CONF_MAX_HEIGHT:
|
|
for (size_t i = 0; i < ARRAY_SIZE(max_heights); i++) {
|
|
if (!strcmp(g_variant_get_string(data, NULL), max_heights[i])) {
|
|
device->max_height = i;
|
|
return SR_OK;
|
|
}
|
|
}
|
|
return SR_ERR_ARG;
|
|
case SR_CONF_OPERATION_MODE: {
|
|
const int mode = g_variant_get_int16(data);
|
|
if (device->source_active || !device->pl_stream_magic ||
|
|
(mode != LO_OP_BUFFER && mode != LO_OP_STREAM &&
|
|
!(mode == LO_OP_ZYNQ_SNAPSHOT && device->pl_snapshot_magic)))
|
|
return SR_ERR_ARG;
|
|
device->stream = mode != LO_OP_ZYNQ_SNAPSHOT;
|
|
device->disk_buffer = mode == LO_OP_BUFFER;
|
|
device->pl_magic = device->stream ? device->pl_stream_magic : device->pl_snapshot_magic;
|
|
device->divider = MAX(device->divider, min_divider(sdi));
|
|
device->limit = MIN(device->limit, max_samples(device));
|
|
return SR_OK;
|
|
}
|
|
case SR_CONF_SAMPLERATE: {
|
|
const uint64_t rate = g_variant_get_uint64(data);
|
|
for (size_t i = 0; i < ARRAY_SIZE(dividers); i++) {
|
|
if (dividers[i] < min_divider(sdi))
|
|
break;
|
|
if (device->clock / dividers[i] == rate) {
|
|
device->divider = dividers[i];
|
|
return SR_OK;
|
|
}
|
|
}
|
|
return SR_ERR_ARG;
|
|
}
|
|
case SR_CONF_LIMIT_SAMPLES:
|
|
device->limit = CLAMP(g_variant_get_uint64(data), SAMPLE_GROUP, max_samples(device));
|
|
return SR_OK;
|
|
case SR_CONF_LOOP_MODE:
|
|
device->loop = device->stream && !device->disk_buffer && g_variant_get_boolean(data);
|
|
return SR_OK;
|
|
case SR_CONF_ZYNQ_LAST_RECORDING:
|
|
if (device->source_active || *g_variant_get_string(data, NULL))
|
|
return SR_ERR_ARG;
|
|
g_clear_pointer(&device->last_recording, g_free);
|
|
return SR_OK;
|
|
case SR_CONF_ZYNQ_RECORD_FILE:
|
|
if (!device->pl || !device->stream || device->source_active)
|
|
return SR_ERR_NA;
|
|
g_free(device->record_file);
|
|
device->record_file = g_variant_dup_string(data, NULL);
|
|
return SR_OK;
|
|
case SR_CONF_ZYNQ_RECORD_SAMPLES:
|
|
if (!device->pl || !device->stream || device->source_active)
|
|
return SR_ERR_NA;
|
|
device->record_samples = CLAMP(g_variant_get_uint64(data), SAMPLE_GROUP,
|
|
(uint64_t)device->clock * RECORD_SECONDS / MAX(device->divider, min_divider(sdi)));
|
|
return SR_OK;
|
|
case SR_CONF_DEVICE_MODE:
|
|
return g_variant_get_int16(data) == LOGIC ? SR_OK : SR_ERR_ARG;
|
|
case SR_CONF_INSTANT:
|
|
case SR_CONF_RLE:
|
|
return SR_OK;
|
|
default:
|
|
return SR_ERR_NA;
|
|
}
|
|
}
|
|
|
|
static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
|
|
const struct sr_channel_group *cg)
|
|
{
|
|
(void)cg;
|
|
const struct zynq_device *device = sdi->priv;
|
|
static const int32_t options[] = {SR_CONF_OPERATION_MODE, SR_CONF_MAX_HEIGHT};
|
|
static const struct sr_list_item modes[] = {
|
|
{LO_OP_BUFFER, "Buffer Mode"},
|
|
{LO_OP_STREAM, "Stream Mode"},
|
|
{LO_OP_ZYNQ_SNAPSHOT, "FPGA Snapshot (Advanced)"},
|
|
{-1, NULL}
|
|
};
|
|
static const struct sr_list_item basic_modes[] = {
|
|
{LO_OP_BUFFER, "Buffer Mode"},
|
|
{LO_OP_STREAM, "Stream Mode"},
|
|
{-1, NULL}
|
|
};
|
|
if (key == SR_CONF_DEVICE_OPTIONS || key == SR_CONF_DEVICE_SESSIONS) {
|
|
const size_t first = device->pl_stream_magic ? 0 : 1;
|
|
*data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, options + first,
|
|
ARRAY_SIZE(options) - first, sizeof(options[0]));
|
|
return SR_OK;
|
|
}
|
|
if (key == SR_CONF_MAX_HEIGHT) {
|
|
*data = g_variant_new_strv(max_heights, ARRAY_SIZE(max_heights));
|
|
return SR_OK;
|
|
}
|
|
if (device->pl_stream_magic && key == SR_CONF_OPERATION_MODE) {
|
|
*data = g_variant_new_uint64((uintptr_t)(device->pl_snapshot_magic ? modes : basic_modes));
|
|
return SR_OK;
|
|
}
|
|
if (key != SR_CONF_SAMPLERATE)
|
|
return SR_ERR_NA;
|
|
|
|
uint64_t rates[ARRAY_SIZE(dividers)];
|
|
size_t count = 0;
|
|
for (size_t i = 0; i < ARRAY_SIZE(dividers); i++) {
|
|
if (dividers[i] < min_divider(sdi))
|
|
break;
|
|
rates[count++] = device->clock / dividers[i];
|
|
}
|
|
GVariantBuilder builder;
|
|
g_variant_builder_init(&builder, G_VARIANT_TYPE_VARDICT);
|
|
g_variant_builder_add(&builder, "{sv}", "samplerates",
|
|
g_variant_new_fixed_array(G_VARIANT_TYPE_UINT64, rates, count, sizeof(rates[0])));
|
|
*data = g_variant_builder_end(&builder);
|
|
return SR_OK;
|
|
}
|
|
|
|
static void packet(const struct sr_dev_inst *sdi, int type, int status, const void *payload)
|
|
{
|
|
struct sr_datafeed_packet feed = {.type = type, .status = status, .payload = payload};
|
|
ds_data_forward(sdi, &feed);
|
|
}
|
|
|
|
static gboolean deliver(const struct sr_dev_inst *sdi, size_t count);
|
|
static size_t pack_group(const uint8_t *samples, uint8_t *output, unsigned mask);
|
|
|
|
static void set_error(struct zynq_device *device, const char *reason)
|
|
{
|
|
g_free(device->last_error);
|
|
device->last_error = g_strdup(reason);
|
|
}
|
|
|
|
static void finish(const struct sr_dev_inst *sdi, int status)
|
|
{
|
|
struct zynq_device *device = sdi->priv;
|
|
if (!device->source_active)
|
|
return;
|
|
|
|
device->source_active = FALSE;
|
|
sr_session_source_remove(-1);
|
|
g_clear_object(&device->connection);
|
|
if (device->datagrams) {
|
|
send_udp(device->datagrams, device->pl_magic, ZLP_STOP, device->nonce, 0);
|
|
g_clear_object(&device->datagrams);
|
|
}
|
|
g_clear_pointer(&device->pl_reorder, g_free);
|
|
if (device->recorder && device->pending_count) {
|
|
memset(device->pending + device->pending_count, 0, SAMPLE_GROUP - device->pending_count);
|
|
size_t bytes = pack_group(device->pending, device->output, device->enabled_mask);
|
|
zynq_record_write(device->recorder, device->output, bytes, device->pending_count);
|
|
device->pending_count = 0;
|
|
}
|
|
gboolean published = FALSE;
|
|
if (!zynq_record_close(device->recorder, &published)) {
|
|
set_error(device, "Ошибка записи RJ2. Проверьте свободное место и скорость диска.");
|
|
status = SR_PKT_DATA_ERROR;
|
|
}
|
|
device->recorder = NULL;
|
|
if (published)
|
|
device->last_recording = g_steal_pointer(&device->active_record);
|
|
g_clear_pointer(&device->active_record, g_free);
|
|
packet(sdi, SR_DF_END, status, NULL);
|
|
}
|
|
|
|
static int stop(const struct sr_dev_inst *sdi, void *cb_data)
|
|
{
|
|
(void)cb_data;
|
|
struct zynq_device *device = sdi->priv;
|
|
if (device->source_active && device->recorder && device->pl_count) {
|
|
deliver(sdi, device->pl_count);
|
|
device->pl_count = 0;
|
|
}
|
|
finish(sdi, SR_PKT_OK);
|
|
return SR_OK;
|
|
}
|
|
|
|
static int open_device(struct sr_dev_inst *sdi)
|
|
{
|
|
sdi->status = SR_ST_ACTIVE;
|
|
return SR_OK;
|
|
}
|
|
|
|
static int close_device(struct sr_dev_inst *sdi)
|
|
{
|
|
stop(sdi, NULL);
|
|
struct zynq_device *device = sdi->priv;
|
|
g_clear_pointer(&device->record_file, g_free);
|
|
g_clear_pointer(&device->active_record, g_free);
|
|
g_clear_pointer(&device->last_recording, g_free);
|
|
g_clear_pointer(&device->last_error, g_free);
|
|
sdi->status = SR_ST_INACTIVE;
|
|
return SR_OK;
|
|
}
|
|
|
|
static int destroy_device(struct sr_dev_inst *sdi)
|
|
{
|
|
close_device(sdi);
|
|
sr_dev_inst_free(sdi);
|
|
return SR_OK;
|
|
}
|
|
|
|
static int get_status(const struct sr_dev_inst *sdi, struct sr_status *status, gboolean progress)
|
|
{
|
|
(void)progress;
|
|
const struct zynq_device *device = sdi->priv;
|
|
const uint32_t captured = g_atomic_int_get(&device->progress);
|
|
const gboolean triggered = g_atomic_int_get(&device->triggered);
|
|
const uint32_t count = triggered ? device->limit - captured : captured;
|
|
memset(status, 0, sizeof(*status));
|
|
status->trig_hit = triggered;
|
|
status->captured_cnt0 = count;
|
|
status->captured_cnt1 = count >> 8;
|
|
status->captured_cnt2 = count >> 16;
|
|
status->captured_cnt3 = count >> 24;
|
|
return SR_OK;
|
|
}
|
|
|
|
static gboolean configure_trigger(struct zynq_device *device)
|
|
{
|
|
device->level_mask = device->level_value = 0;
|
|
device->edge_mask = device->edge_value = device->any_edge_mask = 0;
|
|
|
|
if (device->pl_magic == ZLT_MAGIC)
|
|
return TRUE; /* Hardware stages are prepared after the simple masks. */
|
|
if (!trigger || !trigger->trigger_en)
|
|
return TRUE;
|
|
if (trigger->trigger_mode != SIMPLE_TRIGGER)
|
|
return FALSE;
|
|
|
|
for (unsigned channel = 0; channel < CHANNELS; channel++) {
|
|
const unsigned bit = 1u << channel;
|
|
if (!(device->enabled_mask & bit))
|
|
continue;
|
|
|
|
switch (trigger->trigger0[TriggerStages][channel]) {
|
|
case '0':
|
|
device->level_mask |= bit;
|
|
break;
|
|
case '1':
|
|
device->level_mask |= bit;
|
|
device->level_value |= bit;
|
|
break;
|
|
case 'F':
|
|
device->edge_mask |= bit;
|
|
break;
|
|
case 'R':
|
|
device->edge_mask |= bit;
|
|
device->edge_value |= bit;
|
|
break;
|
|
case 'C':
|
|
device->any_edge_mask |= bit;
|
|
break;
|
|
}
|
|
}
|
|
return device->stream || device->pl || !device->any_edge_mask;
|
|
}
|
|
|
|
static gboolean trigger_pattern(const char *pattern, uint32_t *result)
|
|
{
|
|
*result = 0;
|
|
for (unsigned i = 0; i < MaxTriggerProbes; i++) {
|
|
const char flag = pattern[i];
|
|
if (flag == 'X' || flag == ' ')
|
|
continue;
|
|
if (i >= CHANNELS)
|
|
return FALSE;
|
|
const unsigned bit = 1u << i;
|
|
if (flag == '0' || flag == '1' || flag == 'R' || flag == 'F')
|
|
*result |= bit;
|
|
if (flag == '1' || flag == 'R')
|
|
*result |= bit << 8;
|
|
if (flag == 'R' || flag == 'F' || flag == 'C')
|
|
*result |= bit << 16;
|
|
}
|
|
return TRUE;
|
|
}
|
|
|
|
static gboolean prepare_hardware_trigger(struct zynq_device *device)
|
|
{
|
|
memset(device->stages, 0, sizeof(device->stages));
|
|
device->stage_sent = 0;
|
|
device->stage_count = 1;
|
|
device->pretrigger = 0;
|
|
device->serial_trigger = FALSE;
|
|
device->external_trigger = trigger && trigger->trigger_en;
|
|
if (!device->external_trigger)
|
|
return TRUE;
|
|
device->pretrigger = MIN(device->limit - 1, device->limit * trigger->trigger_pos / 100);
|
|
if (trigger->trigger_mode == SIMPLE_TRIGGER) {
|
|
device->stages[0][0] = 1u << 4;
|
|
return trigger_pattern(trigger->trigger0[TriggerStages], &device->stages[0][1]);
|
|
}
|
|
if (trigger->trigger_mode == ADV_TRIGGER) {
|
|
device->stage_count = MIN((unsigned)trigger->trigger_stages + 1, TriggerStages);
|
|
for (unsigned i = 0; i < device->stage_count; i++) {
|
|
uint32_t *stage = device->stages[i];
|
|
stage[0] = i | (trigger->trigger_logic[i] & 3u) << 4 |
|
|
(trigger->trigger0_inv[i] & 1u) << 6 | (trigger->trigger1_inv[i] & 1u) << 7;
|
|
if (!trigger_pattern(trigger->trigger0[i], &stage[1]) ||
|
|
!trigger_pattern(trigger->trigger1[i], &stage[2]))
|
|
return FALSE;
|
|
stage[3] = MAX(trigger->trigger0_count[i], 1u) - 1;
|
|
}
|
|
return TRUE;
|
|
}
|
|
if (trigger->trigger_mode != SERIAL_TRIGGER)
|
|
return FALSE;
|
|
device->serial_trigger = TRUE;
|
|
device->stage_count = 4;
|
|
for (unsigned i = 0; i < device->stage_count; i++)
|
|
device->stages[i][0] = i;
|
|
if (!trigger_pattern(trigger->trigger0[0], &device->stages[0][1]) ||
|
|
!trigger_pattern(trigger->trigger1[0], &device->stages[0][2]) ||
|
|
!trigger_pattern(trigger->trigger0[1], &device->stages[1][1]) ||
|
|
!device->stages[1][1] || trigger->trigger0_count[STriggerDataStage] > 15)
|
|
return FALSE;
|
|
unsigned pin = 0;
|
|
while (pin < CHANNELS && trigger->trigger0[2][pin] != '0')
|
|
pin++;
|
|
if (pin == CHANNELS)
|
|
return FALSE;
|
|
device->stages[2][3] = pin;
|
|
const unsigned bits = trigger->trigger0_count[STriggerDataStage];
|
|
device->stages[3][3] = bits;
|
|
for (unsigned i = 0; i <= bits; i++) {
|
|
const char value = trigger->trigger0[STriggerDataStage][i];
|
|
if (value == '0' || value == '1')
|
|
device->stages[3][2] |= 1u << i;
|
|
if (value == '1')
|
|
device->stages[3][1] |= 1u << i;
|
|
}
|
|
return TRUE;
|
|
}
|
|
|
|
static size_t trigger_offset(const struct sr_dev_inst *sdi, const uint8_t *samples, size_t count)
|
|
{
|
|
struct zynq_device *device = sdi->priv;
|
|
if (g_atomic_int_get(&device->triggered))
|
|
return 0;
|
|
|
|
const unsigned edges = device->edge_mask | device->any_edge_mask;
|
|
for (size_t i = 0; i < count; i++) {
|
|
const unsigned sample = samples[i];
|
|
gboolean matches = !device->stream || device->pl_magic == ZLR_MAGIC ||
|
|
(((sample & device->level_mask) == device->level_value) &&
|
|
(!edges || (device->previous_valid &&
|
|
((sample ^ device->previous) & edges) == edges &&
|
|
(sample & device->edge_mask) == device->edge_value)));
|
|
device->previous = sample;
|
|
device->previous_valid = TRUE;
|
|
if (matches) {
|
|
const struct ds_trigger_pos position = {
|
|
.status = device->pl_magic == ZLT_MAGIC ? device->external_trigger :
|
|
(device->level_mask | edges) != 0,
|
|
.real_pos = device->pl_magic == ZLT_MAGIC ? device->pretrigger : 0
|
|
};
|
|
g_atomic_int_set(&device->triggered, 1);
|
|
packet(sdi, SR_DF_TRIGGER, SR_PKT_OK, &position);
|
|
return i;
|
|
}
|
|
}
|
|
return count;
|
|
}
|
|
|
|
/* Turn eight samples (one byte each) into eight channel bytes. */
|
|
static uint64_t transpose(const uint8_t *data)
|
|
{
|
|
uint64_t matrix, swap;
|
|
memcpy(&matrix, data, sizeof(matrix));
|
|
matrix = GUINT64_FROM_LE(matrix);
|
|
swap = (matrix ^ (matrix >> 7)) & UINT64_C(0x00aa00aa00aa00aa);
|
|
matrix ^= swap ^ (swap << 7);
|
|
swap = (matrix ^ (matrix >> 14)) & UINT64_C(0x0000cccc0000cccc);
|
|
matrix ^= swap ^ (swap << 14);
|
|
swap = (matrix ^ (matrix >> 28)) & UINT64_C(0x00000000f0f0f0f0);
|
|
return matrix ^ swap ^ (swap << 28);
|
|
}
|
|
|
|
static size_t pack_group(const uint8_t *samples, uint8_t *output, unsigned mask)
|
|
{
|
|
uint64_t planes[8];
|
|
for (size_t i = 0; i < ARRAY_SIZE(planes); i++)
|
|
planes[i] = transpose(samples + i * 8);
|
|
|
|
size_t bytes = 0;
|
|
for (unsigned channel = 0; channel < CHANNELS; channel++) {
|
|
if (mask & (1u << channel)) {
|
|
for (size_t i = 0; i < ARRAY_SIZE(planes); i++)
|
|
output[bytes++] = planes[i] >> (channel * 8);
|
|
}
|
|
}
|
|
return bytes;
|
|
}
|
|
|
|
static gboolean deliver(const struct sr_dev_inst *sdi, size_t count)
|
|
{
|
|
struct zynq_device *device = sdi->priv;
|
|
const size_t skip = trigger_offset(sdi, device->input, count);
|
|
const uint8_t *samples = device->input + skip;
|
|
const uint64_t progress = device->delivered;
|
|
const uint64_t limit = device->recorder ? device->record_samples : device->limit;
|
|
const gboolean bounded = device->recorder ? !device->disk_buffer : !device->loop;
|
|
size_t size = count - skip;
|
|
if (bounded)
|
|
size = MIN(size, limit - progress);
|
|
const uint64_t next = progress + size;
|
|
const gboolean done = bounded && next == limit;
|
|
const size_t available = size + device->pending_count;
|
|
size_t bytes = 0;
|
|
|
|
if (device->pending_count) {
|
|
size_t take = MIN(size, SAMPLE_GROUP - device->pending_count);
|
|
memcpy(device->pending + device->pending_count, samples, take);
|
|
device->pending_count += take;
|
|
samples += take;
|
|
size -= take;
|
|
if (device->pending_count == SAMPLE_GROUP) {
|
|
bytes += pack_group(device->pending, device->output, device->enabled_mask);
|
|
device->pending_count = 0;
|
|
}
|
|
}
|
|
while (size >= SAMPLE_GROUP) {
|
|
bytes += pack_group(samples, device->output + bytes, device->enabled_mask);
|
|
samples += SAMPLE_GROUP;
|
|
size -= SAMPLE_GROUP;
|
|
}
|
|
if (size) {
|
|
memcpy(device->pending, samples, size);
|
|
device->pending_count = size;
|
|
}
|
|
|
|
/* DSView's last packet is clipped to the requested sample count. */
|
|
if (done && device->pending_count) {
|
|
memset(device->pending + device->pending_count, 0, SAMPLE_GROUP - device->pending_count);
|
|
bytes += pack_group(device->pending, device->output + bytes, device->enabled_mask);
|
|
device->pending_count = 0;
|
|
}
|
|
if (bytes) {
|
|
const size_t valid_samples = available - device->pending_count;
|
|
if (!zynq_record_write(device->recorder, device->output, bytes, valid_samples))
|
|
return TRUE;
|
|
if (!device->disk_buffer) {
|
|
const struct sr_datafeed_logic logic = {
|
|
.format = LA_CROSS_DATA, .length = bytes, .unitsize = 1, .data = device->output
|
|
};
|
|
packet(sdi, SR_DF_LOGIC, SR_PKT_OK, &logic);
|
|
}
|
|
}
|
|
device->delivered = next;
|
|
if (device->disk_buffer && device->recorder)
|
|
g_atomic_int_set(&device->recorded_tenths,
|
|
MIN(next * 10 / (device->clock / device->divider), G_MAXINT));
|
|
uint64_t preview_progress = MIN(next, device->limit);
|
|
if (device->recorder)
|
|
preview_progress = bounded ? (double)next / limit * device->limit : 0;
|
|
g_atomic_int_set(&device->progress, preview_progress);
|
|
return done;
|
|
}
|
|
|
|
static void prepare_receive(struct zynq_device *device, enum receive_state state, size_t size)
|
|
{
|
|
device->state = state;
|
|
device->received = 0;
|
|
device->wanted = size;
|
|
}
|
|
|
|
static int receive_failure(const struct sr_dev_inst *sdi, const char *reason)
|
|
{
|
|
struct zynq_device *device = sdi->priv;
|
|
sr_err("%s: %s", sdi->name, reason);
|
|
set_error(device, reason);
|
|
// Preserve the contiguous tail that was already decoded before the gap.
|
|
if (device->recorder && device->pl_count && !zynq_record_failed(device->recorder)) {
|
|
deliver(sdi, device->pl_count);
|
|
device->pl_count = 0;
|
|
}
|
|
finish(sdi, SR_PKT_DATA_ERROR);
|
|
return FALSE;
|
|
}
|
|
|
|
static gboolean append_pl_block(const struct sr_dev_inst *sdi, const uint8_t *data)
|
|
{
|
|
struct zynq_device *device = sdi->priv;
|
|
if (device->pl_magic == ZLR_MAGIC) {
|
|
for (size_t i = 0; i < ZLP_BLOCK; i += 2) {
|
|
size_t left = (size_t)data[i + 1] + 1;
|
|
while (left) {
|
|
size_t size = MIN(left, ZLS_BLOCK - device->pl_count);
|
|
memset(device->input + device->pl_count, data[i], size);
|
|
device->pl_count += size;
|
|
left -= size;
|
|
if (device->pl_count == ZLS_BLOCK) {
|
|
device->pl_count = 0;
|
|
if (deliver(sdi, ZLS_BLOCK))
|
|
return TRUE;
|
|
}
|
|
}
|
|
}
|
|
return FALSE;
|
|
}
|
|
if (device->pl_packed) {
|
|
for (size_t i = 0; i < ZLP_BLOCK; i++) {
|
|
uint8_t packed = data[i];
|
|
device->input[device->pl_count++] = device->pl_expand[packed & 15];
|
|
device->input[device->pl_count++] = device->pl_expand[packed >> 4];
|
|
}
|
|
} else {
|
|
memcpy(device->input + device->pl_count, data, ZLP_BLOCK);
|
|
device->pl_count += ZLP_BLOCK;
|
|
}
|
|
if (device->pl_count < ZLS_BLOCK)
|
|
return FALSE;
|
|
|
|
gboolean done = deliver(sdi, device->pl_count);
|
|
device->pl_count = 0;
|
|
return done;
|
|
}
|
|
|
|
static void start_pl_triggered(struct zynq_device *device)
|
|
{
|
|
const uint32_t count = (device->limit + ZLP_BLOCK - 1) / ZLP_BLOCK * ZLP_BLOCK;
|
|
const uint32_t trigger_config = device->level_mask | device->level_value << 8 |
|
|
device->edge_mask << 16 | device->edge_value << 24;
|
|
const gboolean rle = device->pl_magic == ZLR_MAGIC;
|
|
const uint32_t value = device->divider | (rle ? device->enabled_mask << 16 : 0);
|
|
uint32_t request[] = {device->pl_magic, ZLP_START, device->nonce, value,
|
|
rle ? 0 : count, trigger_config, device->any_edge_mask};
|
|
if (device->pl_magic == ZLT_MAGIC) {
|
|
request[5] = (device->pretrigger << 16) | ((device->stage_count - 1) << 8) |
|
|
device->serial_trigger;
|
|
request[6] = 0;
|
|
}
|
|
for (size_t i = 0; i < ARRAY_SIZE(request); i++)
|
|
request[i] = GUINT32_TO_LE(request[i]);
|
|
g_socket_send(device->datagrams, (const char *)request, sizeof(request), NULL, NULL);
|
|
}
|
|
|
|
static void send_trigger_stage(struct zynq_device *device)
|
|
{
|
|
const uint32_t *stage = device->stages[device->stage_sent];
|
|
uint32_t request[] = {ZLT_MAGIC, ZLP_CONFIG, device->nonce, stage[0], stage[1], stage[2], stage[3]};
|
|
for (size_t i = 0; i < ARRAY_SIZE(request); i++)
|
|
request[i] = GUINT32_TO_LE(request[i]);
|
|
g_socket_send(device->datagrams, (const char *)request, sizeof(request), NULL, NULL);
|
|
}
|
|
|
|
static int receive_pl(const struct sr_dev_inst *sdi)
|
|
{
|
|
struct zynq_device *device = sdi->priv;
|
|
GSocket *socket = device->datagrams;
|
|
const gint64 now = g_get_monotonic_time();
|
|
if (zynq_record_failed(device->recorder))
|
|
return receive_failure(sdi, "RJ2 disk recording failed");
|
|
if (now >= device->keepalive) {
|
|
uint32_t value = device->divider;
|
|
const gboolean hardware_trigger = !device->stream || device->pl_magic == ZLR_MAGIC;
|
|
if (device->pl_magic == ZLP2_MAGIC)
|
|
value |= device->enabled_mask << 16;
|
|
if (device->pl_magic == ZLT_MAGIC && device->stage_sent < device->stage_count)
|
|
send_trigger_stage(device);
|
|
else if (hardware_trigger && !device->pl_started) {
|
|
start_pl_triggered(device);
|
|
// Waiting for the trigger may take arbitrarily long. The probe
|
|
// distinguishes that wait from a disconnected Ethernet link.
|
|
send_udp(socket, device->pl_magic, ZLP_PROBE, device->nonce, 0);
|
|
} else {
|
|
send_udp(socket, device->pl_magic, device->pl_started ? ZLP_KEEPALIVE : ZLP_START,
|
|
device->nonce, value);
|
|
}
|
|
device->keepalive = now + PL_HEARTBEAT;
|
|
}
|
|
if (now >= device->deadline) {
|
|
if (device->pl_pending) {
|
|
g_autofree char *reason = g_strdup_printf(
|
|
"RJ2 не получил UDP-пакет №%u. В очереди %u следующих пакетов.",
|
|
device->sequence, device->pl_pending);
|
|
return receive_failure(sdi, reason);
|
|
}
|
|
return receive_failure(sdi, "RJ2 не отвечает. Проверьте Ethernet и адрес платы.");
|
|
}
|
|
|
|
for (unsigned n = 0; n < 64; n++) {
|
|
uint8_t response[ZLP_BLOCK + 17];
|
|
GError *error = NULL;
|
|
gssize bytes = g_socket_receive(socket, (char *)response, sizeof(response), NULL, &error);
|
|
if (bytes < 0) {
|
|
gboolean waiting = g_error_matches(error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
|
|
g_clear_error(&error);
|
|
if (!waiting)
|
|
return receive_failure(sdi, "RJ2 socket failed");
|
|
if (device->pl_count && deliver(sdi, device->pl_count)) {
|
|
finish(sdi, SR_PKT_OK);
|
|
return FALSE;
|
|
}
|
|
device->pl_count = 0;
|
|
g_socket_condition_timed_wait(socket, G_IO_IN, 2 * G_TIME_SPAN_MILLISECOND, NULL, NULL);
|
|
break;
|
|
}
|
|
if (bytes < 16 || word(response, 0) != device->pl_magic || word(response, 2) != device->nonce)
|
|
continue;
|
|
|
|
const uint32_t type = word(response, 1);
|
|
const uint32_t value = word(response, 3);
|
|
if (type == ZLP_INFO && bytes == 16) {
|
|
if (!device->pl_started && value == device->clock)
|
|
device->deadline = now + IO_TIMEOUT;
|
|
continue;
|
|
}
|
|
if (device->pl_magic == ZLT_MAGIC && type == ZLP_CONFIGURED && bytes == 16) {
|
|
if (value == device->stage_sent && value < device->stage_count) {
|
|
device->stage_sent++;
|
|
device->keepalive = 0;
|
|
}
|
|
continue;
|
|
}
|
|
if (type == ZLP_ERROR && bytes == 16) {
|
|
if (value == ZLP_BUSY && !device->pl_started)
|
|
continue; /* Retry START while the last capture drains. */
|
|
if (value == ZLP_OVERFLOW) {
|
|
return receive_failure(sdi, device->pl_magic == ZLR_MAGIC ?
|
|
"Переполнение FPGA. Для этих сигналов выберите 100 Мвыб/с или меньше каналов." :
|
|
"Переполнение FIFO FPGA. Проверьте сеть или снизьте частоту.");
|
|
}
|
|
return receive_failure(sdi, "RJ2 rejected the capture");
|
|
}
|
|
if (type != ZLP_DATA || bytes != ZLP_BLOCK + 16)
|
|
return receive_failure(sdi, "Invalid RJ2 sample packet");
|
|
// Unsigned distance keeps the window valid across sequence wraparound.
|
|
uint32_t ahead = value - device->sequence;
|
|
if (ahead >= (UINT32_C(1) << 31))
|
|
continue; /* Old duplicate. */
|
|
if (ahead >= PL_REORDER_BLOCKS) {
|
|
g_autofree char *reason = g_strdup_printf(
|
|
"Разрыв UDP в RJ2: ожидался пакет №%u, получен №%u.", device->sequence, value);
|
|
return receive_failure(sdi, reason);
|
|
}
|
|
|
|
device->pl_started = TRUE;
|
|
struct pl_sample_block *block = &device->pl_reorder[value % PL_REORDER_BLOCKS];
|
|
if (block->ready)
|
|
continue; /* Duplicate inside the window. */
|
|
memcpy(block->data, response + 16, ZLP_BLOCK);
|
|
block->ready = TRUE;
|
|
device->pl_pending++;
|
|
block = &device->pl_reorder[device->sequence % PL_REORDER_BLOCKS];
|
|
while (block->ready) {
|
|
block->ready = FALSE;
|
|
device->pl_pending--;
|
|
device->sequence++;
|
|
device->deadline = now + IO_TIMEOUT;
|
|
if (append_pl_block(sdi, block->data)) {
|
|
finish(sdi, SR_PKT_OK);
|
|
return FALSE;
|
|
}
|
|
if (!device->source_active)
|
|
return FALSE;
|
|
block = &device->pl_reorder[device->sequence % PL_REORDER_BLOCKS];
|
|
}
|
|
}
|
|
return TRUE;
|
|
}
|
|
|
|
static int receive_data(int fd, int revents, const struct sr_dev_inst *sdi)
|
|
{
|
|
(void)fd;
|
|
if (revents < 0)
|
|
return TRUE;
|
|
|
|
struct zynq_device *device = sdi->priv;
|
|
if (device->pl)
|
|
return receive_pl(sdi);
|
|
GSocket *socket = g_socket_connection_get_socket(device->connection);
|
|
|
|
for (unsigned step = 0; step < 64; step++) {
|
|
GError *error = NULL;
|
|
gssize bytes = g_socket_receive(socket, (char *)device->input + device->received,
|
|
device->wanted - device->received, NULL, &error);
|
|
if (bytes <= 0) {
|
|
gboolean waiting = g_error_matches(error, G_IO_ERROR, G_IO_ERROR_WOULD_BLOCK);
|
|
g_clear_error(&error);
|
|
if (!waiting)
|
|
return receive_failure(sdi, "Ethernet connection closed before capture completed");
|
|
if (g_get_monotonic_time() >= device->deadline)
|
|
return receive_failure(sdi, "Capture timed out");
|
|
g_socket_condition_timed_wait(socket, G_IO_IN, 2 * G_TIME_SPAN_MILLISECOND, NULL, NULL);
|
|
return TRUE;
|
|
}
|
|
|
|
device->deadline = g_get_monotonic_time() + IO_TIMEOUT;
|
|
device->received += bytes;
|
|
if (device->received < device->wanted)
|
|
continue;
|
|
|
|
const uint8_t *input = device->input;
|
|
switch (device->state) {
|
|
case REPLY:
|
|
if (word(input, 0) != (device->stream ? ZLS_MAGIC : ZLA_MAGIC) ||
|
|
word(input, 1) != 1 || word(input, 3) != device->clock ||
|
|
word(input, 4) != device->divider - !device->stream ||
|
|
word(input, 6) != CHANNELS || word(input, 7) != !device->stream)
|
|
return receive_failure(sdi, "Invalid capture reply");
|
|
if (word(input, 2) != 0)
|
|
return receive_failure(sdi, !device->stream && word(input, 2) == 2 ?
|
|
"Trigger timed out" : "Board rejected the capture request");
|
|
if (word(input, 5) != (device->stream ? ZLS_BLOCK : device->limit))
|
|
return receive_failure(sdi, "Unexpected capture length");
|
|
if (device->stream)
|
|
prepare_receive(device, FRAME_HEADER, 12);
|
|
else
|
|
prepare_receive(device, SAMPLES, MIN(device->limit, ZLS_BLOCK));
|
|
break;
|
|
case FRAME_HEADER:
|
|
if (word(input, 0) != device->sequence++)
|
|
return receive_failure(sdi, "Ethernet stream lost sample order");
|
|
if (word(input, 2) == ZLS_OVERFLOW && word(input, 1) == 0) {
|
|
packet(sdi, SR_DF_OVERFLOW, SR_PKT_OK, NULL);
|
|
finish(sdi, SR_PKT_OK);
|
|
return FALSE;
|
|
}
|
|
if (word(input, 1) != ZLS_BLOCK || word(input, 2))
|
|
return receive_failure(sdi, "Invalid Ethernet stream frame");
|
|
prepare_receive(device, SAMPLES, ZLS_BLOCK);
|
|
break;
|
|
case SAMPLES:
|
|
if (deliver(sdi, device->wanted)) {
|
|
finish(sdi, SR_PKT_OK);
|
|
return FALSE;
|
|
}
|
|
if (device->stream)
|
|
prepare_receive(device, FRAME_HEADER, 12);
|
|
else
|
|
prepare_receive(device, SAMPLES,
|
|
MIN(device->limit - g_atomic_int_get(&device->progress), ZLS_BLOCK));
|
|
break;
|
|
}
|
|
}
|
|
return TRUE;
|
|
}
|
|
|
|
static int start(struct sr_dev_inst *sdi, void *cb_data)
|
|
{
|
|
(void)cb_data;
|
|
struct zynq_device *device = sdi->priv;
|
|
device->enabled_mask = channel_mask(sdi);
|
|
device->divider = MAX(device->divider, min_divider(sdi));
|
|
g_clear_pointer(&device->last_error, g_free);
|
|
g_clear_pointer(&device->last_recording, g_free);
|
|
if (device->disk_buffer && (!device->record_file || !*device->record_file))
|
|
return SR_ERR_ARG;
|
|
if (!device->enabled_mask || !configure_trigger(device)) {
|
|
sr_err("%s: enable a channel and use a supported simple trigger", sdi->name);
|
|
return SR_ERR_ARG;
|
|
}
|
|
if (device->pl_magic == ZLT_MAGIC && !prepare_hardware_trigger(device)) {
|
|
set_error(device, "Проверьте настройки триггера. Доступны входы D0–D7.");
|
|
sr_err("%s: invalid advanced trigger", sdi->name);
|
|
return SR_ERR_ARG;
|
|
}
|
|
|
|
if (device->pl) {
|
|
device->datagrams = connect_udp(sdi->path);
|
|
device->nonce = g_random_int() | 1u;
|
|
device->keepalive = 0;
|
|
device->pl_started = FALSE;
|
|
device->pl_count = 0;
|
|
device->pl_pending = 0;
|
|
if (device->pl_rle_available && device->stream)
|
|
device->pl_magic = device->divider == 1 && !pack_channels(device->enabled_mask) ?
|
|
ZLR_MAGIC : ZLP2_MAGIC;
|
|
device->pl_packed = device->pl_magic == ZLP2_MAGIC && pack_channels(device->enabled_mask);
|
|
if (device->pl_packed) {
|
|
for (unsigned nibble = 0; nibble < ARRAY_SIZE(device->pl_expand); nibble++) {
|
|
unsigned bit = 0;
|
|
unsigned raw = 0;
|
|
for (unsigned channel = 0; channel < CHANNELS; channel++) {
|
|
if (device->enabled_mask & (1u << channel))
|
|
raw |= ((nibble >> bit++) & 1u) << channel;
|
|
}
|
|
device->pl_expand[nibble] = raw;
|
|
}
|
|
}
|
|
if (!device->datagrams)
|
|
return SR_ERR;
|
|
device->pl_reorder = g_try_new0(struct pl_sample_block, PL_REORDER_BLOCKS);
|
|
if (!device->pl_reorder) {
|
|
g_clear_object(&device->datagrams);
|
|
return SR_ERR_MALLOC;
|
|
}
|
|
} else {
|
|
GSocketConnection *connection = connect_to(sdi->path, device->stream ? ZLS_PORT : ZLA_PORT);
|
|
if (!connection) {
|
|
sr_err("%s: cannot open capture connection", sdi->name);
|
|
return SR_ERR;
|
|
}
|
|
uint32_t stream_request[] = {ZLS_MAGIC, 1, device->divider, 0};
|
|
uint32_t snapshot_request[] = {ZLA_REQUEST, 1, device->limit, device->divider - 1,
|
|
device->level_mask, device->level_value, device->edge_mask, device->edge_value,
|
|
0, 0, 0};
|
|
gboolean sent = device->stream ?
|
|
send_request(connection, stream_request, ARRAY_SIZE(stream_request)) :
|
|
send_request(connection, snapshot_request, ARRAY_SIZE(snapshot_request));
|
|
if (!sent) {
|
|
g_object_unref(connection);
|
|
return SR_ERR;
|
|
}
|
|
g_socket_set_blocking(g_socket_connection_get_socket(connection), FALSE);
|
|
device->connection = connection;
|
|
}
|
|
|
|
if (device->record_file && *device->record_file) {
|
|
char *file = g_steal_pointer(&device->record_file);
|
|
device->record_samples = CLAMP(device->record_samples, SAMPLE_GROUP,
|
|
(uint64_t)device->clock * RECORD_SECONDS / device->divider);
|
|
device->recorder = zynq_record_open(file, device->clock,
|
|
device->divider, device->enabled_mask, device->pl_magic == ZLR_MAGIC);
|
|
if (!device->recorder) {
|
|
g_free(file);
|
|
g_clear_object(&device->datagrams);
|
|
g_clear_pointer(&device->pl_reorder, g_free);
|
|
return SR_ERR;
|
|
}
|
|
device->active_record = file;
|
|
}
|
|
/* Use the dummy-source loop. On Windows g_poll() expects HANDLEs. */
|
|
if (sr_session_source_add(-1, 0, 0, receive_data, sdi) != SR_OK) {
|
|
g_clear_object(&device->connection);
|
|
g_clear_object(&device->datagrams);
|
|
g_clear_pointer(&device->pl_reorder, g_free);
|
|
zynq_record_close(device->recorder, NULL);
|
|
device->recorder = NULL;
|
|
g_clear_pointer(&device->active_record, g_free);
|
|
return SR_ERR;
|
|
}
|
|
device->source_active = TRUE;
|
|
device->sequence = 0;
|
|
device->previous_valid = FALSE;
|
|
device->pending_count = 0;
|
|
device->delivered = 0;
|
|
g_atomic_int_set(&device->progress, 0);
|
|
g_atomic_int_set(&device->triggered, 0);
|
|
g_atomic_int_set(&device->recorded_tenths, 0);
|
|
device->deadline = g_get_monotonic_time() +
|
|
(device->stream && device->pl_magic != ZLR_MAGIC ? IO_TIMEOUT : SNAPSHOT_TIMEOUT);
|
|
prepare_receive(device, REPLY, 32);
|
|
packet(sdi, SR_DF_HEADER, SR_PKT_OK, NULL);
|
|
return SR_OK;
|
|
}
|
|
|
|
static struct sr_dev_driver zynq_driver = {
|
|
.name = "Zynq Ethernet",
|
|
.longname = "Zynq Ethernet Logic Analyzer",
|
|
.api_version = 1,
|
|
.config_get = config_get,
|
|
.config_set = config_set,
|
|
.config_list = config_list,
|
|
.dev_open = open_device,
|
|
.dev_close = close_device,
|
|
.dev_destroy = destroy_device,
|
|
.dev_status_get = get_status,
|
|
.dev_acquisition_start = start,
|
|
.dev_acquisition_stop = stop
|
|
};
|
|
|
|
SR_PRIV struct sr_dev_inst *sr_zynq_probe(const char *address, enum zynq_transport transport)
|
|
{
|
|
const gboolean pl = transport == ZYNQ_PL_STREAM || transport == ZYNQ_PL_BUFFER;
|
|
const gboolean stream = transport == ZYNQ_PL_STREAM || transport == ZYNQ_TCP_STREAM;
|
|
uint32_t buffer_magic = ZLT_MAGIC;
|
|
uint32_t clock = pl ? probe_pl(address, stream ? ZLP2_MAGIC : buffer_magic) : probe_clock(address, stream);
|
|
if (!clock && pl) {
|
|
buffer_magic = ZLB_MAGIC;
|
|
clock = probe_pl(address, stream ? ZLP_MAGIC : buffer_magic);
|
|
}
|
|
if (!clock)
|
|
return NULL;
|
|
|
|
const char *mode = stream ? (pl ? "RJ2" : "Stream") : (pl ? "RJ2 Snapshot" : "Buffer");
|
|
char *name = g_strdup_printf("Zynq %s (%s)", mode, address);
|
|
struct sr_dev_inst *sdi = sr_dev_inst_new(LOGIC, SR_ST_INACTIVE, "Zynq", name, "1");
|
|
g_free(name);
|
|
if (!sdi)
|
|
return NULL;
|
|
|
|
struct zynq_device *device = g_new0(struct zynq_device, 1);
|
|
device->stream = stream;
|
|
device->pl = pl;
|
|
device->clock = clock;
|
|
device->pl_magic = pl ? (stream ? (clock == ZLP2_CLOCK_HZ ? ZLP2_MAGIC : ZLP_MAGIC) : buffer_magic) : 0;
|
|
device->pl_rle_available = device->pl_magic == ZLP2_MAGIC && probe_pl(address, ZLR_MAGIC);
|
|
if (pl && stream) {
|
|
device->pl_stream_magic = device->pl_magic;
|
|
if (probe_pl(address, ZLT_MAGIC))
|
|
device->pl_snapshot_magic = ZLT_MAGIC;
|
|
else if (probe_pl(address, ZLB_MAGIC))
|
|
device->pl_snapshot_magic = ZLB_MAGIC;
|
|
}
|
|
sdi->priv = device;
|
|
device->divider = pl && stream ? 2 : min_divider(sdi);
|
|
device->limit = stream ? 1000000 : ZLA_MAX_SAMPLES;
|
|
device->record_samples = device->limit;
|
|
sdi->driver = &zynq_driver;
|
|
sdi->dev_type = DEV_TYPE_NETWORK;
|
|
sdi->path = g_strdup(address);
|
|
for (unsigned channel = 0; channel < CHANNELS; channel++) {
|
|
char label[3] = {'D', '0' + channel, '\0'};
|
|
sdi->channels = g_slist_append(sdi->channels, sr_channel_new(channel, SR_CHANNEL_LOGIC, TRUE, label));
|
|
}
|
|
return sdi;
|
|
}
|