OpenCores
URL https://opencores.org/ocsvn/minsoc/minsoc/trunk

Subversion Repositories minsoc

[/] [minsoc/] [branches/] [rc-1.0/] [utils/] [cable_parallel.c] - Blame information for rev 134

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 36 rfajardo
/* cable_parallel.c - Parallel cable drivers (XPC3 and XESS) for the Advanced JTAG Bridge
2
   Copyright (C) 2001 Marko Mlinar, markom@opencores.org
3
   Copyright (C) 2004 György Jeney, nog@sdf.lonestar.org
4
 
5
 
6
   This program is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License as published by
8
   the Free Software Foundation; either version 2 of the License, or
9
   (at your option) any later version.
10
 
11
   This program is distributed in the hope that it will be useful,
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
   GNU General Public License for more details.
15
 
16
   You should have received a copy of the GNU General Public License
17
   along with this program; if not, write to the Free Software
18
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
 
20
 
21
 
22
#include <stdio.h>
23
#include <unistd.h>
24
 
25
#include <sys/types.h>
26
#include <sys/stat.h>
27
#include <fcntl.h>
28
#include <linux/ppdev.h>
29
#include <linux/ioctl.h>
30
#include <linux/parport.h>
31
 
32
#include "cable_common.h"
33
#include "errcodes.h"
34
 
35
 
36
// Common functions used by both cable types
37
static int cable_parallel_out(uint8_t value);
38
static int cable_parallel_inout(uint8_t value, uint8_t *inval);
39
 
40
 
41
int fd;
42
 
43
 
44
 
45
/////////////////////////////////////////////////////////////////////////////////
46
/*-------------------------------------[ Parallel port specific functions ]---*/
47
///////////////////////////////////////////////////////////////////////////////
48
 
49
int cable_parallel_init()
50
{
51
/*
52
  //#ifndef WIN32
53
  if (ioperm(base, 3, 1)) {
54
    fprintf(stderr, "Couldn't get the port at %x\n", base);
55
    perror("Root privileges are required.\n");
56
    return APP_ERR_INIT_FAILED;
57
  }
58
  printf("Connected to parallel port at %x\n", base);
59
  printf("Dropping root privileges.\n");
60
  setreuid(getuid(), getuid());
61
  //#endif
62
*/
63
  int mode = IEEE1284_MODE_COMPAT;
64
  fd = open("/dev/parport0", O_RDWR | O_NONBLOCK);
65
  if (fd == -1)
66
  {
67
    perror("Not able to acquire the device desriptor\n");
68
    return APP_ERR_INIT_FAILED;
69
  }
70
  if (ioctl(fd, PPCLAIM) == -1)
71
  {
72
    perror("Fail to claim the interface for itself.\n");
73
    return APP_ERR_INIT_FAILED;
74
  }
75
  if (ioctl(fd, PPSETMODE, &mode) == -1)
76
  {
77
    perror("Setting compatibility mode failed.\n");
78
    return APP_ERR_INIT_FAILED;
79
  }
80
 
81
  return APP_ERR_NONE;
82
}
83
 
84
 
85
int cable_parallel_opt(int c, char *str)
86
{
87
  switch(c) {
88
  case 'p':
89
//    if(!sscanf(str, "%x", &base)) {
90
//      fprintf(stderr, "p parameter must have a hex number as parameter\n");
91
      return APP_ERR_BAD_PARAM;
92
//    }
93
    break;
94
  default:
95
    fprintf(stderr, "Unknown parameter '%c'\n", c);
96
    return APP_ERR_BAD_PARAM;
97
  }
98
  return APP_ERR_NONE;
99
}
100
 
101
/*-----------------------------------------[ Physical board wait function ]---*/
102
void cable_parallel_phys_wait()
103
{
104
  /* Multiple users have reported poor performance of parallel cables,
105
   * which has been traced to usleep() sleeping much longer than
106
   * microseconds.  The same users have reported error-free functionality
107
   * and an order of magnitude improvement in upload speed.
108
   * If you get strange data errors while running, add this sleep back
109
   * in, or perhaps a busy-wait delay.
110
   */
111
  /* usleep(10); */
112
}
113
 
114
/*----------------------------------------------[ xpc3 specific functions ]---*/
115
int cable_xpc3_out(uint8_t value)
116
{
117
  uint8_t out = 0;
118
 
119
  /* First convert the bits in value byte to the ones that the cable wants */
120
  if(value & TCLK_BIT)
121
    out |= 0x02; /* D1 pin 3 */
122
  if(value & TRST_BIT)
123
    out |= 0x10; /* Not used */
124
  if(value & TDI_BIT)
125
    out |= 0x01; /* D0 pin 2 */
126
  if(value & TMS_BIT)
127
    out |= 0x04; /* D2 pin 4 */
128
 
129
  return cable_parallel_out(out);
130
}
131
 
132
int cable_xpc3_inout(uint8_t value, uint8_t *inval)
133
{
134
  uint8_t in;
135
  int retval;
136
  uint8_t out = 0;
137
 
138
  /* First convert the bits in value byte to the ones that the cable wants */
139
  if(value & TCLK_BIT)
140
    out |= 0x02; /* D1 pin 3 */
141
  if(value & TRST_BIT)
142
    out |= 0x10; /* Not used */
143
  if(value & TDI_BIT)
144
    out |= 0x01; /* D0 pin 2 */
145
  if(value & TMS_BIT)
146
    out |= 0x04; /* D2 pin 4 */
147
 
148
  retval = cable_parallel_inout(out, &in);
149
 
150
  if(in & 0x10) /* S6 pin 13 */
151
    *inval = 1;
152
  else
153
    *inval = 0;
154
 
155
  return retval;
156
}
157
 
158
/*----------------------------------------------[ xess specific functions ]---*/
159
int cable_xess_out(uint8_t value)
160
{
161
  uint8_t out = 0;
162
 
163
  /* First convert the bits in value byte to the ones that the cable wants */
164
  if(value & TCLK_BIT)
165
    out |= 0x04; /* D2 pin 4 */
166
  if(value & TRST_BIT)
167
    out |= 0x08; /* D3 pin 5 */
168
  if(value & TDI_BIT)
169
    out |= 0x10; /* D4 pin 6 */
170
  if(value & TMS_BIT)
171
    out |= 0x20; /* D3 pin 5 */
172
 
173
  return cable_parallel_out(out);
174
}
175
 
176
uint8_t cable_xess_inout(uint8_t value, uint8_t *inval)
177
{
178
  uint8_t in;
179
  int retval;
180
  uint8_t out = 0;
181
 
182
  /* First convert the bits in value byte to the ones that the cable wants */
183
  if(value & TCLK_BIT)
184
    out |= 0x04; /* D2 pin 4 */
185
  if(value & TRST_BIT)
186
    out |= 0x08; /* D3 pin 5 */
187
  if(value & TDI_BIT)
188
    out |= 0x10; /* D4 pin 6 */
189
  if(value & TMS_BIT)
190
    out |= 0x20; /* D3 pin 5 */
191
 
192
  retval = cable_parallel_inout(out, &in);
193
 
194
  if(in & 0x20) /* S5 pin 12*/
195
    *inval = 1;
196
  else
197
    *inval = 0;
198
 
199
  return retval;
200
}
201
 
202
 
203
/*----------------------------------------------[ common helper functions ]---*/
204
// 'static' for internal access only
205
 
206
static int cable_parallel_out(uint8_t value)
207
{
208
  ioctl(fd, PPWDATA, &value);
209
 
210
  return APP_ERR_NONE;
211
}
212
 
213
static int cable_parallel_inout(uint8_t value, uint8_t *inval)
214
{
215
  ioctl(fd, PPRSTATUS, inval);
216
  ioctl(fd, PPWDATA, &value);
217
 
218
  return APP_ERR_NONE;
219
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.