fxload fx2lp dev board
[fx2fw-sdcc] / hw_xpcu_x.c
1 /*-----------------------------------------------------------------------------\r
2  * Hardware-dependent code for usb_jtag\r
3  *-----------------------------------------------------------------------------\r
4  * Copyright (C) 2007 Kolja Waschk, ixo.de\r
5  *-----------------------------------------------------------------------------\r
6  * This code is part of usbjtag. usbjtag is free software; you can redistribute\r
7  * it and/or modify it under the terms of the GNU General Public License as\r
8  * published by the Free Software Foundation; either version 2 of the License,\r
9  * or (at your option) any later version. usbjtag is distributed in the hope\r
10  * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied\r
11  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
12  * GNU General Public License for more details.  You should have received a\r
13  * copy of the GNU General Public License along with this program in the file\r
14  * COPYING; if not, write to the Free Software Foundation, Inc., 51 Franklin\r
15  * St, Fifth Floor, Boston, MA  02110-1301  USA\r
16  *-----------------------------------------------------------------------------\r
17  */\r
18 \r
19 #include "delay.h"\r
20 #include "syncdelay.h"\r
21 #include "hardware.h"\r
22 \r
23 #include "isr.h"\r
24 #include "timer.h"\r
25 #include "delay.h"\r
26 #include "fx2regs.h"\r
27 #include "fx2utils.h"\r
28 #include "usb_common.h"\r
29 #include "usb_descriptors.h"\r
30 #include "usb_requests.h"\r
31 #include "syncdelay.h"\r
32 #include "eeprom.h"\r
33 \r
34 \r
35 \r
36 #define HAVE_OE_LED 1\r
37 \r
38 sbit at 0x80+1        OELED; // red LED on S3E Starter Kit (0x80+0 is green one)\r
39 \r
40 #define SetOELED(x)   do{OELED=(x);}while(0)\r
41 \r
42 \r
43 \r
44 void ProgIO_Poll(void)    {}\r
45 void ProgIO_Enable(void)  {}\r
46 void ProgiO_Disable(void) {}\r
47 void ProgiO_Deinit(void)  {}\r
48 \r
49 static unsigned char curios;\r
50 \r
51 const unsigned char wavedata[64] =\r
52 {\r
53   /* Single Write:\r
54      s0: BITS=D0     NEXT/SGLCRC DATA WAIT 4\r
55      s1: BITS=       DATA WAIT 4\r
56      s2: BITS=D1|D0  DATA WAIT 4\r
57      s3: BITS=D1     DATA WAIT 3\r
58      s4: BITS=D1     DATA DP IF(RDY0) THEN 5 ELSE 2\r
59      s5: BITS=D1|D0  DATA WAIT 4\r
60      s6: BITS=D1     DATA WAIT 3\r
61      s7: BITS=D1     DATA FIN */\r
62 \r
63   4, 4, 4, 3, 0x2A, 4, 3, 7,\r
64   6, 2, 2, 2, 3,    2, 2, 2,\r
65   1, 0, 3, 2, 2,    3, 2, 2,\r
66   0, 0, 0, 0, 0,    0, 0, 0x3F,\r
67 \r
68   /* Single Read:\r
69      s0: BITS=D0     WAIT 4\r
70      s1: BITS=       WAIT 4\r
71      s2: BITS=D1|D0  WAIT 4\r
72      s3: BITS=D1     WAIT 4\r
73      s4: BITS=D1|D0  WAIT 3\r
74      s5: BITS=D1|D0  DP IF(RDY0) THEN 6 ELSE 3\r
75      s6: BITS=D1     DATA WAIT 4\r
76      s7: BITS=D1     FIN */\r
77 \r
78   4, 4, 4, 4, 3, 0x33, 4, 7,\r
79   0, 0, 0, 0, 0, 1,    2, 0,\r
80   1, 0, 3, 2, 3, 3,    2, 2,\r
81   0, 0, 0, 0, 0, 0,    0, 0x3F\r
82 };\r
83 \r
84 void ProgIO_Init(void)\r
85 {\r
86   unsigned char i;\r
87 \r
88   /* The following code depends on your actual circuit design.\r
89      Make required changes _before_ you try the code! */\r
90 \r
91   // set the CPU clock to 48MHz, enable clock output to FPGA\r
92   CPUCS = bmCLKOE | bmCLKSPD1;\r
93 \r
94   // Use internal 48 MHz, enable output, GPIF Master mode\r
95   IFCONFIG = bmIFCLKSRC | bm3048MHZ | bmIFCLKOE | bmIFGPIF;\r
96 \r
97   PORTACFG = 0x00; OEA = 0xFB; IOA = 0x20;\r
98   PORTCCFG = 0x00; OEC = 0xFF; IOC = 0x10;\r
99   PORTECFG = 0x00; OEE = 0xFC; IOE = 0xC0;\r
100  \r
101   GPIFABORT    = 0xFF;\r
102 \r
103   GPIFREADYCFG = 0xA0;\r
104   GPIFCTLCFG   = 0x00;\r
105   GPIFIDLECS   = 0x00;\r
106   GPIFIDLECTL  = 0x00;\r
107   GPIFWFSELECT = 0x01;\r
108 \r
109   // Copy waveform data\r
110   AUTOPTRSETUP = 0x07;\r
111   APTR1H = MSB( &wavedata );\r
112   APTR1L = LSB( &wavedata );\r
113   AUTOPTRH2 = 0xE4;\r
114   AUTOPTRL2 = 0x00;\r
115   for ( i = 0; i < 64; i++ ) EXTAUTODAT2 = EXTAUTODAT1;\r
116 \r
117   SYNCDELAY;\r
118   GPIFADRH      = 0x00;\r
119   SYNCDELAY;\r
120   GPIFADRL      = 0x00;\r
121 \r
122   FLOWSTATE     = 0x00;\r
123   FLOWLOGIC     = 0x00;\r
124   FLOWEQ0CTL    = 0x00;\r
125   FLOWEQ1CTL    = 0x00;\r
126   FLOWHOLDOFF   = 0x00;\r
127   FLOWSTB       = 0x00;\r
128   FLOWSTBEDGE   = 0x00;\r
129   FLOWSTBHPERIOD = 0x00;\r
130 \r
131   curios = 0;\r
132 }\r
133 \r
134 unsigned char GetTDO(unsigned char r)\r
135 {\r
136   unsigned char x;\r
137 \r
138   IOC = 0x41;\r
139   while(!(GPIFTRIG & 0x80)); x = XGPIFSGLDATLX;\r
140   while(!(GPIFTRIG & 0x80)); x = XGPIFSGLDATLNOX;\r
141 \r
142   if(IOA & 0x20) IOA |= 0x40; else IOA &= ~0x40;\r
143 \r
144   return (x&1) ? r : 0;\r
145 }\r
146 \r
147 #define SetPins(x) while(!(GPIFTRIG & 0x80)); XGPIFSGLDATLX = (x)\r
148 \r
149 void ProgIO_Set_State(unsigned char d)\r
150 {\r
151   /* Set state of output pins:\r
152    *\r
153    * d.0 => TCK (PE.6)\r
154    * d.1 => TMS (PE.5)\r
155    * d.4 => TDI (PE.4)\r
156    * d.6 => LED / Output Enable\r
157    */\r
158 \r
159   curios  = (d & bmBIT0) ? 0x40 : 0; // TCK\r
160   curios |= (d & bmBIT1) ? 0x20 : 0; // TMS\r
161   curios |= (d & bmBIT4) ? 0x10 : 0; // TDI\r
162 \r
163   IOC = 0x81; /* Select direction */\r
164   SetPins(curios);\r
165 \r
166   if(d & bmBIT6) IOA=(IOA&~3)|1; else IOA=(IOA&~3)|2;\r
167 }\r
168 \r
169 unsigned char ProgIO_Set_Get_State(unsigned char d)\r
170 {\r
171 \r
172   ProgIO_Set_State(d);\r
173 \r
174   /* Read state of input pins:\r
175    *\r
176    * TDO => d.0\r
177    * DATAOUT => d.1 (only #ifdef HAVE_AS_MODE)\r
178    */\r
179 \r
180   return 2|GetTDO(0x01);\r
181 }\r
182 \r
183 \r
184 \r
185 void ProgIO_ShiftOut(unsigned char c)\r
186 {\r
187   unsigned char r,i;\r
188   unsigned char locios = curios & ~0x50;\r
189 \r
190   IOC = 0x81; /* Select direction */\r
191 \r
192   for(i=0,r=1;i<8;i++)\r
193   {\r
194     unsigned char t = locios;\r
195     if(c & r) t |= 0x10;\r
196     SetPins(t);\r
197     SetPins(t|0x40);\r
198     r <<= 1;\r
199     SetPins(t);\r
200   };\r
201 \r
202   curios = locios;\r
203 }\r
204 \r
205 \r
206 unsigned char ProgIO_ShiftInOut(unsigned char c)\r
207 {\r
208   unsigned char r,i,n;\r
209   unsigned char locios = curios & ~0x50;\r
210 \r
211   for(i=0,r=1,n=0;i<8;i++)\r
212   {\r
213     unsigned char t = locios;\r
214 \r
215     n |= GetTDO(r);\r
216 \r
217     IOC = 0x81;\r
218 \r
219     if(c & r) t |= 0x10;\r
220 \r
221     SetPins(t);\r
222     SetPins(t|0x40);\r
223     r <<= 1;\r
224     SetPins(t);\r
225   };\r
226 \r
227   curios = locios;\r
228   return n;\r
229 }\r
230 \r
231 \r