This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
/
serbb_posix.c
312 lines (261 loc) · 6.68 KB
/
serbb_posix.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
* avrdude - A Downloader/Uploader for AVR device programmers
* Copyright (C) 2000, 2001, 2002, 2003 Brian S. Dean <[email protected]>
* Copyright (C) 2005 Michael Holzt <[email protected]>
* Copyright (C) 2006 Joerg Wunsch <[email protected]>
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* $Id$ */
/*
* Posix serial bitbanging interface for avrdude.
*/
#if !defined(WIN32NATIVE)
#include "ac_cfg.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <termios.h>
#include "avrdude.h"
#include "avr.h"
#include "pindefs.h"
#include "pgm.h"
#include "bitbang.h"
#undef DEBUG
static struct termios oldmode;
/*
serial port/pin mapping
1 cd <-
2 (rxd) <-
3 txd ->
4 dtr ->
5 GND
6 dsr <-
7 rts ->
8 cts <-
9 ri <-
*/
#define DB9PINS 9
static int serregbits[DB9PINS + 1] =
{ 0, TIOCM_CD, 0, 0, TIOCM_DTR, 0, TIOCM_DSR, TIOCM_RTS, TIOCM_CTS, TIOCM_RI };
#ifdef DEBUG
static char *serpins[DB9PINS + 1] =
{ "NONE", "CD", "RXD", "TXD", "DTR", "GND", "DSR", "RTS", "CTS", "RI" };
#endif
static int serbb_setpin(PROGRAMMER * pgm, int pin, int value)
{
unsigned int ctl;
int r;
if (pin & PIN_INVERSE)
{
value = !value;
pin &= PIN_MASK;
}
if ( pin < 1 || pin > DB9PINS )
return -1;
#ifdef DEBUG
printf("%s to %d\n",serpins[pin],value);
#endif
switch ( pin )
{
case 3: /* txd */
r = ioctl(pgm->fd.ifd, value ? TIOCSBRK : TIOCCBRK, 0);
if (r < 0) {
perror("ioctl(\"TIOCxBRK\")");
return -1;
}
break;
case 4: /* dtr */
case 7: /* rts */
r = ioctl(pgm->fd.ifd, TIOCMGET, &ctl);
if (r < 0) {
perror("ioctl(\"TIOCMGET\")");
return -1;
}
if ( value )
ctl |= serregbits[pin];
else
ctl &= ~(serregbits[pin]);
r = ioctl(pgm->fd.ifd, TIOCMSET, &ctl);
if (r < 0) {
perror("ioctl(\"TIOCMSET\")");
return -1;
}
break;
default: /* impossible */
return -1;
}
if (pgm->ispdelay > 1)
bitbang_delay(pgm->ispdelay);
return 0;
}
static int serbb_getpin(PROGRAMMER * pgm, int pin)
{
unsigned int ctl;
unsigned char invert;
int r;
if (pin & PIN_INVERSE)
{
invert = 1;
pin &= PIN_MASK;
} else
invert = 0;
if ( pin < 1 || pin > DB9PINS )
return(-1);
switch ( pin )
{
case 2: /* rxd, currently not implemented, FIXME */
return(-1);
case 1: /* cd */
case 6: /* dsr */
case 8: /* cts */
case 9: /* ri */
r = ioctl(pgm->fd.ifd, TIOCMGET, &ctl);
if (r < 0) {
perror("ioctl(\"TIOCMGET\")");
return -1;
}
if ( !invert )
{
#ifdef DEBUG
printf("%s is %d\n",serpins[pin],(ctl & serregbits[pin]) ? 1 : 0 );
#endif
return ( (ctl & serregbits[pin]) ? 1 : 0 );
}
else
{
#ifdef DEBUG
printf("%s is %d (~)\n",serpins[pin],(ctl & serregbits[pin]) ? 0 : 1 );
#endif
return (( ctl & serregbits[pin]) ? 0 : 1 );
}
default: /* impossible */
return(-1);
}
}
static int serbb_highpulsepin(PROGRAMMER * pgm, int pin)
{
if ( (pin & PIN_MASK) < 1 || (pin & PIN_MASK) > DB9PINS )
return -1;
serbb_setpin(pgm, pin, 1);
serbb_setpin(pgm, pin, 0);
return 0;
}
static void serbb_display(PROGRAMMER *pgm, const char *p)
{
/* MAYBE */
}
static void serbb_enable(PROGRAMMER *pgm)
{
/* nothing */
}
static void serbb_disable(PROGRAMMER *pgm)
{
/* nothing */
}
static void serbb_powerup(PROGRAMMER *pgm)
{
/* nothing */
}
static void serbb_powerdown(PROGRAMMER *pgm)
{
/* nothing */
}
static int serbb_open(PROGRAMMER *pgm, char *port)
{
struct termios mode;
int flags;
int r;
bitbang_check_prerequisites(pgm);
/* adapted from uisp code */
pgm->fd.ifd = open(port, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (pgm->fd.ifd < 0) {
perror(port);
return(-1);
}
r = tcgetattr(pgm->fd.ifd, &mode);
if (r < 0) {
fprintf(stderr, "%s: ", port);
perror("tcgetattr");
return(-1);
}
oldmode = mode;
mode.c_iflag = IGNBRK | IGNPAR;
mode.c_oflag = 0;
mode.c_cflag = CLOCAL | CREAD | CS8 | B9600;
mode.c_cc [VMIN] = 1;
mode.c_cc [VTIME] = 0;
r = tcsetattr(pgm->fd.ifd, TCSANOW, &mode);
if (r < 0) {
fprintf(stderr, "%s: ", port);
perror("tcsetattr");
return(-1);
}
/* Clear O_NONBLOCK flag. */
flags = fcntl(pgm->fd.ifd, F_GETFL, 0);
if (flags == -1)
{
fprintf(stderr, "%s: Can not get flags: %s\n",
progname, strerror(errno));
return(-1);
}
flags &= ~O_NONBLOCK;
if (fcntl(pgm->fd.ifd, F_SETFL, flags) == -1)
{
fprintf(stderr, "%s: Can not clear nonblock flag: %s\n",
progname, strerror(errno));
return(-1);
}
return(0);
}
static void serbb_close(PROGRAMMER *pgm)
{
if (pgm->fd.ifd != -1)
{
(void)tcsetattr(pgm->fd.ifd, TCSANOW, &oldmode);
pgm->setpin(pgm, pgm->pinno[PIN_AVR_RESET], 1);
close(pgm->fd.ifd);
}
return;
}
void serbb_initpgm(PROGRAMMER *pgm)
{
strcpy(pgm->type, "SERBB");
pgm->rdy_led = bitbang_rdy_led;
pgm->err_led = bitbang_err_led;
pgm->pgm_led = bitbang_pgm_led;
pgm->vfy_led = bitbang_vfy_led;
pgm->initialize = bitbang_initialize;
pgm->display = serbb_display;
pgm->enable = serbb_enable;
pgm->disable = serbb_disable;
pgm->powerup = serbb_powerup;
pgm->powerdown = serbb_powerdown;
pgm->program_enable = bitbang_program_enable;
pgm->chip_erase = bitbang_chip_erase;
pgm->cmd = bitbang_cmd;
pgm->open = serbb_open;
pgm->close = serbb_close;
pgm->setpin = serbb_setpin;
pgm->getpin = serbb_getpin;
pgm->highpulsepin = serbb_highpulsepin;
pgm->read_byte = avr_read_byte_default;
pgm->write_byte = avr_write_byte_default;
}
#endif /* WIN32NATIVE */