From 5d747a52ceb2f044793523a887abcf95059596f8 Mon Sep 17 00:00:00 2001 From: Gareth McMullin Date: Mon, 7 Feb 2011 11:00:10 +1300 Subject: Restored old Linux platform and added untested SWDP support. --- src/linux/Makefile.inc | 1 + src/linux/gdb_if.c | 101 ++++++++++++++++++++ src/linux/jtagtap.c | 255 +++++++++++++++++++++++++++++++++++++++++++++++++ src/linux/platform.c | 23 +++++ src/linux/platform.h | 42 ++++++++ src/linux/swdptap.c | 182 +++++++++++++++++++++++++++++++++++ 6 files changed, 604 insertions(+) create mode 100644 src/linux/Makefile.inc create mode 100644 src/linux/gdb_if.c create mode 100644 src/linux/jtagtap.c create mode 100644 src/linux/platform.c create mode 100644 src/linux/platform.h create mode 100644 src/linux/swdptap.c (limited to 'src/linux') diff --git a/src/linux/Makefile.inc b/src/linux/Makefile.inc new file mode 100644 index 0000000..0968764 --- /dev/null +++ b/src/linux/Makefile.inc @@ -0,0 +1 @@ +LDFLAGS = -lftdi diff --git a/src/linux/gdb_if.c b/src/linux/gdb_if.c new file mode 100644 index 0000000..1fb3491 --- /dev/null +++ b/src/linux/gdb_if.c @@ -0,0 +1,101 @@ +/* + * This file is part of the Black Magic Debug project. + * + * Copyright (C) 2011 Black Sphere Technologies Ltd. + * Written by Gareth McMullin + * + * 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 . + */ + +/* 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 +#include +#include + +#include + +#include + +#include "general.h" +#include "gdb_if.h" + +static int gdb_if_serv, gdb_if_conn; + +int gdb_if_init(void) +{ + struct sockaddr_in addr; + + 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); + 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/linux/jtagtap.c b/src/linux/jtagtap.c new file mode 100644 index 0000000..a8f06a9 --- /dev/null +++ b/src/linux/jtagtap.c @@ -0,0 +1,255 @@ +/* + * This file is part of the Black Magic Debug project. + * + * Copyright (C) 2008 Black Sphere Technologies Ltd. + * Written by Gareth McMullin + * + * 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 . + */ + +/* 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 +#include +#include + +#include +#include + +#include + +#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 + +static struct ftdi_context ftdic; +static int f; + +#define BUF_SIZE 4096 +static uint8_t outbuf[BUF_SIZE]; +static uint16_t bufptr = 0; + +static void buffer_flush(void) +{ + assert(ftdi_write_data(&ftdic, outbuf, bufptr) == bufptr); +// printf("FT2232 buffer flush: %d bytes\n", bufptr); + bufptr = 0; +} + +static int buffer_write(const uint8_t *data, int size) +{ + if((bufptr + size) / BUF_SIZE > 0) buffer_flush(); + memcpy(outbuf + bufptr, data, size); + bufptr += size; + return size; +} + +static int buffer_read(uint8_t *data, int size) +{ + int index = 0; + buffer_flush(); + while((index += ftdi_read_data(&ftdic, data + index, size-index)) != size); + return size; +} + + +int jtagtap_init(void) +{ + ftdi_init(&ftdic); + ftdi_set_interface(&ftdic, INTERFACE_A); + f = ftdi_usb_open(&ftdic, FT2232_VID, FT2232_PID); + if(f < 0 && f != -5) { + fprintf(stderr, "unable to open ftdi device: %d (%s)\n", + f, ftdi_get_error_string(&ftdic)); + return -1; + } + fprintf(stderr, "ftdi open succeeded(channel 1): %d\n",f); + + ftdi_set_latency_timer(&ftdic, 1); + ftdi_set_baudrate(&ftdic, 480000000); + ftdi_usb_purge_buffers(&ftdic); + + fprintf(stderr, "enabling bitbang mode(channel 1)\n"); + assert(ftdi_set_bitmode(&ftdic, 0xAB, BITMODE_MPSSE) == 0); + assert(ftdi_write_data(&ftdic, "\x86\x00\x00\x80\xA8\xAB", 6) == 6); + ftdi_write_data_set_chunksize(&ftdic, BUF_SIZE); + + return 0; +} + +void jtagtap_reset(void) +{ + jtagtap_soft_reset(); +} + +void jtagtap_srst(void) +{ + 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); + 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); + 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); + buffer_write(tmp, index); +// index = 0; +// while((index += ftdi_read_data(&ftdic, tmp + index, rsize-index)) != rsize); + 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); + buffer_write(tmp, 3); + buffer_read(&ret, 1); + + ret &= 0x80; + +// DEBUG("jtagtap_next(TMS = %d, TDO = %d) = %02X\n", dTMS, dTDO, ret); + + return ret; +} + diff --git a/src/linux/platform.c b/src/linux/platform.c new file mode 100644 index 0000000..c18d8a6 --- /dev/null +++ b/src/linux/platform.c @@ -0,0 +1,23 @@ +/* + * This file is part of the Black Magic Debug project. + * + * Copyright (C) 2011 Black Sphere Technologies Ltd. + * Written by Gareth McMullin + * + * 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 . + */ +#include "platform.h" + +int platform_init(void) { return 0; } + diff --git a/src/linux/platform.h b/src/linux/platform.h new file mode 100644 index 0000000..866ffc6 --- /dev/null +++ b/src/linux/platform.h @@ -0,0 +1,42 @@ +/* + * This file is part of the Black Magic Debug project. + * + * Copyright (C) 2011 Black Sphere Technologies Ltd. + * Written by Gareth McMullin + * + * 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 . + */ + +#ifndef __PLATFORM_H +#define __PLATFORM_H + +#include + +#define FT2232_VID 0x0403 +#define FT2232_PID 0xbcd9 + +#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 + +int platform_init(void); + +#endif + diff --git a/src/linux/swdptap.c b/src/linux/swdptap.c new file mode 100644 index 0000000..1185564 --- /dev/null +++ b/src/linux/swdptap.c @@ -0,0 +1,182 @@ +/* + * This file is part of the Black Magic Debug project. + * + * Copyright (C) 2011 Black Sphere Technologies Ltd. + * Written by Gareth McMullin + * + * 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 . + */ + +/* Quick hack for bit-banging SW-DP interface over FT2232. + * Intended as proof of concept, not for production. + */ + +#include +#include +#include + +#include "platform.h" +#include "swdptap.h" + +static struct ftdi_context ftdic; +static int f; + +#define BUF_SIZE 4096 + +int swdptap_init(void) +{ + ftdi_init(&ftdic); + ftdi_set_interface(&ftdic, INTERFACE_A); + f = ftdi_usb_open(&ftdic, FT2232_VID, FT2232_PID); + if(f < 0 && f != -5) { + fprintf(stderr, "unable to open ftdi device: %d (%s)\n", + f, ftdi_get_error_string(&ftdic)); + return -1; + } + printf("ftdi open succeeded(channel 1): %d\n",f); + + ftdi_set_latency_timer(&ftdic, 1); + ftdi_set_baudrate(&ftdic, 10000000); + ftdi_usb_purge_buffers(&ftdic); + + printf("enabling bitbang mode(channel 1)\n"); + assert(ftdi_set_bitmode(&ftdic, 0xAB, BITMODE_BITBANG) == 0); + //assert(ftdi_write_data(&ftdic, "\x86\x00\x00\x80\xA8\xAB", 6) == 6); + ftdi_write_data(&ftdic, "\xAB\xA8", 2); + ftdi_write_data_set_chunksize(&ftdic, BUF_SIZE); + + /* This must be investigated in more detail. + * As described in STM32 Reference Manual... */ + 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); +} + +void swdptap_turnaround(uint8_t dir) +{ + static uint8_t olddir = 0; + + DEBUG("%s", dir ? "\n-> ":"\n<- "); + + 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); +} + +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("%x\n", ret); + + return ret & 0x08; +} + +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); +} + +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); +} + -- cgit v1.2.3