fxload fx2lp dev board
[fx2fw-sdcc] / hw_xpcu_i.c
1 /*-----------------------------------------------------------------------------\r
2  *\r
3  * Hardware-dependent code for usb_jtag\r
4  *-----------------------------------------------------------------------------\r
5  * Copyright (C) 2007 Kolja Waschk, ixo.de\r
6  *-----------------------------------------------------------------------------\r
7  * This code is part of usbjtag. usbjtag is free software; you can redistribute\r
8  * it and/or modify it under the terms of the GNU General Public License as\r
9  * published by the Free Software Foundation; either version 2 of the License,\r
10  * or (at your option) any later version. usbjtag is distributed in the hope\r
11  * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied\r
12  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13  * GNU General Public License for more details.  You should have received a\r
14  * copy of the GNU General Public License along with this program in the file\r
15  * COPYING; if not, write to the Free Software Foundation, Inc., 51 Franklin\r
16  * St, Fifth Floor, Boston, MA  02110-1301  USA\r
17  *-----------------------------------------------------------------------------\r
18  */\r
19 \r
20 #include "hardware.h"\r
21 #include "fx2regs.h"\r
22 #include "syncdelay.h"\r
23 \r
24 //---------------------------------------------------------------------------\r
25 \r
26 #define SetTCK(x)     do{if(x) IOE|=0x08; else IOE&=~0x08; }while(0)\r
27 #define SetTMS(x)     do{if(x) IOE|=0x10; else IOE&=~0x10; }while(0)\r
28 #define SetTDI(x)     do{if(x) IOE|=0x40; else IOE&=~0x40; }while(0)\r
29 #define GetTDO()      ((IOE>>5)&1)\r
30 \r
31 /* XPCU has neither AS nor PS mode pins */\r
32 \r
33 #define HAVE_OE_LED 1\r
34 /* +0=green led, +1=red led */\r
35 sbit at 0x80+1        OELED;\r
36 #define SetOELED(x)   do{OELED=(x);}while(0)\r
37 \r
38 //-----------------------------------------------------------------------------\r
39 \r
40 void ProgIO_Poll(void)    {}\r
41 void ProgIO_Enable(void)  {}\r
42 void ProgIO_Disable(void) {}\r
43 void ProgIO_Deinit(void)  {}\r
44 \r
45 void ProgIO_Init(void)\r
46 {\r
47   /* The following code depends on your actual circuit design.\r
48      Make required changes _before_ you try the code! */\r
49 \r
50   // set the CPU clock to 48MHz, enable clock output to FPGA\r
51   CPUCS = bmCLKOE | bmCLKSPD1;\r
52 \r
53   // Use internal 48 MHz, enable output, use "Port" mode for all pins\r
54   IFCONFIG = bmIFCLKSRC | bm3048MHZ | bmIFCLKOE;\r
55 \r
56   GPIFABORT = 0xFF;\r
57 \r
58   PORTACFG = 0x00; OEA = 0x03; IOA=0x01;\r
59   PORTCCFG = 0x00; OEC = 0x00; IOC=0x00;\r
60   PORTECFG = 0x00; OEE = 0x58; IOE=0x00;\r
61 }\r
62 \r
63 void ProgIO_Set_State(unsigned char d)\r
64 {\r
65   /* Set state of output pins\r
66    * (d is the byte from the host):\r
67    *\r
68    * d.0 => TCK\r
69    * d.1 => TMS\r
70    * d.2 => nCE (only #ifdef HAVE_AS_MODE)\r
71    * d.3 => nCS (only #ifdef HAVE_AS_MODE)\r
72    * d.4 => TDI\r
73    * d.6 => LED / Output Enable\r
74    */\r
75 \r
76   SetTCK((d & bmBIT0) ? 1 : 0);\r
77   SetTMS((d & bmBIT1) ? 1 : 0);\r
78   SetTDI((d & bmBIT4) ? 1 : 0);\r
79 #ifdef HAVE_OE_LED\r
80   SetOELED((d & bmBIT5) ? 1 : 0);\r
81 #endif\r
82 }\r
83 \r
84 unsigned char ProgIO_Set_Get_State(unsigned char d)\r
85 {\r
86   /* Set state of output pins (s.a.)\r
87    * then read state of input pins:\r
88    *\r
89    * TDO => d.0\r
90    * DATAOUT => d.1 (only #ifdef HAVE_AS_MODE)\r
91    */\r
92 \r
93   ProgIO_Set_State(d);\r
94   return 2|GetTDO(); /* DATAOUT assumed high, no AS mode */\r
95 }\r
96 \r
97 void ProgIO_ShiftOut(unsigned char c)\r
98 {\r
99   /* Shift out byte C:\r
100    *\r
101    * 8x {\r
102    *   Output least significant bit on TDI\r
103    *   Raise TCK\r
104    *   Shift c right\r
105    *   Lower TCK\r
106    * }\r
107    */\r
108 \r
109   unsigned char lc=c;\r
110 \r
111   if(lc&1) IOE|=0x40; else IOE&=~0x40; IOE|=0x08; lc>>=1; IOE&=~0x08;\r
112   if(lc&1) IOE|=0x40; else IOE&=~0x40; IOE|=0x08; lc>>=1; IOE&=~0x08;\r
113   if(lc&1) IOE|=0x40; else IOE&=~0x40; IOE|=0x08; lc>>=1; IOE&=~0x08;\r
114   if(lc&1) IOE|=0x40; else IOE&=~0x40; IOE|=0x08; lc>>=1; IOE&=~0x08;\r
115 \r
116   if(lc&1) IOE|=0x40; else IOE&=~0x40; IOE|=0x08; lc>>=1; IOE&=~0x08;\r
117   if(lc&1) IOE|=0x40; else IOE&=~0x40; IOE|=0x08; lc>>=1; IOE&=~0x08;\r
118   if(lc&1) IOE|=0x40; else IOE&=~0x40; IOE|=0x08; lc>>=1; IOE&=~0x08;\r
119   if(lc&1) IOE|=0x40; else IOE&=~0x40; IOE|=0x08; lc>>=1; IOE&=~0x08;\r
120 }\r
121 \r
122 unsigned char ProgIO_ShiftInOut(unsigned char c)\r
123 {\r
124   /* Shift out byte C, shift in from TDO:\r
125    *\r
126    * 8x {\r
127    *   Read carry from TDO\r
128    *   Output least significant bit on TDI\r
129    *   Raise TCK\r
130    *   Shift c right, append carry (TDO) at left (into MSB)\r
131    *   Lower TCK\r
132    * }\r
133    * Return c.\r
134    */\r
135 \r
136   unsigned char carry;\r
137   unsigned char lc=c;\r
138 \r
139   carry = (IOE&0x20)<<2; if(lc&1) IOE|=0x40; else IOE&=~0x40; IOE|=0x08; lc=carry|(lc>>1); IOE&=~0x08;\r
140   carry = (IOE&0x20)<<2; if(lc&1) IOE|=0x40; else IOE&=~0x40; IOE|=0x08; lc=carry|(lc>>1); IOE&=~0x08;\r
141   carry = (IOE&0x20)<<2; if(lc&1) IOE|=0x40; else IOE&=~0x40; IOE|=0x08; lc=carry|(lc>>1); IOE&=~0x08;\r
142   carry = (IOE&0x20)<<2; if(lc&1) IOE|=0x40; else IOE&=~0x40; IOE|=0x08; lc=carry|(lc>>1); IOE&=~0x08;\r
143 \r
144   carry = (IOE&0x20)<<2; if(lc&1) IOE|=0x40; else IOE&=~0x40; IOE|=0x08; lc=carry|(lc>>1); IOE&=~0x08;\r
145   carry = (IOE&0x20)<<2; if(lc&1) IOE|=0x40; else IOE&=~0x40; IOE|=0x08; lc=carry|(lc>>1); IOE&=~0x08;\r
146   carry = (IOE&0x20)<<2; if(lc&1) IOE|=0x40; else IOE&=~0x40; IOE|=0x08; lc=carry|(lc>>1); IOE&=~0x08;\r
147   carry = (IOE&0x20)<<2; if(lc&1) IOE|=0x40; else IOE&=~0x40; IOE|=0x08; lc=carry|(lc>>1); IOE&=~0x08;\r
148 \r
149   return lc;\r
150 }\r
151 \r
152 \r