added syncdelays + cpu init
[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 0xe624 volatile u8 EP6AUTOINLENH;
44 xdata at 0xe625 volatile u8 EP6AUTOINLENL;
45
46 /* special funtion registers */
47 sfr at 0xb5 OED;
48 sfr at 0xb0 IOD;
49
50 /* synchronization delay after writing/reading to registers 0xe600 - 0xe6ff
51  * and some others (p. 438).
52  * maximum delay necessary at highest cpu speed: 16 cycles => 17 nops */
53 #define SYNCDELAY _asm \
54         nop; nop; nop; nop; nop; nop; nop; nop; \
55         nop; nop; nop; nop; nop; nop; nop; nop; \
56         nop; _endasm
57
58 void power_init() {
59
60         /* pin 7 of port d connected to mosfet gate controlling the board power
61          *
62          * ref: http://digilentinc.com/Data/Products/NEXYS/Nexys_sch.pdf
63          */
64
65         /* configure pin 7 of port d as output */
66         OED|=(1<<7);
67
68 }
69
70 void toggle_power() {
71
72         /* toggle high/low state of the mosfet gate */
73
74         if((IOD&(1<<7)))
75                 IOD&=~(1<<7);
76         else
77                 IOD|=(1<<7);
78
79 }
80
81 void cpu_init() {
82
83         /* cpu initialization: (0x10)
84          * - 48 mhz
85          * - none inverted signal
86          * - no clk out
87          */
88
89         CPUCS=(1<<4);
90         SYNCDELAY;
91
92 }
93
94 void slave_fifo_init() {
95
96         /* initialization of the slave fifo, used by external logic (the fpga)
97          * to do usb communication with the host */
98
99         /* set bit 0 and 1 - fifo slave config */       
100         IFCONFIG|=0x03;
101         SYNCDELAY;
102
103         /* async mode */
104         IFCONFIG|=0x04;
105         SYNCDELAY;
106
107         /* p. 180: must be set to 1 */
108         REVCTL|=((1<<0)|(1<<1));
109         SYNCDELAY;
110
111         /* 8 bit fifo to all endpoints
112          *
113          * ('or' of all these bits define port d functionality)
114          */
115         EP2FIFOCFG&=~(1<<0);
116         SYNCDELAY;
117         EP4FIFOCFG&=~(1<<0);
118         SYNCDELAY;
119         EP6FIFOCFG&=~(1<<0);
120         SYNCDELAY;
121         EP8FIFOCFG&=~(1<<0);
122         SYNCDELAY;
123
124         /* default indexed flag configuration:
125          *
126          * flag a: programmable level
127          * flag b: full
128          * flag c: empty
129          *
130          * todo: -> fixed configuration
131          */
132
133         /* endpoint configuration:
134          *
135          * ep2: bulk in 4x512
136          * ep6: bulk out 4x512
137          *
138          * 0xa0 = 1 0 1 0 0 0 0 0 = bulk out 4x512
139          * 0xe0 = 1 1 1 0 0 0 0 0 = bulk in 4x512
140          * 0x01 = 0 0 0 0 0 0 0 1 = invalid (bit,type,buf)
141          */
142         EP2CFG=0xa0;
143         SYNCDELAY;
144         EP4CFG=0x01;
145         SYNCDELAY;
146         EP6CFG=0xe0;
147         SYNCDELAY;
148         EP8CFG=0x01;
149         SYNCDELAY;
150
151         /* reset the fifo */
152         FIFORESET=0x80; /* nak all transfers */
153         SYNCDELAY;
154         FIFORESET=0x02; /* reset ep2 */
155         SYNCDELAY;
156         FIFORESET=0x06; /* reset ep6 */
157         SYNCDELAY;
158         FIFORESET=0x00; /* restore normal operation */
159         SYNCDELAY;
160
161         /* auto in/out, no cpu interaction! auto in len = 512 */
162         EP2FIFOCFG|=(1<<4);
163         SYNCDELAY;
164         EP6FIFOCFG|=(1<<3);
165         SYNCDELAY;
166         EP6AUTOINLENH=(1<<1);
167         SYNCDELAY;
168         EP6AUTOINLENL=0;
169         SYNCDELAY;
170
171 }
172
173 void ep1_init() {
174
175         /* initialize endpoint 1
176          *
177          * used for jtag & control
178          */
179
180         /* endpoint 1 configuration:
181          *
182          * default (valid, bulk) fits!
183          */
184
185 }
186
187 void fx2_init() {
188
189         /* cpu init */
190         cpu_init();
191
192         /* power init & power on */
193         power_init();
194         toggle_power();
195
196         /* slave fifo init */
197         slave_fifo_init();
198
199         /* ep1 init */
200         ep1_init();
201 }
202
203 void main() {
204
205         /* initialize the fx2 */
206         fx2_init();
207
208         /* do the job ... */
209         while(1) {
210         
211         }
212
213 }
214