Root/Software/xc3sprog/progalgxcf.cpp

1/* XCF Flash PROM JTAG programming algorithms
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
20
21
22#include "progalgxcf.h"
23
24const byte ProgAlgXCF::SERASE=0x0a;
25const byte ProgAlgXCF::ISPEN=0xe8;
26const byte ProgAlgXCF::FPGM=0xea;
27const byte ProgAlgXCF::FADDR=0xeb;
28const byte ProgAlgXCF::FERASE=0xec;
29const byte ProgAlgXCF::FDATA0=0xed;
30const byte ProgAlgXCF::FVFY0=0xef;
31const byte ProgAlgXCF::NORMRST=0xf0;
32const byte ProgAlgXCF::IDCODE=0xfe;
33const byte ProgAlgXCF::BYPASS=0xff;
34
35const byte ProgAlgXCF::BIT3=0x08;
36const byte ProgAlgXCF::BIT4=0x10;
37
38ProgAlgXCF::ProgAlgXCF(Jtag &j, IOBase &i)
39{
40  jtag=&j;
41  io=&i;
42}
43
44int ProgAlgXCF::erase()
45{
46  byte data[4];
47  jtag->shiftIR(&NORMRST);
48  io->cycleTCK(40000);
49  byte ircap[1];
50  jtag->shiftIR(&BYPASS,ircap);
51  if((ircap[0]&BIT3)==BIT3){
52    fprintf(stderr,"Device is write protected.\n",ircap[0]);
53    return 1;
54  }
55  jtag->shiftIR(&ISPEN);
56  jtag->shiftIR(&FADDR);
57  jtag->longToByteArray(1,data);
58  jtag->shiftDR(data,0,16);
59  io->cycleTCK(2);
60  
61  printf("Erasing...."); fflush(stdout);
62  jtag->shiftIR(&FERASE);
63  io->cycleTCK(2400000);
64  printf("done.\n");
65
66  jtag->shiftIR(&BYPASS);
67  io->tapTestLogicReset();
68 
69  return 0;
70}
71
72int ProgAlgXCF::program(BitFile &file)
73{
74  jtag->shiftIR(&NORMRST);
75  io->cycleTCK(40000);
76  io->setTapState(IOBase::TEST_LOGIC_RESET);
77  byte data[4];
78  jtag->shiftIR(&ISPEN);
79  data[0]=0x34;
80  jtag->shiftDR(data,0,6);
81
82  for(int i=0; i<file.getLength(); i+=4096){
83    int frame=i/128;
84    printf("Programming frames 0x%04x to 0x%04x....",frame,frame+31); fflush(stdout);
85    jtag->shiftIR(&FDATA0);
86    if((i+4096)<=file.getLength()){
87      jtag->shiftDR(&(file.getData())[i/8],0,4096);
88    }
89    else{
90      int rem=(file.getLength()-i)/8; // Bytes remaining
91      int pad=512-rem;
92      byte paddata[pad]; for(int k=0; k<pad; k++)paddata[k]=0xff;
93      jtag->shiftDR(&(file.getData())[i/8],0,rem*8,0,false); // Do not goto EXIT1-DR
94      jtag->shiftDR(paddata,0,pad*8);
95    }
96    jtag->longToByteArray(frame,data);
97    jtag->shiftIR(&FADDR);
98    jtag->shiftDR(data,0,16);
99    io->cycleTCK(2);
100    jtag->shiftIR(&FPGM);
101    io->cycleTCK(5000);
102    printf("done.\n");
103  }
104
105  jtag->shiftIR(&BYPASS);
106  io->tapTestLogicReset();
107  return 0;
108}
109
110int ProgAlgXCF::verify(BitFile &file)
111{
112  return 0;
113}
114

Archive Download this file

Branches:
master



interactive