-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuif.c
170 lines (140 loc) · 3.92 KB
/
uif.c
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* MSPDebug - debugging tool for the eZ430
* 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>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <stdint.h>
#include <unistd.h>
#include <termios.h>
#if defined(__linux__)
#include <linux/serial.h>
#endif
#include <sys/ioctl.h>
#include <fcntl.h>
#include "uif.h"
#include "util.h"
#include "output.h"
#if defined(__APPLE__) || defined(__OpenBSD__)
#define B460800 460800
#define B500000 500000
#endif
struct uif_transport {
struct transport base;
int serial_fd;
};
static int serial_send(transport_t tr_base, const uint8_t *data, int len)
{
struct uif_transport *tr = (struct uif_transport *)tr_base;
#ifdef DEBUG_SERIAL
debug_hexdump("Serial transfer out:", data, len);
#endif
if (write_all(tr->serial_fd, data, len) < 0) {
pr_error("uif: write error");
return -1;
}
return 0;
}
static int serial_recv(transport_t tr_base, uint8_t *data, int max_len)
{
struct uif_transport *tr = (struct uif_transport *)tr_base;
int r;
r = read_with_timeout(tr->serial_fd, data, max_len);
if (r < 0) {
pr_error("uif: 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 uif_transport *tr = (struct uif_transport *)tr_base;
close(tr->serial_fd);
free(tr);
}
#if defined(__linux__)
static int open_olimex_iso(const char *device)
{
int fd = open(device, O_RDWR | O_NOCTTY);
struct termios attr;
struct serial_struct serial_info;
if (fd < 0)
return -1;
tcgetattr(fd, &attr);
cfmakeraw(&attr);
cfsetispeed(&attr, B38400);
cfsetospeed(&attr, B38400);
serial_info.flags = ASYNC_SPD_CUST;
serial_info.custom_divisor = 120;
if (ioctl(fd, TIOCSSERIAL, &serial_info) < 0) {
printc_err("open_olimex_iso: can't do ioctl TIOCSSERIAL: %s\n",
strerror(errno));
return -1;
}
if (tcsetattr(fd, TCSAFLUSH, &attr) < 0)
return -1;
return fd;
}
#else
static int open_olimex_iso(const char *device)
{
printc_err("open_olimex_iso: this driver is only supported on "
"Linux\n");
return -1;
}
#endif
transport_t uif_open(const char *device, uif_type_t type)
{
struct uif_transport *tr = malloc(sizeof(*tr));
if (!tr) {
pr_error("uif: couldn't allocate memory");
return NULL;
}
tr->base.send = serial_send;
tr->base.recv = serial_recv;
tr->base.destroy = serial_destroy;
switch (type) {
case UIF_TYPE_FET:
printc("Trying to open UIF on %s...\n", device);
tr->serial_fd = open_serial(device, B460800);
break;
case UIF_TYPE_OLIMEX:
printc("Trying to open Olimex on %s...\n", device);
tr->serial_fd = open_serial(device, B500000);
break;
case UIF_TYPE_OLIMEX_ISO:
#if defined(__linux__)
printc("Trying to open Olimex (ISO) on %s...\n", device);
tr->serial_fd = open_olimex_iso(device);
#else
printc_err("uif_open: ioctl TIOCSSERIAL not supported on this platform\n");
#endif
break;
}
if (tr->serial_fd < 0) {
printc_err("uif: can't open serial device: %s: %s\n",
device, strerror(errno));
free(tr);
return NULL;
}
return (transport_t)tr;
}