This repository has been archived by the owner on Jul 2, 2024. It is now read-only.
forked from avrdudes/avrdude
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ppi.c
236 lines (185 loc) · 4.38 KB
/
ppi.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
/*
* avrdude - A Downloader/Uploader for AVR device programmers
* Copyright (C) 2000-2004 Brian S. Dean <[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, see <http://www.gnu.org/licenses/>.
*/
/* $Id$ */
#if !defined(WIN32NATIVE)
#include "ac_cfg.h"
#if HAVE_PARPORT
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
# include "freebsd_ppi.h"
#elif defined(__linux__)
# include "linux_ppdev.h"
#elif defined(__sun__) || defined(__sun) /* Solaris */
# include "solaris_ecpp.h"
#endif
#include "avrdude.h"
#include "libavrdude.h"
#include "ppi.h"
enum {
PPI_READ,
PPI_WRITE,
PPI_SHADOWREAD
};
static int ppi_shadow_access(union filedescriptor *fdp, int reg,
unsigned char *v, unsigned char action)
{
static unsigned char shadow[3];
int shadow_num;
switch (reg) {
case PPIDATA:
shadow_num = 0;
break;
case PPICTRL:
shadow_num = 1;
break;
case PPISTATUS:
shadow_num = 2;
break;
default:
avrdude_message(MSG_INFO, "%s: avr_set(): invalid register=%d\n",
progname, reg);
return -1;
break;
}
switch (action) {
case PPI_SHADOWREAD:
*v = shadow[shadow_num];
break;
case PPI_READ:
DO_PPI_READ(fdp->ifd, reg, v);
shadow[shadow_num]=*v;
break;
case PPI_WRITE:
shadow[shadow_num]=*v;
DO_PPI_WRITE(fdp->ifd, reg, v);
break;
}
return 0;
}
/*
* set the indicated bit of the specified register.
*/
int ppi_set(union filedescriptor *fdp, int reg, int bit)
{
unsigned char v;
int rc;
rc = ppi_shadow_access(fdp, reg, &v, PPI_SHADOWREAD);
v |= bit;
rc |= ppi_shadow_access(fdp, reg, &v, PPI_WRITE);
if (rc)
return -1;
return 0;
}
/*
* clear the indicated bit of the specified register.
*/
int ppi_clr(union filedescriptor *fdp, int reg, int bit)
{
unsigned char v;
int rc;
rc = ppi_shadow_access(fdp, reg, &v, PPI_SHADOWREAD);
v &= ~bit;
rc |= ppi_shadow_access(fdp, reg, &v, PPI_WRITE);
if (rc)
return -1;
return 0;
}
/*
* get the indicated bit of the specified register.
*/
int ppi_get(union filedescriptor *fdp, int reg, int bit)
{
unsigned char v;
int rc;
rc = ppi_shadow_access(fdp, reg, &v, PPI_READ);
v &= bit;
if (rc)
return -1;
return v; /* v == bit */
}
/*
* toggle the indicated bit of the specified register.
*/
int ppi_toggle(union filedescriptor *fdp, int reg, int bit)
{
unsigned char v;
int rc;
rc = ppi_shadow_access(fdp, reg, &v, PPI_SHADOWREAD);
v ^= bit;
rc |= ppi_shadow_access(fdp, reg, &v, PPI_WRITE);
if (rc)
return -1;
return 0;
}
/*
* get all bits of the specified register.
*/
int ppi_getall(union filedescriptor *fdp, int reg)
{
unsigned char v;
int rc;
rc = ppi_shadow_access(fdp, reg, &v, PPI_READ);
if (rc)
return -1;
return v; /* v == bit */
}
/*
* set all bits of the specified register to val.
*/
int ppi_setall(union filedescriptor *fdp, int reg, int val)
{
unsigned char v;
int rc;
v = val;
rc = ppi_shadow_access(fdp, reg, &v, PPI_WRITE);
if (rc)
return -1;
return 0;
}
void ppi_open(char * port, union filedescriptor *fdp)
{
int fd;
unsigned char v;
fd = open(port, O_RDWR);
if (fd < 0) {
avrdude_message(MSG_INFO, "%s: can't open device \"%s\": %s\n",
progname, port, strerror(errno));
fdp->ifd = -1;
return;
}
ppi_claim (fd);
/*
* Initialize shadow registers
*/
ppi_shadow_access (fdp, PPIDATA, &v, PPI_READ);
ppi_shadow_access (fdp, PPICTRL, &v, PPI_READ);
ppi_shadow_access (fdp, PPISTATUS, &v, PPI_READ);
fdp->ifd = fd;
}
void ppi_close(union filedescriptor *fdp)
{
ppi_release (fdp->ifd);
close(fdp->ifd);
}
#endif /* HAVE_PARPORT */
#endif /* !WIN32NATIVE */