From 346d22c5b6d8a52cfbdadfe9408fc804f86a1ced Mon Sep 17 00:00:00 2001 From: Fabien Le Mentec Date: Fri, 30 Sep 2011 06:04:07 +0800 Subject: [PATCH 1/3] [missing] stlink-hw.h --- stm32l/src/stlink-hw.h | 45 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 stm32l/src/stlink-hw.h diff --git a/stm32l/src/stlink-hw.h b/stm32l/src/stlink-hw.h new file mode 100644 index 000000000..ca558cb6d --- /dev/null +++ b/stm32l/src/stlink-hw.h @@ -0,0 +1,45 @@ +#ifndef STLINK_HW_H_INCLUDED +# define STLINK_HW_H_INCLUDED + +// STLINK_DEBUG_RESETSYS, etc: +#define STLINK_OK 0x80 +#define STLINK_FALSE 0x81 +#define STLINK_CORE_RUNNING 0x80 +#define STLINK_CORE_HALTED 0x81 +#define STLINK_CORE_STAT_UNKNOWN -1 + +#define STLINK_GET_VERSION 0xf1 +#define STLINK_GET_CURRENT_MODE 0xf5 + +#define STLINK_DEBUG_COMMAND 0xF2 +#define STLINK_DFU_COMMAND 0xF3 +#define STLINK_DFU_EXIT 0x07 + +// STLINK_GET_CURRENT_MODE +#define STLINK_DEV_DFU_MODE 0x00 +#define STLINK_DEV_MASS_MODE 0x01 +#define STLINK_DEV_DEBUG_MODE 0x02 +#define STLINK_DEV_UNKNOWN_MODE -1 + +// jtag mode cmds +#define STLINK_DEBUG_ENTER 0x20 +#define STLINK_DEBUG_EXIT 0x21 +#define STLINK_DEBUG_READCOREID 0x22 +#define STLINK_DEBUG_GETSTATUS 0x01 +#define STLINK_DEBUG_FORCEDEBUG 0x02 +#define STLINK_DEBUG_RESETSYS 0x03 +#define STLINK_DEBUG_READALLREGS 0x04 +#define STLINK_DEBUG_READREG 0x05 +#define STLINK_DEBUG_WRITEREG 0x06 +#define STLINK_DEBUG_READMEM_32BIT 0x07 +#define STLINK_DEBUG_WRITEMEM_32BIT 0x08 +#define STLINK_DEBUG_RUNCORE 0x09 +#define STLINK_DEBUG_STEPCORE 0x0a +#define STLINK_DEBUG_SETFP 0x0b +#define STLINK_DEBUG_WRITEMEM_8BIT 0x0d +#define STLINK_DEBUG_CLEARFP 0x0e +#define STLINK_DEBUG_WRITEDEBUGREG 0x0f +#define STLINK_DEBUG_ENTER_SWD 0xa3 +#define STLINK_DEBUG_ENTER_JTAG 0x00 + +#endif /* STLINK_HW_H_INCLUDED */ From dbda7343d4a4d8e4ae1a7c5094d0942db501a7f5 Mon Sep 17 00:00:00 2001 From: Dmitry Bravikov Date: Wed, 5 Oct 2011 18:15:44 +0800 Subject: [PATCH 2/3] added installation, added ST_LINK autodetection --- INSTALL | 25 ++++++++++++++++++++ build/Makefile | 6 +++++ src/gdb-server.c | 60 ++++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 84 insertions(+), 7 deletions(-) create mode 100644 INSTALL diff --git a/INSTALL b/INSTALL new file mode 100644 index 000000000..49e761c31 --- /dev/null +++ b/INSTALL @@ -0,0 +1,25 @@ +INSTALL +======= + +1) Load the sg kernel module: + + $ modprobe sg + +2) On Ubuntu you need to install the package libsgutils2-dev: + + $ sudo apt-get install libsgutils2-dev + +3) Make: + + $ make -C build + +4) Install or reinstall + + $ sudo make install -C build + +5) Run: + + $ st-util [/dev/sgX] + +6) Have fun! + diff --git a/build/Makefile b/build/Makefile index c50747fa8..8004d3fc2 100644 --- a/build/Makefile +++ b/build/Makefile @@ -20,3 +20,9 @@ $(PRG): $(OBJS) clean: @rm -vf *.d *.o $(PRG) + +install: + @cp -vf $(PRG) /usr/bin/$(PRG) + +uninstall: + @rm -vf /usr/bin/$(PRG) diff --git a/src/gdb-server.c b/src/gdb-server.c index 04fd2bcb4..5e12da02a 100644 --- a/src/gdb-server.c +++ b/src/gdb-server.c @@ -54,14 +54,60 @@ int serve(struct stlink* sl, int port); char* make_memory_map(const struct chip_params *params, uint32_t flash_size); int main(int argc, char** argv) { - if(argc != 3) { - fprintf(stderr, "Usage: %s /dev/sgX\n", argv[0]); - return 1; - } - struct stlink *sl = stlink_quirk_open(argv[2], 0); - if (sl == NULL) - return 1; + struct stlink *sl = NULL; + + switch(argc) { + + default: { + fprintf(stderr, "Usage: %s [/dev/sgX] \n", argv[0]); + return 1; + } + + case 3 : { + sl = stlink_quirk_open(argv[2], 0); + if(sl == NULL) return 1; + break; + } + + case 2 : { // Search ST-LINK (from /dev/sg0 to /dev/sg99) + const int DevNumMax = 99; + int ExistDevCount = 0; + + for(int DevNum = 0; DevNum <= DevNumMax; DevNum++) + { + if(DevNum < 10) { + char DevName[] = "/dev/sgX"; + const int X_index = 7; + DevName[X_index] = DevNum + '0'; + if ( !access(DevName, F_OK) ) { + sl = stlink_quirk_open(DevName, 0); + ExistDevCount++; + } + } + else if(DevNum < 100) { + char DevName[] = "/dev/sgXY"; + const int X_index = 7; + const int Y_index = 8; + DevName[X_index] = DevNum/10 + '0'; + DevName[Y_index] = DevNum%10 + '0'; + if ( !access(DevName, F_OK) ) { + sl = stlink_quirk_open(DevName, 0); + ExistDevCount++; + } + } + if(sl != NULL) break; + } + + if(sl == NULL) { + fprintf(stdout, "\nNumber of /dev/sgX devices found: %i \n", + ExistDevCount); + fprintf(stderr, "ST-LINK not found\n"); + return 1; + } + break; + } + } if(stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE) stlink_enter_swd_mode(sl); From d99d1038a2cc1b61ba2bf6e3f6917e9ea519f785 Mon Sep 17 00:00:00 2001 From: Dmitry Bravikov Date: Thu, 6 Oct 2011 02:42:23 +0800 Subject: [PATCH 3/3] random port --- INSTALL | 6 +++++- src/gdb-server.c | 26 +++++++++++++++++++++++--- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/INSTALL b/INSTALL index 49e761c31..f0bd88210 100644 --- a/INSTALL +++ b/INSTALL @@ -19,7 +19,11 @@ INSTALL 5) Run: - $ st-util [/dev/sgX] + $ st-util port [/dev/sgX] +or + $ st-util [port] +or + $ st-util --help 6) Have fun! diff --git a/src/gdb-server.c b/src/gdb-server.c index 5e12da02a..c732d38c9 100644 --- a/src/gdb-server.c +++ b/src/gdb-server.c @@ -57,10 +57,15 @@ int main(int argc, char** argv) { struct stlink *sl = NULL; + const char * HelpStr = "Usage:\n" + "\t st-util port [/dev/sgX]\n" + "\t st-util [port]\n" + "\t st-util --help\n"; + switch(argc) { default: { - fprintf(stderr, "Usage: %s [/dev/sgX] \n", argv[0]); + fprintf(stderr, HelpStr, NULL); return 1; } @@ -70,7 +75,14 @@ int main(int argc, char** argv) { break; } - case 2 : { // Search ST-LINK (from /dev/sg0 to /dev/sg99) + case 2 : { + if (strcmp(argv[1], "--help") == 0) { + fprintf(stdout, HelpStr, NULL); + return 1; + } + } + + case 1 : { // Search ST-LINK (from /dev/sg0 to /dev/sg99) const int DevNumMax = 99; int ExistDevCount = 0; @@ -149,7 +161,15 @@ int main(int argc, char** argv) { // memory map is in 1k blocks. current_memory_map = make_memory_map(params, flash_size * 0x400); - int port = atoi(argv[1]); + int port; + + if(argc == 1) { + srand((unsigned int)&port); + port = rand()/65535; + } + else { + port = atoi(argv[1]); + } while(serve(sl, port) == 0);