static int buspirate_start_spi_mode_ascii(struct programmer_t *pgm) { int spi_cmd = -1; int cmd; char *rcvd, mode[11], buf[5]; buspirate_send(pgm, "m\n"); while(1) { rcvd = buspirate_readline(pgm, NULL, 0); if (spi_cmd == -1 && sscanf(rcvd, "%d. %10s", &cmd, mode)) { if (strcmp(mode, "SPI") == 0) spi_cmd = cmd; } if (buspirate_is_prompt(rcvd)) break; } if (spi_cmd == -1) { fprintf(stderr, "%s: SPI mode number not found. Does your BusPirate support SPI?\n", progname); fprintf(stderr, "%s: Try powercycling your BusPirate and try again.\n", progname); return -1; } snprintf(buf, sizeof(buf), "%d\n", spi_cmd); buspirate_send(pgm, buf); buf[0] = '\0'; while (1) { rcvd = buspirate_readline(pgm, NULL, 0); if (strstr(rcvd, "Normal (H=3.3V, L=GND)")) { /* BP firmware 2.1 defaults to Open-drain output. * That doesn't work on my board, even with pull-up * resistors. Select 3.3V output mode instead. */ sscanf(rcvd, " %d.", &cmd); snprintf(buf, sizeof(buf), "%d\n", cmd); } if (buspirate_is_prompt(rcvd)) { if (strncmp(rcvd, "SPI>", 4) == 0) { fprintf(stderr, "BusPirate is now configured for SPI\n"); break; } /* Not yet 'SPI>' prompt */ if (buf[0]) { buspirate_send(pgm, buf); buf[0] = '\0'; } else buspirate_send(pgm, "\n"); } } return 0; }
static int buspirate_cmd_ascii(struct programmer_t *pgm, unsigned char cmd[4], unsigned char res[4]) { char buf[25]; char *rcvd; int spi_write, spi_read, i = 0; snprintf(buf, sizeof(buf), "0x%02x 0x%02x 0x%02x 0x%02x\n", cmd[0], cmd[1], cmd[2], cmd[3]); buspirate_send(pgm, buf); while (i < 4) { rcvd = buspirate_readline(pgm, NULL, 0); /* WRITE: 0xAC READ: 0x04 */ if (sscanf(rcvd, "WRITE: 0x%x READ: 0x%x", &spi_write, &spi_read) == 2) { res[i++] = spi_read; } if (buspirate_is_prompt(rcvd)) break; } if (i != 4) { fprintf(stderr, "%s: error: SPI has not read 4 bytes back\n", progname); return -1; } /* wait for prompt */ while (buspirate_getc(pgm) != '>') /* do nothing */; return 0; }
static int buspirate_expect(struct programmer_t *pgm, char *send, char *expect, int wait_for_prompt) { int got_it = 0; size_t expect_len = strlen(expect); char *rcvd; buspirate_send(pgm, send); while (1) { rcvd = buspirate_readline(pgm, NULL, 0); if (strncmp(rcvd, expect, expect_len) == 0) { if (! wait_for_prompt) { serial_drain(&pgm->fd, 0); return 1; } else { got_it = 1; } } if (buspirate_is_prompt(rcvd)) break; } return got_it; }