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)
48 memset(&cmd->raw[0], PICKIT2_CMD_PADCHAR, PICKIT2_PACKET_LENGTH);
52 static void pickit2_cmd_append(struct pickit2_cmd *cmd, uint8_t b)
57 if (cmd->length == PICKIT2_PACKET_LENGTH)
59 cmd->raw[cmd->length++] = b;
62 static int pickit2_usb_send(const struct sr_dev_inst *sdi, struct pickit2_cmd *cmd)
64 struct sr_usb_dev_inst *usb;
74 text = sr_hexdump_new(&cmd->raw[0], cmd->length);
75 sr_dbg("usb sent: %s", text->str);
76 sr_hexdump_free(text);
78 ret = libusb_interrupt_transfer(usb->devhdl,
79 LIBUSB_ENDPOINT_OUT | PICKIT2_USB_ENDPOINT,
80 &cmd->raw[0], PICKIT2_PACKET_LENGTH,
81 &sent, PICKIT2_USB_TIMEOUT);
83 sr_err("USB transmit error: %s.", libusb_error_name(ret));
86 if (sent != PICKIT2_PACKET_LENGTH) {
87 sr_err("USB short send: %d/%d bytes.",
88 sent, PICKIT2_PACKET_LENGTH);
95 static int pickit2_usb_recv(const struct sr_dev_inst *sdi, struct pickit2_cmd *cmd)
97 struct sr_usb_dev_inst *usb;
107 ret = libusb_interrupt_transfer(usb->devhdl,
108 LIBUSB_ENDPOINT_IN | PICKIT2_USB_ENDPOINT,
109 &cmd->raw[0], PICKIT2_PACKET_LENGTH,
110 &rcvd, PICKIT2_USB_TIMEOUT);
112 if (ret == LIBUSB_ERROR_TIMEOUT)
113 sr_dbg("USB receive error: %s.", libusb_error_name(ret));
115 sr_err("USB receive error: %s.", libusb_error_name(ret));
119 text = sr_hexdump_new(&cmd->raw[0], rcvd);
120 sr_dbg("usb recv: %s", text->str);
121 sr_hexdump_free(text);
124 if (rcvd != PICKIT2_PACKET_LENGTH) {
125 sr_err("USB short recv: %d/%d bytes.",
126 rcvd, PICKIT2_PACKET_LENGTH);
133 /* Send a request, (optionally) keep reading until response became available. */
134 static int pickit2_usb_send_recv(const struct sr_dev_inst *sdi,
135 struct pickit2_cmd *send_cmd, struct pickit2_cmd *recv_cmd, int do_wait)
139 /* Send the command when one got specified. Ignore errors. */
141 (void)pickit2_usb_send(sdi, send_cmd);
144 * Try receiving data, always ignore errors. When requested by
145 * the caller then keep receiving until response data became
151 ret = pickit2_usb_recv(sdi, recv_cmd);
160 SR_PRIV int microchip_pickit2_setup_trigger(const struct sr_dev_inst *sdi)
162 static const uint8_t trigger_channel_masks[PICKIT2_CHANNEL_COUNT] = {
163 /* Bit positions for channels in trigger registers. */
166 static const uint16_t captureratio_magics[] = {
168 * How to exactly calculate these magic 16bit values?
169 * They seem to neither match a percentage value nor a
170 * sample count (assuming 1 window holds 1K samples).
171 * As long as the formula is unknown, we are stuck with
172 * looking up magic values from a table of few pre-sets.
174 0x0000, /* unspecified ratio value */
175 0x03cc, 0x000a, 0x0248, /* 10%/50%/90% in the first window */
176 0x07b4, 0x0b9c, 0x0f84, /* 10% "plus 1/2/3 window widths" */
179 struct dev_context *devc;
180 uint8_t trig_en, trig_lvl, trig_edge, trig_rep, trig_div;
183 size_t trig_pos_idx, ch_idx;
184 uint8_t ch_mask, ch_cond;
185 struct pickit2_cmd cmd;
189 /* Translate user specs to internal setup values. */
190 trig_en = trig_lvl = trig_edge = 0;
191 for (ch_idx = 0; ch_idx < PICKIT2_CHANNEL_COUNT; ch_idx++) {
192 if (!devc->triggers[ch_idx])
194 ch_mask = trigger_channel_masks[ch_idx];
195 ch_cond = devc->triggers[ch_idx];
199 case SR_TRIGGER_RISING:
204 case SR_TRIGGER_FALLING:
205 case SR_TRIGGER_RISING:
206 trig_edge |= ch_mask;
211 trig_rep = MIN(trig_rep, 255);
212 trig_rep = MAX(trig_rep, 1);
215 rate = devc->samplerates[devc->curr_samplerate_idx];
216 rate = SR_MHZ(1) / rate - 1;
217 trig_div = rate & 0xff;
218 trig_pos_idx = devc->trigpos;
219 if (trig_pos_idx >= ARRAY_SIZE(captureratio_magics))
221 trig_pos = captureratio_magics[trig_pos_idx];
223 /* Construct the SETUP packet. */
224 pickit2_cmd_clear(&cmd);
225 pickit2_cmd_append(&cmd, PICKIT2_CMD_SETUP);
226 pickit2_cmd_append(&cmd, 0x01);
227 pickit2_cmd_append(&cmd, trig_en);
228 pickit2_cmd_append(&cmd, trig_lvl);
229 pickit2_cmd_append(&cmd, trig_edge);
230 pickit2_cmd_append(&cmd, trig_rep);
231 pickit2_cmd_append(&cmd, trig_pos % 256);
232 pickit2_cmd_append(&cmd, trig_pos / 256);
233 pickit2_cmd_append(&cmd, trig_div);
236 * Transmit the SETUP packet. Only send it out, poll for the
237 * response later. When a trigger is involved, the response may
238 * take considerable amounts of time to arrive. We want apps
239 * to remain responsive during that period of time.
241 (void)pickit2_usb_send_recv(sdi, &cmd, NULL, FALSE);
246 /* Read specified bank data at given offset into caller provided buffer. */
247 static int pickit2_retrieve_bank(struct sr_dev_inst *sdi,
248 size_t bank_idx, size_t offset, uint8_t **buf, size_t *len)
250 struct pickit2_cmd send_cmd, recv_cmd;
252 size_t copy_iter, copy_len;
254 /* Construct and send the SETPOS packet. No response expected. */
255 pickit2_cmd_clear(&send_cmd);
256 pickit2_cmd_append(&send_cmd, PICKIT2_CMD_SETPOS);
257 pickit2_cmd_append(&send_cmd, offset & 0xff);
258 pickit2_cmd_append(&send_cmd, PICKIT2_SEL_BANK0 + bank_idx);
259 ret = pickit2_usb_send_recv(sdi, &send_cmd, NULL, FALSE);
262 sr_dbg("read bank: pos set");
264 /* Run two READ cycles, get 2x 64 bytes => 128 bytes raw data. */
265 pickit2_cmd_clear(&send_cmd);
266 pickit2_cmd_append(&send_cmd, PICKIT2_CMD_READ);
268 while (copy_iter-- > 0) {
269 ret = pickit2_usb_send_recv(sdi, &send_cmd, &recv_cmd, TRUE);
272 copy_len = MIN(PICKIT2_PACKET_LENGTH, *len);
273 memcpy(*buf, &recv_cmd.raw[0], copy_len);
281 /* Read all of the (banked, raw) sample data after acquisition completed. */
282 static int pickit2_retrieve_sample_data(struct sr_dev_inst *sdi)
284 struct dev_context *devc;
290 rdpos = &devc->samples_raw[0];
291 rdlen = sizeof(devc->samples_raw);
293 ret = pickit2_retrieve_bank(sdi, 0, 0x00, &rdpos, &rdlen);
296 ret = pickit2_retrieve_bank(sdi, 0, 0x80, &rdpos, &rdlen);
299 ret = pickit2_retrieve_bank(sdi, 1, 0x00, &rdpos, &rdlen);
302 ret = pickit2_retrieve_bank(sdi, 1, 0x80, &rdpos, &rdlen);
309 /* Send converted sample data to the sigrok session. */
310 static int pickit2_submit_logic_data(struct sr_dev_inst *sdi)
312 struct dev_context *devc;
314 uint8_t raw_mask, conv_mask;
315 } ch_map[PICKIT2_CHANNEL_COUNT] = {
320 uint8_t *raw_buf, raw_byte, *conv_buf;
321 size_t raw_len, conv_len;
323 struct sr_datafeed_packet packet;
324 struct sr_datafeed_logic logic;
329 * TODO Manipulate (or create) the above channel mapping table.
330 * Remove disabled channels, create dense output format.
332 * - Loop over the index, check the corresponding channel's
333 * state, clear out the conv_mask part and shift down all
334 * subsequent conv_mask parts.
338 * Convert raw dump (two samples per byte, at odd positions) to
339 * internal sigrok format (one sample per byte, at increasing
340 * offsets which start at 0).
342 #define handle_nibble(n) do { \
346 for (ch_idx = 0; ch_idx < PICKIT2_CHANNEL_COUNT; ch_idx++) { \
347 if ((n) & ch_map[ch_idx].raw_mask) \
348 conv_byte |= ch_map[ch_idx].conv_mask; \
350 *conv_buf++ = conv_byte; \
354 raw_len = sizeof(devc->samples_raw);
355 raw_buf = &devc->samples_raw[raw_len];
356 conv_buf = &devc->samples_conv[0];
358 while (raw_len-- > 0) {
359 raw_byte = *(--raw_buf);
360 handle_nibble((raw_byte >> 0) & 0x0f);
361 handle_nibble((raw_byte >> 4) & 0x0f);
364 /* Submit a logic packet to the sigrok session. */
365 packet.type = SR_DF_LOGIC;
366 packet.payload = &logic;
367 logic.unitsize = sizeof(uint8_t);
368 logic.data = &devc->samples_conv[0];
369 logic.length = conv_len;
370 limit = devc->sw_limits.limit_samples;
371 if (limit && limit < logic.length)
372 logic.length = limit;
373 sr_session_send(sdi, &packet);
378 static gboolean pickit2_status_is_cancel(uint16_t status)
380 /* "Button press" and "transfer timeout" translate to "cancelled". */
381 static const uint16_t status_cancel_mask = 0x4004;
383 sr_dbg("recv: status 0x%04x", status);
384 if ((status & status_cancel_mask) == status_cancel_mask)
389 /* Periodically invoked poll routine, checking for incoming receive data. */
390 SR_PRIV int microchip_pickit2_receive_data(int fd, int revents, void *cb_data)
392 struct sr_dev_inst *sdi;
393 struct dev_context *devc;
394 struct pickit2_cmd cmd;
408 /* Waiting for the trigger condition? */
409 if (devc->state == STATE_WAIT) {
410 /* Keep waiting until status becomes available. */
411 ret = pickit2_usb_send_recv(sdi, NULL, &cmd, FALSE);
414 /* Check status flags for cancel requests. */
415 devc->state = STATE_DATA;
416 status = RL16(&cmd.raw[0]);
417 if (pickit2_status_is_cancel(status)) {
418 sr_info("User cancelled acquisition.");
419 sr_dev_acquisition_stop(sdi);
422 sr_dbg("recv: Data has become available.");
427 * Retrieve acquired sample data (blocking, acquisition has
428 * completed and samples are few), and stop acquisition (have
429 * the poll routine unregistered).
431 ret = pickit2_retrieve_sample_data(sdi);
434 ret = pickit2_submit_logic_data(sdi);
437 sr_dev_acquisition_stop(sdi);