X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fhardware%2Fmaynuo-m97%2Fapi.c;h=23b0e68bc14b63788c1cb581e261849717a373a0;hb=f1ba6b4b2c9a8ecf90bb31efb218752aa7e49d1a;hp=f1afdfe6cb886158bb92a8d0b7b58f52c0ff185f;hpb=ffb580cf732f060f167cbb3b910299395dd67aaa;p=libsigrok.git diff --git a/src/hardware/maynuo-m97/api.c b/src/hardware/maynuo-m97/api.c index f1afdfe6..23b0e68b 100644 --- a/src/hardware/maynuo-m97/api.c +++ b/src/hardware/maynuo-m97/api.c @@ -17,6 +17,7 @@ * along with this program. If not, see . */ +#include #include "protocol.h" static const uint32_t scanopts[] = { @@ -30,7 +31,7 @@ static const uint32_t drvopts[] = { }; static const uint32_t devopts[] = { - SR_CONF_CONTINUOUS | SR_CONF_SET, + SR_CONF_CONTINUOUS, SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET, SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET, }; @@ -52,9 +53,11 @@ static const uint32_t devopts_cg[] = { SR_CONF_OVER_TEMPERATURE_PROTECTION_ACTIVE | SR_CONF_GET, }; -/* The IDs in this list are only guessed and needs to be verified - against some real hardware. If at least a few of them matches, - it will probably be safe to enable the others. */ +/* + * The IDs in this list are only guessed and needs to be verified + * against some real hardware. If at least a few of them matches, + * it will probably be safe to enable the others. + */ static const struct maynuo_m97_model supported_models[] = { // { 53, "M9711" , 30, 150, 150 }, // { 54, "M9712" , 30, 150, 300 }, @@ -108,12 +111,7 @@ static const struct maynuo_m97_model supported_models[] = { // { 102, "M9812B" , 15, 500, 300 }, }; -SR_PRIV struct sr_dev_driver maynuo_m97_driver_info; - -static int init(struct sr_dev_driver *di, struct sr_context *sr_ctx) -{ - return std_init(sr_ctx, di, LOG_PREFIX); -} +static struct sr_dev_driver maynuo_m97_driver_info; static struct sr_dev_inst *probe_device(struct sr_modbus_dev_inst *modbus) { @@ -128,18 +126,18 @@ static struct sr_dev_inst *probe_device(struct sr_modbus_dev_inst *modbus) int ret = maynuo_m97_get_model_version(modbus, &id, &version); if (ret != SR_OK) return NULL; - for (i=0; istatus = SR_ST_ACTIVE; + sdi->status = SR_ST_INACTIVE; sdi->vendor = g_strdup("Maynuo"); sdi->model = g_strdup(model->name); sdi->version = g_strdup_printf("v%d.%d", version/10, version%10); @@ -174,12 +172,12 @@ static int config_compare(gconstpointer a, gconstpointer b) static GSList *scan(struct sr_dev_driver *di, GSList *options) { struct sr_config default_serialcomm = { - .key = SR_CONF_SERIALCOMM, - .data = g_variant_new_string("9600/8n1"), + .key = SR_CONF_SERIALCOMM, + .data = g_variant_new_string("9600/8n1"), }; struct sr_config default_modbusaddr = { - .key = SR_CONF_MODBUSADDR, - .data = g_variant_new_uint64(1), + .key = SR_CONF_MODBUSADDR, + .data = g_variant_new_uint64(1), }; GSList *opts = options, *devices; @@ -188,7 +186,7 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) if (!g_slist_find_custom(options, &default_modbusaddr, config_compare)) opts = g_slist_prepend(opts, &default_modbusaddr); - devices = sr_modbus_scan(di->priv, opts, probe_device); + devices = sr_modbus_scan(di->context, opts, probe_device); while (opts != options) opts = g_slist_delete_link(opts, opts); @@ -198,16 +196,6 @@ static GSList *scan(struct sr_dev_driver *di, GSList *options) return devices; } -static GSList *dev_list(const struct sr_dev_driver *di) -{ - return ((struct drv_context *)(di->priv))->instances; -} - -static int dev_clear(const struct sr_dev_driver *di) -{ - return std_dev_clear(di, g_free); -} - static int dev_open(struct sr_dev_inst *sdi) { struct sr_modbus_dev_inst *modbus = sdi->conn; @@ -215,8 +203,6 @@ static int dev_open(struct sr_dev_inst *sdi) if (sr_modbus_open(modbus) < 0) return SR_ERR; - sdi->status = SR_ST_ACTIVE; - maynuo_m97_set_bit(modbus, PC1, 1); return SR_OK; @@ -227,34 +213,23 @@ static int dev_close(struct sr_dev_inst *sdi) struct dev_context *devc; struct sr_modbus_dev_inst *modbus; - if (sdi->status != SR_ST_ACTIVE) - return SR_ERR_DEV_CLOSED; - modbus = sdi->conn; - if (modbus) { - devc = sdi->priv; - if (devc->expecting_registers) { - /* Wait for the last data that was requested from the device. */ - uint16_t registers[devc->expecting_registers]; - sr_modbus_read_holding_registers(modbus, -1, - devc->expecting_registers, - registers); - } + if (!modbus) + return SR_ERR_BUG; - maynuo_m97_set_bit(modbus, PC1, 0); + devc = sdi->priv; - if (sr_modbus_close(modbus) < 0) - return SR_ERR; - sdi->status = SR_ST_INACTIVE; + if (devc->expecting_registers) { + /* Wait for the last data that was requested from the device. */ + uint16_t registers[devc->expecting_registers]; + sr_modbus_read_holding_registers(modbus, -1, + devc->expecting_registers, registers); } - return SR_OK; -} + maynuo_m97_set_bit(modbus, PC1, 0); -static int cleanup(const struct sr_dev_driver *di) -{ - return dev_clear(di); + return sr_modbus_close(modbus); } static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi, @@ -274,10 +249,8 @@ static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *s ret = SR_OK; switch (key) { case SR_CONF_LIMIT_SAMPLES: - *data = g_variant_new_uint64(devc->limit_samples); - break; case SR_CONF_LIMIT_MSEC: - *data = g_variant_new_uint64(devc->limit_msec); + ret = sr_sw_limits_config_get(&devc->limits, key, data); break; case SR_CONF_ENABLED: if ((ret = maynuo_m97_get_bit(modbus, ISTATE, &ivalue)) == SR_OK) @@ -350,22 +323,16 @@ static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sd struct sr_modbus_dev_inst *modbus; int ret; - (void)data; (void)cg; - if (sdi->status != SR_ST_ACTIVE) - return SR_ERR_DEV_CLOSED; - modbus = sdi->conn; devc = sdi->priv; ret = SR_OK; switch (key) { case SR_CONF_LIMIT_SAMPLES: - devc->limit_samples = g_variant_get_uint64(data); - break; case SR_CONF_LIMIT_MSEC: - devc->limit_msec = g_variant_get_uint64(data); + ret = sr_sw_limits_config_set(&devc->limits, key, data); break; case SR_CONF_ENABLED: ret = maynuo_m97_set_input(modbus, g_variant_get_boolean(data)); @@ -452,18 +419,12 @@ static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst * return ret; } -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; struct sr_modbus_dev_inst *modbus; int ret; - (void)cb_data; - - if (sdi->status != SR_ST_ACTIVE) - return SR_ERR_DEV_CLOSED; - modbus = sdi->conn; devc = sdi->priv; @@ -471,28 +432,17 @@ static int dev_acquisition_start(const struct sr_dev_inst *sdi, maynuo_m97_receive_data, (void *)sdi)) != SR_OK) return ret; - /* Send header packet to the session bus. */ - std_session_send_df_header(sdi, LOG_PREFIX); - - devc->num_samples = 0; - devc->starttime = g_get_monotonic_time(); + sr_sw_limits_acquisition_start(&devc->limits); + std_session_send_df_header(sdi); return maynuo_m97_capture_start(sdi); } -static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) +static int dev_acquisition_stop(struct sr_dev_inst *sdi) { struct sr_modbus_dev_inst *modbus; - struct sr_datafeed_packet packet; - - (void)cb_data; - - if (sdi->status != SR_ST_ACTIVE) - return SR_ERR_DEV_CLOSED; - /* End of last frame. */ - packet.type = SR_DF_END; - sr_session_send(sdi, &packet); + std_session_send_df_end(sdi); modbus = sdi->conn; sr_modbus_source_remove(sdi->session, modbus); @@ -500,15 +450,14 @@ static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data) return SR_OK; } -SR_PRIV struct sr_dev_driver maynuo_m97_driver_info = { +static struct sr_dev_driver maynuo_m97_driver_info = { .name = "maynuo-m97", .longname = "maynuo M97/M98 series", .api_version = 1, - .init = init, - .cleanup = cleanup, + .init = std_init, + .cleanup = std_cleanup, .scan = scan, - .dev_list = dev_list, - .dev_clear = dev_clear, + .dev_list = std_dev_list, .config_get = config_get, .config_set = config_set, .config_list = config_list, @@ -516,5 +465,6 @@ SR_PRIV struct sr_dev_driver maynuo_m97_driver_info = { .dev_close = dev_close, .dev_acquisition_start = dev_acquisition_start, .dev_acquisition_stop = dev_acquisition_stop, - .priv = NULL, + .context = NULL, }; +SR_REGISTER_DEV_DRIVER(maynuo_m97_driver_info);