mspdebug/main.c

415 lines
8.6 KiB
C
Raw Normal View History

/* MSPDebug - debugging tool for MSP430 MCUs
* Copyright (C) 2009, 2010 Daniel Beer
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
2010-05-18 23:48:01 +00:00
#include <getopt.h>
#include "dis.h"
#include "device.h"
#include "binfile.h"
#include "stab.h"
2010-01-08 05:03:51 +00:00
#include "util.h"
#include "gdb.h"
2010-04-08 01:15:30 +00:00
#include "rtools.h"
#include "sym.h"
2010-04-10 02:35:36 +00:00
#include "devcmd.h"
2010-05-01 05:54:10 +00:00
#include "expr.h"
2010-04-30 04:01:03 +00:00
#include "sim.h"
#include "bsl.h"
#include "fet.h"
2010-05-19 00:08:50 +00:00
#include "vector.h"
2010-05-18 23:48:01 +00:00
#include "fet_db.h"
2010-04-30 04:01:03 +00:00
2010-04-30 04:57:57 +00:00
#include "uif.h"
#include "rf2500.h"
2010-05-01 06:34:43 +00:00
static void io_prefix(stab_t stab,
2010-05-13 00:57:21 +00:00
const char *prefix, uint16_t pc,
uint16_t addr, int is_byte)
2010-04-30 04:01:03 +00:00
{
char name[64];
2010-05-01 06:34:43 +00:00
if (!stab_nearest(stab, pc, name, sizeof(name), &pc)) {
2010-04-30 04:01:03 +00:00
printf("%s", name);
if (pc)
printf("+0x%x", pc);
} else {
printf("0x%04x", pc);
}
printf(": IO %s.%c: 0x%04x", prefix, is_byte ? 'B' : 'W', addr);
2010-05-01 06:34:43 +00:00
if (!stab_nearest(stab, addr, name, sizeof(name), &addr)) {
2010-04-30 04:01:03 +00:00
printf(" (%s", name);
if (addr)
printf("+0x%x", addr);
printf(")");
}
}
2010-05-13 00:57:21 +00:00
static int fetch_io(void *user_data, uint16_t pc,
uint16_t addr, int is_byte, uint16_t *data_ret)
2010-04-30 04:01:03 +00:00
{
2010-05-01 06:34:43 +00:00
stab_t stab = (stab_t)user_data;
io_prefix(stab, "READ", pc, addr, is_byte);
2010-04-30 04:01:03 +00:00
for (;;) {
char text[128];
int len;
int data;
printf("? ");
fflush(stdout);
if (!fgets(text, sizeof(text), stdin)) {
printf("\nAborted IO request\n");
return -1;
}
len = strlen(text);
while (len && isspace(text[len - 1]))
len--;
text[len] = 0;
if (!len)
return 0;
2010-05-01 06:34:43 +00:00
if (!expr_eval(stab, text, &data)) {
2010-04-30 04:01:03 +00:00
if (data_ret)
*data_ret = data;
return 0;
}
}
return 0;
}
2010-05-13 00:57:21 +00:00
static void store_io(void *user_data, uint16_t pc,
uint16_t addr, int is_byte, uint16_t data)
2010-04-30 04:01:03 +00:00
{
2010-05-01 06:34:43 +00:00
stab_t stab = (stab_t)user_data;
io_prefix(stab, "WRITE", pc, addr, is_byte);
2010-04-30 04:01:03 +00:00
if (is_byte)
printf(" => 0x%02x\n", data & 0xff);
else
printf(" => 0x%04x\n", data);
}
static void usage(const char *progname)
{
fprintf(stderr,
2010-04-08 02:43:19 +00:00
"Usage: %s [options] -R [-v voltage] [command ...]\n"
" %s [options] -u <device> [-j] [-v voltage] [command ...]\n"
" %s [options] -B <device> [command ...]\n"
" %s [options] -s [command ...]\n"
"\n"
2010-03-17 05:40:03 +00:00
" -R\n"
" Open the first available RF2500 device on the USB bus.\n"
2010-01-04 09:34:23 +00:00
" -u device\n"
" Open the given tty device (MSP430 UIF compatible devices).\n"
" -j\n"
" Use JTAG, rather than spy-bi-wire (UIF devices only).\n"
" -v voltage\n"
" Set the supply voltage, in millivolts.\n"
" -B device\n"
" Debug the FET itself through the bootloader.\n"
" -s\n"
2010-03-20 02:17:33 +00:00
" Start in simulation mode.\n"
2010-04-08 02:43:19 +00:00
" -n\n"
" Do not read ~/.mspdebug on startup.\n"
2010-05-18 23:48:01 +00:00
" --help\n"
2010-04-08 02:43:19 +00:00
" Show this help text.\n"
2010-05-18 23:48:01 +00:00
" --fet-list\n"
" Show a list of devices supported by the FET driver.\n"
" --fet-force-id string\n"
" Override the device ID returned by the FET.\n"
2010-01-04 09:34:23 +00:00
"\n"
"By default, the first RF2500 device on the USB bus is opened.\n"
"\n"
"If commands are given, they will be executed. Otherwise, an interactive\n"
"command reader is started.\n",
2010-03-17 05:40:03 +00:00
progname, progname, progname, progname);
}
2010-04-21 04:56:56 +00:00
static void process_rc_file(cproc_t cp)
2010-04-08 02:43:19 +00:00
{
const char *home = getenv("HOME");
char text[256];
if (!home)
return;
snprintf(text, sizeof(text), "%s/.mspdebug", home);
2010-04-10 03:03:18 +00:00
if (!access(text, F_OK))
2010-04-21 04:56:56 +00:00
cproc_process_file(cp, text);
2010-04-08 02:43:19 +00:00
}
2010-03-17 05:40:03 +00:00
#define MODE_RF2500 0x01
#define MODE_UIF 0x02
#define MODE_UIF_BSL 0x04
#define MODE_SIM 0x08
2010-04-29 03:56:47 +00:00
struct cmdline_args {
const char *uif_device;
const char *bsl_device;
const char *fet_force_id;
2010-04-29 03:56:47 +00:00
int mode;
int want_jtag;
int no_rc;
int vcc_mv;
};
2010-05-19 00:08:50 +00:00
static int add_fet_device(void *user_data, const struct fet_db_record *r)
2010-05-18 23:48:01 +00:00
{
2010-05-19 00:08:50 +00:00
struct vector *v = (struct vector *)user_data;
return vector_push(v, &r->name, 1);
}
static int cmp_char_ptr(const void *a, const void *b)
{
return strcmp(*(const char **)a, *(const char **)b);
}
static int list_devices(void)
{
struct vector v;
int i;
vector_init(&v, sizeof(const char *));
if (fet_db_enum(add_fet_device, &v) < 0) {
perror("couldn't allocate memory");
vector_destroy(&v);
return -1;
}
qsort(v.ptr, v.size, v.elemsize, cmp_char_ptr);
printf("Devices supported by FET driver:\n");
for (i = 0; i < v.size; i++)
printf(" %s\n", VECTOR_AT(v, i, const char *));
vector_destroy(&v);
2010-05-18 23:48:01 +00:00
return 0;
}
2010-04-29 03:56:47 +00:00
static int parse_cmdline_args(int argc, char **argv,
struct cmdline_args *args)
{
int opt;
2010-05-18 23:48:01 +00:00
const static struct option longopts[] = {
{"help", 0, 0, 'H'},
{"fet-list", 0, 0, 'L'},
{"fet-force-id", 1, 0, 'F'},
2010-05-18 23:48:01 +00:00
{NULL, 0, 0, 0}
};
while ((opt = getopt_long(argc, argv, "u:jv:B:sR?n",
longopts, NULL)) >= 0)
switch (opt) {
2010-05-18 23:48:01 +00:00
case 'L':
2010-05-19 00:08:50 +00:00
exit(list_devices());
2010-05-18 23:48:01 +00:00
case 'F':
args->fet_force_id = optarg;
break;
case 'H':
usage(argv[0]);
exit(0);
2010-03-17 05:40:03 +00:00
case 'R':
2010-04-29 03:56:47 +00:00
args->mode |= MODE_RF2500;
2010-03-17 05:40:03 +00:00
break;
case 'u':
2010-04-29 03:56:47 +00:00
args->uif_device = optarg;
args->mode |= MODE_UIF;
break;
case 'v':
2010-04-29 03:56:47 +00:00
args->vcc_mv = atoi(optarg);
break;
2010-01-04 09:34:23 +00:00
case 'j':
2010-04-29 03:56:47 +00:00
args->want_jtag = 1;
2010-01-04 09:34:23 +00:00
break;
case 'B':
2010-04-29 03:56:47 +00:00
args->bsl_device = optarg;
args->mode |= MODE_UIF_BSL;
break;
case 's':
2010-04-29 03:56:47 +00:00
args->mode |= MODE_SIM;
break;
2010-04-08 02:43:19 +00:00
case 'n':
2010-04-29 03:56:47 +00:00
args->no_rc = 1;
2010-04-08 02:43:19 +00:00
break;
2010-03-17 05:40:03 +00:00
case '?':
return -1;
2010-03-17 05:40:03 +00:00
default:
fprintf(stderr, "Invalid argument: %c\n"
2010-05-18 23:48:01 +00:00
"Try --help for help.\n", opt);
return -1;
}
2010-03-17 05:40:03 +00:00
/* Check for incompatible arguments */
2010-04-29 03:56:47 +00:00
if (args->mode & (args->mode - 1)) {
2010-03-17 05:40:03 +00:00
fprintf(stderr, "Multiple incompatible options specified.\n"
2010-05-18 23:48:01 +00:00
"Try --help for help.\n");
2010-03-17 05:40:03 +00:00
return -1;
}
2010-04-29 03:56:47 +00:00
if (!args->mode) {
2010-03-17 05:40:03 +00:00
fprintf(stderr, "You need to specify an operating mode.\n"
2010-05-18 23:48:01 +00:00
"Try --help for help.\n");
2010-03-17 05:40:03 +00:00
return -1;
}
2010-04-29 03:56:47 +00:00
return 0;
}
2010-05-01 06:34:43 +00:00
device_t setup_device(const struct cmdline_args *args,
stab_t stab)
2010-04-29 03:56:47 +00:00
{
2010-04-30 04:01:03 +00:00
device_t msp430_dev = NULL;
2010-04-30 04:57:57 +00:00
transport_t trans = NULL;
/* Open a device */
2010-04-29 03:56:47 +00:00
if (args->mode == MODE_SIM) {
2010-05-01 06:34:43 +00:00
msp430_dev = sim_open(fetch_io, store_io, stab);
2010-04-29 03:56:47 +00:00
} else if (args->mode == MODE_UIF_BSL) {
msp430_dev = bsl_open(args->bsl_device);
} else if (args->mode == MODE_RF2500 || args->mode == MODE_UIF) {
int flags = 0;
/* Open the appropriate transport */
2010-04-29 03:56:47 +00:00
if (args->mode == MODE_UIF) {
trans = uif_open(args->uif_device);
} else {
trans = rf2500_open();
flags |= FET_PROTO_RF2500;
}
if (!trans)
2010-04-29 03:56:47 +00:00
return NULL;
/* Then initialize the device */
2010-04-29 03:56:47 +00:00
if (!args->want_jtag)
flags |= FET_PROTO_SPYBIWIRE;
msp430_dev = fet_open(trans, flags, args->vcc_mv,
args->fet_force_id);
}
2010-04-29 03:56:47 +00:00
if (!msp430_dev) {
if (trans)
2010-04-30 04:57:57 +00:00
trans->destroy(trans);
2010-04-29 03:56:47 +00:00
return NULL;
}
return msp430_dev;
}
2010-04-30 04:01:03 +00:00
cproc_t setup_cproc(const struct cmdline_args *args)
{
2010-05-01 06:34:43 +00:00
device_t msp430_dev;
stab_t stab;
2010-04-30 04:01:03 +00:00
cproc_t cp;
2010-05-01 06:34:43 +00:00
stab = stab_new();
if (!stab)
return NULL;
msp430_dev = setup_device(args, stab);
if (!msp430_dev) {
stab_destroy(stab);
2010-04-30 04:01:03 +00:00
return NULL;
2010-05-01 06:34:43 +00:00
}
2010-04-30 04:01:03 +00:00
2010-05-01 06:34:43 +00:00
cp = cproc_new(msp430_dev, stab);
2010-04-30 04:01:03 +00:00
if (!cp) {
msp430_dev->destroy(msp430_dev);
2010-05-01 06:34:43 +00:00
stab_destroy(stab);
2010-04-30 04:01:03 +00:00
return NULL;
}
if (sym_register(cp) < 0 ||
devcmd_register(cp) < 0 ||
gdb_register(cp) < 0 ||
rtools_register(cp) < 0) {
cproc_destroy(cp);
return NULL;
}
return cp;
}
2010-04-29 03:56:47 +00:00
int main(int argc, char **argv)
{
struct cmdline_args args = {0};
cproc_t cp;
int ret = 0;
puts(
2010-04-30 05:16:58 +00:00
"MSPDebug version 0.7 - debugging tool for MSP430 MCUs\n"
2010-04-29 03:56:47 +00:00
"Copyright (C) 2009, 2010 Daniel Beer <daniel@tortek.co.nz>\n"
"This is free software; see the source for copying conditions. There is NO\n"
"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
args.vcc_mv = 3000;
if (parse_cmdline_args(argc, argv, &args) < 0)
return -1;
ctrlc_init();
2010-04-30 04:01:03 +00:00
cp = setup_cproc(&args);
2010-05-01 06:34:43 +00:00
if (!cp)
2010-04-21 04:56:56 +00:00
return -1;
2010-04-29 03:56:47 +00:00
if (!args.no_rc)
2010-04-21 04:56:56 +00:00
process_rc_file(cp);
/* Process commands */
if (optind < argc) {
2010-04-08 01:15:30 +00:00
while (optind < argc) {
2010-04-21 04:56:56 +00:00
if (cproc_process_command(cp, argv[optind++]) < 0) {
2010-04-08 01:15:30 +00:00
ret = -1;
break;
}
}
} else {
2010-04-21 04:56:56 +00:00
cproc_reader_loop(cp);
}
2010-04-29 03:56:47 +00:00
cproc_destroy(cp);
2010-03-27 09:50:02 +00:00
2010-04-08 01:15:30 +00:00
return ret;
}