]> sigrok.org Git - libsigrok.git/blame - src/hardware/hantek-4032l/protocol.c
hantek-4032l: Fix mismatch in magic number.
[libsigrok.git] / src / hardware / hantek-4032l / protocol.c
CommitLineData
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
27enum 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 34struct h4032l_status_packet {
5089a143
AZ
35 uint32_t magic;
36 uint32_t values;
37 uint32_t status;
38};
39
40SR_PRIV int h4032l_receive_data(int fd, int revents, void *cb_data)
6a25fa42 41{
5089a143
AZ
42 struct timeval tv;
43 struct drv_context *drvc;
6a25fa42
AZ
44
45 (void)fd;
5089a143
AZ
46 (void)revents;
47
48 drvc = (struct drv_context *)cb_data;
49
50 tv.tv_sec = tv.tv_usec = 0;
51 libusb_handle_events_timeout(drvc->sr_ctx->libusb_ctx, &tv);
6a25fa42 52
5089a143
AZ
53 return TRUE;
54}
6a25fa42 55
5089a143
AZ
56void LIBUSB_CALL h4032l_usb_callback(struct libusb_transfer *transfer)
57{
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;
62 gboolean cmd = FALSE;
63 uint32_t max_samples = 512 / sizeof(uint32_t);
64 uint32_t *buffer;
65 struct h4032l_status_packet *status;
66 struct sr_datafeed_packet packet;
67 struct sr_datafeed_logic logic;
68 uint32_t number_samples;
69 int ret;
6a25fa42 70
74c4c174
AV
71 if (transfer->status != LIBUSB_TRANSFER_COMPLETED)
72 sr_dbg("%s error: %d.", __func__, transfer->status);
6a25fa42 73
5089a143
AZ
74 buffer = (uint32_t *)transfer->buffer;
75
76 switch (devc->status) {
77 case H4032L_STATUS_IDLE:
78 sr_err("USB callback called in idle.");
79 break;
80 case H4032L_STATUS_CMD_CONFIGURE:
81 /* Select status request as next. */
82 cmd = TRUE;
83 devc->cmd_pkt.cmd = CMD_STATUS;
84 devc->status = H4032L_STATUS_CMD_STATUS;
85 break;
86 case H4032L_STATUS_CMD_STATUS:
87 /* Select status request as next. */
88 devc->status = H4032L_STATUS_RESPONSE_STATUS;
89 break;
90 case H4032L_STATUS_RESPONSE_STATUS:
91 /*
92 * Check magic and if status is complete, then select
93 * First Transfer as next.
94 */
95 status = (struct h4032l_status_packet *)transfer->buffer;
96 if (status->magic != H4032L_STATUS_PACKET_MAGIC) {
74c4c174 97 devc->status = H4032L_STATUS_RESPONSE_STATUS;
5089a143
AZ
98 } else if (status->status == 2) {
99 devc->status = H4032L_STATUS_RESPONSE_STATUS_CONTINUE;
100 } else {
101 devc->status = H4032L_STATUS_RESPONSE_STATUS_RETRY;
102 }
103 break;
104 case H4032L_STATUS_RESPONSE_STATUS_RETRY:
105 devc->status = H4032L_STATUS_CMD_STATUS;
106 devc->cmd_pkt.cmd = CMD_STATUS;
107 cmd = TRUE;
108 break;
109 case H4032L_STATUS_RESPONSE_STATUS_CONTINUE:
110 devc->status = H4032L_STATUS_CMD_GET;
111 devc->cmd_pkt.cmd = CMD_GET;
112 cmd = TRUE;
113 break;
114 case H4032L_STATUS_CMD_GET:
115 devc->status = H4032L_STATUS_FIRST_TRANSFER;
116 break;
117 case H4032L_STATUS_FIRST_TRANSFER:
74c4c174 118 /* Drop packets until H4032L_START_PACKET_MAGIC. */
5089a143 119 if (buffer[0] != H4032L_START_PACKET_MAGIC) {
74c4c174 120 sr_dbg("Mismatch magic number of start poll.");
5089a143
AZ
121 break;
122 }
123 devc->status = H4032L_STATUS_TRANSFER;
124 max_samples--;
125 buffer++;
97200156 126 /* Fallthrough. */
5089a143
AZ
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);
134 logic.data = buffer;
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.");
144 }
145 break;
146 }
147
148 if (devc->status != H4032L_STATUS_IDLE) {
149 if (cmd) {
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,
157 H4032L_USB_TIMEOUT);
158 } else {
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),
164 h4032l_usb_callback,
165 (void *)sdi, H4032L_USB_TIMEOUT);
166 }
4868f15a 167 /* Send prepared USB packet. */
5089a143
AZ
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;
172 }
173 } else {
174 sr_dbg("Now idle.");
175 }
176
177 if (devc->status == H4032L_STATUS_IDLE)
178 libusb_free_transfer(transfer);
179}
180
181uint16_t h4032l_voltage2pwm(double voltage)
182{
183 /*
184 * word PwmA - channel A Vref PWM value, pseudocode:
185 * -6V < ThresholdVoltage < +6V
186 * Vref = 1.8 - ThresholdVoltage
187 * if Vref > 10.0
188 * Vref = 10.0
189 * if Vref < -5.0
190 * Vref = -5.0
191 * pwm = ToInt((Vref + 5.0) / 15.0 * 4096.0)
192 * if pwm > 4095
193 * pwm = 4095
194 */
195 voltage = 1.8 - voltage;
196 if (voltage > 10.0)
197 voltage = 10.0;
198 else if (voltage < -5.0)
199 voltage = -5.0;
200
201 return (uint16_t) ((voltage + 5.0) * (4096.0 / 15.0));
202}
203
204SR_PRIV int h4032l_start(const struct sr_dev_inst *sdi)
205{
206 struct dev_context *devc = sdi->priv;
207 struct sr_usb_dev_inst *usb = sdi->conn;
208 struct libusb_transfer *transfer;
74c4c174 209 unsigned char buffer[] = {0x0f, 0x03, 0x03, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
5089a143
AZ
210 int ret;
211
74c4c174
AV
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));
217 return SR_ERR;
218 }
219
220 /* Wait for reset vendor request. */
221 g_usleep(20 * 1000);
222
223 /* Send configure command. */
5089a143
AZ
224 devc->cmd_pkt.cmd = CMD_CONFIGURE;
225 devc->status = H4032L_STATUS_CMD_CONFIGURE;
226 devc->remaining_samples = devc->cmd_pkt.sample_size;
227
228 transfer = libusb_alloc_transfer(0);
229
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);
234
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);
238 return SR_ERR;
239 }
240
241 std_session_send_df_header(sdi);
242
243 return SR_OK;
244}
245
246SR_PRIV int h4032l_dev_open(struct sr_dev_inst *sdi)
247{
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];
254
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));
259 return SR_ERR;
260 }
261
262 for (i = 0; i < device_count; i++) {
263 libusb_get_device_descriptor(devlist[i], &des);
264
265 if (des.idVendor != H4032L_USB_VENDOR ||
266 des.idProduct != H4032L_USB_PRODUCT)
267 continue;
268
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. */
276 continue;
277 }
278
279 if (!(ret = libusb_open(devlist[i], &usb->devhdl))) {
280 if (usb->address == 0xff)
281 /*
282 * First time we touch this device after FW
283 * upload, so we don't know the address yet.
284 */
285 usb->address =
286 libusb_get_device_address(devlist[i]);
287 } else {
288 sr_err("Failed to open device: %s.",
289 libusb_error_name(ret));
290 ret = SR_ERR;
291 break;
292 }
293
294 ret = SR_OK;
295 break;
296 }
297
298 libusb_free_device_list(devlist, 1);
299 return ret;
6a25fa42 300}