]> sigrok.org Git - sigrok-firmware-fx2lafw.git/blame - gpif-acquisition.c
Not need to SYNCDELAY after writing to GPIF_WAVE_DATA
[sigrok-firmware-fx2lafw.git] / gpif-acquisition.c
CommitLineData
e41576ec
JH
1/*
2 * This file is part of the fx2lafw project.
3 *
4 * Copyright (C) 2011-2012 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22#include <fx2regs.h>
23#include <fx2macros.h>
24#include <delay.h>
25#include <gpif.h>
26
27#include <fx2lafw.h>
28#include <gpif-acquisition.h>
29
421e7d6d
JH
30static void gpif_reset_waveforms(void)
31{
32 int i;
e41576ec 33
421e7d6d
JH
34 /* Reset WAVEDATA. */
35 AUTOPTRSETUP = 0x03;
36 AUTOPTRH1 = 0xe4;
37 AUTOPTRL1 = 0x00;
38 for (i = 0; i < 128; i++)
39 EXTAUTODAT1 = 0;
40}
e41576ec
JH
41
42static void gpif_setup_registers(void)
43{
44 /* TODO. Value probably irrelevant, as we don't use RDY* signals? */
45 GPIFREADYCFG = 0;
46
47 /*
48 * Set TRICTL = 0, thus CTL0-CTL5 are CMOS outputs.
49 * TODO: Probably irrelevant, as we don't use CTL0-CTL5?
50 */
51 GPIFCTLCFG = 0;
52
53 /* When GPIF is idle, tri-state the data bus. */
54 /* Bit 7: DONE, bit 0: IDLEDRV. TODO: Set/clear DONE bit? */
55 GPIFIDLECS = (1 << 0);
56
57 /* When GPIF is idle, set CTL0-CTL5 to 0. */
58 GPIFIDLECTL = 0;
59
60 /*
421e7d6d 61 * Map index 0 in WAVEDATA to FIFORD. The rest is assigned too,
e41576ec
JH
62 * but not used by us.
63 *
64 * GPIFWFSELECT: [7:6] = SINGLEWR index, [5:4] = SINGLERD index,
65 * [3:2] = FIFOWR index, [1:0] = FIFORD index
66 */
67 GPIFWFSELECT = (0x3 << 6) | (0x2 << 4) | (0x1 << 2) | (0x0 << 0);
68
69 /* Contains RDY* pin values. Read-only according to TRM. */
70 GPIFREADYSTAT = 0;
1e588d06
JH
71
72 /* Make GPIF stop on transcation count not flag */
73 EP2GPIFPFSTOP = (0 << 0);
e41576ec
JH
74}
75
e41576ec
JH
76static void gpif_init_addr_pins(void)
77{
78 /*
79 * Configure the 9 GPIF address pins (GPIFADR[8:0], which consist of
80 * PORTC[7:0] and PORTE[7]), and output an initial address (zero).
81 * TODO: Probably irrelevant, the 56pin FX2 has no ports C and E.
82 */
83 PORTCCFG = 0xff; /* Set PORTC[7:0] as alt. func. (GPIFADR[7:0]). */
84 OEC = 0xff; /* Configure PORTC[7:0] as outputs. */
85 PORTECFG |= 0x80; /* Set PORTE[7] as alt. func. (GPIFADR[8]). */
86 OEE |= 0x80; /* Configure PORTE[7] as output. */
87 SYNCDELAY();
88 GPIFADRL = 0x00; /* Clear GPIFADR[7:0]. */
89 SYNCDELAY();
90 GPIFADRH = 0x00; /* Clear GPIFADR[8]. */
91}
92
93static void gpif_init_flowstates(void)
94{
95 /* Clear all flowstate registers, we don't use this functionality. */
96 FLOWSTATE = 0;
97 FLOWLOGIC = 0;
98 FLOWEQ0CTL = 0;
99 FLOWEQ1CTL = 0;
100 FLOWHOLDOFF = 0;
101 FLOWSTB = 0;
102 FLOWSTBEDGE = 0;
103 FLOWSTBHPERIOD = 0;
104}
105
106void gpif_init_la(void)
107{
108 /*
109 * Setup the FX2 in GPIF master mode, using the internal clock
110 * (non-inverted) at 48MHz, and using async sampling.
111 */
112 IFCONFIG = 0xee;
113
114 /* Abort currently executing GPIF waveform (if any). */
115 GPIFABORT = 0xff;
116
117 /* Setup the GPIF registers. */
118 gpif_setup_registers();
119
421e7d6d
JH
120 /* Reset WAVEDATA. */
121 gpif_reset_waveforms();
e41576ec
JH
122
123 /* Initialize GPIF address pins, output initial values. */
124 gpif_init_addr_pins();
125
126 /* Initialize flowstate registers (not used by us). */
127 gpif_init_flowstates();
128}
129
2846a114 130void gpif_acquisition_start(const struct cmd_start_acquisition *cmd)
e41576ec 131{
421e7d6d
JH
132 xdata volatile BYTE *pSTATE;
133
3b969d92
JH
134 /* Ensure GPIF is idle before reconfiguration */
135 while(!(GPIFTRIG & 0x80));
136
809dc779
JH
137 /* Set IFCONFIG to the correct clock source */
138 if(cmd->flags & CMD_START_FLAGS_CLK_48MHZ) {
139 IFCONFIG = bmIFCLKSRC |
140 bm3048MHZ |
141 bmIFCLKOE |
142 bmASYNC |
143 bmGSTATE |
144 bmIFGPIF;
145 } else {
146 IFCONFIG = bmIFCLKSRC |
147 bmIFCLKOE |
148 bmASYNC |
149 bmGSTATE |
150 bmIFGPIF;
151 }
2846a114 152
421e7d6d
JH
153 /* GPIF terminology: DP = decision point, NDP = non-decision-point */
154
155 /* Populate WAVEDATA
156 *
157 * This is the basic algorithm implemented in our GPIF state machine:
158 *
159 * State 0: NDP: Sample the FIFO data bus.
160 * State 1: DP: If EP2 is full, go to state 7 (the IDLE state), i.e.,
161 * end the current waveform. Otherwise, go to state 0 again,
162 * i.e., sample data until EP2 is full.
163 * State 2: Unused.
164 * State 3: Unused.
165 * State 4: Unused.
166 * State 5: Unused.
167 * State 6: Unused.
168 */
169
170 /* Populate S0 */
171 pSTATE = &GPIF_WAVE_DATA;
2846a114 172 pSTATE[0] = cmd->sample_delay;
421e7d6d
JH
173 pSTATE[8] = 0x02;
174 pSTATE[16] = 0x00;
175 pSTATE[24] = 0x00;
176
177 /* Populate S1 */
178 pSTATE = &GPIF_WAVE_DATA + 1;
179 pSTATE[0] = 0x00;
180 pSTATE[8] = 0x01;
181 pSTATE[16] = 0x00;
182 pSTATE[24] = 0x36;
183
1e588d06
JH
184 /* Execute the whole GPIF waveform once */
185 gpif_set_tc16(1);
186
e41576ec
JH
187 /* Perform the initial GPIF read. */
188 gpif_fifo_read(GPIF_EP2);
189}