source: rtems-source-builder/source-builder/patches/texane-stlink-3494c11-2.diff @ eaab26a

4.11
Last change on this file since eaab26a was c80560d, checked in by Chris Johns <chrisj@…>, on 11/05/12 at 23:09:40

Move into the source-builder tree.

  • Property mode set to 100644
File size: 17.2 KB
  • configure.ac

    diff --git a/configure.ac b/configure.ac
    index 1f00f3a..eb140e5 100644
    a b AC_CHECK_HEADERS(sys/poll.h) 
    2121AC_REPLACE_FUNCS(mmap)
    2222
    2323# Checks for libraries.
    24 PKG_CHECK_MODULES(USB, libusb-1.0 >= 1.0.0,,
    25                  AC_MSG_ERROR([*** Required libusb-1.0 >= 1.0.0 not installed ***]))
    26 AC_CHECK_LIB([usbpath],[usb_path2devnum],,,-lusb)
     24AC_CHECK_HEADER([libusb.h], [USB_LIBS="-lusb-1.0"])
    2725
    2826LIBS="$LIBS $USB_LIBS"
    2927CFLAGS="$CFLAGS $USB_CFLAGS"
  • gdbserver/gdb-server.c

    diff --git a/gdbserver/gdb-server.c b/gdbserver/gdb-server.c
    index 9d27ae4..fd25e8b 100644
    a b  
    11/* -*- tab-width:8 -*- */
    2 #define DEBUG 0
     2
    33/*
    44 Copyright (C)  2011 Peter Zotov <whitequark@whitequark.org>
    55 Use of this source code is governed by a BSD-style
    66 license that can be found in the LICENSE file.
    77*/
    88
     9#include <stdarg.h>
    910#include <getopt.h>
    1011#include <stdio.h>
    1112#include <string.h>
     
    1819#include <sys/socket.h>
    1920#include <netinet/in.h>
    2021#include <arpa/inet.h>
    21 #include <signal.h>
    2222#endif
     23#include <signal.h>
    2324
    2425#include <stlink-common.h>
    2526
    2627#include "gdb-remote.h"
    2728
    28 #define DEFAULT_LOGGING_LEVEL 50
     29#define DEFAULT_LOGGING_LEVEL   0
     30#define LOGGING_LEVEL_GDBSERVER  2
    2931#define DEFAULT_GDB_LISTEN_PORT 4242
    3032
    3133#define STRINGIFY_inner(name) #name
    static const char hex[] = "0123456789abcdef"; 
    4042
    4143static const char* current_memory_map = NULL;
    4244
     45FILE *my_stderr;
     46FILE *my_stdout;
     47
    4348typedef struct _st_state_t {
    44     // things from command line, bleh
    45     int stlink_version;
    46     // "/dev/serial/by-id/usb-FTDI_TTL232R-3V3_FTE531X6-if00-port0" is only 58 chars
    47     char devicename[100];
    48     int logging_level;
     49        // things from command line, bleh
     50        int pipe;
     51        int stlink_version;
     52        // "/dev/serial/by-id/usb-FTDI_TTL232R-3V3_FTE531X6-if00-port0" is only 58 chars
     53        char devicename[100];
     54        int logging_level;
    4955        int listen_port;
    5056} st_state_t;
    5157
     58int logging_level = DEFAULT_LOGGING_LEVEL;
     59
     60int remote_desc = -1;
     61int remote_piping = 0;
    5262
    53 int serve(stlink_t *sl, int port);
     63int remote_open(int port);
     64int serve(stlink_t *sl);
    5465char* make_memory_map(stlink_t *sl);
    5566
     67/*
     68 * Own printf and redirect when piping.
     69 */
     70int
     71printf (const char *format,...)
     72{
     73        int ret;
     74        va_list args;
     75        va_start (args, format);
     76        ret =  vfprintf (my_stdout, format, args);
     77        fflush (my_stdout);
     78        return ret;
     79}
    5680
    5781int parse_options(int argc, char** argv, st_state_t *st) {
    5882    static struct option long_options[] = {
    5983        {"help", no_argument, NULL, 'h'},
    6084        {"verbose", optional_argument, NULL, 'v'},
     85        {"pipe", no_argument, NULL, 'P'},
    6186        {"device", required_argument, NULL, 'd'},
    6287        {"stlink_version", required_argument, NULL, 's'},
    6388        {"stlinkv1", no_argument, NULL, '1'},
    int parse_options(int argc, char** argv, st_state_t *st) { 
    78103        "(default port: " STRINGIFY(DEFAULT_GDB_LISTEN_PORT) ")\n"
    79104        ;
    80105
    81 
    82106    int option_index = 0;
    83107    int c;
    84108    int q;
    85     while ((c = getopt_long(argc, argv, "hv::d:s:1p:", long_options, &option_index)) != -1) {
     109    while ((c = getopt_long(argc, argv, "hv::d:s:1Pp:", long_options, &option_index)) != -1) {
    86110        switch (c) {
    87111        case 0:
    88112            printf("XXXXX Shouldn't really normally come here, only if there's no corresponding option\n");
    int parse_options(int argc, char** argv, st_state_t *st) { 
    96120            printf(help_str, argv[0]);
    97121            exit(EXIT_SUCCESS);
    98122            break;
     123        case 'P':
     124            st->pipe = 1;
     125            break;
    99126        case 'v':
    100127            if (optarg) {
    101128                st->logging_level = atoi(optarg);
    102129            } else {
    103130                st->logging_level = DEFAULT_LOGGING_LEVEL;
    104131            }
     132            logging_level = st->logging_level;
    105133            break;
    106134        case 'd':
    107135            if (strlen(optarg) > sizeof (st->devicename)) {
    int main(int argc, char** argv) { 
    149177        st_state_t state;
    150178        memset(&state, 0, sizeof(state));
    151179        // set defaults...
     180        state.pipe = 0;
    152181        state.stlink_version = 2;
    153182        state.logging_level = DEFAULT_LOGGING_LEVEL;
    154183        state.listen_port = DEFAULT_GDB_LISTEN_PORT;
    155184        parse_options(argc, argv, &state);
     185
     186        my_stdout = stdout;
     187        my_stderr = stderr;
     188
     189        if (state.pipe) {
     190               my_stdout = my_stderr = stderr;
     191               state.listen_port = 0;
     192               remote_piping = 1;
     193        }
     194
    156195        switch (state.stlink_version) {
    157196        case 2:
    158197                sl = stlink_open_usb(state.logging_level);
    int main(int argc, char** argv) { 
    162201                sl = stlink_v1_open(state.logging_level);
    163202                if(sl == NULL) return 1;
    164203                break;
    165     }
     204        }
     205
     206#ifdef __MINGW32__
     207        if (!remote_piping) {
     208                WSADATA wsadata;
     209                if (WSAStartup(MAKEWORD(2,2),&wsadata) !=0 ) {
     210                        goto winsock_error;
     211                }
     212        }
     213#endif
     214
     215        remote_open(state.listen_port);
    166216
    167217        printf("Chip ID is %08x, Core ID is  %08x.\n", sl->chip_id, sl->core_id);
    168218
    int main(int argc, char** argv) { 
    170220
    171221        current_memory_map = make_memory_map(sl);
    172222
    173 #ifdef __MINGW32__
    174         WSADATA wsadata;
    175         if (WSAStartup(MAKEWORD(2,2),&wsadata) !=0 ) {
    176                 goto winsock_error;
    177         }
    178 #endif
    179 
    180         while(serve(sl, state.listen_port) == 0);
     223        while(serve(sl) == 0);
    181224
    182225#ifdef __MINGW32__
     226        if (!remote_piping) {
    183227winsock_error:
    184         WSACleanup();
     228                WSACleanup();
     229        }
    185230#endif
    186231
    187232        /* Switch back to mass storage mode before closing. */
    struct code_hw_watchpoint { 
    342387struct code_hw_watchpoint data_watches[DATA_WATCH_NUM];
    343388
    344389static void init_data_watchpoints(stlink_t *sl) {
    345         #ifdef DEBUG
    346         printf("init watchpoints\n");
    347         #endif
     390        if (logging_level >= LOGGING_LEVEL_GDBSERVER)
     391                printf("init watchpoints\n");
    348392
    349393        // set trcena in debug command to turn on dwt unit
    350394        stlink_write_debug32(sl, 0xE000EDFC,
    static int add_data_watchpoint(stlink_t *sl, enum watchfun wf, stm32_addr_t addr 
    377421                for(i = 0; i < DATA_WATCH_NUM; i++) {
    378422                        // is this an empty slot ?
    379423                        if(data_watches[i].fun == WATCHDISABLED) {
    380                                 #ifdef DEBUG
    381                                 printf("insert watchpoint %d addr %x wf %u mask %u len %d\n", i, addr, wf, mask, len);
    382                                 #endif
     424                                if (logging_level >= LOGGING_LEVEL_GDBSERVER)
     425                                        printf("insert watchpoint %d addr %x wf %u mask %u len %d\n", i, addr, wf, mask, len);
    383426
    384427                                data_watches[i].fun = wf;
    385428                                data_watches[i].addr = addr;
    static int add_data_watchpoint(stlink_t *sl, enum watchfun wf, stm32_addr_t addr 
    401444                }
    402445        }
    403446
    404         #ifdef DEBUG
    405         printf("failure: add watchpoints addr %x wf %u len %u\n", addr, wf, len);
    406         #endif
     447
     448        if (logging_level >= LOGGING_LEVEL_GDBSERVER)
     449                printf("failure: add watchpoints addr %x wf %u len %u\n", addr, wf, len);
     450
    407451        return -1;
    408452}
    409453
    static int delete_data_watchpoint(stlink_t *sl, stm32_addr_t addr) 
    413457
    414458        for(i = 0 ; i < DATA_WATCH_NUM; i++) {
    415459                if((data_watches[i].addr == addr) && (data_watches[i].fun != WATCHDISABLED)) {
    416                         #ifdef DEBUG
    417                         printf("delete watchpoint %d addr %x\n", i, addr);
    418                         #endif
     460                        if (logging_level >= LOGGING_LEVEL_GDBSERVER)
     461                                printf("delete watchpoint %d addr %x\n", i, addr);
    419462
    420463                        data_watches[i].fun = WATCHDISABLED;
    421464                        stlink_write_debug32(sl, 0xe0001028 + i * 16, 0);
    static int delete_data_watchpoint(stlink_t *sl, stm32_addr_t addr) 
    424467                }
    425468        }
    426469
    427         #ifdef DEBUG
    428         printf("failure: delete watchpoint addr %x\n", addr);
    429         #endif
     470        if (logging_level >= LOGGING_LEVEL_GDBSERVER)
     471                printf("failure: delete watchpoint addr %x\n", addr);
    430472
    431473        return -1;
    432474}
    static int update_code_breakpoint(stlink_t *sl, stm32_addr_t addr, int set) { 
    485527        else    brk->type &= ~type;
    486528
    487529        if(brk->type == 0) {
    488                 #ifdef DEBUG
    489                 printf("clearing hw break %d\n", id);
    490                 #endif
     530                if (logging_level >= LOGGING_LEVEL_GDBSERVER)
     531                        printf("clearing hw break %d\n", id);
    491532
    492533                stlink_write_debug32(sl, 0xe0002008 + id * 4, 0);
    493534        } else {
    494535                uint32_t mask = (brk->addr) | 1 | (brk->type << 30);
    495536
    496                 #ifdef DEBUG
    497                 printf("setting hw break %d at %08x (%d)\n",
    498                         id, brk->addr, brk->type);
    499                 printf("reg %08x \n",
    500                         mask);
    501                 #endif
     537                if (logging_level >= LOGGING_LEVEL_GDBSERVER) {
     538                        printf("setting hw break %d at %08x (%d)\n",
     539                                id, brk->addr, brk->type);
     540                        printf("reg %08x \n",
     541                                mask);
     542                }
    502543
    503544                stlink_write_debug32(sl, 0xe0002008 + id * 4, mask);
    504545        }
    static int flash_go(stlink_t *sl) { 
    589630        stlink_reset(sl);
    590631
    591632        for(struct flash_block* fb = flash_root; fb; fb = fb->next) {
    592                 #ifdef DEBUG
    593                 printf("flash_do: block %08x -> %04x\n", fb->addr, fb->length);
    594                 #endif
     633                if (logging_level >= LOGGING_LEVEL_GDBSERVER)
     634                        printf("flash_do: block %08x -> %04x\n", fb->addr, fb->length);
    595635
    596636                unsigned length = fb->length;
    597637                for(stm32_addr_t page = fb->addr; page < fb->addr + fb->length; page += FLASH_PAGE) {
    static int flash_go(stlink_t *sl) { 
    599639                        //Update FLASH_PAGE
    600640                        stlink_calculate_pagesize(sl, page);
    601641
    602                         #ifdef DEBUG
    603                         printf("flash_do: page %08x\n", page);
    604                         #endif
     642                        if (logging_level >= LOGGING_LEVEL_GDBSERVER)
     643                                printf("flash_do: page %08x\n", page);
    605644
    606645                        if(stlink_write_flash(sl, page, fb->data + (page - fb->addr),
    607646                                        length > FLASH_PAGE ? FLASH_PAGE : length) < 0)
    error: 
    625664        return error;
    626665}
    627666
    628 int serve(stlink_t *sl, int port) {
    629         int sock = socket(AF_INET, SOCK_STREAM, 0);
    630         if(sock < 0) {
    631                 perror("socket");
    632                 return 1;
    633         }
    634 
    635         unsigned int val = 1;
    636         setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val));
     667int remote_open(int port) {
     668        int sock = -1;
     669        if (port == 0) {
     670                remote_desc = STDOUT_FILENO;
     671                remote_piping = 1;
     672                signal(SIGINT, SIG_IGN);
     673#ifdef __MINGW32__
     674                if (_setmode (_fileno( stdout ), _O_BINARY) < 0)
     675                        fprintf(stderr, "cannot change stdout mode to binary");
     676                if (_setmode (_fileno( stdin ), _O_BINARY) < 0)
     677                        fprintf(stderr, "cannot change stdin mode to binary");
     678#else
     679                signal(SIGIO, SIG_IGN);
     680                signal(SIGCHLD, SIG_IGN);
     681#endif
     682        }
     683        else {
     684                sock = socket(AF_INET, SOCK_STREAM, 0);
     685                if(sock < 0) {
     686                        perror("socket");
     687                        return 1;
     688                }
    637689
    638         struct sockaddr_in serv_addr;
    639         memset(&serv_addr,0,sizeof(struct sockaddr_in));
    640         serv_addr.sin_family = AF_INET;
    641         serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    642         serv_addr.sin_port = htons(port);
     690                unsigned int val = 1;
     691                setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&val, sizeof(val));
    643692
    644         if(bind(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
    645                 perror("bind");
    646                 return 1;
    647         }
     693                struct sockaddr_in serv_addr;
     694                memset(&serv_addr,0,sizeof(struct sockaddr_in));
     695                serv_addr.sin_family = AF_INET;
     696                serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
     697                serv_addr.sin_port = htons(port);
    648698
    649         if(listen(sock, 5) < 0) {
    650                 perror("listen");
    651                 return 1;
    652         }
     699                if(bind(sock, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) {
     700                        perror("bind");
     701                        return 1;
     702                }
    653703
    654         stlink_force_debug(sl);
    655         stlink_reset(sl);
    656         init_code_breakpoints(sl);
    657         init_data_watchpoints(sl);
     704                if(listen(sock, 5) < 0) {
     705                        perror("listen");
     706                        return 1;
     707                }
     708        }
    658709
    659         printf("Listening at *:%d...\n", port);
     710        if (!remote_piping) {
     711                printf("Listening at *:%d...\n", port);
    660712
    661         int client = accept(sock, NULL, NULL);
    662         //signal (SIGINT, SIG_DFL);
    663         if(client < 0) {
    664                 perror("accept");
    665                 return 1;
    666         }
     713                remote_desc = accept(sock, NULL, NULL);
     714                //signal (SIGINT, SIG_DFL);
     715                if(remote_desc < 0) {
     716                        perror("accept");
     717                        return 1;
     718                }
    667719
    668         close(sock);
     720                close(sock);
     721        }
    669722
    670723        printf("GDB connected.\n");
    671724
     725        return 0;
     726}
     727
     728int serve(stlink_t *sl) {
     729
     730        stlink_force_debug(sl);
     731        stlink_reset(sl);
     732        init_code_breakpoints(sl);
     733        init_data_watchpoints(sl);
     734
    672735        /*
    673736         * To allow resetting the chip from GDB it is required to
    674737         * emulate attaching and detaching to target.
    int serve(stlink_t *sl, int port) { 
    678741        while(1) {
    679742                char* packet;
    680743
    681                 int status = gdb_recv_packet(client, &packet);
     744                int status = gdb_recv_packet(remote_desc, &packet);
    682745                if(status < 0) {
    683746                        fprintf(stderr, "cannot recv: %d\n", status);
    684747                        return 1;
    685748                }
    686749
    687                 #ifdef DEBUG
    688                 printf("recv: %s\n", packet);
    689                 #endif
     750                if (logging_level >= LOGGING_LEVEL_GDBSERVER)
     751                        printf("recv: %s\n", packet);
    690752
    691753                char* reply = NULL;
    692754                reg regp;
    int serve(stlink_t *sl, int port) { 
    709771                        char* queryName = calloc(queryNameLength + 1, 1);
    710772                        strncpy(queryName, &packet[1], queryNameLength);
    711773
    712                         #ifdef DEBUG
    713                         printf("query: %s;%s\n", queryName, params);
    714                         #endif
     774                        if (logging_level >= LOGGING_LEVEL_GDBSERVER)
     775                                printf("query: %s;%s\n", queryName, params);
    715776
    716777                        if(!strcmp(queryName, "Supported")) {
    717778                if(sl->chip_id==STM32_CHIPID_F4) {
    int serve(stlink_t *sl, int port) { 
    734795                                unsigned addr = strtoul(__s_addr, NULL, 16),
    735796                                       length = strtoul(s_length, NULL, 16);
    736797
    737                                 #ifdef DEBUG
    738                                 printf("Xfer: type:%s;op:%s;annex:%s;addr:%d;length:%d\n",
    739                                         type, op, annex, addr, length);
    740                                 #endif
     798                                if (logging_level >= LOGGING_LEVEL_GDBSERVER)
     799                                        printf("Xfer: type:%s;op:%s;annex:%s;addr:%d;length:%d\n",
     800                                                type, op, annex, addr, length);
    741801
    742802                                const char* data = NULL;
    743803
    int serve(stlink_t *sl, int port) { 
    771831
    772832
    773833                                if (!strncmp(params,"726573756d65",12)) {// resume
    774 #ifdef DEBUG
    775                                         printf("Rcmd: resume\n");
    776 #endif
     834                                       if (logging_level >= LOGGING_LEVEL_GDBSERVER)
     835                                                printf("Rcmd: resume\n");
    777836                                        stlink_run(sl);
    778837
    779838                                        reply = strdup("OK");
    int serve(stlink_t *sl, int port) { 
    782841
    783842                                        stlink_force_debug(sl);
    784843
    785 #ifdef DEBUG
    786                                         printf("Rcmd: halt\n");
    787 #endif
     844                                       if (logging_level >= LOGGING_LEVEL_GDBSERVER)
     845                                                printf("Rcmd: halt\n");
     846
    788847                } else if (!strncmp(params,"6a7461675f7265736574",20)) { //jtag_reset
    789848                                        reply = strdup("OK");
    790849
    int serve(stlink_t *sl, int port) { 
    792851                                        stlink_jtag_reset(sl, 0);
    793852                                        stlink_force_debug(sl);
    794853
    795 #ifdef DEBUG
    796                                         printf("Rcmd: jtag_reset\n");
    797 #endif
     854                                       if (logging_level >= LOGGING_LEVEL_GDBSERVER)
     855                                                printf("Rcmd: jtag_reset\n");
     856
    798857                } else if (!strncmp(params,"7265736574",10)) { //reset
    799858                                        reply = strdup("OK");
    800859
    int serve(stlink_t *sl, int port) { 
    803862                                        init_code_breakpoints(sl);
    804863                                        init_data_watchpoints(sl);
    805864
    806 #ifdef DEBUG
    807                                         printf("Rcmd: reset\n");
    808 #endif
    809                                 } else {
    810 #ifdef DEBUG
    811                                         printf("Rcmd: %s\n", params);
    812 #endif
     865                                       if (logging_level >= LOGGING_LEVEL_GDBSERVER)
     866                                                printf("Rcmd: reset\n");
    813867
     868                                } else {
     869                                       if (logging_level >= LOGGING_LEVEL_GDBSERVER)
     870                                                printf("Rcmd: %s\n", params);
    814871                                }
    815872
    816873                        }
    int serve(stlink_t *sl, int port) { 
    839896                                unsigned addr = strtoul(__s_addr, NULL, 16),
    840897                                       length = strtoul(s_length, NULL, 16);
    841898
    842                                 #ifdef DEBUG
    843                                 printf("FlashErase: addr:%08x,len:%04x\n",
    844                                         addr, length);
    845                                 #endif
     899                                if (logging_level >= LOGGING_LEVEL_GDBSERVER)
     900                                        printf("FlashErase: addr:%08x,len:%04x\n",
     901                                                addr, length);
    846902
    847903                                if(flash_add_block(addr, length, sl) < 0) {
    848904                                        reply = strdup("E00");
    int serve(stlink_t *sl, int port) { 
    877933                                if(dec_index % 2 != 0)
    878934                                        dec_index++;
    879935
    880                                 #ifdef DEBUG
    881                                 printf("binary packet %d -> %d\n", data_length, dec_index);
    882                                 #endif
     936                                if (logging_level >= LOGGING_LEVEL_GDBSERVER)
     937                                        printf("binary packet %d -> %d\n", data_length, dec_index);
    883938
    884939                                if(flash_populate(addr, decoded, dec_index) < 0) {
    885940                                        reply = strdup("E00");
    int serve(stlink_t *sl, int port) { 
    908963                        stlink_run(sl);
    909964
    910965                        while(1) {
    911                                 int status = gdb_check_for_interrupt(client);
     966                                int status = gdb_check_for_interrupt(remote_desc);
    912967                                if(status < 0) {
    913968                                        fprintf(stderr, "cannot check for int: %d\n", status);
    914969                                        return 1;
    int serve(stlink_t *sl, int port) { 
    11901245                }
    11911246
    11921247                if(reply) {
    1193                         #ifdef DEBUG
    1194                         printf("send: %s\n", reply);
    1195                         #endif
     1248                        if (logging_level >= LOGGING_LEVEL_GDBSERVER)
     1249                                printf("send: %s\n", reply);
    11961250
    1197                         int result = gdb_send_packet(client, reply);
     1251                        int result = gdb_send_packet(remote_desc, reply);
    11981252                        if(result != 0) {
    11991253                                fprintf(stderr, "cannot send: %d\n", result);
    12001254                                return 1;
Note: See TracBrowser for help on using the repository browser.