OpenCores
URL https://opencores.org/ocsvn/usb_device_core/usb_device_core/trunk

Subversion Repositories usb_device_core

[/] [usb_device_core/] [trunk/] [sw/] [usbf_hw.c] - Blame information for rev 5

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 5 ultra_embe
//-----------------------------------------------------------------
2
//                         USB CDC Device SW
3
//                               V0.1
4
//                         Ultra-Embedded.com
5
//                          Copyright 2014
6
//
7
//                  Email: admin@ultra-embedded.com
8
//
9
//                          License: GPL
10
// If you would like a version with a more permissive license for use in
11
// closed source commercial applications please contact me for details.
12
//-----------------------------------------------------------------
13
//
14
// This file is part of USB CDC Device SW.
15
//
16
// USB CDC Device SW is free software; you can redistribute it and/or modify
17
// it under the terms of the GNU General Public License as published by
18
// the Free Software Foundation; either version 2 of the License, or
19
// (at your option) any later version.
20
//
21
// USB CDC Device SW is distributed in the hope that it will be useful,
22
// but WITHOUT ANY WARRANTY; without even the implied warranty of
23
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24
// GNU General Public License for more details.
25
//
26
// You should have received a copy of the GNU General Public License
27
// along with USB CDC Device SW; if not, write to the Free Software
28
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29
//-----------------------------------------------------------------
30
#include "usbf_hw.h"
31
#include "usb_log.h"
32
#include "timer.h"
33
 
34
//-----------------------------------------------------------------
35
// Peripheral registers:
36
//-----------------------------------------------------------------
37
#define USB_FUNC_CTRL     0x0
38
    #define USB_FUNC_CTRL_HS_CHIRP_EN            8
39
    #define USB_FUNC_CTRL_HS_CHIRP_EN_SHIFT      8
40
    #define USB_FUNC_CTRL_HS_CHIRP_EN_MASK       0x1
41
 
42
    #define USB_FUNC_CTRL_PHY_DMPULLDOWN         7
43
    #define USB_FUNC_CTRL_PHY_DMPULLDOWN_SHIFT   7
44
    #define USB_FUNC_CTRL_PHY_DMPULLDOWN_MASK    0x1
45
 
46
    #define USB_FUNC_CTRL_PHY_DPPULLDOWN         6
47
    #define USB_FUNC_CTRL_PHY_DPPULLDOWN_SHIFT   6
48
    #define USB_FUNC_CTRL_PHY_DPPULLDOWN_MASK    0x1
49
 
50
    #define USB_FUNC_CTRL_PHY_TERMSELECT         5
51
    #define USB_FUNC_CTRL_PHY_TERMSELECT_SHIFT   5
52
    #define USB_FUNC_CTRL_PHY_TERMSELECT_MASK    0x1
53
 
54
    #define USB_FUNC_CTRL_PHY_XCVRSELECT_SHIFT   3
55
    #define USB_FUNC_CTRL_PHY_XCVRSELECT_MASK    0x3
56
 
57
    #define USB_FUNC_CTRL_PHY_OPMODE_SHIFT       1
58
    #define USB_FUNC_CTRL_PHY_OPMODE_MASK        0x3
59
 
60
    #define USB_FUNC_CTRL_INT_EN_SOF             0
61
    #define USB_FUNC_CTRL_INT_EN_SOF_SHIFT       0
62
    #define USB_FUNC_CTRL_INT_EN_SOF_MASK        0x1
63
 
64
#define USB_FUNC_STAT     0x4
65
    #define USB_FUNC_STAT_RST                    13
66
    #define USB_FUNC_STAT_RST_SHIFT              13
67
    #define USB_FUNC_STAT_RST_MASK               0x1
68
 
69
    #define USB_FUNC_STAT_LINESTATE_SHIFT        11
70
    #define USB_FUNC_STAT_LINESTATE_MASK         0x3
71
 
72
    #define USB_FUNC_STAT_FRAME_SHIFT            0
73
    #define USB_FUNC_STAT_FRAME_MASK             0x7ff
74
 
75
#define USB_FUNC_ADDR     0x8
76
    #define USB_FUNC_ADDR_DEV_ADDR_SHIFT         0
77
    #define USB_FUNC_ADDR_DEV_ADDR_MASK          0x7f
78
 
79
#define USB_EP0_CFG       0xc
80
    #define USB_EP0_CFG_INT_RX                   3
81
    #define USB_EP0_CFG_INT_RX_SHIFT             3
82
    #define USB_EP0_CFG_INT_RX_MASK              0x1
83
 
84
    #define USB_EP0_CFG_INT_TX                   2
85
    #define USB_EP0_CFG_INT_TX_SHIFT             2
86
    #define USB_EP0_CFG_INT_TX_MASK              0x1
87
 
88
    #define USB_EP0_CFG_STALL_EP                 1
89
    #define USB_EP0_CFG_STALL_EP_SHIFT           1
90
    #define USB_EP0_CFG_STALL_EP_MASK            0x1
91
 
92
    #define USB_EP0_CFG_ISO                      0
93
    #define USB_EP0_CFG_ISO_SHIFT                0
94
    #define USB_EP0_CFG_ISO_MASK                 0x1
95
 
96
#define USB_EP0_TX_CTRL   0x10
97
    #define USB_EP0_TX_CTRL_TX_FLUSH             17
98
    #define USB_EP0_TX_CTRL_TX_FLUSH_SHIFT       17
99
    #define USB_EP0_TX_CTRL_TX_FLUSH_MASK        0x1
100
 
101
    #define USB_EP0_TX_CTRL_TX_START             16
102
    #define USB_EP0_TX_CTRL_TX_START_SHIFT       16
103
    #define USB_EP0_TX_CTRL_TX_START_MASK        0x1
104
 
105
    #define USB_EP0_TX_CTRL_TX_LEN_SHIFT         0
106
    #define USB_EP0_TX_CTRL_TX_LEN_MASK          0x7ff
107
 
108
#define USB_EP0_RX_CTRL   0x14
109
    #define USB_EP0_RX_CTRL_RX_FLUSH             1
110
    #define USB_EP0_RX_CTRL_RX_FLUSH_SHIFT       1
111
    #define USB_EP0_RX_CTRL_RX_FLUSH_MASK        0x1
112
 
113
    #define USB_EP0_RX_CTRL_RX_ACCEPT            0
114
    #define USB_EP0_RX_CTRL_RX_ACCEPT_SHIFT      0
115
    #define USB_EP0_RX_CTRL_RX_ACCEPT_MASK       0x1
116
 
117
#define USB_EP0_STS       0x18
118
    #define USB_EP0_STS_TX_ERR                   20
119
    #define USB_EP0_STS_TX_ERR_SHIFT             20
120
    #define USB_EP0_STS_TX_ERR_MASK              0x1
121
 
122
    #define USB_EP0_STS_TX_BUSY                  19
123
    #define USB_EP0_STS_TX_BUSY_SHIFT            19
124
    #define USB_EP0_STS_TX_BUSY_MASK             0x1
125
 
126
    #define USB_EP0_STS_RX_ERR                   18
127
    #define USB_EP0_STS_RX_ERR_SHIFT             18
128
    #define USB_EP0_STS_RX_ERR_MASK              0x1
129
 
130
    #define USB_EP0_STS_RX_SETUP                 17
131
    #define USB_EP0_STS_RX_SETUP_SHIFT           17
132
    #define USB_EP0_STS_RX_SETUP_MASK            0x1
133
 
134
    #define USB_EP0_STS_RX_READY                 16
135
    #define USB_EP0_STS_RX_READY_SHIFT           16
136
    #define USB_EP0_STS_RX_READY_MASK            0x1
137
 
138
    #define USB_EP0_STS_RX_COUNT_SHIFT           0
139
    #define USB_EP0_STS_RX_COUNT_MASK            0x7ff
140
 
141
#define USB_EP0_DATA      0x1c
142
    #define USB_EP0_DATA_DATA_SHIFT              0
143
    #define USB_EP0_DATA_DATA_MASK               0xff
144
 
145
#define USB_EP1_CFG       0x20
146
    #define USB_EP1_CFG_INT_RX                   3
147
    #define USB_EP1_CFG_INT_RX_SHIFT             3
148
    #define USB_EP1_CFG_INT_RX_MASK              0x1
149
 
150
    #define USB_EP1_CFG_INT_TX                   2
151
    #define USB_EP1_CFG_INT_TX_SHIFT             2
152
    #define USB_EP1_CFG_INT_TX_MASK              0x1
153
 
154
    #define USB_EP1_CFG_STALL_EP                 1
155
    #define USB_EP1_CFG_STALL_EP_SHIFT           1
156
    #define USB_EP1_CFG_STALL_EP_MASK            0x1
157
 
158
    #define USB_EP1_CFG_ISO                      0
159
    #define USB_EP1_CFG_ISO_SHIFT                0
160
    #define USB_EP1_CFG_ISO_MASK                 0x1
161
 
162
#define USB_EP1_TX_CTRL   0x24
163
    #define USB_EP1_TX_CTRL_TX_FLUSH             17
164
    #define USB_EP1_TX_CTRL_TX_FLUSH_SHIFT       17
165
    #define USB_EP1_TX_CTRL_TX_FLUSH_MASK        0x1
166
 
167
    #define USB_EP1_TX_CTRL_TX_START             16
168
    #define USB_EP1_TX_CTRL_TX_START_SHIFT       16
169
    #define USB_EP1_TX_CTRL_TX_START_MASK        0x1
170
 
171
    #define USB_EP1_TX_CTRL_TX_LEN_SHIFT         0
172
    #define USB_EP1_TX_CTRL_TX_LEN_MASK          0x7ff
173
 
174
#define USB_EP1_RX_CTRL   0x28
175
    #define USB_EP1_RX_CTRL_RX_FLUSH             1
176
    #define USB_EP1_RX_CTRL_RX_FLUSH_SHIFT       1
177
    #define USB_EP1_RX_CTRL_RX_FLUSH_MASK        0x1
178
 
179
    #define USB_EP1_RX_CTRL_RX_ACCEPT            0
180
    #define USB_EP1_RX_CTRL_RX_ACCEPT_SHIFT      0
181
    #define USB_EP1_RX_CTRL_RX_ACCEPT_MASK       0x1
182
 
183
#define USB_EP1_STS       0x2c
184
    #define USB_EP1_STS_TX_ERR                   20
185
    #define USB_EP1_STS_TX_ERR_SHIFT             20
186
    #define USB_EP1_STS_TX_ERR_MASK              0x1
187
 
188
    #define USB_EP1_STS_TX_BUSY                  19
189
    #define USB_EP1_STS_TX_BUSY_SHIFT            19
190
    #define USB_EP1_STS_TX_BUSY_MASK             0x1
191
 
192
    #define USB_EP1_STS_RX_ERR                   18
193
    #define USB_EP1_STS_RX_ERR_SHIFT             18
194
    #define USB_EP1_STS_RX_ERR_MASK              0x1
195
 
196
    #define USB_EP1_STS_RX_SETUP                 17
197
    #define USB_EP1_STS_RX_SETUP_SHIFT           17
198
    #define USB_EP1_STS_RX_SETUP_MASK            0x1
199
 
200
    #define USB_EP1_STS_RX_READY                 16
201
    #define USB_EP1_STS_RX_READY_SHIFT           16
202
    #define USB_EP1_STS_RX_READY_MASK            0x1
203
 
204
    #define USB_EP1_STS_RX_COUNT_SHIFT           0
205
    #define USB_EP1_STS_RX_COUNT_MASK            0x7ff
206
 
207
#define USB_EP1_DATA      0x30
208
    #define USB_EP1_DATA_DATA_SHIFT              0
209
    #define USB_EP1_DATA_DATA_MASK               0xff
210
 
211
#define USB_EP2_CFG       0x34
212
    #define USB_EP2_CFG_INT_RX                   3
213
    #define USB_EP2_CFG_INT_RX_SHIFT             3
214
    #define USB_EP2_CFG_INT_RX_MASK              0x1
215
 
216
    #define USB_EP2_CFG_INT_TX                   2
217
    #define USB_EP2_CFG_INT_TX_SHIFT             2
218
    #define USB_EP2_CFG_INT_TX_MASK              0x1
219
 
220
    #define USB_EP2_CFG_STALL_EP                 1
221
    #define USB_EP2_CFG_STALL_EP_SHIFT           1
222
    #define USB_EP2_CFG_STALL_EP_MASK            0x1
223
 
224
    #define USB_EP2_CFG_ISO                      0
225
    #define USB_EP2_CFG_ISO_SHIFT                0
226
    #define USB_EP2_CFG_ISO_MASK                 0x1
227
 
228
#define USB_EP2_TX_CTRL   0x38
229
    #define USB_EP2_TX_CTRL_TX_FLUSH             17
230
    #define USB_EP2_TX_CTRL_TX_FLUSH_SHIFT       17
231
    #define USB_EP2_TX_CTRL_TX_FLUSH_MASK        0x1
232
 
233
    #define USB_EP2_TX_CTRL_TX_START             16
234
    #define USB_EP2_TX_CTRL_TX_START_SHIFT       16
235
    #define USB_EP2_TX_CTRL_TX_START_MASK        0x1
236
 
237
    #define USB_EP2_TX_CTRL_TX_LEN_SHIFT         0
238
    #define USB_EP2_TX_CTRL_TX_LEN_MASK          0x7ff
239
 
240
#define USB_EP2_RX_CTRL   0x3c
241
    #define USB_EP2_RX_CTRL_RX_FLUSH             1
242
    #define USB_EP2_RX_CTRL_RX_FLUSH_SHIFT       1
243
    #define USB_EP2_RX_CTRL_RX_FLUSH_MASK        0x1
244
 
245
    #define USB_EP2_RX_CTRL_RX_ACCEPT            0
246
    #define USB_EP2_RX_CTRL_RX_ACCEPT_SHIFT      0
247
    #define USB_EP2_RX_CTRL_RX_ACCEPT_MASK       0x1
248
 
249
#define USB_EP2_STS       0x40
250
    #define USB_EP2_STS_TX_ERR                   20
251
    #define USB_EP2_STS_TX_ERR_SHIFT             20
252
    #define USB_EP2_STS_TX_ERR_MASK              0x1
253
 
254
    #define USB_EP2_STS_TX_BUSY                  19
255
    #define USB_EP2_STS_TX_BUSY_SHIFT            19
256
    #define USB_EP2_STS_TX_BUSY_MASK             0x1
257
 
258
    #define USB_EP2_STS_RX_ERR                   18
259
    #define USB_EP2_STS_RX_ERR_SHIFT             18
260
    #define USB_EP2_STS_RX_ERR_MASK              0x1
261
 
262
    #define USB_EP2_STS_RX_SETUP                 17
263
    #define USB_EP2_STS_RX_SETUP_SHIFT           17
264
    #define USB_EP2_STS_RX_SETUP_MASK            0x1
265
 
266
    #define USB_EP2_STS_RX_READY                 16
267
    #define USB_EP2_STS_RX_READY_SHIFT           16
268
    #define USB_EP2_STS_RX_READY_MASK            0x1
269
 
270
    #define USB_EP2_STS_RX_COUNT_SHIFT           0
271
    #define USB_EP2_STS_RX_COUNT_MASK            0x7ff
272
 
273
#define USB_EP2_DATA      0x44
274
    #define USB_EP2_DATA_DATA_SHIFT              0
275
    #define USB_EP2_DATA_DATA_MASK               0xff
276
 
277
#define USB_EP3_CFG       0x48
278
    #define USB_EP3_CFG_INT_RX                   3
279
    #define USB_EP3_CFG_INT_RX_SHIFT             3
280
    #define USB_EP3_CFG_INT_RX_MASK              0x1
281
 
282
    #define USB_EP3_CFG_INT_TX                   2
283
    #define USB_EP3_CFG_INT_TX_SHIFT             2
284
    #define USB_EP3_CFG_INT_TX_MASK              0x1
285
 
286
    #define USB_EP3_CFG_STALL_EP                 1
287
    #define USB_EP3_CFG_STALL_EP_SHIFT           1
288
    #define USB_EP3_CFG_STALL_EP_MASK            0x1
289
 
290
    #define USB_EP3_CFG_ISO                      0
291
    #define USB_EP3_CFG_ISO_SHIFT                0
292
    #define USB_EP3_CFG_ISO_MASK                 0x1
293
 
294
#define USB_EP3_TX_CTRL   0x4c
295
    #define USB_EP3_TX_CTRL_TX_FLUSH             17
296
    #define USB_EP3_TX_CTRL_TX_FLUSH_SHIFT       17
297
    #define USB_EP3_TX_CTRL_TX_FLUSH_MASK        0x1
298
 
299
    #define USB_EP3_TX_CTRL_TX_START             16
300
    #define USB_EP3_TX_CTRL_TX_START_SHIFT       16
301
    #define USB_EP3_TX_CTRL_TX_START_MASK        0x1
302
 
303
    #define USB_EP3_TX_CTRL_TX_LEN_SHIFT         0
304
    #define USB_EP3_TX_CTRL_TX_LEN_MASK          0x7ff
305
 
306
#define USB_EP3_RX_CTRL   0x50
307
    #define USB_EP3_RX_CTRL_RX_FLUSH             1
308
    #define USB_EP3_RX_CTRL_RX_FLUSH_SHIFT       1
309
    #define USB_EP3_RX_CTRL_RX_FLUSH_MASK        0x1
310
 
311
    #define USB_EP3_RX_CTRL_RX_ACCEPT            0
312
    #define USB_EP3_RX_CTRL_RX_ACCEPT_SHIFT      0
313
    #define USB_EP3_RX_CTRL_RX_ACCEPT_MASK       0x1
314
 
315
#define USB_EP3_STS       0x54
316
    #define USB_EP3_STS_TX_ERR                   20
317
    #define USB_EP3_STS_TX_ERR_SHIFT             20
318
    #define USB_EP3_STS_TX_ERR_MASK              0x1
319
 
320
    #define USB_EP3_STS_TX_BUSY                  19
321
    #define USB_EP3_STS_TX_BUSY_SHIFT            19
322
    #define USB_EP3_STS_TX_BUSY_MASK             0x1
323
 
324
    #define USB_EP3_STS_RX_ERR                   18
325
    #define USB_EP3_STS_RX_ERR_SHIFT             18
326
    #define USB_EP3_STS_RX_ERR_MASK              0x1
327
 
328
    #define USB_EP3_STS_RX_SETUP                 17
329
    #define USB_EP3_STS_RX_SETUP_SHIFT           17
330
    #define USB_EP3_STS_RX_SETUP_MASK            0x1
331
 
332
    #define USB_EP3_STS_RX_READY                 16
333
    #define USB_EP3_STS_RX_READY_SHIFT           16
334
    #define USB_EP3_STS_RX_READY_MASK            0x1
335
 
336
    #define USB_EP3_STS_RX_COUNT_SHIFT           0
337
    #define USB_EP3_STS_RX_COUNT_MASK            0x7ff
338
 
339
#define USB_EP3_DATA      0x58
340
    #define USB_EP3_DATA_DATA_SHIFT              0
341
    #define USB_EP3_DATA_DATA_MASK               0xff
342
 
343
//-----------------------------------------------------------------
344
// Defines:
345
//-----------------------------------------------------------------
346
#define EP_STRIDE           0x14
347
 
348
#define USB_FUNC_CTRL_MODE_MASK   (0xFF << USB_FUNC_CTRL_PHY_OPMODE_SHIFT)
349
 
350
#define USB_FUNC_CTRL_MODE_HS    ((0 << USB_FUNC_CTRL_PHY_OPMODE_SHIFT) | \
351
                                 (0 << USB_FUNC_CTRL_PHY_XCVRSELECT_SHIFT) | \
352
                                 (0 << USB_FUNC_CTRL_PHY_TERMSELECT_SHIFT) | \
353
                                 (0 << USB_FUNC_CTRL_PHY_DPPULLDOWN_SHIFT) | \
354
                                 (0 << USB_FUNC_CTRL_PHY_DMPULLDOWN_SHIFT) | \
355
                                 (0 << USB_FUNC_CTRL_HS_CHIRP_EN_SHIFT))
356
#define USB_FUNC_CTRL_MODE_PERIP_CHIRP ((2 << USB_FUNC_CTRL_PHY_OPMODE_SHIFT) | \
357
                                 (0 << USB_FUNC_CTRL_PHY_XCVRSELECT_SHIFT) | \
358
                                 (1 << USB_FUNC_CTRL_PHY_TERMSELECT_SHIFT) | \
359
                                 (0 << USB_FUNC_CTRL_PHY_DPPULLDOWN_SHIFT) | \
360
                                 (0 << USB_FUNC_CTRL_PHY_DMPULLDOWN_SHIFT) | \
361
                                 (1 << USB_FUNC_CTRL_HS_CHIRP_EN_SHIFT))
362
#define USB_FUNC_CTRL_MODE_HOST_CHIRP ((2 << USB_FUNC_CTRL_PHY_OPMODE_SHIFT) | \
363
                                 (0 << USB_FUNC_CTRL_PHY_XCVRSELECT_SHIFT) | \
364
                                 (1 << USB_FUNC_CTRL_PHY_TERMSELECT_SHIFT) | \
365
                                 (0 << USB_FUNC_CTRL_PHY_DPPULLDOWN_SHIFT) | \
366
                                 (0 << USB_FUNC_CTRL_PHY_DMPULLDOWN_SHIFT) | \
367
                                 (0 << USB_FUNC_CTRL_HS_CHIRP_EN_SHIFT))
368
#define USB_FUNC_CTRL_PULLUP_EN ((0 << USB_FUNC_CTRL_PHY_OPMODE_SHIFT) | \
369
                                 (1 << USB_FUNC_CTRL_PHY_XCVRSELECT_SHIFT) | \
370
                                 (1 << USB_FUNC_CTRL_PHY_TERMSELECT_SHIFT) | \
371
                                 (0 << USB_FUNC_CTRL_PHY_DPPULLDOWN_SHIFT) | \
372
                                 (0 << USB_FUNC_CTRL_PHY_DMPULLDOWN_SHIFT))
373
#define USB_FUNC_CTRL_PULLUP_DIS ((1 << USB_FUNC_CTRL_PHY_OPMODE_SHIFT) | \
374
                                 (0 << USB_FUNC_CTRL_PHY_XCVRSELECT_SHIFT) | \
375
                                 (0 << USB_FUNC_CTRL_PHY_TERMSELECT_SHIFT) | \
376
                                 (0 << USB_FUNC_CTRL_PHY_DPPULLDOWN_SHIFT) | \
377
                                 (0 << USB_FUNC_CTRL_PHY_DMPULLDOWN_SHIFT))
378
 
379
#define USB_FUNC_ENDPOINTS      4
380
 
381
#define MIN(a,b)                ((a)<=(b)?(a):(b))
382
 
383
//-----------------------------------------------------------------
384
// Locals:
385
//-----------------------------------------------------------------
386
static unsigned int _tx_count[USB_FUNC_ENDPOINTS];
387
static int _addressed;
388
static int _configured;
389
static int _attached;
390
static int _endpoint_stalled[USB_FUNC_ENDPOINTS];
391
static FUNC_PTR _func_bus_reset;
392
static FUNC_PTR _func_setup;
393
static FUNC_PTR _func_ctrl_out;
394
static unsigned int _ctrl_reg;
395
static unsigned int _usb_base;
396
 
397
//-----------------------------------------------------------------
398
// usbhw_reg_write:
399
//-----------------------------------------------------------------
400
static void usbhw_reg_write(unsigned int addr, unsigned data)
401
{
402
    *((volatile unsigned int*)(_usb_base + addr)) = data;
403
}
404
//-----------------------------------------------------------------
405
// usbhw_reg_read:
406
//-----------------------------------------------------------------
407
static unsigned int usbhw_reg_read(unsigned int addr)
408
{
409
    return *((volatile unsigned int*)(_usb_base + addr));
410
}
411
//-----------------------------------------------------------------
412
// usbhw_init:
413
//-----------------------------------------------------------------
414
void usbhw_init(unsigned int base, FUNC_PTR bus_reset, FUNC_PTR on_setup, FUNC_PTR on_out)
415
{
416
    int i;
417
 
418
    _usb_base = base;
419
 
420
    log_printf(USBLOG_HW_RESET, "Init\n");
421
 
422
    for (i=0;i<USB_FUNC_ENDPOINTS;i++)
423
    {
424
        _tx_count[i] = 0;
425
        _endpoint_stalled[i] = 0;
426
    }
427
 
428
    _addressed  = 0;
429
    _configured = 0;
430
    _attached   = 0;
431
 
432
    _ctrl_reg = USB_FUNC_CTRL_PULLUP_DIS;
433
 
434
    _func_bus_reset = bus_reset;
435
    _func_setup = on_setup;
436
    _func_ctrl_out = on_out;
437
 
438
    usbhw_reg_write(USB_EP0_TX_CTRL, (1 << USB_EP0_TX_CTRL_TX_FLUSH_SHIFT));
439
    usbhw_reg_write(USB_EP1_TX_CTRL, (1 << USB_EP1_TX_CTRL_TX_FLUSH_SHIFT));
440
    usbhw_reg_write(USB_EP2_TX_CTRL, (1 << USB_EP2_TX_CTRL_TX_FLUSH_SHIFT));
441
    usbhw_reg_write(USB_EP3_TX_CTRL, (1 << USB_EP3_TX_CTRL_TX_FLUSH_SHIFT));
442
 
443
    usbhw_reg_write(USB_EP0_RX_CTRL, (1 << USB_EP0_RX_CTRL_RX_FLUSH_SHIFT));
444
    usbhw_reg_write(USB_EP1_RX_CTRL, (1 << USB_EP1_RX_CTRL_RX_FLUSH_SHIFT));
445
    usbhw_reg_write(USB_EP2_RX_CTRL, (1 << USB_EP2_RX_CTRL_RX_FLUSH_SHIFT));
446
    usbhw_reg_write(USB_EP3_RX_CTRL, (1 << USB_EP3_RX_CTRL_RX_FLUSH_SHIFT));
447
}
448
//-----------------------------------------------------------------
449
// usbhw_service:
450
//-----------------------------------------------------------------
451
void usbhw_service(void)
452
{
453
    unsigned int status = usbhw_reg_read(USB_FUNC_STAT);
454
    static int _initial  = 1;
455
    static int _reset_count = 0;
456
    int i;
457
 
458
    // Acknowledge fake USB reset after attach..
459
    if (_initial)
460
    {
461
        usbhw_reg_write(USB_FUNC_CTRL, _ctrl_reg);
462
 
463
        _initial = 0;
464
        return ;
465
    }
466
 
467
#ifdef USB_SPEED_HS
468
    // Bus reset event
469
    if (status & (1 << USB_FUNC_STAT_RST))
470
    {
471
        // Ack bus reset by writing to CTRL register
472
        usbhw_reg_write(USB_FUNC_STAT, (1 << USB_FUNC_STAT_RST));
473
 
474
        usbhw_reg_write(USB_EP0_TX_CTRL, (1 << USB_EP0_TX_CTRL_TX_FLUSH_SHIFT));
475
        usbhw_reg_write(USB_EP0_RX_CTRL, (1 << USB_EP0_RX_CTRL_RX_FLUSH_SHIFT));
476
 
477
        usbhw_reg_write(USB_EP1_TX_CTRL, (1 << USB_EP1_TX_CTRL_TX_FLUSH_SHIFT));
478
        usbhw_reg_write(USB_EP2_TX_CTRL, (1 << USB_EP2_TX_CTRL_TX_FLUSH_SHIFT));
479
        usbhw_reg_write(USB_EP3_TX_CTRL, (1 << USB_EP3_TX_CTRL_TX_FLUSH_SHIFT));
480
        usbhw_reg_write(USB_EP1_RX_CTRL, (1 << USB_EP1_RX_CTRL_RX_FLUSH_SHIFT));
481
        usbhw_reg_write(USB_EP2_RX_CTRL, (1 << USB_EP2_RX_CTRL_RX_FLUSH_SHIFT));
482
        usbhw_reg_write(USB_EP3_RX_CTRL, (1 << USB_EP3_RX_CTRL_RX_FLUSH_SHIFT));
483
 
484
        // Fail ... 
485
        if (_reset_count > 4)
486
        {
487
            log_printf(USBLOG_HW_RESET, " DEVICE: Another Reset Detected %d\n", timer_now());
488
 
489
            // Force detach
490
            usbhw_attach(0);
491
            timer_sleep(100);
492
            usbhw_attach(1);
493
 
494
            _reset_count = 0;
495
            return ;
496
        }
497
 
498
        _configured = 0;
499
        _addressed  = 0;
500
 
501
        for (i=0;i<USB_FUNC_ENDPOINTS;i++)
502
        {
503
            _tx_count[i] = 0;
504
            _endpoint_stalled[i] = 0;
505
        }
506
 
507
        log_printf(USBLOG_HW_RESET, " DEVICE: Reset Detected %d\n", timer_now());
508
 
509
        // Detected SE0 for at least 2.5us, now drive HS CHIRP for at least 1ms
510
        usbhw_reg_write(USB_FUNC_CTRL, USB_FUNC_CTRL_MODE_PERIP_CHIRP);
511
        timer_sleep(3);
512
 
513
        // Stop driving the chirp pattern
514
        log_printf(USBLOG_HW_RESET, " DEVICE: Peripheral Chirp De-assert %d\n", timer_now());
515
        usbhw_reg_write(USB_FUNC_CTRL, USB_FUNC_CTRL_MODE_HOST_CHIRP);
516
 
517
        int chirps = 0;
518
        while (chirps < 5)
519
        {
520
            static unsigned old_ls = ~0;
521
            unsigned new_ls = (usbhw_reg_read(USB_FUNC_STAT) >> USB_FUNC_STAT_LINESTATE_SHIFT) & 0x3;
522
            if ((old_ls != new_ls) && (new_ls == 1 || new_ls == 2))
523
                chirps++;
524
            old_ls = new_ls;
525
        }
526
 
527
        log_printf(USBLOG_HW_RESET, " DEVICE: Host Chirp Detected %d\n", timer_now());
528
        usbhw_reg_write(USB_FUNC_CTRL, USB_FUNC_CTRL_MODE_HS);
529
 
530
 
531
        log_printf(USBLOG_HW_RESET, " DEVICE: BUS RESET %d\n", timer_now());
532
 
533
 
534
        if (_func_bus_reset)
535
            _func_bus_reset();
536
 
537
        // Ack bus reset by writing to CTRL register
538
        usbhw_reg_write(USB_FUNC_STAT, (1 << USB_FUNC_STAT_RST));
539
        _reset_count += 1;
540
    }
541
#else
542
    // Bus reset event
543
    if (status & (1 << USB_FUNC_STAT_RST))
544
    {
545
        // Ack bus reset by writing to CTRL register
546
        usbhw_reg_write(USB_FUNC_STAT, (1 << USB_FUNC_STAT_RST));
547
 
548
        _configured = 0;
549
        _addressed  = 0;
550
 
551
        for (i=0;i<USB_FUNC_ENDPOINTS;i++)
552
        {
553
            _tx_count[i] = 0;
554
            _endpoint_stalled[i] = 0;
555
        }
556
 
557
        log_printf(USBLOG_HW_RESET, " DEVICE: BUS RESET %d\n", timer_now());
558
 
559
        usbhw_reg_write(USB_EP0_TX_CTRL, (1 << USB_EP0_TX_CTRL_TX_FLUSH_SHIFT));
560
        usbhw_reg_write(USB_EP1_TX_CTRL, (1 << USB_EP1_TX_CTRL_TX_FLUSH_SHIFT));
561
        usbhw_reg_write(USB_EP2_TX_CTRL, (1 << USB_EP2_TX_CTRL_TX_FLUSH_SHIFT));
562
        usbhw_reg_write(USB_EP3_TX_CTRL, (1 << USB_EP3_TX_CTRL_TX_FLUSH_SHIFT));
563
 
564
        usbhw_reg_write(USB_EP0_RX_CTRL, (1 << USB_EP0_RX_CTRL_RX_FLUSH_SHIFT));
565
        usbhw_reg_write(USB_EP1_RX_CTRL, (1 << USB_EP1_RX_CTRL_RX_FLUSH_SHIFT));
566
        usbhw_reg_write(USB_EP2_RX_CTRL, (1 << USB_EP2_RX_CTRL_RX_FLUSH_SHIFT));
567
        usbhw_reg_write(USB_EP3_RX_CTRL, (1 << USB_EP3_RX_CTRL_RX_FLUSH_SHIFT));
568
 
569
        if (_func_bus_reset)
570
            _func_bus_reset();
571
    }
572
#endif
573
 
574
    status = usbhw_reg_read(USB_EP0_STS);
575
 
576
    // Tx underflow error 
577
    if (status & (1 << USB_EP0_STS_TX_ERR))
578
    {
579
        log_printf(USBLOG_ERR, "Tx underflow error!\n");
580
        while (1)
581
            ;
582
    }
583
    // CRC error 
584
    else if (status & (1 << USB_EP0_STS_RX_ERR))
585
    {
586
        log_printf(USBLOG_ERR, "Rx CRC error!\n");
587
        while (1)
588
            ;
589
    }
590
    // SETUP packet received (EP0)
591
    else if ((status & (1 << USB_EP0_STS_RX_READY)) && (status & (1 << USB_EP0_STS_RX_SETUP)))
592
    {
593
        log_printf(USBLOG_HW_CTRL, "SETUP packet received\n");
594
 
595
        if (_func_setup)
596
            _func_setup();
597
 
598
        log_printf(USBLOG_HW_CTRL, "SETUP packet processed\n");
599
    }
600
    // OUT data received on EP0
601
    else if (status & (1 << USB_EP0_STS_RX_READY))
602
    {
603
        log_printf(USBLOG_HW_CTRL, "OUT packet received on EP0\n");
604
 
605
        if (_func_ctrl_out)
606
            _func_ctrl_out();
607
    }
608
}
609
//-----------------------------------------------------------------
610
// usbhw_attach:
611
//-----------------------------------------------------------------
612
void usbhw_attach(int state)
613
{
614
    // Pull up D+ to Vdd
615
    if ( state )
616
    {
617
        _attached = 1;
618
        _ctrl_reg &= ~USB_FUNC_CTRL_MODE_MASK;
619
        _ctrl_reg |= USB_FUNC_CTRL_PULLUP_EN;
620
        usbhw_reg_write(USB_FUNC_CTRL, _ctrl_reg);
621
        log_printf(USBLOG_HW_CTRL, "ATTACH\n");
622
 
623
        // Reset any previous reset detection
624
        usbhw_reg_write(USB_FUNC_STAT, (1 << USB_FUNC_STAT_RST));
625
    }
626
    // Disconnect pull-up to disconnect from bus
627
    else
628
    {
629
        _attached = 0;
630
        _ctrl_reg &= ~USB_FUNC_CTRL_MODE_MASK;
631
        _ctrl_reg |= USB_FUNC_CTRL_PULLUP_DIS;
632
        usbhw_reg_write(USB_FUNC_CTRL, _ctrl_reg);
633
        log_printf(USBLOG_HW_CTRL, "DETACH\n");
634
    }
635
}
636
//-----------------------------------------------------------------
637
// usbhw_is_configured:
638
//-----------------------------------------------------------------
639
int usbhw_is_configured(void)
640
{
641
    return _configured;
642
}
643
//-----------------------------------------------------------------
644
// usbhw_is_addressed:
645
//-----------------------------------------------------------------
646
int usbhw_is_addressed(void)
647
{
648
    return _addressed;
649
}
650
//-----------------------------------------------------------------
651
// usbhw_is_attached:
652
//-----------------------------------------------------------------
653
int usbhw_is_attached(void)
654
{
655
    return _attached;
656
}
657
//-----------------------------------------------------------------
658
// usbhw_set_configured:
659
//-----------------------------------------------------------------
660
void usbhw_set_configured(int configured)
661
{
662
    _configured = configured;
663
}
664
//-----------------------------------------------------------------
665
// usbhw_set_address:
666
//-----------------------------------------------------------------
667
void usbhw_set_address(unsigned char addr)
668
{
669
    usbhw_reg_write(USB_FUNC_ADDR, addr);
670
    _addressed = 1;
671
}
672
//-----------------------------------------------------------------
673
// usbhw_is_endpoint_stalled:
674
//-----------------------------------------------------------------
675
int usbhw_is_endpoint_stalled(unsigned char endpoint)
676
{
677
    return _endpoint_stalled[endpoint];
678
}
679
//-----------------------------------------------------------------
680
// usbhw_clear_endpoint_stall:
681
//-----------------------------------------------------------------
682
void usbhw_clear_endpoint_stall(unsigned char endpoint)
683
{
684
    unsigned int data = usbhw_reg_read(USB_EP0_CFG + (endpoint * EP_STRIDE));
685
    data &= ~(1 << USB_EP0_CFG_STALL_EP);
686
    usbhw_reg_write(USB_EP0_CFG + (endpoint * EP_STRIDE), data);
687
 
688
    _endpoint_stalled[endpoint] = 0;
689
}
690
//-----------------------------------------------------------------
691
// usbhw_set_endpoint_stall:
692
//-----------------------------------------------------------------
693
void usbhw_set_endpoint_stall(unsigned char endpoint)
694
{
695
    unsigned int data = usbhw_reg_read(USB_EP0_CFG + (endpoint * EP_STRIDE));
696
    data |= (1 << USB_EP0_CFG_STALL_EP);
697
    usbhw_reg_write(USB_EP0_CFG + (endpoint * EP_STRIDE), data);
698
 
699
    _endpoint_stalled[endpoint] = 1;
700
}
701
//-----------------------------------------------------------------
702
// usbhw_is_rx_ready: Is some receive data ready on an endpoint?
703
//-----------------------------------------------------------------
704
int usbhw_is_rx_ready(unsigned char endpoint)
705
{
706
    return (usbhw_reg_read(USB_EP0_STS + (endpoint * EP_STRIDE)) & (1 << USB_EP0_STS_RX_READY_SHIFT)) ? 1 : 0;
707
}
708
//-----------------------------------------------------------------
709
// usbhw_get_rx_count: Get amount of data waiting in endpoint
710
//-----------------------------------------------------------------
711
int usbhw_get_rx_count(unsigned char endpoint)
712
{
713
    return (usbhw_reg_read(USB_EP0_STS + (endpoint * EP_STRIDE)) >> USB_EP0_STS_RX_COUNT_SHIFT) & USB_EP0_STS_RX_COUNT_MASK;
714
}
715
//-----------------------------------------------------------------
716
// usbhw_get_rx_data: Read data from endpoint
717
//-----------------------------------------------------------------
718
int usbhw_get_rx_data(unsigned char endpoint, unsigned char *data, int max_len)
719
{
720
    int i;
721
    int bytes_ready;
722
    int bytes_read = 0;
723
 
724
    // Received data count includes CRC
725
    bytes_ready = usbhw_get_rx_count(endpoint);
726
 
727
    // Limit data read to buffer size
728
    bytes_read = MIN(bytes_ready, max_len);
729
 
730
    for (i=0;i<bytes_read;i++)
731
        *data++ = usbhw_reg_read(USB_EP0_DATA + (endpoint * EP_STRIDE));
732
 
733
    // Return number of bytes read
734
    return bytes_read;
735
}
736
//-----------------------------------------------------------------
737
// usbhw_get_rx_byte: Read byte from endpoint
738
//-----------------------------------------------------------------
739
unsigned char usbhw_get_rx_byte(unsigned char endpoint)
740
{
741
    return usbhw_reg_read(USB_EP0_DATA + (endpoint * EP_STRIDE));
742
}
743
//-----------------------------------------------------------------
744
// usbhw_clear_rx_ready: Clear Rx data ready flag
745
//-----------------------------------------------------------------
746
void usbhw_clear_rx_ready(unsigned char endpoint)
747
{
748
    log_printf(USBLOG_HW_DATA, "Clear endpoint buffer\n");
749
    usbhw_reg_write(USB_EP0_RX_CTRL + (endpoint * EP_STRIDE), 1 << USB_EP1_RX_CTRL_RX_ACCEPT_SHIFT);
750
}
751
//-----------------------------------------------------------------
752
// usbhw_has_tx_space: Is there space in the tx buffer
753
//-----------------------------------------------------------------
754
int usbhw_has_tx_space(unsigned char endpoint)
755
{
756
    return (usbhw_reg_read(USB_EP0_STS + (endpoint * EP_STRIDE)) & (1 << USB_EP1_STS_TX_BUSY_SHIFT)) ? 0 : 1;
757
}
758
//-----------------------------------------------------------------
759
// usbhw_load_tx_buffer: Load tx buffer & start transfer (non-blocking) 
760
//-----------------------------------------------------------------
761
int usbhw_load_tx_buffer(unsigned char endpoint, unsigned char *data, int count)
762
{
763
    int i;
764
    unsigned int word_data = 0;
765
 
766
    for (i=0;i<count;i++)
767
        usbhw_reg_write(USB_EP0_DATA + (endpoint * EP_STRIDE), *data++);
768
 
769
    // Start transmit
770
    word_data = (1 << USB_EP0_TX_CTRL_TX_START_SHIFT) | count;
771
    usbhw_reg_write(USB_EP0_TX_CTRL + (endpoint * EP_STRIDE), word_data);
772
 
773
    log_printf(USBLOG_HW_DATA, "Tx %d bytes\n", count);
774
 
775
    return count;
776
}
777
//-----------------------------------------------------------------
778
// usbhw_write_tx_byte: Write a byte to Tx buffer (don't send yet)
779
//-----------------------------------------------------------------
780
void usbhw_write_tx_byte(unsigned char endpoint, unsigned char data)
781
{
782
    usbhw_reg_write(USB_EP0_DATA + (endpoint * EP_STRIDE), data);
783
    _tx_count[endpoint]++;
784
}
785
//-----------------------------------------------------------------
786
// usbhw_start_tx: Start a tx packet with data loaded into endpoint
787
//-----------------------------------------------------------------
788
void usbhw_start_tx(unsigned char endpoint)
789
{
790
    // Start transmit
791
    unsigned int word_data = (1 << USB_EP0_TX_CTRL_TX_START_SHIFT) | _tx_count[endpoint];
792
    usbhw_reg_write(USB_EP0_TX_CTRL + (endpoint * EP_STRIDE), word_data);
793
 
794
    log_printf(USBLOG_HW_DATA, "Tx %d bytes\n", _tx_count[endpoint]);
795
 
796
    _tx_count[endpoint] = 0;
797
}
798
//-----------------------------------------------------------------
799
// usbhw_control_endpoint_stall:
800
//-----------------------------------------------------------------
801
void usbhw_control_endpoint_stall(void)
802
{
803
    log_printf(USBLOG_ERR, "Error, send EP stall!\n");
804
    usbhw_set_endpoint_stall(0);
805
 
806
    // Control endpoint stalls are self clearing
807
    _endpoint_stalled[0] = 0;
808
}
809
//-----------------------------------------------------------------
810
// usbhw_control_endpoint_ack:
811
//-----------------------------------------------------------------
812
void usbhw_control_endpoint_ack(void)
813
{
814
    // Send ZLP on EP0
815
    log_printf(USBLOG_HW_DATA, "Send ZLP\n");
816
    usbhw_reg_write(USB_EP0_TX_CTRL, (1 << USB_EP0_TX_CTRL_TX_START_SHIFT));
817
 
818
    log_printf(USBLOG_HW_DATA, "Tx [ZLP/ACK]\n");
819
 
820
    while (!usbhw_has_tx_space(0))
821
        ;
822
 
823
    log_printf(USBLOG_HW_DATA, "Tx complete\n");
824
}
825
//-----------------------------------------------------------------
826
// usbhw_get_frame_number:
827
//-----------------------------------------------------------------
828
unsigned short usbhw_get_frame_number(void)
829
{
830
    return (usbhw_reg_read(USB_FUNC_STAT) >> USB_FUNC_STAT_FRAME_SHIFT) & USB_FUNC_STAT_FRAME_MASK;
831
}
832
 
833
//-----------------------------------------------------------------
834
// usbhw_timer_now:
835
//-----------------------------------------------------------------
836
t_time usbhw_timer_now(void)
837
{
838
    return timer_now();
839
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.