-
Notifications
You must be signed in to change notification settings - Fork 33
/
gpif-acquisition.c
284 lines (235 loc) · 6.6 KB
/
gpif-acquisition.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
/*
* This file is part of the sigrok-firmware-fx2lafw project.
*
* Copyright (C) 2011-2012 Uwe Hermann <uwe@hermann-uwe.de>
* Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
*
* 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, see <http://www.gnu.org/licenses/>.
*/
#include <eputils.h>
#include <fx2regs.h>
#include <fx2macros.h>
#include <delay.h>
#include <gpif.h>
#include <fx2lafw.h>
#include <gpif-acquisition.h>
enum gpif_status gpif_acquiring = STOPPED;
static void gpif_reset_waveforms(void)
{
int i;
/* Reset WAVEDATA. */
AUTOPTRSETUP = 0x03;
AUTOPTRH1 = 0xe4;
AUTOPTRL1 = 0x00;
for (i = 0; i < 128; i++)
EXTAUTODAT1 = 0;
}
static void gpif_setup_registers(void)
{
/* TODO. Value probably irrelevant, as we don't use RDY* signals? */
GPIFREADYCFG = 0;
/* Set TRICTL = 0, thus CTL0-CTL5 are CMOS outputs. */
GPIFCTLCFG = 0;
/* When GPIF is idle, tri-state the data bus. */
/* Bit 7: DONE, bit 0: IDLEDRV. TODO: Set/clear DONE bit? */
GPIFIDLECS = (0 << 0);
/* When GPIF is idle, set CTL0-CTL5 to 0. */
GPIFIDLECTL = 0;
/*
* Map index 0 in WAVEDATA to FIFORD. The rest is assigned too,
* but not used by us.
*
* GPIFWFSELECT: [7:6] = SINGLEWR index, [5:4] = SINGLERD index,
* [3:2] = FIFOWR index, [1:0] = FIFORD index
*/
GPIFWFSELECT = (0x3u << 6) | (0x2u << 4) | (0x1u << 2) | (0x0u << 0);
/* Contains RDY* pin values. Read-only according to TRM. */
GPIFREADYSTAT = 0;
/* Make GPIF stop on transaction count not flag. */
EP2GPIFPFSTOP = (0 << 0);
}
static void gpif_init_addr_pins(void)
{
/*
* Configure the 9 GPIF address pins (GPIFADR[8:0], which consist of
* PORTC[7:0] and PORTE[7]), and output an initial address (zero).
* TODO: Probably irrelevant, the 56pin FX2 has no ports C and E.
*/
PORTCCFG = 0xff; /* Set PORTC[7:0] as alt. func. (GPIFADR[7:0]). */
OEC = 0xff; /* Configure PORTC[7:0] as outputs. */
PORTECFG |= 0x80; /* Set PORTE[7] as alt. func. (GPIFADR[8]). */
OEE |= 0x80; /* Configure PORTE[7] as output. */
SYNCDELAY();
GPIFADRL = 0x00; /* Clear GPIFADR[7:0]. */
SYNCDELAY();
GPIFADRH = 0x00; /* Clear GPIFADR[8]. */
}
static void gpif_init_flowstates(void)
{
/* Clear all flowstate registers, we don't use this functionality. */
FLOWSTATE = 0;
FLOWLOGIC = 0;
FLOWEQ0CTL = 0;
FLOWEQ1CTL = 0;
FLOWHOLDOFF = 0;
FLOWSTB = 0;
FLOWSTBEDGE = 0;
FLOWSTBHPERIOD = 0;
}
void gpif_init_la(void)
{
/*
* Setup the FX2 in GPIF master mode, using the internal clock
* (non-inverted) at 48MHz, and using async sampling.
*/
IFCONFIG = 0xee;
/* Abort currently executing GPIF waveform (if any). */
GPIFABORT = 0xff;
/* Setup the GPIF registers. */
gpif_setup_registers();
/* Reset WAVEDATA. */
gpif_reset_waveforms();
/* Initialize GPIF address pins, output initial values. */
gpif_init_addr_pins();
/* Initialize flowstate registers (not used by us). */
gpif_init_flowstates();
/* Reset the status. */
gpif_acquiring = STOPPED;
}
static void gpif_make_delay_state(volatile BYTE *pSTATE, uint8_t delay, uint8_t output)
{
/*
* DELAY
* Delay cmd->sample_delay clocks.
*/
pSTATE[0] = delay;
/*
* OPCODE
* SGL=0, GIN=0, INCAD=0, NEXT=0, DATA=0, DP=0
*/
pSTATE[8] = 0;
/*
* OUTPUT
* CTL[0:5]=output
*/
pSTATE[16] = output;
/*
* LOGIC FUNCTION
* Not used.
*/
pSTATE[24] = 0x00;
}
static void gpif_make_data_dp_state(volatile BYTE *pSTATE)
{
/*
* BRANCH
* Branch to IDLE if condition is true, back to S0 otherwise.
*/
pSTATE[0] = (1u << 7) | (7u << 3) | (0u << 0);
/*
* OPCODE
* SGL=0, GIN=0, INCAD=0, NEXT=0, DATA=1, DP=1
*/
pSTATE[8] = (1 << 1) | (1 << 0);
/*
* OUTPUT
* CTL[0:5]=0
*/
pSTATE[16] = 0x00;
/*
* LOGIC FUNCTION
* Evaluate if the FIFO full flag is set.
* LFUNC=0 (AND), TERMA=6 (FIFO Flag), TERMB=6 (FIFO Flag)
*/
pSTATE[24] = (6 << 3) | (6 << 0);
}
bool gpif_acquisition_prepare(const struct cmd_start_acquisition *cmd)
{
int i;
volatile BYTE *pSTATE = &GPIF_WAVE_DATA;
/* Ensure GPIF is idle before reconfiguration. */
while (!(GPIFTRIG & 0x80));
/* Configure the EP2 FIFO. */
if (cmd->flags & CMD_START_FLAGS_SAMPLE_16BIT)
EP2FIFOCFG = bmAUTOIN | bmWORDWIDE;
else
EP2FIFOCFG = bmAUTOIN;
SYNCDELAY();
/* Set IFCONFIG to the correct clock source. */
if (cmd->flags & CMD_START_FLAGS_CLK_48MHZ) {
IFCONFIG = bmIFCLKSRC | bm3048MHZ | bmIFCLKOE | bmASYNC |
bmGSTATE | bmIFGPIF;
} else {
IFCONFIG = bmIFCLKSRC | bmIFCLKOE | bmASYNC |
bmGSTATE | bmIFGPIF;
}
/* Populate delay states. */
if (cmd->sample_delay_h >= 6)
return false;
if (cmd->flags & CMD_START_FLAGS_CLK_CTL2) {
uint8_t delay_1, delay_2 = cmd->sample_delay_l;
/* We need a pulse where the CTL1/2 pins alternate states. */
if (cmd->sample_delay_h) {
for (i = 0; i < cmd->sample_delay_h; i++)
gpif_make_delay_state(pSTATE++, 0, 0x06);
} else {
delay_1 = delay_2 / 2;
delay_2 -= delay_1;
gpif_make_delay_state(pSTATE++, delay_1, 0x06);
}
/* sample_delay_l is always != 0 for the supported rates. */
gpif_make_delay_state(pSTATE++, delay_2, 0x00);
} else {
for (i = 0; i < cmd->sample_delay_h; i++)
gpif_make_delay_state(pSTATE++, 0, 0x00);
if (cmd->sample_delay_l != 0)
gpif_make_delay_state(pSTATE++, cmd->sample_delay_l, 0x00);
}
/* Populate S1 - the decision point. */
gpif_make_data_dp_state(pSTATE++);
/* Update the status. */
gpif_acquiring = PREPARED;
return true;
}
void gpif_acquisition_start(void)
{
/* Execute the whole GPIF waveform once. */
gpif_set_tc16(1);
/* Perform the initial GPIF read. */
gpif_fifo_read(GPIF_EP2);
/* Update the status. */
gpif_acquiring = RUNNING;
}
void gpif_poll(void)
{
/* Detect if acquisition has completed. */
if ((gpif_acquiring == RUNNING) && (GPIFTRIG & 0x80)) {
/* Activate NAK-ALL to avoid race conditions. */
FIFORESET = 0x80;
SYNCDELAY();
/* Switch to manual mode. */
EP2FIFOCFG = 0;
SYNCDELAY();
/* Reset EP2. */
FIFORESET = 0x02;
SYNCDELAY();
/* Return to auto mode. */
EP2FIFOCFG = bmAUTOIN;
SYNCDELAY();
/* Release NAK-ALL. */
FIFORESET = 0x00;
SYNCDELAY();
gpif_acquiring = STOPPED;
}
}