X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fhardware%2Fdemo%2Fapi.c;h=7d3e08872d07c21b38832845a138d5cf2bd3fe80;hb=f6ce25ec05e8707ee3783b111ea13779f237c3b3;hp=a2d397365d02c981ad357210af4d69ff15ded0cc;hpb=105df67463b84683a35f3474eccc871e5aa4ed0e;p=libsigrok.git diff --git a/src/hardware/demo/api.c b/src/hardware/demo/api.c index a2d39736..7d3e0887 100644 --- a/src/hardware/demo/api.c +++ b/src/hardware/demo/api.c @@ -44,11 +44,13 @@ static const char *logic_pattern_str[] = { "all-low", "all-high", "squid", + "graycode", }; static const uint32_t scanopts[] = { SR_CONF_NUM_LOGIC_CHANNELS, SR_CONF_NUM_ANALOG_CHANNELS, + SR_CONF_LIMIT_FRAMES, }; static const uint32_t drvopts[] = { @@ -61,9 +63,12 @@ static const uint32_t devopts[] = { SR_CONF_CONTINUOUS, SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET, + SR_CONF_LIMIT_FRAMES | SR_CONF_GET | SR_CONF_SET, SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, SR_CONF_AVERAGING | SR_CONF_GET | SR_CONF_SET, SR_CONF_AVG_SAMPLES | SR_CONF_GET | SR_CONF_SET, + SR_CONF_TRIGGER_MATCH | SR_CONF_LIST, + SR_CONF_CAPTURE_RATIO | SR_CONF_GET | SR_CONF_SET, }; static const uint32_t devopts_cg_logic[] = { @@ -79,6 +84,14 @@ static const uint32_t devopts_cg_analog_channel[] = { SR_CONF_AMPLITUDE | SR_CONF_GET | SR_CONF_SET, }; +static const int32_t trigger_matches[] = { + SR_TRIGGER_ZERO, + SR_TRIGGER_ONE, + SR_TRIGGER_RISING, + SR_TRIGGER_FALLING, + SR_TRIGGER_EDGE, +}; + static const uint64_t samplerates[] = { SR_HZ(1), SR_GHZ(1), @@ -95,10 +108,12 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) struct analog_gen *ag; GSList *l; int num_logic_channels, num_analog_channels, pattern, i; + uint64_t limit_frames; char channel_name[16]; num_logic_channels = DEFAULT_NUM_LOGIC_CHANNELS; num_analog_channels = DEFAULT_NUM_ANALOG_CHANNELS; + limit_frames = DEFAULT_LIMIT_FRAMES; for (l = options; l; l = l->next) { src = l->data; switch (src->key) { @@ -108,6 +123,9 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) case SR_CONF_NUM_ANALOG_CHANNELS: num_analog_channels = g_variant_get_int32(src->data); break; + case SR_CONF_LIMIT_FRAMES: + limit_frames = g_variant_get_uint64(src->data); + break; } } @@ -119,8 +137,14 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) devc->cur_samplerate = SR_KHZ(200); devc->num_logic_channels = num_logic_channels; devc->logic_unitsize = (devc->num_logic_channels + 7) / 8; + devc->all_logic_channels_mask = 1UL << 0; + devc->all_logic_channels_mask <<= devc->num_logic_channels; + devc->all_logic_channels_mask--; devc->logic_pattern = DEFAULT_LOGIC_PATTERN; devc->num_analog_channels = num_analog_channels; + devc->limit_frames = limit_frames; + devc->capture_ratio = 20; + devc->stl = NULL; if (num_logic_channels > 0) { /* Logic channels, all in one channel group. */ @@ -197,8 +221,8 @@ static int dev_clear(const struct sr_dev_driver *di) return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper); } -static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, - const struct sr_channel_group *cg) +static int config_get(uint32_t key, GVariant **data, + const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { struct dev_context *devc; struct sr_channel *ch; @@ -219,6 +243,9 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s case SR_CONF_LIMIT_MSEC: *data = g_variant_new_uint64(devc->limit_msec); break; + case SR_CONF_LIMIT_FRAMES: + *data = g_variant_new_uint64(devc->limit_frames); + break; case SR_CONF_AVERAGING: *data = g_variant_new_boolean(devc->avg); break; @@ -250,6 +277,9 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s ag = g_hash_table_lookup(devc->ch_ag, ch); *data = g_variant_new_double(ag->amplitude); break; + case SR_CONF_CAPTURE_RATIO: + *data = g_variant_new_uint64(devc->capture_ratio); + break; default: return SR_ERR_NA; } @@ -257,16 +287,14 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s return SR_OK; } -static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi, - const struct sr_channel_group *cg) +static int config_set(uint32_t key, GVariant *data, + const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { struct dev_context *devc; struct analog_gen *ag; struct sr_channel *ch; GSList *l; int logic_pattern, analog_pattern; - unsigned int i; - const char *stropt; devc = sdi->priv; @@ -282,6 +310,9 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd devc->limit_msec = g_variant_get_uint64(data); devc->limit_samples = 0; break; + case SR_CONF_LIMIT_FRAMES: + devc->limit_frames = g_variant_get_uint64(data); + break; case SR_CONF_AVERAGING: devc->avg = g_variant_get_boolean(data); sr_dbg("%s averaging", devc->avg ? "Enabling" : "Disabling"); @@ -293,21 +324,9 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd case SR_CONF_PATTERN_MODE: if (!cg) return SR_ERR_CHANNEL_GROUP; - stropt = g_variant_get_string(data, NULL); - logic_pattern = analog_pattern = -1; - for (i = 0; i < ARRAY_SIZE(logic_pattern_str); i++) { - if (!strcmp(stropt, logic_pattern_str[i])) { - logic_pattern = i; - break; - } - } - for (i = 0; i < ARRAY_SIZE(analog_pattern_str); i++) { - if (!strcmp(stropt, analog_pattern_str[i])) { - analog_pattern = i; - break; - } - } - if (logic_pattern == -1 && analog_pattern == -1) + logic_pattern = std_str_idx(data, ARRAY_AND_SIZE(logic_pattern_str)); + analog_pattern = std_str_idx(data, ARRAY_AND_SIZE(analog_pattern_str)); + if (logic_pattern < 0 && analog_pattern < 0) return SR_ERR_ARG; for (l = cg->channels; l; l = l->next) { ch = l->data; @@ -344,6 +363,9 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd ag->amplitude = g_variant_get_double(data); } break; + case SR_CONF_CAPTURE_RATIO: + devc->capture_ratio = g_variant_get_uint64(data); + break; default: return SR_ERR_NA; } @@ -351,8 +373,8 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd return SR_OK; } -static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, - const struct sr_channel_group *cg) +static int config_list(uint32_t key, GVariant **data, + const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { struct sr_channel *ch; @@ -362,7 +384,10 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst * case SR_CONF_DEVICE_OPTIONS: return STD_CONFIG_LIST(key, data, sdi, cg, scanopts, drvopts, devopts); case SR_CONF_SAMPLERATE: - *data = std_gvar_samplerates_steps(samplerates, ARRAY_SIZE(samplerates)); + *data = std_gvar_samplerates_steps(ARRAY_AND_SIZE(samplerates)); + break; + case SR_CONF_TRIGGER_MATCH: + *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches)); break; default: return SR_ERR_NA; @@ -372,12 +397,12 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst * switch (key) { case SR_CONF_DEVICE_OPTIONS: if (ch->type == SR_CHANNEL_LOGIC) - *data = std_gvar_array_u32(devopts_cg_logic, ARRAY_SIZE(devopts_cg_logic)); + *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_logic)); else if (ch->type == SR_CHANNEL_ANALOG) { if (strcmp(cg->name, "Analog") == 0) - *data = std_gvar_array_u32(devopts_cg_analog_group, ARRAY_SIZE(devopts_cg_analog_group)); + *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog_group)); else - *data = std_gvar_array_u32(devopts_cg_analog_channel, ARRAY_SIZE(devopts_cg_analog_channel)); + *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg_analog_channel)); } else return SR_ERR_BUG; @@ -388,11 +413,9 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst * return SR_ERR_NA; if (ch->type == SR_CHANNEL_LOGIC) - *data = g_variant_new_strv(logic_pattern_str, - ARRAY_SIZE(logic_pattern_str)); + *data = g_variant_new_strv(ARRAY_AND_SIZE(logic_pattern_str)); else if (ch->type == SR_CHANNEL_ANALOG) - *data = g_variant_new_strv(analog_pattern_str, - ARRAY_SIZE(analog_pattern_str)); + *data = g_variant_new_strv(ARRAY_AND_SIZE(analog_pattern_str)); else return SR_ERR_BUG; break; @@ -413,9 +436,32 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi) uint8_t mask; GHashTableIter iter; void *value; + struct sr_trigger *trigger; devc = sdi->priv; devc->sent_samples = 0; + devc->sent_frame_samples = 0; + + /* Setup triggers */ + if ((trigger = sr_session_trigger_get(sdi->session))) { + int pre_trigger_samples = 0; + if (devc->limit_samples > 0) + pre_trigger_samples = (devc->capture_ratio * devc->limit_samples) / 100; + devc->stl = soft_trigger_logic_new(sdi, trigger, pre_trigger_samples); + if (!devc->stl) + return SR_ERR_MALLOC; + + /* Disable all analog channels since using them when there are logic + * triggers set up would require having pre-trigger sample buffers + * for analog sample data. + */ + for (l = sdi->channels; l; l = l->next) { + ch = l->data; + if (ch->type == SR_CHANNEL_ANALOG) + ch->enabled = FALSE; + } + } + devc->trigger_fired = FALSE; /* * Determine the numbers of logic and analog channels that are @@ -469,6 +515,9 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi) std_session_send_df_header(sdi); + if (devc->limit_frames > 0) + std_session_send_frame_begin(sdi); + /* We use this timestamp to decide how many more samples to send. */ devc->start_us = g_get_monotonic_time(); devc->spent_us = 0; @@ -479,9 +528,21 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi) static int dev_acquisition_stop(struct sr_dev_inst *sdi) { + struct dev_context *devc; + sr_session_source_remove(sdi->session, -1); + + devc = sdi->priv; + if (devc->limit_frames > 0) + std_session_send_frame_end(sdi); + std_session_send_df_end(sdi); + if (devc->stl) { + soft_trigger_logic_free(devc->stl); + devc->stl = NULL; + } + return SR_OK; }