2009-11-17 02:11:46 +00:00
|
|
|
/* MSPDebug - debugging tool for the eZ430
|
2012-09-13 02:33:04 +00:00
|
|
|
* Copyright (C) 2009-2012 Daniel Beer
|
2009-11-17 02:11:46 +00:00
|
|
|
*
|
|
|
|
* 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>
|
2009-11-17 02:11:46 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
2010-05-13 00:57:21 +00:00
|
|
|
#include <stdint.h>
|
2009-11-17 02:11:46 +00:00
|
|
|
#include <unistd.h>
|
2010-11-08 20:16:31 +00:00
|
|
|
|
2012-09-13 02:33:04 +00:00
|
|
|
#include "comport.h"
|
2010-01-08 05:03:51 +00:00
|
|
|
#include "util.h"
|
2010-08-16 23:07:03 +00:00
|
|
|
#include "output.h"
|
2011-07-26 23:26:02 +00:00
|
|
|
#include "sport.h"
|
2009-11-17 02:11:46 +00:00
|
|
|
|
2012-09-13 02:33:04 +00:00
|
|
|
struct comport_transport {
|
2010-04-30 04:57:57 +00:00
|
|
|
struct transport base;
|
2009-11-17 02:11:46 +00:00
|
|
|
|
2011-07-26 23:26:02 +00:00
|
|
|
sport_t serial_fd;
|
2010-04-30 04:57:57 +00:00
|
|
|
};
|
|
|
|
|
2010-05-13 00:57:21 +00:00
|
|
|
static int serial_send(transport_t tr_base, const uint8_t *data, int len)
|
2009-11-17 02:11:46 +00:00
|
|
|
{
|
2012-09-13 02:33:04 +00:00
|
|
|
struct comport_transport *tr = (struct comport_transport *)tr_base;
|
2009-11-17 02:11:46 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_SERIAL
|
2010-04-21 02:12:12 +00:00
|
|
|
debug_hexdump("Serial transfer out:", data, len);
|
2009-11-17 02:11:46 +00:00
|
|
|
#endif
|
|
|
|
|
2011-07-26 23:26:02 +00:00
|
|
|
if (sport_write_all(tr->serial_fd, data, len) < 0) {
|
2012-09-13 02:33:04 +00:00
|
|
|
pr_error("comport: write error");
|
2010-01-11 03:54:36 +00:00
|
|
|
return -1;
|
2009-11-17 02:11:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-05-13 00:57:21 +00:00
|
|
|
static int serial_recv(transport_t tr_base, uint8_t *data, int max_len)
|
2009-11-17 02:11:46 +00:00
|
|
|
{
|
2012-09-13 02:33:04 +00:00
|
|
|
struct comport_transport *tr = (struct comport_transport *)tr_base;
|
2010-01-08 05:39:07 +00:00
|
|
|
int r;
|
2009-11-17 02:11:46 +00:00
|
|
|
|
2011-07-26 23:26:02 +00:00
|
|
|
r = sport_read(tr->serial_fd, data, max_len);
|
2010-01-11 03:54:36 +00:00
|
|
|
if (r < 0) {
|
2012-09-13 02:33:04 +00:00
|
|
|
pr_error("comport: read error");
|
2010-01-11 03:54:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2009-11-17 02:11:46 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG_SERIAL
|
2010-04-21 02:12:12 +00:00
|
|
|
debug_hexdump("Serial transfer in", data, r);
|
2009-11-17 02:11:46 +00:00
|
|
|
#endif
|
2010-01-08 05:39:07 +00:00
|
|
|
return r;
|
2009-11-17 02:11:46 +00:00
|
|
|
}
|
|
|
|
|
2010-04-30 04:57:57 +00:00
|
|
|
static void serial_destroy(transport_t tr_base)
|
2009-11-17 02:11:46 +00:00
|
|
|
{
|
2012-09-13 02:33:04 +00:00
|
|
|
struct comport_transport *tr = (struct comport_transport *)tr_base;
|
2009-11-17 02:11:46 +00:00
|
|
|
|
2011-07-26 23:26:02 +00:00
|
|
|
sport_close(tr->serial_fd);
|
2010-04-30 04:57:57 +00:00
|
|
|
free(tr);
|
2009-11-17 02:11:46 +00:00
|
|
|
}
|
|
|
|
|
2012-09-10 02:22:00 +00:00
|
|
|
static int serial_flush(transport_t tr_base)
|
|
|
|
{
|
2012-09-13 02:33:04 +00:00
|
|
|
struct comport_transport *tr = (struct comport_transport *)tr_base;
|
2012-09-10 02:22:00 +00:00
|
|
|
|
|
|
|
if (sport_flush(tr->serial_fd) < 0) {
|
2012-09-13 02:33:04 +00:00
|
|
|
pr_error("comport: flush failed");
|
2012-09-10 02:22:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int serial_set_modem(transport_t tr_base, transport_modem_t state)
|
|
|
|
{
|
2012-09-13 02:33:04 +00:00
|
|
|
struct comport_transport *tr = (struct comport_transport *)tr_base;
|
2012-09-10 02:22:00 +00:00
|
|
|
int bits = 0;
|
|
|
|
|
|
|
|
if (state & TRANSPORT_MODEM_DTR)
|
|
|
|
bits |= SPORT_MC_DTR;
|
|
|
|
|
|
|
|
if (state & TRANSPORT_MODEM_RTS)
|
|
|
|
bits |= SPORT_MC_RTS;
|
|
|
|
|
|
|
|
if (sport_set_modem(tr->serial_fd, bits) < 0) {
|
2012-09-13 02:33:04 +00:00
|
|
|
pr_error("comport: failed to set modem control lines\n");
|
2012-09-10 02:22:00 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-09-13 02:33:04 +00:00
|
|
|
static const struct transport_class comport_transport = {
|
2012-09-10 02:22:00 +00:00
|
|
|
.destroy = serial_destroy,
|
|
|
|
.send = serial_send,
|
|
|
|
.recv = serial_recv,
|
|
|
|
.flush = serial_flush,
|
|
|
|
.set_modem = serial_set_modem
|
|
|
|
};
|
|
|
|
|
2012-09-13 02:33:04 +00:00
|
|
|
transport_t comport_open(const char *device, int baud_rate)
|
2010-01-08 05:39:07 +00:00
|
|
|
{
|
2012-09-13 02:33:04 +00:00
|
|
|
struct comport_transport *tr = malloc(sizeof(*tr));
|
2010-01-08 05:39:07 +00:00
|
|
|
|
2010-04-30 04:57:57 +00:00
|
|
|
if (!tr) {
|
2012-09-13 02:33:04 +00:00
|
|
|
pr_error("comport: couldn't allocate memory");
|
2010-04-30 04:57:57 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2010-01-08 05:39:07 +00:00
|
|
|
|
2012-09-13 02:33:04 +00:00
|
|
|
tr->base.ops = &comport_transport;
|
2009-11-17 02:11:46 +00:00
|
|
|
|
2012-09-13 02:33:04 +00:00
|
|
|
printc("Trying to open %s at %d bps...\n", device, baud_rate);
|
|
|
|
tr->serial_fd = sport_open(device, baud_rate, 0);
|
2011-07-26 13:21:44 +00:00
|
|
|
if (SPORT_ISERR(tr->serial_fd)) {
|
2012-09-13 02:33:04 +00:00
|
|
|
printc_err("comport: can't open serial device: %s: %s\n",
|
2011-07-26 13:51:06 +00:00
|
|
|
device, last_error());
|
2010-04-30 04:57:57 +00:00
|
|
|
free(tr);
|
2010-01-06 01:48:08 +00:00
|
|
|
return NULL;
|
2009-11-17 02:11:46 +00:00
|
|
|
}
|
|
|
|
|
2012-09-13 02:33:04 +00:00
|
|
|
if (sport_set_modem(tr->serial_fd, 0) < 0)
|
|
|
|
pr_error("warning: comport: failed to set "
|
|
|
|
"modem control lines");
|
|
|
|
|
2010-04-30 04:57:57 +00:00
|
|
|
return (transport_t)tr;
|
2009-11-17 02:11:46 +00:00
|
|
|
}
|