X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fhardware%2Fpipistrello-ols%2Fapi.c;h=8aca1ed12f844c30cec2aadabbe26013dc116712;hb=a162eeb2e8f23da99d1a7ca7d62dc6ad0981afc6;hp=3c7fc7b1790c0af6e1c37f33d929bb85523631f7;hpb=41812aca436805b0614f2a8f31cf2f8ce494aea0;p=libsigrok.git diff --git a/src/hardware/pipistrello-ols/api.c b/src/hardware/pipistrello-ols/api.c index 3c7fc7b1..8aca1ed1 100644 --- a/src/hardware/pipistrello-ols/api.c +++ b/src/hardware/pipistrello-ols/api.c @@ -17,10 +17,14 @@ * along with this program. If not, see . */ +#include #include "protocol.h" -static const uint32_t devopts[] = { +static const uint32_t drvopts[] = { SR_CONF_LOGIC_ANALYZER, +}; + +static const uint32_t devopts[] = { SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, SR_CONF_SAMPLERATE | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST, SR_CONF_TRIGGER_MATCH | SR_CONF_LIST, @@ -74,17 +78,9 @@ static const uint64_t samplerates[] = { SR_HZ(1), }; -SR_PRIV struct sr_dev_driver p_ols_driver_info; - -static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx) -{ - return std_init(sr_ctx, di, LOG_PREFIX); -} - static GSList *scan(struct sr_dev_driver *di, GSList *options) { struct sr_dev_inst *sdi; - struct drv_context *drvc; struct dev_context *devc; GSList *devices; int ret, i; @@ -93,36 +89,27 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) (void)options; - drvc = di->context; - devices = NULL; - /* Allocate memory for our private device context. */ devc = g_malloc0(sizeof(struct dev_context)); - /* Device-specific settings */ devc->max_samplebytes = devc->max_samplerate = devc->protocol_version = 0; - /* Acquisition settings */ devc->limit_samples = devc->capture_ratio = 0; devc->trigger_at = -1; devc->channel_mask = 0xffffffff; devc->flag_reg = 0; - /* Allocate memory for the incoming ftdi data. */ devc->ftdi_buf = g_malloc0(FTDI_BUF_SIZE); - /* Allocate memory for the FTDI context (ftdic) and initialize it. */ if (!(devc->ftdic = ftdi_new())) { sr_err("Failed to initialize libftdi."); goto err_free_ftdi_buf;; } - /* Try to open the FTDI device */ - if (p_ols_open(devc) != SR_OK) { + if (p_ols_open(devc) != SR_OK) goto err_free_ftdic; - } - + /* The discovery procedure is like this: first send the Reset * command (0x00) 5 times, since the device could be anywhere * in a 5-byte command. Then send the ID command (0x02). @@ -172,7 +159,6 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) goto err_close_ftdic; } - /* Close device. We'll reopen it again when we need it. */ p_ols_close(devc); /* Parse the metadata. */ @@ -183,15 +169,14 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) sr_dbg("Failed to set default samplerate (%"PRIu64").", DEFAULT_SAMPLERATE); - drvc->instances = g_slist_append(drvc->instances, sdi); devices = g_slist_append(devices, sdi); - return devices; + return std_scan_complete(di, devices); err_close_ftdic: p_ols_close(devc); err_free_ftdic: - ftdi_free(devc->ftdic); /* NOT free() or g_free()! */ + ftdi_free(devc->ftdic); err_free_ftdi_buf: g_free(devc->ftdi_buf); g_free(devc); @@ -199,33 +184,19 @@ err_free_ftdi_buf: return NULL; } -static GSList *dev_list(const struct sr_dev_driver *di) -{ - return ((struct drv_context *)(di->context))->instances; -} - -static void clear_helper(void *priv) +static void clear_helper(struct dev_context *devc) { - struct dev_context *devc; - - devc = priv; - ftdi_free(devc->ftdic); g_free(devc->ftdi_buf); } static int dev_clear(const struct sr_dev_driver *di) { - return std_dev_clear(di, clear_helper); + return std_dev_clear_with_callback(di, (std_dev_clear_callback)clear_helper); } -static int cleanup(const struct sr_dev_driver *di) -{ - return dev_clear(di); -} - -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; @@ -266,8 +237,8 @@ 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; uint16_t flag; @@ -277,9 +248,6 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd (void)cg; - if (sdi->status != SR_ST_ACTIVE) - return SR_ERR_DEV_CLOSED; - devc = sdi->priv; switch (key) { @@ -298,10 +266,9 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd break; case SR_CONF_CAPTURE_RATIO: devc->capture_ratio = g_variant_get_uint64(data); - if (devc->capture_ratio < 0 || devc->capture_ratio > 100) { - devc->capture_ratio = 0; + if (devc->capture_ratio < 0 || devc->capture_ratio > 100) ret = SR_ERR; - } else + else ret = SR_OK; break; case SR_CONF_EXTERNAL_CLOCK: @@ -363,35 +330,23 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd return ret; } -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 dev_context *devc; - GVariant *gvar, *grange[2]; - GVariantBuilder gvb; int num_pols_changrp, i; - (void)cg; - switch (key) { case SR_CONF_DEVICE_OPTIONS: - *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32, - devopts, ARRAY_SIZE(devopts), sizeof(uint32_t)); - break; + return STD_CONFIG_LIST(key, data, sdi, cg, NULL, drvopts, devopts); case SR_CONF_SAMPLERATE: - g_variant_builder_init(&gvb, G_VARIANT_TYPE("a{sv}")); - gvar = g_variant_new_fixed_array(G_VARIANT_TYPE("t"), samplerates, - ARRAY_SIZE(samplerates), sizeof(uint64_t)); - g_variant_builder_add(&gvb, "{sv}", "samplerate-steps", gvar); - *data = g_variant_builder_end(&gvb); + *data = std_gvar_samplerates_steps(ARRAY_AND_SIZE(samplerates)); break; case SR_CONF_TRIGGER_MATCH: - *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32, - trigger_matches, ARRAY_SIZE(trigger_matches), - sizeof(int32_t)); + *data = std_gvar_array_i32(ARRAY_AND_SIZE(trigger_matches)); break; case SR_CONF_PATTERN_MODE: - *data = g_variant_new_strv(patterns, ARRAY_SIZE(patterns)); + *data = g_variant_new_strv(ARRAY_AND_SIZE(patterns)); break; case SR_CONF_LIMIT_SAMPLES: if (!sdi) @@ -415,12 +370,9 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst * /* 3 channel groups takes as many bytes as 4 channel groups */ if (num_pols_changrp == 3) num_pols_changrp = 4; - grange[0] = g_variant_new_uint64(MIN_NUM_SAMPLES); - if (num_pols_changrp) - grange[1] = g_variant_new_uint64(devc->max_samplebytes / num_pols_changrp); - else - grange[1] = g_variant_new_uint64(MIN_NUM_SAMPLES); - *data = g_variant_new_tuple(grange, 2); + + *data = std_gvar_tuple_u64(MIN_NUM_SAMPLES, + (num_pols_changrp) ? devc->max_samplebytes / num_pols_changrp : MIN_NUM_SAMPLES); break; default: return SR_ERR_NA; @@ -435,32 +387,16 @@ static int dev_open(struct sr_dev_inst *sdi) devc = sdi->priv; - if (p_ols_open(devc) != SR_OK) { - return SR_ERR; - } else { - sdi->status = SR_ST_ACTIVE; - return SR_OK; - } + return p_ols_open(devc); } static int dev_close(struct sr_dev_inst *sdi) { - int ret; struct dev_context *devc; - ret = SR_OK; devc = sdi->priv; - if (sdi->status == SR_ST_ACTIVE) { - sr_dbg("Status ACTIVE, closing device."); - ret = p_ols_close(devc); - } else { - sr_spew("Status not ACTIVE, nothing to do."); - } - - sdi->status = SR_ST_INACTIVE; - - return ret; + return p_ols_close(devc); } static int set_trigger(const struct sr_dev_inst *sdi, int stage) @@ -535,8 +471,7 @@ static int disable_trigger(const struct sr_dev_inst *sdi, int stage) return SR_OK; } -static int dev_acquisition_start(const struct sr_dev_inst *sdi, - void *cb_data) +static int dev_acquisition_start(const struct sr_dev_inst *sdi) { struct dev_context *devc; uint32_t samplecount, readcount, delaycount; @@ -545,9 +480,6 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, int num_pols_changrp, samplespercount; int ret, i; - if (sdi->status != SR_ST_ACTIVE) - return SR_ERR_DEV_CLOSED; - devc = sdi->priv; pols_channel_mask(sdi); @@ -576,7 +508,7 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, * buffer. */ sr_dbg("max_samples = %d", devc->max_samples); - sr_dbg("limit_samples = %d", devc->limit_samples); + sr_dbg("limit_samples = %" PRIu64, devc->limit_samples); samplecount = MIN(devc->max_samples, devc->limit_samples); sr_dbg("Samplecount = %d", samplecount); @@ -664,8 +596,8 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, sr_dbg("flag_reg = %x", devc->flag_reg); /* - * In demux mode the OLS is processing two 8-bit or 16-bit samples - * in parallel and for this to work the lower two bits of the four + * In demux mode the OLS is processing two 8-bit or 16-bit samples + * in parallel and for this to work the lower two bits of the four * "channel_disable" bits must be replicated to the upper two bits. */ flag_tmp = devc->flag_reg; @@ -689,36 +621,30 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, devc->cnt_bytes = devc->cnt_samples = devc->cnt_samples_rle = 0; memset(devc->sample, 0, 4); - /* Send header packet to the session bus. */ - std_session_send_df_header(cb_data, LOG_PREFIX); + std_session_send_df_header(sdi); /* Hook up a dummy handler to receive data from the device. */ - sr_session_source_add(sdi->session, 0, G_IO_IN, 10, p_ols_receive_data, - cb_data); + sr_session_source_add(sdi->session, -1, 0, 10, p_ols_receive_data, + (struct sr_dev_inst *)sdi); return SR_OK; } -static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) +static int dev_acquisition_stop(struct sr_dev_inst *sdi) { struct dev_context *devc; - struct sr_datafeed_packet packet; devc = sdi->priv; - sr_dbg("Stopping acquisition."); write_shortcommand(devc, CMD_RESET); write_shortcommand(devc, CMD_RESET); write_shortcommand(devc, CMD_RESET); write_shortcommand(devc, CMD_RESET); write_shortcommand(devc, CMD_RESET); - sr_session_source_remove(sdi->session, 0); + sr_session_source_remove(sdi->session, -1); - /* Send end packet to the session bus. */ - sr_dbg("Sending SR_DF_END."); - packet.type = SR_DF_END; - sr_session_send(cb_data, &packet); + std_session_send_df_end(sdi); return SR_OK; } @@ -727,10 +653,10 @@ SR_PRIV struct sr_dev_driver p_ols_driver_info = { .name = "p-ols", .longname = "Pipistrello OLS", .api_version = 1, - .init = init, - .cleanup = cleanup, + .init = std_init, + .cleanup = std_cleanup, .scan = scan, - .dev_list = dev_list, + .dev_list = std_dev_list, .dev_clear = dev_clear, .config_get = config_get, .config_set = config_set, @@ -741,3 +667,4 @@ SR_PRIV struct sr_dev_driver p_ols_driver_info = { .dev_acquisition_stop = dev_acquisition_stop, .context = NULL, }; +SR_REGISTER_DEV_DRIVER(p_ols_driver_info);