From: Dmitry Bravikov Date: Wed, 5 Oct 2011 10:15:44 +0000 (+0800) Subject: added installation, added ST_LINK autodetection X-Git-Url: https://git.gag.com/?p=fw%2Fstlink;a=commitdiff_plain;h=dbda7343d4a4d8e4ae1a7c5094d0942db501a7f5 added installation, added ST_LINK autodetection --- diff --git a/INSTALL b/INSTALL new file mode 100644 index 0000000..49e761c --- /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 c50747f..8004d3f 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 04fd2bc..5e12da0 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);