+/* Send a command to the device. */
+SR_PRIV int zketech_ebd_usb_send(struct sr_serial_dev_inst *serial,
+ uint8_t buf[], size_t count)
+{
+ int ret;
+
+ zketech_ebd_usb_buffer_debug("Sending", buf, count);
+ ret = serial_write_blocking(serial, buf, count, 0);
+ if (ret < 0) {
+ sr_err("Error sending command: %d.", ret);
+ return ret;
+ }
+ sr_dbg("Sent %d bytes.", ret);
+
+ return (ret == (int)count) ? SR_OK : SR_ERR;
+}
+
+/* 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)
+{
+ uint8_t init[] = { 0xfa, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05, 0xf8 };
+
+ (void)devc;
+
+ int ret = zketech_ebd_usb_send(serial, init, 10);
+ if (ret == SR_OK)
+ devc->running = TRUE;
+
+ return ret;
+}
+
+/* Start the load functionality. */
+SR_PRIV int zketech_ebd_usb_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 = zketech_ebd_usb_send(serial, start, 10);
+ sr_dbg("Current limit: %f.", devc->current_limit);
+ if (zketech_ebd_usb_current_is0(devc))
+ return SR_OK;
+
+ ret = zketech_ebd_usb_sendcfg(serial, devc);
+ if (ret == SR_OK) {
+ sr_dbg("Load activated.");
+ devc->load_activated = TRUE;
+ }
+
+ return ret;
+}
+
+/* Stop the load functionality. */
+SR_PRIV int zketech_ebd_usb_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 };
+
+ ret = zketech_ebd_usb_send(serial, stop, 10);
+ if (ret == SR_OK)
+ devc->load_activated = FALSE;
+
+ return ret;
+}
+
+/* Stop the drive. */
+SR_PRIV int zketech_ebd_usb_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;
+
+ (void) devc;
+
+ ret = zketech_ebd_usb_send(serial, stop, 10);
+ if (ret == SR_OK) {
+ devc->load_activated = FALSE;
+ devc->running= FALSE;
+ }
+
+ return ret;
+}
+
+/* Send updated configuration values to the load. */
+SR_PRIV int zketech_ebd_usb_sendcfg(struct sr_serial_dev_inst *serial,
+ struct dev_context *devc)
+{
+ uint8_t send[] = { 0xfa, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf8 };
+
+ zketech_ebd_usb_value_encode(devc->current_limit, &send[2],
+ &send[3], 1000.0);
+
+ send[8] = send[1] ^ send[2] ^ send[3] ^ send[4] ^ send[5] ^ \
+ send[6] ^ send[7];
+
+ return zketech_ebd_usb_send(serial, send, 10);
+}
+
+/** 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)
+{
+ int ret, received, turns;
+
+ received = 0;
+ turns = 0;
+
+ do {
+ ret = serial_read_blocking(serial, buf + received,
+ count - received, serial_timeout(serial, count));
+ if (ret < 0) {
+ sr_err("Error %d reading %d bytes.", ret, count);
+ return ret;
+ }
+ received += ret;
+ turns++;
+ } while ((received < count) && (turns < 100));
+
+ zketech_ebd_usb_buffer_debug("Received", buf, received);
+
+ return received;
+}
+
+/* Decode high byte and low byte into a float. */
+SR_PRIV float zketech_ebd_usb_value_decode(uint8_t hi, uint8_t lo, float divisor)
+{
+ return ((float)hi * 240.0 + (float)lo) / divisor;
+}
+
+/* Encode a float into high byte and low byte. */
+SR_PRIV void zketech_ebd_usb_value_encode(float current, uint8_t *hi,
+ uint8_t *lo, float divisor)
+{
+ int value;
+
+ value = (int)(current * divisor);
+ sr_dbg("Value %d %d %d", value, value / 240, value % 240);
+ *hi = value / 240;
+ *lo = value % 240;
+}
+