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

Subversion Repositories w11

[/] [w11/] [tags/] [w11a_V0.6/] [tools/] [bin/] [fx2load_wrapper] - Blame information for rev 24

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 17 wfjm
#!/usr/bin/perl -w
2 22 wfjm
# $Id: fx2load_wrapper 547 2013-12-29 13:10:07Z mueller $
3 17 wfjm
#
4 18 wfjm
# Copyright 2011-2013 by Walter F.J. Mueller 
5 17 wfjm
#
6
# This program is free software; you may redistribute and/or modify it under
7
# the terms of the GNU General Public License as published by the Free
8
# Software Foundation, either version 2, or at your option any later version.
9
#
10
# This program is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY, without even the implied warranty of MERCHANTABILITY
12
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13
# for complete details.
14
#
15
#  Revision History:
16
# Date         Rev Version  Comment
17
# 2012-02-11   457   1.0.1  use RETRO_FX2_VID/PID; check iProduct string
18
# 2011-12-29   446   1.0    Initial version
19
#
20
 
21
use 5.005;                                  # require Perl 5.005 or higher
22
use strict;                                 # require strict checking
23
use FileHandle;
24
use Time::HiRes qw(usleep);
25
 
26
use Getopt::Long;
27
 
28
my %opts = ();
29
 
30
GetOptions(\%opts, "help", "dry_run", "force", "cycfx2prog",
31
           "board=s", "file=s", "ihx_path=s")
32
  or exit 1;
33
 
34
#
35
# setup defaults for board and file
36
#
37
if (not defined $opts{board}) {
38
  $opts{board} = "nexys2";
39
}
40
if (not defined $opts{file}) {
41
  $opts{file} = "nexys2_jtag_2fifo_as.ihx" if $opts{board} eq "nexys2";
42
  $opts{file} = "nexys3_jtag_2fifo_as.ihx" if $opts{board} eq "nexys3";
43
  $opts{file} = "nexys3_jtag_2fifo_as.ihx" if $opts{board} eq "atlys";
44
}
45
if (not defined $opts{ihx_path}) {
46
  unless (exists $ENV{RETROBASE}) {
47
    print STDERR "fx2load_wrapper-F: RETROBASE not set\n";
48
    exit 1;
49
  }
50
  $opts{ihx_path} = $ENV{RETROBASE} . "/tools/fx2/bin";
51
}
52
 
53
sub print_help;
54
sub run_command;
55
sub get_usb_id;
56
sub get_usb_prodinfo;
57
 
58
autoflush STDOUT 1 if (-p STDOUT);          # autoflush if output into pipe
59
 
60
if (exists $opts{help}) {
61
  print_help;
62
  exit 0;
63
}
64
 
65
my $board = $opts{board};
66
my $ifile = $opts{file};
67
 
68
# setup digilent default usb id's
69
 
70
my $usbid_digi;
71
 
72
if    ($board eq "nexys2") { $usbid_digi = "1443:0005";}
73
elsif ($board eq "nexys3") { $usbid_digi = "1443:0007";}
74
elsif ($board eq "atlys")  { $usbid_digi = "1443:0007";}
75
else {
76
  print STDERR
77
    "fx2load_wrapper-E: only nexys2,3/atlys supported\n";
78
  exit 1;
79
}
80
 
81
# handle USB VID/PID of board
82
#  taken from RETRO_FX2_VID and RETRO_FX2_PID environment variables
83
#  in the retro11 project the default is:
84
#    VID: 16c0  (VOTI)
85
#    PID: 03ef  (VOTI free for internal lab use 1007)
86
#
87
#   !! Important Note on Usage of this USB VID/PID !!
88
#   This VID/PID is owned by VOTI, a small dutch company. Usage is granted
89
#   for 'internal lab use only' by VOTI under the conditions:
90
#     - the gadgets in which you use those PIDs do not leave your desk
91
#     - you won't complain to VOTI if you get in trouble with duplicate PIDs
92
#       (for instance because someone else did not follow the previous rule).
93
#   See also http://www.voti.nl/pids/pidfaq.html
94
#
95
 
96
my $fx2_vid = $ENV{RETRO_FX2_VID};
97
my $fx2_pid = $ENV{RETRO_FX2_PID};
98
$fx2_vid = "16c0" unless defined $fx2_vid;
99
$fx2_pid = "03ef" unless defined $fx2_pid;
100
 
101
my $usbid_retro = "$fx2_vid:$fx2_pid";
102
my $n_digi  = 0;
103
my $n_retro = 0;
104
 
105
my $fx2_bus;
106
my $fx2_dev;
107
my $fx2_id;
108
my $fx2_prodinfo;
109
 
110
($n_digi, $n_retro, $fx2_bus, $fx2_dev, $fx2_id) = get_usb_id();
111
 
112
if ($n_digi+$n_retro == 0) {
113
  print STDERR "fx2load_wrapper-E no board detected\n";
114
  exit 1;
115
}
116
if ($n_digi+$n_retro > 1) {
117
  print STDERR "fx2load_wrapper-E more than one board detected\n";
118
  exit 1;
119
}
120
 
121
if ($n_retro > 0) {
122
  $fx2_prodinfo = get_usb_prodinfo($fx2_id);
123
}
124
 
125
if ($n_retro == 1 &&
126
    $opts{file} eq $fx2_prodinfo . ".ihx" &&
127
    (not defined $opts{force}) ) {
128
  print "fx2load_wrapper-I board already configured with $fx2_prodinfo.ihx\n";
129
  exit 0;
130
}
131
 
132
my $full_file = $opts{ihx_path} . "/" . $opts{file};
133
unless (-r $full_file) {
134
  print STDERR "fx2load_wrapper-E: ihx file \'$full_file\' not found\n";
135
  exit 1;
136
}
137
 
138
my $fx2_path = "/dev/bus/usb/$fx2_bus/$fx2_dev";
139
 
140
unless ( -r $fx2_path && -w $fx2_path)  {
141
  print STDERR
142
    "fx2load_wrapper-E: usb device \'$fx2_path\' not user accessible\n";
143
  exit 1;
144
}
145
 
146
my $cmd;
147
 
148
if ($opts{cycfx2prog}) {
149
  my $proc = `which cycfx2prog`;
150
  chomp $proc;
151
  unless (-x $proc) {
152
    print STDERR "fx2load_wrapper-E: cycfx2prog not found or executable\n";
153
    exit 1;
154
  }
155
  $cmd = "cycfx2prog -id=$fx2_id prg:$full_file run";
156
 
157
} else {
158
  my $proc = `which fxload`;
159
  chomp $proc;
160
  $proc = "/sbin/fxload" if ($proc eq "");
161
  unless (-x $proc) {
162
    print STDERR "fx2load_wrapper-E: fxload not found or executable\n";
163
    exit 1;
164
  }
165
  $cmd = "$proc -t fx2 -I $full_file -D $fx2_path";
166
}
167
 
168
my $rc = 0;
169
if (defined $opts{dry_run}) {
170
  print "$cmd\n";
171
} else {
172
  print "fx2load_wrapper-I: loading $opts{file}\n";
173
  $rc = run_command($cmd);
174
  print "fx2load_wrapper-I: loaded $opts{file}\n";
175
  usleep(1500000);
176
}
177
 
178
exit $rc;
179
 
180
#-------------------------------------------------------------------------------
181
 
182
sub run_command {
183
 
184
  my ($cmd) = @_;
185
 
186
  my $wrc = system "/bin/sh", "-c", "$cmd";
187
 
188
  my $rc  = 0;
189
  if ($wrc != 0) {
190
    my $rc = int($wrc/256);
191
    if ($rc == 0) {
192
      my $sig = $wrc % 256;
193
      print STDERR "fx2load_wrapper-E \'$cmd\' aborted by signal $sig\n";
194
      $rc = 1;
195
    } else {
196
      print STDERR "fx2load_wrapper-E \'$cmd\' failed (rc=$rc) $?\n";
197
    }
198
  }
199
 
200
  return $rc;
201
}
202
 
203
#-------------------------------------------------------------------------------
204
 
205
sub print_help {
206 22 wfjm
  print "usage: fx2load_wrapper [--board=b] [--file=f] [OPTIONS]\n";
207 17 wfjm
  print "  --help           this message\n";
208
  print "  --dry_run        print command only\n";
209 18 wfjm
  print "  --force          reload even when proper firmware detected\n";
210 17 wfjm
  print "  --cycfx2prog     use cycfx2prog instead of fxload\n";
211
  print "  --board=b        type of board (default nexys2)\n";
212
  print "  --file=f         ihx file to load (default 2fifo_as)\n";
213
  print "  --ihx_path=p     path to ihx files\n";
214
}
215
 
216
 
217
#-------------------------------------------------------------------------------
218
 
219
sub get_usb_id {
220
  my @lsusb = `lsusb`;
221
 
222
  my $n_digi  = 0;
223
  my $n_retro = 0;
224
  my $fx2_bus;
225
  my $fx2_dev;
226
  my $fx2_id;
227
 
228
  foreach (@lsusb) {
229
    if (/^Bus\s+(\d+)\s+Device\s+(\d+):\s+ID\s+([:0-9a-f]+)\s+(.*)$/) {
230
      my ($bus,$dev,$id,$text) = ($1,$2,$3,$4);
231
      my $match = 0;
232
      if ($id eq $usbid_digi) {
233
        $n_digi += 1;
234
        $match = 1;
235
      } elsif ($id eq $usbid_retro) {
236
        $n_retro += 1;
237
        $match = 1;
238
      }
239
      if ($match) {
240
        $fx2_bus = $bus;
241
        $fx2_dev = $dev;
242
        $fx2_id  = $id;
243
      }
244
    }
245
  }
246
 
247
  return ($n_digi, $n_retro, $fx2_bus, $fx2_dev, $fx2_id);
248
 
249
}
250
 
251
#-------------------------------------------------------------------------------
252
 
253
sub get_usb_prodinfo {
254
  my ($fx2_id) = @_;
255
  my @lsusb = `lsusb -d $fx2_id -v`;
256
  foreach (@lsusb) {
257
    if (/^\s*iProduct\s*\d*\s*(.*)$/) {
258
      return $1;
259
    }
260
  }
261
  return "";
262
}

powered by: WebSVN 2.1.0

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