+ /* Accept 200kHz to 50MHz range, and map to near value. */
+ if (want_rate >= SR_KHZ(200) && want_rate <= SR_MHZ(50)) {
+ div = SR_MHZ(50) / want_rate;
+ rate = SR_MHZ(50) / div;
+ if (have_rate)
+ *have_rate = rate;
+ return SR_OK;
+ }
+
+ return SR_ERR_ARG;
+}
+
+/* Gets called at probe time. Can seed software settings from hardware state. */
+SR_PRIV int sigma_fetch_hw_config(const struct sr_dev_inst *sdi)
+{
+ struct dev_context *devc;
+ int ret;
+ uint8_t regaddr, regval;
+
+ devc = sdi->priv;
+ if (!devc)
+ return SR_ERR_ARG;
+
+ /* Seed configuration values from defaults. */
+ devc->firmware_idx = SIGMA_FW_NONE;
+ devc->clock.samplerate = samplerates[0];
+
+ /* TODO
+ * Ideally the device driver could retrieve recently stored
+ * details from hardware registers, thus re-use user specified
+ * configuration values across sigrok sessions. Which could
+ * avoid repeated expensive though unnecessary firmware uploads,
+ * improve performance and usability. Unfortunately it appears
+ * that the registers range which is documented as available for
+ * application use keeps providing 0xff data content. At least
+ * with the netlist version which ships with sigrok. The same
+ * was observed with unused registers in the first page.
+ */
+ return SR_ERR_NA;
+
+ /* This is for research, currently does not work yet. */
+ ret = sigma_check_open(sdi);
+ regaddr = 16;
+ regaddr = 14;
+ ret = sigma_set_register(devc, regaddr, 'F');
+ ret = sigma_get_register(devc, regaddr, ®val);
+ sr_warn("%s() reg[%u] val[%u] rc[%d]", __func__, regaddr, regval, ret);
+ ret = sigma_check_close(devc);
+ return ret;
+}
+
+/* Gets called after successful (volatile) hardware configuration. */
+SR_PRIV int sigma_store_hw_config(const struct sr_dev_inst *sdi)
+{
+ /* TODO See above, registers seem to not hold written data. */
+ (void)sdi;
+ return SR_ERR_NA;
+}
+
+SR_PRIV int sigma_set_samplerate(const struct sr_dev_inst *sdi)
+{
+ struct dev_context *devc;
+ struct drv_context *drvc;
+ uint64_t samplerate;
+ int ret;
+ size_t num_channels;
+
+ devc = sdi->priv;
+ drvc = sdi->driver->context;
+
+ /* Accept any caller specified rate which the hardware supports. */
+ ret = sigma_normalize_samplerate(devc->clock.samplerate, &samplerate);
+ if (ret != SR_OK)
+ return ret;
+
+ /*
+ * Depending on the samplerates of 200/100/50- MHz, specific
+ * firmware is required and higher rates might limit the set
+ * of available channels.
+ */
+ num_channels = devc->interp.num_channels;
+ if (samplerate <= SR_MHZ(50)) {
+ ret = upload_firmware(drvc->sr_ctx, devc, SIGMA_FW_50MHZ);
+ num_channels = 16;
+ } else if (samplerate == SR_MHZ(100)) {
+ ret = upload_firmware(drvc->sr_ctx, devc, SIGMA_FW_100MHZ);
+ num_channels = 8;
+ } else if (samplerate == SR_MHZ(200)) {
+ ret = upload_firmware(drvc->sr_ctx, devc, SIGMA_FW_200MHZ);
+ num_channels = 4;
+ }
+
+ /*
+ * The samplerate affects the number of available logic channels
+ * as well as a sample memory layout detail (the number of samples
+ * which the device will communicate within an "event").
+ */
+ if (ret == SR_OK) {
+ devc->interp.num_channels = num_channels;
+ devc->interp.samples_per_event = 16 / devc->interp.num_channels;
+ }
+
+ /*
+ * Store the firmware type and most recently configured samplerate
+ * in hardware, such that subsequent sessions can start from there.
+ * This is a "best effort" approach. Failure is non-fatal.
+ */
+ if (ret == SR_OK)
+ (void)sigma_store_hw_config(sdi);
+
+ return ret;
+}
+
+/*
+ * Arrange for a session feed submit buffer. A queue where a number of
+ * samples gets accumulated to reduce the number of send calls. Which
+ * also enforces an optional sample count limit for data acquisition.
+ *
+ * The buffer holds up to CHUNK_SIZE bytes. The unit size is fixed (the
+ * driver provides a fixed channel layout regardless of samplerate).
+ */
+
+#define CHUNK_SIZE (4 * 1024 * 1024)
+
+struct submit_buffer {
+ size_t unit_size;
+ size_t max_samples, curr_samples;
+ uint8_t *sample_data;
+ uint8_t *write_pointer;
+ struct sr_dev_inst *sdi;
+ struct sr_datafeed_packet packet;
+ struct sr_datafeed_logic logic;
+};
+
+static int alloc_submit_buffer(struct sr_dev_inst *sdi)
+{
+ struct dev_context *devc;
+ struct submit_buffer *buffer;
+ size_t size;
+
+ devc = sdi->priv;
+
+ buffer = g_malloc0(sizeof(*buffer));
+ devc->buffer = buffer;
+
+ buffer->unit_size = sizeof(uint16_t);
+ size = CHUNK_SIZE;
+ size /= buffer->unit_size;
+ buffer->max_samples = size;
+ size *= buffer->unit_size;
+ buffer->sample_data = g_try_malloc0(size);
+ if (!buffer->sample_data)
+ return SR_ERR_MALLOC;
+ buffer->write_pointer = buffer->sample_data;
+ sr_sw_limits_init(&devc->limit.submit);
+
+ buffer->sdi = sdi;
+ memset(&buffer->logic, 0, sizeof(buffer->logic));
+ buffer->logic.unitsize = buffer->unit_size;
+ buffer->logic.data = buffer->sample_data;
+ memset(&buffer->packet, 0, sizeof(buffer->packet));
+ buffer->packet.type = SR_DF_LOGIC;
+ buffer->packet.payload = &buffer->logic;
+
+ return SR_OK;
+}
+
+static int setup_submit_limit(struct dev_context *devc)
+{
+ struct sr_sw_limits *limits;
+ int ret;
+ GVariant *data;
+ uint64_t total;
+
+ limits = &devc->limit.submit;
+
+ ret = sr_sw_limits_config_get(&devc->limit.config,
+ SR_CONF_LIMIT_SAMPLES, &data);
+ if (ret != SR_OK)
+ return ret;
+ total = g_variant_get_uint64(data);
+ g_variant_unref(data);
+
+ sr_sw_limits_init(limits);
+ if (total) {
+ data = g_variant_new_uint64(total);
+ ret = sr_sw_limits_config_set(limits,
+ SR_CONF_LIMIT_SAMPLES, data);
+ g_variant_unref(data);
+ if (ret != SR_OK)
+ return ret;
+ }
+
+ sr_sw_limits_acquisition_start(limits);
+
+ return SR_OK;
+}
+
+static void free_submit_buffer(struct dev_context *devc)
+{
+ struct submit_buffer *buffer;
+
+ if (!devc)
+ return;
+
+ buffer = devc->buffer;
+ if (!buffer)
+ return;
+ devc->buffer = NULL;
+
+ g_free(buffer->sample_data);
+ g_free(buffer);
+}
+
+static int flush_submit_buffer(struct dev_context *devc)
+{
+ struct submit_buffer *buffer;
+ int ret;
+
+ buffer = devc->buffer;
+
+ /* Is queued sample data available? */
+ if (!buffer->curr_samples)
+ return SR_OK;
+
+ /* Submit to the session feed. */
+ buffer->logic.length = buffer->curr_samples * buffer->unit_size;
+ ret = sr_session_send(buffer->sdi, &buffer->packet);
+ if (ret != SR_OK)
+ return ret;
+
+ /* Rewind queue position. */
+ buffer->curr_samples = 0;
+ buffer->write_pointer = buffer->sample_data;
+
+ return SR_OK;
+}
+
+static int addto_submit_buffer(struct dev_context *devc,
+ uint16_t sample, size_t count)
+{
+ struct submit_buffer *buffer;
+ struct sr_sw_limits *limits;
+ int ret;
+
+ buffer = devc->buffer;
+ limits = &devc->limit.submit;
+ if (!devc->use_triggers && sr_sw_limits_check(limits))
+ count = 0;
+
+ /*
+ * Individually accumulate and check each sample, such that
+ * accumulation between flushes won't exceed local storage, and
+ * enforcement of user specified limits is exact.
+ */
+ while (count--) {
+ write_u16le_inc(&buffer->write_pointer, sample);
+ buffer->curr_samples++;
+ if (buffer->curr_samples == buffer->max_samples) {
+ ret = flush_submit_buffer(devc);
+ if (ret != SR_OK)
+ return ret;
+ }
+ sr_sw_limits_update_samples_read(limits, 1);
+ if (!devc->use_triggers && sr_sw_limits_check(limits))
+ break;
+ }