mspdebug/uif.c

108 lines
2.5 KiB
C
Raw Normal View History

/* MSPDebug - debugging tool for the eZ430
2010-01-08 08:17:24 +00:00
* 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>
2010-04-30 04:57:57 +00:00
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
2010-05-13 00:57:21 +00:00
#include <stdint.h>
#include <unistd.h>
#include <termios.h>
2010-04-30 04:57:57 +00:00
#include "uif.h"
2010-01-08 05:03:51 +00:00
#include "util.h"
#ifdef __APPLE__
#define B460800 460800
#define B500000 500000
#endif
2010-04-30 04:57:57 +00:00
struct uif_transport {
struct transport base;
2010-04-30 04:57:57 +00:00
int serial_fd;
};
2010-05-13 00:57:21 +00:00
static int serial_send(transport_t tr_base, const uint8_t *data, int len)
{
2010-04-30 04:57:57 +00:00
struct uif_transport *tr = (struct uif_transport *)tr_base;
#ifdef DEBUG_SERIAL
debug_hexdump("Serial transfer out:", data, len);
#endif
2010-04-30 04:57:57 +00:00
if (write_all(tr->serial_fd, data, len) < 0) {
perror("uif: write error");
return -1;
}
return 0;
}
2010-05-13 00:57:21 +00:00
static int serial_recv(transport_t tr_base, uint8_t *data, int max_len)
{
2010-04-30 04:57:57 +00:00
struct uif_transport *tr = (struct uif_transport *)tr_base;
int r;
2010-04-30 04:57:57 +00:00
r = read_with_timeout(tr->serial_fd, data, max_len);
if (r < 0) {
perror("uif: read error");
return -1;
}
#ifdef DEBUG_SERIAL
debug_hexdump("Serial transfer in", data, r);
#endif
return r;
}
2010-04-30 04:57:57 +00:00
static void serial_destroy(transport_t tr_base)
{
2010-04-30 04:57:57 +00:00
struct uif_transport *tr = (struct uif_transport *)tr_base;
2010-04-30 04:57:57 +00:00
close(tr->serial_fd);
free(tr);
}
transport_t uif_open(const char *device, int is_olimex)
{
2010-04-30 04:57:57 +00:00
struct uif_transport *tr = malloc(sizeof(*tr));
2010-04-30 04:57:57 +00:00
if (!tr) {
perror("uif: couldn't allocate memory");
return NULL;
}
2010-04-30 04:57:57 +00:00
tr->base.send = serial_send;
tr->base.recv = serial_recv;
tr->base.destroy = serial_destroy;
printf("Trying to open UIF on %s...\n", device);
tr->serial_fd = open_serial(device, is_olimex ? B500000 : B460800);
2010-04-30 04:57:57 +00:00
if (tr->serial_fd < 0) {
fprintf(stderr, "uif: can't open serial device: %s: %s\n",
device, strerror(errno));
2010-04-30 04:57:57 +00:00
free(tr);
return NULL;
}
2010-04-30 04:57:57 +00:00
return (transport_t)tr;
}