2 * This file is part of the libsigrok project.
4 * Copyright (C) 2016 Andreas Zschunke <andreas.zschunke@gmx.net>
5 * Copyright (C) 2017 Andrej Valek <andy@skyrain.eu>
6 * Copyright (C) 2017 Uwe Hermann <uwe@hermann-uwe.de>
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #define H4032L_USB_TIMEOUT 500
28 CMD_RESET = 0x00b3, /* Also arms the logic analyzer. */
29 CMD_CONFIGURE = 0x2b1a,
34 struct h4032l_status_packet {
40 SR_PRIV int h4032l_receive_data(int fd, int revents, void *cb_data)
43 struct drv_context *drvc;
48 drvc = (struct drv_context *)cb_data;
50 tv.tv_sec = tv.tv_usec = 0;
51 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
56 void LIBUSB_CALL h4032l_usb_callback(struct libusb_transfer *transfer)
58 const struct sr_dev_inst *sdi = transfer->user_data;
59 struct dev_context *devc = sdi->priv;
60 struct drv_context *drvc = sdi->driver->context;
61 struct sr_usb_dev_inst *usb = sdi->conn;
63 uint32_t max_samples = 512 / sizeof(uint32_t);
65 struct h4032l_status_packet *status;
66 struct sr_datafeed_packet packet;
67 struct sr_datafeed_logic logic;
68 uint32_t number_samples;
71 if (transfer->status != LIBUSB_TRANSFER_COMPLETED)
72 sr_dbg("%s error: %d.", __func__, transfer->status);
74 buffer = (uint32_t *)transfer->buffer;
76 switch (devc->status) {
77 case H4032L_STATUS_IDLE:
78 sr_err("USB callback called in idle.");
80 case H4032L_STATUS_CMD_CONFIGURE:
81 /* Select status request as next. */
83 devc->cmd_pkt.cmd = CMD_STATUS;
84 devc->status = H4032L_STATUS_CMD_STATUS;
86 case H4032L_STATUS_CMD_STATUS:
87 /* Select status request as next. */
88 devc->status = H4032L_STATUS_RESPONSE_STATUS;
90 case H4032L_STATUS_RESPONSE_STATUS:
92 * Check magic and if status is complete, then select
93 * First Transfer as next.
95 status = (struct h4032l_status_packet *)transfer->buffer;
96 if (status->magic != H4032L_STATUS_PACKET_MAGIC) {
97 devc->status = H4032L_STATUS_RESPONSE_STATUS;
98 } else if (status->status == 2) {
99 devc->status = H4032L_STATUS_RESPONSE_STATUS_CONTINUE;
101 devc->status = H4032L_STATUS_RESPONSE_STATUS_RETRY;
104 case H4032L_STATUS_RESPONSE_STATUS_RETRY:
105 devc->status = H4032L_STATUS_CMD_STATUS;
106 devc->cmd_pkt.cmd = CMD_STATUS;
109 case H4032L_STATUS_RESPONSE_STATUS_CONTINUE:
110 devc->status = H4032L_STATUS_CMD_GET;
111 devc->cmd_pkt.cmd = CMD_GET;
114 case H4032L_STATUS_CMD_GET:
115 devc->status = H4032L_STATUS_FIRST_TRANSFER;
117 case H4032L_STATUS_FIRST_TRANSFER:
118 /* Drop packets until H4032L_START_PACKET_MAGIC. */
119 if (buffer[0] != H4032L_START_PACKET_MAGIC) {
120 sr_dbg("Mismatch magic number of start poll.");
123 devc->status = H4032L_STATUS_TRANSFER;
127 case H4032L_STATUS_TRANSFER:
128 number_samples = (devc->remaining_samples < max_samples) ? devc->remaining_samples : max_samples;
129 devc->remaining_samples -= number_samples;
130 packet.type = SR_DF_LOGIC;
131 packet.payload = &logic;
132 logic.length = number_samples * sizeof(uint32_t);
133 logic.unitsize = sizeof(uint32_t);
135 sr_session_send(sdi, &packet);
136 sr_dbg("Remaining: %d %08X %08X.", devc->remaining_samples,
137 buffer[0], buffer[1]);
138 if (devc->remaining_samples == 0) {
139 std_session_send_df_end(sdi);
140 usb_source_remove(sdi->session, drvc->sr_ctx);
141 devc->status = H4032L_STATUS_IDLE;
142 if (buffer[number_samples] != H4032L_END_PACKET_MAGIC)
143 sr_err("Mismatch magic number of end poll.");
148 if (devc->status != H4032L_STATUS_IDLE) {
150 /* Setup new USB cmd packet, reuse transfer object. */
151 sr_dbg("New command: %d.", devc->status);
152 libusb_fill_bulk_transfer(transfer, usb->devhdl,
153 2 | LIBUSB_ENDPOINT_OUT,
154 (unsigned char *)&devc->cmd_pkt,
155 sizeof(struct h4032l_cmd_pkt),
156 h4032l_usb_callback, (void *)sdi,
159 /* Setup new USB poll packet, reuse transfer object. */
160 sr_dbg("Poll: %d.", devc->status);
161 libusb_fill_bulk_transfer(transfer, usb->devhdl,
162 6 | LIBUSB_ENDPOINT_IN,
163 devc->buffer, ARRAY_SIZE(devc->buffer),
165 (void *)sdi, H4032L_USB_TIMEOUT);
167 /* Send prepared USB packet. */
168 if ((ret = libusb_submit_transfer(transfer)) != 0) {
169 sr_err("Failed to submit transfer: %s.",
170 libusb_error_name(ret));
171 devc->status = H4032L_STATUS_IDLE;
177 if (devc->status == H4032L_STATUS_IDLE)
178 libusb_free_transfer(transfer);
181 uint16_t h4032l_voltage2pwm(double voltage)
184 * word PwmA - channel A Vref PWM value, pseudocode:
185 * -6V < ThresholdVoltage < +6V
186 * Vref = 1.8 - ThresholdVoltage
191 * pwm = ToInt((Vref + 5.0) / 15.0 * 4096.0)
195 voltage = 1.8 - voltage;
198 else if (voltage < -5.0)
201 return (uint16_t) ((voltage + 5.0) * (4096.0 / 15.0));
204 SR_PRIV int h4032l_start(const struct sr_dev_inst *sdi)
206 struct dev_context *devc = sdi->priv;
207 struct sr_usb_dev_inst *usb = sdi->conn;
208 struct libusb_transfer *transfer;
209 unsigned char buffer[] = {0x0f, 0x03, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
212 /* Send reset command to arm the logic analyzer. */
213 if ((ret = libusb_control_transfer(usb->devhdl,
214 LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT, CMD_RESET,
215 0x00, 0x00, buffer, ARRAY_SIZE(buffer), H4032L_USB_TIMEOUT)) < 0) {
216 sr_err("Failed to send vendor request %s.", libusb_error_name(ret));
220 /* Wait for reset vendor request. */
223 /* Send configure command. */
224 devc->cmd_pkt.cmd = CMD_CONFIGURE;
225 devc->status = H4032L_STATUS_CMD_CONFIGURE;
226 devc->remaining_samples = devc->cmd_pkt.sample_size;
228 transfer = libusb_alloc_transfer(0);
230 libusb_fill_bulk_transfer(transfer, usb->devhdl,
231 2 | LIBUSB_ENDPOINT_OUT, (unsigned char *)&devc->cmd_pkt,
232 sizeof(struct h4032l_cmd_pkt), h4032l_usb_callback,
233 (void *)sdi, H4032L_USB_TIMEOUT);
235 if ((ret = libusb_submit_transfer(transfer)) != 0) {
236 sr_err("Failed to submit transfer: %s.", libusb_error_name(ret));
237 libusb_free_transfer(transfer);
241 std_session_send_df_header(sdi);
246 SR_PRIV int h4032l_dev_open(struct sr_dev_inst *sdi)
248 struct drv_context *drvc = sdi->driver->context;
249 struct sr_usb_dev_inst *usb = sdi->conn;
250 struct libusb_device_descriptor des;
251 libusb_device **devlist;
252 int ret = SR_ERR, i, device_count;
253 char connection_id[64];
255 device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist);
256 if (device_count < 0) {
257 sr_err("Failed to get device list: %s.",
258 libusb_error_name(device_count));
262 for (i = 0; i < device_count; i++) {
263 libusb_get_device_descriptor(devlist[i], &des);
265 if (des.idVendor != H4032L_USB_VENDOR ||
266 des.idProduct != H4032L_USB_PRODUCT)
269 if ((sdi->status == SR_ST_INITIALIZING) ||
270 (sdi->status == SR_ST_INACTIVE)) {
271 /* Check device by its physical USB bus/port address. */
272 usb_get_port_path(devlist[i], connection_id,
273 sizeof(connection_id));
274 if (strcmp(sdi->connection_id, connection_id))
275 /* This is not the one. */
279 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
280 if (usb->address == 0xff)
282 * First time we touch this device after FW
283 * upload, so we don't know the address yet.
286 libusb_get_device_address(devlist[i]);
288 sr_err("Failed to open device: %s.",
289 libusb_error_name(ret));
298 libusb_free_device_list(devlist, 1);