2 * This file is part of the libsigrok project.
4 * Copyright (C) 2018 Gerhard Sittig <gerhard.sittig@gmx.net>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #define PICKIT2_PACKET_LENGTH 64
25 #define PICKIT2_USB_ENDPOINT 1
26 #define PICKIT2_USB_TIMEOUT 250
28 #define PICKIT2_CMD_CHKSTAT 0xa2
29 #define PICKIT2_CMD_CHKVOLT 0xa3
30 #define PICKIT2_CMD_READ 0xac
31 #define PICKIT2_CMD_PADCHAR 0xad
32 #define PICKIT2_CMD_SETUP 0xb8
33 #define PICKIT2_CMD_SETPOS 0xb9
35 #define PICKIT2_SEL_BANK0 0x06
36 #define PICKIT2_SEL_BANK1 0x07
40 uint8_t raw[PICKIT2_PACKET_LENGTH];
43 static void pickit2_cmd_clear(struct pickit2_cmd *cmd)
47 memset(&cmd->raw[0], PICKIT2_CMD_PADCHAR, PICKIT2_PACKET_LENGTH);
51 static void pickit2_cmd_append(struct pickit2_cmd *cmd, uint8_t b)
55 if (cmd->length == PICKIT2_PACKET_LENGTH)
57 cmd->raw[cmd->length++] = b;
60 static int pickit2_usb_send(const struct sr_dev_inst *sdi, struct pickit2_cmd *cmd)
62 struct sr_usb_dev_inst *usb;
72 text = sr_hexdump_new(&cmd->raw[0], cmd->length);
73 sr_dbg("USB sent: %s", text->str);
74 sr_hexdump_free(text);
76 ret = libusb_interrupt_transfer(usb->devhdl,
77 LIBUSB_ENDPOINT_OUT | PICKIT2_USB_ENDPOINT,
78 &cmd->raw[0], PICKIT2_PACKET_LENGTH,
79 &sent, PICKIT2_USB_TIMEOUT);
81 sr_err("USB transmit error: %s.", libusb_error_name(ret));
84 if (sent != PICKIT2_PACKET_LENGTH) {
85 sr_err("USB short send: %d/%d bytes.",
86 sent, PICKIT2_PACKET_LENGTH);
93 static int pickit2_usb_recv(const struct sr_dev_inst *sdi, struct pickit2_cmd *cmd)
95 struct sr_usb_dev_inst *usb;
105 ret = libusb_interrupt_transfer(usb->devhdl,
106 LIBUSB_ENDPOINT_IN | PICKIT2_USB_ENDPOINT,
107 &cmd->raw[0], PICKIT2_PACKET_LENGTH,
108 &rcvd, PICKIT2_USB_TIMEOUT);
110 if (ret == LIBUSB_ERROR_TIMEOUT)
111 sr_dbg("USB receive error: %s.", libusb_error_name(ret));
113 sr_err("USB receive error: %s.", libusb_error_name(ret));
117 text = sr_hexdump_new(&cmd->raw[0], rcvd);
118 sr_dbg("USB recv: %s", text->str);
119 sr_hexdump_free(text);
122 if (rcvd != PICKIT2_PACKET_LENGTH) {
123 sr_err("USB short recv: %d/%d bytes.",
124 rcvd, PICKIT2_PACKET_LENGTH);
131 /* Send a request, (optionally) keep reading until response became available. */
132 static int pickit2_usb_send_recv(const struct sr_dev_inst *sdi,
133 struct pickit2_cmd *send_cmd, struct pickit2_cmd *recv_cmd, int do_wait)
137 /* Send the command when one got specified. Ignore errors. */
139 (void)pickit2_usb_send(sdi, send_cmd);
142 * Try receiving data, always ignore errors. When requested by
143 * the caller then keep receiving until response data became
149 ret = pickit2_usb_recv(sdi, recv_cmd);
158 SR_PRIV int microchip_pickit2_setup_trigger(const struct sr_dev_inst *sdi)
160 static const uint8_t trigger_channel_masks[PICKIT2_CHANNEL_COUNT] = {
161 /* Bit positions for channels in trigger registers. */
164 static const uint16_t captureratio_magics[] = {
166 * How to exactly calculate these magic 16bit values?
167 * They seem to neither match a percentage value nor a
168 * sample count (assuming 1 window holds 1K samples).
169 * As long as the formula is unknown, we are stuck with
170 * looking up magic values from a table of few pre-sets.
172 0x0000, /* unspecified ratio value */
173 0x03cc, 0x000a, 0x0248, /* 10%/50%/90% in the first window */
174 0x07b4, 0x0b9c, 0x0f84, /* 10% "plus 1/2/3 window widths" */
177 struct dev_context *devc;
178 uint8_t trig_en, trig_lvl, trig_edge, trig_rep, trig_div;
181 size_t trig_pos_idx, ch_idx;
182 uint8_t ch_mask, ch_cond;
183 struct pickit2_cmd cmd;
187 /* Translate user specs to internal setup values. */
188 trig_en = trig_lvl = trig_edge = 0;
189 for (ch_idx = 0; ch_idx < PICKIT2_CHANNEL_COUNT; ch_idx++) {
190 if (!devc->triggers[ch_idx])
192 ch_mask = trigger_channel_masks[ch_idx];
193 ch_cond = devc->triggers[ch_idx];
197 case SR_TRIGGER_RISING:
202 case SR_TRIGGER_FALLING:
203 case SR_TRIGGER_RISING:
204 trig_edge |= ch_mask;
209 trig_rep = MIN(trig_rep, 255);
210 trig_rep = MAX(trig_rep, 1);
213 rate = devc->samplerates[devc->curr_samplerate_idx];
214 rate = SR_MHZ(1) / rate - 1;
215 trig_div = rate & 0xff;
216 trig_pos_idx = devc->trigpos;
217 if (trig_pos_idx >= ARRAY_SIZE(captureratio_magics))
219 trig_pos = captureratio_magics[trig_pos_idx];
221 /* Construct the SETUP packet. */
222 pickit2_cmd_clear(&cmd);
223 pickit2_cmd_append(&cmd, PICKIT2_CMD_SETUP);
224 pickit2_cmd_append(&cmd, 0x01);
225 pickit2_cmd_append(&cmd, trig_en);
226 pickit2_cmd_append(&cmd, trig_lvl);
227 pickit2_cmd_append(&cmd, trig_edge);
228 pickit2_cmd_append(&cmd, trig_rep);
229 pickit2_cmd_append(&cmd, trig_pos % 256);
230 pickit2_cmd_append(&cmd, trig_pos / 256);
231 pickit2_cmd_append(&cmd, trig_div);
234 * Transmit the SETUP packet. Only send it out, poll for the
235 * response later. When a trigger is involved, the response may
236 * take considerable amounts of time to arrive. We want apps
237 * to remain responsive during that period of time.
239 (void)pickit2_usb_send_recv(sdi, &cmd, NULL, FALSE);
244 /* Read specified bank data at given offset into caller provided buffer. */
245 static int pickit2_retrieve_bank(struct sr_dev_inst *sdi,
246 size_t bank_idx, size_t offset, uint8_t **buf, size_t *len)
248 struct pickit2_cmd send_cmd, recv_cmd;
250 size_t copy_iter, copy_len;
252 /* Construct and send the SETPOS packet. No response expected. */
253 pickit2_cmd_clear(&send_cmd);
254 pickit2_cmd_append(&send_cmd, PICKIT2_CMD_SETPOS);
255 pickit2_cmd_append(&send_cmd, offset & 0xff);
256 pickit2_cmd_append(&send_cmd, PICKIT2_SEL_BANK0 + bank_idx);
257 ret = pickit2_usb_send_recv(sdi, &send_cmd, NULL, FALSE);
260 sr_dbg("read bank: pos set");
262 /* Run two READ cycles, get 2x 64 bytes => 128 bytes raw data. */
263 pickit2_cmd_clear(&send_cmd);
264 pickit2_cmd_append(&send_cmd, PICKIT2_CMD_READ);
266 while (copy_iter-- > 0) {
267 ret = pickit2_usb_send_recv(sdi, &send_cmd, &recv_cmd, TRUE);
270 copy_len = MIN(PICKIT2_PACKET_LENGTH, *len);
271 memcpy(*buf, &recv_cmd.raw[0], copy_len);
279 /* Read all of the (banked, raw) sample data after acquisition completed. */
280 static int pickit2_retrieve_sample_data(struct sr_dev_inst *sdi)
282 struct dev_context *devc;
288 rdpos = &devc->samples_raw[0];
289 rdlen = sizeof(devc->samples_raw);
291 ret = pickit2_retrieve_bank(sdi, 0, 0x00, &rdpos, &rdlen);
294 ret = pickit2_retrieve_bank(sdi, 0, 0x80, &rdpos, &rdlen);
297 ret = pickit2_retrieve_bank(sdi, 1, 0x00, &rdpos, &rdlen);
300 ret = pickit2_retrieve_bank(sdi, 1, 0x80, &rdpos, &rdlen);
307 /* Send converted sample data to the sigrok session. */
308 static int pickit2_submit_logic_data(struct sr_dev_inst *sdi)
310 struct dev_context *devc;
312 uint8_t raw_mask, conv_mask;
313 } ch_map[PICKIT2_CHANNEL_COUNT] = {
318 uint8_t *raw_buf, raw_byte, *conv_buf;
319 size_t raw_len, conv_len;
321 struct sr_datafeed_packet packet;
322 struct sr_datafeed_logic logic;
327 * TODO Manipulate (or create) the above channel mapping table.
328 * Remove disabled channels, create dense output format.
330 * - Loop over the index, check the corresponding channel's
331 * state, clear out the conv_mask part and shift down all
332 * subsequent conv_mask parts.
336 * Convert raw dump (two samples per byte, at odd positions) to
337 * internal sigrok format (one sample per byte, at increasing
338 * offsets which start at 0).
340 #define handle_nibble(n) do { \
344 for (ch_idx = 0; ch_idx < PICKIT2_CHANNEL_COUNT; ch_idx++) { \
345 if ((n) & ch_map[ch_idx].raw_mask) \
346 conv_byte |= ch_map[ch_idx].conv_mask; \
348 *conv_buf++ = conv_byte; \
352 raw_len = sizeof(devc->samples_raw);
353 raw_buf = &devc->samples_raw[raw_len];
354 conv_buf = &devc->samples_conv[0];
356 while (raw_len-- > 0) {
357 raw_byte = *(--raw_buf);
358 handle_nibble((raw_byte >> 0) & 0x0f);
359 handle_nibble((raw_byte >> 4) & 0x0f);
362 /* Submit a logic packet to the sigrok session. */
363 packet.type = SR_DF_LOGIC;
364 packet.payload = &logic;
365 logic.unitsize = sizeof(uint8_t);
366 logic.data = &devc->samples_conv[0];
367 logic.length = conv_len;
368 limit = devc->sw_limits.limit_samples;
369 if (limit && limit < logic.length)
370 logic.length = limit;
371 sr_session_send(sdi, &packet);
376 static gboolean pickit2_status_is_cancel(uint16_t status)
378 /* "Button press" and "transfer timeout" translate to "cancelled". */
379 static const uint16_t status_cancel_mask = 0x4004;
381 sr_dbg("recv: status 0x%04x", status);
382 if ((status & status_cancel_mask) == status_cancel_mask)
387 /* Periodically invoked poll routine, checking for incoming receive data. */
388 SR_PRIV int microchip_pickit2_receive_data(int fd, int revents, void *cb_data)
390 struct sr_dev_inst *sdi;
391 struct dev_context *devc;
392 struct pickit2_cmd cmd;
406 /* Waiting for the trigger condition? */
407 if (devc->state == STATE_WAIT) {
408 /* Keep waiting until status becomes available. */
409 ret = pickit2_usb_send_recv(sdi, NULL, &cmd, FALSE);
412 /* Check status flags for cancel requests. */
413 devc->state = STATE_DATA;
414 status = RL16(&cmd.raw[0]);
415 if (pickit2_status_is_cancel(status)) {
416 sr_info("User cancelled acquisition.");
417 sr_dev_acquisition_stop(sdi);
420 sr_dbg("recv: Data has become available.");
425 * Retrieve acquired sample data (blocking, acquisition has
426 * completed and samples are few), and stop acquisition (have
427 * the poll routine unregistered).
429 ret = pickit2_retrieve_sample_data(sdi);
432 ret = pickit2_submit_logic_data(sdi);
435 sr_dev_acquisition_stop(sdi);