Implemented UIF support (untested).

RF2500 support is tested and still working.
This commit is contained in:
Daniel Beer 2009-11-17 15:11:46 +13:00
parent 22955480b0
commit 3e0f736e1d
7 changed files with 306 additions and 72 deletions

View File

@ -25,7 +25,7 @@ clean:
.SUFFIXES: .c .o
mspdebug: main.o fet.o rf2500.o dis.o
mspdebug: main.o fet.o rf2500.o dis.o uif.o
$(CC) $(CFLAGS) -o $@ $^ -lusb
.c.o:

155
fet.c
View File

@ -24,6 +24,7 @@
#include "fet.h"
static const struct fet_transport *fet_transport;
static int fet_is_rf2500;
/*********************************************************************
* Checksum calculation
@ -88,7 +89,7 @@ static u_int16_t calc_checksum(const char *data, int len)
*
* No checksums are included.
*/
static int fet_send_data(const char *data, int len)
static int send_rf2500_data(const char *data, int len)
{
int offset = 0;
@ -119,7 +120,7 @@ static int fet_len;
#define BUFFER_BYTE(b, x) ((int)((u_int8_t *)(b))[x])
#define BUFFER_WORD(b, x) ((BUFFER_BYTE(b, x + 1) << 8) | BUFFER_BYTE(b, x))
static const char *fet_recv_packet(int *pktlen)
static const char *recv_packet(int *pktlen)
{
int plen = BUFFER_WORD(fet_buf, 0);
@ -144,7 +145,7 @@ static const char *fet_recv_packet(int *pktlen)
*pktlen = plen - 2;
if (c != r) {
fprintf(stderr, "fet_fecv_packet: checksum "
fprintf(stderr, "recv_packet: checksum "
"error (calc %04x, recv %04x)\n",
c, r);
return NULL;
@ -163,22 +164,36 @@ static const char *fet_recv_packet(int *pktlen)
return NULL;
}
static int fet_send_command(const char *data, int len)
static int send_command(const char *data, int datalen,
const char *extra, int exlen)
{
char datapkt[256];
int len = 0;
char buf[256];
u_int16_t cksum = calc_checksum(data, len);
u_int16_t cksum;
int i = 0;
int j;
assert (len + 4 <= sizeof(buf));
assert (len + 2 <= sizeof(datapkt));
assert (len + exlen + 2 <= sizeof(datapkt));
assert (fet_transport != NULL);
memcpy(datapkt, data, len);
/* Assemble the unescaped undelimeted packet into datapkt. */
memcpy(datapkt, data, datalen);
len += datalen;
if (extra) {
memcpy(datapkt + len, extra, exlen);
len += exlen;
}
cksum = calc_checksum(datapkt, len);
datapkt[len++] = cksum & 0xff;
datapkt[len++] = cksum >> 8;
/* Copy into buf, escaping special characters and adding
* delimeters.
*/
buf[i++] = 0x7e;
for (j = 0; j < len; j++) {
char c = datapkt[j];
@ -197,19 +212,26 @@ static int fet_send_command(const char *data, int len)
return fet_transport->send(buf, i);
}
static const char *fet_send_recv(const char *data, int len, int *recvlen)
static const char *xfer(const char *command, int len,
const char *data, int datalen,
int *recvlen)
{
const char *buf;
if (fet_send_command(data, len) < 0)
if (data && fet_is_rf2500) {
if (send_rf2500_data(data, datalen) < 0)
return NULL;
if (send_command(command, len, NULL, 0) < 0)
return NULL;
} else if (send_command(command, len, data, datalen) < 0)
return NULL;
buf = fet_recv_packet(recvlen);
buf = recv_packet(recvlen);
if (!buf)
return NULL;
if (data[0] != buf[0]) {
fprintf(stderr, "fet_send_recv: reply type mismatch\n");
if (command[0] != buf[0]) {
fprintf(stderr, "xfer: reply type mismatch\n");
return NULL;
}
@ -232,56 +254,58 @@ int fet_open(const struct fet_transport *tr, int proto_flags, int vcc_mv)
};
fet_transport = tr;
fet_is_rf2500 = proto_flags & FET_PROTO_RF2500;
init_codes();
/* open */
if (!fet_send_recv("\x01\x01", 2, NULL)) {
fprintf(stderr, "fet_startup: open failed\n");
if (!xfer("\x01\x01", 2, NULL, 0, NULL)) {
fprintf(stderr, "fet_open: open failed\n");
return -1;
}
/* init */
if (!fet_send_recv("\x27\x02\x01\x00\x04\x00\x00\x00\x00", 8, NULL)) {
fprintf(stderr, "fet_startup: init failed\n");
if (!xfer("\x27\x02\x01\x00\x04\x00\x00\x00\x00", 8, NULL, 0, NULL)) {
fprintf(stderr, "fet_open: init failed\n");
return -1;
}
/* configure: Spy-Bi-Wire or JTAG */
config[8] = (proto_flags & FET_PROTO_SPYBIWIRE) ? 1 : 0;
if (!fet_send_recv(config, 12, NULL)) {
fprintf(stderr, "fet_startup: configure failed\n");
if (!xfer(config, 12, NULL, 0, NULL)) {
fprintf(stderr, "fet_open: configure failed\n");
return -1;
}
/* I don't know what this is. It's RF2500-specific. It may have
* something to do with flash -- 0x1d is sent before an erase.
*/
if (!fet_send_recv("\x1e\x01", 2, NULL)) {
fprintf(stderr, "fet_startup: command 0x1e failed\n");
if (fet_is_rf2500 && !xfer("\x1e\x01", 2, NULL, 0, NULL)) {
fprintf(stderr, "fet_open: command 0x1e failed\n");
return -1;
}
/* set VCC */
vcc[4] = vcc_mv & 0xff;
vcc[5] = vcc_mv >> 8;
if (!fet_send_recv(vcc, 8, NULL)) {
fprintf(stderr, "fet_startup: set VCC failed\n");
if (!xfer(vcc, 8, NULL, 0, NULL)) {
fprintf(stderr, "fet_open: set VCC failed\n");
return -1;
}
/* I don't know what this is, but it appears to halt the MSP. Without
* it, memory reads return garbage. This is RF2500-specific.
*/
if (!fet_send_recv("\x28\x02\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00",
12, NULL)) {
fprintf(stderr, "fet_startup: command 0x28 failed\n");
if (fet_is_rf2500 &&
!xfer("\x28\x02\x02\x00\x00\x00\x00\x00\x00\x00\x00\x00",
12, NULL, 0, NULL)) {
fprintf(stderr, "fet_open: command 0x28 failed\n");
return -1;
}
/* Who knows what this is. Without it, register reads don't work.
* This is RF2500-specific.
*/
{
if (fet_is_rf2500) {
static char data[] = {
0x00, 0x80, 0xff, 0xff, 0x00, 0x00, 0x00, 0x10,
0xff, 0x10, 0x40, 0x00, 0x00, 0x02, 0xff, 0x05,
@ -295,11 +319,10 @@ int fet_open(const struct fet_transport *tr, int proto_flags, int vcc_mv)
0xff, 0xff,
};
if (fet_send_data(data, sizeof(data)) < 0 ||
!fet_send_recv("\x29\x02\x04\x00\x00\x00\x00\x00"
if (!xfer("\x29\x02\x04\x00\x00\x00\x00\x00"
"\x39\x00\x00\x00\x31\x00\x00\x00"
"\x4a\x00\x00\x00", 20, NULL)) {
fprintf(stderr, "fet_startup: command 0x29 failed\n");
"\x4a\x00\x00\x00", 20, data, sizeof(data), NULL)) {
fprintf(stderr, "fet_open: command 0x29 failed\n");
return -1;
}
}
@ -326,7 +349,7 @@ int fet_reset(int flags)
reset[12] = 1;
}
if (!fet_send_recv(reset, 16, NULL)) {
if (!xfer(reset, 16, NULL, 0, NULL)) {
fprintf(stderr, "fet_reset: reset failed\n");
return -1;
}
@ -336,7 +359,7 @@ int fet_reset(int flags)
int fet_close(void)
{
if (!fet_send_recv("\x02\x02\x01\x00", 4, NULL)) {
if (!xfer("\x02\x02\x01\x00", 4, NULL, 0, NULL)) {
fprintf(stderr, "fet_shutdown: close command failed\n");
return -1;
}
@ -353,7 +376,7 @@ int fet_get_context(u_int16_t *regs)
int i;
const char *buf;
buf = fet_send_recv("\x08\x01", 2, &len);
buf = xfer("\x08\x01", 2, NULL, 0, &len);
if (len < 72) {
fprintf(stderr, "fet_get_context: short reply (%d bytes)\n",
len);
@ -378,9 +401,21 @@ int fet_set_context(u_int16_t *regs)
buf[i * 4 + 1] = regs[i] >> 8;
}
if (fet_send_data(buf, sizeof(buf)) < 0 ||
!fet_send_recv("\x09\x02\x02\x00\xff\xff\x00\x00"
"\x40\x00\x00\x00", 12, NULL)) {
static char cmd[] = {
0x09, 0x00, 0x00, 0x00,
0xff, 0xff, 0x00, 0x00,
0x40, 0x00, 0x00, 0x00
};
if (fet_is_rf2500) {
cmd[1] = 0x02;
cmd[2] = 0x02;
} else {
cmd[1] = 0x04;
cmd[2] = 0x01;
}
if (!xfer(cmd, 12, buf, sizeof(buf), NULL)) {
fprintf(stderr, "fet_set_context: context set failed\n");
return -1;
}
@ -404,7 +439,7 @@ int fet_read_mem(u_int16_t addr, char *buffer, int count)
readmem[5] = addr >> 8;
readmem[8] = plen;
buf = fet_send_recv(readmem, 12, &len);
buf = xfer(readmem, 12, NULL, 0, &len);
if (!buf) {
fprintf(stderr, "fet_read_mem: failed to read "
"from 0x%04x\n", addr);
@ -432,16 +467,23 @@ int fet_write_mem(u_int16_t addr, char *buffer, int count)
int plen = count > 128 ? 128 : count;
static char writemem[] = {
0x0e, 0x02, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00
};
if (fet_is_rf2500) {
writemem[1] = 0x02;
writemem[2] = 0x02;
} else {
writemem[1] = 0x04;
writemem[2] = 0x01;
}
writemem[4] = addr & 0xff;
writemem[5] = addr >> 8;
writemem[8] = plen;
if (fet_send_data(buffer, plen) < 0 ||
!fet_send_recv(writemem, 12, NULL)) {
if (!xfer(writemem, 12, buffer, plen, NULL)) {
fprintf(stderr, "fet_write_mem: failed to write "
"to 0x%04x\n", addr);
return -1;
@ -489,24 +531,24 @@ int fet_erase(int type, u_int16_t addr)
break;
}
if (!fet_send_recv("\x1d\x01", 2, NULL)) {
if (!xfer("\x1d\x01", 2, NULL, 0, NULL)) {
fprintf(stderr, "fet_erase: command 1d failed\n");
return -1;
}
if (!fet_send_recv("\x05\x02\x02\x00\x02\x00\x00\x00\x26\x00\x00\x00",
12, NULL)) {
if (!xfer("\x05\x02\x02\x00\x02\x00\x00\x00\x26\x00\x00\x00",
12, NULL, 0, NULL)) {
fprintf(stderr, "fet_erase: config (1) failed\n");
return -1;
}
if (!fet_send_recv("\x05\x02\x02\x00\x05\x00\x00\x00\x00\x00\x00\x00",
12, NULL)) {
if (!xfer("\x05\x02\x02\x00\x05\x00\x00\x00\x00\x00\x00\x00",
12, NULL, 0, NULL)) {
fprintf(stderr, "fet_erase: config (2) failed\n");
return -1;
}
if (!fet_send_recv(erase, 16, NULL)) {
if (!xfer(erase, 16, NULL, 0, NULL)) {
fprintf(stderr, "fet_erase: erase command failed\n");
return -1;
}
@ -523,7 +565,7 @@ int fet_poll(void)
if (usleep(500000) < 0)
return -1;
reply = fet_send_recv("\x12\x02\x01\x00\x00\x00\x00\x00", 8, &len);
reply = xfer("\x12\x02\x01\x00\x00\x00\x00\x00", 8, NULL, 0, &len);
if (!reply) {
fprintf(stderr, "fet_poll: polling failed\n");
return -1;
@ -534,8 +576,8 @@ int fet_poll(void)
int fet_step(void)
{
if (!fet_send_recv("\x11\x02\x02\x00\x02\x00\x00\x00\x00\x00\x00\x00",
12, NULL)) {
if (!xfer("\x11\x02\x02\x00\x02\x00\x00\x00\x00\x00\x00\x00",
12, NULL, 0, NULL)) {
fprintf(stderr, "fet_step: failed to single-step\n");
return -1;
}
@ -545,8 +587,8 @@ int fet_step(void)
int fet_run(void)
{
if (!fet_send_recv("\x11\x02\x02\x00\x03\x00\x00\x00\x00\x00\x00\x00",
12, NULL)) {
if (!xfer("\x11\x02\x02\x00\x03\x00\x00\x00\x00\x00\x00\x00",
12, NULL, 0, NULL)) {
fprintf(stderr, "fet_run: run failed\n");
return -1;
}
@ -556,7 +598,7 @@ int fet_run(void)
int fet_stop(void)
{
if (!fet_send_recv("\x12\x02\x01\x00\x01\x00\x00\x00", 8, NULL)) {
if (!xfer("\x12\x02\x01\x00\x01\x00\x00\x00", 8, NULL, 0, NULL)) {
fprintf(stderr, "fet_stop: stop failed\n");
return -1;
}
@ -593,13 +635,12 @@ int fet_break(int enable, u_int16_t addr)
buf[52] = 1;
}
if (fet_send_data(buf, sizeof(buf)) < 0 ||
!fet_send_recv("\x2a\x02\x04\x00\x08\x00\x00\x00\xb0\x00\x00\x00"
"\x00\x00\x00\x00\x40\x00\x00\x00", 20, NULL)) {
if (!xfer("\x2a\x02\x04\x00\x08\x00\x00\x00\xb0\x00\x00\x00"
"\x00\x00\x00\x00\x40\x00\x00\x00", 20,
buf, sizeof(buf), NULL)) {
fprintf(stderr, "fet_break: set breakpoint failed\n");
return -1;
}
return 0;
}

1
fet.h
View File

@ -36,6 +36,7 @@ struct fet_transport {
* millivolts and a set of protocol mode flags.
*/
#define FET_PROTO_SPYBIWIRE 0x01
#define FET_PROTO_RF2500 0x02
int fet_open(const struct fet_transport *transport,
int proto_flags, int vcc_mv);

60
main.c
View File

@ -22,12 +22,14 @@
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include "dis.h"
#include "fet.h"
#include "rf2500.h"
#include "uif.h"
static void hexdump(int addr, const char *data, int len)
void hexdump(int addr, const char *data, int len)
{
int offset = 0;
@ -542,24 +544,15 @@ static void sigint_handler(int signum)
{
}
int main(void)
static void reader_loop(void)
{
const static struct sigaction siga = {
.sa_handler = sigint_handler,
.sa_flags = 0
};
puts(
"MSPDebug version 0.1+ - debugging tool for the eZ430\n"
"Copyright (C) 2009 Daniel Beer <dlbeer@gmail.com>\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");
if (rf2500_open() < 0)
return -1;
sigaction(SIGINT, &siga, NULL);
cmd_help(NULL);
sigaction(SIGINT, &siga, NULL);
for (;;) {
char buf[128];
@ -595,6 +588,49 @@ int main(void)
}
printf("\n");
}
static void usage(const char *progname)
{
fprintf(stderr, "Usage: %s [-u device]\n"
"By default, the first RF2500 device on the USB bus is opened. If -u is\n"
"given, then a UIF device attached to the specified serial port is\n"
"opened.\n", progname);
}
int main(int argc, char **argv)
{
const char *uif_device = NULL;
int opt;
int result;
puts(
"MSPDebug version 0.1+ - debugging tool for the eZ430\n"
"Copyright (C) 2009 Daniel Beer <dlbeer@gmail.com>\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");
while ((opt = getopt(argc, argv, "u:")) >= 0)
switch (opt) {
case 'u':
uif_device = optarg;
break;
default:
usage(argv[0]);
return -1;
}
/* Open the appropriate device */
if (uif_device)
result = uif_open(uif_device);
else
result = rf2500_open();
if (result < 0)
return -1;
reader_loop();
fet_run();
fet_close();

View File

@ -22,6 +22,8 @@
#include "fet.h"
void hexdump(int addr, const char *data, int len);
/*********************************************************************
* USB transport
*
@ -195,7 +197,8 @@ int rf2500_open(void)
usbtr_flush();
if (fet_open(&usbtr_transport,
FET_PROTO_SPYBIWIRE, 3000) < 0) {
FET_PROTO_SPYBIWIRE |
FET_PROTO_RF2500, 3000) < 0) {
usbtr_close();
return -1;
}

125
uif.c Normal file
View File

@ -0,0 +1,125 @@
/* MSPDebug - debugging tool for the eZ430
* Copyright (C) 2009 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 <string.h>
#include <errno.h>
#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include "uif.h"
#include "fet.h"
void hexdump(int addr, const char *data, int len);
static int serial_fd = -1;
static int serial_send(const char *data, int len)
{
int result;
assert (serial_fd >= 0);
#ifdef DEBUG_SERIAL
puts("Serial transfer out:");
hexdump(0, data, len);
#endif
while (len) {
result = write(serial_fd, data, len);
if (result < 0) {
if (errno == EINTR)
continue;
perror("serial_send");
return -1;
}
data += result;
len -= result;
}
return 0;
}
static int serial_recv(char *data, int max_len)
{
int len;
assert (serial_fd >= 0);
len = read(serial_fd, data, max_len);
if (len < 0) {
perror("serial_recv");
return -1;
}
#ifdef DEBUG_SERIAL
puts("Serial transfer in:");
hexdump(0, data, len);
#endif
return len;
}
static void serial_close(void)
{
assert (serial_fd >= 0);
close(serial_fd);
}
static const struct fet_transport serial_transport = {
.send = serial_send,
.recv = serial_recv,
.close = serial_close
};
int uif_open(const char *device)
{
struct termios attr;
printf("Trying to open UIF on %s...\n", device);
serial_fd = open(device, O_RDWR | O_NOCTTY);
if (serial_fd < 0) {
fprintf(stderr, "uif_open: open: %s: %s\n",
device, strerror(errno));
return -1;
}
tcgetattr(serial_fd, &attr);
cfmakeraw(&attr);
cfsetspeed(&attr, B460800);
if (tcsetattr(serial_fd, TCSAFLUSH, &attr) < 0) {
fprintf(stderr, "uif_open: tcsetattr: %s: %s\n",
device, strerror(errno));
return -1;
}
if (fet_open(&serial_transport, FET_PROTO_SPYBIWIRE, 3000) < 0) {
close(serial_fd);
return -1;
}
return 0;
}

28
uif.h Normal file
View File

@ -0,0 +1,28 @@
/* MSPDebug - debugging tool for the eZ430
* Copyright (C) 2009 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
*/
#ifndef UIF_H_
#define UIF_H_
/* This function is for opening an eZ430-F2013 or FET430UIF device via
* a kernel-supported serial interface. The argument given should be the
* filename of the relevant tty device.
*/
int uif_open(const char *device);
#endif