From 8d190cdbb9c4e4377d0c33424e453c5ac166eed8 Mon Sep 17 00:00:00 2001 From: Gareth McMullin Date: Sun, 10 Jun 2012 16:40:07 +1200 Subject: Renamed platforms to 'native' and 'libftdi' and moved into 'platforms' dir. --- src/linux/Makefile.inc | 1 - src/linux/gdb_if.c | 114 ------------------------- src/linux/jtagtap.c | 221 ------------------------------------------------- src/linux/platform.c | 128 ---------------------------- src/linux/platform.h | 56 ------------- src/linux/swdptap.c | 176 --------------------------------------- 6 files changed, 696 deletions(-) delete mode 100644 src/linux/Makefile.inc delete mode 100644 src/linux/gdb_if.c delete mode 100644 src/linux/jtagtap.c delete mode 100644 src/linux/platform.c delete mode 100644 src/linux/platform.h delete mode 100644 src/linux/swdptap.c (limited to 'src/linux') diff --git a/src/linux/Makefile.inc b/src/linux/Makefile.inc deleted file mode 100644 index 095b3a7..0000000 --- a/src/linux/Makefile.inc +++ /dev/null @@ -1 +0,0 @@ -LDFLAGS += -lftdi -lusb diff --git a/src/linux/gdb_if.c b/src/linux/gdb_if.c deleted file mode 100644 index 4b4f4a5..0000000 --- a/src/linux/gdb_if.c +++ /dev/null @@ -1,114 +0,0 @@ -/* - * 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 - -#ifndef WIN32 -# include -# include -# include -#else -# include -# include -#endif - -#include - -#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/linux/jtagtap.c b/src/linux/jtagtap.c deleted file mode 100644 index fdfb051..0000000 --- a/src/linux/jtagtap.c +++ /dev/null @@ -1,221 +0,0 @@ -/* - * 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 "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/linux/platform.c b/src/linux/platform.c deleted file mode 100644 index 9690a92..0000000 --- a/src/linux/platform.c +++ /dev/null @@ -1,128 +0,0 @@ -/* - * 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" -#include "gdb_if.h" -#include "jtag_scan.h" - -#include -#include -#include - -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/linux/platform.h b/src/linux/platform.h deleted file mode 100644 index 325f67b..0000000 --- a/src/linux/platform.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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 -#include - -#ifndef WIN32 -# include -#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/linux/swdptap.c b/src/linux/swdptap.c deleted file mode 100644 index be329d8..0000000 --- a/src/linux/swdptap.c +++ /dev/null @@ -1,176 +0,0 @@ -/* - * 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 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); -} - -- cgit v1.2.3