version 4.1.0 from http://fpga4u.epfl.ch/wiki/FX2
[fx2fw-sdcc] / hw_xpcu_i.c
1 /*-----------------------------------------------------------------------------\r
2 \r
3  *\r
4 \r
5  * Hardware-dependent code for usb_jtag\r
6 \r
7  *-----------------------------------------------------------------------------\r
8 \r
9  * Copyright (C) 2007 Kolja Waschk, ixo.de\r
10 \r
11  *-----------------------------------------------------------------------------\r
12 \r
13  * This code is part of usbjtag. usbjtag is free software; you can redistribute\r
14 \r
15  * it and/or modify it under the terms of the GNU General Public License as\r
16 \r
17  * published by the Free Software Foundation; either version 2 of the License,\r
18 \r
19  * or (at your option) any later version. usbjtag is distributed in the hope\r
20 \r
21  * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied\r
22 \r
23  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
24 \r
25  * GNU General Public License for more details.  You should have received a\r
26 \r
27  * copy of the GNU General Public License along with this program in the file\r
28 \r
29  * COPYING; if not, write to the Free Software Foundation, Inc., 51 Franklin\r
30 \r
31  * St, Fifth Floor, Boston, MA  02110-1301  USA\r
32 \r
33  *-----------------------------------------------------------------------------\r
34 \r
35  */\r
36 \r
37 \r
38 \r
39 #include "hardware.h"\r
40 \r
41 #include "fx2regs.h"\r
42 \r
43 #include "syncdelay.h"\r
44 \r
45 \r
46 \r
47 //---------------------------------------------------------------------------\r
48 \r
49 \r
50 \r
51 #define SetTCK(x)     do{if(x) IOE|=0x08; else IOE&=~0x08; }while(0)\r
52 \r
53 #define SetTMS(x)     do{if(x) IOE|=0x10; else IOE&=~0x10; }while(0)\r
54 \r
55 #define SetTDI(x)     do{if(x) IOE|=0x40; else IOE&=~0x40; }while(0)\r
56 \r
57 #define GetTDO()      ((IOE>>5)&1)\r
58 \r
59 \r
60 \r
61 /* XPCU has neither AS nor PS mode pins */\r
62 \r
63 \r
64 \r
65 #define HAVE_OE_LED 1\r
66 \r
67 /* +0=green led, +1=red led */\r
68 \r
69 sbit at 0x80+1        OELED;\r
70 \r
71 #define SetOELED(x)   do{OELED=(x);}while(0)\r
72 \r
73 \r
74 \r
75 //-----------------------------------------------------------------------------\r
76 \r
77 \r
78 \r
79 void ProgIO_Poll(void)    {}\r
80 \r
81 void ProgIO_Enable(void)  {}\r
82 \r
83 void ProgIO_Disable(void) {}\r
84 \r
85 void ProgIO_Deinit(void)  {}\r
86 \r
87 \r
88 \r
89 void ProgIO_Init(void)\r
90 \r
91 {\r
92 \r
93   /* The following code depends on your actual circuit design.\r
94 \r
95      Make required changes _before_ you try the code! */\r
96 \r
97 \r
98 \r
99   // set the CPU clock to 48MHz, enable clock output to FPGA\r
100 \r
101   CPUCS = bmCLKOE | bmCLKSPD1;\r
102 \r
103 \r
104 \r
105   // Use internal 48 MHz, enable output, use "Port" mode for all pins\r
106 \r
107   IFCONFIG = bmIFCLKSRC | bm3048MHZ | bmIFCLKOE;\r
108 \r
109 \r
110 \r
111   GPIFABORT = 0xFF;\r
112 \r
113 \r
114 \r
115   PORTACFG = 0x00; OEA = 0x03; IOA=0x01;\r
116 \r
117   PORTCCFG = 0x00; OEC = 0x00; IOC=0x00;\r
118 \r
119   PORTECFG = 0x00; OEE = 0x58; IOE=0x00;\r
120 \r
121 }\r
122 \r
123 \r
124 \r
125 void ProgIO_Set_State(unsigned char d)\r
126 \r
127 {\r
128 \r
129   /* Set state of output pins:\r
130 \r
131    *\r
132 \r
133    * d.0 => TCK\r
134 \r
135    * d.1 => TMS\r
136 \r
137    * d.2 => nCE (only #ifdef HAVE_AS_MODE)\r
138 \r
139    * d.3 => nCS (only #ifdef HAVE_AS_MODE)\r
140 \r
141    * d.4 => TDI\r
142 \r
143    * d.6 => LED / Output Enable\r
144 \r
145    */\r
146 \r
147 \r
148 \r
149   SetTCK((d & bmBIT0) ? 1 : 0);\r
150 \r
151   SetTMS((d & bmBIT1) ? 1 : 0);\r
152 \r
153   SetTDI((d & bmBIT4) ? 1 : 0);\r
154 \r
155 #ifdef HAVE_OE_LED\r
156 \r
157   SetOELED((d & bmBIT5) ? 1 : 0);\r
158 \r
159 #endif\r
160 \r
161 }\r
162 \r
163 \r
164 \r
165 unsigned char ProgIO_Set_Get_State(unsigned char d)\r
166 \r
167 {\r
168 \r
169   /* Read state of input pins:\r
170 \r
171    *\r
172 \r
173    * TDO => d.0\r
174 \r
175    * DATAOUT => d.1 (only #ifdef HAVE_AS_MODE)\r
176 \r
177    */\r
178 \r
179 \r
180 \r
181   ProgIO_Set_State(d);\r
182 \r
183   return 2|GetTDO();\r
184 \r
185 }\r
186 \r
187 \r
188 \r
189 void ProgIO_ShiftOut(unsigned char c)\r
190 \r
191 {\r
192 \r
193   /* Shift out byte C:\r
194 \r
195    *\r
196 \r
197    * 8x {\r
198 \r
199    *   Output least significant bit on TDI\r
200 \r
201    *   Raise TCK\r
202 \r
203    *   Shift c right\r
204 \r
205    *   Lower TCK\r
206 \r
207    * }\r
208 \r
209    */\r
210 \r
211 \r
212 \r
213   unsigned char lc=c;\r
214 \r
215 \r
216 \r
217   if(lc&1) IOE|=0x40; else IOE&=~0x40; IOE|=0x08; lc>>=1; IOE&=~0x08;\r
218 \r
219   if(lc&1) IOE|=0x40; else IOE&=~0x40; IOE|=0x08; lc>>=1; IOE&=~0x08;\r
220 \r
221   if(lc&1) IOE|=0x40; else IOE&=~0x40; IOE|=0x08; lc>>=1; IOE&=~0x08;\r
222 \r
223   if(lc&1) IOE|=0x40; else IOE&=~0x40; IOE|=0x08; lc>>=1; IOE&=~0x08;\r
224 \r
225 \r
226 \r
227   if(lc&1) IOE|=0x40; else IOE&=~0x40; IOE|=0x08; lc>>=1; IOE&=~0x08;\r
228 \r
229   if(lc&1) IOE|=0x40; else IOE&=~0x40; IOE|=0x08; lc>>=1; IOE&=~0x08;\r
230 \r
231   if(lc&1) IOE|=0x40; else IOE&=~0x40; IOE|=0x08; lc>>=1; IOE&=~0x08;\r
232 \r
233   if(lc&1) IOE|=0x40; else IOE&=~0x40; IOE|=0x08; lc>>=1; IOE&=~0x08;\r
234 \r
235 }\r
236 \r
237 \r
238 \r
239 unsigned char ProgIO_ShiftInOut(unsigned char c)\r
240 \r
241 {\r
242 \r
243   /* Shift out byte C, shift in from TDO:\r
244 \r
245    *\r
246 \r
247    * 8x {\r
248 \r
249    *   Read carry from TDO\r
250 \r
251    *   Output least significant bit on TDI\r
252 \r
253    *   Raise TCK\r
254 \r
255    *   Shift c right, append carry (TDO) at left\r
256 \r
257    *   Lower TCK\r
258 \r
259    * }\r
260 \r
261    * Return c.\r
262 \r
263    */\r
264 \r
265 \r
266 \r
267   unsigned char carry;\r
268 \r
269   unsigned char lc=c;\r
270 \r
271 \r
272 \r
273   carry = (IOE&0x20)<<2; if(lc&1) IOE|=0x40; else IOE&=~0x40; IOE|=0x08; lc=carry|(lc>>1); IOE&=~0x08;\r
274 \r
275   carry = (IOE&0x20)<<2; if(lc&1) IOE|=0x40; else IOE&=~0x40; IOE|=0x08; lc=carry|(lc>>1); IOE&=~0x08;\r
276 \r
277   carry = (IOE&0x20)<<2; if(lc&1) IOE|=0x40; else IOE&=~0x40; IOE|=0x08; lc=carry|(lc>>1); IOE&=~0x08;\r
278 \r
279   carry = (IOE&0x20)<<2; if(lc&1) IOE|=0x40; else IOE&=~0x40; IOE|=0x08; lc=carry|(lc>>1); IOE&=~0x08;\r
280 \r
281 \r
282 \r
283   carry = (IOE&0x20)<<2; if(lc&1) IOE|=0x40; else IOE&=~0x40; IOE|=0x08; lc=carry|(lc>>1); IOE&=~0x08;\r
284 \r
285   carry = (IOE&0x20)<<2; if(lc&1) IOE|=0x40; else IOE&=~0x40; IOE|=0x08; lc=carry|(lc>>1); IOE&=~0x08;\r
286 \r
287   carry = (IOE&0x20)<<2; if(lc&1) IOE|=0x40; else IOE&=~0x40; IOE|=0x08; lc=carry|(lc>>1); IOE&=~0x08;\r
288 \r
289   carry = (IOE&0x20)<<2; if(lc&1) IOE|=0x40; else IOE&=~0x40; IOE|=0x08; lc=carry|(lc>>1); IOE&=~0x08;\r
290 \r
291 \r
292 \r
293   return lc;\r
294 \r
295 }\r
296 \r
297 \r
298 \r
299 \r
300 \r