Separated transport handling from FET control.

This commit is contained in:
Daniel Beer 2010-01-06 14:48:08 +13:00
parent d3606d810d
commit 7505ce654d
5 changed files with 71 additions and 53 deletions

25
fet.h
View File

@ -19,30 +19,7 @@
#ifndef FET_H_
#define FET_H_
#include <sys/types.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, int want_jtag);
/* Search the USB bus for the first eZ430-RF2500, and initialize it. If
* successful, 0 is returned and the fet_* functions are ready for use.
* If an error occurs, -1 is returned.
*/
int rf2500_open(void);
/* This structure is used to provide an interface to a lower-level
* transport. The transport mechanism is viewed as a stream by the FET
* controller, which handles packet encapsulation, checksums and other
* high-level functions.
*/
struct fet_transport {
int (*send)(const u_int8_t *data, int len);
int (*recv)(u_int8_t *data, int max_len);
void (*close)(void);
};
#include "transport.h"
/* Start up the FET controller by specifying a transport, a voltage in
* millivolts and a set of protocol mode flags.

20
main.c
View File

@ -671,9 +671,10 @@ static void usage(const char *progname)
int main(int argc, char **argv)
{
const struct fet_transport *trans;
const char *uif_device = NULL;
int opt;
int result;
int flags = 0;
int want_jtag = 0;
puts(
@ -697,13 +698,20 @@ int main(int argc, char **argv)
return -1;
}
/* Open the appropriate device */
/* Open the appropriate transport */
if (uif_device)
result = uif_open(uif_device, want_jtag);
else
result = rf2500_open();
trans = uif_open(uif_device);
else {
trans = rf2500_open();
flags |= FET_PROTO_RF2500;
}
if (!trans)
return -1;
if (result < 0)
/* Then initialize the device */
if (!want_jtag)
flags |= FET_PROTO_SPYBIWIRE;
if (fet_open(trans, flags, 3000) < 0)
return -1;
if (optind < argc) {

View File

@ -20,7 +20,7 @@
#include <string.h>
#include <usb.h>
#include "fet.h"
#include "transport.h"
extern void hexdump(int addr, const u_int8_t *data, int len);
@ -180,7 +180,7 @@ static const struct fet_transport usbtr_transport = {
.close = usbtr_close
};
int rf2500_open(void)
const struct fet_transport *rf2500_open(void)
{
struct usb_bus *bus;
@ -196,19 +196,11 @@ int rf2500_open(void)
dev->descriptor.idProduct == USB_FET_PRODUCT &&
!usbtr_open_device(dev)) {
usbtr_flush();
if (fet_open(&usbtr_transport,
FET_PROTO_SPYBIWIRE |
FET_PROTO_RF2500, 3000) < 0) {
usbtr_close();
return -1;
}
return 0;
return &usbtr_transport;
}
}
}
fprintf(stderr, "usbtr_open: no devices could be found\n");
return -1;
return NULL;
}

47
transport.h Normal file
View File

@ -0,0 +1,47 @@
/* 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 TRANSPORT_H_
#define TRANSPORT_H_
#include <sys/types.h>
/* This structure is used to provide an interface to a lower-level
* transport. The transport mechanism is viewed as a stream by the FET
* controller, which handles packet encapsulation, checksums and other
* high-level functions.
*/
struct fet_transport {
int (*send)(const u_int8_t *data, int len);
int (*recv)(u_int8_t *data, int max_len);
void (*close)(void);
};
/* 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.
*/
const struct fet_transport *uif_open(const char *device);
/* Search the USB bus for the first eZ430-RF2500, and initialize it. If
* successful, 0 is returned and the fet_* functions are ready for use.
* If an error occurs, -1 is returned.
*/
const struct fet_transport *rf2500_open(void);
#endif

16
uif.c
View File

@ -26,7 +26,7 @@
#include <termios.h>
#include <unistd.h>
#include "fet.h"
#include "transport.h"
extern void hexdump(int addr, const u_int8_t *data, int len);
@ -93,7 +93,7 @@ static const struct fet_transport serial_transport = {
.close = serial_close
};
int uif_open(const char *device, int want_jtag)
const struct fet_transport *uif_open(const char *device)
{
struct termios attr;
@ -103,7 +103,7 @@ int uif_open(const char *device, int want_jtag)
if (serial_fd < 0) {
fprintf(stderr, "uif_open: open: %s: %s\n",
device, strerror(errno));
return -1;
return NULL;
}
tcgetattr(serial_fd, &attr);
@ -112,14 +112,8 @@ int uif_open(const char *device, int want_jtag)
if (tcsetattr(serial_fd, TCSAFLUSH, &attr) < 0) {
fprintf(stderr, "uif_open: tcsetattr: %s: %s\n",
device, strerror(errno));
return -1;
return NULL;
}
if (fet_open(&serial_transport, want_jtag ? 0 : FET_PROTO_SPYBIWIRE,
3000) < 0) {
close(serial_fd);
return -1;
}
return 0;
return &serial_transport;
}