Root/Software/xc3sprog/xc3sprog.cpp

1/* Spartan3 JTAG programmer
2
3Copyright (C) 2004 Andrew Rogers
4
5This program is free software; you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation; either version 2 of the License, or
8(at your option) any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program; if not, write to the Free Software
17Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
18
19#include <stdio.h>
20#include <string.h>
21#include <stdlib.h>
22
23
24#include "sakcXCProgrammer.h"
25#include "bitfile.h"
26#include "jtag.h"
27#include "devicedb.h"
28#include "progalgxcf.h"
29#include "progalgxc3s.h"
30
31#define MEMDEV "/dev/mem"
32
33#define DEVICEDB "/usr/share/xc3sprog/devlist.txt"
34
35void process(IOBase &io, BitFile &file, int chainpos);
36void programXC3S(Jtag &jtag, IOBase &io, BitFile &file);
37void programXCF(Jtag &jtag, IOBase &io, BitFile &file);
38
39int main(int argc, char **args)
40{
41  // Produce release info from CVS tags
42  char release[]={"$Name: Release-0-5 $"};
43  char *loc0=strchr(release,'-');
44  if(loc0>0){
45    loc0++;
46    char *loc=loc0;
47    do{
48      loc=strchr(loc,'-');
49      if(loc)*loc='.';
50    }while(loc);
51    release[strlen(release)-1]='\0'; // Strip off $
52  }
53  printf("Release %s\n",loc0);
54
55  sakcXCProgrammer io;
56  int chainpos=0;
57  if(io.checkError()){
58    fprintf(stderr,"Can map physical address into virtual space! or can not open '%s'.\n",MEMDEV);
59  }
60  if(argc<=1){
61    fprintf(stderr,"\nUsage: %s infile.bit [POS]\n\n",args[0]);
62    fprintf(stderr,"\tPOS position in JTAG chain, 0=closest to TDI (default)\n\n",args[0]);
63    return 1;
64  }
65  if(argc>2)chainpos=atoi(args[2]);
66  BitFile file;
67  if(file.load(args[1]))process(io,file,chainpos);
68  else return 1;
69  return 0;
70}
71
72void process(IOBase &io, BitFile &file, int chainpos)
73{
74  Jtag jtag(&io);
75  int num=jtag.getChain();
76
77  // Synchronise database with chain of devices.
78  DeviceDB db(DEVICEDB);
79  for(int i=0; i<num; i++){
80    printf("ID=%x\n", jtag.getDeviceID(i));
81    int length=db.loadDevice(jtag.getDeviceID(i));
82    if(length>0)jtag.setDeviceIRLength(i,length);
83    else{
84      unsigned id=jtag.getDeviceID(i);
85      fprintf(stderr,"Cannot find device having IDCODE=%08x\n",id);
86      return;
87    }
88  }
89  
90
91  if(jtag.selectDevice(chainpos)<0){
92    fprintf(stderr,"Invalid chain position %d, position must be less than %d (but not less than 0).\n",chainpos,num);
93    return;
94  }
95
96  // Find the programming algorithm required for device
97  const char *dd=db.getDeviceDescription(chainpos);
98  if(strncmp("XC3S",dd,4)==0) {
99    printf("Programming..\n");
100    programXC3S(jtag,io,file);
101  }
102  else if(strncmp("XCF",dd,3)==0) programXCF(jtag,io,file);
103  else{
104    fprintf(stderr,"Sorry, cannot program '%s', a later release may be able to.\n",dd);
105    return;
106  }
107}
108
109void programXC3S(Jtag &jtag, IOBase &io, BitFile &file)
110{
111
112  ProgAlgXC3S alg(jtag,io);
113  alg.program(file);
114  return;
115}
116
117void programXCF(Jtag &jtag, IOBase &io, BitFile &file)
118{
119  ProgAlgXCF alg(jtag,io);
120  alg.erase();
121  alg.program(file);
122  return;
123}
124

Archive Download this file

Branches:
master



interactive