cafete backups
[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 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_on() {
59
60         /* high level must be applied to the mosfet gate for power on
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         /* pull it high */
69         IOD|=(1<<7);
70 }
71
72 void slave_fifo_init() {
73
74         /* initialization of the slave fifo, used by external logic (the fpga)
75          * to do usb communication with the host */
76
77         /* set bit 0 and 1 - fifo slave config */       
78         IFCONFIG|=0x03;
79
80         /* async mode */
81         IFCONFIG|=0x04;
82
83         /* p. 180: must be set to 1 */
84         REVCTL|=((1<<0)|(1<<1));
85
86         /* 8 bit fifo to all endpoints
87          *
88          * ('or' of all these bits define port d functionality)
89          */
90         EP2FIFOCFG&=~(1<<0);
91         EP4FIFOCFG&=~(1<<0);
92         EP6FIFOCFG&=~(1<<0);
93         EP8FIFOCFG&=~(1<<0);
94
95         /* default indexed flag configuration:
96          *
97          * flag a: programmable level
98          * flag b: full
99          * flag c: empty
100          *
101          * todo: -> fixed configuration
102          */
103
104         /* endpoint configuration:
105          *
106          * ep2: bulk in 4x512
107          * ep6: bulk out 4x512
108          *
109          * 0xa0 = 1 0 1 0 0 0 0 0 = bulk out 4x512
110          * 0xe0 = 1 1 1 0 0 0 0 0 = bulk in 4x512
111          * 0x01 = 0 0 0 0 0 0 0 1 = invalid (bit,type,buf)
112          */
113         EP2CFG=0xa0;
114         EP4CFG=0x01;
115         EP6CFG=0xe0;
116         EP8CFG=0x01;
117
118         /* reset the fifo */
119         FIFORESET=0x80; /* nak all transfers */
120         FIFORESET=0x02; /* reset ep2 */
121         FIFORESET=0x06; /* reset ep6 */
122         FIFORESET=0x00; /* restore normal operation */
123
124         /* auto in/out, no cpu interaction! auto in len = 512 */
125         EP2FIFOCFG|=(1<<4);
126         EP6FIFOCFG|=(1<<3);
127         EP6AUTOINLENH=(1<<1);
128         EP6AUTOINLENL=0;
129
130         /* maybe OUTPKTEND necessary (with skip=1) */
131 }
132
133 void ep1_init() {
134
135         /* initialize endpoint 1 (will be used for jtag) */
136
137         /* endpoint 1 configuration:
138          *
139          * default (valid, bulk) fits!
140          */
141
142 }
143
144 void fx2_init() {
145
146         /* swicth on power */
147         power_on();
148
149         /* slave fifo init */
150         slave_fifo_init();
151
152         /* ep1_init(); */
153 }
154
155 void main() {
156
157         /* initialize the fx2 */
158         fx2_init();
159
160         /* do the job ... */
161         while(1) {
162         
163         }
164
165 }
166