forked from DreamSourceLab/DSLogic-fw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDSLogic.c
312 lines (265 loc) · 8.5 KB
/
DSLogic.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
* This file is part of the DSLogic-fw project.
*
* Copyright (C) 2012 Uwe Hermann <[email protected]>
* Copyright (C) 2012 Joel Holdsworth <[email protected]>
* Copyright (C) 2014 DreamSourceLab <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <fx2regs.h>
#include <fx2macros.h>
#include <delay.h>
#include <setupdat.h>
#include <eputils.h>
#include <gpif.h>
#include <i2c.h>
#include <DSLogic.h>
#include <interface.h>
/* ... */
#define GPIFTRIGWR 0
#define GPIFTRIGRD 4
/* ... */
volatile __bit recv_setup;
BYTE command;
BOOL cfg_enable = FALSE;
BOOL set_enable = FALSE;
BOOL set_init = FALSE;
BOOL set_dso_ctrl = FALSE;
BYTE setting_count_b0;
BYTE setting_count_b1;
BYTE setting_count_b2;
char dsoConfig[4] = {0x00, 0xc8, 0x61, 0x55};
static void setup_endpoints(void)
{
/* Setup EP2 (out). */
EP2CFG = (1 << 7) | /* EP is valid/activated */
(0 << 6) | /* EP direction: OUT */
(1 << 5) | (0 << 4) | /* EP Type: bulk */
(0 << 3) | /* EP buffer size: 1024 */
(0 << 2) | /* Reserved. */
(0 << 1) | (0 << 0); /* EP buffering: double buffering */
SYNCDELAY();
/* Setup EP6 (IN). */
EP6CFG = (1 << 7) | /* EP is valid/activated */
(1 << 6) | /* EP direction: IN */
(1 << 5) | (0 << 4) | /* EP Type: bulk */
(0 << 3) | /* EP buffer size: 1024 */
(0 << 2) | /* Reserved. */
(0 << 1) | (0 << 0); /* EP buffering: double buffering */
SYNCDELAY();
/* Disable all other EPs (EP1, EP4, and EP8). */
EP1INCFG &= ~bmVALID;
SYNCDELAY();
EP1OUTCFG &= ~bmVALID;
SYNCDELAY();
EP4CFG &= ~bmVALID;
SYNCDELAY();
EP8CFG &= ~bmVALID;
SYNCDELAY();
RESETFIFOS();
/* EP2 */
EP2FIFOCFG = 0x00; // allow core to see zero to one transition of auto out bit
SYNCDELAY();
EP2FIFOCFG = 0x10; // auto out mode, disable PKTEND zero length send, byte operation
SYNCDELAY();
EP2GPIFFLGSEL = 0x01; // For EP2OUT, GPIF uses emtpy flag
SYNCDELAY();
/* EP6 */
/* EP6: Enable AUTOIN mode. Set FIFO width to 8bits. */
EP6FIFOCFG = bmAUTOIN;
SYNCDELAY();
/* EP6: Auto-commit 512 (0x200) byte packets (due to AUTOIN = 1). */
EP6AUTOINLENH = 0x02;
SYNCDELAY();
EP6AUTOINLENL = 0x00;
SYNCDELAY();
/* EP6: Set the GPIF flag to 'full'. */
EP6GPIFFLGSEL = (1 << 1) | (0 << 1);
SYNCDELAY();
/* PA1~PA0: LED; PA2: sample enable; PA3: sample clear */
OEA = 0x0f;
IOA = 0x00;
}
void DSLogic_init(void)
{
recv_setup = FALSE;
command = 0;
/* Renumerate. */
RENUMERATE_UNCOND();
/* Set 8051 run @48MHz */
SETCPUFREQ(CLK_48M);
/* USB init */
USE_USB_INTS();
ENABLE_SOF();
ENABLE_HISPEED();
ENABLE_USBRESET();
ENABLE_SUDAV();
/* Global (8051) interrupt enable. */
EA = 1;
/* Setup the endpoints. */
setup_endpoints();
/* GPIF Wavedata Init*/
setup_gpif_waveforms();
}
void DSLogic_poll(void)
{
if (recv_setup) {
handle_setupdata();
recv_setup = FALSE;
}
switch (command) {
case CMD_START:
if ((EP0CS & bmEPBUSY) != 0)
break;
if (EP0BCL == sizeof(struct cmd_start)) {
if ((*(uint8_t *)EP0BUF) & CMD_START_FLAGS_STOP)
stop_capture();
else
start_capture();
}
/* Acknowledge the command. */
command = 0;
break;
case CMD_CONFIG:
if ((EP0CS & bmEPBUSY) != 0)
break;
if (EP0BCL == sizeof(struct cmd_cfg_count)) {
init_config_intf();
FIFORESET = 0x80; // set NAKALL bit to NAK all transfers from host
SYNCDELAY();
FIFORESET = 0x02; // reset EP2 FIFO
SYNCDELAY();
FIFORESET = 0x06; // reset EP6 FIFO
SYNCDELAY();
FIFORESET = 0x00; // clear NAKALL bit to resume normal operation
SYNCDELAY();
EP2FIFOCFG = 0x00; // allow core to see zero to one transition of auto out bit
SYNCDELAY();
EP2FIFOCFG = 0x10; // auto out mode, disable PKTEND zero length send, word ops
SYNCDELAY();
EP6FIFOCFG = 0x08; // auto in mode, disable PKTEND zero length send, word ops
SYNCDELAY();
GPIFIDLECTL &= 0xFB; //PROG_B signal low
delay(1); //PROG_B signal kept asserted for 1 ms
GPIFIDLECTL |= 0x04; //PROG_B signal high
SYNCDELAY();
// setup transaction count
GPIFTCB0 = ((const struct cmd_cfg_count *)EP0BUF)->byte0;
SYNCDELAY();
GPIFTCB1 = ((const struct cmd_cfg_count *)EP0BUF)->byte1;
SYNCDELAY();
GPIFTCB2 = ((const struct cmd_cfg_count *)EP0BUF)->byte2;
SYNCDELAY();
cfg_enable = TRUE;
}
/* Acknowledge the command. */
command = 0;
break;
case CMD_SETTING:
if ((EP0CS & bmEPBUSY) != 0)
break;
if (EP0BCL == sizeof(struct cmd_setting_count)) {
GPIFABORT = 0xff;
SYNCDELAY();
EP2FIFOCFG = 0x11; // auto out mode, disable PKTEND zero length send, word operation
SYNCDELAY();
setting_count_b0 = ((const struct cmd_setting_count *)EP0BUF)->byte0;
setting_count_b1 = ((const struct cmd_setting_count *)EP0BUF)->byte1;
setting_count_b2 = ((const struct cmd_setting_count *)EP0BUF)->byte2;
set_enable = TRUE;
}
/* Acknowledge the command. */
command = 0;
break;
case CMD_CONTROL:
if ((EP0CS & bmEPBUSY) != 0)
break;
if (EP0BCL == sizeof(struct cmd_control)) {
dsoConfig[0] = ((const struct cmd_control *)EP0BUF)->byte0;
dsoConfig[1] = ((const struct cmd_control *)EP0BUF)->byte1;
dsoConfig[2] = ((const struct cmd_control *)EP0BUF)->byte2;
dsoConfig[3] = ((const struct cmd_control *)EP0BUF)->byte3;
set_dso_ctrl = TRUE;
}
/* Acknowledge the command. */
command = 0;
break;
default:
/* Unimplemented command. */
command = 0;
break;
}
if (cfg_enable && (GPIFTRIG & 0x80)) { // if GPIF interface IDLE
if ( (EP24FIFOFLGS & 0x01) && (GPIFREADYSTAT & 0x01)) {
// if there's a packet in the peripheral domain for EP2
// and FPGA is ready to receive the configuration bitstream
IFCONFIG = 0xA6;
// 7 IFCLKSRC=1 , FIFOs executes on internal clk source
// 6 xMHz=0 , 30MHz internal clk rate
// 5 IFCLKOE=1 , Drive IFCLK pin signal at 30MHz
// 4 IFCLKPOL=0 , Don't invert IFCLK pin signal from internal clk
// 3 ASYNC=0 , master samples asynchronous
// 2 GSTATE=1 , Drive GPIF states out on PORTE[2:0], debug WF
// 1:0 IFCFG=10, FX2 in GPIF master mode
SYNCDELAY();
//delay(1); //avoid CSI_B deasserted during sync words
GPIFTRIG = GPIFTRIGWR | GPIF_EP2; // launch GPIF FIFO WRITE Transaction from EP2 FIFO
SYNCDELAY();
while( !( GPIFTRIG & 0x80 ) ); // poll GPIFTRIG.7 GPIF Done bit
SYNCDELAY();
cfg_enable= FALSE; //end of configuration
/* Put the FX2 into GPIF master mode and setup the GPIF. */
init_capture_intf();
if (GPIFREADYSTAT & 0x02) { // FPGA Configure Done
IOA |= 0x01;
IOA &= 0xf5;
delay(1);
IOA |= 0x08;
} else {
IOA &= 0xfc;
}
}
}
if (set_enable && (GPIFTRIG & 0x80)) { // if GPIF interface IDLE
if (!(EP24FIFOFLGS & 0x02)) {
SYNCDELAY();
GPIFTCB2 = setting_count_b2;
SYNCDELAY();
GPIFTCB1 = setting_count_b1; // fpga setting count
SYNCDELAY();
GPIFTCB0 = setting_count_b0;
SYNCDELAY();
GPIFTRIG = GPIFTRIGWR | GPIF_EP2; // launch GPIF FIFO WRITE Transaction from EP2 FIFO
SYNCDELAY();
while( !( GPIFTRIG & 0x80 ) ); // poll GPIFTRIG.7 GPIF Done bit
SYNCDELAY();
set_enable= FALSE; //end of configuration
/* Put the FX2 into GPIF master mode and setup the GPIF. */
init_capture_intf();
}
}
if (set_dso_ctrl) {
i2c_write (0x51, 0, NULL, 4, dsoConfig);
set_dso_ctrl = FALSE;
}
poll_intf();
}
void main(void)
{
DSLogic_init();
while (1)
DSLogic_poll();
}