blob: a0f275d11eca8b0fac726655aae73a757a6f8aaf [file] [log] [blame]
Justin Chevrier66e554b2015-02-08 21:58:10 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2010 Carl-Daniel Hailfinger
5 * Copyright (C) 2014 Justin Chevrier
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
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.
Justin Chevrier66e554b2015-02-08 21:58:10 +000015 */
16
17/*
18 * Connections are as follows:
19 *
20 * +------+-----+----------+
21 * | SPI | Pin | PICkit2 |
22 * +------+-----+----------+
23 * | /CS | 1 | VPP/MCLR |
24 * | VCC | 2 | VDD |
25 * | GND | 3 | GND |
26 * | MISO | 4 | PGD |
27 * | SCLK | 5 | PDC |
28 * | MOSI | 6 | AUX |
29 * +------+-----+----------+
30 *
31 * Inspiration and some specifics of the interface came via the AVRDude
32 * PICkit2 code: https://github.com/steve-m/avrdude/blob/master/pickit2.c
33 */
34
Justin Chevrier66e554b2015-02-08 21:58:10 +000035#include <stdlib.h>
36#include <stdio.h>
37#include <string.h>
38#include <limits.h>
39#include <errno.h>
Thomas Heijligenb221cd72019-04-05 15:08:35 +020040#include <libusb.h>
Justin Chevrier66e554b2015-02-08 21:58:10 +000041
42#include "flash.h"
Justin Chevrier66e554b2015-02-08 21:58:10 +000043#include "programmer.h"
44#include "spi.h"
45
Thomas Heijligencc853d82021-05-04 15:32:17 +020046static const struct dev_entry devs_pickit2_spi[] = {
Stefan Taunerf31fe842016-02-22 08:59:15 +000047 {0x04D8, 0x0033, OK, "Microchip", "PICkit 2"},
48
Evgeny Zinoviev83c56b82019-11-05 17:47:43 +030049 {0}
Stefan Taunerf31fe842016-02-22 08:59:15 +000050};
51
Anastasia Klimchuk47e7d472021-04-30 14:10:04 +100052struct pickit2_spi_data {
53 libusb_device_handle *pickit2_handle;
54};
Justin Chevrier66e554b2015-02-08 21:58:10 +000055
56/* Default USB transaction timeout in ms */
57#define DFLT_TIMEOUT 10000
58
59#define CMD_LENGTH 64
60#define ENDPOINT_OUT 0x01
61#define ENDPOINT_IN 0x81
62
Justin Chevrier66e554b2015-02-08 21:58:10 +000063#define CMD_GET_VERSION 0x76
64#define CMD_SET_VDD 0xA0
65#define CMD_SET_VPP 0xA1
66#define CMD_READ_VDD_VPP 0xA3
67#define CMD_EXEC_SCRIPT 0xA6
68#define CMD_CLR_DLOAD_BUFF 0xA7
69#define CMD_DOWNLOAD_DATA 0xA8
70#define CMD_CLR_ULOAD_BUFF 0xA9
71#define CMD_UPLOAD_DATA 0xAA
72#define CMD_END_OF_BUFFER 0xAD
73
74#define SCR_SPI_READ_BUF 0xC5
75#define SCR_SPI_WRITE_BUF 0xC6
76#define SCR_SET_AUX 0xCF
77#define SCR_LOOP 0xE9
78#define SCR_SET_ICSP_CLK_PERIOD 0xEA
79#define SCR_SET_PINS 0xF3
80#define SCR_BUSY_LED_OFF 0xF4
81#define SCR_BUSY_LED_ON 0xF5
82#define SCR_MCLR_GND_OFF 0xF6
83#define SCR_MCLR_GND_ON 0xF7
84#define SCR_VPP_PWM_OFF 0xF8
85#define SCR_VPP_PWM_ON 0xF9
86#define SCR_VPP_OFF 0xFA
87#define SCR_VPP_ON 0xFB
88#define SCR_VDD_OFF 0xFE
89#define SCR_VDD_ON 0xFF
90
Angel Pons75d6ec62022-07-16 12:12:50 +020091static int pickit2_interrupt_transfer(libusb_device_handle *handle, unsigned char endpoint, unsigned char *data)
92{
93 int transferred;
94 return libusb_interrupt_transfer(handle, endpoint, data, CMD_LENGTH, &transferred, DFLT_TIMEOUT);
95}
96
Anastasia Klimchuk47e7d472021-04-30 14:10:04 +100097static int pickit2_get_firmware_version(libusb_device_handle *pickit2_handle)
Justin Chevrier66e554b2015-02-08 21:58:10 +000098{
99 int ret;
100 uint8_t command[CMD_LENGTH] = {CMD_GET_VERSION, CMD_END_OF_BUFFER};
Elyes HAOUAS0cacb112019-02-04 12:16:38 +0100101
Angel Pons75d6ec62022-07-16 12:12:50 +0200102 ret = pickit2_interrupt_transfer(pickit2_handle, ENDPOINT_OUT, command);
Elyes HAOUAS3384fb62019-07-18 14:00:13 +0200103
Thomas Heijligenb221cd72019-04-05 15:08:35 +0200104 if (ret != 0) {
105 msg_perr("Command Get Firmware Version failed!\n");
Justin Chevrier66e554b2015-02-08 21:58:10 +0000106 return 1;
107 }
108
Angel Pons75d6ec62022-07-16 12:12:50 +0200109 ret = pickit2_interrupt_transfer(pickit2_handle, ENDPOINT_IN, command);
Elyes HAOUAS3384fb62019-07-18 14:00:13 +0200110
111 if (ret != 0) {
112 msg_perr("Command Get Firmware Version failed!\n");
113 return 1;
114 }
115
116 msg_pdbg("PICkit2 Firmware Version: %d.%d\n", (int)command[0], (int)command[1]);
Justin Chevrier66e554b2015-02-08 21:58:10 +0000117 return 0;
118}
119
Anastasia Klimchuk47e7d472021-04-30 14:10:04 +1000120static int pickit2_set_spi_voltage(libusb_device_handle *pickit2_handle, int millivolt)
Justin Chevrier66e554b2015-02-08 21:58:10 +0000121{
122 double voltage_selector;
123 switch (millivolt) {
124 case 0:
125 /* Admittedly this one is an assumption. */
126 voltage_selector = 0;
127 break;
128 case 1800:
129 voltage_selector = 1.8;
130 break;
131 case 2500:
132 voltage_selector = 2.5;
133 break;
134 case 3500:
135 voltage_selector = 3.5;
136 break;
137 default:
138 msg_perr("Unknown voltage %i mV! Aborting.\n", millivolt);
139 return 1;
140 }
141 msg_pdbg("Setting SPI voltage to %u.%03u V\n", millivolt / 1000,
142 millivolt % 1000);
143
144 uint8_t command[CMD_LENGTH] = {
145 CMD_SET_VDD,
146 voltage_selector * 2048 + 672,
147 (voltage_selector * 2048 + 672) / 256,
148 voltage_selector * 36,
149 CMD_SET_VPP,
150 0x40,
151 voltage_selector * 18.61,
152 voltage_selector * 13,
153 CMD_END_OF_BUFFER
154 };
Angel Pons75d6ec62022-07-16 12:12:50 +0200155 int ret = pickit2_interrupt_transfer(pickit2_handle, ENDPOINT_OUT, command);
Justin Chevrier66e554b2015-02-08 21:58:10 +0000156
Thomas Heijligenb221cd72019-04-05 15:08:35 +0200157 if (ret != 0) {
158 msg_perr("Command Set Voltage failed!\n");
Justin Chevrier66e554b2015-02-08 21:58:10 +0000159 return 1;
160 }
161
162 return 0;
163}
164
165struct pickit2_spispeeds {
166 const char *const name;
167 const int speed;
168};
169
170static const struct pickit2_spispeeds spispeeds[] = {
171 { "1M", 0x1 },
172 { "500k", 0x2 },
173 { "333k", 0x3 },
174 { "250k", 0x4 },
175 { NULL, 0x0 },
176};
177
Anastasia Klimchuk47e7d472021-04-30 14:10:04 +1000178static int pickit2_set_spi_speed(libusb_device_handle *pickit2_handle, unsigned int spispeed_idx)
Justin Chevrier66e554b2015-02-08 21:58:10 +0000179{
180 msg_pdbg("SPI speed is %sHz\n", spispeeds[spispeed_idx].name);
181
182 uint8_t command[CMD_LENGTH] = {
183 CMD_EXEC_SCRIPT,
184 2,
185 SCR_SET_ICSP_CLK_PERIOD,
186 spispeed_idx,
187 CMD_END_OF_BUFFER
188 };
189
Angel Pons75d6ec62022-07-16 12:12:50 +0200190 int ret = pickit2_interrupt_transfer(pickit2_handle, ENDPOINT_OUT, command);
Justin Chevrier66e554b2015-02-08 21:58:10 +0000191
Thomas Heijligenb221cd72019-04-05 15:08:35 +0200192 if (ret != 0) {
193 msg_perr("Command Set SPI Speed failed!\n");
Justin Chevrier66e554b2015-02-08 21:58:10 +0000194 return 1;
195 }
196
197 return 0;
198}
199
Nico Huber610c1aa2023-02-15 02:56:05 +0100200static int pickit2_spi_send_command(const struct spi_master *mst, unsigned int writecnt, unsigned int readcnt,
201 const unsigned char *writearr, unsigned char *readarr)
Justin Chevrier66e554b2015-02-08 21:58:10 +0000202{
Felix Singer48c3f182022-07-29 03:16:19 +0200203 const unsigned int total_packetsize = writecnt + readcnt + 20;
Nico Huber610c1aa2023-02-15 02:56:05 +0100204 struct pickit2_spi_data *pickit2_data = mst->data;
Justin Chevrier66e554b2015-02-08 21:58:10 +0000205
206 /* Maximum number of bytes per transaction (including command overhead) is 64. Lets play it safe
207 * and always assume the worst case scenario of 20 bytes command overhead.
208 */
Felix Singer48c3f182022-07-29 03:16:19 +0200209 if (total_packetsize > CMD_LENGTH) {
Felix Singerd07c4a42022-07-29 03:09:02 +0200210 msg_perr("\nTotal packetsize (%i) is greater than %i supported, aborting.\n",
Felix Singer48c3f182022-07-29 03:16:19 +0200211 total_packetsize, CMD_LENGTH);
Justin Chevrier66e554b2015-02-08 21:58:10 +0000212 return 1;
213 }
214
215 uint8_t buf[CMD_LENGTH] = {CMD_DOWNLOAD_DATA, writecnt};
Nico Huber519be662018-12-23 20:03:35 +0100216 unsigned int i = 2;
Justin Chevrier66e554b2015-02-08 21:58:10 +0000217 for (; i < writecnt + 2; i++) {
218 buf[i] = writearr[i - 2];
219 }
220
221 buf[i++] = CMD_CLR_ULOAD_BUFF;
222 buf[i++] = CMD_EXEC_SCRIPT;
223
224 /* Determine script length based on number of bytes to be read or written */
225 if (writecnt == 1 && readcnt == 1)
226 buf[i++] = 7;
227 else if (writecnt == 1 || readcnt == 1)
228 buf[i++] = 10;
229 else
230 buf[i++] = 13;
Elyes HAOUAS0cacb112019-02-04 12:16:38 +0100231
Justin Chevrier66e554b2015-02-08 21:58:10 +0000232 /* Assert CS# */
233 buf[i++] = SCR_VPP_OFF;
234 buf[i++] = SCR_MCLR_GND_ON;
235
236 buf[i++] = SCR_SPI_WRITE_BUF;
237
238 if (writecnt > 1) {
239 buf[i++] = SCR_LOOP;
240 buf[i++] = 1; /* Loop back one instruction */
241 buf[i++] = writecnt - 1; /* Number of times to loop */
242 }
243
244 if (readcnt)
245 buf[i++] = SCR_SPI_READ_BUF;
246
247 if (readcnt > 1) {
248 buf[i++] = SCR_LOOP;
249 buf[i++] = 1; /* Loop back one instruction */
250 buf[i++] = readcnt - 1; /* Number of times to loop */
251 }
252
253 /* De-assert CS# */
254 buf[i++] = SCR_MCLR_GND_OFF;
255 buf[i++] = SCR_VPP_PWM_ON;
256 buf[i++] = SCR_VPP_ON;
257
258 buf[i++] = CMD_UPLOAD_DATA;
259 buf[i++] = CMD_END_OF_BUFFER;
260
Anastasia Klimchuk47e7d472021-04-30 14:10:04 +1000261 int ret = pickit2_interrupt_transfer(pickit2_data->pickit2_handle, ENDPOINT_OUT, buf);
Justin Chevrier66e554b2015-02-08 21:58:10 +0000262
Thomas Heijligenb221cd72019-04-05 15:08:35 +0200263 if (ret != 0) {
264 msg_perr("Send SPI failed!\n");
Justin Chevrier66e554b2015-02-08 21:58:10 +0000265 return 1;
266 }
267
268 if (readcnt) {
Thomas Heijligenb221cd72019-04-05 15:08:35 +0200269 int length = 0;
Anastasia Klimchuk47e7d472021-04-30 14:10:04 +1000270 ret = libusb_interrupt_transfer(pickit2_data->pickit2_handle,
271 ENDPOINT_IN, buf, CMD_LENGTH, &length, DFLT_TIMEOUT);
Justin Chevrier66e554b2015-02-08 21:58:10 +0000272
Thomas Heijligenb221cd72019-04-05 15:08:35 +0200273 if (length == 0 || ret != 0) {
274 msg_perr("Receive SPI failed\n");
Justin Chevrier66e554b2015-02-08 21:58:10 +0000275 return 1;
276 }
277
278 /* First byte indicates number of bytes transferred from upload buffer */
279 if (buf[0] != readcnt) {
280 msg_perr("Unexpected number of bytes transferred, expected %i, got %i!\n",
281 readcnt, ret);
282 return 1;
283 }
Elyes HAOUAS0cacb112019-02-04 12:16:38 +0100284
Justin Chevrier66e554b2015-02-08 21:58:10 +0000285 /* Actual data starts at byte number two */
286 memcpy(readarr, &buf[1], readcnt);
287 }
288
289 return 0;
290}
291
292/* Copied from dediprog.c */
293/* Might be useful for other USB devices as well. static for now. */
294static int parse_voltage(char *voltage)
295{
296 char *tmp = NULL;
297 int i;
298 int millivolt = 0, fraction = 0;
299
300 if (!voltage || !strlen(voltage)) {
301 msg_perr("Empty voltage= specified.\n");
302 return -1;
303 }
304 millivolt = (int)strtol(voltage, &tmp, 0);
305 voltage = tmp;
306 /* Handle "," and "." as decimal point. Everything after it is assumed
307 * to be in decimal notation.
308 */
309 if ((*voltage == '.') || (*voltage == ',')) {
310 voltage++;
311 for (i = 0; i < 3; i++) {
312 fraction *= 10;
313 /* Don't advance if the current character is invalid,
314 * but continue multiplying.
315 */
316 if ((*voltage < '0') || (*voltage > '9'))
317 continue;
318 fraction += *voltage - '0';
319 voltage++;
320 }
321 /* Throw away remaining digits. */
322 voltage += strspn(voltage, "0123456789");
323 }
324 /* The remaining string must be empty or "mV" or "V". */
325 tolower_string(voltage);
326
327 /* No unit or "V". */
328 if ((*voltage == '\0') || !strncmp(voltage, "v", 1)) {
329 millivolt *= 1000;
330 millivolt += fraction;
331 } else if (!strncmp(voltage, "mv", 2) ||
332 !strncmp(voltage, "millivolt", 9)) {
333 /* No adjustment. fraction is discarded. */
334 } else {
335 /* Garbage at the end of the string. */
336 msg_perr("Garbage voltage= specified.\n");
337 return -1;
338 }
339 return millivolt;
340}
341
Anastasia Klimchukc63d9182021-07-06 16:18:44 +1000342static int pickit2_shutdown(void *data);
343
Nico Huber03f3a6d2021-05-11 17:53:34 +0200344static const struct spi_master spi_master_pickit2 = {
Justin Chevrier66e554b2015-02-08 21:58:10 +0000345 .max_data_read = 40,
346 .max_data_write = 40,
347 .command = pickit2_spi_send_command,
348 .multicommand = default_spi_send_multicommand,
349 .read = default_spi_read,
350 .write_256 = default_spi_write_256,
Anastasia Klimchukc63d9182021-07-06 16:18:44 +1000351 .shutdown = pickit2_shutdown,
Aarya Chaumal0cea7532022-07-04 18:21:50 +0530352 .probe_opcode = default_spi_probe_opcode,
Justin Chevrier66e554b2015-02-08 21:58:10 +0000353};
354
355static int pickit2_shutdown(void *data)
356{
Anastasia Klimchuk47e7d472021-04-30 14:10:04 +1000357 struct pickit2_spi_data *pickit2_data = data;
358
Justin Chevrier66e554b2015-02-08 21:58:10 +0000359 /* Set all pins to float and turn voltages off */
360 uint8_t command[CMD_LENGTH] = {
361 CMD_EXEC_SCRIPT,
362 8,
363 SCR_SET_PINS,
364 3, /* Bit-0=1(PDC In), Bit-1=1(PGD In), Bit-2=0(PDC LL), Bit-3=0(PGD LL) */
365 SCR_SET_AUX,
366 1, /* Bit-0=1(Aux In), Bit-1=0(Aux LL) */
367 SCR_MCLR_GND_OFF,
368 SCR_VPP_OFF,
369 SCR_VDD_OFF,
370 SCR_BUSY_LED_OFF,
371 CMD_END_OF_BUFFER
372 };
373
Anastasia Klimchuk47e7d472021-04-30 14:10:04 +1000374 int ret = pickit2_interrupt_transfer(pickit2_data->pickit2_handle, ENDPOINT_OUT, command);
Justin Chevrier66e554b2015-02-08 21:58:10 +0000375
Thomas Heijligenb221cd72019-04-05 15:08:35 +0200376 if (ret != 0) {
377 msg_perr("Command Shutdown failed!\n");
Justin Chevrier66e554b2015-02-08 21:58:10 +0000378 ret = 1;
379 }
Anastasia Klimchuk47e7d472021-04-30 14:10:04 +1000380 if (libusb_release_interface(pickit2_data->pickit2_handle, 0) != 0) {
Justin Chevrier66e554b2015-02-08 21:58:10 +0000381 msg_perr("Could not release USB interface!\n");
382 ret = 1;
383 }
Anastasia Klimchuk47e7d472021-04-30 14:10:04 +1000384 libusb_close(pickit2_data->pickit2_handle);
Thomas Heijligenb221cd72019-04-05 15:08:35 +0200385 libusb_exit(NULL);
Anastasia Klimchuk47e7d472021-04-30 14:10:04 +1000386
387 free(data);
Justin Chevrier66e554b2015-02-08 21:58:10 +0000388 return ret;
389}
390
Nico Hubere3a26882023-01-11 21:45:51 +0100391static int pickit2_spi_init(struct flashprog_programmer *const prog)
Justin Chevrier66e554b2015-02-08 21:58:10 +0000392{
Justin Chevrier66e554b2015-02-08 21:58:10 +0000393 uint8_t buf[CMD_LENGTH] = {
394 CMD_EXEC_SCRIPT,
395 10, /* Script length */
396 SCR_SET_PINS,
397 2, /* Bit-0=0(PDC Out), Bit-1=1(PGD In), Bit-2=0(PDC LL), Bit-3=0(PGD LL) */
398 SCR_SET_AUX,
399 0, /* Bit-0=0(Aux Out), Bit-1=0(Aux LL) */
400 SCR_VDD_ON,
401 SCR_MCLR_GND_OFF, /* Let CS# float */
402 SCR_VPP_PWM_ON,
403 SCR_VPP_ON, /* Pull CS# high */
404 SCR_BUSY_LED_ON,
405 CMD_CLR_DLOAD_BUFF,
406 CMD_CLR_ULOAD_BUFF,
407 CMD_END_OF_BUFFER
408 };
409
Anastasia Klimchuk47e7d472021-04-30 14:10:04 +1000410 libusb_device_handle *pickit2_handle;
411 struct pickit2_spi_data *pickit2_data;
Justin Chevrier66e554b2015-02-08 21:58:10 +0000412 int spispeed_idx = 0;
413 char *spispeed = extract_programmer_param("spispeed");
414 if (spispeed != NULL) {
415 int i = 0;
416 for (; spispeeds[i].name; i++) {
417 if (strcasecmp(spispeeds[i].name, spispeed) == 0) {
418 spispeed_idx = i;
419 break;
420 }
421 }
422 if (spispeeds[i].name == NULL) {
423 msg_perr("Error: Invalid 'spispeed' value.\n");
424 free(spispeed);
425 return 1;
426 }
427 free(spispeed);
428 }
429
430 int millivolt = 3500;
431 char *voltage = extract_programmer_param("voltage");
432 if (voltage != NULL) {
433 millivolt = parse_voltage(voltage);
434 free(voltage);
435 if (millivolt < 0)
436 return 1;
437 }
438
Thomas Heijligenb221cd72019-04-05 15:08:35 +0200439 if (libusb_init(NULL) < 0) {
440 msg_perr("Couldn't initialize libusb!\n");
441 return -1;
442 }
443
444#if LIBUSB_API_VERSION < 0x01000106
445 libusb_set_debug(NULL, 3);
446#else
447 libusb_set_option(NULL, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_INFO);
448#endif
449
Stefan Taunerf31fe842016-02-22 08:59:15 +0000450 const uint16_t vid = devs_pickit2_spi[0].vendor_id;
451 const uint16_t pid = devs_pickit2_spi[0].device_id;
Thomas Heijligenb221cd72019-04-05 15:08:35 +0200452 pickit2_handle = libusb_open_device_with_vid_pid(NULL, vid, pid);
453 if (pickit2_handle == NULL) {
454 msg_perr("Could not open device PICkit2!\n");
455 libusb_exit(NULL);
Justin Chevrier66e554b2015-02-08 21:58:10 +0000456 return 1;
457 }
Justin Chevrier66e554b2015-02-08 21:58:10 +0000458
Thomas Heijligenb221cd72019-04-05 15:08:35 +0200459 if (libusb_set_configuration(pickit2_handle, 1) != 0) {
460 msg_perr("Could not set USB device configuration.\n");
461 libusb_close(pickit2_handle);
462 libusb_exit(NULL);
Justin Chevrier66e554b2015-02-08 21:58:10 +0000463 return 1;
464 }
Thomas Heijligenb221cd72019-04-05 15:08:35 +0200465 if (libusb_claim_interface(pickit2_handle, 0) != 0) {
466 msg_perr("Could not claim USB device interface\n");
467 libusb_close(pickit2_handle);
468 libusb_exit(NULL);
Justin Chevrier66e554b2015-02-08 21:58:10 +0000469 return 1;
470 }
471
Anastasia Klimchuk47e7d472021-04-30 14:10:04 +1000472 pickit2_data = calloc(1, sizeof(*pickit2_data));
473 if (!pickit2_data) {
474 msg_perr("Unable to allocate space for SPI master data\n");
475 libusb_close(pickit2_handle);
476 libusb_exit(NULL);
477 return 1;
478 }
479 pickit2_data->pickit2_handle = pickit2_handle;
Anastasia Klimchuk47e7d472021-04-30 14:10:04 +1000480
481 if (pickit2_get_firmware_version(pickit2_handle))
Anastasia Klimchuk1c9c73a2021-04-30 11:25:44 +1000482 goto init_err_cleanup_exit;
Justin Chevrier66e554b2015-02-08 21:58:10 +0000483
484 /* Command Set SPI Speed */
Anastasia Klimchuk47e7d472021-04-30 14:10:04 +1000485 if (pickit2_set_spi_speed(pickit2_handle, spispeed_idx))
Anastasia Klimchuk1c9c73a2021-04-30 11:25:44 +1000486 goto init_err_cleanup_exit;
Justin Chevrier66e554b2015-02-08 21:58:10 +0000487
488 /* Command Set SPI Voltage */
489 msg_pdbg("Setting voltage to %i mV.\n", millivolt);
Anastasia Klimchuk47e7d472021-04-30 14:10:04 +1000490 if (pickit2_set_spi_voltage(pickit2_handle, millivolt) != 0)
Anastasia Klimchuk1c9c73a2021-04-30 11:25:44 +1000491 goto init_err_cleanup_exit;
Justin Chevrier66e554b2015-02-08 21:58:10 +0000492
493 /* Perform basic setup.
494 * Configure pin directions and logic levels, turn Vdd on, turn busy LED on and clear buffers. */
Angel Pons75d6ec62022-07-16 12:12:50 +0200495 if (pickit2_interrupt_transfer(pickit2_handle, ENDPOINT_OUT, buf) != 0) {
Thomas Heijligenb221cd72019-04-05 15:08:35 +0200496 msg_perr("Command Setup failed!\n");
Anastasia Klimchuk1c9c73a2021-04-30 11:25:44 +1000497 goto init_err_cleanup_exit;
Justin Chevrier66e554b2015-02-08 21:58:10 +0000498 }
499
Nico Huber89569d62023-01-12 23:31:40 +0100500 return register_spi_master(&spi_master_pickit2, 0, pickit2_data);
Anastasia Klimchuk1c9c73a2021-04-30 11:25:44 +1000501
502init_err_cleanup_exit:
Anastasia Klimchuk47e7d472021-04-30 14:10:04 +1000503 pickit2_shutdown(pickit2_data);
Anastasia Klimchuk1c9c73a2021-04-30 11:25:44 +1000504 return 1;
Justin Chevrier66e554b2015-02-08 21:58:10 +0000505}
Thomas Heijligencc853d82021-05-04 15:32:17 +0200506
507const struct programmer_entry programmer_pickit2_spi = {
508 .name = "pickit2_spi",
509 .type = USB,
510 .devs.dev = devs_pickit2_spi,
511 .init = pickit2_spi_init,
Thomas Heijligencc853d82021-05-04 15:32:17 +0200512};