aboutsummaryrefslogtreecommitdiff
path: root/src/platforms/libftdi
diff options
context:
space:
mode:
Diffstat (limited to 'src/platforms/libftdi')
-rw-r--r--src/platforms/libftdi/Makefile.inc1
-rw-r--r--src/platforms/libftdi/gdb_if.c114
-rw-r--r--src/platforms/libftdi/jtagtap.c221
-rw-r--r--src/platforms/libftdi/platform.c128
-rw-r--r--src/platforms/libftdi/platform.h56
-rw-r--r--src/platforms/libftdi/swdptap.c176
6 files changed, 696 insertions, 0 deletions
diff --git a/src/platforms/libftdi/Makefile.inc b/src/platforms/libftdi/Makefile.inc
new file mode 100644
index 0000000..095b3a7
--- /dev/null
+++ b/src/platforms/libftdi/Makefile.inc
@@ -0,0 +1 @@
+LDFLAGS += -lftdi -lusb
diff --git a/src/platforms/libftdi/gdb_if.c b/src/platforms/libftdi/gdb_if.c
new file mode 100644
index 0000000..4b4f4a5
--- /dev/null
+++ b/src/platforms/libftdi/gdb_if.c
@@ -0,0 +1,114 @@
+/*
+ * This file is part of the Black Magic Debug project.
+ *
+ * Copyright (C) 2011 Black Sphere Technologies Ltd.
+ * Written by Gareth McMullin <gareth@blacksphere.co.nz>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/* This file implements a transparent channel over which the GDB Remote
+ * Serial Debugging protocol is implemented. This implementation for Linux
+ * uses a TCP server on port 2000.
+ */
+#include <stdio.h>
+
+#ifndef WIN32
+# include <sys/socket.h>
+# include <netinet/in.h>
+# include <sys/select.h>
+#else
+# include <windows.h>
+# include <ws2tcpip.h>
+#endif
+
+#include <assert.h>
+
+#include "general.h"
+#include "gdb_if.h"
+
+static int gdb_if_serv, gdb_if_conn;
+
+int gdb_if_init(void)
+{
+#ifdef WIN32
+ WSADATA wsaData;
+ WSAStartup(MAKEWORD(2, 2), &wsaData);
+#endif
+ struct sockaddr_in addr;
+ int opt;
+
+ addr.sin_family = AF_INET;
+ addr.sin_port = htons(2000);
+ addr.sin_addr.s_addr = htonl(INADDR_ANY);
+
+ assert((gdb_if_serv = socket(PF_INET, SOCK_STREAM, 0)) != -1);
+ opt = 1;
+ assert(setsockopt(gdb_if_serv, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) != -1);
+
+ assert(bind(gdb_if_serv, (void*)&addr, sizeof(addr)) != -1);
+ assert(listen(gdb_if_serv, 1) != -1);
+
+ DEBUG("Listening on TCP:2000\n");
+
+ return 0;
+}
+
+
+unsigned char gdb_if_getchar(void)
+{
+ unsigned char ret;
+ int i = 0;
+
+ while(i <= 0) {
+ if(gdb_if_conn <= 0) {
+ gdb_if_conn = accept(gdb_if_serv, NULL, NULL);
+ DEBUG("Got connection\n");
+ }
+ i = recv(gdb_if_conn, &ret, 1, 0);
+ if(i <= 0) {
+ gdb_if_conn = -1;
+ DEBUG("Dropped broken connection\n");
+ /* Return '+' in case we were waiting for an ACK */
+ return '+';
+ }
+ }
+ return ret;
+}
+
+unsigned char gdb_if_getchar_to(int timeout)
+{
+ fd_set fds;
+ struct timeval tv;
+
+ if(gdb_if_conn == -1) return -1;
+
+ tv.tv_sec = timeout / 1000;
+ tv.tv_usec = (timeout % 1000) * 1000;
+
+ FD_ZERO(&fds);
+ FD_SET(gdb_if_conn, &fds);
+
+ if(select(gdb_if_conn+1, &fds, NULL, NULL, &tv) > 0)
+ return gdb_if_getchar();
+
+ return -1;
+}
+
+void gdb_if_putchar(unsigned char c, int flush)
+{
+ if(gdb_if_conn > 0)
+ send(gdb_if_conn, &c, 1, 0);
+}
+
diff --git a/src/platforms/libftdi/jtagtap.c b/src/platforms/libftdi/jtagtap.c
new file mode 100644
index 0000000..fdfb051
--- /dev/null
+++ b/src/platforms/libftdi/jtagtap.c
@@ -0,0 +1,221 @@
+/*
+ * This file is part of the Black Magic Debug project.
+ *
+ * Copyright (C) 2008 Black Sphere Technologies Ltd.
+ * Written by Gareth McMullin <gareth@blacksphere.co.nz>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/* Low level JTAG implementation using FT2232 with libftdi.
+ *
+ * Issues:
+ * This code is old, rotten and unsupported.
+ * Magic numbers everywhere.
+ * Should share interface with swdptap.c or at least clean up...
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+#include <string.h>
+
+#include <assert.h>
+
+#include <ftdi.h>
+
+#include "general.h"
+
+#define PROVIDE_GENERIC_TAP_SEQ
+//#define PROVIDE_GENERIC_TAP_TMS_SEQ
+//#define PROVIDE_GENERIC_TAP_TDI_TDO_SEQ
+//#define PROVIDE_GENERIC_TAP_TDI_SEQ
+#include "jtagtap.h"
+
+#define ALL_ZERO 0xA0
+#define TCK 0x01
+#define TDI 0x02
+#define TDO 0x04
+#define TMS 0x08
+#define nSRST 0x20
+
+int jtagtap_init(void)
+{
+ int err;
+
+ assert(ftdic != NULL);
+
+ if((err = ftdi_set_bitmode(ftdic, 0xAB, BITMODE_MPSSE)) != 0) {
+ fprintf(stderr, "ftdi_set_bitmode: %d: %s\n",
+ err, ftdi_get_error_string(ftdic));
+ abort();
+ }
+
+ assert(ftdi_write_data(ftdic, "\x86\x00\x00\x80\xA8\xAB", 6) == 6);
+
+ /* Go to JTAG mode for SWJ-DP */
+ for(int i = 0; i <= 50; i++) jtagtap_next(1, 0); /* Reset SW-DP */
+ jtagtap_tms_seq(0xE73C, 16); /* SWD to JTAG sequence */
+ jtagtap_soft_reset();
+
+ return 0;
+}
+
+void jtagtap_reset(void)
+{
+ jtagtap_soft_reset();
+}
+
+void jtagtap_srst(void)
+{
+ platform_buffer_flush();
+ //ftdi_write_data(ftdic, "\x80\x88\xAB", 3);
+ //usleep(1000);
+ //ftdi_write_data(ftdic, "\x80\xA8\xAB", 3);
+}
+
+#ifndef PROVIDE_GENERIC_TAP_TMS_SEQ
+void
+jtagtap_tms_seq(uint32_t MS, int ticks)
+{
+ uint8_t tmp[3] = "\x4B";
+ while(ticks >= 0) {
+ //jtagtap_next(MS & 1, 1);
+ tmp[1] = ticks<7?ticks-1:6;
+ tmp[2] = 0x80 | (MS & 0x7F);
+
+// assert(ftdi_write_data(ftdic, tmp, 3) == 3);
+ platform_buffer_write(tmp, 3);
+ MS >>= 7; ticks -= 7;
+ }
+}
+#endif
+
+#ifndef PROVIDE_GENERIC_TAP_TDI_SEQ
+void
+jtagtap_tdi_seq(const uint8_t final_tms, const uint8_t *DI, int ticks)
+{
+ char *tmp;
+ int index = 0;
+ int rticks;
+
+ if(!ticks) return;
+
+ if(final_tms) ticks--;
+ rticks = ticks & 7;
+ ticks >>= 3;
+ tmp = alloca(ticks + 9);
+
+ if(ticks) {
+ tmp[index++] = 0x19;
+ tmp[index++] = ticks - 1;
+ tmp[index++] = 0;
+ while(ticks--) tmp[index++] = *DI++;
+ }
+
+ if(rticks) {
+ tmp[index++] = 0x1B;
+ tmp[index++] = rticks - 1;
+ tmp[index++] = *DI;
+ }
+
+ if(final_tms) {
+ tmp[index++] = 0x4B;
+ tmp[index++] = 0;
+ tmp[index++] = (*DI)>>rticks?0x81:0x01;
+ }
+// assert(ftdi_write_data(ftdic, tmp, index) == index);
+ platform_buffer_write(tmp, index);
+}
+#endif
+
+#ifndef PROVIDE_GENERIC_TAP_TDI_TDO_SEQ
+void
+jtagtap_tdi_tdo_seq(uint8_t *DO, const uint8_t final_tms, const uint8_t *DI, int ticks)
+{
+ uint8_t *tmp;
+ int index = 0, rsize;
+ int rticks;
+
+ if(!ticks) return;
+
+// printf("ticks: %d\n", ticks);
+ if(final_tms) ticks--;
+ rticks = ticks & 7;
+ ticks >>= 3;
+ tmp = alloca(ticks + 9);
+ rsize = ticks;
+ if(ticks) {
+ tmp[index++] = 0x39;
+ tmp[index++] = ticks - 1;
+ tmp[index++] = 0;
+ while(ticks--) tmp[index++] = *DI++;
+ }
+
+ if(rticks) {
+ rsize++;
+ tmp[index++] = 0x3B;
+ tmp[index++] = rticks - 1;
+ tmp[index++] = *DI;
+ }
+
+ if(final_tms) {
+ rsize++;
+ tmp[index++] = 0x6B;
+ tmp[index++] = 0;
+ tmp[index++] = (*DI)>>rticks?0x81:0x01;
+ }
+// assert(ftdi_write_data(ftdic, tmp, index) == index);
+ platform_buffer_write(tmp, index);
+// index = 0;
+// while((index += ftdi_read_data(ftdic, tmp + index, rsize-index)) != rsize);
+ platform_buffer_read(tmp, rsize);
+ /*for(index = 0; index < rsize; index++)
+ printf("%02X ", tmp[index]);
+ printf("\n");*/
+ index = 0;
+ if(final_tms) rsize--;
+
+ while(rsize--) {
+ /*if(rsize) printf("%02X ", tmp[index]);*/
+ *DO++ = tmp[index++];
+ }
+ if(final_tms) {
+ rticks++;
+ *(--DO) >>= 1;
+ *DO |= tmp[index] & 0x80;
+ } else DO--;
+ if(rticks) {
+ *DO >>= (8-rticks);
+ }
+ /*printf("%02X\n", *DO);*/
+}
+#endif
+
+uint8_t jtagtap_next(uint8_t dTMS, uint8_t dTDO)
+{
+ uint8_t ret;
+ uint8_t tmp[3] = "\x6B\x00\x00";
+ tmp[2] = (dTDO?0x80:0) | (dTMS?0x01:0);
+// assert(ftdi_write_data(ftdic, tmp, 3) == 3);
+// while(ftdi_read_data(ftdic, &ret, 1) != 1);
+ platform_buffer_write(tmp, 3);
+ platform_buffer_read(&ret, 1);
+
+ ret &= 0x80;
+
+// DEBUG("jtagtap_next(TMS = %d, TDO = %d) = %02X\n", dTMS, dTDO, ret);
+
+ return ret;
+}
+
diff --git a/src/platforms/libftdi/platform.c b/src/platforms/libftdi/platform.c
new file mode 100644
index 0000000..9690a92
--- /dev/null
+++ b/src/platforms/libftdi/platform.c
@@ -0,0 +1,128 @@
+/*
+ * This file is part of the Black Magic Debug project.
+ *
+ * Copyright (C) 2011 Black Sphere Technologies Ltd.
+ * Written by Gareth McMullin <gareth@blacksphere.co.nz>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+#include "platform.h"
+#include "gdb_if.h"
+#include "jtag_scan.h"
+
+#include <stdio.h>
+#include <string.h>
+#include <assert.h>
+
+struct ftdi_context *ftdic;
+
+#define BUF_SIZE 4096
+static uint8_t outbuf[BUF_SIZE];
+static uint16_t bufptr = 0;
+
+int platform_init(void)
+{
+ int err;
+
+ if(ftdic) {
+ ftdi_usb_close(ftdic);
+ ftdi_free(ftdic);
+ ftdic = NULL;
+ }
+ if((ftdic = ftdi_new()) == NULL) {
+ fprintf(stderr, "ftdi_new: %s\n",
+ ftdi_get_error_string(ftdic));
+ abort();
+ }
+ if((err = ftdi_set_interface(ftdic, INTERFACE_A)) != 0) {
+ fprintf(stderr, "ftdi_set_interface: %d: %s\n",
+ err, ftdi_get_error_string(ftdic));
+ abort();
+ }
+ if((err = ftdi_usb_open(ftdic, FT2232_VID, FT2232_PID)) != 0) {
+ fprintf(stderr, "unable to open ftdi device: %d (%s)\n",
+ err, ftdi_get_error_string(ftdic));
+ abort();
+ }
+
+ if((err = ftdi_set_latency_timer(ftdic, 1)) != 0) {
+ fprintf(stderr, "ftdi_set_latency_timer: %d: %s\n",
+ err, ftdi_get_error_string(ftdic));
+ abort();
+ }
+ if((err = ftdi_set_baudrate(ftdic, 1000000)) != 0) {
+ fprintf(stderr, "ftdi_set_baudrate: %d: %s\n",
+ err, ftdi_get_error_string(ftdic));
+ abort();
+ }
+ if((err = ftdi_usb_purge_buffers(ftdic)) != 0) {
+ fprintf(stderr, "ftdi_set_baudrate: %d: %s\n",
+ err, ftdi_get_error_string(ftdic));
+ abort();
+ }
+ if((err = ftdi_write_data_set_chunksize(ftdic, BUF_SIZE)) != 0) {
+ fprintf(stderr, "ftdi_write_data_set_chunksize: %d: %s\n",
+ err, ftdi_get_error_string(ftdic));
+ abort();
+ }
+
+ assert(gdb_if_init() == 0);
+
+ jtag_scan();
+
+ return 0;
+}
+
+void platform_buffer_flush(void)
+{
+ assert(ftdi_write_data(ftdic, outbuf, bufptr) == bufptr);
+// printf("FT2232 platform_buffer flush: %d bytes\n", bufptr);
+ bufptr = 0;
+}
+
+int platform_buffer_write(const uint8_t *data, int size)
+{
+ if((bufptr + size) / BUF_SIZE > 0) platform_buffer_flush();
+ memcpy(outbuf + bufptr, data, size);
+ bufptr += size;
+ return size;
+}
+
+int platform_buffer_read(uint8_t *data, int size)
+{
+ int index = 0;
+ platform_buffer_flush();
+ while((index += ftdi_read_data(ftdic, data + index, size-index)) != size);
+ return size;
+}
+
+#ifdef WIN32
+#warning "This vasprintf() is dubious!"
+int vasprintf(char **strp, const char *fmt, va_list ap)
+{
+ int size = 128, ret = 0;
+
+ *strp = malloc(size);
+ while(*strp && ((ret = vsnprintf(*strp, size, fmt, ap)) == size))
+ *strp = realloc(*strp, size <<= 1);
+
+ return ret;
+}
+#endif
+
+const char *platform_target_voltage(void)
+{
+ return "not supported";
+}
+
diff --git a/src/platforms/libftdi/platform.h b/src/platforms/libftdi/platform.h
new file mode 100644
index 0000000..325f67b
--- /dev/null
+++ b/src/platforms/libftdi/platform.h
@@ -0,0 +1,56 @@
+/*
+ * This file is part of the Black Magic Debug project.
+ *
+ * Copyright (C) 2011 Black Sphere Technologies Ltd.
+ * Written by Gareth McMullin <gareth@blacksphere.co.nz>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __PLATFORM_H
+#define __PLATFORM_H
+
+#include <stdint.h>
+#include <ftdi.h>
+
+#ifndef WIN32
+# include <alloca.h>
+#else
+# define alloca __builtin_alloca
+#endif
+
+#define FT2232_VID 0x0403
+#define FT2232_PID 0x6010
+
+#define SET_RUN_STATE(state)
+#define SET_IDLE_STATE(state)
+#define SET_ERROR_STATE(state)
+
+#define PLATFORM_FATAL_ERROR(error) abort()
+#define PLATFORM_SET_FATAL_ERROR_RECOVERY()
+
+#define morse(x, y) do {} while(0)
+#define morse_msg 0
+
+extern struct ftdi_context *ftdic;
+
+int platform_init(void);
+const char *platform_target_voltage(void);
+
+void platform_buffer_flush(void);
+int platform_buffer_write(const uint8_t *data, int size);
+int platform_buffer_read(uint8_t *data, int size);
+
+#endif
+
diff --git a/src/platforms/libftdi/swdptap.c b/src/platforms/libftdi/swdptap.c
new file mode 100644
index 0000000..be329d8
--- /dev/null
+++ b/src/platforms/libftdi/swdptap.c
@@ -0,0 +1,176 @@
+/*
+ * This file is part of the Black Magic Debug project.
+ *
+ * Copyright (C) 2011 Black Sphere Technologies Ltd.
+ * Written by Gareth McMullin <gareth@blacksphere.co.nz>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/* Quick hack for bit-banging SW-DP interface over FT2232.
+ * Intended as proof of concept, not for production.
+ */
+
+#include <stdio.h>
+#include <assert.h>
+#include <ftdi.h>
+
+#include "platform.h"
+#include "swdptap.h"
+
+static void swdptap_turnaround(uint8_t dir);
+static uint8_t swdptap_bit_in(void);
+static void swdptap_bit_out(uint8_t val);
+
+int swdptap_init(void)
+{
+ int err;
+
+ assert(ftdic != NULL);
+
+ if((err = ftdi_set_bitmode(ftdic, 0xAB, BITMODE_BITBANG)) != 0) {
+ fprintf(stderr, "ftdi_set_bitmode: %d: %s\n",
+ err, ftdi_get_error_string(ftdic));
+ abort();
+ }
+
+ assert(ftdi_write_data(ftdic, "\xAB\xA8", 2) == 2);
+
+ /* This must be investigated in more detail.
+ * As described in STM32 Reference Manual... */
+ swdptap_seq_out(0xFFFF, 16);
+ swdptap_reset();
+ swdptap_seq_out(0xE79E, 16); /* 0b0111100111100111 */
+ swdptap_reset();
+ swdptap_seq_out(0, 16);
+
+ return 0;
+}
+
+void swdptap_reset(void)
+{
+ swdptap_turnaround(0);
+ /* 50 clocks with TMS high */
+ for(int i = 0; i < 50; i++) swdptap_bit_out(1);
+}
+
+static void swdptap_turnaround(uint8_t dir)
+{
+ static uint8_t olddir = 0;
+
+ //DEBUG("%s", dir ? "\n-> ":"\n<- ");
+ platform_buffer_flush();
+
+ if(dir == olddir) return;
+ olddir = dir;
+
+ if(dir) /* SWDIO goes to input */
+ assert(ftdi_set_bitmode(ftdic, 0xA3, BITMODE_BITBANG) == 0);
+
+ /* One clock cycle */
+ ftdi_write_data(ftdic, "\xAB\xA8", 2);
+
+ if(!dir) /* SWDIO goes to output */
+ assert(ftdi_set_bitmode(ftdic, 0xAB, BITMODE_BITBANG) == 0);
+}
+
+static uint8_t swdptap_bit_in(void)
+{
+ uint8_t ret;
+
+ //ftdi_read_data(ftdic, &ret, 1);
+ ftdi_read_pins(ftdic, &ret);
+ ret &= 0x08;
+ ftdi_write_data(ftdic, "\xA1\xA0", 2);
+
+ //DEBUG("%d", ret?1:0);
+
+ return ret;
+}
+
+static void swdptap_bit_out(uint8_t val)
+{
+ uint8_t buf[3] = "\xA0\xA1\xA0";
+
+ //DEBUG("%d", val);
+
+ if(val) {
+ for(int i = 0; i < 3; i++)
+ buf[i] |= 0x08;
+ }
+ //ftdi_write_data(ftdic, buf, 3);
+ platform_buffer_write(buf, 3);
+}
+
+uint32_t swdptap_seq_in(int ticks)
+{
+ uint32_t index = 1;
+ uint32_t ret = 0;
+
+ swdptap_turnaround(1);
+
+ while (ticks--) {
+ if (swdptap_bit_in())
+ ret |= index;
+ index <<= 1;
+ }
+
+ return ret;
+}
+
+uint8_t swdptap_seq_in_parity(uint32_t *ret, int ticks)
+{
+ uint32_t index = 1;
+ uint8_t parity = 0;
+ *ret = 0;
+
+ swdptap_turnaround(1);
+
+ while (ticks--) {
+ if (swdptap_bit_in()) {
+ *ret |= index;
+ parity ^= 1;
+ }
+ index <<= 1;
+ }
+ if (swdptap_bit_in())
+ parity ^= 1;
+
+ return parity;
+}
+
+void swdptap_seq_out(uint32_t MS, int ticks)
+{
+ swdptap_turnaround(0);
+
+ while (ticks--) {
+ swdptap_bit_out(MS & 1);
+ MS >>= 1;
+ }
+}
+
+void swdptap_seq_out_parity(uint32_t MS, int ticks)
+{
+ uint8_t parity = 0;
+
+ swdptap_turnaround(0);
+
+ while (ticks--) {
+ swdptap_bit_out(MS & 1);
+ parity ^= MS;
+ MS >>= 1;
+ }
+ swdptap_bit_out(parity & 1);
+}
+