Temp fix to work with F4 up to 64k of flash, changed chip detection
[fw/stlink] / gdbserver / gdb-server.c
index 72f433ffe907ead83a0ee7ee0c7ec29af4defadb..4d549a67dcb5fd285f32ef5eae78868849a3826d 100644 (file)
@@ -1,5 +1,5 @@
 /* -*- tab-width:8 -*- */
-
+#define DEBUG 1
 /*
  Copyright (C)  2011 Peter Zotov <whitequark@whitequark.org>
  Use of this source code is governed by a BSD-style
@@ -14,6 +14,7 @@
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
+#include <signal.h>
 
 #include <stlink-common.h>
 
 #define FLASH_PAGE_MASK (~((1 << 10) - 1))
 #define FLASH_SIZE (FLASH_PAGE * 128)
 
+volatile int do_exit = 0;
+void ctrl_c(int sig)
+{
+  do_exit = 1;
+}
+
 static const char hex[] = "0123456789abcdef";
 
 static const char* current_memory_map = NULL;
 
-/*
- * Chip IDs are explained in the appropriate programming manual for the
- * DBGMCU_IDCODE register (0xE0042000)
- */
 struct chip_params {
        uint32_t chip_id;
        char* description;
@@ -43,11 +46,12 @@ struct chip_params {
        { 0x410, "F1 Medium-density device", 0x1ffff7e0,
          0x20000,  0x400, 0x5000,  0x1ffff000, 0x800  }, // table 2, pm0063
        { 0x411, "F2 device", 0, /* No flash size register found in the docs*/
-         0x100000,   0x20000, 0x20000, 0x1ff00000, 0x7800  }, // table 1, pm0059
+         0x100000,   0x20000, 0x20000, 0x1fff0000, 0x7800  }, // table 1, pm0059
        { 0x412, "F1 Low-density device", 0x1ffff7e0,
          0x8000,   0x400, 0x2800,  0x1ffff000, 0x800  }, // table 1, pm0063
+         /*No flash size register? page size is variable */
        { 0x413, "F4 device", 0x1FFF7A10,
-         0x100000,   0x20000, 0x20000,  0x1ff00000, 0x7800  }, // table 1, pm0081
+         0x100000,   0x4000, 0x30000,  0x1fff0000, 0x7800  }, // table 1, pm0081
        { 0x414, "F1 High-density device", 0x1ffff7e0,
          0x80000,  0x800, 0x10000, 0x1ffff000, 0x800  },  // table 3 pm0063 
           // This ignores the EEPROM! (and uses the page erase size,
@@ -79,15 +83,10 @@ int main(int argc, char** argv) {
 
        switch(argc) {
 
-               default: {
-                       fprintf(stderr, HelpStr, NULL);
-                       return 1;
-               }
-
                case 3 : {
                        //sl = stlink_quirk_open(argv[2], 0);
-                        // FIXME - hardcoded to usb....
-                        sl = stlink_open_usb(argv[2], 10);
+                       // FIXME - hardcoded to usb....
+                       sl = stlink_open_usb(10);
                        if(sl == NULL) return 1;
                        break;
                }
@@ -99,6 +98,7 @@ int main(int argc, char** argv) {
                        }
                }
 
+#if CONFIG_USE_LIBSG
                case 1 : { // Search ST-LINK (from /dev/sg0 to /dev/sg99)
                        const int DevNumMax = 99;
                        int ExistDevCount = 0;
@@ -136,23 +136,31 @@ int main(int argc, char** argv) {
                        }
                        break;
                }
+#endif
+
+               default: {
+                       fprintf(stderr, HelpStr, NULL);
+                       return 1;
+               }
        }
 
-        if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE) {
-            stlink_exit_dfu_mode(sl);
-        }
+       if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE) {
+               stlink_exit_dfu_mode(sl);
+       }
 
        if(stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE) {
          stlink_enter_swd_mode(sl);
        }
 
-       uint32_t chip_id = stlink_chip_id(sl);
-       printf("Chip ID is %08x.\n", chip_id);
+       stlink_identify_device(sl);
+       printf("Chip ID is %08x, Core ID is  %08x.\n", sl->chip_id, sl->core_id);
+
+       sl->verbose=0;
 
        const struct chip_params* params = NULL;
 
        for(int i = 0; i < sizeof(devices) / sizeof(devices[0]); i++) {
-               if(devices[i].chip_id == (chip_id & 0xFFF)) {
+               if(devices[i].chip_id == (sl->chip_id & 0xFFF)) {
                        params = &devices[i];
                        break;
                }
@@ -169,11 +177,16 @@ int main(int argc, char** argv) {
 
        FLASH_PAGE = params->flash_pagesize;
 
+       //sl->flash_pgsz=0x4000;
+       //sl->flash_size=0x100000;
+
        uint32_t flash_size;
 
        stlink_read_mem32(sl, params->flash_size_reg, 4);
        flash_size = sl->q_buf[0] | (sl->q_buf[1] << 8);
 
+       //flash_size=0x100000;
+
        printf("Flash size is %d KiB.\n", flash_size);
        // memory map is in 1k blocks.
        current_memory_map = make_memory_map(params, flash_size * 0x400);
@@ -182,6 +195,9 @@ int main(int argc, char** argv) {
 
        while(serve(sl, port) == 0);
 
+       /* Switch back to mass storage mode before closing. */
+       stlink_run(sl);
+       stlink_exit_debug_mode(sl);
        stlink_close(sl);
 
        return 0;
@@ -521,6 +537,7 @@ static int flash_go(stlink_t *sl) {
                        printf("flash_do: page %08x\n", page);
                        #endif
 
+                       //todo:write flash already does erase so why is this here?
                        stlink_erase_flash_page(sl, page);
 
                        if(stlink_write_flash(sl, page, fb->data + (page - fb->addr),
@@ -578,7 +595,9 @@ int serve(stlink_t *sl, int port) {
 
        printf("Listening at *:%d...\n", port);
 
+       (void) signal (SIGINT, ctrl_c);
        int client = accept(sock, NULL, NULL);
+       signal (SIGINT, SIG_DFL);
        if(client < 0) {
                perror("accept");
                return 1;