]>
Commit | Line | Data |
---|---|---|
6a25fa42 AZ |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
5089a143 AZ |
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> | |
6a25fa42 AZ |
7 | * |
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. | |
12 | * | |
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. | |
17 | * | |
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/>. | |
20 | */ | |
21 | ||
22 | #include <config.h> | |
23 | #include "protocol.h" | |
24 | ||
5089a143 AZ |
25 | #define H4032L_USB_TIMEOUT 500 |
26 | ||
27 | enum h4032l_cmd { | |
74c4c174 AV |
28 | CMD_RESET = 0x00b3, /* Also arms the logic analyzer. */ |
29 | CMD_CONFIGURE = 0x2b1a, | |
5089a143 AZ |
30 | CMD_STATUS = 0x4b3a, |
31 | CMD_GET = 0x6b5a | |
32 | }; | |
33 | ||
e80e1858 | 34 | struct h4032l_status_packet { |
5089a143 AZ |
35 | uint32_t magic; |
36 | uint32_t values; | |
37 | uint32_t status; | |
2bd5d17c UH |
38 | uint32_t usbxi_data; |
39 | uint32_t fpga_version; | |
5089a143 AZ |
40 | }; |
41 | ||
a5b9880e AV |
42 | static void finish_acquisition(struct sr_dev_inst *sdi) |
43 | { | |
44 | struct drv_context *drvc = sdi->driver->context; | |
45 | ||
46 | std_session_send_df_end(sdi); | |
47 | usb_source_remove(sdi->session, drvc->sr_ctx); | |
48 | } | |
49 | ||
50 | static void free_transfer(struct libusb_transfer *transfer) | |
51 | { | |
52 | struct sr_dev_inst *sdi = transfer->user_data; | |
53 | struct dev_context *devc = sdi->priv; | |
54 | ||
55 | transfer->buffer = NULL; | |
56 | libusb_free_transfer(transfer); | |
57 | devc->usb_transfer = NULL; | |
58 | ||
59 | finish_acquisition(sdi); | |
60 | } | |
61 | ||
5089a143 | 62 | SR_PRIV int h4032l_receive_data(int fd, int revents, void *cb_data) |
6a25fa42 | 63 | { |
5089a143 AZ |
64 | struct timeval tv; |
65 | struct drv_context *drvc; | |
6a25fa42 AZ |
66 | |
67 | (void)fd; | |
5089a143 AZ |
68 | (void)revents; |
69 | ||
70 | drvc = (struct drv_context *)cb_data; | |
71 | ||
72 | tv.tv_sec = tv.tv_usec = 0; | |
73 | libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv); | |
6a25fa42 | 74 | |
5089a143 AZ |
75 | return TRUE; |
76 | } | |
6a25fa42 | 77 | |
5089a143 AZ |
78 | void LIBUSB_CALL h4032l_usb_callback(struct libusb_transfer *transfer) |
79 | { | |
80 | const struct sr_dev_inst *sdi = transfer->user_data; | |
81 | struct dev_context *devc = sdi->priv; | |
82 | struct drv_context *drvc = sdi->driver->context; | |
83 | struct sr_usb_dev_inst *usb = sdi->conn; | |
84 | gboolean cmd = FALSE; | |
85 | uint32_t max_samples = 512 / sizeof(uint32_t); | |
86 | uint32_t *buffer; | |
87 | struct h4032l_status_packet *status; | |
88 | struct sr_datafeed_packet packet; | |
89 | struct sr_datafeed_logic logic; | |
90 | uint32_t number_samples; | |
91 | int ret; | |
6a25fa42 | 92 | |
a5b9880e AV |
93 | /* |
94 | * If acquisition has already ended, just free any queued up | |
95 | * transfers that come in. | |
96 | */ | |
97 | if (devc->acq_aborted) { | |
98 | free_transfer(transfer); | |
99 | return; | |
100 | } | |
101 | ||
74c4c174 AV |
102 | if (transfer->status != LIBUSB_TRANSFER_COMPLETED) |
103 | sr_dbg("%s error: %d.", __func__, transfer->status); | |
6a25fa42 | 104 | |
5089a143 AZ |
105 | buffer = (uint32_t *)transfer->buffer; |
106 | ||
107 | switch (devc->status) { | |
108 | case H4032L_STATUS_IDLE: | |
109 | sr_err("USB callback called in idle."); | |
110 | break; | |
111 | case H4032L_STATUS_CMD_CONFIGURE: | |
112 | /* Select status request as next. */ | |
113 | cmd = TRUE; | |
114 | devc->cmd_pkt.cmd = CMD_STATUS; | |
115 | devc->status = H4032L_STATUS_CMD_STATUS; | |
116 | break; | |
117 | case H4032L_STATUS_CMD_STATUS: | |
118 | /* Select status request as next. */ | |
119 | devc->status = H4032L_STATUS_RESPONSE_STATUS; | |
120 | break; | |
121 | case H4032L_STATUS_RESPONSE_STATUS: | |
122 | /* | |
123 | * Check magic and if status is complete, then select | |
124 | * First Transfer as next. | |
125 | */ | |
126 | status = (struct h4032l_status_packet *)transfer->buffer; | |
28f2d07f | 127 | if (status->magic != H4032L_STATUS_PACKET_MAGIC) |
74c4c174 | 128 | devc->status = H4032L_STATUS_RESPONSE_STATUS; |
28f2d07f | 129 | else if (status->status == 2) |
5089a143 | 130 | devc->status = H4032L_STATUS_RESPONSE_STATUS_CONTINUE; |
28f2d07f | 131 | else |
5089a143 | 132 | devc->status = H4032L_STATUS_RESPONSE_STATUS_RETRY; |
5089a143 AZ |
133 | break; |
134 | case H4032L_STATUS_RESPONSE_STATUS_RETRY: | |
135 | devc->status = H4032L_STATUS_CMD_STATUS; | |
136 | devc->cmd_pkt.cmd = CMD_STATUS; | |
137 | cmd = TRUE; | |
138 | break; | |
139 | case H4032L_STATUS_RESPONSE_STATUS_CONTINUE: | |
140 | devc->status = H4032L_STATUS_CMD_GET; | |
141 | devc->cmd_pkt.cmd = CMD_GET; | |
142 | cmd = TRUE; | |
143 | break; | |
144 | case H4032L_STATUS_CMD_GET: | |
145 | devc->status = H4032L_STATUS_FIRST_TRANSFER; | |
146 | break; | |
147 | case H4032L_STATUS_FIRST_TRANSFER: | |
74c4c174 | 148 | /* Drop packets until H4032L_START_PACKET_MAGIC. */ |
5089a143 | 149 | if (buffer[0] != H4032L_START_PACKET_MAGIC) { |
74c4c174 | 150 | sr_dbg("Mismatch magic number of start poll."); |
5089a143 AZ |
151 | break; |
152 | } | |
153 | devc->status = H4032L_STATUS_TRANSFER; | |
154 | max_samples--; | |
155 | buffer++; | |
97200156 | 156 | /* Fallthrough. */ |
5089a143 | 157 | case H4032L_STATUS_TRANSFER: |
28f2d07f AV |
158 | number_samples = (devc->remaining_samples < max_samples) ? |
159 | devc->remaining_samples : max_samples; | |
5089a143 AZ |
160 | devc->remaining_samples -= number_samples; |
161 | packet.type = SR_DF_LOGIC; | |
162 | packet.payload = &logic; | |
163 | logic.length = number_samples * sizeof(uint32_t); | |
164 | logic.unitsize = sizeof(uint32_t); | |
165 | logic.data = buffer; | |
166 | sr_session_send(sdi, &packet); | |
167 | sr_dbg("Remaining: %d %08X %08X.", devc->remaining_samples, | |
28f2d07f | 168 | buffer[0], buffer[1]); |
5089a143 AZ |
169 | if (devc->remaining_samples == 0) { |
170 | std_session_send_df_end(sdi); | |
171 | usb_source_remove(sdi->session, drvc->sr_ctx); | |
172 | devc->status = H4032L_STATUS_IDLE; | |
173 | if (buffer[number_samples] != H4032L_END_PACKET_MAGIC) | |
174 | sr_err("Mismatch magic number of end poll."); | |
175 | } | |
176 | break; | |
177 | } | |
178 | ||
179 | if (devc->status != H4032L_STATUS_IDLE) { | |
180 | if (cmd) { | |
181 | /* Setup new USB cmd packet, reuse transfer object. */ | |
182 | sr_dbg("New command: %d.", devc->status); | |
183 | libusb_fill_bulk_transfer(transfer, usb->devhdl, | |
184 | 2 | LIBUSB_ENDPOINT_OUT, | |
185 | (unsigned char *)&devc->cmd_pkt, | |
186 | sizeof(struct h4032l_cmd_pkt), | |
28f2d07f AV |
187 | h4032l_usb_callback, |
188 | (void *)sdi, H4032L_USB_TIMEOUT); | |
5089a143 AZ |
189 | } else { |
190 | /* Setup new USB poll packet, reuse transfer object. */ | |
191 | sr_dbg("Poll: %d.", devc->status); | |
192 | libusb_fill_bulk_transfer(transfer, usb->devhdl, | |
193 | 6 | LIBUSB_ENDPOINT_IN, | |
194 | devc->buffer, ARRAY_SIZE(devc->buffer), | |
195 | h4032l_usb_callback, | |
196 | (void *)sdi, H4032L_USB_TIMEOUT); | |
197 | } | |
4868f15a | 198 | /* Send prepared USB packet. */ |
5089a143 AZ |
199 | if ((ret = libusb_submit_transfer(transfer)) != 0) { |
200 | sr_err("Failed to submit transfer: %s.", | |
201 | libusb_error_name(ret)); | |
202 | devc->status = H4032L_STATUS_IDLE; | |
203 | } | |
204 | } else { | |
205 | sr_dbg("Now idle."); | |
206 | } | |
207 | ||
208 | if (devc->status == H4032L_STATUS_IDLE) | |
a5b9880e | 209 | free_transfer(transfer); |
5089a143 AZ |
210 | } |
211 | ||
212 | uint16_t h4032l_voltage2pwm(double voltage) | |
213 | { | |
214 | /* | |
215 | * word PwmA - channel A Vref PWM value, pseudocode: | |
216 | * -6V < ThresholdVoltage < +6V | |
217 | * Vref = 1.8 - ThresholdVoltage | |
218 | * if Vref > 10.0 | |
219 | * Vref = 10.0 | |
220 | * if Vref < -5.0 | |
221 | * Vref = -5.0 | |
222 | * pwm = ToInt((Vref + 5.0) / 15.0 * 4096.0) | |
223 | * if pwm > 4095 | |
224 | * pwm = 4095 | |
225 | */ | |
226 | voltage = 1.8 - voltage; | |
227 | if (voltage > 10.0) | |
228 | voltage = 10.0; | |
229 | else if (voltage < -5.0) | |
230 | voltage = -5.0; | |
231 | ||
232 | return (uint16_t) ((voltage + 5.0) * (4096.0 / 15.0)); | |
233 | } | |
234 | ||
235 | SR_PRIV int h4032l_start(const struct sr_dev_inst *sdi) | |
236 | { | |
237 | struct dev_context *devc = sdi->priv; | |
238 | struct sr_usb_dev_inst *usb = sdi->conn; | |
239 | struct libusb_transfer *transfer; | |
74c4c174 | 240 | unsigned char buffer[] = {0x0f, 0x03, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; |
5089a143 AZ |
241 | int ret; |
242 | ||
74c4c174 AV |
243 | /* Send reset command to arm the logic analyzer. */ |
244 | if ((ret = libusb_control_transfer(usb->devhdl, | |
245 | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_ENDPOINT_OUT, CMD_RESET, | |
246 | 0x00, 0x00, buffer, ARRAY_SIZE(buffer), H4032L_USB_TIMEOUT)) < 0) { | |
28f2d07f AV |
247 | sr_err("Failed to send vendor request %s.", |
248 | libusb_error_name(ret)); | |
74c4c174 AV |
249 | return SR_ERR; |
250 | } | |
251 | ||
252 | /* Wait for reset vendor request. */ | |
253 | g_usleep(20 * 1000); | |
254 | ||
255 | /* Send configure command. */ | |
5089a143 AZ |
256 | devc->cmd_pkt.cmd = CMD_CONFIGURE; |
257 | devc->status = H4032L_STATUS_CMD_CONFIGURE; | |
258 | devc->remaining_samples = devc->cmd_pkt.sample_size; | |
259 | ||
260 | transfer = libusb_alloc_transfer(0); | |
261 | ||
262 | libusb_fill_bulk_transfer(transfer, usb->devhdl, | |
263 | 2 | LIBUSB_ENDPOINT_OUT, (unsigned char *)&devc->cmd_pkt, | |
264 | sizeof(struct h4032l_cmd_pkt), h4032l_usb_callback, | |
265 | (void *)sdi, H4032L_USB_TIMEOUT); | |
266 | ||
267 | if ((ret = libusb_submit_transfer(transfer)) != 0) { | |
28f2d07f AV |
268 | sr_err("Failed to submit transfer: %s.", |
269 | libusb_error_name(ret)); | |
5089a143 AZ |
270 | libusb_free_transfer(transfer); |
271 | return SR_ERR; | |
272 | } | |
273 | ||
274 | std_session_send_df_header(sdi); | |
275 | ||
276 | return SR_OK; | |
277 | } | |
278 | ||
279 | SR_PRIV int h4032l_dev_open(struct sr_dev_inst *sdi) | |
280 | { | |
281 | struct drv_context *drvc = sdi->driver->context; | |
282 | struct sr_usb_dev_inst *usb = sdi->conn; | |
283 | struct libusb_device_descriptor des; | |
284 | libusb_device **devlist; | |
285 | int ret = SR_ERR, i, device_count; | |
286 | char connection_id[64]; | |
287 | ||
288 | device_count = libusb_get_device_list(drvc->sr_ctx->libusb_ctx, &devlist); | |
289 | if (device_count < 0) { | |
290 | sr_err("Failed to get device list: %s.", | |
291 | libusb_error_name(device_count)); | |
292 | return SR_ERR; | |
293 | } | |
294 | ||
295 | for (i = 0; i < device_count; i++) { | |
296 | libusb_get_device_descriptor(devlist[i], &des); | |
297 | ||
298 | if (des.idVendor != H4032L_USB_VENDOR || | |
299 | des.idProduct != H4032L_USB_PRODUCT) | |
300 | continue; | |
301 | ||
302 | if ((sdi->status == SR_ST_INITIALIZING) || | |
303 | (sdi->status == SR_ST_INACTIVE)) { | |
304 | /* Check device by its physical USB bus/port address. */ | |
28f2d07f AV |
305 | if (usb_get_port_path(devlist[i], connection_id, sizeof(connection_id)) < 0) |
306 | continue; | |
307 | ||
5089a143 AZ |
308 | if (strcmp(sdi->connection_id, connection_id)) |
309 | /* This is not the one. */ | |
310 | continue; | |
311 | } | |
312 | ||
313 | if (!(ret = libusb_open(devlist[i], &usb->devhdl))) { | |
314 | if (usb->address == 0xff) | |
315 | /* | |
316 | * First time we touch this device after FW | |
317 | * upload, so we don't know the address yet. | |
318 | */ | |
319 | usb->address = | |
28f2d07f | 320 | libusb_get_device_address(devlist[i]); |
5089a143 AZ |
321 | } else { |
322 | sr_err("Failed to open device: %s.", | |
323 | libusb_error_name(ret)); | |
324 | ret = SR_ERR; | |
325 | break; | |
326 | } | |
327 | ||
328 | ret = SR_OK; | |
329 | break; | |
330 | } | |
331 | ||
332 | libusb_free_device_list(devlist, 1); | |
333 | return ret; | |
6a25fa42 | 334 | } |
7a7afc00 AV |
335 | |
336 | SR_PRIV int h4032l_get_fpga_version(const struct sr_dev_inst *sdi) | |
337 | { | |
338 | struct dev_context *devc = sdi->priv; | |
339 | struct sr_usb_dev_inst *usb = sdi->conn; | |
340 | struct h4032l_status_packet *status; | |
341 | int transferred; | |
342 | int i, ret; | |
343 | ||
344 | /* Set command to status. */ | |
345 | devc->cmd_pkt.magic = H4032L_CMD_PKT_MAGIC; | |
346 | devc->cmd_pkt.cmd = CMD_STATUS; | |
347 | ||
348 | /* Send status request. */ | |
349 | if ((ret = libusb_bulk_transfer(usb->devhdl, | |
350 | 2 | LIBUSB_ENDPOINT_OUT, (unsigned char *)&devc->cmd_pkt, | |
351 | sizeof(struct h4032l_cmd_pkt), &transferred, H4032L_USB_TIMEOUT)) < 0) { | |
352 | sr_err("Unable to send FPGA version request: %s.", | |
353 | libusb_error_name(ret)); | |
354 | return SR_ERR; | |
355 | } | |
356 | ||
357 | /* Attempt to get FGPA version. */ | |
358 | for (i = 0; i < 10; i++) { | |
359 | if ((ret = libusb_bulk_transfer(usb->devhdl, | |
360 | 6 | LIBUSB_ENDPOINT_IN, devc->buffer, | |
361 | ARRAY_SIZE(devc->buffer), &transferred, H4032L_USB_TIMEOUT)) < 0) { | |
362 | sr_err("Unable to receive FPGA version: %s.", | |
363 | libusb_error_name(ret)); | |
364 | return SR_ERR; | |
365 | } | |
366 | status = (struct h4032l_status_packet *)devc->buffer; | |
367 | if (status->magic == H4032L_STATUS_PACKET_MAGIC) { | |
368 | sr_dbg("FPGA version: 0x%x.", status->fpga_version); | |
369 | devc->fpga_version = status->fpga_version; | |
370 | return SR_OK; | |
371 | } | |
372 | } | |
373 | ||
374 | sr_err("Unable to get FPGA version."); | |
375 | ||
376 | return SR_ERR; | |
377 | } |