6b2fe21fc15af8769ad4325bfebdc2913fdc1a60
[my-code/fpga.git] / fx2 / fx2.c
1 /*
2  * fx2 firmware 
3  *
4  * author: hackbard@hackdaworld.org
5  *
6  * number of priorities:
7  * - switch on board power
8  * - allow high speed 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 0xe60b volatile u8 REVCTL;
34 xdata at 0xe612 volatile u8 EP2CFG;
35 xdata at 0xe614 volatile u8 EP6CFG;
36 xdata at 0xe618 volatile u8 EP2FIFOCFG;
37 xdata at 0xe619 volatile u8 EP4FIFOCFG;
38 xdata at 0xe61a volatile u8 EP6FIFOCFG;
39 xdata at 0xe61b volatile u8 EP8FIFOCFG;
40
41 /* special funtion registers */
42 sfr at 0xb5 OED;
43 sfr at 0xb0 IOD;
44
45 /* synchronization delay after writing/reading to registers 0xe600 - 0xe6ff
46  * and some others (p. 438).
47  * maximum delay necessary at highest cpu speed: 16 cycles => 17 nops */
48 #define SYNCDELAY _asm \
49         nop; nop; nop; nop; nop; nop; nop; nop; \
50         nop; nop; nop; nop; nop; nop; nop; nop; \
51         nop; _endasm
52
53 void power_on() {
54
55         /* high level must be applied to the mosfet gate for power on
56          *
57          * ref: http://digilentinc.com/Data/Products/NEXYS/Nexys_sch.pdf
58          */
59
60         /* configure pin 7 of port d as output */
61         OED|=(1<<7);
62
63         /* pull it high */
64         IOD|=(1<<7);
65 }
66
67 void slave_fifo_init() {
68
69         /* initialization of the slave fifo, used by external logic (the fpga)
70          * to do usb communication with the host */
71
72         /* set bit 0 and 1 - fifo slave config */       
73         IFCONFIG|=0x03;
74
75         /* async mode */
76         IFCONFIG|=0x04;
77
78         /* p. 180: must be set to 1 */
79         REVCTL|=((1<<0)|(1<<1));
80
81         /* 8 bit fifo to all endpoints
82          *
83          * ('or' of all these bits define port d functionality)
84          */
85         EP2FIFOCFG&=~(1<<0);
86         EP4FIFOCFG&=~(1<<0);
87         EP6FIFOCFG&=~(1<<0);
88         EP8FIFOCFG&=~(1<<0);
89
90         /* default indexed flag configuration:
91          *
92          * flag a: programmable level
93          * flag b: full
94          * flag c: empty
95          *
96          * todo: -> fixed configuration
97          */
98
99         /* endpoint configuration:
100          *
101          * (assuming 'high bandwidth in' [fpga -> host]
102          *  and 'low bandwidth out' [host->fpga] applications)
103          * 
104          * ep2: bulk in 3x1024
105          * ep6: bulk out 2x512
106          *
107          * 0xeb = 1 1 1 0 1 0 1 1 = bulk in 3x1024
108          * 0xa2 = 1 0 1 0 0 0 1 0 = bulk out 2x512
109          * 0x01 = 0 0 0 0 0 0 0 1 = invalid (bit,type,buf)
110          */
111         EP2CFG=0xeb;
112         EP4CFG=0x01;
113         EP6CFG=0xa2;
114         EP8CFG=0x01;
115
116         /* reset the fifo */
117         FIFORESET=0x80; /* nak all transfers */
118         FIFORESET=0x02; /* reset ep2 */
119         FIFORESET=0x06; /* reset ep6 */
120         FIFORESET=0x00; /* restore normal operation */
121
122         /* auto in/out, no cpu interaction! auto in len = 1024 */
123         EP2FIFOCFG|=(1<<3);
124         EP2AUTOINLENH=(1<<2);
125         EP2AUTOINLENL=0;
126         EP6FIFOCFG|=(1<<4);
127
128         /* maybe OUTPKTEND necessary (with skip=1) */
129 }
130
131 void ep1_init() {
132
133         /* initialize endpoint 1 (will be used for jtag) */
134
135         /* endpoint 1 configuration:
136          *
137          * default (valid, bulk) fits!
138          */
139
140 }
141
142 void fx2_init() {
143
144         /* swicth on power */
145         power_on();
146
147         /* slave fifo init */
148         slave_fifo_init();
149
150         /* ep1_init(); */
151 }
152
153 void main() {
154
155         /* initialize the fx2 */
156         fx2_init();
157
158         /* do the job ... */
159         while(1) {
160         
161         }
162
163 }
164