]>
Commit | Line | Data |
---|---|---|
d5f5ea73 UH |
1 | /* |
2 | * This file is part of the fx2lafw project. | |
3 | * | |
4 | * Copyright (C) 2011-2012 Uwe Hermann <uwe@hermann-uwe.de> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
21 | /* | |
22 | * fx2lafw is an open-source firmware for Cypress FX2 based logic analyzers. | |
23 | * | |
24 | * It is written in C, using fx2lib as helper library, and sdcc as compiler. | |
25 | * The code is licensed under the terms of the GNU GPL, version 2 or later. | |
26 | * | |
27 | * Technical notes: | |
28 | * | |
29 | * - We use the FX2 in GPIF mode to sample the data (asynchronously). | |
30 | * - We use the internal 48MHz clock for GPIF. | |
31 | * - The 8 channels/pins we sample (the GPIF data bus) are PB0-PB7. | |
32 | * Support for 16 channels is not yet included, but might be added later. | |
33 | * - Endpoint 2 is used for data transfers from FX2 to host. | |
34 | * - The endpoint is quad-buffered. | |
35 | * | |
36 | * Documentation: | |
37 | * | |
38 | * - See http://sigrok.org/wiki/Fx2lafw | |
39 | */ | |
40 | ||
41 | #include <fx2regs.h> | |
42 | #include <fx2macros.h> | |
43 | #include <delay.h> | |
d5f5ea73 UH |
44 | #include <setupdat.h> |
45 | #include <eputils.h> | |
46 | #include <gpif.h> | |
e41576ec | 47 | |
64d47730 | 48 | #include <command.h> |
8f4a701f | 49 | #include <fx2lafw.h> |
e41576ec | 50 | #include <gpif-acquisition.h> |
d5f5ea73 | 51 | |
d5f5ea73 UH |
52 | /* ... */ |
53 | volatile bit got_sud; | |
2846a114 | 54 | BYTE vendor_command; |
d5f5ea73 | 55 | |
d5f5ea73 UH |
56 | static void setup_endpoints(void) |
57 | { | |
d5f5ea73 UH |
58 | /* Setup EP2 (IN). */ |
59 | EP2CFG = (1 << 7) | /* EP is valid/activated */ | |
60 | (1 << 6) | /* EP direction: IN */ | |
61 | (1 << 5) | (0 << 4) | /* EP Type: bulk */ | |
5a95b634 | 62 | (1 << 3) | /* EP buffer size: 1024 */ |
d5f5ea73 UH |
63 | (0 << 2) | /* Reserved. */ |
64 | (0 << 1) | (0 << 0); /* EP buffering: quad buffering */ | |
65 | SYNCDELAY(); | |
66 | ||
576c6627 | 67 | /* Disable all other EPs (EP1, EP4, EP6, and EP8). */ |
4ad20a4c UH |
68 | EP1INCFG &= ~bmVALID; |
69 | SYNCDELAY(); | |
70 | EP1OUTCFG &= ~bmVALID; | |
71 | SYNCDELAY(); | |
d5f5ea73 UH |
72 | EP4CFG &= ~bmVALID; |
73 | SYNCDELAY(); | |
576c6627 JH |
74 | EP6CFG &= ~bmVALID; |
75 | SYNCDELAY(); | |
d5f5ea73 UH |
76 | EP8CFG &= ~bmVALID; |
77 | SYNCDELAY(); | |
78 | ||
4ad20a4c | 79 | /* EP2: Reset the FIFOs. */ |
d5f5ea73 | 80 | /* Note: RESETFIFO() gets the EP number WITHOUT bit 7 set/cleared. */ |
d5f5ea73 | 81 | RESETFIFO(0x02) |
2d62ae47 | 82 | |
dc7ac8bf | 83 | /* EP2: Enable AUTOIN mode. Set FIFO width to 8bits. */ |
2d62ae47 | 84 | EP2FIFOCFG = bmAUTOIN; |
dc7ac8bf UH |
85 | SYNCDELAY(); |
86 | ||
87 | /* EP2: Auto-commit 512 (0x200) byte packets (due to AUTOIN = 1). */ | |
88 | EP2AUTOINLENH = 0x02; | |
89 | SYNCDELAY(); | |
90 | EP2AUTOINLENL = 0x00; | |
91 | SYNCDELAY(); | |
92 | ||
4ad20a4c | 93 | /* EP2: Set the GPIF flag to 'full'. */ |
fb0b6d28 | 94 | EP2GPIFFLGSEL = (1 << 1) | (0 << 1); |
d5f5ea73 UH |
95 | SYNCDELAY(); |
96 | } | |
97 | ||
18544912 JH |
98 | static void send_fw_version(void) |
99 | { | |
cd29817d UH |
100 | /* Populate the buffer. */ |
101 | struct version_info *const vi = (struct version_info *)EP0BUF; | |
18544912 JH |
102 | vi->major = FX2LAFW_VERSION_MAJOR; |
103 | vi->minor = FX2LAFW_VERSION_MINOR; | |
104 | ||
cd29817d | 105 | /* Send the message. */ |
18544912 JH |
106 | EP0BCH = 0; |
107 | EP0BCL = sizeof(struct version_info); | |
108 | } | |
109 | ||
d5f5ea73 UH |
110 | BOOL handle_vendorcommand(BYTE cmd) |
111 | { | |
c7283c28 | 112 | /* Protocol implementation */ |
c7283c28 | 113 | switch (cmd) { |
3b6919fa | 114 | case CMD_START: |
18544912 | 115 | vendor_command = cmd; |
2846a114 | 116 | EP0BCL = 0; |
18544912 | 117 | return TRUE; |
cd29817d | 118 | break; |
2846a114 | 119 | case CMD_GET_FW_VERSION: |
18544912 | 120 | send_fw_version(); |
3b6919fa | 121 | return TRUE; |
cd29817d | 122 | break; |
c7283c28 | 123 | } |
4ad20a4c | 124 | |
d5f5ea73 UH |
125 | return FALSE; |
126 | } | |
127 | ||
128 | BOOL handle_get_interface(BYTE ifc, BYTE *alt_ifc) | |
129 | { | |
130 | /* We only support interface 0, alternate interface 0. */ | |
131 | if (ifc != 0) | |
132 | return FALSE; | |
133 | ||
134 | *alt_ifc = 0; | |
135 | return TRUE; | |
136 | } | |
137 | ||
138 | BOOL handle_set_interface(BYTE ifc, BYTE alt_ifc) | |
139 | { | |
140 | /* We only support interface 0, alternate interface 0. */ | |
141 | if (ifc != 0 || alt_ifc != 0) | |
142 | return FALSE; | |
143 | ||
144 | /* Perform procedure from TRM, section 2.3.7: */ | |
145 | ||
146 | /* (1) TODO. */ | |
147 | ||
148 | /* (2) Reset data toggles of the EPs in the interface. */ | |
149 | /* Note: RESETTOGGLE() gets the EP number WITH bit 7 set/cleared. */ | |
d5f5ea73 | 150 | RESETTOGGLE(0x82); |
106ee45c UH |
151 | #ifdef DEBUG |
152 | RESETTOGGLE(0x86); | |
153 | #endif | |
d5f5ea73 UH |
154 | |
155 | /* (3) Restore EPs to their default conditions. */ | |
156 | /* Note: RESETFIFO() gets the EP number WITHOUT bit 7 set/cleared. */ | |
d5f5ea73 UH |
157 | RESETFIFO(0x02); |
158 | /* TODO */ | |
106ee45c | 159 | #ifdef DEBUG |
c430e296 | 160 | RESETFIFO(0x06); |
106ee45c | 161 | #endif |
c430e296 | 162 | |
d5f5ea73 UH |
163 | /* (4) Clear the HSNAK bit. Not needed, fx2lib does this. */ |
164 | ||
165 | return TRUE; | |
166 | } | |
167 | ||
168 | BYTE handle_get_configuration(void) | |
169 | { | |
170 | /* We only support configuration 1. */ | |
171 | return 1; | |
172 | } | |
173 | ||
174 | BOOL handle_set_configuration(BYTE cfg) | |
175 | { | |
176 | /* We only support configuration 1. */ | |
177 | return (cfg == 1) ? TRUE : FALSE; | |
178 | } | |
179 | ||
180 | void sudav_isr(void) interrupt SUDAV_ISR | |
181 | { | |
182 | got_sud = TRUE; | |
183 | CLEAR_SUDAV(); | |
184 | } | |
185 | ||
186 | void sof_isr(void) interrupt SOF_ISR using 1 | |
187 | { | |
188 | CLEAR_SOF(); | |
189 | } | |
190 | ||
191 | void usbreset_isr(void) interrupt USBRESET_ISR | |
192 | { | |
193 | handle_hispeed(FALSE); | |
194 | CLEAR_USBRESET(); | |
195 | } | |
196 | ||
197 | void hispeed_isr(void) interrupt HISPEED_ISR | |
198 | { | |
199 | handle_hispeed(TRUE); | |
200 | CLEAR_HISPEED(); | |
201 | } | |
202 | ||
1cbff47d | 203 | void fx2lafw_init(void) |
d5f5ea73 UH |
204 | { |
205 | /* Set DYN_OUT and ENH_PKT bits, as recommended by the TRM. */ | |
e7434142 | 206 | REVCTL = bmNOAUTOARM | bmSKIPCOMMIT; |
d5f5ea73 UH |
207 | |
208 | got_sud = FALSE; | |
2846a114 | 209 | vendor_command = 0; |
d5f5ea73 UH |
210 | |
211 | /* Renumerate. */ | |
212 | RENUMERATE_UNCOND(); | |
213 | ||
214 | SETCPUFREQ(CLK_48M); | |
215 | ||
216 | USE_USB_INTS(); | |
217 | ||
218 | /* TODO: Does the order of the following lines matter? */ | |
219 | ENABLE_SUDAV(); | |
220 | ENABLE_SOF(); | |
221 | ENABLE_HISPEED(); | |
222 | ENABLE_USBRESET(); | |
223 | ||
224 | /* Global (8051) interrupt enable. */ | |
225 | EA = 1; | |
226 | ||
227 | /* Setup the endpoints. */ | |
228 | setup_endpoints(); | |
229 | ||
230 | /* Put the FX2 into GPIF master mode and setup the GPIF. */ | |
231 | gpif_init_la(); | |
1cbff47d | 232 | } |
d5f5ea73 | 233 | |
28d52f41 | 234 | void fx2lafw_poll(void) |
1cbff47d JH |
235 | { |
236 | if (got_sud) { | |
237 | handle_setupdata(); | |
238 | got_sud = FALSE; | |
d5f5ea73 | 239 | } |
2846a114 JH |
240 | |
241 | if (vendor_command) { | |
242 | switch (vendor_command) { | |
2846a114 | 243 | case CMD_START: |
cd29817d | 244 | if ((EP0CS & bmEPBUSY) != 0) |
2846a114 JH |
245 | break; |
246 | ||
cd29817d | 247 | if (EP0BCL == 2) { |
2846a114 | 248 | gpif_acquisition_start( |
cd29817d | 249 | (const struct cmd_start_acquisition *)EP0BUF); |
2846a114 JH |
250 | } |
251 | ||
252 | /* Acknowledge the vendor command. */ | |
253 | vendor_command = 0; | |
254 | break; | |
2846a114 JH |
255 | default: |
256 | /* Unimplemented command. */ | |
257 | vendor_command = 0; | |
258 | break; | |
259 | } | |
260 | } | |
293d7e9e JH |
261 | |
262 | gpif_poll(); | |
d5f5ea73 | 263 | } |
f7f91781 JH |
264 | |
265 | void main(void) | |
266 | { | |
267 | fx2lafw_init(); | |
268 | while (1) | |
269 | fx2lafw_poll(); | |
270 | } |