90e40e9d815b0543b78ff702d607c510a39e644e
[my-code/fpga.git] / fx2 / fx2.c
1 /*
2  * fx2 firmware 
3  *
4  * author: hackbard@hackdaworld.org
5  *
6  * feature list:
7  * - switch on board power (done)
8  * - allow high speed bulk usb transfer
9  * - do jtag
10  * 
11  */
12
13 /* constant definitions */
14 #define TRUE    1
15 #define FALSE   0
16 #define POWER_ON        1;
17 #define POWER_OFF       0;
18
19 /* type definitions */
20 typedef unsigned char u8;
21 typedef unsigned short u16;
22 typedef unsigned int u32;
23
24 /*
25  * fx2 register
26  */
27
28 /* general configuration */
29 xdata at 0xe600 volatile u8 CPUCS;
30 xdata at 0xe601 volatile u8 IFCONFIG;
31
32 /* endpoint configuration */
33 xdata at 0xe604 volatile u8 FIFORESET;
34 xdata at 0xe60b volatile u8 REVCTL;
35 xdata at 0xe612 volatile u8 EP2CFG;
36 xdata at 0xe613 volatile u8 EP4CFG;
37 xdata at 0xe614 volatile u8 EP6CFG;
38 xdata at 0xe615 volatile u8 EP8CFG;
39 xdata at 0xe618 volatile u8 EP2FIFOCFG;
40 xdata at 0xe619 volatile u8 EP4FIFOCFG;
41 xdata at 0xe61a volatile u8 EP6FIFOCFG;
42 xdata at 0xe61b volatile u8 EP8FIFOCFG;
43 xdata at 0xe620 volatile u8 EP2AUTOINLENH;
44 xdata at 0xe621 volatile u8 EP2AUTOINLENL;
45 xdata at 0xe624 volatile u8 EP6AUTOINLENH;
46 xdata at 0xe625 volatile u8 EP6AUTOINLENL;
47
48 /* special funtion registers */
49 sfr at 0xb5 OED;
50 sfr at 0xb0 IOD;
51
52 /* synchronization delay after writing/reading to registers 0xe600 - 0xe6ff
53  * and some others (p. 438).
54  * maximum delay necessary at highest cpu speed: 16 cycles => 17 nops */
55 #define SYNCDELAY _asm \
56         nop; nop; nop; nop; nop; nop; nop; nop; \
57         nop; nop; nop; nop; nop; nop; nop; nop; \
58         nop; _endasm
59
60 void power_init() {
61
62         /* pin 7 of port d connected to mosfet gate controlling the board power
63          *
64          * ref: http://digilentinc.com/Data/Products/NEXYS/Nexys_sch.pdf
65          */
66
67         /* configure pin 7 of port d as output */
68         OED|=(1<<7);
69         SYNCDELAY;
70
71 }
72
73 void toggle_power() {
74
75         /* toggle high/low state of the mosfet gate */
76
77         if((IOD&(1<<7)))
78                 IOD&=~(1<<7);
79         else
80                 IOD|=(1<<7);
81
82         SYNCDELAY;
83
84 }
85
86 void cpu_init() {
87
88         /* cpu initialization: (0x10)
89          * - 48 mhz
90          * - none inverted signal
91          * - no clk out
92          */
93
94         CPUCS=(1<<4);
95         SYNCDELAY;
96
97 }
98
99 void slave_fifo_init() {
100
101         /* initialization of the slave fifo, used by external logic (the fpga)
102          * to do usb communication with the host */
103
104         /* set bit 0 and 1 - fifo slave config */       
105         IFCONFIG|=0x03;
106         SYNCDELAY;
107
108         /* async mode */
109         IFCONFIG|=0x04;
110         SYNCDELAY;
111
112         /* p. 180: must be set to 1 */
113         REVCTL|=((1<<0)|(1<<1));
114         SYNCDELAY;
115
116         /* 8 bit fifo to all endpoints
117          *
118          * ('or' of all these bits define port d functionality)
119          */
120         EP2FIFOCFG&=~(1<<0);
121         SYNCDELAY;
122         EP4FIFOCFG&=~(1<<0);
123         SYNCDELAY;
124         EP6FIFOCFG&=~(1<<0);
125         SYNCDELAY;
126         EP8FIFOCFG&=~(1<<0);
127         SYNCDELAY;
128
129         /* default indexed flag configuration:
130          *
131          * flag a: programmable level
132          * flag b: full
133          * flag c: empty
134          *
135          * todo: -> fixed configuration
136          */
137
138         /* endpoint configuration:
139          *
140          * ep2: bulk out 4x512
141          * ep6: bulk in 4x512
142          *
143          * 0xa0 = 1 0 1 0 0 0 0 0 = bulk out 4x512
144          * 0xe0 = 1 1 1 0 0 0 0 0 = bulk in 4x512
145          */
146         EP2CFG=0xa0;
147         SYNCDELAY;
148         EP4CFG&=(~0x80);
149         SYNCDELAY;
150         EP6CFG=0xe0;
151         SYNCDELAY;
152         EP8CFG&=(~0x80);
153         SYNCDELAY;
154
155         /* reset the fifo */
156         FIFORESET=0x80; /* nak all transfers */
157         SYNCDELAY;
158         FIFORESET=0x02; /* reset ep2 */
159         SYNCDELAY;
160         FIFORESET=0x06; /* reset ep6 */
161         SYNCDELAY;
162         FIFORESET=0x00; /* restore normal operation */
163         SYNCDELAY;
164
165         /* auto in/out, no cpu interaction! auto in len = 512 */
166         EP2FIFOCFG|=(1<<4);
167         SYNCDELAY;
168         EP6FIFOCFG|=(1<<3);
169         SYNCDELAY;
170         EP6AUTOINLENH=(1<<1);
171         SYNCDELAY;
172         EP6AUTOINLENL=0;
173         SYNCDELAY;
174
175 }
176
177 void ep1_init() {
178
179         /* initialize endpoint 1
180          *
181          * used for jtag & control
182          */
183
184         /* endpoint 1 configuration:
185          *
186          * default (valid, bulk) fits!
187          */
188
189 }
190
191 void fx2_init() {
192
193         /* cpu init */
194         cpu_init();
195
196         /* power init & power on */
197         power_init();
198         toggle_power();
199
200         /* slave fifo init */
201         slave_fifo_init();
202
203         /* ep1 init */
204         ep1_init();
205 }
206
207 void main() {
208
209         /* initialize the fx2 */
210         fx2_init();
211
212         /* do the job ... */
213         while(1) {
214         
215         }
216
217 }
218