File: comport.c

package info (click to toggle)
mspdebug 0.25-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 4,700 kB
  • sloc: ansic: 67,821; makefile: 201; xml: 19
file content (142 lines) | stat: -rw-r--r-- 3,452 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* MSPDebug - debugging tool for the eZ430
 * Copyright (C) 2009-2012 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 <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdint.h>
#include <unistd.h>

#include "comport.h"
#include "util.h"
#include "output.h"
#include "sport.h"

struct comport_transport {
	struct transport        base;

	sport_t                 serial_fd;
};

static int serial_send(transport_t tr_base, const uint8_t *data, int len)
{
	struct comport_transport *tr = (struct comport_transport *)tr_base;

#ifdef DEBUG_SERIAL
	debug_hexdump("Serial transfer out:", data, len);
#endif

	if (sport_write_all(tr->serial_fd, data, len) < 0) {
		pr_error("comport: write error");
		return -1;
	}

	return 0;
}

static int serial_recv(transport_t tr_base, uint8_t *data, int max_len)
{
	struct comport_transport *tr = (struct comport_transport *)tr_base;
	int r;

	r = sport_read(tr->serial_fd, data, max_len);
	if (r < 0) {
		pr_error("comport: read error");
		return -1;
	}

#ifdef DEBUG_SERIAL
	debug_hexdump("Serial transfer in", data, r);
#endif
	return r;
}

static void serial_destroy(transport_t tr_base)
{
	struct comport_transport *tr = (struct comport_transport *)tr_base;

	sport_close(tr->serial_fd);
	free(tr);
}

static int serial_flush(transport_t tr_base)
{
	struct comport_transport *tr = (struct comport_transport *)tr_base;

	if (sport_flush(tr->serial_fd) < 0) {
		pr_error("comport: flush failed");
		return -1;
	}

	return 0;
}

static int serial_set_modem(transport_t tr_base, transport_modem_t state)
{
	struct comport_transport *tr = (struct comport_transport *)tr_base;
	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) {
		pr_error("comport: failed to set modem control lines\n");
		return -1;
	}

	return 0;
}

static const struct transport_class comport_transport = {
	.destroy	= serial_destroy,
	.send		= serial_send,
	.recv		= serial_recv,
	.flush		= serial_flush,
	.set_modem	= serial_set_modem
};

transport_t comport_open(const char *device, int baud_rate)
{
	struct comport_transport *tr = malloc(sizeof(*tr));

	if (!tr) {
		pr_error("comport: couldn't allocate memory");
		return NULL;
	}

	tr->base.ops = &comport_transport;

	printc_dbg("Trying to open %s at %d bps...\n", device, baud_rate);
	tr->serial_fd = sport_open(device, baud_rate, 0);
	if (SPORT_ISERR(tr->serial_fd)) {
		printc_err("comport: can't open serial device: %s: %s\n",
			   device, last_error());
		free(tr);
		return NULL;
	}

	if (sport_set_modem(tr->serial_fd, 0) < 0)
		pr_error("warning: comport: failed to set "
			 "modem control lines");

	return (transport_t)tr;
}