2 * This file is part of the fx2lafw project.
4 * Copyright (C) 2011-2012 Uwe Hermann <uwe@hermann-uwe.de>
5 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
24 #include <fx2macros.h>
28 #include <gpif-acquisition.h>
32 static void gpif_reset_waveforms(void)
40 for (i = 0; i < 128; i++)
44 static void gpif_setup_registers(void)
46 /* TODO. Value probably irrelevant, as we don't use RDY* signals? */
50 * Set TRICTL = 0, thus CTL0-CTL5 are CMOS outputs.
51 * TODO: Probably irrelevant, as we don't use CTL0-CTL5?
55 /* When GPIF is idle, tri-state the data bus. */
56 /* Bit 7: DONE, bit 0: IDLEDRV. TODO: Set/clear DONE bit? */
57 GPIFIDLECS = (1 << 0);
59 /* When GPIF is idle, set CTL0-CTL5 to 0. */
63 * Map index 0 in WAVEDATA to FIFORD. The rest is assigned too,
66 * GPIFWFSELECT: [7:6] = SINGLEWR index, [5:4] = SINGLERD index,
67 * [3:2] = FIFOWR index, [1:0] = FIFORD index
69 GPIFWFSELECT = (0x3 << 6) | (0x2 << 4) | (0x1 << 2) | (0x0 << 0);
71 /* Contains RDY* pin values. Read-only according to TRM. */
74 /* Make GPIF stop on transcation count not flag */
75 EP2GPIFPFSTOP = (0 << 0);
78 static void gpif_init_addr_pins(void)
81 * Configure the 9 GPIF address pins (GPIFADR[8:0], which consist of
82 * PORTC[7:0] and PORTE[7]), and output an initial address (zero).
83 * TODO: Probably irrelevant, the 56pin FX2 has no ports C and E.
85 PORTCCFG = 0xff; /* Set PORTC[7:0] as alt. func. (GPIFADR[7:0]). */
86 OEC = 0xff; /* Configure PORTC[7:0] as outputs. */
87 PORTECFG |= 0x80; /* Set PORTE[7] as alt. func. (GPIFADR[8]). */
88 OEE |= 0x80; /* Configure PORTE[7] as output. */
90 GPIFADRL = 0x00; /* Clear GPIFADR[7:0]. */
92 GPIFADRH = 0x00; /* Clear GPIFADR[8]. */
95 static void gpif_init_flowstates(void)
97 /* Clear all flowstate registers, we don't use this functionality. */
108 void gpif_init_la(void)
111 * Setup the FX2 in GPIF master mode, using the internal clock
112 * (non-inverted) at 48MHz, and using async sampling.
116 /* Abort currently executing GPIF waveform (if any). */
119 /* Setup the GPIF registers. */
120 gpif_setup_registers();
122 /* Reset WAVEDATA. */
123 gpif_reset_waveforms();
125 /* Initialize GPIF address pins, output initial values. */
126 gpif_init_addr_pins();
128 /* Initialize flowstate registers (not used by us). */
129 gpif_init_flowstates();
131 /* Reset the status */
132 gpif_acquiring = FALSE;
135 static void gpif_make_delay_state(volatile BYTE *pSTATE, uint8_t delay)
139 * Delay cmd->sample_delay clocks.
145 * SGL=0, GIN=0, INCAD=0, NEXT=0, DATA=0, DP=0
146 * Collect data in this state.
152 * OE[0:3]=0, CTL[0:3]=0
163 static void gpid_make_data_dp_state(volatile BYTE *pSTATE)
167 * Branch to IDLE if condition is true, back to S0 otherwise.
169 pSTATE[0] = (7 << 3) | (0 << 0);
173 * SGL=0, GIN=0, INCAD=0, NEXT=0, DATA=1, DP=1
175 pSTATE[8] = (1 << 1) | (1 << 0);
179 * OE[0:3]=0, CTL[0:3]=0
185 * Evaluate if the FIFO full flag is set.
186 * LFUNC=0 (AND), TERMA=6 (FIFO Flag), TERMB=6 (FIFO Flag)
188 pSTATE[24] = (6 << 3) | (6 << 0);
191 bool gpif_acquisition_start(const struct cmd_start_acquisition *cmd)
194 volatile BYTE *pSTATE = &GPIF_WAVE_DATA;
196 /* Ensure GPIF is idle before reconfiguration. */
197 while (!(GPIFTRIG & 0x80));
199 /* Configure the EP2 FIFO */
200 if(cmd->flags & CMD_START_FLAGS_SAMPLE_16BIT) {
201 EP2FIFOCFG = bmAUTOIN | bmWORDWIDE;
203 EP2FIFOCFG = bmAUTOIN;
207 /* Set IFCONFIG to the correct clock source. */
208 if (cmd->flags & CMD_START_FLAGS_CLK_48MHZ) {
209 IFCONFIG = bmIFCLKSRC | bm3048MHZ | bmIFCLKOE | bmASYNC |
212 IFCONFIG = bmIFCLKSRC | bmIFCLKOE | bmASYNC |
216 /* Populate delay states */
217 if((cmd->sample_delay_h == 0 && cmd->sample_delay_l == 0) ||
218 cmd->sample_delay_h >= 6)
221 for(i = 0; i < cmd->sample_delay_h; i++)
222 gpif_make_delay_state(pSTATE++, 0);
224 if(cmd->sample_delay_l != 0)
225 gpif_make_delay_state(pSTATE++, cmd->sample_delay_l);
227 /* Populate S1 - the decision point. */
228 gpid_make_data_dp_state(pSTATE++);
230 /* Execute the whole GPIF waveform once. */
233 /* Perform the initial GPIF read. */
234 gpif_fifo_read(GPIF_EP2);
236 /* Update the status. */
237 gpif_acquiring = TRUE;
244 /* Detect if acquisition has completed. */
245 if (gpif_acquiring && (GPIFTRIG & 0x80)) {
246 /* Activate NAK-ALL to avoid race conditions. */
250 /* Switch to manual mode. */
258 /* Return to auto mode. */
259 EP2FIFOCFG = bmAUTOIN;
262 /* Release NAK-ALL. */
266 gpif_acquiring = FALSE;