Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 4 | * Copyright (C) 2009, 2010, 2011, 2012 Carl-Daniel Hailfinger |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 5 | * |
| 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; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include <stdio.h> |
Carl-Daniel Hailfinger | 1c6d2ff | 2012-08-27 00:44:42 +0000 | [diff] [blame] | 17 | #include <strings.h> |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 18 | #include <string.h> |
Felix Singer | cf6b708 | 2022-08-18 23:57:31 +0200 | [diff] [blame] | 19 | #include <stdbool.h> |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 20 | #include <stdlib.h> |
| 21 | #include <ctype.h> |
Carl-Daniel Hailfinger | d2f007f | 2010-09-16 22:34:25 +0000 | [diff] [blame] | 22 | #include <unistd.h> |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 23 | #include "flash.h" |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 24 | #include "programmer.h" |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 25 | #include "spi.h" |
| 26 | |
| 27 | /* Change this to #define if you want to test without a serial implementation */ |
| 28 | #undef FAKE_COMMUNICATION |
| 29 | |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 30 | struct buspirate_speeds { |
Carl-Daniel Hailfinger | c422484 | 2011-06-09 20:06:34 +0000 | [diff] [blame] | 31 | const char *name; |
| 32 | const int speed; |
| 33 | }; |
| 34 | |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 35 | #define BP_DEFAULTBAUD 115200 |
| 36 | |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 37 | #ifndef FAKE_COMMUNICATION |
Carl-Daniel Hailfinger | ad3cc55 | 2010-07-03 11:02:10 +0000 | [diff] [blame] | 38 | static int buspirate_serialport_setup(char *dev) |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 39 | { |
| 40 | /* 115200bps, 8 databits, no parity, 1 stopbit */ |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 41 | sp_fd = sp_openserport(dev, BP_DEFAULTBAUD); |
Elyes HAOUAS | 124ef38 | 2018-03-27 12:15:09 +0200 | [diff] [blame] | 42 | if (sp_fd == SER_INV_FD) |
Niklas Söderlund | 2a95e87 | 2012-07-30 19:42:33 +0000 | [diff] [blame] | 43 | return 1; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 44 | return 0; |
| 45 | } |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 46 | #else |
| 47 | #define buspirate_serialport_setup(...) 0 |
Carl-Daniel Hailfinger | efa151e | 2010-01-06 16:09:10 +0000 | [diff] [blame] | 48 | #define serialport_shutdown(...) 0 |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 49 | #define serialport_write(...) 0 |
| 50 | #define serialport_read(...) 0 |
Patrick Georgi | 3b6237d | 2010-01-06 19:09:40 +0000 | [diff] [blame] | 51 | #define sp_flush_incoming(...) 0 |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 52 | #endif |
| 53 | |
Anastasia Klimchuk | d8f8c99 | 2021-05-06 07:47:04 +1000 | [diff] [blame] | 54 | struct bp_spi_data { |
Anastasia Klimchuk | 346d54d | 2021-06-10 09:57:51 +1000 | [diff] [blame] | 55 | unsigned char *commbuf; |
| 56 | int commbufsize; |
Anastasia Klimchuk | d8f8c99 | 2021-05-06 07:47:04 +1000 | [diff] [blame] | 57 | }; |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 58 | |
Anastasia Klimchuk | d8f8c99 | 2021-05-06 07:47:04 +1000 | [diff] [blame] | 59 | static int buspirate_commbuf_grow(int bufsize, unsigned char **bp_commbuf, int *bp_commbufsize) |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 60 | { |
| 61 | unsigned char *tmpbuf; |
| 62 | |
| 63 | /* Never shrink. realloc() calls are expensive. */ |
Anastasia Klimchuk | d8f8c99 | 2021-05-06 07:47:04 +1000 | [diff] [blame] | 64 | if (bufsize <= *bp_commbufsize) |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 65 | return 0; |
| 66 | |
Anastasia Klimchuk | d8f8c99 | 2021-05-06 07:47:04 +1000 | [diff] [blame] | 67 | tmpbuf = realloc(*bp_commbuf, bufsize); |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 68 | if (!tmpbuf) { |
| 69 | /* Keep the existing buffer because memory is already tight. */ |
| 70 | msg_perr("Out of memory!\n"); |
| 71 | return ERROR_OOM; |
| 72 | } |
| 73 | |
Anastasia Klimchuk | d8f8c99 | 2021-05-06 07:47:04 +1000 | [diff] [blame] | 74 | *bp_commbuf = tmpbuf; |
| 75 | *bp_commbufsize = bufsize; |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 76 | return 0; |
| 77 | } |
| 78 | |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 79 | static int buspirate_sendrecv(unsigned char *buf, unsigned int writecnt, |
| 80 | unsigned int readcnt) |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 81 | { |
Nico Huber | 519be66 | 2018-12-23 20:03:35 +0100 | [diff] [blame] | 82 | unsigned int i; |
| 83 | int ret = 0; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 84 | |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 85 | msg_pspew("%s: write %i, read %i ", __func__, writecnt, readcnt); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 86 | if (!writecnt && !readcnt) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 87 | msg_perr("Zero length command!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 88 | return 1; |
| 89 | } |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 90 | if (writecnt) |
| 91 | msg_pspew("Sending"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 92 | for (i = 0; i < writecnt; i++) |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 93 | msg_pspew(" 0x%02x", buf[i]); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 94 | #ifdef FAKE_COMMUNICATION |
| 95 | /* Placate the caller for now. */ |
| 96 | if (readcnt) { |
| 97 | buf[0] = 0x01; |
| 98 | memset(buf + 1, 0xff, readcnt - 1); |
| 99 | } |
| 100 | ret = 0; |
| 101 | #else |
| 102 | if (writecnt) |
| 103 | ret = serialport_write(buf, writecnt); |
| 104 | if (ret) |
| 105 | return ret; |
| 106 | if (readcnt) |
| 107 | ret = serialport_read(buf, readcnt); |
| 108 | if (ret) |
| 109 | return ret; |
| 110 | #endif |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 111 | if (readcnt) |
| 112 | msg_pspew(", receiving"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 113 | for (i = 0; i < readcnt; i++) |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 114 | msg_pspew(" 0x%02x", buf[i]); |
| 115 | msg_pspew("\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 116 | return 0; |
| 117 | } |
| 118 | |
Jacob Garber | 4a84ec2 | 2019-07-25 19:12:31 -0600 | [diff] [blame] | 119 | static int buspirate_wait_for_string(unsigned char *buf, const char *key) |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 120 | { |
| 121 | unsigned int keylen = strlen(key); |
| 122 | int ret; |
| 123 | |
| 124 | ret = buspirate_sendrecv(buf, 0, keylen); |
| 125 | while (!ret) { |
| 126 | if (!memcmp(buf, key, keylen)) |
| 127 | return 0; |
| 128 | memmove(buf, buf + 1, keylen - 1); |
| 129 | ret = buspirate_sendrecv(buf + keylen - 1, 0, 1); |
| 130 | } |
| 131 | return ret; |
| 132 | } |
| 133 | |
Edward O'Callaghan | 5eca427 | 2020-04-12 17:27:53 +1000 | [diff] [blame] | 134 | static int buspirate_spi_send_command_v1(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt, |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 135 | const unsigned char *writearr, unsigned char *readarr); |
Edward O'Callaghan | 5eca427 | 2020-04-12 17:27:53 +1000 | [diff] [blame] | 136 | static int buspirate_spi_send_command_v2(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt, |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 137 | const unsigned char *writearr, unsigned char *readarr); |
Anastasia Klimchuk | c63d918 | 2021-07-06 16:18:44 +1000 | [diff] [blame] | 138 | static int buspirate_spi_shutdown(void *data); |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 139 | |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 140 | static struct spi_master spi_master_buspirate = { |
Nico Huber | 1cf407b | 2017-11-10 20:18:23 +0100 | [diff] [blame] | 141 | .features = SPI_MASTER_4BA, |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 142 | .max_data_read = MAX_DATA_UNSPECIFIED, |
| 143 | .max_data_write = MAX_DATA_UNSPECIFIED, |
| 144 | .command = NULL, |
Uwe Hermann | 91f4afa | 2011-07-28 08:13:25 +0000 | [diff] [blame] | 145 | .multicommand = default_spi_send_multicommand, |
| 146 | .read = default_spi_read, |
| 147 | .write_256 = default_spi_write_256, |
Anastasia Klimchuk | c63d918 | 2021-07-06 16:18:44 +1000 | [diff] [blame] | 148 | .shutdown = buspirate_spi_shutdown, |
Aarya Chaumal | 0cea753 | 2022-07-04 18:21:50 +0530 | [diff] [blame] | 149 | .probe_opcode = default_spi_probe_opcode, |
Michael Karcher | b9dbe48 | 2011-05-11 17:07:07 +0000 | [diff] [blame] | 150 | }; |
| 151 | |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 152 | static const struct buspirate_speeds spispeeds[] = { |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 153 | {"30k", 0x0}, |
| 154 | {"125k", 0x1}, |
| 155 | {"250k", 0x2}, |
| 156 | {"1M", 0x3}, |
| 157 | {"2M", 0x4}, |
| 158 | {"2.6M", 0x5}, |
| 159 | {"4M", 0x6}, |
| 160 | {"8M", 0x7}, |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 161 | {NULL, 0x0} |
| 162 | }; |
| 163 | |
| 164 | static const struct buspirate_speeds serialspeeds[] = { |
| 165 | {"115200", 115200}, |
| 166 | {"230400", 230400}, |
| 167 | {"250000", 250000}, |
| 168 | {"2000000", 2000000}, |
| 169 | {"2M", 2000000}, |
| 170 | {NULL, 0} |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 171 | }; |
| 172 | |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 173 | static int buspirate_spi_shutdown(void *data) |
| 174 | { |
Anastasia Klimchuk | d8f8c99 | 2021-05-06 07:47:04 +1000 | [diff] [blame] | 175 | struct bp_spi_data *bp_data = data; |
Anastasia Klimchuk | 346d54d | 2021-06-10 09:57:51 +1000 | [diff] [blame] | 176 | unsigned char *const bp_commbuf = bp_data->commbuf; |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 177 | int ret = 0, ret2 = 0; |
| 178 | /* No need to allocate a buffer here, we know that bp_commbuf is at least DEFAULT_BUFSIZE big. */ |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 179 | |
| 180 | /* Exit raw SPI mode (enter raw bitbang mode) */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 181 | bp_commbuf[0] = 0x00; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 182 | if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0))) |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 183 | goto out_shutdown; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 184 | if ((ret = buspirate_wait_for_string(bp_commbuf, "BBIO"))) |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 185 | goto out_shutdown; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 186 | if ((ret = buspirate_sendrecv(bp_commbuf, 0, 1))) |
| 187 | goto out_shutdown; |
| 188 | msg_pdbg("Raw bitbang mode version %c\n", bp_commbuf[0]); |
| 189 | if (bp_commbuf[0] != '1') { |
| 190 | msg_perr("Can't handle raw bitbang mode version %c!\n", bp_commbuf[0]); |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 191 | ret = 1; |
| 192 | goto out_shutdown; |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 193 | } |
| 194 | /* Reset Bus Pirate (return to user terminal) */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 195 | bp_commbuf[0] = 0x0f; |
| 196 | ret = buspirate_sendrecv(bp_commbuf, 1, 0); |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 197 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 198 | out_shutdown: |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 199 | /* Shut down serial port communication */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 200 | ret2 = serialport_shutdown(NULL); |
| 201 | /* Keep the oldest error, it is probably the best indicator. */ |
| 202 | if (ret2 && !ret) |
| 203 | ret = ret2; |
Anastasia Klimchuk | d8f8c99 | 2021-05-06 07:47:04 +1000 | [diff] [blame] | 204 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 205 | free(bp_commbuf); |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 206 | if (ret) |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 207 | msg_pdbg("Bus Pirate shutdown failed.\n"); |
| 208 | else |
| 209 | msg_pdbg("Bus Pirate shutdown completed.\n"); |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 210 | |
Anastasia Klimchuk | d8f8c99 | 2021-05-06 07:47:04 +1000 | [diff] [blame] | 211 | free(data); |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 212 | return ret; |
David Hendricks | 8bb2021 | 2011-06-14 01:35:36 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 215 | #define BP_FWVERSION(a,b) ((a) << 8 | (b)) |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 216 | #define BP_HWVERSION(a,b) BP_FWVERSION(a,b) |
| 217 | |
| 218 | /** |
| 219 | * The Bus Pirate's PIC microcontroller supports custom baud rates by manually specifying a |
| 220 | * clock divisor that can be calculated with the formula (16000000 / (4 * baud)) - 1. |
| 221 | */ |
| 222 | #define BP_DIVISOR(baud) ((4000000/(baud)) - 1) |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 223 | |
Nico Huber | e3a2688 | 2023-01-11 21:45:51 +0100 | [diff] [blame] | 224 | static int buspirate_spi_init(struct flashprog_programmer *const prog) |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 225 | { |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 226 | char *tmp; |
Brian Salcedo | 30dfdba | 2013-01-03 20:44:30 +0000 | [diff] [blame] | 227 | char *dev; |
| 228 | int i; |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 229 | int cnt; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 230 | unsigned int fw_version_major = 0; |
| 231 | unsigned int fw_version_minor = 0; |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 232 | unsigned int hw_version_major = 0; |
| 233 | unsigned int hw_version_minor = 0; |
Carl-Daniel Hailfinger | 082c8b5 | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 234 | int spispeed = 0x7; |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 235 | int serialspeed_index = -1; |
Carl-Daniel Hailfinger | 082c8b5 | 2011-08-15 19:54:20 +0000 | [diff] [blame] | 236 | int ret = 0; |
Felix Singer | cf6b708 | 2022-08-18 23:57:31 +0200 | [diff] [blame] | 237 | bool pullup = false; |
| 238 | bool psu = false; |
Anastasia Klimchuk | d8f8c99 | 2021-05-06 07:47:04 +1000 | [diff] [blame] | 239 | unsigned char *bp_commbuf; |
| 240 | int bp_commbufsize; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 241 | |
Carl-Daniel Hailfinger | 2b6dcb3 | 2010-07-08 10:13:37 +0000 | [diff] [blame] | 242 | dev = extract_programmer_param("dev"); |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 243 | if (dev && !strlen(dev)) { |
| 244 | free(dev); |
| 245 | dev = NULL; |
| 246 | } |
| 247 | if (!dev) { |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 248 | msg_perr("No serial device given. Use flashprog -p buspirate_spi:dev=/dev/ttyUSB0\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 249 | return 1; |
| 250 | } |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 251 | |
Brian Salcedo | 30dfdba | 2013-01-03 20:44:30 +0000 | [diff] [blame] | 252 | tmp = extract_programmer_param("spispeed"); |
| 253 | if (tmp) { |
| 254 | for (i = 0; spispeeds[i].name; i++) { |
| 255 | if (!strncasecmp(spispeeds[i].name, tmp, strlen(spispeeds[i].name))) { |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 256 | spispeed = spispeeds[i].speed; |
| 257 | break; |
| 258 | } |
Brian Salcedo | 30dfdba | 2013-01-03 20:44:30 +0000 | [diff] [blame] | 259 | } |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 260 | if (!spispeeds[i].name) |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 261 | msg_perr("Invalid SPI speed, using default.\n"); |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 262 | } |
Brian Salcedo | 30dfdba | 2013-01-03 20:44:30 +0000 | [diff] [blame] | 263 | free(tmp); |
| 264 | |
Elyes HAOUAS | e2c90c4 | 2018-08-18 09:04:41 +0200 | [diff] [blame] | 265 | /* Extract serialspeed parameter */ |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 266 | tmp = extract_programmer_param("serialspeed"); |
| 267 | if (tmp) { |
| 268 | for (i = 0; serialspeeds[i].name; i++) { |
| 269 | if (!strncasecmp(serialspeeds[i].name, tmp, strlen(serialspeeds[i].name))) { |
| 270 | serialspeed_index = i; |
| 271 | break; |
| 272 | } |
| 273 | } |
| 274 | if (!serialspeeds[i].name) |
| 275 | msg_perr("Invalid serial speed %s, using default.\n", tmp); |
| 276 | } |
| 277 | free(tmp); |
| 278 | |
Brian Salcedo | 30dfdba | 2013-01-03 20:44:30 +0000 | [diff] [blame] | 279 | tmp = extract_programmer_param("pullups"); |
| 280 | if (tmp) { |
| 281 | if (strcasecmp("on", tmp) == 0) |
Felix Singer | cf6b708 | 2022-08-18 23:57:31 +0200 | [diff] [blame] | 282 | pullup = true; |
Brian Salcedo | 30dfdba | 2013-01-03 20:44:30 +0000 | [diff] [blame] | 283 | else if (strcasecmp("off", tmp) == 0) |
| 284 | ; // ignore |
| 285 | else |
| 286 | msg_perr("Invalid pullups state, not using them.\n"); |
| 287 | } |
| 288 | free(tmp); |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 289 | |
Jeremy Kerr | 98bdcb4 | 2021-05-23 17:58:06 +0800 | [diff] [blame] | 290 | tmp = extract_programmer_param("psus"); |
| 291 | if (tmp) { |
| 292 | if (strcasecmp("on", tmp) == 0) |
Felix Singer | cf6b708 | 2022-08-18 23:57:31 +0200 | [diff] [blame] | 293 | psu = true; |
Jeremy Kerr | 98bdcb4 | 2021-05-23 17:58:06 +0800 | [diff] [blame] | 294 | else if (strcasecmp("off", tmp) == 0) |
| 295 | ; // ignore |
| 296 | else |
| 297 | msg_perr("Invalid psus state, not enabling.\n"); |
| 298 | } |
| 299 | free(tmp); |
| 300 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 301 | /* Default buffer size is 19: 16 bytes data, 3 bytes control. */ |
| 302 | #define DEFAULT_BUFSIZE (16 + 3) |
| 303 | bp_commbuf = malloc(DEFAULT_BUFSIZE); |
| 304 | if (!bp_commbuf) { |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 305 | msg_perr("Out of memory!\n"); |
Stefan Reinauer | 1838591 | 2014-04-26 16:12:45 +0000 | [diff] [blame] | 306 | free(dev); |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 307 | return ERROR_OOM; |
| 308 | } |
| 309 | bp_commbufsize = DEFAULT_BUFSIZE; |
| 310 | |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 311 | ret = buspirate_serialport_setup(dev); |
Carl-Daniel Hailfinger | 744132a | 2010-07-06 09:55:48 +0000 | [diff] [blame] | 312 | free(dev); |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 313 | if (ret) { |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 314 | free(bp_commbuf); |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 315 | return ret; |
| 316 | } |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 317 | |
Anastasia Klimchuk | d8f8c99 | 2021-05-06 07:47:04 +1000 | [diff] [blame] | 318 | |
| 319 | struct bp_spi_data *bp_data = calloc(1, sizeof(*bp_data)); |
| 320 | if (!bp_data) { |
| 321 | msg_perr("Unable to allocate space for SPI master data\n"); |
| 322 | free(bp_commbuf); |
| 323 | return 1; |
| 324 | } |
Anastasia Klimchuk | 346d54d | 2021-06-10 09:57:51 +1000 | [diff] [blame] | 325 | bp_data->commbuf = bp_commbuf; |
| 326 | bp_data->commbufsize = bp_commbufsize; |
Anastasia Klimchuk | d8f8c99 | 2021-05-06 07:47:04 +1000 | [diff] [blame] | 327 | |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 328 | /* This is the brute force version, but it should work. |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 329 | * It is likely to fail if a previous flashprog run was aborted during a write with the new SPI commands |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 330 | * in firmware v5.5 because that firmware may wait for up to 4096 bytes of input before responding to |
| 331 | * 0x00 again. The obvious workaround (sending 4096 bytes of \0) may cause significant startup delays. |
| 332 | */ |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 333 | for (i = 0; i < 20; i++) { |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 334 | /* Enter raw bitbang mode */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 335 | bp_commbuf[0] = 0x00; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 336 | /* Send the command, don't read the response. */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 337 | ret = buspirate_sendrecv(bp_commbuf, 1, 0); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 338 | if (ret) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 339 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 340 | /* The old way to handle responses from a Bus Pirate already in BBIO mode was to flush any |
| 341 | * response which came in over serial. Unfortunately that does not work reliably on Linux |
| 342 | * with FTDI USB-serial. |
| 343 | */ |
| 344 | //sp_flush_incoming(); |
| 345 | /* The Bus Pirate can't handle UART input buffer overflow in BBIO mode, and sending a sequence |
| 346 | * of 0x00 too fast apparently triggers such an UART input buffer overflow. |
| 347 | */ |
Maksim Kuleshov | 73dc0db | 2013-04-05 08:06:10 +0000 | [diff] [blame] | 348 | internal_sleep(10000); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 349 | } |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 350 | /* We know that 20 commands of \0 should elicit at least one BBIO1 response. */ |
| 351 | if ((ret = buspirate_wait_for_string(bp_commbuf, "BBIO"))) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 352 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 353 | |
| 354 | /* Reset the Bus Pirate. */ |
| 355 | bp_commbuf[0] = 0x0f; |
| 356 | /* Send the command, don't read the response. */ |
| 357 | if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0))) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 358 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 359 | if ((ret = buspirate_wait_for_string(bp_commbuf, "irate "))) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 360 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 361 | /* Read the hardware version string. Last byte of the buffer is reserved for \0. */ |
| 362 | for (i = 0; i < DEFAULT_BUFSIZE - 1; i++) { |
| 363 | if ((ret = buspirate_sendrecv(bp_commbuf + i, 0, 1))) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 364 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 365 | if (strchr("\r\n\t ", bp_commbuf[i])) |
| 366 | break; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 367 | } |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 368 | bp_commbuf[i] = '\0'; |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 369 | msg_pdbg("Detected Bus Pirate hardware "); |
| 370 | if (bp_commbuf[0] != 'v') |
| 371 | msg_pdbg("(unknown version number format)"); |
| 372 | else if (!strchr("0123456789", bp_commbuf[1])) |
| 373 | msg_pdbg("(unknown version number format)"); |
| 374 | else { |
| 375 | hw_version_major = strtoul((char *)bp_commbuf + 1, &tmp, 10); |
| 376 | while ((*tmp != '\0') && !strchr("0123456789", *tmp)) |
| 377 | tmp++; |
| 378 | hw_version_minor = strtoul(tmp, NULL, 10); |
| 379 | msg_pdbg("%u.%u", hw_version_major, hw_version_minor); |
| 380 | } |
| 381 | msg_pdbg2(" (\"%s\")", bp_commbuf); |
| 382 | msg_pdbg("\n"); |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 383 | |
| 384 | if ((ret = buspirate_wait_for_string(bp_commbuf, "irmware "))) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 385 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 386 | /* Read the firmware version string. Last byte of the buffer is reserved for \0. */ |
| 387 | for (i = 0; i < DEFAULT_BUFSIZE - 1; i++) { |
| 388 | if ((ret = buspirate_sendrecv(bp_commbuf + i, 0, 1))) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 389 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 390 | if (strchr("\r\n\t ", bp_commbuf[i])) |
| 391 | break; |
| 392 | } |
| 393 | bp_commbuf[i] = '\0'; |
| 394 | msg_pdbg("Detected Bus Pirate firmware "); |
| 395 | if (bp_commbuf[0] != 'v') |
| 396 | msg_pdbg("(unknown version number format)"); |
| 397 | else if (!strchr("0123456789", bp_commbuf[1])) |
| 398 | msg_pdbg("(unknown version number format)"); |
| 399 | else { |
| 400 | fw_version_major = strtoul((char *)bp_commbuf + 1, &tmp, 10); |
| 401 | while ((*tmp != '\0') && !strchr("0123456789", *tmp)) |
| 402 | tmp++; |
| 403 | fw_version_minor = strtoul(tmp, NULL, 10); |
| 404 | msg_pdbg("%u.%u", fw_version_major, fw_version_minor); |
| 405 | } |
| 406 | msg_pdbg2(" (\"%s\")", bp_commbuf); |
| 407 | msg_pdbg("\n"); |
| 408 | |
| 409 | if ((ret = buspirate_wait_for_string(bp_commbuf, "HiZ>"))) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 410 | goto init_err_cleanup_exit; |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 411 | |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 412 | /* Tell the user about missing SPI binary mode in firmware 2.3 and older. */ |
| 413 | if (BP_FWVERSION(fw_version_major, fw_version_minor) < BP_FWVERSION(2, 4)) { |
| 414 | msg_pinfo("Bus Pirate firmware 2.3 and older does not support binary SPI access.\n"); |
| 415 | msg_pinfo("Please upgrade to the latest firmware (at least 2.4).\n"); |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 416 | ret = SPI_PROGRAMMER_ERROR; |
| 417 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 420 | /* Use fast SPI mode in firmware 5.5 and newer. */ |
| 421 | if (BP_FWVERSION(fw_version_major, fw_version_minor) >= BP_FWVERSION(5, 5)) { |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 422 | msg_pdbg("Using SPI command set v2.\n"); |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 423 | /* Sensible default buffer size. */ |
Anastasia Klimchuk | d8f8c99 | 2021-05-06 07:47:04 +1000 | [diff] [blame] | 424 | if (buspirate_commbuf_grow(260 + 5, &bp_commbuf, &bp_commbufsize)) { |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 425 | ret = ERROR_OOM; |
| 426 | goto init_err_cleanup_exit; |
| 427 | } |
Anastasia Klimchuk | 346d54d | 2021-06-10 09:57:51 +1000 | [diff] [blame] | 428 | bp_data->commbuf = bp_commbuf; |
| 429 | bp_data->commbufsize = bp_commbufsize; |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 430 | spi_master_buspirate.max_data_read = 2048; |
| 431 | spi_master_buspirate.max_data_write = 256; |
| 432 | spi_master_buspirate.command = buspirate_spi_send_command_v2; |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 433 | } else { |
| 434 | msg_pinfo("Bus Pirate firmware 5.4 and older does not support fast SPI access.\n"); |
| 435 | msg_pinfo("Reading/writing a flash chip may take hours.\n"); |
| 436 | msg_pinfo("It is recommended to upgrade to firmware 5.5 or newer.\n"); |
| 437 | /* Sensible default buffer size. */ |
Anastasia Klimchuk | d8f8c99 | 2021-05-06 07:47:04 +1000 | [diff] [blame] | 438 | if (buspirate_commbuf_grow(16 + 3, &bp_commbuf, &bp_commbufsize)) { |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 439 | ret = ERROR_OOM; |
| 440 | goto init_err_cleanup_exit; |
| 441 | } |
Anastasia Klimchuk | 346d54d | 2021-06-10 09:57:51 +1000 | [diff] [blame] | 442 | bp_data->commbuf = bp_commbuf; |
| 443 | bp_data->commbufsize = bp_commbufsize; |
Carl-Daniel Hailfinger | a5bcbce | 2014-07-19 22:03:29 +0000 | [diff] [blame] | 444 | spi_master_buspirate.max_data_read = 12; |
| 445 | spi_master_buspirate.max_data_write = 12; |
| 446 | spi_master_buspirate.command = buspirate_spi_send_command_v1; |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 449 | /* Workaround for broken speed settings in firmware 6.1 and older. */ |
| 450 | if (BP_FWVERSION(fw_version_major, fw_version_minor) < BP_FWVERSION(6, 2)) |
| 451 | if (spispeed > 0x4) { |
| 452 | msg_perr("Bus Pirate firmware 6.1 and older does not support SPI speeds above 2 MHz. " |
| 453 | "Limiting speed to 2 MHz.\n"); |
| 454 | msg_pinfo("It is recommended to upgrade to firmware 6.2 or newer.\n"); |
| 455 | spispeed = 0x4; |
| 456 | } |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 457 | |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 458 | /* This works because speeds numbering starts at 0 and is contiguous. */ |
| 459 | msg_pdbg("SPI speed is %sHz\n", spispeeds[spispeed].name); |
| 460 | |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 461 | /* Set 2M baud serial speed by default on hardware 3.0 and newer if a custom speed was not set */ |
| 462 | if (serialspeed_index == -1 && BP_HWVERSION(hw_version_major, hw_version_minor) >= BP_HWVERSION(3, 0)) { |
| 463 | serialspeed_index = ARRAY_SIZE(serialspeeds) - 2; |
| 464 | msg_pdbg("Bus Pirate v3 or newer detected. Set serial speed to 2M baud.\n"); |
| 465 | } |
| 466 | |
| 467 | /* Set custom serial speed if specified */ |
| 468 | if (serialspeed_index != -1) { |
| 469 | if (BP_FWVERSION(fw_version_major, fw_version_minor) < BP_FWVERSION(5, 5)) { |
| 470 | /* This feature requires firmware 5.5 or newer */ |
| 471 | msg_perr("Bus Pirate firmware 5.4 and older does not support custom serial speeds." |
| 472 | "Using default speed of 115200 baud.\n"); |
| 473 | } else if (serialspeeds[serialspeed_index].speed != BP_DEFAULTBAUD) { |
| 474 | /* Set the serial speed to match the user's choice if it doesn't already */ |
| 475 | |
| 476 | if (BP_HWVERSION(hw_version_major, hw_version_minor) < BP_HWVERSION(3, 0)) |
| 477 | msg_pwarn("Increased serial speeds may not work on older (<3.0) Bus Pirates." |
| 478 | " Continue at your own risk.\n"); |
| 479 | |
| 480 | /* Enter baud rate configuration mode */ |
| 481 | cnt = snprintf((char *)bp_commbuf, DEFAULT_BUFSIZE, "b\n"); |
| 482 | if ((ret = buspirate_sendrecv(bp_commbuf, cnt, 0))) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 483 | goto init_err_cleanup_exit; |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 484 | if ((ret = buspirate_wait_for_string(bp_commbuf, ">"))) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 485 | goto init_err_cleanup_exit; |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 486 | |
| 487 | /* Enter manual clock divisor entry mode */ |
| 488 | cnt = snprintf((char *)bp_commbuf, DEFAULT_BUFSIZE, "10\n"); |
| 489 | if ((ret = buspirate_sendrecv(bp_commbuf, cnt, 0))) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 490 | goto init_err_cleanup_exit; |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 491 | if ((ret = buspirate_wait_for_string(bp_commbuf, ">"))) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 492 | goto init_err_cleanup_exit; |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 493 | |
| 494 | /* Set the clock divisor to the value calculated from the user's input */ |
| 495 | cnt = snprintf((char *)bp_commbuf, DEFAULT_BUFSIZE, "%d\n", |
| 496 | BP_DIVISOR(serialspeeds[serialspeed_index].speed)); |
| 497 | |
| 498 | if ((ret = buspirate_sendrecv(bp_commbuf, cnt, 0))) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 499 | goto init_err_cleanup_exit; |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 500 | sleep(1); |
| 501 | |
| 502 | /* Reconfigure the host's serial baud rate to the new value */ |
| 503 | if ((ret = serialport_config(sp_fd, serialspeeds[serialspeed_index].speed))) { |
| 504 | msg_perr("Unable to configure system baud rate to specified value."); |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 505 | goto init_err_cleanup_exit; |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | /* Return to the main prompt */ |
| 509 | bp_commbuf[0] = ' '; |
| 510 | if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0))) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 511 | goto init_err_cleanup_exit; |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 512 | if ((ret = buspirate_wait_for_string(bp_commbuf, "HiZ>"))) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 513 | goto init_err_cleanup_exit; |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 514 | |
| 515 | msg_pdbg("Serial speed is %d baud\n", serialspeeds[serialspeed_index].speed); |
| 516 | } |
| 517 | |
| 518 | } |
| 519 | |
| 520 | |
| 521 | |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 522 | /* Enter raw bitbang mode */ |
| 523 | for (i = 0; i < 20; i++) { |
| 524 | bp_commbuf[0] = 0x00; |
| 525 | if ((ret = buspirate_sendrecv(bp_commbuf, 1, 0))) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 526 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 527 | } |
| 528 | if ((ret = buspirate_wait_for_string(bp_commbuf, "BBIO"))) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 529 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 530 | if ((ret = buspirate_sendrecv(bp_commbuf, 0, 1))) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 531 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 532 | msg_pdbg("Raw bitbang mode version %c\n", bp_commbuf[0]); |
| 533 | if (bp_commbuf[0] != '1') { |
| 534 | msg_perr("Can't handle raw bitbang mode version %c!\n", bp_commbuf[0]); |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 535 | ret = 1; |
| 536 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 537 | } |
| 538 | /* Enter raw SPI mode */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 539 | bp_commbuf[0] = 0x01; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 540 | ret = buspirate_sendrecv(bp_commbuf, 1, 0); |
Anastasia Klimchuk | ee6d548 | 2021-06-24 16:25:43 +1000 | [diff] [blame] | 541 | if (ret) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 542 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 543 | if ((ret = buspirate_wait_for_string(bp_commbuf, "SPI"))) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 544 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 545 | if ((ret = buspirate_sendrecv(bp_commbuf, 0, 1))) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 546 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | a16a892 | 2012-08-17 17:30:43 +0000 | [diff] [blame] | 547 | msg_pdbg("Raw SPI mode version %c\n", bp_commbuf[0]); |
| 548 | if (bp_commbuf[0] != '1') { |
| 549 | msg_perr("Can't handle raw SPI mode version %c!\n", bp_commbuf[0]); |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 550 | ret = 1; |
| 551 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | /* Initial setup (SPI peripherals config): Enable power, CS high, AUX */ |
Brian Salcedo | 30dfdba | 2013-01-03 20:44:30 +0000 | [diff] [blame] | 555 | bp_commbuf[0] = 0x40 | 0x0b; |
Felix Singer | cf6b708 | 2022-08-18 23:57:31 +0200 | [diff] [blame] | 556 | if (pullup) { |
Brian Salcedo | 30dfdba | 2013-01-03 20:44:30 +0000 | [diff] [blame] | 557 | bp_commbuf[0] |= (1 << 2); |
| 558 | msg_pdbg("Enabling pull-up resistors.\n"); |
| 559 | } |
Felix Singer | cf6b708 | 2022-08-18 23:57:31 +0200 | [diff] [blame] | 560 | if (psu) { |
Jeremy Kerr | 98bdcb4 | 2021-05-23 17:58:06 +0800 | [diff] [blame] | 561 | bp_commbuf[0] |= (1 << 3); |
| 562 | msg_pdbg("Enabling PSUs.\n"); |
| 563 | } |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 564 | ret = buspirate_sendrecv(bp_commbuf, 1, 1); |
Anastasia Klimchuk | ee6d548 | 2021-06-24 16:25:43 +1000 | [diff] [blame] | 565 | if (ret) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 566 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 567 | if (bp_commbuf[0] != 0x01) { |
Brian Salcedo | 30dfdba | 2013-01-03 20:44:30 +0000 | [diff] [blame] | 568 | msg_perr("Protocol error while setting power/CS/AUX(/Pull-up resistors)!\n"); |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 569 | ret = 1; |
| 570 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 571 | } |
| 572 | |
Carl-Daniel Hailfinger | d5b28fa | 2009-11-24 18:27:10 +0000 | [diff] [blame] | 573 | /* Set SPI speed */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 574 | bp_commbuf[0] = 0x60 | spispeed; |
| 575 | ret = buspirate_sendrecv(bp_commbuf, 1, 1); |
Anastasia Klimchuk | ee6d548 | 2021-06-24 16:25:43 +1000 | [diff] [blame] | 576 | if (ret) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 577 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 578 | if (bp_commbuf[0] != 0x01) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 579 | msg_perr("Protocol error while setting SPI speed!\n"); |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 580 | ret = 1; |
| 581 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 582 | } |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 583 | |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 584 | /* Set SPI config: output type, idle, clock edge, sample */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 585 | bp_commbuf[0] = 0x80 | 0xa; |
Felix Singer | cf6b708 | 2022-08-18 23:57:31 +0200 | [diff] [blame] | 586 | if (pullup) { |
Maxime Vincent | 2099c64 | 2017-11-24 13:04:08 +0100 | [diff] [blame] | 587 | bp_commbuf[0] &= ~(1 << 3); |
| 588 | msg_pdbg("Pull-ups enabled, so using HiZ pin output! (Open-Drain mode)\n"); |
| 589 | } |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 590 | ret = buspirate_sendrecv(bp_commbuf, 1, 1); |
Anastasia Klimchuk | ee6d548 | 2021-06-24 16:25:43 +1000 | [diff] [blame] | 591 | if (ret) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 592 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 593 | if (bp_commbuf[0] != 0x01) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 594 | msg_perr("Protocol error while setting SPI config!\n"); |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 595 | ret = 1; |
| 596 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 597 | } |
| 598 | |
| 599 | /* De-assert CS# */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 600 | bp_commbuf[0] = 0x03; |
| 601 | ret = buspirate_sendrecv(bp_commbuf, 1, 1); |
Anastasia Klimchuk | ee6d548 | 2021-06-24 16:25:43 +1000 | [diff] [blame] | 602 | if (ret) |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 603 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 604 | if (bp_commbuf[0] != 0x01) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 605 | msg_perr("Protocol error while raising CS#!\n"); |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 606 | ret = 1; |
| 607 | goto init_err_cleanup_exit; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Nico Huber | 89569d6 | 2023-01-12 23:31:40 +0100 | [diff] [blame] | 610 | return register_spi_master(&spi_master_buspirate, 0, bp_data); |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 611 | |
| 612 | init_err_cleanup_exit: |
Anastasia Klimchuk | d8f8c99 | 2021-05-06 07:47:04 +1000 | [diff] [blame] | 613 | buspirate_spi_shutdown(bp_data); |
Anastasia Klimchuk | 7dcb6f3 | 2021-05-24 15:12:10 +1000 | [diff] [blame] | 614 | return ret; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Edward O'Callaghan | 5eca427 | 2020-04-12 17:27:53 +1000 | [diff] [blame] | 617 | static int buspirate_spi_send_command_v1(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt, |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 618 | const unsigned char *writearr, unsigned char *readarr) |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 619 | { |
Nico Huber | 9a11cbf | 2023-01-13 01:19:07 +0100 | [diff] [blame] | 620 | struct bp_spi_data *bp_data = flash->mst.spi->data; |
Stefan Tauner | c69c9c8 | 2011-11-23 09:13:48 +0000 | [diff] [blame] | 621 | unsigned int i = 0; |
| 622 | int ret = 0; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 623 | |
| 624 | if (writecnt > 16 || readcnt > 16 || (readcnt + writecnt) > 16) |
| 625 | return SPI_INVALID_LENGTH; |
| 626 | |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 627 | /* 3 bytes extra for CS#, len, CS#. */ |
Anastasia Klimchuk | 346d54d | 2021-06-10 09:57:51 +1000 | [diff] [blame] | 628 | if (buspirate_commbuf_grow(writecnt + readcnt + 3, &bp_data->commbuf, &bp_data->commbufsize)) |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 629 | return ERROR_OOM; |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 630 | |
Anastasia Klimchuk | 346d54d | 2021-06-10 09:57:51 +1000 | [diff] [blame] | 631 | unsigned char *const bp_commbuf = bp_data->commbuf; |
Anastasia Klimchuk | d8f8c99 | 2021-05-06 07:47:04 +1000 | [diff] [blame] | 632 | |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 633 | /* Assert CS# */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 634 | bp_commbuf[i++] = 0x02; |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 635 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 636 | bp_commbuf[i++] = 0x10 | (writecnt + readcnt - 1); |
| 637 | memcpy(bp_commbuf + i, writearr, writecnt); |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 638 | i += writecnt; |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 639 | memset(bp_commbuf + i, 0, readcnt); |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 640 | |
| 641 | i += readcnt; |
| 642 | /* De-assert CS# */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 643 | bp_commbuf[i++] = 0x03; |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 644 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 645 | ret = buspirate_sendrecv(bp_commbuf, i, i); |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 646 | |
| 647 | if (ret) { |
| 648 | msg_perr("Bus Pirate communication error!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 649 | return SPI_GENERIC_ERROR; |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 652 | if (bp_commbuf[0] != 0x01) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 653 | msg_perr("Protocol error while lowering CS#!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 654 | return SPI_GENERIC_ERROR; |
| 655 | } |
| 656 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 657 | if (bp_commbuf[1] != 0x01) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 658 | msg_perr("Protocol error while reading/writing SPI!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 659 | return SPI_GENERIC_ERROR; |
| 660 | } |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 661 | |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 662 | if (bp_commbuf[i - 1] != 0x01) { |
Sean Nelson | 84f7bce | 2010-01-09 23:56:41 +0000 | [diff] [blame] | 663 | msg_perr("Protocol error while raising CS#!\n"); |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 664 | return SPI_GENERIC_ERROR; |
| 665 | } |
| 666 | |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 667 | /* Skip CS#, length, writearr. */ |
Carl-Daniel Hailfinger | 316fdfb | 2012-06-08 15:27:47 +0000 | [diff] [blame] | 668 | memcpy(readarr, bp_commbuf + 2 + writecnt, readcnt); |
Carl-Daniel Hailfinger | 04e18be | 2010-07-29 16:24:09 +0000 | [diff] [blame] | 669 | |
Carl-Daniel Hailfinger | 5cca01f | 2009-11-24 00:20:03 +0000 | [diff] [blame] | 670 | return ret; |
| 671 | } |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 672 | |
Edward O'Callaghan | 5eca427 | 2020-04-12 17:27:53 +1000 | [diff] [blame] | 673 | static int buspirate_spi_send_command_v2(const struct flashctx *flash, unsigned int writecnt, unsigned int readcnt, |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 674 | const unsigned char *writearr, unsigned char *readarr) |
| 675 | { |
Nico Huber | 9a11cbf | 2023-01-13 01:19:07 +0100 | [diff] [blame] | 676 | struct bp_spi_data *bp_data = flash->mst.spi->data; |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 677 | int i = 0, ret = 0; |
| 678 | |
| 679 | if (writecnt > 4096 || readcnt > 4096 || (readcnt + writecnt) > 4096) |
| 680 | return SPI_INVALID_LENGTH; |
| 681 | |
| 682 | /* 5 bytes extra for command, writelen, readlen. |
| 683 | * 1 byte extra for Ack/Nack. |
| 684 | */ |
Anastasia Klimchuk | 346d54d | 2021-06-10 09:57:51 +1000 | [diff] [blame] | 685 | if (buspirate_commbuf_grow(max(writecnt + 5, readcnt + 1), &bp_data->commbuf, &bp_data->commbufsize)) |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 686 | return ERROR_OOM; |
| 687 | |
Anastasia Klimchuk | 346d54d | 2021-06-10 09:57:51 +1000 | [diff] [blame] | 688 | unsigned char *const bp_commbuf = bp_data->commbuf; |
Anastasia Klimchuk | d8f8c99 | 2021-05-06 07:47:04 +1000 | [diff] [blame] | 689 | |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 690 | /* Combined SPI write/read. */ |
| 691 | bp_commbuf[i++] = 0x04; |
| 692 | bp_commbuf[i++] = (writecnt >> 8) & 0xff; |
| 693 | bp_commbuf[i++] = writecnt & 0xff; |
| 694 | bp_commbuf[i++] = (readcnt >> 8) & 0xff; |
| 695 | bp_commbuf[i++] = readcnt & 0xff; |
| 696 | memcpy(bp_commbuf + i, writearr, writecnt); |
Shawn Anastasio | 2b5adfb | 2017-12-31 00:17:15 -0600 | [diff] [blame] | 697 | |
Carl-Daniel Hailfinger | 64263c7 | 2012-11-01 23:38:51 +0000 | [diff] [blame] | 698 | ret = buspirate_sendrecv(bp_commbuf, i + writecnt, 1 + readcnt); |
| 699 | |
| 700 | if (ret) { |
| 701 | msg_perr("Bus Pirate communication error!\n"); |
| 702 | return SPI_GENERIC_ERROR; |
| 703 | } |
| 704 | |
| 705 | if (bp_commbuf[0] != 0x01) { |
| 706 | msg_perr("Protocol error while sending SPI write/read!\n"); |
| 707 | return SPI_GENERIC_ERROR; |
| 708 | } |
| 709 | |
| 710 | /* Skip Ack. */ |
| 711 | memcpy(readarr, bp_commbuf + 1, readcnt); |
| 712 | |
| 713 | return ret; |
Nico Huber | deeac7e | 2017-04-22 00:09:42 +0200 | [diff] [blame] | 714 | } |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 715 | |
| 716 | const struct programmer_entry programmer_buspirate_spi = { |
| 717 | .name = "buspirate_spi", |
| 718 | .type = OTHER, |
| 719 | /* FIXME */ |
| 720 | .devs.note = "Dangerous Prototypes Bus Pirate\n", |
| 721 | .init = buspirate_spi_init, |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 722 | }; |