Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/****************************************************************************
* arch/arm/src/stm32/stm32_usbdev.c
*
* Copyright (C) 2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/arch.h>
#include <nuttx/usb.h>
#include <nuttx/usbdev.h>
#include <nuttx/usbdev_trace.h>
#include <arch/irq.h>
#include "up_arch.h"
#include "stm32_internal.h"
#include "stm32_usbdev.h"
#if defined(CONFIG_USBDEV) && defined(CONFIG_STM32_USB)
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Configuration ************************************************************/
#ifndef CONFIG_USBDEV_EP0_MAXSIZE
# define CONFIG_USBDEV_EP0_MAXSIZE 64
#endif
#ifndef CONFIG_USBDEV_MAXPOWER
# define CONFIG_USBDEV_MAXPOWER 100 /* mA */
#endif
#define USB_SLOW_INT USBDEV_DEVINT_EPSLOW
#define USB_DEVSTATUS_INT USBDEV_DEVINT_DEVSTAT
#ifdef CONFIG_STM32_USBDEV_EPFAST_INTERRUPT
# define USB_FAST_INT USBDEV_DEVINT_EPFAST
#else
# define USB_FAST_INT 0
#endif
#ifndef CONFIG_USB_PRI
# define CONFIG_USB_PRI 2
#endif
/* Extremely detailed register debug that you would normally never want
* enabled.
*/
#ifndef CONFIG_DEBUG
# undef CONFIG_STM32_USBDEV_REGDEBUG
#endif
/* Initial interrupt mask */
#define STM32_CNTR_SETUP (USB_CNTR_RESETM|USB_CNTR_SUSPM|USB_CNTR_WKUPM|USB_CNTR_CTRM)
/* Endpoint identifiers. The STM32 supports up to 16 mono-directional or 8
* bidirectional endpoints. However, when you take into account PMA buffer
* usage (see below) and the fact that EP0 is bidirectional, then there is
* a functional limitation of EP0 + 5 mono-directional endpoints = 6.
*/
#define STM32_NENDPOINTS (8)
#define EP0 (0)
#define EP1 (1)
#define EP2 (2)
#define EP3 (3)
#define EP4 (4)
#define EP5 (5)
#define EP6 (6)
#define EP7 (7)
#define STM32_ENDP_BIT(ep) (1 << (ep))
#define STM32_ENDP_ALLSET 0xff
/* Packet sizes. We us a fixed 64 max packet size for all endpoint types */
#define STM32_MAXPACKET_SHIFT (6)
#define STM32_MAXPACKET_SIZE (1 << (STM32_MAXPACKET_SHIFT))
#define STM32_MAXPACKET_MASK (STM32_MAXPACKET_SIZE-1)
#define STM32_EP0MAXPACKET STM32_MAXPACKET_SIZE
/* Buffer description table. We assume that USB has excluse use of the buffer
* table. The buffer table is positioned at the beginning of the 512-byte
* CAN/USB memory. We will use the first STM32_NENDPOINTS*4 words for the buffer
* table.
*/
#define STM32_BTABLE_ADDRESS (0x00)
#define STM32_BTABLE_SIZE (0x18)
/* Buffer layout. Assume that all buffers are 64-bytes (maxpacketsize), then
* we have space for only 7 buffers; endpoint 0 will require two buffers, leaving
* 5 for other endpoints.
*/
#define STM32_BUFFER_START STM32_BTABLE_SIZE
#define STM32_EP0_RXADDR STM32_BUFFER_START
#define STM32_EP0_TXADDR (STM32_EP0_RXADDR+STM32_EP0MAXPACKET)
#define STM32_BUFFER_EP0 0x03
#define STM32_NBUFFERS 7
#define STM32_BUFFER_BIT(bn) (1 << (bn))
#define STM32_BUFFER_ALLSET 0x7f
#define STM32_BUFNO2BUF(bn) (STM32_BUFFER_START+((bn)<<STM32_MAXPACKET_SHIFT))
/* USB-related masks */
#define REQRECIPIENT_MASK (USB_REQ_TYPE_MASK | USB_REQ_RECIPIENT_MASK)
/* Endpoint rister masks (handling toggle fields) */
#define EPR_NOTOG_MASK (USB_EPR_CTR_RX | USB_EPR_SETUP | USB_EPR_EPTYPE_MASK |\
USB_EPR_EP_KIND | USB_EPR_CTR_TX | USB_EPR_EA_MASK)
#define EPR_TXDTOG_MASK (USB_EPR_STATTX_MASK | EPR_NOTOG_MASK)
#define EPR_RXDTOG_MASK (USB_EPR_STATRX_MASK | EPR_NOTOG_MASK)
/* Request queue operations *************************************************/
#define stm32_rqempty(ep) ((ep)->head == NULL)
#define stm32_rqpeek(ep) ((ep)->head)
/* USB trace ****************************************************************/
/* Trace error codes */
#define STM32_TRACEERR_ALLOCFAIL 0x0001
#define STM32_TRACEERR_BADCLEARFEATURE 0x0002
#define STM32_TRACEERR_BADDEVGETSTATUS 0x0003
#define STM32_TRACEERR_BADEPGETSTATUS 0x0004
#define STM32_TRACEERR_BADEPNO 0x0005
#define STM32_TRACEERR_BADEPTYPE 0x0006
#define STM32_TRACEERR_BADGETCONFIG 0x0007
#define STM32_TRACEERR_BADGETSETDESC 0x0008
#define STM32_TRACEERR_BADGETSTATUS 0x0009
#define STM32_TRACEERR_BADSETADDRESS 0x000a
#define STM32_TRACEERR_BADSETCONFIG 0x000b
#define STM32_TRACEERR_BADSETFEATURE 0x000c
#define STM32_TRACEERR_BINDFAILED 0x000d
#define STM32_TRACEERR_DISPATCHSTALL 0x000e
#define STM32_TRACEERR_DRIVER 0x000f
#define STM32_TRACEERR_DRIVERREGISTERED 0x0010
#define STM32_TRACEERR_EP0SETUPSTALLED 0x0011
#define STM32_TRACEERR_EPBUFFER 0x0012
#define STM32_TRACEERR_EPDISABLED 0x0013
#define STM32_TRACEERR_EPOUTNULLPACKET 0x0014
#define STM32_TRACEERR_EPRESERVE 0x0015
#define STM32_TRACEERR_INVALIDCTRLREQ 0x0016
#define STM32_TRACEERR_INVALIDPARMS 0x0017
#define STM32_TRACEERR_IRQREGISTRATION 0x0018
#define STM32_TRACEERR_NOTCONFIGURED 0x0019
#define STM32_TRACEERR_REQABORTED 0x001a
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/* Trace interrupt codes */
#define STM32_TRACEINTID_CLEARFEATURE 0x0001
#define STM32_TRACEINTID_DEVGETSTATUS 0x0002
#define STM32_TRACEINTID_DISPATCH 0x0003
#define STM32_TRACEINTID_EP0SETUPSETADDRESS 0x0004
#define STM32_TRACEINTID_EPGETSTATUS 0x0005
#define STM32_TRACEINTID_EPIN 0x0006
#define STM32_TRACEINTID_EPINQEMPTY 0x0007
#define STM32_TRACEINTID_EPOUT 0x0008
#define STM32_TRACEINTID_EPOUTPENDING 0x0009
#define STM32_TRACEINTID_EPOUTQEMPTY 0x000a
#define STM32_TRACEINTID_ESOF 0x000b
#define STM32_TRACEINTID_GETCONFIG 0x000c
#define STM32_TRACEINTID_GETSETDESC 0x000d
#define STM32_TRACEINTID_GETSETIF 0x000e
#define STM32_TRACEINTID_GETSTATUS 0x000f
#define STM32_TRACEINTID_HPINTERRUPT 0x0010
#define STM32_TRACEINTID_IFGETSTATUS 0x0011
#define STM32_TRACEINTID_LPCTR 0x0012
#define STM32_TRACEINTID_LPINTERRUPT 0x0013
#define STM32_TRACEINTID_NOSTDREQ 0x0014
#define STM32_TRACEINTID_RESET 0x0015
#define STM32_TRACEINTID_SETCONFIG 0x0016
#define STM32_TRACEINTID_SETFEATURE 0x0017
#define STM32_TRACEINTID_SUSP 0x0018
#define STM32_TRACEINTID_SYNCHFRAME 0x0019
#define STM32_TRACEINTID_WKUP 0x001a
/* Ever-present MIN and MAX macros */
#ifndef MIN
# define MIN(a,b) (a < b ? a : b)
#endif
#ifndef MAX
# define MAX(a,b) (a > b ? a : b)
#endif
/****************************************************************************
* Private Type Definitions
****************************************************************************/
/* The various states of a control pipe */
enum stm32_devstate_e
{
DEVSTATE_INIT,
DEVSTATE_RDREQUEST, /* Read request in progress */
DEVSTATE_WRREQUEST, /* Write request in progress */
DEVSTATE_IDLE, /* No transfer in progress */
DEVSTATE_STALLED /* We are stalled */
};
/* Resume states */
enum stm32_rsmstate_e
{
RSMSTATE_IDLE = 0, /* Device is either fully suspended or running */
RSMSTATE_STARTED, /* Resume sequence has been started */
RSMSTATE_WAITING /* Waiting (on ESOFs) for end of sequence */
};
union wb_u
{
uint16 w;
ubyte b[2];
};
/* A container for a request so that the request make be retained in a list */
struct stm32_req_s
{
struct usbdev_req_s req; /* Standard USB request */
struct stm32_req_s *flink; /* Supports a singly linked list */
};
/* This is the internal representation of an endpoint */
struct stm32_ep_s
{
/* Common endpoint fields. This must be the first thing defined in the
* structure so that it is possible to simply cast from struct usbdev_ep_s
* to struct stm32_ep_s.
*/
struct usbdev_ep_s ep; /* Standard endpoint structure */
/* STR71X-specific fields */
struct stm32_usbdev_s *dev; /* Reference to private driver data */
struct stm32_req_s *head; /* Request list for this endpoint */
struct stm32_req_s *tail;
ubyte bufno; /* Allocated buffer number */
ubyte stalled:1; /* 1: Endpoint is stalled */
ubyte halted:1; /* 1: Endpoint feature halted */
ubyte txbusy:1; /* 1: TX endpoint FIFO full */
ubyte txnullpkt:1; /* Null packet needed at end of transfer */
};
struct stm32_usbdev_s
{
/* Common device fields. This must be the first thing defined in the
* structure so that it is possible to simply cast from struct usbdev_s
* to structstm32_usbdev_s.
*/
struct usbdev_s usbdev;
/* The bound device class driver */
struct usbdevclass_driver_s *driver;
/* STM32-specific fields */
struct usb_ctrlreq_s ctrl; /* Last EP0 request */
ubyte devstate; /* Driver state (see enum stm32_devstate_e) */
ubyte rsmstate; /* Resume state (see enum stm32_rsmstate_e) */
ubyte nesofs; /* ESOF counter (for resume support) */
ubyte rxpending:1; /* 1: OUT data in PMA, but no read requests */
ubyte selfpowered:1; /* 1: Device is self powered */
ubyte epavail; /* Bitset of available endpoints */
ubyte bufavail; /* Bitset of available buffers */
uint16 rxstatus; /* Saved during interrupt processing */
uint16 txstatus; /* " " " " " " " " */
uint16 imask; /* Current interrupt mask */
/* The endpoint list */
struct stm32_ep_s eplist[STM32_NENDPOINTS];
};
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/* Register operations ******************************************************/
#if defined(CONFIG_STM32_USBDEV_REGDEBUG) && defined(CONFIG_DEBUG)
static uint16 stm32_getreg(uint32 addr);
static void stm32_putreg(uint16 val, uint32 addr);
#else
# define stm32_getreg(addr) getreg16(addr)
# define stm32_putreg(val,addr) putreg16(val,addr)
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
#endif
/* Low-Level Helpers ********************************************************/
static inline void
stm32_epsettxcount(ubyte epno, uint16 count);
static inline void
stm32_seteptxaddr(ubyte epno, ubyte addr);
static void stm32_epsetrxcount(ubyte epno, uint16 count);
static inline void
stm32_seteprxaddr(ubyte epno, ubyte addr);
static inline void
stm32_setepaddress(ubyte epno, ubyte addr);
static inline void
stm32_seteptype(ubyte epno, uint16 type);
static inline void
stm32_seteptxaddr(ubyte epno, ubyte addr);
static inline void
stm32_setstatusout(ubyte epno);
static inline void
stm32_clrstatusout(ubyte epno);
static void stm32_clrrxdtog(ubyte epno);
static void stm32_clrtxdtog(ubyte epno);
static void stm32_clrepctrrx(ubyte epno);
static void stm32_clrepctrtx(ubyte epno);
static void stm32_seteptxstatus(ubyte epno, uint16 state);
static void stm32_seteprxstatus(ubyte epno, uint16 state);
static inline uint16
stm32_geteptxstatus(ubyte epno);
static inline uint16
stm32_geteprxstatus(ubyte epno);
static uint16 stm32_eptxstalled(ubyte epno);
static uint16 stm32_eprxstalled(ubyte epno);
static void stm32_suspend(struct stm32_usbdev_s *priv);
static void stm32_initresume(struct stm32_usbdev_s *priv);
static void stm32_esofpoll(struct stm32_usbdev_s *priv) ;
/* Request Helpers **********************************************************/
static void stm32_copytopma(const ubyte *buffer, uint16 pmaoffset, uint16 nbytes);
static inline void
stm32_copyfrompma(ubyte *buffer, uint16 pmaoffset, uint16 nbytes);
static struct stm32_req_s *
stm32_rqdequeue(struct stm32_ep_s *privep);
static void stm32_rqenqueue(struct stm32_ep_s *privep,
struct stm32_req_s *req);
static inline void
stm32_abortrequest(struct stm32_ep_s *privep,
struct stm32_req_s *privreq, sint16 result);
static void stm32_reqcomplete(struct stm32_ep_s *privep, sint16 result);
static void stm32_epwrite(struct stm32_usbdev_s *buf, struct stm32_ep_s *privep,
const ubyte *data, uint32 nbytes);
static int stm32_wrrequest(struct stm32_usbdev_s *priv, struct stm32_ep_s *privep);
static int stm32_rdrequest(struct stm32_usbdev_s *priv, struct stm32_ep_s *privep);
/* Interrupt level processing ***********************************************/
static int stm32_dispatchrequest(struct stm32_usbdev_s *priv);
static void stm32_ep0post(struct stm32_usbdev_s *priv);
static void stm32_ep0setup(struct stm32_usbdev_s *priv);
static void stm32_ep0out(struct stm32_usbdev_s *priv, struct stm32_ep_s *privep);
static void stm32_ep0in(struct stm32_usbdev_s *priv);
static void stm32_setdevaddr(struct stm32_usbdev_s *priv, ubyte value);
static void stm32_lptransfer(struct stm32_usbdev_s *priv);
static int stm32_hpinterrupt(int irq, void *context);
static int stm32_lpinterrupt(int irq, void *context);
/* Endpoint helpers *********************************************************/
static inline struct stm32_ep_s *
stm32_reserveep(struct stm32_usbdev_s *priv, ubyte epset);
static inline void
stm32_unreserveep(struct stm32_usbdev_s *priv,
struct stm32_ep_s *privep);
static inline boolean
stm32_epreserved(struct stm32_usbdev_s *priv, int epno);
static int stm32_allocpma(struct stm32_usbdev_s *priv);
static inline void
stm32_freepma(struct stm32_usbdev_s *priv,
struct stm32_ep_s *privep);
/* Endpoint operations ******************************************************/
static int stm32_epconfigure(struct usbdev_ep_s *ep,
const struct usb_epdesc_s *desc, boolean last);
static int stm32_epdisable(struct usbdev_ep_s *ep);
static struct usbdev_req_s *
stm32_epallocreq(struct usbdev_ep_s *ep);
static void stm32_epfreereq(struct usbdev_ep_s *ep,
struct usbdev_req_s *);
static int stm32_epsubmit(struct usbdev_ep_s *ep,
struct usbdev_req_s *req);
static int stm32_epcancel(struct usbdev_ep_s *ep,
struct usbdev_req_s *req);
static int stm32_epstall(struct usbdev_ep_s *ep, boolean resume);
/* USB device controller operations *****************************************/
static struct usbdev_ep_s *
stm32_allocep(struct usbdev_s *dev, ubyte epno, boolean in,
ubyte eptype);
static void stm32_freeep(struct usbdev_s *dev, struct usbdev_ep_s *ep);
static int stm32_getframe(struct usbdev_s *dev);
static int stm32_wakeup(struct usbdev_s *dev);
static int stm32_selfpowered(struct usbdev_s *dev, boolean selfpowered);
/* Initialization ***********************************************************/
static void stm32_reset(struct stm32_usbdev_s *priv);
static void stm32_hwreset(struct stm32_usbdev_s *priv);
/****************************************************************************
* Private Data
****************************************************************************/
/* Since there is only a single USB interface, all status information can be
* be simply retained in a single global instance.
*/
static struct stm32_usbdev_s g_usbdev;
static const struct usbdev_epops_s g_epops =
{
.configure = stm32_epconfigure,
.disable = stm32_epdisable,
.allocreq = stm32_epallocreq,
.freereq = stm32_epfreereq,
.submit = stm32_epsubmit,
.cancel = stm32_epcancel,
.stall = stm32_epstall,
};
static const struct usbdev_ops_s g_devops =
{
.allocep = stm32_allocep,
.freeep = stm32_freeep,
.getframe = stm32_getframe,
.wakeup = stm32_wakeup,
.selfpowered = stm32_selfpowered,
.pullup = stm32_usbpullup,
};
/****************************************************************************
* Public Data
****************************************************************************/
/****************************************************************************
* Private Private Functions
****************************************************************************/
/****************************************************************************
* Register Operations
****************************************************************************/
/****************************************************************************
* Name: stm32_getreg
****************************************************************************/
#if defined(CONFIG_STM32_USBDEV_REGDEBUG) && defined(CONFIG_DEBUG)
static uint16 stm32_getreg(uint32 addr)
{
static uint32 prevaddr = 0;
static uint16 preval = 0;
static uint32 count = 0;
/* Read the value from the register */
uint16 val = getreg16(addr);
/* Is this the same value that we read from the same register last time?
* Are we polling the register? If so, suppress some of the output.
*/
if (addr == prevaddr || val == preval)
{
if (count == 0xffffffff || ++count > 3)
{
if (count == 4)
{
lldbg("...\n");
}
return val;
}
}
/* No this is a new address or value */
else
{
/* Did we print "..." for the previous value? */
if (count > 3)
{
/* Yes.. then show how many times the value repeated */
lldbg("[repeats %d more times]\n", count-3);
}
/* Save the new address, value, and count */
prevaddr = addr;
preval = val;
count = 1;
}
/* Show the register value read */
lldbg("%08x->%08x\n", addr, val);
return val;
}
#endif
/****************************************************************************
* Name: stm32_putreg
****************************************************************************/
#if defined(CONFIG_STM32_USBDEV_REGDEBUG) && defined(CONFIG_DEBUG)
static void stm32_putreg(uint16 val, uint32 addr)
{
/* Show the register value being written */
lldbg("%08x<-%08x\n", addr, val);
/* Write the value */
putreg32(val, addr);
}
#endif
/****************************************************************************
* Name: stm32_checksetup
****************************************************************************/
#if defined(CONFIG_STM32_USBDEV_REGDEBUG) && defined(CONFIG_DEBUG)
static void stm32_checksetup(void)
{
uint32 cfgr = getreg32(STM32_RCC_CFGR);
uint32 apb1rstr = getreg32(STM32_RCC_APB1RSTR);
uint32 apb1enr = getreg32(STM32_RCC_APB1ENR);
lldbg("CFGR: %08x APB1RSTR: %08x APB1ENR: %08x\n", cfgr, apb1rstr, apb1enr);
if ((apb1rstr & RCC_APB1RSTR_USBRST) != 0 ||
(apb1enr & RCC_APB1ENR_USBEN) == 0)
{
lldbg("ERROR: USB is NOT setup correctly\n");
}
}
#endif
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/****************************************************************************
* Low-Level Helpers
****************************************************************************/
/****************************************************************************
* Name: stm32_epsettxcount
****************************************************************************/
static inline void stm32_epsettxcount(ubyte epno, uint16 count)
{
uint32 *epaddr = (uint32*)STM32_USB_COUNT_TX(epno);
*epaddr = count;
}
/****************************************************************************
* Name: stm32_seteptxaddr
****************************************************************************/
static inline void stm32_seteptxaddr(ubyte epno, ubyte addr)
{
uint32 *txaddr = (uint32*)STM32_USB_ADDR_TX(epno);
*txaddr = addr;
}
/****************************************************************************
* Name: stm32_epsetrxcount
****************************************************************************/
static void stm32_epsetrxcount(ubyte epno, uint16 count)
{
uint32 *epaddr = (uint32*)STM32_USB_COUNT_RX(epno);
uint16 nblocks;
if (count > 62)
{
/* Blocks of 32 */
nblocks = count >> 5;
if((count & 0x1f) == 0)
{
nblocks--;
}
*epaddr = (uint32)((nblocks << 10) | 0x8000);
}
else
{
/* Blocks of 2 */
nblocks = count >> 1;
if((count & 0x1) != 0)
{
nblocks++;
}
*epaddr = (uint32)(nblocks << 10);
}
}
/****************************************************************************
* Name: stm32_seteprxaddr
****************************************************************************/
static inline void stm32_seteprxaddr(ubyte epno, ubyte addr)
{
uint32 *rxaddr = (uint32*)STM32_USB_ADDR_RX(epno);
*rxaddr = addr;
}
/****************************************************************************
* Name: stm32_setepaddress
****************************************************************************/
static inline void stm32_setepaddress(ubyte epno, ubyte addr)
{
uint32 epaddr = STM32_USB_EPR(epno);
uint16 regval;
regval = stm32_getreg(epaddr);
regval &= EPR_NOTOG_MASK;
regval &= ~USB_EPR_EA_MASK;
regval |= USB_EPR_EA_SHIFT;
stm32_putreg(regval, epaddr);
}
/****************************************************************************
* Name: stm32_seteptype
****************************************************************************/
static inline void stm32_seteptype(ubyte epno, uint16 type)
{
uint32 epaddr = STM32_USB_EPR(epno);
uint16 regval;
regval = stm32_getreg(epaddr);
regval &= EPR_NOTOG_MASK;
regval &= ~USB_EPR_EPTYPE_MASK;
regval |= type;
stm32_putreg(regval, epaddr);
}
/****************************************************************************
* Name: stm32_setstatusout
****************************************************************************/
static inline void stm32_setstatusout(ubyte epno)
{
uint32 epaddr = STM32_USB_EPR(epno);
uint16 regval;
/* For a BULK endpoint the EP_KIND bit is used to enabled double buffering;
* for a CONTROL endpoint, it is set to indicatate that a status OUT
* transaction is expected. The bit is not used with out endpoint types.
*/
regval = stm32_getreg(epaddr);
regval &= EPR_NOTOG_MASK;
regval |= USB_EPR_EP_KIND;
stm32_putreg(regval, epaddr);
}
/****************************************************************************
* Name: stm32_clrstatusout
****************************************************************************/
static inline void stm32_clrstatusout(ubyte epno)
{
uint32 epaddr = STM32_USB_EPR(epno);
uint16 regval;
/* For a BULK endpoint the EP_KIND bit is used to enabled double buffering;
* for a CONTROL endpoint, it is set to indicatate that a status OUT
* transaction is expected. The bit is not used with out endpoint types.
*/
regval = stm32_getreg(epaddr);
regval &= EPR_NOTOG_MASK;
regval &= ~USB_EPR_EP_KIND;
stm32_putreg(regval, epaddr);
}
/****************************************************************************
* Name: stm32_clrrxdtog
****************************************************************************/
static void stm32_clrrxdtog(ubyte epno)
{
uint32 epaddr = STM32_USB_EPR(epno);
uint16 regval;
regval = stm32_getreg(epaddr);
if ((regval & USB_EPR_DTOG_RX) != 0)
{
regval &= EPR_NOTOG_MASK;
regval |= USB_EPR_DTOG_RX;
stm32_putreg(regval, epaddr);
}
}
/****************************************************************************
* Name: stm32_clrtxdtog
****************************************************************************/
static void stm32_clrtxdtog(ubyte epno)
{
uint32 epaddr = STM32_USB_EPR(epno);
uint16 regval;
regval = stm32_getreg(epaddr);
if ((regval & USB_EPR_DTOG_TX) != 0)
{
regval &= EPR_NOTOG_MASK;
regval |= USB_EPR_DTOG_TX;
stm32_putreg(regval, epaddr);
}
}
/****************************************************************************
* Name: stm32_clrepctrrx
****************************************************************************/
static void stm32_clrepctrrx(ubyte epno)
{
uint32 epaddr = STM32_USB_EPR(epno);
uint16 regval;
regval = stm32_getreg(epaddr);
regval &= EPR_NOTOG_MASK;
regval &= ~USB_EPR_CTR_RX;
stm32_putreg(regval, epaddr);
}
/****************************************************************************
* Name: stm32_clrepctrtx
****************************************************************************/
static void stm32_clrepctrtx(ubyte epno)
{
uint32 epaddr = STM32_USB_EPR(epno);
uint16 regval;
regval = stm32_getreg(epaddr);
regval &= EPR_NOTOG_MASK;
regval &= ~USB_EPR_CTR_TX;
stm32_putreg(regval, epaddr);
}
/****************************************************************************
* Name: stm32_geteptxstatus
****************************************************************************/
static inline uint16 stm32_geteptxstatus(ubyte epno)
{
return (uint16)(stm32_getreg(STM32_USB_EPR(epno)) & USB_EPR_STATTX_MASK);
}
/****************************************************************************
* Name: stm32_geteprxstatus
****************************************************************************/
static inline uint16 stm32_geteprxstatus(ubyte epno)
{
return (stm32_getreg(STM32_USB_EPR(epno)) & USB_EPR_STATRX_MASK);
}
/****************************************************************************
* Name: stm32_seteptxstatus
****************************************************************************/
static void stm32_seteptxstatus(ubyte epno, uint16 state)
{
uint32 epaddr = STM32_USB_EPR(epno);
uint16 regval;
regval = stm32_getreg(epaddr) & EPR_TXDTOG_MASK;
/* Toggle first bit */
if ((USB_EPR_STATTX_DTOG1 & state) != 0)
{
regval ^= USB_EPR_STATTX_DTOG1;
}
/* Toggle second bit */
if ((USB_EPR_STATTX_DTOG2 & state) != 0)
{
regval ^= USB_EPR_STATTX_DTOG2;
}
stm32_putreg(regval, epaddr);
}
/****************************************************************************
* Name: stm32_seteprxstatus
****************************************************************************/
static void stm32_seteprxstatus(ubyte epno, uint16 state)
{
uint32 epaddr = STM32_USB_EPR(epno);
uint16 regval;
regval = stm32_getreg(epaddr) & EPR_RXDTOG_MASK;
/* Toggle first bit */
if ((USB_EPR_STATTX_DTOG1 & state) != 0)
{
regval ^= USB_EPR_STATRX_DTOG1;
}
/* Toggle second bit */
if ((USB_EPR_STATTX_DTOG2 & state) != 0)
{
regval ^= USB_EPR_STATRX_DTOG2;
}
stm32_putreg(regval, epaddr);
}
/****************************************************************************
* Name: stm32_eptxstalled
****************************************************************************/
static inline uint16 stm32_eptxstalled(ubyte epno)
{
return (stm32_geteptxstatus(epno) == USB_EPR_STATTX_STALL);
}
/****************************************************************************
* Name: stm32_eprxstalled
****************************************************************************/
static inline uint16 stm32_eprxstalled(ubyte epno)
{
return (stm32_geteprxstatus(epno) == USB_EPR_STATRX_STALL);
}
/****************************************************************************
* Request Helpers
****************************************************************************/
/****************************************************************************
* Name: stm32_copytopma
****************************************************************************/
static void stm32_copytopma(const ubyte *buffer, uint16 pmaoffset, uint16 nbytes)
{
uint16 *dest;
uint16 ms;
uint16 ls;
int nwords = (nbytes + 1) >> 1;
int i;
/* Copy loop. Source=user buffer, Dest=packet memory */
dest = (uint16*)((pmaoffset << 1) + STM32_USBCANRAM_BASE);
for (i = nwords; i != 0; i--)
{
/* Read two bytes and pack into on 16-bit word */
ls = (uint16)(*buffer++);
ms = (uint16)(*buffer++);
*dest = ms << 8 | ls;
/* Source address increments by 2*sizeof(ubyte) = 2; Dest address
* increments by 2*sizeof(uint16) = 4.
*/
dest += 2;
}
}
/****************************************************************************
* Name: stm32_copyfrompma
****************************************************************************/
static inline void
stm32_copyfrompma(ubyte *buffer, uint16 pmaoffset, uint16 nbytes)
{
uint32 *src;
int nwords = (nbytes + 1) >> 1;
int i;
/* Copy loop. Source=packet memory, Dest=user buffer */
src = (uint32*)((pmaoffset << 1) + STM32_USBCANRAM_BASE);
for (i = nwords; i != 0; i--)
{
/* Copy 16-bits from packet memory to user buffer. */
*(uint16*)buffer = *src++;
/* Source address increments by 1*sizeof(uint32) = 4; Dest address
* increments by 2*sizeof(ubyte) = 2.
*/
buffer += 2;
}
}
/****************************************************************************
* Name: stm32_rqdequeue
****************************************************************************/
static struct stm32_req_s *stm32_rqdequeue(struct stm32_ep_s *privep)
{
struct stm32_req_s *ret = privep->head;
if (ret)
{
privep->head = ret->flink;
if (!privep->head)
{
privep->tail = NULL;
}
ret->flink = NULL;
}
return ret;
}
/****************************************************************************
* Name: stm32_rqenqueue
****************************************************************************/
static void stm32_rqenqueue(struct stm32_ep_s *privep,
struct stm32_req_s *req)
{
req->flink = NULL;
if (!privep->head)
{
privep->head = req;
privep->tail = req;
}
else
{
privep->tail->flink = req;
privep->tail = req;
}
}
/****************************************************************************
* Name: stm32_abortrequest
****************************************************************************/
static inline void
stm32_abortrequest(struct stm32_ep_s *privep, struct stm32_req_s *privreq, sint16 result)