X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fhardware%2Fzketech-ebd-usb%2Fprotocol.c;h=4c586f6fd71989deec056a650a2abf96fb4e29a2;hb=1e91a90f2ad5f915c2490c1f099f05e15277a365;hp=b494aa14e7cf64d6877e20de2e25c057920284e4;hpb=cb8a0efc5ce1929858104050c06f83335235676f;p=libsigrok.git diff --git a/src/hardware/zketech-ebd-usb/protocol.c b/src/hardware/zketech-ebd-usb/protocol.c index b494aa14..4c586f6f 100644 --- a/src/hardware/zketech-ebd-usb/protocol.c +++ b/src/hardware/zketech-ebd-usb/protocol.c @@ -30,7 +30,7 @@ static void log_buf(const char *message, uint8_t buf[], size_t count) buffer[count * 2] = 0; - sr_dbg("%s: %s [%lu bytes]", message, buffer, count); + sr_dbg("%s: %s [%zu bytes]", message, buffer, count); } /* Send a command to the device. */ @@ -44,7 +44,6 @@ static int send_cmd(struct sr_serial_dev_inst *serial, uint8_t buf[], size_t cou sr_err("Error sending command: %d.", ret); return ret; } - sr_dbg("Sent %d bytes.", ret); return (ret == (int)count) ? SR_OK : SR_ERR; } @@ -80,8 +79,7 @@ static int send_cfg(struct sr_serial_dev_inst *serial, struct dev_context *devc) } /* Send the init/connect sequence; drive starts sending voltage and current. */ -SR_PRIV int zketech_ebd_usb_init(struct sr_serial_dev_inst *serial, - struct dev_context *devc) +SR_PRIV int ebd_init(struct sr_serial_dev_inst *serial, struct dev_context *devc) { uint8_t init[] = { 0xfa, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0xf8 }; @@ -95,15 +93,17 @@ SR_PRIV int zketech_ebd_usb_init(struct sr_serial_dev_inst *serial, } /* Start the load functionality. */ -SR_PRIV int zketech_ebd_usb_loadstart(struct sr_serial_dev_inst *serial, - struct dev_context *devc) +SR_PRIV int ebd_loadstart(struct sr_serial_dev_inst *serial, struct dev_context *devc) { uint8_t start[] = { 0xfa, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xf8 }; int ret; ret = send_cmd(serial, start, 10); + if (ret) + return ret; + sr_dbg("Current limit: %f.", devc->current_limit); - if (zketech_ebd_usb_current_is0(devc)) + if (ebd_current_is0(devc)) return SR_OK; ret = send_cfg(serial, devc); @@ -116,8 +116,7 @@ SR_PRIV int zketech_ebd_usb_loadstart(struct sr_serial_dev_inst *serial, } /* Stop the load functionality. */ -SR_PRIV int zketech_ebd_usb_loadstop(struct sr_serial_dev_inst *serial, - struct dev_context *devc) +SR_PRIV int ebd_loadstop(struct sr_serial_dev_inst *serial, struct dev_context *devc) { int ret; uint8_t stop[] = { 0xfa, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xF8 }; @@ -130,8 +129,7 @@ SR_PRIV int zketech_ebd_usb_loadstop(struct sr_serial_dev_inst *serial, } /* Stop the drive. */ -SR_PRIV int zketech_ebd_usb_stop(struct sr_serial_dev_inst *serial, - struct dev_context *devc) +SR_PRIV int ebd_stop(struct sr_serial_dev_inst *serial, struct dev_context *devc) { uint8_t stop[] = { 0xfa, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0xF8 }; int ret; @@ -148,8 +146,7 @@ SR_PRIV int zketech_ebd_usb_stop(struct sr_serial_dev_inst *serial, } /** Read count bytes from the serial connection. */ -SR_PRIV int zketech_ebd_usb_read_chars(struct sr_serial_dev_inst *serial, - int count, uint8_t *buf) +SR_PRIV int ebd_read_chars(struct sr_serial_dev_inst *serial, int count, uint8_t *buf) { int ret, received, turns; @@ -172,7 +169,7 @@ SR_PRIV int zketech_ebd_usb_read_chars(struct sr_serial_dev_inst *serial, return received; } -SR_PRIV int zketech_ebd_usb_receive_data(int fd, int revents, void *cb_data) +SR_PRIV int ebd_receive_data(int fd, int revents, void *cb_data) { struct sr_dev_inst *sdi; struct dev_context *devc; @@ -197,7 +194,7 @@ SR_PRIV int zketech_ebd_usb_receive_data(int fd, int revents, void *cb_data) serial = sdi->conn; uint8_t reply[MSG_LEN]; - int ret = zketech_ebd_usb_read_chars(serial, MSG_LEN, reply); + int ret = ebd_read_chars(serial, MSG_LEN, reply); /* Tests for correct message. */ if (ret != MSG_LEN) { @@ -230,9 +227,7 @@ SR_PRIV int zketech_ebd_usb_receive_data(int fd, int revents, void *cb_data) sr_dbg("Current limit %f", current_limit); /* Begin frame. */ - packet.type = SR_DF_FRAME_BEGIN; - packet.payload = NULL; - sr_session_send(sdi, &packet); + std_session_send_df_frame_begin(sdi); sr_analog_init(&analog, &encoding, &meaning, &spec, 4); @@ -263,9 +258,7 @@ SR_PRIV int zketech_ebd_usb_receive_data(int fd, int revents, void *cb_data) g_slist_free(l); /* End frame. */ - packet.type = SR_DF_FRAME_END; - packet.payload = NULL; - sr_session_send(sdi, &packet); + std_session_send_df_frame_end(sdi); sr_sw_limits_update_samples_read(&devc->limits, 1); if (sr_sw_limits_check(&devc->limits)) @@ -274,8 +267,7 @@ SR_PRIV int zketech_ebd_usb_receive_data(int fd, int revents, void *cb_data) return TRUE; } -SR_PRIV int zketech_ebd_usb_get_current_limit(const struct sr_dev_inst *sdi, - float *current) +SR_PRIV int ebd_get_current_limit(const struct sr_dev_inst *sdi, float *current) { struct dev_context *devc; @@ -289,8 +281,7 @@ SR_PRIV int zketech_ebd_usb_get_current_limit(const struct sr_dev_inst *sdi, return SR_OK; } -SR_PRIV int zketech_ebd_usb_set_current_limit(const struct sr_dev_inst *sdi, - float current) +SR_PRIV int ebd_set_current_limit(const struct sr_dev_inst *sdi, float current) { struct dev_context *devc; int ret; @@ -310,20 +301,20 @@ SR_PRIV int zketech_ebd_usb_set_current_limit(const struct sr_dev_inst *sdi, sr_dbg("Setting current limit to %fV.", current); if (devc->load_activated) { - if (zketech_ebd_usb_current_is0(devc)) { + if (ebd_current_is0(devc)) { /* Stop load. */ - ret = zketech_ebd_usb_loadstop(sdi->conn, devc); + ret = ebd_loadstop(sdi->conn, devc); } else { /* Send new current. */ ret = send_cfg(sdi->conn, devc); } } else { - if (zketech_ebd_usb_current_is0(devc)) { + if (ebd_current_is0(devc)) { /* Nothing to do. */ ret = SR_OK; } else { /* Start load. */ - ret = zketech_ebd_usb_loadstart(sdi->conn, devc); + ret = ebd_loadstart(sdi->conn, devc); } } @@ -332,7 +323,7 @@ SR_PRIV int zketech_ebd_usb_set_current_limit(const struct sr_dev_inst *sdi, return ret; } -SR_PRIV gboolean zketech_ebd_usb_current_is0(struct dev_context *devc) +SR_PRIV gboolean ebd_current_is0(struct dev_context *devc) { return devc->current_limit < 0.001; }