1 |
786 |
skrzyp |
//==========================================================================
|
2 |
|
|
//
|
3 |
|
|
// usbs_d12.c
|
4 |
|
|
//
|
5 |
|
|
// Driver for the D12 USB Slave Board
|
6 |
|
|
//
|
7 |
|
|
//==========================================================================
|
8 |
|
|
// ####ECOSGPLCOPYRIGHTBEGIN####
|
9 |
|
|
// -------------------------------------------
|
10 |
|
|
// This file is part of eCos, the Embedded Configurable Operating System.
|
11 |
|
|
// Copyright (C) 1998, 1999, 2000, 2001, 2002, 2006 Free Software Foundation, Inc.
|
12 |
|
|
//
|
13 |
|
|
// eCos is free software; you can redistribute it and/or modify it under
|
14 |
|
|
// the terms of the GNU General Public License as published by the Free
|
15 |
|
|
// Software Foundation; either version 2 or (at your option) any later
|
16 |
|
|
// version.
|
17 |
|
|
//
|
18 |
|
|
// eCos is distributed in the hope that it will be useful, but WITHOUT
|
19 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
20 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
21 |
|
|
// for more details.
|
22 |
|
|
//
|
23 |
|
|
// You should have received a copy of the GNU General Public License
|
24 |
|
|
// along with eCos; if not, write to the Free Software Foundation, Inc.,
|
25 |
|
|
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
26 |
|
|
//
|
27 |
|
|
// As a special exception, if other files instantiate templates or use
|
28 |
|
|
// macros or inline functions from this file, or you compile this file
|
29 |
|
|
// and link it with other works to produce a work based on this file,
|
30 |
|
|
// this file does not by itself cause the resulting work to be covered by
|
31 |
|
|
// the GNU General Public License. However the source code for this file
|
32 |
|
|
// must still be made available in accordance with section (3) of the GNU
|
33 |
|
|
// General Public License v2.
|
34 |
|
|
//
|
35 |
|
|
// This exception does not invalidate any other reasons why a work based
|
36 |
|
|
// on this file might be covered by the GNU General Public License.
|
37 |
|
|
// -------------------------------------------
|
38 |
|
|
// ####ECOSGPLCOPYRIGHTEND####
|
39 |
|
|
//==========================================================================
|
40 |
|
|
//#####DESCRIPTIONBEGIN####
|
41 |
|
|
//
|
42 |
|
|
// Author(s): Frank M. Pagliughi (fmp)
|
43 |
|
|
// Date: 2004-05-22
|
44 |
|
|
//
|
45 |
|
|
// This code is a device driver for the SoRo Systems USB-D12-104, a PC/104
|
46 |
|
|
// (ISA) Full-Speed USB slave board, which turns a PC/104 stack into a USB
|
47 |
|
|
// slave device. The board contains a Philips PDIUSBD12 Peripheral Controller
|
48 |
|
|
// Chip mapped into the PC's I/O space, with jumper-selectable I/O base
|
49 |
|
|
// address, IRQ, and DMA settings. The eCos config tool is used to adjust
|
50 |
|
|
// settings for this driver to match the physical jumper settings. The chip
|
51 |
|
|
// could run in polled mode without an IRQ, but this wouldn't be a great idea
|
52 |
|
|
// other than maybe a debug environment.
|
53 |
|
|
//
|
54 |
|
|
// The board supports DMA transfers over the Main endpoint, but I temporarily
|
55 |
|
|
// removed that code to make the driver portable to other platforms.
|
56 |
|
|
//
|
57 |
|
|
// *** This driver should also work with the Philips ISA Eval Board
|
58 |
|
|
// for the D12, but I couldn't get one of them from Philips, so
|
59 |
|
|
// I couldn't test it.
|
60 |
|
|
//
|
61 |
|
|
// The D12 uses an indexed register set, which it describes as "commands."
|
62 |
|
|
// You first write a command (index) to the command register then you can
|
63 |
|
|
// read or write data to that register. Each multi-byte command read or write
|
64 |
|
|
// must be dione atomically, so all access to the chip must be serialized.
|
65 |
|
|
//
|
66 |
|
|
// The D12 requests service through a single interrupt. The driver can
|
67 |
|
|
// be configured to service the chip through a DSR or a thread. In either
|
68 |
|
|
// case, the "service" code assumes it has unfettered access to the chip.
|
69 |
|
|
// The interrupt, therefore never touches the chip. It just schedules the
|
70 |
|
|
// DSR or service thread.
|
71 |
|
|
// Currently, the code gets exclusive access to the chip by locking the
|
72 |
|
|
// scheduler. This is suboptimal (locking the whole OS to touch one I/O
|
73 |
|
|
// chip), and better method should be explored.
|
74 |
|
|
//
|
75 |
|
|
// This version of the driver does not support Isocronous transfers.
|
76 |
|
|
//
|
77 |
|
|
// Additional notes on the D12:
|
78 |
|
|
//
|
79 |
|
|
// - The D12 has 4 endpoints (2 IN, and 2 OUT) in addition to the main
|
80 |
|
|
// control endpoint:
|
81 |
|
|
// - Endp 0 (Control In & Out, 16 byte buffer)
|
82 |
|
|
// - Endp 1 (IN & OUT, Bulk or Interrupt, 16 byte ea)
|
83 |
|
|
// - Endp 2 (IN and/or OUT, Bulk, Interrupt, or Isoc, 64 bytes ea)
|
84 |
|
|
//
|
85 |
|
|
// - The "main" endpoint (as Philips calls it) is Endp 2. It's double
|
86 |
|
|
// buffered and has a DMA interface, and thus, is suited for high
|
87 |
|
|
// throughput. For applications that perform either Isoc In or Out,
|
88 |
|
|
// the buffers for Endp 2 can be combined for a 128 byte space.
|
89 |
|
|
// This driver, however, currently does not support this.
|
90 |
|
|
//
|
91 |
|
|
// - There may be a flaw in the double buffering of the rx main endpoint.
|
92 |
|
|
// According to the documentation it should be invisible to the software,
|
93 |
|
|
// but if both buffers fill (on an rx/OUT), they must both be read
|
94 |
|
|
// together, otherwise it appears that the buffers/packets are returned
|
95 |
|
|
// in reverse order. ReadMainEndpointBuf() returns the data properly.
|
96 |
|
|
//
|
97 |
|
|
// - All the interrupt sources on the chip - individual endpoints, bus reset,
|
98 |
|
|
// suspend, and DMA - are OR'ed together and can be checked via the
|
99 |
|
|
// interrupt status register. When using edge-sensitive interrupts, as
|
100 |
|
|
// we do here, the ISR/DSR must be sure all interrupts are cleared before
|
101 |
|
|
// returning otherwise no new interrupts will be latched.
|
102 |
|
|
//
|
103 |
|
|
// - If the DMA controller is not used for the Main Endpoint, you MUST enable
|
104 |
|
|
// the main endpoint interrupts in the DMA register (bits 6 & 7).
|
105 |
|
|
// Personally, I think this should be the default at reset, to make it
|
106 |
|
|
// compatible with the other endpoints, but Philips didn't see it that
|
107 |
|
|
// way.
|
108 |
|
|
//
|
109 |
|
|
// - When a Setup (Device Request) packet arrives in the control endpoint, a
|
110 |
|
|
// bit is set in the endpoint's status register indicating the packet is
|
111 |
|
|
// setup and not data. By the USB standard, a setup packet can not be
|
112 |
|
|
// NAK'ed or STALL'ed, so when the chip receives a setup packet, it
|
113 |
|
|
// flushes the Ctrl (EP0) IN buffer and disables the Validate and Clear
|
114 |
|
|
// Buffer commands. We must send an "acknowledge setup" to both
|
115 |
|
|
// EP0 IN and OUT before a Validate or Clear Buffer command is effective.
|
116 |
|
|
// See ReadSetupPacket().
|
117 |
|
|
//
|
118 |
|
|
//####DESCRIPTIONEND####
|
119 |
|
|
//==========================================================================
|
120 |
|
|
|
121 |
|
|
#include <cyg/infra/cyg_type.h>
|
122 |
|
|
#include <cyg/infra/cyg_ass.h>
|
123 |
|
|
#include <cyg/infra/cyg_trac.h>
|
124 |
|
|
#include <cyg/infra/diag.h>
|
125 |
|
|
|
126 |
|
|
#include <pkgconf/devs_usb_d12.h>
|
127 |
|
|
|
128 |
|
|
#include <cyg/hal/drv_api.h>
|
129 |
|
|
#include <cyg/hal/hal_arch.h>
|
130 |
|
|
#include <cyg/hal/hal_io.h>
|
131 |
|
|
#include <cyg/hal/hal_cache.h>
|
132 |
|
|
#include <cyg/error/codes.h>
|
133 |
|
|
|
134 |
|
|
#include <cyg/io/usb/usb.h>
|
135 |
|
|
#include <cyg/io/usb/usbs.h>
|
136 |
|
|
|
137 |
|
|
#include <string.h>
|
138 |
|
|
|
139 |
|
|
// --------------------------------------------------------------------------
|
140 |
|
|
// Common Types
|
141 |
|
|
// --------------------------------------------------------------------------
|
142 |
|
|
|
143 |
|
|
typedef cyg_uint8 byte;
|
144 |
|
|
typedef cyg_uint8 uint8;
|
145 |
|
|
typedef cyg_int16 int16;
|
146 |
|
|
typedef cyg_uint16 uint16;
|
147 |
|
|
typedef cyg_int32 int32;
|
148 |
|
|
typedef cyg_uint32 uint32;
|
149 |
|
|
|
150 |
|
|
// --------------------------------------------------------------------------
|
151 |
|
|
// Tracing & Debug
|
152 |
|
|
// --------------------------------------------------------------------------
|
153 |
|
|
// If the driver is configured to use a thread to service the chip, then it
|
154 |
|
|
// can also be configured to dump a lot of debug output.
|
155 |
|
|
// Care must be taken that USB timing requirements are not violated by
|
156 |
|
|
// dumping debug info. If the data is sent to a serial port, it should use
|
157 |
|
|
// a hardware driver and have a large output buffer (115200 baud & 2kB
|
158 |
|
|
// buffer works for me).
|
159 |
|
|
|
160 |
|
|
#if defined(CYGFUN_DEVS_USB_D12_DEBUG) && CYGFUN_DEVS_USB_D12_DEBUG
|
161 |
|
|
#define TRACE_D12 diag_printf
|
162 |
|
|
#else
|
163 |
|
|
#define TRACE_D12 (1) ? (void)0 : diag_printf
|
164 |
|
|
#endif
|
165 |
|
|
|
166 |
|
|
#if defined(CYGSEM_DEVS_USB_D12_DEBUG_DUMP_EP0_BUFS) && CYGSEM_DEVS_USB_D12_DEBUG_DUMP_EP0_BUFS
|
167 |
|
|
#define TRACE_EP0 1
|
168 |
|
|
#endif
|
169 |
|
|
|
170 |
|
|
#if defined(CYGSEM_DEVS_USB_D12_DEBUG_DUMP_BUFS) && CYGSEM_DEVS_USB_D12_DEBUG_DUMP_BUFS
|
171 |
|
|
#define TRACE_EP 1
|
172 |
|
|
#endif
|
173 |
|
|
|
174 |
|
|
#if defined(TRACE_EP0) || defined(TRACE_EP)
|
175 |
|
|
static void _trace_buf(const char *hdr, const byte* buf, unsigned n)
|
176 |
|
|
{
|
177 |
|
|
unsigned i;
|
178 |
|
|
|
179 |
|
|
if (buf != 0 && n != 0) {
|
180 |
|
|
if (hdr && hdr[0])
|
181 |
|
|
TRACE_D12("%s ", hdr);
|
182 |
|
|
|
183 |
|
|
TRACE_D12("[");
|
184 |
|
|
for (i=0; i<n; i++)
|
185 |
|
|
TRACE_D12(" x%02X", buf[i]);
|
186 |
|
|
TRACE_D12("]\n");
|
187 |
|
|
}
|
188 |
|
|
}
|
189 |
|
|
#endif
|
190 |
|
|
|
191 |
|
|
#if defined(TRACE_EP0)
|
192 |
|
|
#define TRACE_BUF0 _trace_buf
|
193 |
|
|
#else
|
194 |
|
|
#define TRACE_BUF0(hdr, buf, n)
|
195 |
|
|
#endif
|
196 |
|
|
|
197 |
|
|
#if defined(TRACE_EP)
|
198 |
|
|
#define TRACE_BUF _trace_buf
|
199 |
|
|
#else
|
200 |
|
|
#define TRACE_BUF(hdr, buf, n)
|
201 |
|
|
#endif
|
202 |
|
|
|
203 |
|
|
// ==========================================================================
|
204 |
|
|
// Chip Wrapper
|
205 |
|
|
// ==========================================================================
|
206 |
|
|
|
207 |
|
|
// This section contains functions that wrapper the low-level access to the
|
208 |
|
|
// chip. There's a function around each register access on the chip, and then
|
209 |
|
|
// some.
|
210 |
|
|
|
211 |
|
|
#if defined(CYGSEM_DEVS_USB_D12_IO_MAPPED)
|
212 |
|
|
typedef void* d12_addr_type;
|
213 |
|
|
#else
|
214 |
|
|
typedef byte* d12_addr_type;
|
215 |
|
|
#endif
|
216 |
|
|
|
217 |
|
|
#define D12_BASE_ADDR ((d12_addr_type) CYGNUM_DEVS_USB_D12_BASEADDR)
|
218 |
|
|
|
219 |
|
|
#define D12_ENDP0_SIZE 16 // Size of Ctrl Endp
|
220 |
|
|
#define D12_MAIN_ENDP 2 // The D12's "Main" Endp is special, double buffered
|
221 |
|
|
#define D12_MAIN_ENDP_SIZE 64 // Size of each main endp buffer
|
222 |
|
|
#define D12_MAX_PACKET_SIZE 128 // Max packet is actually double main endp
|
223 |
|
|
|
224 |
|
|
#define D12_CHIP_ID 0x1012 // Value that's returned by a read of
|
225 |
|
|
//the D12's Chip ID register
|
226 |
|
|
|
227 |
|
|
// ----- Endpoint Indices -----
|
228 |
|
|
|
229 |
|
|
enum {
|
230 |
|
|
D12_ENDP_INVALID = 0xFF,
|
231 |
|
|
D12_ENDP_MIN = 0,
|
232 |
|
|
|
233 |
|
|
D12_RX_CTRL_ENDP = D12_ENDP_MIN, // Rx/Tx Nomenclature
|
234 |
|
|
D12_TX_CTRL_ENDP,
|
235 |
|
|
|
236 |
|
|
D12_RX_ENDP0 = D12_ENDP_MIN,
|
237 |
|
|
D12_TX_ENDP0,
|
238 |
|
|
D12_RX_ENDP1,
|
239 |
|
|
D12_TX_ENDP1,
|
240 |
|
|
D12_RX_ENDP2,
|
241 |
|
|
D12_TX_ENDP2,
|
242 |
|
|
D12_RX_MAIN_ENDP = D12_RX_ENDP2,
|
243 |
|
|
D12_TX_MAIN_ENDP = D12_TX_ENDP2,
|
244 |
|
|
|
245 |
|
|
|
246 |
|
|
D12_CTRL_ENDP_OUT = D12_ENDP_MIN, // IN/OUT Nomenclature
|
247 |
|
|
D12_CTRL_ENDP_IN,
|
248 |
|
|
|
249 |
|
|
D12_ENDP0_OUT = D12_ENDP_MIN,
|
250 |
|
|
D12_ENDP0_IN,
|
251 |
|
|
D12_ENDP1_OUT,
|
252 |
|
|
D12_ENDP1_IN,
|
253 |
|
|
D12_ENDP2_OUT,
|
254 |
|
|
D12_ENDP2_IN,
|
255 |
|
|
D12_MAIN_ENDP_OUT = D12_ENDP2_OUT,
|
256 |
|
|
D12_MAIN_ENDP_IN = D12_ENDP2_IN,
|
257 |
|
|
|
258 |
|
|
D12_ENDP_INSERT_BEFORE,
|
259 |
|
|
D12_ENDP_MAX = D12_ENDP_INSERT_BEFORE-1
|
260 |
|
|
};
|
261 |
|
|
|
262 |
|
|
// ----- Set Mode Reg configuration byte -----
|
263 |
|
|
|
264 |
|
|
enum {
|
265 |
|
|
D12_MODE_CFG_NO_LAZYCLOCK = 0x02,
|
266 |
|
|
D12_MODE_CFG_CLOCK_RUNNING = 0x04,
|
267 |
|
|
D12_MODE_CFG_INTERRUPT = 0x08,
|
268 |
|
|
D12_MODE_CFG_SOFT_CONNECT = 0x10,
|
269 |
|
|
|
270 |
|
|
D12_MODE_CFG_NON_ISO = 0x00,
|
271 |
|
|
D12_MODE_CFG_ISO_OUT = 0x40,
|
272 |
|
|
D12_MODE_CFG_ISO_IN = 0x80,
|
273 |
|
|
D12_MODE_CFG_ISO_IO = 0xC0,
|
274 |
|
|
|
275 |
|
|
D12_MODE_CFG_DFLT = (D12_MODE_CFG_NO_LAZYCLOCK |
|
276 |
|
|
D12_MODE_CFG_CLOCK_RUNNING |
|
277 |
|
|
D12_MODE_CFG_NON_ISO)
|
278 |
|
|
};
|
279 |
|
|
|
280 |
|
|
// ----- Set Mode Reg clock div factor -----
|
281 |
|
|
|
282 |
|
|
enum {
|
283 |
|
|
D12_MODE_CLK_24_MHZ = 1,
|
284 |
|
|
D12_MODE_CLK_16_MHZ = 2,
|
285 |
|
|
D12_MODE_CLK_12_MHZ = 3,
|
286 |
|
|
D12_MODE_CLK_8_MHZ = 5,
|
287 |
|
|
D12_MODE_CLK_6_MHZ = 7,
|
288 |
|
|
D12_MODE_CLK_4_MHZ = 11,
|
289 |
|
|
|
290 |
|
|
D12_MODE_CLK_DIV_MASK = 0x0F,
|
291 |
|
|
|
292 |
|
|
D12_MODE_CLK_SET_TO_ONE = 0x40,
|
293 |
|
|
D12_MODE_CLK_SOF_ONLY_INTR = 0x80,
|
294 |
|
|
|
295 |
|
|
D12_MODE_CLK_DFLT = (D12_MODE_CLK_4_MHZ |
|
296 |
|
|
D12_MODE_CLK_SET_TO_ONE)
|
297 |
|
|
};
|
298 |
|
|
|
299 |
|
|
// ----- Set DMA Register -----
|
300 |
|
|
|
301 |
|
|
enum {
|
302 |
|
|
D12_DMA_SINGLE_CYCLE,
|
303 |
|
|
D12_DMA_BURST_4_CYCLE,
|
304 |
|
|
D12_DMA_BURST_8_CYCLE,
|
305 |
|
|
D12_DMA_BURST_16_CYCLE,
|
306 |
|
|
|
307 |
|
|
D12_DMA_ENABLE = 0x04,
|
308 |
|
|
D12_DMA_DIR_WRITE = 0x08,
|
309 |
|
|
D12_DMA_DIR_READ = 0x00,
|
310 |
|
|
D12_DMA_AUTO_RELOAD = 0x10,
|
311 |
|
|
D12_DMA_INTR_PIN_MODE = 0x20,
|
312 |
|
|
|
313 |
|
|
D12_DMA_MAIN_ENDP_OUT_INTR_ENABLE = 0x40,
|
314 |
|
|
D12_DMA_MAIN_RX_ENDP_INTR_ENABLE = 0x40,
|
315 |
|
|
|
316 |
|
|
D12_DMA_MAIN_ENDP_IN_INTR_ENABLE = 0x80,
|
317 |
|
|
D12_DMA_MAIN_TX_ENDP_INTR_ENABLE = 0x80,
|
318 |
|
|
|
319 |
|
|
D12_DMA_MAIN_ENDP_INTR_ENABLE = 0xC0 // Enables IN & OUT Intr
|
320 |
|
|
};
|
321 |
|
|
|
322 |
|
|
// ----- Interrupt Register Bits -----
|
323 |
|
|
|
324 |
|
|
enum {
|
325 |
|
|
D12_INTR_RX_CTRL_ENDP = 0x0001,
|
326 |
|
|
D12_INTR_TX_CTRL_ENDP = 0x0002,
|
327 |
|
|
|
328 |
|
|
D12_INTR_RX_ENDP0 = D12_INTR_RX_CTRL_ENDP,
|
329 |
|
|
D12_INTR_TX_ENDP0 = D12_INTR_TX_CTRL_ENDP,
|
330 |
|
|
D12_INTR_RX_ENDP1 = 0x0004,
|
331 |
|
|
D12_INTR_TX_ENDP1 = 0x0008,
|
332 |
|
|
D12_INTR_RX_ENDP2 = 0x0010,
|
333 |
|
|
D12_INTR_TX_ENDP2 = 0x0020,
|
334 |
|
|
|
335 |
|
|
D12_INTR_BUS_RESET = 0x0040,
|
336 |
|
|
D12_INTR_SUSPEND_CHANGE = 0x0080,
|
337 |
|
|
D12_INTR_DMA_EOT = 0x0100
|
338 |
|
|
};
|
339 |
|
|
|
340 |
|
|
// ----- Read Endpoint Status -----
|
341 |
|
|
|
342 |
|
|
enum {
|
343 |
|
|
D12_ENDP_STAT_SETUP_PACKET = 0x04,
|
344 |
|
|
D12_ENDP_STAT_BUF0_FULL = 0x20,
|
345 |
|
|
D12_ENDP_STAT_BUF1_FULL = 0x40,
|
346 |
|
|
D12_ENDP_STAT_ANY_BUF_FULL = 0x60,
|
347 |
|
|
D12_ENDP_STAT_BOTH_BUF_FULL = 0x60,
|
348 |
|
|
D12_ENDP_STAT_STALL = 0x80,
|
349 |
|
|
};
|
350 |
|
|
|
351 |
|
|
// ----- Last Transaction Status Bits -----
|
352 |
|
|
|
353 |
|
|
enum {
|
354 |
|
|
D12_LAST_TRANS_DATA_SUCCESS = 0x01,
|
355 |
|
|
D12_LAST_TRANS_ERR_CODE_MASK = 0x1E,
|
356 |
|
|
D12_LAST_TRANS_SETUP_PACKET = 0x20,
|
357 |
|
|
D12_LAST_TRANS_DATA1_PACKET = 0x40,
|
358 |
|
|
D12_LAST_TRANS_PREV_STAT_NOT_READ = 0x80
|
359 |
|
|
};
|
360 |
|
|
|
361 |
|
|
static const byte RX_ENDP_INDEX[] =
|
362 |
|
|
{ D12_RX_ENDP0, D12_RX_ENDP1, D12_RX_ENDP2 };
|
363 |
|
|
static const byte TX_ENDP_INDEX[] =
|
364 |
|
|
{ D12_TX_ENDP0, D12_TX_ENDP1, D12_TX_ENDP2 };
|
365 |
|
|
|
366 |
|
|
static const int RX_ENDP_SIZE[] = { 16, 16, 64 };
|
367 |
|
|
static const int TX_ENDP_SIZE[] = { 16, 16, 64 };
|
368 |
|
|
|
369 |
|
|
typedef void (*completion_fn)(void*, int);
|
370 |
|
|
|
371 |
|
|
#ifndef USB_SETUP_PACKET_LEN
|
372 |
|
|
#define USB_SETUP_PACKET_LEN 8
|
373 |
|
|
#endif
|
374 |
|
|
|
375 |
|
|
// ----- Command Definitions -----
|
376 |
|
|
|
377 |
|
|
enum {
|
378 |
|
|
CMD_SET_ADDR_EN = 0xD0, // Write 1 byte
|
379 |
|
|
CMD_SET_ENDP_EN = 0xD8, // Write 1 byte
|
380 |
|
|
CMD_SET_MODE = 0xF3, // Write 2 bytes
|
381 |
|
|
CMD_SET_DMA = 0xFB, // Write/Read 1 byte
|
382 |
|
|
CMD_READ_INTR_REG = 0xF4, // Read 2 bytes
|
383 |
|
|
CMD_SEL_ENDP = 0x00, // (+ Endp Index) Read 1 byte (opt)
|
384 |
|
|
CMD_READ_LAST_TRANS_STAT = 0x40, // (+ Endp Index) Read 1 byte (opt)
|
385 |
|
|
CMD_READ_ENDP_STAT = 0x80, // (+ Endp Index) Read 1 byte
|
386 |
|
|
CMD_READ_BUF = 0xF0, // Read n bytes
|
387 |
|
|
CMD_WRITE_BUF = 0xF0, // Write n bytes
|
388 |
|
|
CMD_SET_ENDP_STAT = 0x40, // (+ Endp Index) Write 1 byte
|
389 |
|
|
CMD_ACK_SETUP = 0xF1, // None
|
390 |
|
|
CMD_CLEAR_BUF = 0xF2, // None
|
391 |
|
|
CMD_VALIDATE_BUF = 0xFA, // None
|
392 |
|
|
CMD_SEND_RESUME = 0xF6, // None
|
393 |
|
|
CMD_READ_CURR_FRAME_NUM = 0xF5, // Read 1 or 2 bytes
|
394 |
|
|
CMD_READ_CHIP_ID = 0xFD // Read 2 bytes
|
395 |
|
|
};
|
396 |
|
|
|
397 |
|
|
// ----- Set Endpoint Enable Register -----
|
398 |
|
|
|
399 |
|
|
enum {
|
400 |
|
|
ENDP_DISABLE,
|
401 |
|
|
ENDP_ENABLE
|
402 |
|
|
};
|
403 |
|
|
|
404 |
|
|
// ----- Select Endpoint Results -----
|
405 |
|
|
|
406 |
|
|
enum {
|
407 |
|
|
SEL_ENDP_FULL = 0x01,
|
408 |
|
|
SEL_ENDP_STALL = 0x02
|
409 |
|
|
};
|
410 |
|
|
|
411 |
|
|
// ----- Error Codes from ReadLastTrans (need to be bit shifter) -----
|
412 |
|
|
|
413 |
|
|
enum {
|
414 |
|
|
ERROR_NO_ERROR,
|
415 |
|
|
ERROR_PID_ENCODING,
|
416 |
|
|
ERROR_PID_UNKNOWN,
|
417 |
|
|
ERROR_UNEXPECTED_PACKET,
|
418 |
|
|
ERROR_TOKEN_CRC,
|
419 |
|
|
ERROR_DATA_CRC,
|
420 |
|
|
ERROR_TIMEOUT,
|
421 |
|
|
ERROR_BABBLE,
|
422 |
|
|
ERROR_UNEXPECTED_EOP,
|
423 |
|
|
ERROR_NAK,
|
424 |
|
|
ERROR_PACKET_ON_STALL,
|
425 |
|
|
ERROR_OVERFLOW,
|
426 |
|
|
ERROR_BITSTUFF,
|
427 |
|
|
ERROR_WRONG_DATA_PID
|
428 |
|
|
};
|
429 |
|
|
|
430 |
|
|
// ------------------------------------------------------------------------
|
431 |
|
|
// Routines to access the D12 registers. The hardware specific driver
|
432 |
|
|
// provides 8bit access functions and block access functions.
|
433 |
|
|
|
434 |
|
|
#include CYGIMP_DEVS_USB_D12_HW_ACCESS_HEADER
|
435 |
|
|
|
436 |
|
|
|
437 |
|
|
static inline uint16
|
438 |
|
|
make_word(byte hi, byte lo)
|
439 |
|
|
{
|
440 |
|
|
return ((uint16) hi << 8) | lo;
|
441 |
|
|
}
|
442 |
|
|
|
443 |
|
|
// These routines read or write 16 bit values to the data area.
|
444 |
|
|
|
445 |
|
|
static inline uint16
|
446 |
|
|
d12_read_data_word(d12_addr_type base_addr)
|
447 |
|
|
{
|
448 |
|
|
uint16 val = d12_read_data_byte(base_addr);
|
449 |
|
|
val |= ((uint16) d12_read_data_byte(base_addr)) << 8;
|
450 |
|
|
return val;
|
451 |
|
|
}
|
452 |
|
|
|
453 |
|
|
static inline void
|
454 |
|
|
d12_write_data_word(d12_addr_type base_addr, uint16 val)
|
455 |
|
|
{
|
456 |
|
|
d12_write_data_byte(base_addr, (byte) val);
|
457 |
|
|
d12_write_data_byte(base_addr, (byte) (val >> 8));
|
458 |
|
|
}
|
459 |
|
|
|
460 |
|
|
// ------------------------------------------------------------------------
|
461 |
|
|
// Command & Data I/O
|
462 |
|
|
// ------------------------------------------------------------------------
|
463 |
|
|
//
|
464 |
|
|
// These routines read & write the registers in the D12. The procedure is
|
465 |
|
|
// to write a register/command value to the command address (A0=1) then
|
466 |
|
|
// read or write any required data a byte at a time to the data address
|
467 |
|
|
// (A0=0). The data can be one byte or two. If two, the low byte is read/
|
468 |
|
|
// written first.
|
469 |
|
|
|
470 |
|
|
// NOTE: These MUST be atomic operations. It's up to the caller
|
471 |
|
|
// to insure this.
|
472 |
|
|
|
473 |
|
|
// The hardware specific driver provides the basic access function.
|
474 |
|
|
//
|
475 |
|
|
|
476 |
|
|
static inline void
|
477 |
|
|
d12_write_byte(d12_addr_type base_addr, byte cmd, byte val)
|
478 |
|
|
{
|
479 |
|
|
d12_write_cmd(base_addr, cmd);
|
480 |
|
|
d12_write_data_byte(base_addr, val);
|
481 |
|
|
}
|
482 |
|
|
|
483 |
|
|
static inline void
|
484 |
|
|
d12_write_word(d12_addr_type base_addr, byte cmd, uint16 val)
|
485 |
|
|
{
|
486 |
|
|
d12_write_cmd(base_addr, cmd);
|
487 |
|
|
d12_write_data_word(base_addr, val);
|
488 |
|
|
}
|
489 |
|
|
|
490 |
|
|
static inline byte
|
491 |
|
|
d12_read_byte(d12_addr_type base_addr, byte cmd)
|
492 |
|
|
{
|
493 |
|
|
d12_write_cmd(base_addr, cmd);
|
494 |
|
|
return d12_read_data_byte(base_addr);
|
495 |
|
|
}
|
496 |
|
|
|
497 |
|
|
static inline uint16
|
498 |
|
|
d12_read_word(d12_addr_type base_addr, byte cmd)
|
499 |
|
|
{
|
500 |
|
|
d12_write_cmd(base_addr, cmd);
|
501 |
|
|
return d12_read_data_word(base_addr);
|
502 |
|
|
}
|
503 |
|
|
|
504 |
|
|
// ------------------------------------------------------------------------
|
505 |
|
|
// Higher Level Commands
|
506 |
|
|
// ------------------------------------------------------------------------
|
507 |
|
|
|
508 |
|
|
// Stalls or Unstalls the endpoint. Bit0=1 for stall, =0 to unstall.
|
509 |
|
|
|
510 |
|
|
static inline void
|
511 |
|
|
d12_set_endp_status(d12_addr_type base_addr, byte endp_idx, byte stat)
|
512 |
|
|
{
|
513 |
|
|
d12_write_byte(base_addr, CMD_SET_ENDP_STAT + endp_idx, stat);
|
514 |
|
|
}
|
515 |
|
|
|
516 |
|
|
// ------------------------------------------------------------------------
|
517 |
|
|
// Stalls the control endpoint (both in & out).
|
518 |
|
|
|
519 |
|
|
static void
|
520 |
|
|
d12_stall_ctrl_endp(d12_addr_type base_addr, bool stall)
|
521 |
|
|
{
|
522 |
|
|
d12_set_endp_status(base_addr, D12_TX_CTRL_ENDP, stall ? 1 : 0);
|
523 |
|
|
d12_set_endp_status(base_addr, D12_RX_CTRL_ENDP, stall ? 1 : 0);
|
524 |
|
|
}
|
525 |
|
|
|
526 |
|
|
// ------------------------------------------------------------------------
|
527 |
|
|
// Stalls/unstalls the specified endpoint.
|
528 |
|
|
|
529 |
|
|
void inline
|
530 |
|
|
d12_stall_endp(d12_addr_type base_addr, byte endp_idx, bool stall)
|
531 |
|
|
{
|
532 |
|
|
d12_set_endp_status(base_addr, endp_idx, stall ? 1 : 0);
|
533 |
|
|
}
|
534 |
|
|
|
535 |
|
|
// ------------------------------------------------------------------------ */
|
536 |
|
|
// Tells the chip that the selected endpoint buffer has been completely
|
537 |
|
|
// read. This should be called after the application reads all the data
|
538 |
|
|
// from an endpoint. While there's data in the buffer the chip will
|
539 |
|
|
// automatically NAK any additional OUT packets from the host.
|
540 |
|
|
|
541 |
|
|
static inline void
|
542 |
|
|
d12_clear_buffer(d12_addr_type base_addr)
|
543 |
|
|
{
|
544 |
|
|
d12_write_cmd(base_addr, CMD_CLEAR_BUF);
|
545 |
|
|
}
|
546 |
|
|
|
547 |
|
|
// ------------------------------------------------------------------------
|
548 |
|
|
// Tells the chip that the data in the selected endpoint buffer is complete
|
549 |
|
|
// and ready to be sent to the host.
|
550 |
|
|
|
551 |
|
|
static inline void
|
552 |
|
|
d12_validate_buffer(d12_addr_type base_addr)
|
553 |
|
|
{
|
554 |
|
|
d12_write_cmd(base_addr, CMD_VALIDATE_BUF);
|
555 |
|
|
}
|
556 |
|
|
|
557 |
|
|
// ------------------------------------------------------------------------
|
558 |
|
|
// Sends an upstream resume signal for 10ms. This command is normally
|
559 |
|
|
// issued when the device is in suspend.
|
560 |
|
|
|
561 |
|
|
static inline void
|
562 |
|
|
d12_send_resume(d12_addr_type base_addr)
|
563 |
|
|
{
|
564 |
|
|
d12_write_cmd(base_addr, CMD_SEND_RESUME);
|
565 |
|
|
}
|
566 |
|
|
|
567 |
|
|
// ------------------------------------------------------------------------
|
568 |
|
|
// Gets the frame number of the last successfully received
|
569 |
|
|
// start-of-frame (SOF).
|
570 |
|
|
|
571 |
|
|
static inline uint16
|
572 |
|
|
d12_read_curr_frame_num(d12_addr_type base_addr)
|
573 |
|
|
{
|
574 |
|
|
return d12_read_word(base_addr, CMD_READ_CURR_FRAME_NUM);
|
575 |
|
|
}
|
576 |
|
|
|
577 |
|
|
// ------------------------------------------------------------------------
|
578 |
|
|
// This routine acknowledges a setup packet by writing an Ack Setup command
|
579 |
|
|
// to the currently selected Endpoint. This must be done for both EP0 out
|
580 |
|
|
// and EP0 IN whenever a setup packet is received.
|
581 |
|
|
|
582 |
|
|
static inline void
|
583 |
|
|
d12_ack_setup(d12_addr_type base_addr)
|
584 |
|
|
{
|
585 |
|
|
d12_write_cmd(base_addr, CMD_ACK_SETUP);
|
586 |
|
|
}
|
587 |
|
|
|
588 |
|
|
// ------------------------------------------------------------------------
|
589 |
|
|
// Gets the value of the 16-bit interrupt register, which indicates the
|
590 |
|
|
// source of an interrupt (if interrupts are not used, this reg can be
|
591 |
|
|
// polled to find when service is required).
|
592 |
|
|
|
593 |
|
|
static inline uint16
|
594 |
|
|
d12_read_intr_reg(d12_addr_type base_addr)
|
595 |
|
|
{
|
596 |
|
|
return d12_read_word(base_addr, CMD_READ_INTR_REG) & 0x01FF;
|
597 |
|
|
}
|
598 |
|
|
|
599 |
|
|
// ------------------------------------------------------------------------
|
600 |
|
|
// Gets/Sets the contents of the DMA register.
|
601 |
|
|
|
602 |
|
|
static inline byte
|
603 |
|
|
d12_get_dma(d12_addr_type base_addr)
|
604 |
|
|
{
|
605 |
|
|
return d12_read_byte(base_addr, CMD_SET_DMA);
|
606 |
|
|
}
|
607 |
|
|
|
608 |
|
|
static inline void
|
609 |
|
|
d12_set_dma(d12_addr_type base_addr, byte mode)
|
610 |
|
|
{
|
611 |
|
|
d12_write_byte(base_addr, CMD_SET_DMA, mode);
|
612 |
|
|
}
|
613 |
|
|
|
614 |
|
|
// ------------------------------------------------------------------------
|
615 |
|
|
// Sends the "Select Endpoint" command (0x00 - 0x0D) to the chip.
|
616 |
|
|
// This command initializes an internal pointer to the start of the
|
617 |
|
|
// selected buffer.
|
618 |
|
|
//
|
619 |
|
|
// Returns: Bitfield containing status of the endpoint
|
620 |
|
|
|
621 |
|
|
static byte
|
622 |
|
|
d12_select_endp(d12_addr_type base_addr, byte endp_idx)
|
623 |
|
|
{
|
624 |
|
|
return d12_read_byte(base_addr, CMD_SEL_ENDP + endp_idx);
|
625 |
|
|
}
|
626 |
|
|
|
627 |
|
|
// ------------------------------------------------------------------------
|
628 |
|
|
// Gets the status of the last transaction of the endpoint. It also resets
|
629 |
|
|
// the corresponding interrupt flag in the interrupt register, and clears
|
630 |
|
|
// the status, indicating that it was read.
|
631 |
|
|
//
|
632 |
|
|
// Returns: Bitfield containing the last transaction status.
|
633 |
|
|
|
634 |
|
|
static inline byte
|
635 |
|
|
d12_read_last_trans_status(d12_addr_type base_addr, byte endp_idx)
|
636 |
|
|
{
|
637 |
|
|
return d12_read_byte(base_addr, CMD_READ_LAST_TRANS_STAT + endp_idx);
|
638 |
|
|
}
|
639 |
|
|
|
640 |
|
|
// ------------------------------------------------------------------------
|
641 |
|
|
// Reads the status of the requested endpoint.
|
642 |
|
|
// Just for the heck of it, we mask off the reserved bits.
|
643 |
|
|
//
|
644 |
|
|
// Returns: Bitfield containing the endpoint status.
|
645 |
|
|
|
646 |
|
|
static inline byte
|
647 |
|
|
d12_read_endp_status(d12_addr_type base_addr, byte endp_idx)
|
648 |
|
|
{
|
649 |
|
|
return d12_read_byte(base_addr, CMD_READ_ENDP_STAT + endp_idx) & 0xE4;
|
650 |
|
|
}
|
651 |
|
|
|
652 |
|
|
// ------------------------------------------------------------------------
|
653 |
|
|
// Returns true if there is data available in the specified endpoint's
|
654 |
|
|
// ram buffer. This is determined by the buf full flags in the endp status
|
655 |
|
|
// register.
|
656 |
|
|
|
657 |
|
|
static inline bool
|
658 |
|
|
d12_data_available(d12_addr_type base_addr, byte endp_idx)
|
659 |
|
|
{
|
660 |
|
|
byte by = d12_read_endp_status(base_addr, endp_idx);
|
661 |
|
|
return (bool) (by & D12_ENDP_STAT_ANY_BUF_FULL);
|
662 |
|
|
}
|
663 |
|
|
|
664 |
|
|
// ------------------------------------------------------------------------
|
665 |
|
|
// Clears the transaction status for each of the endpoints by calling the
|
666 |
|
|
// d12_read_last_trans_status() function for each.
|
667 |
|
|
|
668 |
|
|
static void
|
669 |
|
|
d12_clear_all_intr(d12_addr_type base_addr)
|
670 |
|
|
{
|
671 |
|
|
uint8 endp;
|
672 |
|
|
|
673 |
|
|
d12_read_intr_reg(base_addr);
|
674 |
|
|
|
675 |
|
|
for (endp=D12_ENDP_MIN; endp<=D12_ENDP_MAX; ++endp)
|
676 |
|
|
d12_read_last_trans_status(base_addr, endp);
|
677 |
|
|
}
|
678 |
|
|
|
679 |
|
|
// ------------------------------------------------------------------------
|
680 |
|
|
// Loads a value into the Set Address / Enable register. This sets the
|
681 |
|
|
// device's USB address (lower 7 bits) and enables/disables the function
|
682 |
|
|
// (msb).
|
683 |
|
|
|
684 |
|
|
static void
|
685 |
|
|
d12_set_addr_enable(d12_addr_type base_addr, byte usb_addr, bool enable)
|
686 |
|
|
{
|
687 |
|
|
if (enable)
|
688 |
|
|
usb_addr |= 0x80;
|
689 |
|
|
|
690 |
|
|
d12_write_byte(base_addr, CMD_SET_ADDR_EN, usb_addr);
|
691 |
|
|
}
|
692 |
|
|
|
693 |
|
|
// ------------------------------------------------------------------------
|
694 |
|
|
// Enables/disables the generic endpoints.
|
695 |
|
|
|
696 |
|
|
static inline void
|
697 |
|
|
d12_set_endp_enable(d12_addr_type base_addr, bool enable)
|
698 |
|
|
{
|
699 |
|
|
d12_write_byte(base_addr, CMD_SET_ENDP_EN,
|
700 |
|
|
(enable) ? ENDP_ENABLE : ENDP_DISABLE);
|
701 |
|
|
}
|
702 |
|
|
|
703 |
|
|
// ------------------------------------------------------------------------
|
704 |
|
|
// Sets the device's configuration and CLKOUT frequency.
|
705 |
|
|
|
706 |
|
|
static void
|
707 |
|
|
d12_set_mode(d12_addr_type base_addr, byte config, byte clk_div)
|
708 |
|
|
{
|
709 |
|
|
uint16 w = make_word(clk_div, config);
|
710 |
|
|
d12_write_word(base_addr, CMD_SET_MODE, w);
|
711 |
|
|
}
|
712 |
|
|
|
713 |
|
|
// ------------------------------------------------------------------------
|
714 |
|
|
// Reads a setup packet from the control endpoint. This procedure is
|
715 |
|
|
// somewhat different than reading a data packet. By the USB standard, a
|
716 |
|
|
// setup packet can not be NAK'ed or STALL'ed, so when the chip receives a
|
717 |
|
|
// setup packet, it flushes the Ctrl (EP0) IN buffer and disables the
|
718 |
|
|
// Validate and Clear Buffer commands. The processor must send an
|
719 |
|
|
// acknowledge setup to both EP0 IN and OUT before a Validate or Clear
|
720 |
|
|
// Buffer command is effective.
|
721 |
|
|
//
|
722 |
|
|
// Parameters:
|
723 |
|
|
// buf buffer to receive the contents of the setup packet. Must
|
724 |
|
|
// be at least 8 bytes.
|
725 |
|
|
// Returns:
|
726 |
|
|
// true if there are 8 bytes waiting in the EP0 OUT RAM buffer
|
727 |
|
|
// on the D12 (i.e., true if successful)
|
728 |
|
|
// false otherwise
|
729 |
|
|
|
730 |
|
|
static bool
|
731 |
|
|
d12_read_setup_packet(d12_addr_type base_addr, byte *buf)
|
732 |
|
|
{
|
733 |
|
|
uint8 n;
|
734 |
|
|
|
735 |
|
|
d12_select_endp(base_addr, D12_RX_CTRL_ENDP);
|
736 |
|
|
|
737 |
|
|
d12_read_byte(base_addr, CMD_READ_BUF); // Read & discard reserved byte
|
738 |
|
|
n = d12_read_data_byte(base_addr); // # bytes available
|
739 |
|
|
|
740 |
|
|
if (n > USB_SETUP_PACKET_LEN) {
|
741 |
|
|
//TRACE("* Warning: Setup Packet too large: %u *\n", (unsigned) n);
|
742 |
|
|
n = USB_SETUP_PACKET_LEN;
|
743 |
|
|
}
|
744 |
|
|
|
745 |
|
|
n = d12_read_data(base_addr, buf, n);
|
746 |
|
|
|
747 |
|
|
d12_ack_setup(base_addr);
|
748 |
|
|
d12_clear_buffer(base_addr);
|
749 |
|
|
|
750 |
|
|
// ----- Ack Setup to EP0 IN ------
|
751 |
|
|
|
752 |
|
|
d12_select_endp(base_addr, D12_TX_CTRL_ENDP);
|
753 |
|
|
d12_ack_setup(base_addr);
|
754 |
|
|
|
755 |
|
|
return n == USB_SETUP_PACKET_LEN;
|
756 |
|
|
}
|
757 |
|
|
|
758 |
|
|
// ------------------------------------------------------------------------
|
759 |
|
|
// Reads the contents of the currently selected endpoint's RAM buffer into
|
760 |
|
|
// the buf[] array.
|
761 |
|
|
//
|
762 |
|
|
// The D12's buffer comes in as follows:
|
763 |
|
|
// [0] junk ("reserved" - can be anything). Just disregard
|
764 |
|
|
// [1] # data bytes to follow
|
765 |
|
|
// [2] data byte 0, ...
|
766 |
|
|
// up to
|
767 |
|
|
// [N+2] data byte N-1
|
768 |
|
|
//
|
769 |
|
|
// Parameters:
|
770 |
|
|
// buf byte array to receive data. This MUST be at least the size
|
771 |
|
|
// of the chip's RAM buffer for the currently selected endpoint.
|
772 |
|
|
// If buf is NULL, the data is read & discarded.
|
773 |
|
|
//
|
774 |
|
|
// Returns: the actual number of bytes read (could be <= n)
|
775 |
|
|
|
776 |
|
|
static uint8
|
777 |
|
|
d12_read_selected_endp_buf(d12_addr_type base_addr, byte *buf)
|
778 |
|
|
{
|
779 |
|
|
uint8 n;
|
780 |
|
|
|
781 |
|
|
d12_read_byte(base_addr, CMD_READ_BUF); // Read & discard reserved byte
|
782 |
|
|
n = d12_read_data_byte(base_addr); // # bytes in chip's buf
|
783 |
|
|
d12_read_data(base_addr, buf, n);
|
784 |
|
|
d12_clear_buffer(base_addr);
|
785 |
|
|
|
786 |
|
|
return n;
|
787 |
|
|
}
|
788 |
|
|
|
789 |
|
|
// ------------------------------------------------------------------------
|
790 |
|
|
// Selects the specified endpoint and reads the contents of it's RAM buffer
|
791 |
|
|
// into the buf[] array. For the Main OUT endpoint, it will check whether
|
792 |
|
|
// both buffers are full, and if so, read them both.
|
793 |
|
|
//
|
794 |
|
|
// Side Effects:
|
795 |
|
|
// - Leaves endp_idx as the currently selected endpoint.
|
796 |
|
|
//
|
797 |
|
|
// Parameters:
|
798 |
|
|
// endp_idx the endpoint from which to read
|
799 |
|
|
// buf buffer to receive the data. This MUST be at least the size
|
800 |
|
|
// of the chip's RAM buffer for the specified endpoint.
|
801 |
|
|
// For the Main endp, it must be 2x the buffer size (128 total)
|
802 |
|
|
//
|
803 |
|
|
// Returns: the # of bytes read.
|
804 |
|
|
|
805 |
|
|
static uint8
|
806 |
|
|
d12_read_endp_buf(d12_addr_type base_addr, byte endp_idx, byte *buf)
|
807 |
|
|
{
|
808 |
|
|
return (d12_select_endp(base_addr, endp_idx) & SEL_ENDP_FULL)
|
809 |
|
|
? d12_read_selected_endp_buf(base_addr, buf) : 0;
|
810 |
|
|
}
|
811 |
|
|
|
812 |
|
|
// ------------------------------------------------------------------------
|
813 |
|
|
// Does a read of the "main" endpoint (#2). Since it's double buffered,
|
814 |
|
|
// this will check if both buffers are full, and if so it will read them
|
815 |
|
|
// both. Thus the caller's buffer, buf, must be large enough to hold all
|
816 |
|
|
// the data - 128 bytes total.
|
817 |
|
|
//
|
818 |
|
|
// If either buffer contains less than the full amount, the done flag
|
819 |
|
|
// is set indicating that a Bulk OUT transfer is complete.
|
820 |
|
|
//
|
821 |
|
|
// This determines if a bulk transfer is done, since the caller can't
|
822 |
|
|
// necessarily determine this from the size of the return buffer.
|
823 |
|
|
// If either buffer is less than full, '*done' is set to a non-zero value.
|
824 |
|
|
|
825 |
|
|
static uint8
|
826 |
|
|
d12_read_main_endp_buf(d12_addr_type base_addr, byte *buf, int *done)
|
827 |
|
|
{
|
828 |
|
|
int nBuf = 1;
|
829 |
|
|
uint8 n = 0;
|
830 |
|
|
byte stat = d12_read_endp_status(base_addr, D12_RX_MAIN_ENDP) &
|
831 |
|
|
D12_ENDP_STAT_ANY_BUF_FULL;
|
832 |
|
|
|
833 |
|
|
if (stat == 0)
|
834 |
|
|
return 0;
|
835 |
|
|
|
836 |
|
|
if (stat == D12_ENDP_STAT_BOTH_BUF_FULL)
|
837 |
|
|
nBuf++;
|
838 |
|
|
|
839 |
|
|
*done = false;
|
840 |
|
|
|
841 |
|
|
while (nBuf--) {
|
842 |
|
|
if (d12_select_endp(base_addr, D12_RX_MAIN_ENDP) & SEL_ENDP_FULL) {
|
843 |
|
|
uint8 n1 = d12_read_selected_endp_buf(base_addr, buf+n);
|
844 |
|
|
n += n1;
|
845 |
|
|
if (n1 < D12_MAIN_ENDP_SIZE) {
|
846 |
|
|
*done = true;
|
847 |
|
|
break;
|
848 |
|
|
}
|
849 |
|
|
}
|
850 |
|
|
else
|
851 |
|
|
*done = true;
|
852 |
|
|
}
|
853 |
|
|
return n;
|
854 |
|
|
}
|
855 |
|
|
|
856 |
|
|
// ------------------------------------------------------------------------
|
857 |
|
|
// Writes the contents of the buf[] array to the currently selected
|
858 |
|
|
// endpoint's RAM buffer. The host will get the data on the on the next IN
|
859 |
|
|
// packet from the endpoint.
|
860 |
|
|
//
|
861 |
|
|
// Note:
|
862 |
|
|
// - The length of the buffer, n, must be no more than the size of the
|
863 |
|
|
// endpoint's RAM space, though, currently, this is not checked.
|
864 |
|
|
// - It's feasible that the application needs to send an empty (NULL)
|
865 |
|
|
// packet. It's valid for 'n' to be zero, and/or buf NULL.
|
866 |
|
|
|
867 |
|
|
static uint8
|
868 |
|
|
d12_write_selected_endp_buf(d12_addr_type base_addr, const byte *buf, uint8 n)
|
869 |
|
|
{
|
870 |
|
|
d12_write_byte(base_addr, CMD_WRITE_BUF, 0);
|
871 |
|
|
d12_write_data_byte(base_addr, n);
|
872 |
|
|
d12_write_data(base_addr, buf, n);
|
873 |
|
|
d12_validate_buffer(base_addr);
|
874 |
|
|
|
875 |
|
|
return n;
|
876 |
|
|
}
|
877 |
|
|
|
878 |
|
|
// ------------------------------------------------------------------------
|
879 |
|
|
// Writes the contents of the buf[] array to the specified endoint's RAM
|
880 |
|
|
// buffer. The host will get this data on the next IN packet from the
|
881 |
|
|
// endpoint.
|
882 |
|
|
//
|
883 |
|
|
// Side Effects:
|
884 |
|
|
// - Leaves endp_idx as the currently selected endpoint.
|
885 |
|
|
|
886 |
|
|
static uint8
|
887 |
|
|
d12_write_endp_buf(d12_addr_type base_addr, byte endp_idx,
|
888 |
|
|
const byte *buf, uint8 n)
|
889 |
|
|
{
|
890 |
|
|
d12_select_endp(base_addr, endp_idx);
|
891 |
|
|
return d12_write_selected_endp_buf(base_addr, buf, n);
|
892 |
|
|
}
|
893 |
|
|
|
894 |
|
|
// ------------------------------------------------------------------------
|
895 |
|
|
// Reads & returns the contents of the Chip ID register.
|
896 |
|
|
|
897 |
|
|
static inline uint16
|
898 |
|
|
d12_read_chip_id(d12_addr_type base_addr)
|
899 |
|
|
{
|
900 |
|
|
return d12_read_word(base_addr, CMD_READ_CHIP_ID);
|
901 |
|
|
}
|
902 |
|
|
|
903 |
|
|
|
904 |
|
|
// ==========================================================================
|
905 |
|
|
// eCos-Specific Device Driver Code
|
906 |
|
|
// ==========================================================================
|
907 |
|
|
|
908 |
|
|
static void usbs_d12_reset(void);
|
909 |
|
|
|
910 |
|
|
// Make some abbreviations for the configuration options.
|
911 |
|
|
|
912 |
|
|
#if defined(CYGPKG_DEVS_USB_D12_RX_EP1)
|
913 |
|
|
#define _RX_EP1
|
914 |
|
|
#endif
|
915 |
|
|
|
916 |
|
|
#if defined(CYGPKG_DEVS_USB_D12_TX_EP1)
|
917 |
|
|
#define _TX_EP1
|
918 |
|
|
#endif
|
919 |
|
|
|
920 |
|
|
#if defined(CYGPKG_DEVS_USB_D12_RX_EP2)
|
921 |
|
|
#define _RX_EP2
|
922 |
|
|
#endif
|
923 |
|
|
|
924 |
|
|
#if defined(CYGPKG_DEVS_USB_D12_TX_EP2)
|
925 |
|
|
#define _TX_EP2
|
926 |
|
|
#endif
|
927 |
|
|
|
928 |
|
|
// --------------------------------------------------------------------------
|
929 |
|
|
// Endpoint 0 Data
|
930 |
|
|
// --------------------------------------------------------------------------
|
931 |
|
|
|
932 |
|
|
static cyg_interrupt usbs_d12_intr_data;
|
933 |
|
|
static cyg_handle_t usbs_d12_intr_handle;
|
934 |
|
|
|
935 |
|
|
static byte ep0_tx_buffer[CYGNUM_DEVS_USB_D12_EP0_TXBUFSIZE];
|
936 |
|
|
|
937 |
|
|
static void usbs_d12_start(usbs_control_endpoint*);
|
938 |
|
|
static void usbs_d12_poll(usbs_control_endpoint*);
|
939 |
|
|
|
940 |
|
|
typedef enum endp_state {
|
941 |
|
|
ENDP_STATE_IDLE,
|
942 |
|
|
ENDP_STATE_IN,
|
943 |
|
|
ENDP_STATE_OUT
|
944 |
|
|
} endp_state;
|
945 |
|
|
|
946 |
|
|
typedef struct ep0_impl {
|
947 |
|
|
usbs_control_endpoint common;
|
948 |
|
|
endp_state ep_state;
|
949 |
|
|
int length;
|
950 |
|
|
int transmitted;
|
951 |
|
|
bool tx_empty;
|
952 |
|
|
} ep0_impl;
|
953 |
|
|
|
954 |
|
|
static ep0_impl ep0 = {
|
955 |
|
|
common:
|
956 |
|
|
{
|
957 |
|
|
state: USBS_STATE_POWERED,
|
958 |
|
|
enumeration_data: (usbs_enumeration_data*) 0,
|
959 |
|
|
start_fn: &usbs_d12_start,
|
960 |
|
|
poll_fn: &usbs_d12_poll,
|
961 |
|
|
interrupt_vector: CYGNUM_DEVS_USB_D12_IRQ,
|
962 |
|
|
control_buffer: { 0, 0, 0, 0, 0, 0, 0, 0 },
|
963 |
|
|
state_change_fn: 0,
|
964 |
|
|
state_change_data: 0,
|
965 |
|
|
standard_control_fn: 0,
|
966 |
|
|
standard_control_data: 0,
|
967 |
|
|
class_control_fn: 0,
|
968 |
|
|
class_control_data: 0,
|
969 |
|
|
vendor_control_fn: 0,
|
970 |
|
|
vendor_control_data: 0,
|
971 |
|
|
reserved_control_fn: 0,
|
972 |
|
|
reserved_control_data: 0,
|
973 |
|
|
buffer: 0,
|
974 |
|
|
buffer_size: 0,
|
975 |
|
|
fill_buffer_fn: 0,
|
976 |
|
|
fill_data: 0,
|
977 |
|
|
fill_index: 0,
|
978 |
|
|
complete_fn: 0
|
979 |
|
|
},
|
980 |
|
|
ep_state: ENDP_STATE_IDLE,
|
981 |
|
|
length: 0,
|
982 |
|
|
transmitted: 0,
|
983 |
|
|
tx_empty: 0
|
984 |
|
|
};
|
985 |
|
|
|
986 |
|
|
extern usbs_control_endpoint usbs_d12_ep0 __attribute__((alias ("ep0")));
|
987 |
|
|
|
988 |
|
|
// --------------------------------------------------------------------------
|
989 |
|
|
// Rx Endpoints 1 & 2 Data
|
990 |
|
|
// --------------------------------------------------------------------------
|
991 |
|
|
|
992 |
|
|
#if defined(_RX_EP1) || defined(_RX_EP2)
|
993 |
|
|
|
994 |
|
|
typedef struct rx_endpoint {
|
995 |
|
|
usbs_rx_endpoint common;
|
996 |
|
|
int endp, received;
|
997 |
|
|
} rx_endpoint;
|
998 |
|
|
|
999 |
|
|
static void usbs_d12_api_start_rx_ep(usbs_rx_endpoint*);
|
1000 |
|
|
static void usbs_d12_api_stall_rx_ep(usbs_rx_endpoint*, cyg_bool);
|
1001 |
|
|
|
1002 |
|
|
static void usbs_d12_ep_rx_complete(rx_endpoint *ep, int result);
|
1003 |
|
|
static void usbs_d12_stall_rx_ep(rx_endpoint*, cyg_bool);
|
1004 |
|
|
|
1005 |
|
|
#endif
|
1006 |
|
|
|
1007 |
|
|
|
1008 |
|
|
#if defined(_RX_EP1)
|
1009 |
|
|
|
1010 |
|
|
static rx_endpoint rx_ep1 = {
|
1011 |
|
|
common: {
|
1012 |
|
|
start_rx_fn: &usbs_d12_api_start_rx_ep,
|
1013 |
|
|
set_halted_fn: &usbs_d12_api_stall_rx_ep,
|
1014 |
|
|
halted: 0
|
1015 |
|
|
},
|
1016 |
|
|
endp: 1
|
1017 |
|
|
};
|
1018 |
|
|
|
1019 |
|
|
extern usbs_rx_endpoint usbs_d12_rx_ep1 __attribute__((alias ("rx_ep1")));
|
1020 |
|
|
|
1021 |
|
|
#endif
|
1022 |
|
|
|
1023 |
|
|
|
1024 |
|
|
#if defined(_RX_EP2)
|
1025 |
|
|
|
1026 |
|
|
static rx_endpoint rx_ep2 = {
|
1027 |
|
|
common: {
|
1028 |
|
|
start_rx_fn: &usbs_d12_api_start_rx_ep,
|
1029 |
|
|
set_halted_fn: &usbs_d12_api_stall_rx_ep,
|
1030 |
|
|
halted: 0
|
1031 |
|
|
},
|
1032 |
|
|
endp: 2
|
1033 |
|
|
};
|
1034 |
|
|
|
1035 |
|
|
extern usbs_rx_endpoint usbs_d12_rx_ep2 __attribute__((alias ("rx_ep2")));
|
1036 |
|
|
|
1037 |
|
|
#endif
|
1038 |
|
|
|
1039 |
|
|
// --------------------------------------------------------------------------
|
1040 |
|
|
// Tx Endpoints 1 & 2 Data
|
1041 |
|
|
// --------------------------------------------------------------------------
|
1042 |
|
|
|
1043 |
|
|
#if defined(_TX_EP1) || defined(_TX_EP2)
|
1044 |
|
|
|
1045 |
|
|
typedef struct tx_endpoint {
|
1046 |
|
|
usbs_tx_endpoint common;
|
1047 |
|
|
int endp, transmitted;
|
1048 |
|
|
bool tx_empty;
|
1049 |
|
|
} tx_endpoint;
|
1050 |
|
|
|
1051 |
|
|
static void usbs_d12_api_start_tx_ep(usbs_tx_endpoint*);
|
1052 |
|
|
static void usbs_d12_api_stall_tx_ep(usbs_tx_endpoint*, cyg_bool);
|
1053 |
|
|
|
1054 |
|
|
static void usbs_d12_ep_tx_complete(tx_endpoint *ep, int result);
|
1055 |
|
|
static void usbs_d12_stall_tx_ep(tx_endpoint*, cyg_bool);
|
1056 |
|
|
#endif
|
1057 |
|
|
|
1058 |
|
|
#if defined(_TX_EP1)
|
1059 |
|
|
|
1060 |
|
|
static tx_endpoint tx_ep1 = {
|
1061 |
|
|
common: {
|
1062 |
|
|
start_tx_fn: &usbs_d12_api_start_tx_ep,
|
1063 |
|
|
set_halted_fn: &usbs_d12_api_stall_tx_ep,
|
1064 |
|
|
halted: 0
|
1065 |
|
|
},
|
1066 |
|
|
endp: 1
|
1067 |
|
|
};
|
1068 |
|
|
|
1069 |
|
|
extern usbs_tx_endpoint usbs_d12_tx_ep1 __attribute__((alias ("tx_ep1")));
|
1070 |
|
|
#endif
|
1071 |
|
|
|
1072 |
|
|
#if defined(_TX_EP2)
|
1073 |
|
|
|
1074 |
|
|
static tx_endpoint tx_ep2 = {
|
1075 |
|
|
common: {
|
1076 |
|
|
start_tx_fn: &usbs_d12_api_start_tx_ep,
|
1077 |
|
|
set_halted_fn: &usbs_d12_api_stall_tx_ep,
|
1078 |
|
|
halted: 0
|
1079 |
|
|
},
|
1080 |
|
|
endp: 2
|
1081 |
|
|
};
|
1082 |
|
|
|
1083 |
|
|
extern usbs_tx_endpoint usbs_d12_tx_ep2 __attribute__((alias ("tx_ep2")));
|
1084 |
|
|
|
1085 |
|
|
#endif
|
1086 |
|
|
|
1087 |
|
|
// --------------------------------------------------------------------------
|
1088 |
|
|
// Synchronization
|
1089 |
|
|
|
1090 |
|
|
static inline void usbs_d12_lock(void) { cyg_scheduler_lock(); }
|
1091 |
|
|
static inline void usbs_d12_unlock(void) { cyg_scheduler_unlock(); }
|
1092 |
|
|
|
1093 |
|
|
// --------------------------------------------------------------------------
|
1094 |
|
|
// Control Endpoint
|
1095 |
|
|
// --------------------------------------------------------------------------
|
1096 |
|
|
|
1097 |
|
|
// Fills the EP0 transmit buffer with a packet. Partial data packets are
|
1098 |
|
|
// retrieved by repeatedly calling the fill function.
|
1099 |
|
|
|
1100 |
|
|
static int
|
1101 |
|
|
ep0_fill_tx_buffer(void)
|
1102 |
|
|
{
|
1103 |
|
|
int nFilled = 0;
|
1104 |
|
|
|
1105 |
|
|
while (nFilled < CYGNUM_DEVS_USB_D12_EP0_TXBUFSIZE) {
|
1106 |
|
|
if (ep0.common.buffer_size != 0) {
|
1107 |
|
|
if ((nFilled + ep0.common.buffer_size) <
|
1108 |
|
|
CYGNUM_DEVS_USB_D12_EP0_TXBUFSIZE) {
|
1109 |
|
|
memcpy(&ep0_tx_buffer[nFilled], ep0.common.buffer,
|
1110 |
|
|
ep0.common.buffer_size);
|
1111 |
|
|
nFilled += ep0.common.buffer_size;
|
1112 |
|
|
ep0.common.buffer_size = 0;
|
1113 |
|
|
}
|
1114 |
|
|
else {
|
1115 |
|
|
break;
|
1116 |
|
|
}
|
1117 |
|
|
}
|
1118 |
|
|
else if (ep0.common.fill_buffer_fn) {
|
1119 |
|
|
(*ep0.common.fill_buffer_fn)(&ep0.common);
|
1120 |
|
|
}
|
1121 |
|
|
else {
|
1122 |
|
|
break;
|
1123 |
|
|
}
|
1124 |
|
|
}
|
1125 |
|
|
CYG_ASSERT((ep0.common.buffer_size == 0) && (!ep0.common.fill_buffer_fn),
|
1126 |
|
|
"EP0 transmit buffer overflow");
|
1127 |
|
|
TRACE_D12("EP0: Filled Tx Buf with %d bytes\n", nFilled);
|
1128 |
|
|
|
1129 |
|
|
ep0.length = nFilled;
|
1130 |
|
|
|
1131 |
|
|
ep0.common.fill_buffer_fn = 0;
|
1132 |
|
|
ep0.common.fill_data = 0;
|
1133 |
|
|
ep0.common.fill_index = 0;
|
1134 |
|
|
|
1135 |
|
|
return nFilled;
|
1136 |
|
|
}
|
1137 |
|
|
|
1138 |
|
|
// --------------------------------------------------------------------------
|
1139 |
|
|
// Called when a transfer is complete on the control endpoint EP0.
|
1140 |
|
|
// It resets the endpoint's data structure and calls the completion function,
|
1141 |
|
|
// if any.
|
1142 |
|
|
//
|
1143 |
|
|
// PARAMETERS:
|
1144 |
|
|
// result 0, on success
|
1145 |
|
|
// -EPIPE or -EIO to indicate a cancellation
|
1146 |
|
|
|
1147 |
|
|
static usbs_control_return
|
1148 |
|
|
usbs_d12_ep0_complete(int result)
|
1149 |
|
|
{
|
1150 |
|
|
usbs_control_return ret = USBS_CONTROL_RETURN_UNKNOWN;
|
1151 |
|
|
|
1152 |
|
|
ep0.ep_state = ENDP_STATE_IDLE;
|
1153 |
|
|
|
1154 |
|
|
if (ep0.common.complete_fn)
|
1155 |
|
|
ret = (*ep0.common.complete_fn)(&ep0.common, result);
|
1156 |
|
|
|
1157 |
|
|
ep0.common.buffer = 0;
|
1158 |
|
|
ep0.common.buffer_size = 0;
|
1159 |
|
|
ep0.common.complete_fn = 0;
|
1160 |
|
|
//ep0.common.fill_buffer_fn = 0;
|
1161 |
|
|
|
1162 |
|
|
return ret;
|
1163 |
|
|
}
|
1164 |
|
|
|
1165 |
|
|
// --------------------------------------------------------------------------
|
1166 |
|
|
// This routine is called when we want to send the next packet to the tx ep0
|
1167 |
|
|
// on the chip. It is used to start a new transfer, and is also called when
|
1168 |
|
|
// the chip interrupts to indicate that the ep0 tx buffer is empty and ready
|
1169 |
|
|
// to receive a new packet.
|
1170 |
|
|
//
|
1171 |
|
|
// NOTE:
|
1172 |
|
|
// On the D12, when you send a zero-length packet to a tx endpoint, the
|
1173 |
|
|
// chip transmits the empty packet to the host, but doesn't interrupt
|
1174 |
|
|
// indicating that it is complete. So immediately after sending the
|
1175 |
|
|
// empty packet we complete the transfer.
|
1176 |
|
|
|
1177 |
|
|
static void
|
1178 |
|
|
usbs_d12_ep0_tx(void)
|
1179 |
|
|
{
|
1180 |
|
|
int nRemaining = ep0.length - ep0.transmitted;
|
1181 |
|
|
uint8 n;
|
1182 |
|
|
|
1183 |
|
|
// ----- Intermittent interrupt? Get out -----
|
1184 |
|
|
|
1185 |
|
|
if (!ep0.common.buffer) {
|
1186 |
|
|
TRACE_D12("EP0: Tx no buffer (%d)\n", nRemaining);
|
1187 |
|
|
return;
|
1188 |
|
|
}
|
1189 |
|
|
|
1190 |
|
|
// ----- If prev packet was last, signal that we're done -----
|
1191 |
|
|
|
1192 |
|
|
if (nRemaining == 0 && !ep0.tx_empty) {
|
1193 |
|
|
TRACE_D12("\tEP0: Tx Complete (%d) %p\n", ep0.transmitted,
|
1194 |
|
|
ep0.common.complete_fn);
|
1195 |
|
|
usbs_d12_ep0_complete(0);
|
1196 |
|
|
return;
|
1197 |
|
|
}
|
1198 |
|
|
|
1199 |
|
|
// ----- Load the next tx packet onto the chip -----
|
1200 |
|
|
|
1201 |
|
|
if (nRemaining < D12_ENDP0_SIZE) {
|
1202 |
|
|
n = (uint8) nRemaining;
|
1203 |
|
|
ep0.tx_empty = false;
|
1204 |
|
|
}
|
1205 |
|
|
else
|
1206 |
|
|
n = D12_ENDP0_SIZE;
|
1207 |
|
|
|
1208 |
|
|
d12_write_endp_buf(D12_BASE_ADDR, D12_TX_ENDP0,
|
1209 |
|
|
&ep0_tx_buffer[ep0.transmitted], n);
|
1210 |
|
|
|
1211 |
|
|
TRACE_D12("EP0: Wrote %u bytes\n", (unsigned) n);
|
1212 |
|
|
TRACE_BUF0("\t", &ep0_tx_buffer[ep0.transmitted], n);
|
1213 |
|
|
|
1214 |
|
|
ep0.transmitted += n;
|
1215 |
|
|
|
1216 |
|
|
// ----- If empty packet, D12 won't interrupt, so end now ----- */
|
1217 |
|
|
|
1218 |
|
|
if (n == 0) {
|
1219 |
|
|
TRACE_D12("\tEP0: Tx Complete (%d) %p\n", ep0.transmitted,
|
1220 |
|
|
ep0.common.complete_fn);
|
1221 |
|
|
usbs_d12_ep0_complete(0);
|
1222 |
|
|
}
|
1223 |
|
|
}
|
1224 |
|
|
|
1225 |
|
|
// --------------------------------------------------------------------------
|
1226 |
|
|
// This function is called when a packet has been successfully sent on the
|
1227 |
|
|
// primary control endpoint (ep0). It indicates that the chip is ready for
|
1228 |
|
|
// another packet. We read the LastTransStatus for the endpoint to clear
|
1229 |
|
|
// the interrupt bit, then call ep0_tx() to continue the transfer.
|
1230 |
|
|
|
1231 |
|
|
static void
|
1232 |
|
|
usbs_d12_ep0_tx_intr(void)
|
1233 |
|
|
{
|
1234 |
|
|
d12_read_last_trans_status(D12_BASE_ADDR, D12_TX_ENDP0);
|
1235 |
|
|
usbs_d12_ep0_tx();
|
1236 |
|
|
}
|
1237 |
|
|
|
1238 |
|
|
// --------------------------------------------------------------------------
|
1239 |
|
|
// Try to handle standard requests. This is a three step process:
|
1240 |
|
|
// 1. If it's something we should handle internally we take care of it.
|
1241 |
|
|
// Currently we can handle SET_ADDRESS requests, and a few others.
|
1242 |
|
|
// 2. If the upper level code has installed a standard control handler
|
1243 |
|
|
// we let that function have a crack at it.
|
1244 |
|
|
// 3. If neither of those handle the packet we let
|
1245 |
|
|
// usbs_handle_standard_control() have a last try at it.
|
1246 |
|
|
//
|
1247 |
|
|
// Locally:
|
1248 |
|
|
// SET_ADDRESS: The host is demanding that we change our USB address.
|
1249 |
|
|
// This is done by updating the Address/Enable register on the D12.
|
1250 |
|
|
// Note, however that the USB protocol requires us to ack at the old
|
1251 |
|
|
// address, change address, and then accept the next control message
|
1252 |
|
|
// at the new address. The D12 address reg is buffered to do this
|
1253 |
|
|
// automatically for us. The updated address on the chip won't take
|
1254 |
|
|
// affect until after the empty ack is sent. Nice.
|
1255 |
|
|
//
|
1256 |
|
|
|
1257 |
|
|
static usbs_control_return
|
1258 |
|
|
usbs_d12_handle_std_req(usb_devreq *req)
|
1259 |
|
|
{
|
1260 |
|
|
usbs_control_return result = USBS_CONTROL_RETURN_UNKNOWN;
|
1261 |
|
|
int recipient = req->type & USB_DEVREQ_RECIPIENT_MASK;
|
1262 |
|
|
|
1263 |
|
|
if (req->request == USB_DEVREQ_SET_ADDRESS) {
|
1264 |
|
|
TRACE_D12("Setting Addr: %u\n", (unsigned) req->value_lo);
|
1265 |
|
|
d12_set_addr_enable(D12_BASE_ADDR, req->value_lo, true);
|
1266 |
|
|
result = USBS_CONTROL_RETURN_HANDLED;
|
1267 |
|
|
}
|
1268 |
|
|
else if (req->request == USB_DEVREQ_GET_STATUS) {
|
1269 |
|
|
if (recipient == USB_DEVREQ_RECIPIENT_DEVICE) {
|
1270 |
|
|
const usbs_enumeration_data *enum_data = ep0.common.enumeration_data;
|
1271 |
|
|
if (enum_data && enum_data->device.number_configurations == 1 &&
|
1272 |
|
|
enum_data->configurations) {
|
1273 |
|
|
ep0.common.control_buffer[0] =
|
1274 |
|
|
(enum_data->configurations[0].attributes
|
1275 |
|
|
& USB_CONFIGURATION_DESCRIPTOR_ATTR_SELF_POWERED) ? 1 : 0;
|
1276 |
|
|
ep0.common.control_buffer[0] |=
|
1277 |
|
|
(enum_data->configurations[0].attributes
|
1278 |
|
|
& USB_CONFIGURATION_DESCRIPTOR_ATTR_REMOTE_WAKEUP) ? 2 : 0;
|
1279 |
|
|
ep0.common.control_buffer[1] = 0;
|
1280 |
|
|
result = USBS_CONTROL_RETURN_HANDLED;
|
1281 |
|
|
}
|
1282 |
|
|
}
|
1283 |
|
|
else if (recipient == USB_DEVREQ_RECIPIENT_ENDPOINT) {
|
1284 |
|
|
bool halted = false;
|
1285 |
|
|
result = USBS_CONTROL_RETURN_HANDLED;
|
1286 |
|
|
|
1287 |
|
|
switch (req->index_lo) {
|
1288 |
|
|
#if defined(_RX_EP1)
|
1289 |
|
|
case 0x01 : halted = rx_ep1.common.halted; break;
|
1290 |
|
|
#endif
|
1291 |
|
|
#if defined(_TX_EP1)
|
1292 |
|
|
case 0x81 : halted = tx_ep1.common.halted; break;
|
1293 |
|
|
#endif
|
1294 |
|
|
#if defined(_RX_EP2)
|
1295 |
|
|
case 0x02 : halted = rx_ep2.common.halted; break;
|
1296 |
|
|
#endif
|
1297 |
|
|
#if defined(_TX_EP2)
|
1298 |
|
|
case 0x82 : halted = tx_ep2.common.halted; break;
|
1299 |
|
|
#endif
|
1300 |
|
|
|
1301 |
|
|
default:
|
1302 |
|
|
result = USBS_CONTROL_RETURN_STALL;
|
1303 |
|
|
}
|
1304 |
|
|
|
1305 |
|
|
TRACE_D12("Get Status: Endp [0x%02X] %s\n", (unsigned) req->index_lo,
|
1306 |
|
|
halted ? "Halt" : "Unhalt");
|
1307 |
|
|
if (result == USBS_CONTROL_RETURN_HANDLED) {
|
1308 |
|
|
ep0.common.control_buffer[0] = (halted) ? 1 : 0;
|
1309 |
|
|
ep0.common.control_buffer[1] = 0;
|
1310 |
|
|
}
|
1311 |
|
|
}
|
1312 |
|
|
|
1313 |
|
|
if (result == USBS_CONTROL_RETURN_HANDLED) {
|
1314 |
|
|
ep0.common.buffer = ep0.common.control_buffer;
|
1315 |
|
|
ep0.common.buffer_size = 2;
|
1316 |
|
|
ep0.common.fill_buffer_fn = 0;
|
1317 |
|
|
ep0.common.complete_fn = 0;
|
1318 |
|
|
}
|
1319 |
|
|
}
|
1320 |
|
|
else if ((req->request == USB_DEVREQ_SET_FEATURE ||
|
1321 |
|
|
req->request == USB_DEVREQ_CLEAR_FEATURE) &&
|
1322 |
|
|
recipient == USB_DEVREQ_RECIPIENT_ENDPOINT) {
|
1323 |
|
|
|
1324 |
|
|
bool halt = (req->request == USB_DEVREQ_SET_FEATURE);
|
1325 |
|
|
result = USBS_CONTROL_RETURN_HANDLED;
|
1326 |
|
|
TRACE_D12("Endpoint [0x%02X] %s\n", (unsigned) req->index_lo,
|
1327 |
|
|
halt ? "Halt" : "Unhalt");
|
1328 |
|
|
|
1329 |
|
|
switch (req->index_lo) {
|
1330 |
|
|
#if defined(_RX_EP1)
|
1331 |
|
|
case 0x01 : usbs_d12_stall_rx_ep(&rx_ep1, halt); break;
|
1332 |
|
|
#endif
|
1333 |
|
|
#if defined(_TX_EP1)
|
1334 |
|
|
case 0x81 : usbs_d12_stall_tx_ep(&tx_ep1, halt); break;
|
1335 |
|
|
#endif
|
1336 |
|
|
#if defined(_RX_EP2)
|
1337 |
|
|
case 0x02 : usbs_d12_stall_rx_ep(&rx_ep2, halt); break;
|
1338 |
|
|
#endif
|
1339 |
|
|
#if defined(_TX_EP2)
|
1340 |
|
|
case 0x82 : usbs_d12_stall_tx_ep(&tx_ep2, halt); break;
|
1341 |
|
|
#endif
|
1342 |
|
|
|
1343 |
|
|
default:
|
1344 |
|
|
result = USBS_CONTROL_RETURN_STALL;
|
1345 |
|
|
}
|
1346 |
|
|
}
|
1347 |
|
|
else if (ep0.common.standard_control_fn != 0) {
|
1348 |
|
|
result = (*ep0.common.standard_control_fn)
|
1349 |
|
|
(&ep0.common,
|
1350 |
|
|
ep0.common.standard_control_data);
|
1351 |
|
|
}
|
1352 |
|
|
|
1353 |
|
|
if (result == USBS_CONTROL_RETURN_UNKNOWN)
|
1354 |
|
|
result = usbs_handle_standard_control(&ep0.common);
|
1355 |
|
|
|
1356 |
|
|
return result;
|
1357 |
|
|
}
|
1358 |
|
|
|
1359 |
|
|
// --------------------------------------------------------------------------
|
1360 |
|
|
// Handler for the receipt of a setup (dev request) packet from the host.
|
1361 |
|
|
// We examine the packet to determine what function(s) should get a crack
|
1362 |
|
|
// at trying to handle it, then pass control to the proper function. If
|
1363 |
|
|
// the function handles the message we either ACK (len==0) or prepare for
|
1364 |
|
|
// an IN or OUT data phase. If no one handled the message, we stall the
|
1365 |
|
|
// control endpoint.
|
1366 |
|
|
|
1367 |
|
|
static void
|
1368 |
|
|
usbs_d12_ep0_setup_packet(usb_devreq* req)
|
1369 |
|
|
{
|
1370 |
|
|
int len, dir, protocol, recipient;
|
1371 |
|
|
usbs_control_return result = USBS_CONTROL_RETURN_UNKNOWN;
|
1372 |
|
|
|
1373 |
|
|
// ----- See who should take the request -----
|
1374 |
|
|
|
1375 |
|
|
len = make_word(req->length_hi, req->length_lo);
|
1376 |
|
|
|
1377 |
|
|
dir = req->type & USB_DEVREQ_DIRECTION_MASK;
|
1378 |
|
|
protocol = req->type & USB_DEVREQ_TYPE_MASK;
|
1379 |
|
|
recipient = req->type & USB_DEVREQ_RECIPIENT_MASK;
|
1380 |
|
|
|
1381 |
|
|
TRACE_BUF0("DevReq: ", ep0.common.control_buffer, sizeof(usb_devreq));
|
1382 |
|
|
|
1383 |
|
|
if (protocol == USB_DEVREQ_TYPE_STANDARD)
|
1384 |
|
|
result = usbs_d12_handle_std_req(req);
|
1385 |
|
|
else {
|
1386 |
|
|
// Pass on non-standard requests to registered handlers
|
1387 |
|
|
|
1388 |
|
|
usbs_control_return (*callback_fn)(usbs_control_endpoint*, void*);
|
1389 |
|
|
void *callback_arg;
|
1390 |
|
|
|
1391 |
|
|
if (protocol == USB_DEVREQ_TYPE_CLASS) {
|
1392 |
|
|
callback_fn = ep0.common.class_control_fn;
|
1393 |
|
|
callback_arg = ep0.common.class_control_data;
|
1394 |
|
|
}
|
1395 |
|
|
else if (protocol == USB_DEVREQ_TYPE_VENDOR) {
|
1396 |
|
|
callback_fn = ep0.common.vendor_control_fn;
|
1397 |
|
|
callback_arg = ep0.common.vendor_control_data;
|
1398 |
|
|
}
|
1399 |
|
|
else {
|
1400 |
|
|
callback_fn = ep0.common.reserved_control_fn;
|
1401 |
|
|
callback_arg = ep0.common.reserved_control_data;
|
1402 |
|
|
}
|
1403 |
|
|
|
1404 |
|
|
result = (callback_fn) ? (*callback_fn)(&ep0.common, callback_arg)
|
1405 |
|
|
: USBS_CONTROL_RETURN_STALL;
|
1406 |
|
|
}
|
1407 |
|
|
|
1408 |
|
|
// ----- If handled prep/handle data phase, otherwise stall -----
|
1409 |
|
|
|
1410 |
|
|
if (result == USBS_CONTROL_RETURN_HANDLED) {
|
1411 |
|
|
if (len == 0) {
|
1412 |
|
|
TRACE_D12("\tCtrl ACK\n");
|
1413 |
|
|
d12_write_endp_buf(D12_BASE_ADDR, D12_TX_ENDP0, 0, 0);
|
1414 |
|
|
}
|
1415 |
|
|
else {
|
1416 |
|
|
// Set EP0 state to IN or OUT mode for data phase
|
1417 |
|
|
ep0.transmitted = 0;
|
1418 |
|
|
ep0.length = len;
|
1419 |
|
|
|
1420 |
|
|
if (dir == USB_DEVREQ_DIRECTION_OUT) {
|
1421 |
|
|
// Wait for the next packet from the host.
|
1422 |
|
|
ep0.ep_state = ENDP_STATE_OUT;
|
1423 |
|
|
CYG_ASSERT(ep0.common.buffer != 0,
|
1424 |
|
|
"A rx buffer should have been provided for EP0");
|
1425 |
|
|
CYG_ASSERT(ep0.common.complete_fn != 0,
|
1426 |
|
|
"A completion function should be provided for EP0 OUT control messages");
|
1427 |
|
|
}
|
1428 |
|
|
else {
|
1429 |
|
|
ep0.tx_empty = true;
|
1430 |
|
|
ep0.ep_state = ENDP_STATE_IN;
|
1431 |
|
|
ep0_fill_tx_buffer();
|
1432 |
|
|
usbs_d12_ep0_tx();
|
1433 |
|
|
}
|
1434 |
|
|
}
|
1435 |
|
|
}
|
1436 |
|
|
else {
|
1437 |
|
|
TRACE_D12("\t*** Unhandled Device Request ***\n");
|
1438 |
|
|
// The request wasn't handled, so stall control endpoint
|
1439 |
|
|
d12_stall_ctrl_endp(D12_BASE_ADDR, true);
|
1440 |
|
|
}
|
1441 |
|
|
}
|
1442 |
|
|
|
1443 |
|
|
// --------------------------------------------------------------------------
|
1444 |
|
|
// This is called when the chip indicates that a packet has been received
|
1445 |
|
|
// on control endpoint 0. If it's a setup packet, we handle it accordingly,
|
1446 |
|
|
// otherwise it's a data packet coming in on ep0.
|
1447 |
|
|
//
|
1448 |
|
|
|
1449 |
|
|
static void
|
1450 |
|
|
usbs_d12_ep0_rx_intr(void)
|
1451 |
|
|
{
|
1452 |
|
|
byte byStat = d12_read_last_trans_status(D12_BASE_ADDR, D12_RX_ENDP0);
|
1453 |
|
|
TRACE_D12("\tEP0 Status: 0x%02X\n", (unsigned) byStat);
|
1454 |
|
|
|
1455 |
|
|
if (byStat & D12_LAST_TRANS_SETUP_PACKET) {
|
1456 |
|
|
usb_devreq *req = (usb_devreq *) ep0.common.control_buffer;
|
1457 |
|
|
|
1458 |
|
|
if (!d12_read_setup_packet(D12_BASE_ADDR, (byte*) req)) {
|
1459 |
|
|
TRACE_D12("ep0_rx_dsr: Error reading setup packet\n");
|
1460 |
|
|
d12_stall_ctrl_endp(D12_BASE_ADDR, true);
|
1461 |
|
|
}
|
1462 |
|
|
else
|
1463 |
|
|
usbs_d12_ep0_setup_packet(req);
|
1464 |
|
|
}
|
1465 |
|
|
else {
|
1466 |
|
|
if (ep0.common.buffer) {
|
1467 |
|
|
uint8 n = d12_read_endp_buf(D12_BASE_ADDR, D12_RX_ENDP0,
|
1468 |
|
|
ep0.common.buffer + ep0.transmitted);
|
1469 |
|
|
ep0.transmitted += n;
|
1470 |
|
|
|
1471 |
|
|
TRACE_D12("EP0: Received %d bytes\n", (unsigned) n);
|
1472 |
|
|
|
1473 |
|
|
if (n < D12_ENDP0_SIZE ||
|
1474 |
|
|
ep0.common.buffer_size - ep0.transmitted < D12_ENDP0_SIZE) {
|
1475 |
|
|
TRACE_D12("\tEP0: Rx Complete (%d) %p\n",
|
1476 |
|
|
ep0.transmitted, ep0.common.complete_fn);
|
1477 |
|
|
|
1478 |
|
|
if (usbs_d12_ep0_complete(0) == USBS_CONTROL_RETURN_HANDLED)
|
1479 |
|
|
d12_write_endp_buf(D12_BASE_ADDR, D12_TX_ENDP0, 0, 0);
|
1480 |
|
|
else
|
1481 |
|
|
d12_stall_ctrl_endp(D12_BASE_ADDR, true);
|
1482 |
|
|
}
|
1483 |
|
|
}
|
1484 |
|
|
else {
|
1485 |
|
|
TRACE_D12("EP0: No Rx buffer. Discarding packet\n");
|
1486 |
|
|
d12_read_endp_buf(D12_BASE_ADDR, D12_RX_ENDP0, NULL);
|
1487 |
|
|
}
|
1488 |
|
|
}
|
1489 |
|
|
}
|
1490 |
|
|
|
1491 |
|
|
// --------------------------------------------------------------------------
|
1492 |
|
|
// Handler for when the device is put into or taken out of suspend mode.
|
1493 |
|
|
// It updates the state variable in the control endpoint and calls the
|
1494 |
|
|
// registered state change function, if any.
|
1495 |
|
|
|
1496 |
|
|
// TODO: Put the chip into low power mode??? Stop clocks, etc???
|
1497 |
|
|
|
1498 |
|
|
static void
|
1499 |
|
|
usbs_d12_suspend(bool suspended)
|
1500 |
|
|
{
|
1501 |
|
|
int old_state = ep0.common.state;
|
1502 |
|
|
usbs_state_change state_change;
|
1503 |
|
|
|
1504 |
|
|
if (suspended) {
|
1505 |
|
|
ep0.common.state |= USBS_STATE_SUSPENDED;
|
1506 |
|
|
state_change = USBS_STATE_CHANGE_SUSPENDED;
|
1507 |
|
|
}
|
1508 |
|
|
else {
|
1509 |
|
|
ep0.common.state &= USBS_STATE_MASK;
|
1510 |
|
|
state_change = USBS_STATE_CHANGE_RESUMED;
|
1511 |
|
|
}
|
1512 |
|
|
|
1513 |
|
|
if (ep0.common.state_change_fn) {
|
1514 |
|
|
(*ep0.common.state_change_fn)(&ep0.common, ep0.common.state_change_data,
|
1515 |
|
|
state_change, old_state);
|
1516 |
|
|
}
|
1517 |
|
|
}
|
1518 |
|
|
|
1519 |
|
|
// --------------------------------------------------------------------------
|
1520 |
|
|
// Common Rx Endpoint 1 & 2
|
1521 |
|
|
// --------------------------------------------------------------------------
|
1522 |
|
|
|
1523 |
|
|
#if defined(_RX_EP1) || defined(_RX_EP2)
|
1524 |
|
|
|
1525 |
|
|
static void usbs_d12_clear_rx_ep(rx_endpoint *ep)
|
1526 |
|
|
{
|
1527 |
|
|
ep->common.buffer = 0;
|
1528 |
|
|
ep->common.buffer_size = 0;
|
1529 |
|
|
ep->common.complete_fn = 0;
|
1530 |
|
|
ep->common.complete_data = 0;
|
1531 |
|
|
|
1532 |
|
|
ep->received = 0;
|
1533 |
|
|
}
|
1534 |
|
|
|
1535 |
|
|
// --------------------------------------------------------------------------
|
1536 |
|
|
// This is called when an rx operation is completed. It resets the endpoint
|
1537 |
|
|
// vars and calls the registered completion function.
|
1538 |
|
|
//
|
1539 |
|
|
|
1540 |
|
|
static void
|
1541 |
|
|
usbs_d12_ep_rx_complete(rx_endpoint *ep, int result)
|
1542 |
|
|
{
|
1543 |
|
|
completion_fn fn = ep->common.complete_fn;
|
1544 |
|
|
void *data = ep->common.complete_data;
|
1545 |
|
|
|
1546 |
|
|
usbs_d12_clear_rx_ep(ep);
|
1547 |
|
|
|
1548 |
|
|
if (fn)
|
1549 |
|
|
(*fn)(data, result);
|
1550 |
|
|
}
|
1551 |
|
|
|
1552 |
|
|
// --------------------------------------------------------------------------
|
1553 |
|
|
// This routine is called when an rx buffer in the chip is full and ready to
|
1554 |
|
|
// be read. If there's an endpoint buffer available and room to hold the data
|
1555 |
|
|
// we read it in, otherwise we call the completion function, but leave the
|
1556 |
|
|
// data in the chip. The hardware will automatically NAK packages from the
|
1557 |
|
|
// host until the app calls another start read to continue receiving data.
|
1558 |
|
|
//
|
1559 |
|
|
// CONTEXT:
|
1560 |
|
|
// Called from either the DSR or application thread, via start rx.
|
1561 |
|
|
// In either case, it's assumed that the chip is locked.
|
1562 |
|
|
//
|
1563 |
|
|
|
1564 |
|
|
static void
|
1565 |
|
|
usbs_d12_ep_rx(rx_endpoint *ep)
|
1566 |
|
|
{
|
1567 |
|
|
int n, ep_size, buf_remaining, endp = ep->endp;
|
1568 |
|
|
bool done;
|
1569 |
|
|
|
1570 |
|
|
// The main endp is double buffered and we need to be prepared
|
1571 |
|
|
// to read both simultaneously.
|
1572 |
|
|
ep_size = (endp == D12_MAIN_ENDP) ? (2 * D12_MAIN_ENDP_SIZE)
|
1573 |
|
|
: RX_ENDP_SIZE[endp];
|
1574 |
|
|
|
1575 |
|
|
buf_remaining = ep->common.buffer_size - ep->received;
|
1576 |
|
|
|
1577 |
|
|
// ----- If no space left in buffer, call completion fn -----
|
1578 |
|
|
|
1579 |
|
|
if (!ep->common.buffer || buf_remaining < ep_size) {
|
1580 |
|
|
int ret = ep->received;
|
1581 |
|
|
|
1582 |
|
|
// See if caller requested a read smaller than the endp. Read &
|
1583 |
|
|
// throw away extra
|
1584 |
|
|
if (ep->common.buffer_size < ep_size) {
|
1585 |
|
|
byte tmp_buf[D12_MAX_PACKET_SIZE];
|
1586 |
|
|
|
1587 |
|
|
if (endp == D12_MAIN_ENDP)
|
1588 |
|
|
n = d12_read_main_endp_buf(D12_BASE_ADDR, tmp_buf, &done);
|
1589 |
|
|
else
|
1590 |
|
|
n = d12_read_endp_buf(D12_BASE_ADDR, RX_ENDP_INDEX[endp], tmp_buf);
|
1591 |
|
|
|
1592 |
|
|
if (n > ep->common.buffer_size) {
|
1593 |
|
|
n = ep->received = ep->common.buffer_size;
|
1594 |
|
|
ret = -ENOMEM;
|
1595 |
|
|
TRACE_D12("\tEP%d: *** Rx Buffer too small. Data Lost ***\n", endp);
|
1596 |
|
|
}
|
1597 |
|
|
else
|
1598 |
|
|
ret = ep->received = n;
|
1599 |
|
|
|
1600 |
|
|
memcpy(ep->common.buffer, tmp_buf, n);
|
1601 |
|
|
buf_remaining = ep->common.buffer_size - n;
|
1602 |
|
|
}
|
1603 |
|
|
|
1604 |
|
|
TRACE_D12("\tEP%d: Rx Complete. Buffer (nearly) full. [%d]\n",
|
1605 |
|
|
endp, buf_remaining);
|
1606 |
|
|
usbs_d12_ep_rx_complete(ep, ret);
|
1607 |
|
|
return;
|
1608 |
|
|
}
|
1609 |
|
|
|
1610 |
|
|
// ----- Read the data from the chip -----
|
1611 |
|
|
|
1612 |
|
|
if (endp == D12_MAIN_ENDP)
|
1613 |
|
|
n = d12_read_main_endp_buf(D12_BASE_ADDR,
|
1614 |
|
|
ep->common.buffer + ep->received, &done);
|
1615 |
|
|
else {
|
1616 |
|
|
n = d12_read_endp_buf(D12_BASE_ADDR, RX_ENDP_INDEX[endp],
|
1617 |
|
|
ep->common.buffer + ep->received);
|
1618 |
|
|
done = (n < RX_ENDP_SIZE[endp]);
|
1619 |
|
|
}
|
1620 |
|
|
|
1621 |
|
|
ep->received += n;
|
1622 |
|
|
buf_remaining = ep->common.buffer_size - ep->received;
|
1623 |
|
|
|
1624 |
|
|
done = done || (buf_remaining < ep_size);
|
1625 |
|
|
|
1626 |
|
|
TRACE_D12("EP%d: Received %d bytes.\n", endp, n);
|
1627 |
|
|
TRACE_BUF("\t", ep->common.buffer + ep->received-n, n);
|
1628 |
|
|
|
1629 |
|
|
// ----- If we're done, complete the receive -----
|
1630 |
|
|
|
1631 |
|
|
if (done) {
|
1632 |
|
|
TRACE_D12("\tEP%d Rx Complete (%d) %p\n", endp,
|
1633 |
|
|
ep->received, ep->common.complete_fn);
|
1634 |
|
|
usbs_d12_ep_rx_complete(ep, ep->received);
|
1635 |
|
|
}
|
1636 |
|
|
}
|
1637 |
|
|
|
1638 |
|
|
// --------------------------------------------------------------------------
|
1639 |
|
|
// Stalls/unstalls the specified endpoint.
|
1640 |
|
|
|
1641 |
|
|
static void
|
1642 |
|
|
usbs_d12_stall_rx_ep(rx_endpoint *ep, cyg_bool halt)
|
1643 |
|
|
{
|
1644 |
|
|
ep->common.halted = halt;
|
1645 |
|
|
d12_stall_endp(D12_BASE_ADDR, RX_ENDP_INDEX[ep->endp], halt);
|
1646 |
|
|
}
|
1647 |
|
|
|
1648 |
|
|
// --------------------------------------------------------------------------
|
1649 |
|
|
// Handler for an Rx endpoint full interrupt. It clears the interrupt on the
|
1650 |
|
|
// D12 by reading the endpoint's status register then calls the routine to
|
1651 |
|
|
// read the data into the buffer.
|
1652 |
|
|
//
|
1653 |
|
|
// Called from the DSR context only.
|
1654 |
|
|
//
|
1655 |
|
|
|
1656 |
|
|
static void
|
1657 |
|
|
usbs_d12_ep_rx_intr(rx_endpoint *ep)
|
1658 |
|
|
{
|
1659 |
|
|
d12_read_last_trans_status(D12_BASE_ADDR, RX_ENDP_INDEX[ep->endp]);
|
1660 |
|
|
usbs_d12_ep_rx(ep);
|
1661 |
|
|
}
|
1662 |
|
|
|
1663 |
|
|
#endif
|
1664 |
|
|
|
1665 |
|
|
// --------------------------------------------------------------------------
|
1666 |
|
|
// Common Tx Endpoint 1 & 2
|
1667 |
|
|
// --------------------------------------------------------------------------
|
1668 |
|
|
|
1669 |
|
|
#if defined(_TX_EP1) || defined(_TX_EP2)
|
1670 |
|
|
|
1671 |
|
|
// Clears out the endpoint data structure before/after a tx is complete.
|
1672 |
|
|
|
1673 |
|
|
static void usbs_d12_clear_tx_ep(tx_endpoint *ep)
|
1674 |
|
|
{
|
1675 |
|
|
ep->common.buffer = 0;
|
1676 |
|
|
ep->common.buffer_size = 0;
|
1677 |
|
|
ep->common.complete_fn = 0;
|
1678 |
|
|
ep->common.complete_data = 0;
|
1679 |
|
|
|
1680 |
|
|
ep->transmitted = 0;
|
1681 |
|
|
ep->tx_empty = false;
|
1682 |
|
|
}
|
1683 |
|
|
|
1684 |
|
|
// --------------------------------------------------------------------------
|
1685 |
|
|
// This is called when a transmit is completed. It resets the endpoint vars
|
1686 |
|
|
// and calls the registered completion function, if any.
|
1687 |
|
|
//
|
1688 |
|
|
// CONTEXT:
|
1689 |
|
|
// Called from either the DSR or the app thread that started tx.
|
1690 |
|
|
|
1691 |
|
|
static void usbs_d12_ep_tx_complete(tx_endpoint *ep, int result)
|
1692 |
|
|
{
|
1693 |
|
|
completion_fn fn = ep->common.complete_fn;
|
1694 |
|
|
void *data = ep->common.complete_data;
|
1695 |
|
|
|
1696 |
|
|
usbs_d12_clear_tx_ep(ep);
|
1697 |
|
|
|
1698 |
|
|
if (fn)
|
1699 |
|
|
(*fn)(data, result);
|
1700 |
|
|
}
|
1701 |
|
|
|
1702 |
|
|
// --------------------------------------------------------------------------
|
1703 |
|
|
// The routine writes data to the chip and updates the endpoint's counters.
|
1704 |
|
|
// It gets called at the start of a transfer operation to prime the device
|
1705 |
|
|
// and then gets called each time the chip finishes sending a packet to the
|
1706 |
|
|
// host and is ready for more data. If the amount of data remaining is
|
1707 |
|
|
// smaller than can fit in the chip's endpoint buffer, then this is the last
|
1708 |
|
|
// packet to send, so we call the completion function.
|
1709 |
|
|
//
|
1710 |
|
|
// CONTEXT:
|
1711 |
|
|
// Called from either the DSR or the app thread that started the tx
|
1712 |
|
|
// In either case, it's assumed the chip is locked.
|
1713 |
|
|
|
1714 |
|
|
static void
|
1715 |
|
|
usbs_d12_ep_tx(tx_endpoint *ep)
|
1716 |
|
|
{
|
1717 |
|
|
int n, nRemaining;
|
1718 |
|
|
|
1719 |
|
|
// ----- Already done. Intermittent interrupt, so get out -----
|
1720 |
|
|
|
1721 |
|
|
if (!ep->common.buffer)
|
1722 |
|
|
return;
|
1723 |
|
|
|
1724 |
|
|
// ----- See how many bytes remaining in buffer -----
|
1725 |
|
|
|
1726 |
|
|
nRemaining = ep->common.buffer_size - ep->transmitted;
|
1727 |
|
|
|
1728 |
|
|
TRACE_D12("EP%d: Tx %p, %d Done, %d Remaining\n", ep->endp,
|
1729 |
|
|
ep->common.buffer, ep->transmitted, nRemaining);
|
1730 |
|
|
|
1731 |
|
|
// ----- If prev packet was last, signal that we're done -----
|
1732 |
|
|
|
1733 |
|
|
if (nRemaining == 0 && !ep->tx_empty) {
|
1734 |
|
|
TRACE_D12("\tEP%d: Tx complete (%d) %p\n", ep->endp,
|
1735 |
|
|
ep->transmitted, ep->common.complete_fn);
|
1736 |
|
|
usbs_d12_ep_tx_complete(ep, ep->transmitted);
|
1737 |
|
|
return;
|
1738 |
|
|
}
|
1739 |
|
|
|
1740 |
|
|
// ----- Write the next packet to chip -----
|
1741 |
|
|
|
1742 |
|
|
if (nRemaining < TX_ENDP_SIZE[ep->endp]) {
|
1743 |
|
|
n = nRemaining;
|
1744 |
|
|
ep->tx_empty = false;
|
1745 |
|
|
}
|
1746 |
|
|
else
|
1747 |
|
|
n = TX_ENDP_SIZE[ep->endp];
|
1748 |
|
|
|
1749 |
|
|
TRACE_D12("EP%d: Writing %d bytes. %s\n", ep->endp,
|
1750 |
|
|
n, (n == 0) ? "DONE" : "");
|
1751 |
|
|
TRACE_BUF("\t", ep->common.buffer + ep->transmitted, n);
|
1752 |
|
|
|
1753 |
|
|
d12_write_endp_buf(D12_BASE_ADDR, TX_ENDP_INDEX[ep->endp],
|
1754 |
|
|
ep->common.buffer + ep->transmitted, (uint8) n);
|
1755 |
|
|
|
1756 |
|
|
ep->transmitted += n;
|
1757 |
|
|
|
1758 |
|
|
// ----- If empty packet, complete now -----
|
1759 |
|
|
|
1760 |
|
|
if (n == 0) {
|
1761 |
|
|
TRACE_D12("\tEP%d: Tx complete (%d) %p\n", ep->endp,
|
1762 |
|
|
ep->transmitted, ep->common.complete_fn);
|
1763 |
|
|
usbs_d12_ep_tx_complete(ep, ep->transmitted);
|
1764 |
|
|
return;
|
1765 |
|
|
}
|
1766 |
|
|
}
|
1767 |
|
|
|
1768 |
|
|
// --------------------------------------------------------------------------
|
1769 |
|
|
// Stalls/unstalls the specified tx endpoint.
|
1770 |
|
|
|
1771 |
|
|
static void
|
1772 |
|
|
usbs_d12_stall_tx_ep(tx_endpoint *ep, cyg_bool halt)
|
1773 |
|
|
{
|
1774 |
|
|
ep->common.halted = halt;
|
1775 |
|
|
d12_stall_endp(D12_BASE_ADDR, TX_ENDP_INDEX[ep->endp], halt);
|
1776 |
|
|
}
|
1777 |
|
|
|
1778 |
|
|
// --------------------------------------------------------------------------
|
1779 |
|
|
// Handler for when the chip's tx RAM for an endoint has just been emptied
|
1780 |
|
|
// (sent to the host) and the chip is ready for more data.
|
1781 |
|
|
// We read the endpoint's last trans status register to clear the interrupt
|
1782 |
|
|
// on the D12, then call the tx function to send the next packet or
|
1783 |
|
|
// complete the transfer.
|
1784 |
|
|
|
1785 |
|
|
static void
|
1786 |
|
|
usbs_d12_ep_tx_intr(tx_endpoint *ep)
|
1787 |
|
|
{
|
1788 |
|
|
d12_read_last_trans_status(D12_BASE_ADDR, TX_ENDP_INDEX[ep->endp]);
|
1789 |
|
|
usbs_d12_ep_tx(ep);
|
1790 |
|
|
}
|
1791 |
|
|
|
1792 |
|
|
#endif // defined(_TX_EP1) || defined(_TX_EP2)
|
1793 |
|
|
|
1794 |
|
|
// --------------------------------------------------------------------------
|
1795 |
|
|
// Application Program Interface (API)
|
1796 |
|
|
// --------------------------------------------------------------------------
|
1797 |
|
|
|
1798 |
|
|
#if defined(_RX_EP1) || defined(_RX_EP2)
|
1799 |
|
|
// Starts a receive operation on the specified endpoint. If the buffer size
|
1800 |
|
|
// is zero the completion function is called immediately. The routine checks
|
1801 |
|
|
// if tehre is data in the chip's endpoint buffer, and if so it will call
|
1802 |
|
|
// ep_rx() to start reading the data out of the chip.
|
1803 |
|
|
//
|
1804 |
|
|
// If the endpoint is currently stalled, a read size of zero can be used to
|
1805 |
|
|
// block the calling thread until the stall is cleared. If the read size is
|
1806 |
|
|
// non-zero and the endpoint is stalled the completion function is called
|
1807 |
|
|
// immediately with an error result.
|
1808 |
|
|
|
1809 |
|
|
static void
|
1810 |
|
|
usbs_d12_api_start_rx_ep(usbs_rx_endpoint *ep)
|
1811 |
|
|
{
|
1812 |
|
|
rx_endpoint *epx = (rx_endpoint *) ep;
|
1813 |
|
|
|
1814 |
|
|
if (ep->halted) {
|
1815 |
|
|
if (ep->buffer_size != 0)
|
1816 |
|
|
usbs_d12_ep_rx_complete(epx, -EAGAIN);
|
1817 |
|
|
}
|
1818 |
|
|
else if (ep->buffer_size == 0) {
|
1819 |
|
|
usbs_d12_ep_rx_complete(epx, 0);
|
1820 |
|
|
}
|
1821 |
|
|
else {
|
1822 |
|
|
TRACE_D12("EP%d: Starting Rx, %p, %d\n", epx->endp, ep->buffer,
|
1823 |
|
|
ep->buffer_size);
|
1824 |
|
|
usbs_d12_lock();
|
1825 |
|
|
|
1826 |
|
|
epx->received = 0;
|
1827 |
|
|
if (d12_data_available(D12_BASE_ADDR, RX_ENDP_INDEX[epx->endp]))
|
1828 |
|
|
usbs_d12_ep_rx(epx);
|
1829 |
|
|
|
1830 |
|
|
usbs_d12_unlock();
|
1831 |
|
|
}
|
1832 |
|
|
}
|
1833 |
|
|
|
1834 |
|
|
// --------------------------------------------------------------------------
|
1835 |
|
|
// Halts/unhalts one of the generic rx (OUT) endpoints.
|
1836 |
|
|
//
|
1837 |
|
|
|
1838 |
|
|
static void usbs_d12_api_stall_rx_ep(usbs_rx_endpoint *ep, cyg_bool halt)
|
1839 |
|
|
{
|
1840 |
|
|
usbs_d12_lock();
|
1841 |
|
|
usbs_d12_stall_rx_ep((rx_endpoint*) ep, halt);
|
1842 |
|
|
usbs_d12_unlock();
|
1843 |
|
|
}
|
1844 |
|
|
|
1845 |
|
|
#endif // defined(_RX_EP1) || defined(_RX_EP2)
|
1846 |
|
|
|
1847 |
|
|
// --------------------------------------------------------------------------
|
1848 |
|
|
// Tx API
|
1849 |
|
|
// --------------------------------------------------------------------------
|
1850 |
|
|
|
1851 |
|
|
#if defined(_TX_EP1) || defined(_TX_EP2)
|
1852 |
|
|
|
1853 |
|
|
// This starts a transmit on one of the data endpoints. If the endpoint is
|
1854 |
|
|
// stalled a buffer size of zero can be used to block until the stall is
|
1855 |
|
|
// cleared. Any other size on a stalled endpoint will result in an error
|
1856 |
|
|
// callback immediately. The first packet is sent to the chip immediately,
|
1857 |
|
|
// in the application context. If the chip's buffer can contain the whole
|
1858 |
|
|
// transfer, the completion function will be called immediately, again,
|
1859 |
|
|
// still in the application context.
|
1860 |
|
|
//
|
1861 |
|
|
// If an empty packet is requested we send one from here and call the
|
1862 |
|
|
// completion function. This should not cause an intr on the D12.
|
1863 |
|
|
//
|
1864 |
|
|
// CONTEXT:
|
1865 |
|
|
// Called from an application thread
|
1866 |
|
|
|
1867 |
|
|
static void usbs_d12_api_start_tx_ep(usbs_tx_endpoint *ep)
|
1868 |
|
|
{
|
1869 |
|
|
tx_endpoint *epx = (tx_endpoint*) ep;
|
1870 |
|
|
|
1871 |
|
|
if (ep->halted) {
|
1872 |
|
|
if (ep->buffer_size != 0)
|
1873 |
|
|
usbs_d12_ep_tx_complete(epx, -EAGAIN);
|
1874 |
|
|
}
|
1875 |
|
|
else if (ep->buffer_size == 0) {
|
1876 |
|
|
usbs_d12_lock();
|
1877 |
|
|
|
1878 |
|
|
d12_write_endp_buf(D12_BASE_ADDR, TX_ENDP_INDEX[epx->endp], 0, 0);
|
1879 |
|
|
usbs_d12_ep_tx_complete(epx, 0);
|
1880 |
|
|
|
1881 |
|
|
usbs_d12_unlock();
|
1882 |
|
|
}
|
1883 |
|
|
else {
|
1884 |
|
|
TRACE_D12("EP%d: Starting Tx, %p, %d\n", epx->endp, ep->buffer,
|
1885 |
|
|
ep->buffer_size);
|
1886 |
|
|
usbs_d12_lock();
|
1887 |
|
|
|
1888 |
|
|
epx->tx_empty = true;
|
1889 |
|
|
epx->transmitted = 0;
|
1890 |
|
|
usbs_d12_ep_tx(epx);
|
1891 |
|
|
|
1892 |
|
|
usbs_d12_unlock();
|
1893 |
|
|
}
|
1894 |
|
|
}
|
1895 |
|
|
|
1896 |
|
|
// --------------------------------------------------------------------------
|
1897 |
|
|
// Halts/unhalts one of the generic endpoints.
|
1898 |
|
|
|
1899 |
|
|
static void
|
1900 |
|
|
usbs_d12_api_stall_tx_ep(usbs_tx_endpoint *ep, cyg_bool halt)
|
1901 |
|
|
{
|
1902 |
|
|
usbs_d12_lock();
|
1903 |
|
|
usbs_d12_stall_tx_ep((tx_endpoint*) ep, halt);
|
1904 |
|
|
usbs_d12_unlock();
|
1905 |
|
|
}
|
1906 |
|
|
|
1907 |
|
|
#endif // defined(_TX_ENDP1) || defined(_TX_EP2)
|
1908 |
|
|
|
1909 |
|
|
// --------------------------------------------------------------------------
|
1910 |
|
|
// DSR
|
1911 |
|
|
// --------------------------------------------------------------------------
|
1912 |
|
|
|
1913 |
|
|
// The DSR for the D12 chip. This is normally called in the DSR context when
|
1914 |
|
|
// the D12 has raised its interrupt flag indicating that it needs to be
|
1915 |
|
|
// serviced. The interrupt register contains bit flags that are OR'ed togther
|
1916 |
|
|
// indicating what items need to be serviced. There are flags for the
|
1917 |
|
|
// following:
|
1918 |
|
|
// - The endpoints (one bit for each)
|
1919 |
|
|
// - Bus Reset
|
1920 |
|
|
// - Suspend Change
|
1921 |
|
|
// - DMA (terminal count)
|
1922 |
|
|
//
|
1923 |
|
|
// Care must be taken in that the D12's interrupt output is level-sensitive
|
1924 |
|
|
// (in that the interrupt sources are OR'ed together and not all cleared
|
1925 |
|
|
// atomically in a single operation). Platforms (such as the PC) may be
|
1926 |
|
|
// expecting edge-triggered interrupts, so we must work around that.
|
1927 |
|
|
// So, we loop on the interrupt register. Even though, in each loop, we
|
1928 |
|
|
// perform all of the required operations to clear the interrupts, a new
|
1929 |
|
|
// one may have arrived before we finished clearing the previous ones.
|
1930 |
|
|
// So we read the intr reg again. Once the intr reg gives a zero reading
|
1931 |
|
|
// we know that the D12 has dropped its IRQ line.
|
1932 |
|
|
//
|
1933 |
|
|
// Note, if we're configured to use a thread, this routine is called from
|
1934 |
|
|
// within a thread context (not a DSR context).
|
1935 |
|
|
//
|
1936 |
|
|
|
1937 |
|
|
static void
|
1938 |
|
|
usbs_d12_dsr(cyg_vector_t vector, cyg_ucount32 count,
|
1939 |
|
|
cyg_addrword_t data)
|
1940 |
|
|
{
|
1941 |
|
|
uint16 status;
|
1942 |
|
|
bool suspended;
|
1943 |
|
|
|
1944 |
|
|
CYG_ASSERT(vector == CYGNUM_DEVS_USB_D12_INT,
|
1945 |
|
|
"DSR should only be invoked for D12 interrupts");
|
1946 |
|
|
|
1947 |
|
|
while ((status = d12_read_intr_reg(D12_BASE_ADDR)) != 0) {
|
1948 |
|
|
TRACE_D12("Intr Status: 0x%04X\n", (unsigned) status);
|
1949 |
|
|
|
1950 |
|
|
if (status & D12_INTR_BUS_RESET) {
|
1951 |
|
|
TRACE_D12("\n>>> Bus Reset <<<\n");
|
1952 |
|
|
usbs_d12_reset();
|
1953 |
|
|
}
|
1954 |
|
|
else {
|
1955 |
|
|
|
1956 |
|
|
// ----- Suspend Change -----
|
1957 |
|
|
|
1958 |
|
|
suspended = (bool) (ep0.common.state & USBS_STATE_SUSPENDED);
|
1959 |
|
|
|
1960 |
|
|
if (status & D12_INTR_SUSPEND_CHANGE) {
|
1961 |
|
|
if (!suspended && (status & ~D12_INTR_SUSPEND_CHANGE) == 0)
|
1962 |
|
|
usbs_d12_suspend(true);
|
1963 |
|
|
}
|
1964 |
|
|
else if (suspended)
|
1965 |
|
|
usbs_d12_suspend(false);
|
1966 |
|
|
|
1967 |
|
|
// ----- Bulk Endpoints -----
|
1968 |
|
|
|
1969 |
|
|
#ifdef _TX_EP2
|
1970 |
|
|
if (status & D12_INTR_TX_ENDP2)
|
1971 |
|
|
usbs_d12_ep_tx_intr(&tx_ep2);
|
1972 |
|
|
#endif
|
1973 |
|
|
|
1974 |
|
|
#ifdef _RX_EP2
|
1975 |
|
|
if (status & D12_INTR_RX_ENDP2)
|
1976 |
|
|
usbs_d12_ep_rx_intr(&rx_ep2);
|
1977 |
|
|
#endif
|
1978 |
|
|
|
1979 |
|
|
// ----- Interrupt Endpoints -----
|
1980 |
|
|
|
1981 |
|
|
#ifdef _TX_EP1
|
1982 |
|
|
if (status & D12_INTR_TX_ENDP1)
|
1983 |
|
|
usbs_d12_ep_tx_intr(&tx_ep1);
|
1984 |
|
|
#endif
|
1985 |
|
|
|
1986 |
|
|
#ifdef _RX_EP1
|
1987 |
|
|
if (status & D12_INTR_RX_ENDP1)
|
1988 |
|
|
usbs_d12_ep_rx_intr(&rx_ep1);
|
1989 |
|
|
#endif
|
1990 |
|
|
|
1991 |
|
|
// ----- Control Endpoint -----
|
1992 |
|
|
|
1993 |
|
|
if (status & D12_INTR_TX_CTRL_ENDP)
|
1994 |
|
|
usbs_d12_ep0_tx_intr();
|
1995 |
|
|
|
1996 |
|
|
if (status & D12_INTR_RX_CTRL_ENDP)
|
1997 |
|
|
usbs_d12_ep0_rx_intr();
|
1998 |
|
|
}
|
1999 |
|
|
}
|
2000 |
|
|
|
2001 |
|
|
cyg_drv_interrupt_unmask(vector);
|
2002 |
|
|
}
|
2003 |
|
|
|
2004 |
|
|
// --------------------------------------------------------------------------
|
2005 |
|
|
// Interrupt
|
2006 |
|
|
// --------------------------------------------------------------------------
|
2007 |
|
|
|
2008 |
|
|
// Here, the ISR does nothing but schedule the DSR to run. The ISR's/DSR's
|
2009 |
|
|
// are serialized. The CPU won't process another ISR until after the DSR
|
2010 |
|
|
// completes.
|
2011 |
|
|
|
2012 |
|
|
static uint32
|
2013 |
|
|
usbs_d12_isr(cyg_vector_t vector, cyg_addrword_t data)
|
2014 |
|
|
{
|
2015 |
|
|
CYG_ASSERT(CYGNUM_DEVS_USB_D12_INT == vector,
|
2016 |
|
|
"usbs_isr: Incorrect interrupt");
|
2017 |
|
|
|
2018 |
|
|
// Prevent another interrupt until DSR completes
|
2019 |
|
|
cyg_drv_interrupt_mask(vector);
|
2020 |
|
|
cyg_drv_interrupt_acknowledge(vector);
|
2021 |
|
|
|
2022 |
|
|
return CYG_ISR_HANDLED | CYG_ISR_CALL_DSR;
|
2023 |
|
|
}
|
2024 |
|
|
|
2025 |
|
|
// --------------------------------------------------------------------------
|
2026 |
|
|
// Polling
|
2027 |
|
|
// --------------------------------------------------------------------------
|
2028 |
|
|
|
2029 |
|
|
static void
|
2030 |
|
|
usbs_d12_poll(usbs_control_endpoint *endp)
|
2031 |
|
|
{
|
2032 |
|
|
CYG_ASSERT(endp == &ep0.common, "usbs_poll: wrong endpoint");
|
2033 |
|
|
|
2034 |
|
|
usbs_d12_lock();
|
2035 |
|
|
usbs_d12_dsr(CYGNUM_DEVS_USB_D12_INT, 0, 0);
|
2036 |
|
|
usbs_d12_unlock();
|
2037 |
|
|
}
|
2038 |
|
|
|
2039 |
|
|
// --------------------------------------------------------------------------
|
2040 |
|
|
// Thread Processing
|
2041 |
|
|
// --------------------------------------------------------------------------
|
2042 |
|
|
|
2043 |
|
|
// The user can opt to configure the driver to service the D12 using a high
|
2044 |
|
|
// priority thread. The thread's priority MUST be greater than the priority
|
2045 |
|
|
// of any application thread making calls into the driver.
|
2046 |
|
|
// When we use a thread, the DSR simply signals a semaphore tio wake the
|
2047 |
|
|
// thread up. The thread, in turn, calls the the routine to service the D12,
|
2048 |
|
|
// now in a thread context. This allows for greater debug options, including
|
2049 |
|
|
// tracing.
|
2050 |
|
|
|
2051 |
|
|
#ifdef CYGPKG_DEVS_USB_D12_THREAD
|
2052 |
|
|
|
2053 |
|
|
static byte usbs_d12_thread_stack[CYGNUM_DEVS_USB_D12_THREAD_STACK_SIZE];
|
2054 |
|
|
static cyg_thread usbs_d12_thread;
|
2055 |
|
|
static cyg_handle_t usbs_d12_thread_handle;
|
2056 |
|
|
static cyg_sem_t usbs_d12_sem;
|
2057 |
|
|
|
2058 |
|
|
static void
|
2059 |
|
|
usbs_d12_thread_dsr(cyg_vector_t vector, cyg_ucount32 count,
|
2060 |
|
|
cyg_addrword_t data)
|
2061 |
|
|
{
|
2062 |
|
|
cyg_semaphore_post(&usbs_d12_sem);
|
2063 |
|
|
|
2064 |
|
|
CYG_UNUSED_PARAM(cyg_vector_t, vector);
|
2065 |
|
|
CYG_UNUSED_PARAM(cyg_ucount32, count);
|
2066 |
|
|
CYG_UNUSED_PARAM(cyg_addrword_t, data);
|
2067 |
|
|
}
|
2068 |
|
|
|
2069 |
|
|
|
2070 |
|
|
static void
|
2071 |
|
|
usbs_d12_thread_fn(cyg_addrword_t param)
|
2072 |
|
|
{
|
2073 |
|
|
while (1) {
|
2074 |
|
|
cyg_semaphore_wait(&usbs_d12_sem);
|
2075 |
|
|
usbs_d12_poll(&ep0.common);
|
2076 |
|
|
}
|
2077 |
|
|
|
2078 |
|
|
CYG_UNUSED_PARAM(cyg_addrword_t, param);
|
2079 |
|
|
}
|
2080 |
|
|
|
2081 |
|
|
|
2082 |
|
|
static void
|
2083 |
|
|
usbs_d12_thread_init(void)
|
2084 |
|
|
{
|
2085 |
|
|
cyg_semaphore_init(&usbs_d12_sem, 0);
|
2086 |
|
|
|
2087 |
|
|
cyg_thread_create(CYGNUM_DEVS_USB_D12_THREAD_PRIORITY,
|
2088 |
|
|
&usbs_d12_thread_fn, 0, "D12 USB Driver Thread",
|
2089 |
|
|
usbs_d12_thread_stack,
|
2090 |
|
|
CYGNUM_DEVS_USB_D12_THREAD_STACK_SIZE,
|
2091 |
|
|
&usbs_d12_thread_handle, &usbs_d12_thread);
|
2092 |
|
|
cyg_thread_resume(usbs_d12_thread_handle);
|
2093 |
|
|
}
|
2094 |
|
|
|
2095 |
|
|
#endif // CYGPKG_DEVS_USB_D12_THREAD
|
2096 |
|
|
|
2097 |
|
|
// --------------------------------------------------------------------------
|
2098 |
|
|
// Start/Reset
|
2099 |
|
|
// --------------------------------------------------------------------------
|
2100 |
|
|
|
2101 |
|
|
// Chip initialization and handler for a USB Bus Reset. This gets called at
|
2102 |
|
|
// system startup and after a USB Bus Reset. It puts the chip into the
|
2103 |
|
|
// default state, with USB Address 0, connected to the bus (i.e.
|
2104 |
|
|
// "SoftConnect" asserted). Interrupts to the main endpoint are turned on
|
2105 |
|
|
// via the DMA register.
|
2106 |
|
|
|
2107 |
|
|
static void
|
2108 |
|
|
usbs_d12_reset(void)
|
2109 |
|
|
{
|
2110 |
|
|
int old_state = ep0.common.state;
|
2111 |
|
|
ep0.common.state = USBS_STATE_DEFAULT;
|
2112 |
|
|
|
2113 |
|
|
if (ep0.common.state_change_fn) {
|
2114 |
|
|
(*ep0.common.state_change_fn)(&ep0.common, ep0.common.state_change_data,
|
2115 |
|
|
USBS_STATE_CHANGE_RESET, old_state);
|
2116 |
|
|
}
|
2117 |
|
|
|
2118 |
|
|
d12_set_addr_enable(D12_BASE_ADDR, 0, true);
|
2119 |
|
|
d12_set_endp_enable(D12_BASE_ADDR, true);
|
2120 |
|
|
d12_set_dma(D12_BASE_ADDR, D12_DMA_MAIN_ENDP_INTR_ENABLE);
|
2121 |
|
|
d12_set_mode(D12_BASE_ADDR, D12_MODE_CFG_DFLT | D12_MODE_CFG_SOFT_CONNECT,
|
2122 |
|
|
D12_MODE_CLK_DFLT);
|
2123 |
|
|
|
2124 |
|
|
// If any endpoints were going, signal the end of transfers
|
2125 |
|
|
|
2126 |
|
|
#if defined(_TX_EP2)
|
2127 |
|
|
usbs_d12_ep_tx_complete(&tx_ep2, -EPIPE);
|
2128 |
|
|
#endif
|
2129 |
|
|
|
2130 |
|
|
#if defined(_RX_EP2)
|
2131 |
|
|
usbs_d12_ep_rx_complete(&rx_ep2, -EPIPE);
|
2132 |
|
|
#endif
|
2133 |
|
|
|
2134 |
|
|
#if defined(_TX_EP1)
|
2135 |
|
|
usbs_d12_ep_tx_complete(&tx_ep1, -EPIPE);
|
2136 |
|
|
#endif
|
2137 |
|
|
|
2138 |
|
|
#if defined(_RX_EP1)
|
2139 |
|
|
usbs_d12_ep_rx_complete(&rx_ep1, -EPIPE);
|
2140 |
|
|
#endif
|
2141 |
|
|
}
|
2142 |
|
|
|
2143 |
|
|
// --------------------------------------------------------------------------
|
2144 |
|
|
// The start function is called indirectly by the application when
|
2145 |
|
|
// initialization is complete. By this time, the enumeration data has been
|
2146 |
|
|
// assigned to the control endpoint and we're ready to connect to the host.
|
2147 |
|
|
// Within the reset function the D12's SoftConnect line is asserted which
|
2148 |
|
|
// allows the host (hub) to see us on the USB bus. If connected, the host
|
2149 |
|
|
// should start the enumeration process.
|
2150 |
|
|
//
|
2151 |
|
|
|
2152 |
|
|
static void usbs_d12_start(usbs_control_endpoint *endpoint)
|
2153 |
|
|
{
|
2154 |
|
|
#if defined(_TRACE) && !defined(_TRACE_STDOUT)
|
2155 |
|
|
TRACE_OPEN(TRACE_SINK);
|
2156 |
|
|
#endif
|
2157 |
|
|
|
2158 |
|
|
CYG_ASSERT(endpoint == &ep0.common, "ep0 start: wrong endpoint");
|
2159 |
|
|
TRACE_D12("USBS D12: Starting.\n");
|
2160 |
|
|
|
2161 |
|
|
d12_clear_all_intr(D12_BASE_ADDR);
|
2162 |
|
|
usbs_d12_reset();
|
2163 |
|
|
}
|
2164 |
|
|
|
2165 |
|
|
// --------------------------------------------------------------------------
|
2166 |
|
|
// Initialization
|
2167 |
|
|
// --------------------------------------------------------------------------
|
2168 |
|
|
|
2169 |
|
|
// This routine is called early in the program's startup, possibly before
|
2170 |
|
|
// main (from within a C++ object initialization). We want to put this chip
|
2171 |
|
|
// and driver in a neutral, but ready, state until the application gets
|
2172 |
|
|
// control, initializes itself and calls the usb start function.
|
2173 |
|
|
//
|
2174 |
|
|
// The D12 has a "Soft Connect" feature to tristate the USB bus, making it
|
2175 |
|
|
// appear that the USB device is not connected to the bus. We initially
|
2176 |
|
|
// keep seperated from the bus to allow for initialization.
|
2177 |
|
|
|
2178 |
|
|
void
|
2179 |
|
|
usbs_d12_init(void)
|
2180 |
|
|
{
|
2181 |
|
|
cyg_DSR_t *pdsr;
|
2182 |
|
|
|
2183 |
|
|
d12_set_mode(D12_BASE_ADDR, D12_MODE_CFG_DFLT & ~D12_MODE_CFG_SOFT_CONNECT,
|
2184 |
|
|
D12_MODE_CLK_DFLT);
|
2185 |
|
|
|
2186 |
|
|
d12_set_addr_enable(D12_BASE_ADDR, 0, false);
|
2187 |
|
|
d12_set_endp_enable(D12_BASE_ADDR, false);
|
2188 |
|
|
|
2189 |
|
|
// ----- Clear the endpoints -----
|
2190 |
|
|
|
2191 |
|
|
#if defined(_TX_EP2)
|
2192 |
|
|
usbs_d12_clear_tx_ep(&tx_ep2);
|
2193 |
|
|
#endif
|
2194 |
|
|
|
2195 |
|
|
#if defined(_RX_EP2)
|
2196 |
|
|
usbs_d12_clear_rx_ep(&rx_ep2);
|
2197 |
|
|
#endif
|
2198 |
|
|
|
2199 |
|
|
#if defined(_TX_EP1)
|
2200 |
|
|
usbs_d12_clear_tx_ep(&tx_ep1);
|
2201 |
|
|
#endif
|
2202 |
|
|
|
2203 |
|
|
#if defined(_RX_EP1)
|
2204 |
|
|
usbs_d12_clear_rx_ep(&rx_ep1);
|
2205 |
|
|
#endif
|
2206 |
|
|
|
2207 |
|
|
// ----- Start the thread (if we're using it) -----
|
2208 |
|
|
|
2209 |
|
|
#ifdef CYGPKG_DEVS_USB_D12_THREAD
|
2210 |
|
|
usbs_d12_thread_init();
|
2211 |
|
|
pdsr = &usbs_d12_thread_dsr;
|
2212 |
|
|
#else
|
2213 |
|
|
pdsr = &usbs_d12_dsr;
|
2214 |
|
|
#endif
|
2215 |
|
|
|
2216 |
|
|
// ----- Attach the ISR -----
|
2217 |
|
|
|
2218 |
|
|
cyg_drv_interrupt_create(CYGNUM_DEVS_USB_D12_INT,
|
2219 |
|
|
0, 0, &usbs_d12_isr, pdsr,
|
2220 |
|
|
&usbs_d12_intr_handle, &usbs_d12_intr_data);
|
2221 |
|
|
|
2222 |
|
|
cyg_drv_interrupt_attach(usbs_d12_intr_handle);
|
2223 |
|
|
cyg_drv_interrupt_unmask(CYGNUM_DEVS_USB_D12_INT);
|
2224 |
|
|
}
|
2225 |
|
|
|
2226 |
|
|
// ----------------------------------------------------------------------------
|
2227 |
|
|
// Testing support.
|
2228 |
|
|
|
2229 |
|
|
usbs_testing_endpoint usbs_testing_endpoints[] = {
|
2230 |
|
|
{
|
2231 |
|
|
endpoint_type : USB_ENDPOINT_DESCRIPTOR_ATTR_CONTROL,
|
2232 |
|
|
endpoint_number : 0,
|
2233 |
|
|
endpoint_direction : USB_ENDPOINT_DESCRIPTOR_ENDPOINT_IN,
|
2234 |
|
|
endpoint : (void*) &ep0.common,
|
2235 |
|
|
#ifdef CYGVAR_DEVS_USB_D12_EP0_DEVTAB_ENTRY
|
2236 |
|
|
devtab_entry : CYGDAT_DEVS_USB_D12_DEVTAB_BASENAME "0c",
|
2237 |
|
|
#else
|
2238 |
|
|
devtab_entry : (const char*) 0,
|
2239 |
|
|
#endif
|
2240 |
|
|
min_size : 1,
|
2241 |
|
|
max_size : CYGNUM_DEVS_USB_D12_EP0_TXBUFSIZE,
|
2242 |
|
|
max_in_padding : 0,
|
2243 |
|
|
alignment : 0
|
2244 |
|
|
},
|
2245 |
|
|
|
2246 |
|
|
/*
|
2247 |
|
|
#ifdef _TX_EP1
|
2248 |
|
|
{
|
2249 |
|
|
endpoint_type : USB_ENDPOINT_DESCRIPTOR_ATTR_INTERRUPT,
|
2250 |
|
|
endpoint_number : 1,
|
2251 |
|
|
endpoint_direction : USB_ENDPOINT_DESCRIPTOR_ENDPOINT_IN,
|
2252 |
|
|
endpoint : (void*) &tx_ep1.common,
|
2253 |
|
|
# ifdef CYGVAR_DEVS_USB_D12_TX_EP2_DEVTAB_ENTRY
|
2254 |
|
|
devtab_entry : CYGDAT_DEVS_USB_D12_DEVTAB_BASENAME "1w",
|
2255 |
|
|
# else
|
2256 |
|
|
devtab_entry : (const char*) 0,
|
2257 |
|
|
# endif
|
2258 |
|
|
min_size : 0,
|
2259 |
|
|
max_size : 0x0FFFF, // Driver limitation, only a single
|
2260 |
|
|
// buffer descriptor is used
|
2261 |
|
|
max_in_padding : 0,
|
2262 |
|
|
alignment : HAL_DCACHE_LINE_SIZE
|
2263 |
|
|
},
|
2264 |
|
|
#endif
|
2265 |
|
|
|
2266 |
|
|
#ifdef _RX_EP1
|
2267 |
|
|
{
|
2268 |
|
|
endpoint_type : USB_ENDPOINT_DESCRIPTOR_ATTR_INTERRUPT,
|
2269 |
|
|
endpoint_number : 1,
|
2270 |
|
|
endpoint_direction : USB_ENDPOINT_DESCRIPTOR_ENDPOINT_OUT,
|
2271 |
|
|
endpoint : (void*) &rx_ep1.common,
|
2272 |
|
|
# ifdef CYGVAR_DEVS_USB_D12_RX_EP2_DEVTAB_ENTRY
|
2273 |
|
|
devtab_entry : CYGDAT_DEVS_USB_D12_DEVTAB_BASENAME "1r",
|
2274 |
|
|
# else
|
2275 |
|
|
devtab_entry : (const char*) 0,
|
2276 |
|
|
# endif
|
2277 |
|
|
min_size : 1,
|
2278 |
|
|
max_size : 0x0FFFF, // Driver limitation
|
2279 |
|
|
max_in_padding : 0,
|
2280 |
|
|
alignment : HAL_DCACHE_LINE_SIZE
|
2281 |
|
|
},
|
2282 |
|
|
#endif
|
2283 |
|
|
*/
|
2284 |
|
|
|
2285 |
|
|
#ifdef _TX_EP2
|
2286 |
|
|
{
|
2287 |
|
|
endpoint_type : USB_ENDPOINT_DESCRIPTOR_ATTR_BULK,
|
2288 |
|
|
endpoint_number : 2,
|
2289 |
|
|
endpoint_direction : USB_ENDPOINT_DESCRIPTOR_ENDPOINT_IN,
|
2290 |
|
|
endpoint : (void*) &tx_ep2.common,
|
2291 |
|
|
# ifdef CYGVAR_DEVS_USB_D12_TX_EP2_DEVTAB_ENTRY
|
2292 |
|
|
devtab_entry : CYGDAT_DEVS_USB_D12_DEVTAB_BASENAME "2w",
|
2293 |
|
|
# else
|
2294 |
|
|
devtab_entry : (const char*) 0,
|
2295 |
|
|
# endif
|
2296 |
|
|
min_size : 0,
|
2297 |
|
|
max_size : 0x1000, // 4k for testing
|
2298 |
|
|
max_in_padding : 0,
|
2299 |
|
|
alignment : HAL_DCACHE_LINE_SIZE
|
2300 |
|
|
},
|
2301 |
|
|
#endif
|
2302 |
|
|
|
2303 |
|
|
#ifdef _RX_EP2
|
2304 |
|
|
{
|
2305 |
|
|
endpoint_type : USB_ENDPOINT_DESCRIPTOR_ATTR_BULK,
|
2306 |
|
|
endpoint_number : 2,
|
2307 |
|
|
endpoint_direction : USB_ENDPOINT_DESCRIPTOR_ENDPOINT_OUT,
|
2308 |
|
|
endpoint : (void*) &rx_ep2.common,
|
2309 |
|
|
# ifdef CYGVAR_DEVS_USB_D12_RX_EP2_DEVTAB_ENTRY
|
2310 |
|
|
devtab_entry : CYGDAT_DEVS_USB_D12_DEVTAB_BASENAME "2r",
|
2311 |
|
|
# else
|
2312 |
|
|
devtab_entry : (const char*) 0,
|
2313 |
|
|
# endif
|
2314 |
|
|
min_size : 1,
|
2315 |
|
|
max_size : 0x1000, // 4k for testing
|
2316 |
|
|
max_in_padding : 0,
|
2317 |
|
|
alignment : HAL_DCACHE_LINE_SIZE
|
2318 |
|
|
},
|
2319 |
|
|
#endif
|
2320 |
|
|
|
2321 |
|
|
USBS_TESTING_ENDPOINTS_TERMINATOR
|
2322 |
|
|
};
|
2323 |
|
|
|