+/rtos

git clone https://git.julia.blue/+/rtos

Vivek Malneediusb mouse testda8d6f3

master
198.1 KiB5488 linesraw
1//*****************************************************************************
2//
3// usb.c - Driver for the USB Interface.
4//
5// Copyright (c) 2007-2017 Texas Instruments Incorporated.  All rights reserved.
6// Software License Agreement
7//
8//   Redistribution and use in source and binary forms, with or without
9//   modification, are permitted provided that the following conditions
10//   are met:
11//
12//   Redistributions of source code must retain the above copyright
13//   notice, this list of conditions and the following disclaimer.
14//
15//   Redistributions in binary form must reproduce the above copyright
16//   notice, this list of conditions and the following disclaimer in the
17//   documentation and/or other materials provided with the
18//   distribution.
19//
20//   Neither the name of Texas Instruments Incorporated nor the names of
21//   its contributors may be used to endorse or promote products derived
22//   from this software without specific prior written permission.
23//
24// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35//
36// This is part of revision 2.1.4.178 of the Tiva Peripheral Driver Library.
37//
38//*****************************************************************************
39
40//*****************************************************************************
41//
42//! \addtogroup usb_api
43//! @{
44//
45//*****************************************************************************
46
47#include "tivaware/usb.h"
48#include "tivaware/hw_ints.h"
49#include "tivaware/hw_memmap.h"
50#include "tivaware/hw_sysctl.h"
51#include "tivaware/hw_types.h"
52#include "tivaware/hw_usb.h"
53#include "tivaware/rom.h"
54#include "tivaware/sysctl.h"
55#include "tivaware/udma.h"
56#include <stdbool.h>
57#include <stdint.h>
58
59#define ASSERT(X)
60
61//*****************************************************************************
62//
63// Amount to shift the RX interrupt sources by in the flags used in the
64// interrupt calls.
65//
66//*****************************************************************************
67#define USB_INTEP_RX_SHIFT 16
68
69//*****************************************************************************
70//
71// Amount to shift the RX endpoint status sources by in the flags used in the
72// calls.
73//
74//*****************************************************************************
75#define USB_RX_EPSTATUS_SHIFT 16
76
77//*****************************************************************************
78//
79// Converts from an endpoint specifier to the offset of the endpoint's
80// control/status registers.
81//
82//*****************************************************************************
83#define EP_OFFSET(Endpoint) (Endpoint - 0x10)
84
85//*****************************************************************************
86//
87// Sets one of the indexed registers.
88//
89// \param ui32Base specifies the USB module base address.
90// \param ui32Endpoint is the endpoint index to target for this write.
91// \param ui32IndexedReg is the indexed register to write to.
92// \param ui8Value is the value to write to the register.
93//
94// This function is used to access the indexed registers for each endpoint.
95// The only registers that are indexed are the FIFO configuration registers,
96// which are not used after configuration.
97//
98// \return None.
99//
100//*****************************************************************************
101static void _USBIndexWrite(uint32_t ui32Base, uint32_t ui32Endpoint,
102                           uint32_t ui32IndexedReg, uint32_t ui32Value,
103                           uint32_t ui32Size) {
104    uint32_t ui32Index;
105
106    //
107    // Check the arguments.
108    //
109    ASSERT(ui32Base == USB0_BASE);
110    ASSERT((ui32Endpoint == 0) || (ui32Endpoint == 1) || (ui32Endpoint == 2) ||
111           (ui32Endpoint == 3));
112    ASSERT((ui32Size == 1) || (ui32Size == 2));
113
114    //
115    // Save the old index in case it was in use.
116    //
117    ui32Index = HWREGB(ui32Base + USB_O_EPIDX);
118
119    //
120    // Set the index.
121    //
122    HWREGB(ui32Base + USB_O_EPIDX) = ui32Endpoint;
123
124    //
125    // Determine the size of the register value.
126    //
127    if (ui32Size == 1) {
128        //
129        // Set the value.
130        //
131        HWREGB(ui32Base + ui32IndexedReg) = ui32Value;
132    } else {
133        //
134        // Set the value.
135        //
136        HWREGH(ui32Base + ui32IndexedReg) = ui32Value;
137    }
138
139    //
140    // Restore the old index in case it was in use.
141    //
142    HWREGB(ui32Base + USB_O_EPIDX) = ui32Index;
143}
144
145//*****************************************************************************
146//
147// Reads one of the indexed registers.
148//
149// \param ui32Base specifies the USB module base address.
150// \param ui32Endpoint is the endpoint index to target for this write.
151// \param ui32IndexedReg is the indexed register to write to.
152// \param ui32Size is a value of 1 or 2 indicating the byte size of the read.
153//
154// This function is used internally to access the indexed registers for each
155// endpoint.  The only registers that are indexed are the FIFO configuration
156// registers, which are not used after configuration.
157//
158// \return The value in the register requested.
159//
160//*****************************************************************************
161static uint32_t _USBIndexRead(uint32_t ui32Base, uint32_t ui32Endpoint,
162                              uint32_t ui32IndexedReg, uint32_t ui32Size) {
163    uint8_t ui8Index;
164    uint32_t ui32Value;
165
166    //
167    // Check the arguments.
168    //
169    ASSERT(ui32Base == USB0_BASE);
170    ASSERT((ui32Endpoint == 0) || (ui32Endpoint == 1) || (ui32Endpoint == 2) ||
171           (ui32Endpoint == 3));
172    ASSERT((ui32Size == 1) || (ui32Size == 2));
173
174    //
175    // Save the old index in case it was in use.
176    //
177    ui8Index = HWREGB(ui32Base + USB_O_EPIDX);
178
179    //
180    // Set the index.
181    //
182    HWREGB(ui32Base + USB_O_EPIDX) = ui32Endpoint;
183
184    //
185    // Determine the size of the register value.
186    //
187    if (ui32Size == 1) {
188        //
189        // Get the value.
190        //
191        ui32Value = HWREGB(ui32Base + ui32IndexedReg);
192    } else {
193        //
194        // Get the value.
195        //
196        ui32Value = HWREGH(ui32Base + ui32IndexedReg);
197    }
198
199    //
200    // Restore the old index in case it was in use.
201    //
202    HWREGB(ui32Base + USB_O_EPIDX) = ui8Index;
203
204    //
205    // Return the register's value.
206    //
207    return (ui32Value);
208}
209
210//*****************************************************************************
211//
212//! Puts the USB bus in a suspended state.
213//!
214//! \param ui32Base specifies the USB module base address.
215//!
216//! When used in host mode, this function puts the USB bus in the suspended
217//! state.
218//!
219//! \note This function must only be called in host mode.
220//!
221//! \return None.
222//
223//*****************************************************************************
224void USBHostSuspend(uint32_t ui32Base) {
225    //
226    // Check the arguments.
227    //
228    ASSERT(ui32Base == USB0_BASE);
229
230    //
231    // Send the suspend signaling to the USB bus.
232    //
233    HWREGB(ui32Base + USB_O_POWER) |= USB_POWER_SUSPEND;
234}
235
236//*****************************************************************************
237//
238//! Handles the USB bus reset condition.
239//!
240//! \param ui32Base specifies the USB module base address.
241//! \param bStart specifies whether to start or stop signaling reset on the USB
242//! bus.
243//!
244//! When this function is called with the \e bStart parameter set to \b true,
245//! this function causes the start of a reset condition on the USB bus.
246//! The caller must then delay at least 20ms before calling this function
247//! again with the \e bStart parameter set to \b false.
248//!
249//! \note This function must only be called in host mode.
250//!
251//! \return None.
252//
253//*****************************************************************************
254void USBHostReset(uint32_t ui32Base, bool bStart) {
255    //
256    // Check the arguments.
257    //
258    ASSERT(ui32Base == USB0_BASE);
259
260    //
261    // Send a reset signal to the bus.
262    //
263    if (bStart) {
264        HWREGB(ui32Base + USB_O_POWER) |= USB_POWER_RESET;
265    } else {
266        HWREGB(ui32Base + USB_O_POWER) &= ~USB_POWER_RESET;
267    }
268}
269
270//*****************************************************************************
271//
272//! Enables or disables USB high-speed negotiation.
273//!
274//! \param ui32Base specifies the USB module base address.
275//! \param bEnable specifies whether to enable or disable high-speed
276//! negotiation.
277//!
278//! High-speed negotiations for both host and device mode are enabled when this
279//! function is called with the \e bEnable parameter set to \b true.  In device
280//! mode this causes the device to negotiate for high speed when the
281//! USB controller receives a reset from the host.  In host mode, the USB host
282//! enables high-speed negotiations when resetting the connected device.  If
283//! \e bEnable is set to \b false the controller only operates only in
284//! full-speed or low-speed.
285//!
286//! \b Example: Enable USB high-speed mode.
287//!
288//! \verbatim
289//! //
290//! // Enable USB high-speed mode.
291//! //
292//! USBHighSpeed(USB0_BASE, true);
293//! \endverbatim
294//!
295//! \note This feature is not available on all Tiva devices and should only be
296//! called when the USB is connected to an external ULPI PHY.  Please
297//! check the data sheet to determine if the USB controller can interface with
298//! a ULPI PHY.
299//!
300//! \return None.
301//
302//*****************************************************************************
303void USBHighSpeed(uint32_t ui32Base, bool bEnable) {
304    //
305    // Check the arguments.
306    //
307    ASSERT(ui32Base == USB0_BASE);
308
309    if (bEnable) {
310        //
311        // Enable high speed mode negotiations in hosts or device mode.
312        //
313        HWREGB(ui32Base + USB_O_POWER) |= USB_POWER_HSENAB;
314    } else {
315        //
316        // Enable high speed mode negotiations in hosts or device mode.
317        //
318        HWREGB(ui32Base + USB_O_POWER) &= ~USB_POWER_HSENAB;
319    }
320}
321
322//*****************************************************************************
323//
324//! Handles the USB bus resume condition.
325//!
326//! \param ui32Base specifies the USB module base address.
327//! \param bStart specifies if the USB controller is entering or leaving the
328//! resume signaling state.
329//!
330//! When in device mode, this function brings the USB controller out of the
331//! suspend state.  This call must first be made with the \e bStart parameter
332//! set to \b true to start resume signaling.  The device application must
333//! then delay at least 10ms but not more than 15ms before calling this
334//! function with the \e bStart parameter set to \b false.
335//!
336//! When in host mode, this function signals devices to leave the suspend
337//! state.  This call must first be made with the \e bStart parameter set to
338//! \b true to start resume signaling.  The host application must then delay
339//! at least 20ms before calling this function with the \e bStart parameter set
340//! to \b false.  This action causes the controller to complete the resume
341//! signaling on the USB bus.
342//!
343//! \note This function must only be called in host mode.
344//!
345//! \return None.
346//
347//*****************************************************************************
348void USBHostResume(uint32_t ui32Base, bool bStart) {
349    //
350    // Check the arguments.
351    //
352    ASSERT(ui32Base == USB0_BASE);
353
354    //
355    // Send a resume signal to the bus.
356    //
357    if (bStart) {
358        HWREGB(ui32Base + USB_O_POWER) |= USB_POWER_RESUME;
359    } else {
360        HWREGB(ui32Base + USB_O_POWER) &= ~USB_POWER_RESUME;
361    }
362}
363
364//*****************************************************************************
365//
366//! Returns the current speed of the USB device connected.
367//!
368//! \param ui32Base specifies the USB module base address.
369//!
370//! This function returns the current speed of the USB bus in host mode.
371//!
372//! \b Example: Get the USB connection speed.
373//!
374//! \verbatim
375//! //
376//! // Get the connection speed of the device connected to the USB controller.
377//! //
378//! USBHostSpeedGet(USB0_BASE);
379//! \endverbatim
380//!
381//! \note This function must only be called in host mode.
382//!
383//! \return Returns one of the following: \b USB_LOW_SPEED, \b USB_FULL_SPEED,
384//! \b USB_HIGH_SPEED, or \b USB_UNDEF_SPEED.
385//
386//*****************************************************************************
387uint32_t USBHostSpeedGet(uint32_t ui32Base) {
388    //
389    // Check the arguments.
390    //
391    ASSERT(ui32Base == USB0_BASE);
392
393    //
394    // If the Full Speed device bit is set, then this is a full speed device.
395    //
396    if (HWREGB(ui32Base + USB_O_POWER) & USB_POWER_HSMODE) {
397        return (USB_HIGH_SPEED);
398    }
399
400    //
401    // If the Full Speed device bit is set, then this is a full speed device.
402    //
403    if (HWREGB(ui32Base + USB_O_DEVCTL) & USB_DEVCTL_FSDEV) {
404        return (USB_FULL_SPEED);
405    }
406
407    //
408    // If the Low Speed device bit is set, then this is a low speed device.
409    //
410    if (HWREGB(ui32Base + USB_O_DEVCTL) & USB_DEVCTL_LSDEV) {
411        return (USB_LOW_SPEED);
412    }
413
414    //
415    // The device speed is not known.
416    //
417    return (USB_UNDEF_SPEED);
418}
419
420//*****************************************************************************
421//
422//! Returns the current speed of the USB controller in device mode.
423//!
424//! \param ui32Base specifies the USB module base address.
425//!
426//! This function returns the operating speed of the connection to the USB host
427//! controller.  This function returns either \b USB_HIGH_SPEED or
428//! \b USB_FULL_SPEED to indicate the connection speed in device mode.
429//!
430//! \b Example: Get the USB connection speed.
431//!
432//! \verbatim
433//! //
434//! // Get the connection speed of the USB controller.
435//! //
436//! USBDevSpeedGet(USB0_BASE);
437//! \endverbatim
438//!
439//! \note This function must only be called in device mode.
440//!
441//! \return Returns either \b USB_HIGH_SPEED or \b USB_FULL_SPEED.
442//
443//*****************************************************************************
444uint32_t USBDevSpeedGet(uint32_t ui32Base) {
445    //
446    // Check the arguments.
447    //
448    ASSERT(ui32Base == USB0_BASE);
449
450    //
451    // If the Full Speed device bit is set, then this is a full speed device.
452    //
453    if (HWREGB(ui32Base + USB_O_POWER) & USB_POWER_HSMODE) {
454        return (USB_HIGH_SPEED);
455    }
456
457    return (USB_FULL_SPEED);
458}
459
460//*****************************************************************************
461//
462//! Disables control interrupts on a specified USB controller.
463//!
464//! \param ui32Base specifies the USB module base address.
465//! \param ui32Flags specifies which control interrupts to disable.
466//!
467//! This function disables the control interrupts for the USB controller
468//! specified by the \e ui32Base parameter.  The \e ui32Flags parameter
469//! specifies which control interrupts to disable.  The flags passed in the
470//! \e ui32Flags parameters must be the definitions that start with
471//! \b USB_INTCTRL_* and not any other \b USB_INT flags.
472//!
473//! \return None.
474//
475//*****************************************************************************
476void USBIntDisableControl(uint32_t ui32Base, uint32_t ui32Flags) {
477    //
478    // Check the arguments.
479    //
480    ASSERT(ui32Base == USB0_BASE);
481    ASSERT((ui32Flags & ~(USB_INTCTRL_ALL)) == 0);
482
483    //
484    // If any general interrupts were disabled then write the general interrupt
485    // settings out to the hardware.
486    //
487    if (ui32Flags & USB_INTCTRL_STATUS) {
488        HWREGB(ui32Base + USB_O_IE) &= ~(ui32Flags & USB_INTCTRL_STATUS);
489    }
490
491    //
492    // Disable the power fault interrupt.
493    //
494    if (ui32Flags & USB_INTCTRL_POWER_FAULT) {
495        HWREG(ui32Base + USB_O_EPCIM) = 0;
496    }
497
498    //
499    // Disable the ID pin detect interrupt.
500    //
501    if (ui32Flags & USB_INTCTRL_MODE_DETECT) {
502        HWREG(USB0_BASE + USB_O_IDVIM) = 0;
503    }
504}
505
506//*****************************************************************************
507//
508//! Enables control interrupts on a specified USB controller.
509//!
510//! \param ui32Base specifies the USB module base address.
511//! \param ui32Flags specifies which control interrupts to enable.
512//!
513//! This function enables the control interrupts for the USB controller
514//! specified by the \e ui32Base parameter.  The \e ui32Flags parameter
515//! specifies which control interrupts to enable.  The flags passed in the
516//! \e ui32Flags parameters must be the definitions that start with
517//! \b USB_INTCTRL_* and not any other \b USB_INT flags.
518//!
519//! \return None.
520//
521//*****************************************************************************
522void USBIntEnableControl(uint32_t ui32Base, uint32_t ui32Flags) {
523    //
524    // Check the arguments.
525    //
526    ASSERT(ui32Base == USB0_BASE);
527    ASSERT((ui32Flags & (~USB_INTCTRL_ALL)) == 0);
528
529    //
530    // If any general interrupts were enabled, then write the general
531    // interrupt settings out to the hardware.
532    //
533    if (ui32Flags & USB_INTCTRL_STATUS) {
534        HWREGB(ui32Base + USB_O_IE) |= ui32Flags;
535    }
536
537    //
538    // Enable the power fault interrupt.
539    //
540    if (ui32Flags & USB_INTCTRL_POWER_FAULT) {
541        HWREG(ui32Base + USB_O_EPCIM) = USB_EPCIM_PF;
542    }
543
544    //
545    // Enable the ID pin detect interrupt.
546    //
547    if (ui32Flags & USB_INTCTRL_MODE_DETECT) {
548        HWREG(USB0_BASE + USB_O_IDVIM) = USB_IDVIM_ID;
549    }
550}
551
552//*****************************************************************************
553//
554//! Returns the control interrupt status on a specified USB controller.
555//!
556//! \param ui32Base specifies the USB module base address.
557//!
558//! This function reads control interrupt status for a USB controller.  This
559//! call returns the current status for control interrupts only, the endpoint
560//! interrupt status is retrieved by calling USBIntStatusEndpoint().  The bit
561//! values returned are compared against the \b USB_INTCTRL_* values.
562//!
563//! The following are the meanings of all \b USB_INCTRL_ flags and the modes
564//! for which they are valid.  These values apply to any calls to
565//! USBIntStatusControl(), USBIntEnableControl(), and USBIntDisableControl().
566//! Some of these flags are only valid in the following modes as indicated in
567//! the parentheses:  Host, Device, and OTG.
568//!
569//! - \b USB_INTCTRL_ALL - A full mask of all control interrupt sources.
570//! - \b USB_INTCTRL_VBUS_ERR - A VBUS error has occurred (Host Only).
571//! - \b USB_INTCTRL_SESSION - Session Start Detected on A-side of cable
572//!                            (OTG Only).
573//! - \b USB_INTCTRL_SESSION_END - Session End Detected (Device Only)
574//! - \b USB_INTCTRL_DISCONNECT - Device Disconnect Detected (Host Only)
575//! - \b USB_INTCTRL_CONNECT - Device Connect Detected (Host Only)
576//! - \b USB_INTCTRL_SOF - Start of Frame Detected.
577//! - \b USB_INTCTRL_BABBLE - USB controller detected a device signaling past
578//!                           the end of a frame (Host Only)
579//! - \b USB_INTCTRL_RESET - Reset signaling detected by device (Device Only)
580//! - \b USB_INTCTRL_RESUME - Resume signaling detected.
581//! - \b USB_INTCTRL_SUSPEND - Suspend signaling detected by device (Device
582//!                            Only)
583//! - \b USB_INTCTRL_MODE_DETECT - OTG cable mode detection has completed
584//!                                (OTG Only)
585//! - \b USB_INTCTRL_POWER_FAULT - Power Fault detected (Host Only)
586//!
587//! \note This call clears the source of all of the control status interrupts.
588//!
589//! \return Returns the status of the control interrupts for a USB controller.
590//
591//*****************************************************************************
592uint32_t USBIntStatusControl(uint32_t ui32Base) {
593    uint32_t ui32Status;
594
595    //
596    // Check the arguments.
597    //
598    ASSERT(ui32Base == USB0_BASE);
599
600    //
601    // Get the general interrupt status, these bits go into the upper 8 bits
602    // of the returned value.
603    //
604    ui32Status = HWREGB(ui32Base + USB_O_IS);
605
606    //
607    // Add the power fault status.
608    //
609    if (HWREG(ui32Base + USB_O_EPCISC) & USB_EPCISC_PF) {
610        //
611        // Indicate a power fault was detected.
612        //
613        ui32Status |= USB_INTCTRL_POWER_FAULT;
614
615        //
616        // Clear the power fault interrupt.
617        //
618        HWREGB(ui32Base + USB_O_EPCISC) |= USB_EPCISC_PF;
619    }
620
621    if (HWREG(USB0_BASE + USB_O_IDVISC) & USB_IDVRIS_ID) {
622        //
623        // Indicate an id detection.
624        //
625        ui32Status |= USB_INTCTRL_MODE_DETECT;
626
627        //
628        // Clear the id detection interrupt.
629        //
630        HWREG(USB0_BASE + USB_O_IDVISC) |= USB_IDVRIS_ID;
631    }
632
633    //
634    // Return the combined interrupt status.
635    //
636    return (ui32Status);
637}
638
639//*****************************************************************************
640//
641//! Disables endpoint interrupts on a specified USB controller.
642//!
643//! \param ui32Base specifies the USB module base address.
644//! \param ui32Flags specifies which endpoint interrupts to disable.
645//!
646//! This function disables endpoint interrupts for the USB controller specified
647//! by the \e ui32Base parameter.  The \e ui32Flags parameter specifies which
648//! endpoint interrupts to disable.  The flags passed in the \e ui32Flags
649//! parameters must be the definitions that start with \b USB_INTEP_* and not
650//! any other \b USB_INT flags.
651//!
652//! \return None.
653//
654//*****************************************************************************
655void USBIntDisableEndpoint(uint32_t ui32Base, uint32_t ui32Flags) {
656    //
657    // Check the arguments.
658    //
659    ASSERT(ui32Base == USB0_BASE);
660
661    //
662    // If any transmit interrupts were disabled, then write the transmit
663    // interrupt settings out to the hardware.
664    //
665    HWREGH(ui32Base + USB_O_TXIE) &=
666        ~(ui32Flags & (USB_INTEP_HOST_OUT | USB_INTEP_DEV_IN | USB_INTEP_0));
667
668    //
669    // If any receive interrupts were disabled, then write the receive
670    // interrupt settings out to the hardware.
671    //
672    HWREGH(ui32Base + USB_O_RXIE) &=
673        ~((ui32Flags & (USB_INTEP_HOST_IN | USB_INTEP_DEV_OUT)) >>
674          USB_INTEP_RX_SHIFT);
675}
676
677//*****************************************************************************
678//
679//! Enables endpoint interrupts on a specified USB controller.
680//!
681//! \param ui32Base specifies the USB module base address.
682//! \param ui32Flags specifies which endpoint interrupts to enable.
683//!
684//! This function enables endpoint interrupts for the USB controller specified
685//! by the \e ui32Base parameter.  The \e ui32Flags parameter specifies which
686//! endpoint interrupts to enable.  The flags passed in the \e ui32Flags
687//! parameters must be the definitions that start with \b USB_INTEP_* and not
688//! any other \b USB_INT flags.
689//!
690//! \return None.
691//
692//*****************************************************************************
693void USBIntEnableEndpoint(uint32_t ui32Base, uint32_t ui32Flags) {
694    //
695    // Check the arguments.
696    //
697    ASSERT(ui32Base == USB0_BASE);
698
699    //
700    // Enable any transmit endpoint interrupts.
701    //
702    HWREGH(ui32Base + USB_O_TXIE) |=
703        ui32Flags & (USB_INTEP_HOST_OUT | USB_INTEP_DEV_IN | USB_INTEP_0);
704
705    //
706    // Enable any receive endpoint interrupts.
707    //
708    HWREGH(ui32Base + USB_O_RXIE) |=
709        ((ui32Flags & (USB_INTEP_HOST_IN | USB_INTEP_DEV_OUT)) >>
710         USB_INTEP_RX_SHIFT);
711}
712
713//*****************************************************************************
714//
715//! Returns the endpoint interrupt status on a specified USB controller.
716//!
717//! \param ui32Base specifies the USB module base address.
718//!
719//! This function reads endpoint interrupt status for a USB controller.  This
720//! call returns the current status for endpoint interrupts only, the control
721//! interrupt status is retrieved by calling USBIntStatusControl().  The bit
722//! values returned are compared against the \b USB_INTEP_* values.
723//! These values are grouped into classes for \b USB_INTEP_HOST_* and
724//! \b USB_INTEP_DEV_* values to handle both host and device modes with all
725//! endpoints.
726//!
727//! \note This call clears the source of all of the endpoint interrupts.
728//!
729//! \return Returns the status of the endpoint interrupts for a USB controller.
730//
731//*****************************************************************************
732uint32_t USBIntStatusEndpoint(uint32_t ui32Base) {
733    uint32_t ui32Status;
734
735    //
736    // Check the arguments.
737    //
738    ASSERT(ui32Base == USB0_BASE);
739
740    //
741    // Get the transmit interrupt status.
742    //
743    ui32Status = HWREGH(ui32Base + USB_O_TXIS);
744    ui32Status |= (HWREGH(ui32Base + USB_O_RXIS) << USB_INTEP_RX_SHIFT);
745
746    //
747    // Return the combined interrupt status.
748    //
749    return (ui32Status);
750}
751
752//*****************************************************************************
753//
754//! Returns the interrupt number for a specified USB module.
755//!
756//! \param ui32Base is the base address of the USB module.
757//!
758//! This function returns the interrupt number for the USB module with the base
759//! address passed in the \e ui32Base parameter.
760//!
761//! \return Returns the USB interrupt number or 0 if the interrupt does not
762//! exist.
763//
764//*****************************************************************************
765/*
766static uint32_t _USBIntNumberGet(uint32_t ui32Base) {
767    uint32_t ui32Int;
768
769    if (CLASS_IS_TM4C123) {
770        ui32Int = INT_USB0_TM4C123;
771    } else if (CLASS_IS_TM4C129) {
772        ui32Int = INT_USB0_TM4C129;
773    } else {
774        ui32Int = 0;
775    }
776    return (ui32Int);
777}
778*/
779
780//*****************************************************************************
781//
782//! Registers an interrupt handler for the USB controller.
783//!
784//! \param ui32Base specifies the USB module base address.
785//! \param pfnHandler is a pointer to the function to be called when a USB
786//! interrupt occurs.
787//!
788//! This function registers the handler to be called when a USB interrupt
789//! occurs and enables the global USB interrupt in the interrupt controller.
790//! The specific desired USB interrupts must be enabled via a separate call to
791//! USBIntEnable().  It is the interrupt handler's responsibility to clear the
792//! interrupt sources via calls to USBIntStatusControl() and
793//! USBIntStatusEndpoint().
794//!
795//! \sa IntRegister() for important information about registering interrupt
796//! handlers.
797//!
798//! \return None.
799//
800//*****************************************************************************
801/*
802void USBIntRegister(uint32_t ui32Base, void (*pfnHandler)(void)) {
803    uint32_t ui32Int;
804
805    //
806    // Check the arguments.
807    //
808    ASSERT(ui32Base == USB0_BASE);
809
810    ui32Int = _USBIntNumberGet(ui32Base);
811
812    ASSERT(ui32Int != 0);
813
814    //
815    // Register the interrupt handler.
816    //
817    ROM_IntRegister(ui32Int, pfnHandler);
818
819    //
820    // Enable the USB interrupt.
821    //
822    ROM_IntEnable(ui32Int);
823}
824*/
825
826//*****************************************************************************
827//
828//! Unregisters an interrupt handler for the USB controller.
829//!
830//! \param ui32Base specifies the USB module base address.
831//!
832//! This function unregisters the interrupt handler.  This function also
833//! disables the USB interrupt in the interrupt controller.
834//!
835//! \sa IntRegister() for important information about registering or
836//! unregistering interrupt handlers.
837//!
838//! \return None.
839//
840//*****************************************************************************
841/*
842void USBIntUnregister(uint32_t ui32Base) {
843    uint32_t ui32Int;
844
845    //
846    // Check the arguments.
847    //
848    ASSERT(ui32Base == USB0_BASE);
849
850    ui32Int = _USBIntNumberGet(ui32Base);
851
852    ASSERT(ui32Int != 0);
853
854    //
855    // Disable the USB interrupt.
856    //
857    ROM_IntDisable(ui32Int);
858
859    //
860    // Unregister the interrupt handler.
861    //
862    ROM_IntUnregister(ui32Int);
863}
864*/
865
866//*****************************************************************************
867//
868//! Returns the current status of an endpoint.
869//!
870//! \param ui32Base specifies the USB module base address.
871//! \param ui32Endpoint is the endpoint to access.
872//!
873//! This function returns the status of a specified endpoint.  If any of these
874//! status bits must be cleared, then the USBDevEndpointStatusClear() or the
875//! USBHostEndpointStatusClear() functions must be called.
876//!
877//! The following are the status flags for host mode:
878//!
879//! - \b USB_HOST_IN_PID_ERROR - PID error on the specified endpoint.
880//! - \b USB_HOST_IN_NOT_COMP - The device failed to respond to an IN request.
881//! - \b USB_HOST_IN_STALL - A stall was received on an IN endpoint.
882//! - \b USB_HOST_IN_DATA_ERROR - There was a CRC or bit-stuff error on an IN
883//!   endpoint in Isochronous mode.
884//! - \b USB_HOST_IN_NAK_TO - NAKs received on this IN endpoint for more than
885//!   the specified timeout period.
886//! - \b USB_HOST_IN_ERROR - Failed to communicate with a device using this IN
887//!   endpoint.
888//! - \b USB_HOST_IN_FIFO_FULL - This IN endpoint's FIFO is full.
889//! - \b USB_HOST_IN_PKTRDY - Data packet ready on this IN endpoint.
890//! - \b USB_HOST_OUT_NAK_TO - NAKs received on this OUT endpoint for more than
891//!   the specified timeout period.
892//! - \b USB_HOST_OUT_NOT_COMP - The device failed to respond to an OUT
893//!   request.
894//! - \b USB_HOST_OUT_STALL - A stall was received on this OUT endpoint.
895//! - \b USB_HOST_OUT_ERROR - Failed to communicate with a device using this
896//!   OUT endpoint.
897//! - \b USB_HOST_OUT_FIFO_NE - This endpoint's OUT FIFO is not empty.
898//! - \b USB_HOST_OUT_PKTPEND - The data transfer on this OUT endpoint has not
899//!   completed.
900//! - \b USB_HOST_EP0_NAK_TO - NAKs received on endpoint zero for more than the
901//!   specified timeout period.
902//! - \b USB_HOST_EP0_ERROR - The device failed to respond to a request on
903//!   endpoint zero.
904//! - \b USB_HOST_EP0_IN_STALL - A stall was received on endpoint zero for an
905//!   IN transaction.
906//! - \b USB_HOST_EP0_IN_PKTRDY - Data packet ready on endpoint zero for an IN
907//!   transaction.
908//!
909//! The following are the status flags for device mode:
910//!
911//! - \b USB_DEV_OUT_SENT_STALL - A stall was sent on this OUT endpoint.
912//! - \b USB_DEV_OUT_DATA_ERROR - There was a CRC or bit-stuff error on an OUT
913//!   endpoint.
914//! - \b USB_DEV_OUT_OVERRUN - An OUT packet was not loaded due to a full FIFO.
915//! - \b USB_DEV_OUT_FIFO_FULL - The OUT endpoint's FIFO is full.
916//! - \b USB_DEV_OUT_PKTRDY - There is a data packet ready in the OUT
917//!   endpoint's FIFO.
918//! - \b USB_DEV_IN_NOT_COMP - A larger packet was split up, more data to come.
919//! - \b USB_DEV_IN_SENT_STALL - A stall was sent on this IN endpoint.
920//! - \b USB_DEV_IN_UNDERRUN - Data was requested on the IN endpoint and no
921//!   data was ready.
922//! - \b USB_DEV_IN_FIFO_NE - The IN endpoint's FIFO is not empty.
923//! - \b USB_DEV_IN_PKTPEND - The data transfer on this IN endpoint has not
924//!   completed.
925//! - \b USB_DEV_EP0_SETUP_END - A control transaction ended before Data End
926//!   condition was sent.
927//! - \b USB_DEV_EP0_SENT_STALL - A stall was sent on endpoint zero.
928//! - \b USB_DEV_EP0_IN_PKTPEND - The data transfer on endpoint zero has not
929//!   completed.
930//! - \b USB_DEV_EP0_OUT_PKTRDY - There is a data packet ready in endpoint
931//!   zero's OUT FIFO.
932//!
933//! \return The current status flags for the endpoint depending on mode.
934//
935//*****************************************************************************
936uint32_t USBEndpointStatus(uint32_t ui32Base, uint32_t ui32Endpoint) {
937    uint32_t ui32Status;
938
939    //
940    // Check the arguments.
941    //
942    ASSERT(ui32Base == USB0_BASE);
943    ASSERT((ui32Endpoint == USB_EP_0) || (ui32Endpoint == USB_EP_1) ||
944           (ui32Endpoint == USB_EP_2) || (ui32Endpoint == USB_EP_3) ||
945           (ui32Endpoint == USB_EP_4) || (ui32Endpoint == USB_EP_5) ||
946           (ui32Endpoint == USB_EP_6) || (ui32Endpoint == USB_EP_7));
947
948    //
949    // Get the TX portion of the endpoint status.
950    //
951    ui32Status = HWREGH(ui32Base + EP_OFFSET(ui32Endpoint) + USB_O_TXCSRL1);
952
953    //
954    // Get the RX portion of the endpoint status.
955    //
956    ui32Status |= ((HWREGH(ui32Base + EP_OFFSET(ui32Endpoint) + USB_O_RXCSRL1))
957                   << USB_RX_EPSTATUS_SHIFT);
958
959    //
960    // Return the endpoint status.
961    //
962    return (ui32Status);
963}
964
965//*****************************************************************************
966//
967//! Clears the status bits in this endpoint in host mode.
968//!
969//! \param ui32Base specifies the USB module base address.
970//! \param ui32Endpoint is the endpoint to access.
971//! \param ui32Flags are the status bits that are cleared.
972//!
973//! This function clears the status of any bits that are passed in the
974//! \e ui32Flags parameter.  The \e ui32Flags parameter can take the value
975//! returned from the USBEndpointStatus() call.
976//!
977//! \note This function must only be called in host mode.
978//!
979//! \return None.
980//
981//*****************************************************************************
982void USBHostEndpointStatusClear(uint32_t ui32Base, uint32_t ui32Endpoint,
983                                uint32_t ui32Flags) {
984    //
985    // Check the arguments.
986    //
987    ASSERT(ui32Base == USB0_BASE);
988    ASSERT((ui32Endpoint == USB_EP_0) || (ui32Endpoint == USB_EP_1) ||
989           (ui32Endpoint == USB_EP_2) || (ui32Endpoint == USB_EP_3) ||
990           (ui32Endpoint == USB_EP_4) || (ui32Endpoint == USB_EP_5) ||
991           (ui32Endpoint == USB_EP_6) || (ui32Endpoint == USB_EP_7));
992
993    //
994    // Clear the specified flags for the endpoint.
995    //
996    if (ui32Endpoint == USB_EP_0) {
997        HWREGB(ui32Base + USB_O_CSRL0) &= ~ui32Flags;
998    } else {
999        HWREGB(ui32Base + USB_O_TXCSRL1 + EP_OFFSET(ui32Endpoint)) &=
1000            ~ui32Flags;
1001        HWREGB(ui32Base + USB_O_RXCSRL1 + EP_OFFSET(ui32Endpoint)) &=
1002            ~(ui32Flags >> USB_RX_EPSTATUS_SHIFT);
1003    }
1004}
1005
1006//*****************************************************************************
1007//
1008//! Clears the status bits in this endpoint in device mode.
1009//!
1010//! \param ui32Base specifies the USB module base address.
1011//! \param ui32Endpoint is the endpoint to access.
1012//! \param ui32Flags are the status bits that are cleared.
1013//!
1014//! This function clears the status of any bits that are passed in the
1015//! \e ui32Flags parameter.  The \e ui32Flags parameter can take the value
1016//! returned from the USBEndpointStatus() call.
1017//!
1018//! \note This function must only be called in device mode.
1019//!
1020//! \return None.
1021//
1022//*****************************************************************************
1023void USBDevEndpointStatusClear(uint32_t ui32Base, uint32_t ui32Endpoint,
1024                               uint32_t ui32Flags) {
1025    //
1026    // Check the arguments.
1027    //
1028    ASSERT(ui32Base == USB0_BASE);
1029    ASSERT((ui32Endpoint == USB_EP_0) || (ui32Endpoint == USB_EP_1) ||
1030           (ui32Endpoint == USB_EP_2) || (ui32Endpoint == USB_EP_3) ||
1031           (ui32Endpoint == USB_EP_4) || (ui32Endpoint == USB_EP_5) ||
1032           (ui32Endpoint == USB_EP_6) || (ui32Endpoint == USB_EP_7));
1033
1034    //
1035    // If this is endpoint 0, then the bits have different meaning and map
1036    // into the TX memory location.
1037    //
1038    if (ui32Endpoint == USB_EP_0) {
1039        //
1040        // Set the Serviced RxPktRdy bit to clear the RxPktRdy.
1041        //
1042        if (ui32Flags & USB_DEV_EP0_OUT_PKTRDY) {
1043            HWREGB(ui32Base + USB_O_CSRL0) |= USB_CSRL0_RXRDYC;
1044        }
1045
1046        //
1047        // Set the serviced Setup End bit to clear the SetupEnd status.
1048        //
1049        if (ui32Flags & USB_DEV_EP0_SETUP_END) {
1050            HWREGB(ui32Base + USB_O_CSRL0) |= USB_CSRL0_SETENDC;
1051        }
1052
1053        //
1054        // Clear the Sent Stall status flag.
1055        //
1056        if (ui32Flags & USB_DEV_EP0_SENT_STALL) {
1057            HWREGB(ui32Base + USB_O_CSRL0) &= ~(USB_DEV_EP0_SENT_STALL);
1058        }
1059    } else {
1060        //
1061        // Clear out any TX flags that were passed in.  Only
1062        // USB_DEV_TX_SENT_STALL and USB_DEV_TX_UNDERRUN must be cleared.
1063        //
1064        HWREGB(ui32Base + USB_O_TXCSRL1 + EP_OFFSET(ui32Endpoint)) &=
1065            ~(ui32Flags & (USB_DEV_TX_SENT_STALL | USB_DEV_TX_UNDERRUN));
1066
1067        //
1068        // Clear out valid RX flags that were passed in.  Only
1069        // USB_DEV_RX_SENT_STALL, USB_DEV_RX_DATA_ERROR, and USB_DEV_RX_OVERRUN
1070        // must be cleared.
1071        //
1072        HWREGB(ui32Base + USB_O_RXCSRL1 + EP_OFFSET(ui32Endpoint)) &=
1073            ~((ui32Flags & (USB_DEV_RX_SENT_STALL | USB_DEV_RX_DATA_ERROR |
1074                            USB_DEV_RX_OVERRUN)) >>
1075              USB_RX_EPSTATUS_SHIFT);
1076    }
1077}
1078
1079//*****************************************************************************
1080//
1081//! Sets the value data toggle on an endpoint in host mode.
1082//!
1083//! \param ui32Base specifies the USB module base address.
1084//! \param ui32Endpoint specifies the endpoint to reset the data toggle.
1085//! \param bDataToggle specifies whether to set the state to DATA0 or DATA1.
1086//! \param ui32Flags specifies whether to set the IN or OUT endpoint.
1087//!
1088//! This function is used to force the state of the data toggle in host mode.
1089//! If the value passed in the \e bDataToggle parameter is \b false, then the
1090//! data toggle is set to the DATA0 state, and if it is \b true it is set to
1091//! the DATA1 state.  The \e ui32Flags parameter can be \b USB_EP_HOST_IN or
1092//! \b USB_EP_HOST_OUT to access the desired portion of this endpoint.  The
1093//! \e ui32Flags parameter is ignored for endpoint zero.
1094//!
1095//! \note This function must only be called in host mode.
1096//!
1097//! \return None.
1098//
1099//*****************************************************************************
1100void USBHostEndpointDataToggle(uint32_t ui32Base, uint32_t ui32Endpoint,
1101                               bool bDataToggle, uint32_t ui32Flags) {
1102    uint32_t ui32DataToggle;
1103
1104    //
1105    // Check the arguments.
1106    //
1107    ASSERT(ui32Base == USB0_BASE);
1108    ASSERT((ui32Endpoint == USB_EP_0) || (ui32Endpoint == USB_EP_1) ||
1109           (ui32Endpoint == USB_EP_2) || (ui32Endpoint == USB_EP_3) ||
1110           (ui32Endpoint == USB_EP_4) || (ui32Endpoint == USB_EP_5) ||
1111           (ui32Endpoint == USB_EP_6) || (ui32Endpoint == USB_EP_7));
1112
1113    //
1114    // The data toggle defaults to DATA0.
1115    //
1116    ui32DataToggle = 0;
1117
1118    //
1119    // See if the data toggle must be set to DATA1.
1120    //
1121    if (bDataToggle) {
1122        //
1123        // Select the data toggle bit based on the endpoint.
1124        //
1125        if (ui32Endpoint == USB_EP_0) {
1126            ui32DataToggle = USB_CSRH0_DT;
1127        } else if (ui32Flags == USB_EP_HOST_IN) {
1128            ui32DataToggle = USB_RXCSRH1_DT;
1129        } else {
1130            ui32DataToggle = USB_TXCSRH1_DT;
1131        }
1132    }
1133
1134    //
1135    // Set the data toggle based on the endpoint.
1136    //
1137    if (ui32Endpoint == USB_EP_0) {
1138        //
1139        // Set the write enable and the bit value for endpoint zero.
1140        //
1141        HWREGB(ui32Base + USB_O_CSRH0) = ((HWREGB(ui32Base + USB_O_CSRH0) &
1142                                           ~(USB_CSRH0_DTWE | USB_CSRH0_DT)) |
1143                                          (ui32DataToggle | USB_CSRH0_DTWE));
1144    } else if (ui32Flags == USB_EP_HOST_IN) {
1145        //
1146        // Set the Write enable and the bit value for an IN endpoint.
1147        //
1148        HWREGB(ui32Base + USB_O_RXCSRH1 + EP_OFFSET(ui32Endpoint)) =
1149            ((HWREGB(ui32Base + USB_O_RXCSRH1 + EP_OFFSET(ui32Endpoint)) &
1150              ~(USB_RXCSRH1_DTWE | USB_RXCSRH1_DT)) |
1151             (ui32DataToggle | USB_RXCSRH1_DTWE));
1152    } else {
1153        //
1154        // Set the Write enable and the bit value for an OUT endpoint.
1155        //
1156        HWREGB(ui32Base + USB_O_TXCSRH1 + EP_OFFSET(ui32Endpoint)) =
1157            ((HWREGB(ui32Base + USB_O_TXCSRH1 + EP_OFFSET(ui32Endpoint)) &
1158              ~(USB_TXCSRH1_DTWE | USB_TXCSRH1_DT)) |
1159             (ui32DataToggle | USB_TXCSRH1_DTWE));
1160    }
1161}
1162
1163//*****************************************************************************
1164//
1165//! Sets the data toggle on an endpoint to zero.
1166//!
1167//! \param ui32Base specifies the USB module base address.
1168//! \param ui32Endpoint specifies the endpoint to reset the data toggle.
1169//! \param ui32Flags specifies whether to access the IN or OUT endpoint.
1170//!
1171//! This function causes the USB controller to clear the data toggle for an
1172//! endpoint.  This call is not valid for endpoint zero and can be made with
1173//! host or device controllers.
1174//!
1175//! The \e ui32Flags parameter must be one of \b USB_EP_HOST_OUT,
1176//! \b USB_EP_HOST_IN, \b USB_EP_DEV_OUT, or \b USB_EP_DEV_IN.
1177//!
1178//! \return None.
1179//
1180//*****************************************************************************
1181void USBEndpointDataToggleClear(uint32_t ui32Base, uint32_t ui32Endpoint,
1182                                uint32_t ui32Flags) {
1183    //
1184    // Check the arguments.
1185    //
1186    ASSERT(ui32Base == USB0_BASE);
1187    ASSERT((ui32Endpoint == USB_EP_1) || (ui32Endpoint == USB_EP_2) ||
1188           (ui32Endpoint == USB_EP_3) || (ui32Endpoint == USB_EP_4) ||
1189           (ui32Endpoint == USB_EP_5) || (ui32Endpoint == USB_EP_6) ||
1190           (ui32Endpoint == USB_EP_7));
1191
1192    //
1193    // See if the transmit or receive data toggle must be cleared.
1194    //
1195    if (ui32Flags & (USB_EP_HOST_OUT | USB_EP_DEV_IN)) {
1196        HWREGB(ui32Base + USB_O_TXCSRL1 + EP_OFFSET(ui32Endpoint)) |=
1197            USB_TXCSRL1_CLRDT;
1198    } else {
1199        HWREGB(ui32Base + USB_O_RXCSRL1 + EP_OFFSET(ui32Endpoint)) |=
1200            USB_RXCSRL1_CLRDT;
1201    }
1202}
1203
1204//*****************************************************************************
1205//
1206//! Enables or disables ping tokens for an endpoint using high-speed control
1207//! transfers in host mode.
1208//!
1209//! \param ui32Base specifies the USB module base address.
1210//! \param ui32Endpoint specifies the endpoint to enable/disable ping tokens.
1211//! \param bEnable specifies whether enable or disable ping tokens.
1212//!
1213//! This function configures the USB controller to either send or not send ping
1214//! tokens during the data and status phase of high speed control transfers.
1215//! The only supported value for \e ui32Endpoint is \b USB_EP_0 because all
1216//! control transfers are handled using this endpoint.  If the \e bEnable is
1217//! \b true then ping tokens are enabled, if \b false then ping tokens are
1218//! disabled.  This must be used if the controller must support
1219//! communications with devices that do not support ping tokens in high speed
1220//! mode.
1221//!
1222//! \b Example: Disable ping transactions in host mode on endpoint 0.
1223//!
1224//! \verbatim
1225//! //
1226//! // Disable ping transaction on endpoint 0.
1227//! //
1228//! USBHostEndpointPing(USB0_BASE, USB_EP_0, false);
1229//! \endverbatim
1230//!
1231//! \note This function must only be called in host mode.
1232//!
1233//! \return None.
1234//
1235//*****************************************************************************
1236void USBHostEndpointPing(uint32_t ui32Base, uint32_t ui32Endpoint,
1237                         bool bEnable) {
1238    //
1239    // Check the arguments.
1240    //
1241    ASSERT(ui32Base == USB0_BASE);
1242    ASSERT((ui32Endpoint == USB_EP_0));
1243
1244    //
1245    // Handle the endpoint 0 case separately.
1246    //
1247    if (bEnable) {
1248        HWREGB(USB0_BASE + USB_O_CSRH0) &= ~USB_CSRH0_DISPING;
1249    } else {
1250        HWREGB(USB0_BASE + USB_O_CSRH0) |= USB_CSRH0_DISPING;
1251    }
1252}
1253
1254//*****************************************************************************
1255//
1256//! Stalls the specified endpoint in device mode.
1257//!
1258//! \param ui32Base specifies the USB module base address.
1259//! \param ui32Endpoint specifies the endpoint to stall.
1260//! \param ui32Flags specifies whether to stall the IN or OUT endpoint.
1261//!
1262//! This function causes the endpoint number passed in to go into a stall
1263//! condition.  If the \e ui32Flags parameter is \b USB_EP_DEV_IN, then the
1264//! stall is issued on the IN portion of this endpoint.  If the \e ui32Flags
1265//! parameter is \b USB_EP_DEV_OUT, then the stall is issued on the OUT portion
1266//! of this endpoint.
1267//!
1268//! \note This function must only be called in device mode.
1269//!
1270//! \return None.
1271//
1272//*****************************************************************************
1273void USBDevEndpointStall(uint32_t ui32Base, uint32_t ui32Endpoint,
1274                         uint32_t ui32Flags) {
1275    //
1276    // Check the arguments.
1277    //
1278    ASSERT(ui32Base == USB0_BASE);
1279    ASSERT((ui32Flags & ~(USB_EP_DEV_IN | USB_EP_DEV_OUT)) == 0);
1280    ASSERT((ui32Endpoint == USB_EP_0) || (ui32Endpoint == USB_EP_1) ||
1281           (ui32Endpoint == USB_EP_2) || (ui32Endpoint == USB_EP_3) ||
1282           (ui32Endpoint == USB_EP_4) || (ui32Endpoint == USB_EP_5) ||
1283           (ui32Endpoint == USB_EP_6) || (ui32Endpoint == USB_EP_7));
1284
1285    //
1286    // Determine how to stall this endpoint.
1287    //
1288    if (ui32Endpoint == USB_EP_0) {
1289        //
1290        // Perform a stall on endpoint zero.
1291        //
1292        HWREGB(ui32Base + USB_O_CSRL0) |= USB_CSRL0_STALL | USB_CSRL0_RXRDYC;
1293    } else if (ui32Flags == USB_EP_DEV_IN) {
1294        //
1295        // Perform a stall on an IN endpoint.
1296        //
1297        HWREGB(ui32Base + USB_O_TXCSRL1 + EP_OFFSET(ui32Endpoint)) |=
1298            USB_TXCSRL1_STALL;
1299    } else {
1300        //
1301        // Perform a stall on an OUT endpoint.
1302        //
1303        HWREGB(ui32Base + USB_O_RXCSRL1 + EP_OFFSET(ui32Endpoint)) |=
1304            USB_RXCSRL1_STALL;
1305    }
1306}
1307
1308//*****************************************************************************
1309//
1310//! Clears the stall condition on the specified endpoint in device mode.
1311//!
1312//! \param ui32Base specifies the USB module base address.
1313//! \param ui32Endpoint specifies which endpoint to remove the stall condition.
1314//! \param ui32Flags specifies whether to remove the stall condition from the
1315//! IN or the OUT portion of this endpoint.
1316//!
1317//! This function causes the endpoint number passed in to exit the stall
1318//! condition.  If the \e ui32Flags parameter is \b USB_EP_DEV_IN, then the
1319//! stall is cleared on the IN portion of this endpoint.  If the \e ui32Flags
1320//! parameter is \b USB_EP_DEV_OUT, then the stall is cleared on the OUT
1321//! portion of this endpoint.
1322//!
1323//! \note This function must only be called in device mode.
1324//!
1325//! \return None.
1326//
1327//*****************************************************************************
1328void USBDevEndpointStallClear(uint32_t ui32Base, uint32_t ui32Endpoint,
1329                              uint32_t ui32Flags) {
1330    //
1331    // Check the arguments.
1332    //
1333    ASSERT(ui32Base == USB0_BASE);
1334    ASSERT((ui32Endpoint == USB_EP_0) || (ui32Endpoint == USB_EP_1) ||
1335           (ui32Endpoint == USB_EP_2) || (ui32Endpoint == USB_EP_3) ||
1336           (ui32Endpoint == USB_EP_4) || (ui32Endpoint == USB_EP_5) ||
1337           (ui32Endpoint == USB_EP_6) || (ui32Endpoint == USB_EP_7));
1338    ASSERT((ui32Flags & ~(USB_EP_DEV_IN | USB_EP_DEV_OUT)) == 0);
1339
1340    //
1341    // Determine how to clear the stall on this endpoint.
1342    //
1343    if (ui32Endpoint == USB_EP_0) {
1344        //
1345        // Clear the stall on endpoint zero.
1346        //
1347        HWREGB(ui32Base + USB_O_CSRL0) &= ~USB_CSRL0_STALLED;
1348    } else if (ui32Flags == USB_EP_DEV_IN) {
1349        //
1350        // Clear the stall on an IN endpoint.
1351        //
1352        HWREGB(ui32Base + USB_O_TXCSRL1 + EP_OFFSET(ui32Endpoint)) &=
1353            ~(USB_TXCSRL1_STALL | USB_TXCSRL1_STALLED);
1354
1355        //
1356        // Reset the data toggle.
1357        //
1358        HWREGB(ui32Base + USB_O_TXCSRL1 + EP_OFFSET(ui32Endpoint)) |=
1359            USB_TXCSRL1_CLRDT;
1360    } else {
1361        //
1362        // Clear the stall on an OUT endpoint.
1363        //
1364        HWREGB(ui32Base + USB_O_RXCSRL1 + EP_OFFSET(ui32Endpoint)) &=
1365            ~(USB_RXCSRL1_STALL | USB_RXCSRL1_STALLED);
1366
1367        //
1368        // Reset the data toggle.
1369        //
1370        HWREGB(ui32Base + USB_O_RXCSRL1 + EP_OFFSET(ui32Endpoint)) |=
1371            USB_RXCSRL1_CLRDT;
1372    }
1373}
1374
1375//*****************************************************************************
1376//
1377//! Connects the USB controller to the bus in device mode.
1378//!
1379//! \param ui32Base specifies the USB module base address.
1380//!
1381//! This function causes the soft connect feature of the USB controller to
1382//! be enabled.  Call USBDevDisconnect() to remove the USB device from the bus.
1383//!
1384//! \note This function must only be called in device mode.
1385//!
1386//! \return None.
1387//
1388//*****************************************************************************
1389void USBDevConnect(uint32_t ui32Base) {
1390    //
1391    // Check the arguments.
1392    //
1393    ASSERT(ui32Base == USB0_BASE);
1394
1395    //
1396    // Enable connection to the USB bus.
1397    //
1398    HWREGB(ui32Base + USB_O_POWER) |= USB_POWER_SOFTCONN;
1399}
1400
1401//*****************************************************************************
1402//
1403//! Removes the USB controller from the bus in device mode.
1404//!
1405//! \param ui32Base specifies the USB module base address.
1406//!
1407//! This function causes the soft connect feature of the USB controller to
1408//! remove the device from the USB bus.  A call to USBDevConnect() is needed to
1409//! reconnect to the bus.
1410//!
1411//! \note This function must only be called in device mode.
1412//!
1413//! \return None.
1414//
1415//*****************************************************************************
1416void USBDevDisconnect(uint32_t ui32Base) {
1417    //
1418    // Check the arguments.
1419    //
1420    ASSERT(ui32Base == USB0_BASE);
1421
1422    //
1423    // Disable connection to the USB bus.
1424    //
1425    HWREGB(ui32Base + USB_O_POWER) &= (~USB_POWER_SOFTCONN);
1426}
1427
1428//*****************************************************************************
1429//
1430//! Sets the address in device mode.
1431//!
1432//! \param ui32Base specifies the USB module base address.
1433//! \param ui32Address is the address to use for a device.
1434//!
1435//! This function configures the device address on the USB bus.  This address
1436//! was likely received via a SET ADDRESS command from the host controller.
1437//!
1438//! \note This function must only be called in device mode.
1439//!
1440//! \return None.
1441//
1442//*****************************************************************************
1443void USBDevAddrSet(uint32_t ui32Base, uint32_t ui32Address) {
1444    //
1445    // Check the arguments.
1446    //
1447    ASSERT(ui32Base == USB0_BASE);
1448
1449    //
1450    // Set the function address in the correct location.
1451    //
1452    HWREGB(ui32Base + USB_O_FADDR) = (uint8_t)ui32Address;
1453}
1454
1455//*****************************************************************************
1456//
1457//! Returns the current device address in device mode.
1458//!
1459//! \param ui32Base specifies the USB module base address.
1460//!
1461//! This function returns the current device address.  This address was set
1462//! by a call to USBDevAddrSet().
1463//!
1464//! \note This function must only be called in device mode.
1465//!
1466//! \return The current device address.
1467//
1468//*****************************************************************************
1469uint32_t USBDevAddrGet(uint32_t ui32Base) {
1470    //
1471    // Check the arguments.
1472    //
1473    ASSERT(ui32Base == USB0_BASE);
1474
1475    //
1476    // Return the function address.
1477    //
1478    return (HWREGB(ui32Base + USB_O_FADDR));
1479}
1480
1481//*****************************************************************************
1482//
1483//! Sets the base configuration for a host endpoint.
1484//!
1485//! \param ui32Base specifies the USB module base address.
1486//! \param ui32Endpoint is the endpoint to access.
1487//! \param ui32MaxPayload is the maximum payload for this endpoint.
1488//! \param ui32NAKPollInterval is the either the NAK timeout limit or the
1489//! polling interval, depending on the type of endpoint.
1490//! \param ui32TargetEndpoint is the endpoint that the host endpoint is
1491//! targeting.
1492//! \param ui32Flags are used to configure other endpoint settings.
1493//!
1494//! This function sets the basic configuration for the transmit or receive
1495//! portion of an endpoint in host mode.  The \e ui32Flags parameter determines
1496//! some of the configuration while the other parameters provide the rest.  The
1497//! \e ui32Flags parameter determines whether this is an IN endpoint
1498//! (\b USB_EP_HOST_IN or \b USB_EP_DEV_IN) or an OUT endpoint
1499//! (\b USB_EP_HOST_OUT or \b USB_EP_DEV_OUT), whether this is a Full speed
1500//! endpoint (\b USB_EP_SPEED_FULL) or a Low speed endpoint
1501//! (\b USB_EP_SPEED_LOW).
1502//!
1503//! The \b USB_EP_MODE_ flags control the type of the endpoint.
1504//! - \b USB_EP_MODE_CTRL is a control endpoint.
1505//! - \b USB_EP_MODE_ISOC is an isochronous endpoint.
1506//! - \b USB_EP_MODE_BULK is a bulk endpoint.
1507//! - \b USB_EP_MODE_INT is an interrupt endpoint.
1508//!
1509//! The \e ui32NAKPollInterval parameter has different meanings based on the
1510//! \b USB_EP_MODE value and whether or not this call is being made for
1511//! endpoint zero or another endpoint.  For endpoint zero or any Bulk
1512//! endpoints, this value always indicates the number of frames to allow a
1513//! device to NAK before considering it a timeout.  If this endpoint is an
1514//! isochronous or interrupt endpoint, this value is the polling interval for
1515//! this endpoint.
1516//!
1517//! For interrupt endpoints, the polling interval is the number of frames
1518//! between interrupt IN requests to an endpoint and has a range of 1 to 255.
1519//! For isochronous endpoints this value represents a polling interval of
1520//! 2 ^ (\e ui32NAKPollInterval - 1) frames.  When used as a NAK timeout, the
1521//! \e ui32NAKPollInterval value specifies 2 ^ (\e ui32NAKPollInterval - 1)
1522//! frames before issuing a time out.
1523//!
1524//! There are two special time out values that can be specified when setting
1525//! the \e ui32NAKPollInterval value.  The first is \b MAX_NAK_LIMIT, which is
1526//! the maximum value that can be passed in this variable.  The other is
1527//! \b DISABLE_NAK_LIMIT, which indicates that there is no limit on the
1528//! number of NAKs.
1529//!
1530//! The \b USB_EP_DMA_MODE_ flags determine the type of DMA access to the
1531//! endpoint data FIFOs.  The choice of the DMA mode depends on how the DMA
1532//! controller is configured and how it is being used.  See the ``Using USB
1533//! with the uDMA Controller'' or the ''Using the integrated USB DMA
1534//! Controller'' section for more information on DMA configuration depending
1535//! on the type of DMA that is supported by the USB controller.
1536//!
1537//! When configuring the OUT portion of an endpoint, the \b USB_EP_AUTO_SET bit
1538//! is specified to cause the transmission of data on the USB bus to start
1539//! as soon as the number of bytes specified by \e ui32MaxPayload has been
1540//! written into the OUT FIFO for this endpoint.
1541//!
1542//! When configuring the IN portion of an endpoint, the \b USB_EP_AUTO_REQUEST
1543//! bit can be specified to trigger the request for more data once the FIFO has
1544//! been drained enough to fit \e ui32MaxPayload bytes.  The
1545//! \b USB_EP_AUTO_CLEAR bit can be used to clear the data packet ready flag
1546//! automatically once the data has been read from the FIFO.  If this option is
1547//! not used, this flag must be manually cleared via a call to
1548//! USBDevEndpointStatusClear() or USBHostEndpointStatusClear().
1549//!
1550//! For interrupt endpoints in low or full speed mode, the polling interval
1551//! (\e ui32NAKPollInterval) is the number of frames between interrupt IN
1552//! requests to an endpoint and has a range of 1 to 255.  For interrupt
1553//! endpoints in high speed mode the polling interval is
1554//! 2 ^ (\e ui32NAKPollInterval - 1) microframes between interrupt IN requests
1555//! to an endpoint and has a range of 1 to 16.
1556//!
1557//! \note This function must only be called in host mode.
1558//!
1559//! \return None.
1560//
1561//*****************************************************************************
1562void USBHostEndpointConfig(uint32_t ui32Base, uint32_t ui32Endpoint,
1563                           uint32_t ui32MaxPayload,
1564                           uint32_t ui32NAKPollInterval,
1565                           uint32_t ui32TargetEndpoint, uint32_t ui32Flags) {
1566    uint32_t ui32Register;
1567
1568    //
1569    // Check the arguments.
1570    //
1571    ASSERT(ui32Base == USB0_BASE);
1572    ASSERT((ui32Endpoint == USB_EP_0) || (ui32Endpoint == USB_EP_1) ||
1573           (ui32Endpoint == USB_EP_2) || (ui32Endpoint == USB_EP_3) ||
1574           (ui32Endpoint == USB_EP_4) || (ui32Endpoint == USB_EP_5) ||
1575           (ui32Endpoint == USB_EP_6) || (ui32Endpoint == USB_EP_7));
1576    ASSERT(ui32NAKPollInterval <= MAX_NAK_LIMIT);
1577
1578    //
1579    // Endpoint zero is configured differently than the other endpoints, so see
1580    // if this is endpoint zero.
1581    //
1582    if (ui32Endpoint == USB_EP_0) {
1583        //
1584        // Set the NAK timeout.
1585        //
1586        HWREGB(ui32Base + USB_O_NAKLMT) = ui32NAKPollInterval;
1587
1588        //
1589        // Set the transfer type information.
1590        //
1591        //
1592        // Set the speed of this endpoint.
1593        //
1594        if (ui32Flags & USB_EP_SPEED_HIGH) {
1595            HWREGB(ui32Base + USB_O_TYPE0) = USB_TYPE0_SPEED_HIGH;
1596        } else if (ui32Flags & USB_EP_SPEED_FULL) {
1597            HWREGB(ui32Base + USB_O_TYPE0) = USB_TYPE0_SPEED_FULL;
1598        } else {
1599            HWREGB(ui32Base + USB_O_TYPE0) = USB_TYPE0_SPEED_LOW;
1600        }
1601    } else {
1602        //
1603        // Start with the target endpoint.
1604        //
1605        ui32Register = ui32TargetEndpoint;
1606
1607        //
1608        // Set the speed for the device using this endpoint.
1609        //
1610        if (ui32Flags & USB_EP_SPEED_HIGH) {
1611            ui32Register |= USB_TXTYPE1_SPEED_HIGH;
1612        } else if (ui32Flags & USB_EP_SPEED_FULL) {
1613            ui32Register |= USB_TXTYPE1_SPEED_FULL;
1614        } else {
1615            ui32Register |= USB_TXTYPE1_SPEED_LOW;
1616        }
1617
1618        //
1619        // Set the protocol for the device using this endpoint.
1620        //
1621        switch (ui32Flags & USB_EP_MODE_MASK) {
1622        //
1623        // The bulk protocol is being used.
1624        //
1625        case USB_EP_MODE_BULK: {
1626            ui32Register |= USB_TXTYPE1_PROTO_BULK;
1627            break;
1628        }
1629
1630        //
1631        // The isochronous protocol is being used.
1632        //
1633        case USB_EP_MODE_ISOC: {
1634            ui32Register |= USB_TXTYPE1_PROTO_ISOC;
1635            break;
1636        }
1637
1638        //
1639        // The interrupt protocol is being used.
1640        //
1641        case USB_EP_MODE_INT: {
1642            ui32Register |= USB_TXTYPE1_PROTO_INT;
1643            break;
1644        }
1645
1646        //
1647        // The control protocol is being used.
1648        //
1649        case USB_EP_MODE_CTRL: {
1650            ui32Register |= USB_TXTYPE1_PROTO_CTRL;
1651            break;
1652        }
1653        }
1654
1655        //
1656        // See if the transmit or receive endpoint is being configured.
1657        //
1658        if (ui32Flags & USB_EP_HOST_OUT) {
1659            //
1660            // Set the transfer type information.
1661            //
1662            HWREGB(ui32Base + EP_OFFSET(ui32Endpoint) + USB_O_TXTYPE1) =
1663                ui32Register;
1664
1665            //
1666            // Set the NAK timeout or polling interval.
1667            //
1668            HWREGB(ui32Base + EP_OFFSET(ui32Endpoint) + USB_O_TXINTERVAL1) =
1669                ui32NAKPollInterval;
1670
1671            //
1672            // Set the Maximum Payload per transaction.
1673            //
1674            HWREGH(ui32Base + EP_OFFSET(ui32Endpoint) + USB_O_TXMAXP1) =
1675                ui32MaxPayload;
1676
1677            //
1678            // Set the transmit control value to zero.
1679            //
1680            ui32Register = 0;
1681
1682            //
1683            // Allow auto setting of TxPktRdy when max packet size has been
1684            // loaded into the FIFO.
1685            //
1686            if (ui32Flags & USB_EP_AUTO_SET) {
1687                ui32Register |= USB_TXCSRH1_AUTOSET;
1688            }
1689
1690            //
1691            // Configure the DMA Mode.
1692            //
1693            if (ui32Flags & USB_EP_DMA_MODE_1) {
1694                ui32Register |= USB_TXCSRH1_DMAEN | USB_TXCSRH1_DMAMOD;
1695            } else if (ui32Flags & USB_EP_DMA_MODE_0) {
1696                ui32Register |= USB_TXCSRH1_DMAEN;
1697            }
1698
1699            //
1700            // Write out the transmit control value.
1701            //
1702            HWREGB(ui32Base + EP_OFFSET(ui32Endpoint) + USB_O_TXCSRH1) =
1703                (uint8_t)ui32Register;
1704        } else {
1705            //
1706            // Set the transfer type information.
1707            //
1708            HWREGB(ui32Base + EP_OFFSET(ui32Endpoint) + USB_O_RXTYPE1) =
1709                ui32Register;
1710
1711            //
1712            // Set the NAK timeout or polling interval.
1713            //
1714            HWREGB(ui32Base + EP_OFFSET(ui32Endpoint) + USB_O_RXINTERVAL1) =
1715                ui32NAKPollInterval;
1716
1717            //
1718            // Set the Maximum Payload per transaction.
1719            //
1720            HWREGH(ui32Base + EP_OFFSET(ui32Endpoint) + USB_O_RXMAXP1) =
1721                ui32MaxPayload;
1722
1723            //
1724            // Set the receive control value to zero.
1725            //
1726            ui32Register = 0;
1727
1728            //
1729            // Allow auto clearing of RxPktRdy when packet of size max packet
1730            // has been unloaded from the FIFO.
1731            //
1732            if (ui32Flags & USB_EP_AUTO_CLEAR) {
1733                ui32Register |= USB_RXCSRH1_AUTOCL;
1734            }
1735
1736            //
1737            // Allow auto generation of DMA requests.
1738            //
1739            if (ui32Flags & USB_EP_AUTO_REQUEST) {
1740                ui32Register |= USB_RXCSRH1_AUTORQ;
1741            }
1742
1743            //
1744            // Configure the DMA Mode.
1745            //
1746            if (ui32Flags & USB_EP_DMA_MODE_1) {
1747                ui32Register |= USB_RXCSRH1_DMAEN | USB_RXCSRH1_DMAMOD;
1748            } else if (ui32Flags & USB_EP_DMA_MODE_0) {
1749                ui32Register |= USB_RXCSRH1_DMAEN;
1750            }
1751
1752            //
1753            // Write out the receive control value.
1754            //
1755            HWREGB(ui32Base + EP_OFFSET(ui32Endpoint) + USB_O_RXCSRH1) =
1756                (uint8_t)ui32Register;
1757        }
1758    }
1759}
1760
1761//*****************************************************************************
1762//
1763//! Changes the speed of the connection for a host endpoint.
1764//!
1765//! \param ui32Base specifies the USB module base address.
1766//! \param ui32Endpoint is the endpoint to access.
1767//! \param ui32Flags are used to configure other endpoint settings.
1768//!
1769//! This function sets the USB speed for an IN or OUT endpoint in host mode.
1770//! The \e ui32Flags parameter specifies the speed using one of the following
1771//! values: \b USB_EP_SPEED_LOW, \b USB_EP_SPEED_FULL, or \b USB_EP_SPEED_HIGH.
1772//! The \e ui32Flags parameter also specifies which direction is set by
1773//! adding the logical OR in either \b USB_EP_HOST_IN or \b USB_EP_HOST_OUT.
1774//! All other flags are ignored.  This function is typically only used for
1775//! endpoint 0, but could be used with other endpoints as well.
1776//!
1777//! \b Example: Set host transactions on endpoint 0 to full speed..
1778//!
1779//! \verbatim
1780//! //
1781//! // Set host endpoint 0 transactions to full speed.
1782//! //
1783//! USBHostEndpointSpeed(USB0_BASE, USB_EP_0, USB_EP_SPEED_FULL);
1784//! \endverbatim
1785//!
1786//! \note This function must only be called in host mode.
1787//!
1788//! \return None.
1789//
1790//*****************************************************************************
1791void USBHostEndpointSpeed(uint32_t ui32Base, uint32_t ui32Endpoint,
1792                          uint32_t ui32Flags) {
1793    uint32_t ui32Reg;
1794    uint32_t ui32Speed;
1795
1796    //
1797    // Check the arguments.
1798    //
1799    ASSERT(ui32Base == USB0_BASE);
1800    ASSERT((ui32Endpoint == USB_EP_0) || (ui32Endpoint == USB_EP_1) ||
1801           (ui32Endpoint == USB_EP_2) || (ui32Endpoint == USB_EP_3) ||
1802           (ui32Endpoint == USB_EP_4) || (ui32Endpoint == USB_EP_5) ||
1803           (ui32Endpoint == USB_EP_6) || (ui32Endpoint == USB_EP_7));
1804
1805    //
1806    // Create the register speed value.
1807    //
1808    if (ui32Flags & USB_EP_SPEED_HIGH) {
1809        ui32Speed = USB_TYPE0_SPEED_HIGH;
1810    } else if (ui32Flags & USB_EP_SPEED_FULL) {
1811        ui32Speed = USB_TYPE0_SPEED_FULL;
1812    } else {
1813        ui32Speed = USB_TYPE0_SPEED_LOW;
1814    }
1815
1816    //
1817    // Endpoint 0 is handled differently as it is bi-directional.
1818    //
1819    if (ui32Endpoint == USB_EP_0) {
1820        HWREGB(ui32Base + USB_O_TYPE0) = ui32Speed;
1821    } else if (ui32Flags & USB_EP_HOST_OUT) {
1822        //
1823        // Clear the current speed and set the new speed.
1824        //
1825        ui32Reg = (HWREGH(ui32Base + EP_OFFSET(ui32Endpoint) + USB_O_TXTYPE1) &
1826                   ~(USB_TXTYPE1_SPEED_M));
1827        ui32Reg |= ui32Speed;
1828
1829        HWREGH(ui32Base + EP_OFFSET(ui32Endpoint) + USB_O_TXTYPE1) |= ui32Reg;
1830    } else {
1831        //
1832        // Clear the current speed and set the new speed.
1833        //
1834        ui32Reg = (HWREGH(ui32Base + EP_OFFSET(ui32Endpoint) + USB_O_RXTYPE1) &
1835                   ~(USB_RXTYPE1_SPEED_M));
1836        ui32Reg |= ui32Speed;
1837
1838        HWREGH(ui32Base + EP_OFFSET(ui32Endpoint) + USB_O_RXTYPE1) |= ui32Reg;
1839    }
1840}
1841
1842//*****************************************************************************
1843//
1844//! Sets the configuration for an endpoint.
1845//!
1846//! \param ui32Base specifies the USB module base address.
1847//! \param ui32Endpoint is the endpoint to access.
1848//! \param ui32MaxPacketSize is the maximum packet size for this endpoint.
1849//! \param ui32Flags are used to configure other endpoint settings.
1850//!
1851//! This function sets the basic configuration for an endpoint in device mode.
1852//! Endpoint zero does not have a dynamic configuration, so this function
1853//! must not be called for endpoint zero.  The \e ui32Flags parameter
1854//! determines some of the configuration while the other parameters provide the
1855//! rest.
1856//!
1857//! The \b USB_EP_MODE_ flags define what the type is for the specified
1858//! endpoint.
1859//!
1860//! - \b USB_EP_MODE_CTRL is a control endpoint.
1861//! - \b USB_EP_MODE_ISOC is an isochronous endpoint.
1862//! - \b USB_EP_MODE_BULK is a bulk endpoint.
1863//! - \b USB_EP_MODE_INT is an interrupt endpoint.
1864//!
1865//! The \b USB_EP_DMA_MODE_ flags determine the type of DMA access to the
1866//! endpoint data FIFOs.  The choice of the DMA mode depends on how the DMA
1867//! controller is configured and how it is being used.  See the ``Using USB
1868//! with the uDMA Controller'' or the ''Using the integrated USB DMA
1869//! Controller'' section for more information on DMA configuration depending
1870//! on the type of DMA that is supported by the USB controller.
1871//!
1872//! When configuring an IN endpoint, the \b USB_EP_AUTO_SET bit can be
1873//! specified to cause the automatic transmission of data on the USB bus as
1874//! soon as \e ui32MaxPacketSize bytes of data are written into the FIFO for
1875//! this endpoint.  This option is commonly used with DMA (both on devices
1876//! with integrated USB DMA as well as those that use uDMA) as no interaction
1877//! is required to start the transmission of data.
1878//!
1879//! When configuring an OUT endpoint, the \b USB_EP_AUTO_REQUEST bit is
1880//! specified to trigger the request for more data once the FIFO has been
1881//! drained enough to receive \e ui32MaxPacketSize more bytes of data.  Also
1882//! for OUT endpoints, the \b USB_EP_AUTO_CLEAR bit can be used to clear the
1883//! data packet ready flag automatically once the data has been read from the
1884//! FIFO.  If this option is not used, this flag must be manually cleared via a
1885//! call to USBDevEndpointStatusClear().  Both of these settings can be used to
1886//! remove the need for extra calls when using the controller with DMA.
1887//!
1888//! \note This function must only be called in device mode.
1889//!
1890//! \return None.
1891//
1892//*****************************************************************************
1893void USBDevEndpointConfigSet(uint32_t ui32Base, uint32_t ui32Endpoint,
1894                             uint32_t ui32MaxPacketSize, uint32_t ui32Flags) {
1895    uint32_t ui32Register;
1896
1897    //
1898    // Check the arguments.
1899    //
1900    ASSERT(ui32Base == USB0_BASE);
1901    ASSERT((ui32Endpoint == USB_EP_1) || (ui32Endpoint == USB_EP_2) ||
1902           (ui32Endpoint == USB_EP_3) || (ui32Endpoint == USB_EP_4) ||
1903           (ui32Endpoint == USB_EP_5) || (ui32Endpoint == USB_EP_6) ||
1904           (ui32Endpoint == USB_EP_7));
1905
1906    //
1907    // Determine if a transmit or receive endpoint is being configured.
1908    //
1909    if (ui32Flags & USB_EP_DEV_IN) {
1910        //
1911        // Set the maximum packet size.
1912        //
1913        HWREGH(ui32Base + EP_OFFSET(ui32Endpoint) + USB_O_TXMAXP1) =
1914            ui32MaxPacketSize;
1915
1916        //
1917        // The transmit control value is zero unless options are enabled.
1918        //
1919        ui32Register = 0;
1920
1921        //
1922        // Allow auto setting of TxPktRdy when max packet size has been loaded
1923        // into the FIFO.
1924        //
1925        if (ui32Flags & USB_EP_AUTO_SET) {
1926            ui32Register |= USB_TXCSRH1_AUTOSET;
1927        }
1928
1929        //
1930        // Configure the DMA mode.
1931        //
1932        if (ui32Flags & USB_EP_DMA_MODE_1) {
1933            ui32Register |= USB_TXCSRH1_DMAEN | USB_TXCSRH1_DMAMOD;
1934        } else if (ui32Flags & USB_EP_DMA_MODE_0) {
1935            ui32Register |= USB_TXCSRH1_DMAEN;
1936        }
1937
1938        //
1939        // Enable isochronous mode if requested.
1940        //
1941        if ((ui32Flags & USB_EP_MODE_MASK) == USB_EP_MODE_ISOC) {
1942            ui32Register |= USB_TXCSRH1_ISO;
1943        }
1944
1945        //
1946        // Write the transmit control value.
1947        //
1948        HWREGB(ui32Base + EP_OFFSET(ui32Endpoint) + USB_O_TXCSRH1) =
1949            (uint8_t)ui32Register;
1950
1951        //
1952        // Reset the Data toggle to zero.
1953        //
1954        HWREGB(ui32Base + EP_OFFSET(ui32Endpoint) + USB_O_TXCSRL1) =
1955            USB_TXCSRL1_CLRDT;
1956    } else {
1957        //
1958        // Set the MaxPacketSize.
1959        //
1960        HWREGH(ui32Base + EP_OFFSET(ui32Endpoint) + USB_O_RXMAXP1) =
1961            ui32MaxPacketSize;
1962
1963        //
1964        // The receive control value is zero unless options are enabled.
1965        //
1966        ui32Register = 0;
1967
1968        //
1969        // Allow auto clearing of RxPktRdy when packet of size max packet
1970        // has been unloaded from the FIFO.
1971        //
1972        if (ui32Flags & USB_EP_AUTO_CLEAR) {
1973            ui32Register = USB_RXCSRH1_AUTOCL;
1974        }
1975
1976        //
1977        // Configure the DMA mode.
1978        //
1979        if (ui32Flags & USB_EP_DMA_MODE_1) {
1980            ui32Register |= USB_RXCSRH1_DMAEN | USB_RXCSRH1_DMAMOD;
1981        } else if (ui32Flags & USB_EP_DMA_MODE_0) {
1982            ui32Register |= USB_RXCSRH1_DMAEN;
1983        }
1984
1985        //
1986        // If requested, disable NYET responses for high-speed bulk and
1987        // interrupt endpoints.
1988        //
1989        if (ui32Flags & USB_EP_DIS_NYET) {
1990            ui32Register |= USB_RXCSRH1_DISNYET;
1991        }
1992
1993        //
1994        // Enable isochronous mode if requested.
1995        //
1996        if ((ui32Flags & USB_EP_MODE_MASK) == USB_EP_MODE_ISOC) {
1997            ui32Register |= USB_RXCSRH1_ISO;
1998        }
1999
2000        //
2001        // Write the receive control value.
2002        //
2003        HWREGB(ui32Base + EP_OFFSET(ui32Endpoint) + USB_O_RXCSRH1) =
2004            (uint8_t)ui32Register;
2005
2006        //
2007        // Reset the Data toggle to zero.
2008        //
2009        HWREGB(ui32Base + EP_OFFSET(ui32Endpoint) + USB_O_RXCSRL1) =
2010            USB_RXCSRL1_CLRDT;
2011    }
2012}
2013
2014//*****************************************************************************
2015//
2016//! Gets the current configuration for an endpoint.
2017//!
2018//! \param ui32Base specifies the USB module base address.
2019//! \param ui32Endpoint is the endpoint to access.
2020//! \param pui32MaxPacketSize is a pointer which is written with the maximum
2021//! packet size for this endpoint.
2022//! \param pui32Flags is a pointer which is written with the current endpoint
2023//! settings.  On entry to the function, this pointer must contain either
2024//! \b USB_EP_DEV_IN or \b USB_EP_DEV_OUT to indicate whether the IN or OUT
2025//! endpoint is to be queried.
2026//!
2027//! This function returns the basic configuration for an endpoint in device
2028//! mode.  The values returned in \e *pui32MaxPacketSize and \e *pui32Flags are
2029//! equivalent to the \e ui32MaxPacketSize and \e ui32Flags previously passed
2030//! to USBDevEndpointConfigSet() for this endpoint.
2031//!
2032//! \note This function must only be called in device mode.
2033//!
2034//! \return None.
2035//
2036//*****************************************************************************
2037void USBDevEndpointConfigGet(uint32_t ui32Base, uint32_t ui32Endpoint,
2038                             uint32_t* pui32MaxPacketSize,
2039                             uint32_t* pui32Flags) {
2040    uint32_t ui32Register;
2041
2042    //
2043    // Check the arguments.
2044    //
2045    ASSERT(ui32Base == USB0_BASE);
2046    ASSERT(pui32MaxPacketSize && pui32Flags);
2047    ASSERT((ui32Endpoint == USB_EP_1) || (ui32Endpoint == USB_EP_2) ||
2048           (ui32Endpoint == USB_EP_3) || (ui32Endpoint == USB_EP_4) ||
2049           (ui32Endpoint == USB_EP_5) || (ui32Endpoint == USB_EP_6) ||
2050           (ui32Endpoint == USB_EP_7));
2051
2052    //
2053    // Determine if a transmit or receive endpoint is being queried.
2054    //
2055    if (*pui32Flags & USB_EP_DEV_IN) {
2056        //
2057        // Clear the flags other than the direction bit.
2058        //
2059        *pui32Flags = USB_EP_DEV_IN;
2060
2061        //
2062        // Get the maximum packet size.
2063        //
2064        *pui32MaxPacketSize = (uint32_t)HWREGH(
2065            ui32Base + EP_OFFSET(ui32Endpoint) + USB_O_TXMAXP1);
2066
2067        //
2068        // Get the current transmit control register value.
2069        //
2070        ui32Register = (uint32_t)HWREGB(ui32Base + EP_OFFSET(ui32Endpoint) +
2071                                        USB_O_TXCSRH1);
2072
2073        //
2074        // Are we allowing auto setting of TxPktRdy when max packet size has
2075        // been loaded into the FIFO?
2076        //
2077        if (ui32Register & USB_TXCSRH1_AUTOSET) {
2078            *pui32Flags |= USB_EP_AUTO_SET;
2079        }
2080
2081        //
2082        // Get the DMA mode.
2083        //
2084        if (ui32Register & USB_TXCSRH1_DMAEN) {
2085            if (ui32Register & USB_TXCSRH1_DMAMOD) {
2086                *pui32Flags |= USB_EP_DMA_MODE_1;
2087            } else {
2088                *pui32Flags |= USB_EP_DMA_MODE_0;
2089            }
2090        }
2091
2092        //
2093        // Are we in isochronous mode?
2094        //
2095        if (ui32Register & USB_TXCSRH1_ISO) {
2096            *pui32Flags |= USB_EP_MODE_ISOC;
2097        } else {
2098            //
2099            // The hardware doesn't differentiate between bulk, interrupt
2100            // and control mode for the endpoint so we just set something
2101            // that isn't isochronous.  This protocol ensures that anyone
2102            // modifying the returned flags in preparation for a call to
2103            // USBDevEndpointConfigSet do not see an unexpected mode change.
2104            // If they decode the returned mode, however, they may be in for
2105            // a surprise.
2106            //
2107            *pui32Flags |= USB_EP_MODE_BULK;
2108        }
2109    } else {
2110        //
2111        // Clear the flags other than the direction bit.
2112        //
2113        *pui32Flags = USB_EP_DEV_OUT;
2114
2115        //
2116        // Get the MaxPacketSize.
2117        //
2118        *pui32MaxPacketSize = (uint32_t)HWREGH(
2119            ui32Base + EP_OFFSET(ui32Endpoint) + USB_O_RXMAXP1);
2120
2121        //
2122        // Get the current receive control register value.
2123        //
2124        ui32Register = (uint32_t)HWREGB(ui32Base + EP_OFFSET(ui32Endpoint) +
2125                                        USB_O_RXCSRH1);
2126
2127        //
2128        // Are we allowing auto clearing of RxPktRdy when packet of size max
2129        // packet has been unloaded from the FIFO?
2130        //
2131        if (ui32Register & USB_RXCSRH1_AUTOCL) {
2132            *pui32Flags |= USB_EP_AUTO_CLEAR;
2133        }
2134
2135        //
2136        // Get the DMA mode.
2137        //
2138        if (ui32Register & USB_RXCSRH1_DMAEN) {
2139            if (ui32Register & USB_RXCSRH1_DMAMOD) {
2140                *pui32Flags |= USB_EP_DMA_MODE_1;
2141            } else {
2142                *pui32Flags |= USB_EP_DMA_MODE_0;
2143            }
2144        }
2145
2146        //
2147        // Are we in isochronous mode?
2148        //
2149        if (ui32Register & USB_RXCSRH1_ISO) {
2150            *pui32Flags |= USB_EP_MODE_ISOC;
2151        } else {
2152            //
2153            // The hardware doesn't differentiate between bulk, interrupt
2154            // and control mode for the endpoint so we just set something
2155            // that isn't isochronous.  This protocol ensures that anyone
2156            // modifying the returned flags in preparation for a call to
2157            // USBDevEndpointConfigSet do not see an unexpected mode change.
2158            // If they decode the returned mode, however, they may be in for
2159            // a surprise.
2160            //
2161            *pui32Flags |= USB_EP_MODE_BULK;
2162        }
2163    }
2164}
2165
2166//*****************************************************************************
2167//
2168//! Sets the FIFO configuration for an endpoint.
2169//!
2170//! \param ui32Base specifies the USB module base address.
2171//! \param ui32Endpoint is the endpoint to access.
2172//! \param ui32FIFOAddress is the starting address for the FIFO.
2173//! \param ui32FIFOSize is the size of the FIFO specified by one of the
2174//! \b USB_FIFO_SZ_ values.
2175//! \param ui32Flags specifies what information to set in the FIFO
2176//! configuration.
2177//!
2178//! This function configures the starting FIFO RAM address and size of the FIFO
2179//! for a specified endpoint.  Endpoint zero does not have a dynamically
2180//! configurable FIFO, so this function must not be called for endpoint zero.
2181//! The \e ui32FIFOSize parameter must be one of the values in the
2182//! \b USB_FIFO_SZ_ values.
2183//!
2184//! The \e ui32FIFOAddress value must be a multiple of 8 bytes and directly
2185//! indicates the starting address in the USB controller's FIFO RAM.  For
2186//! example, a value of 64 indicates that the FIFO starts 64 bytes into
2187//! the USB controller's FIFO memory.  The \e ui32Flags value specifies whether
2188//! the endpoint's OUT or IN FIFO must be configured.  If in host mode, use
2189//! \b USB_EP_HOST_OUT or \b USB_EP_HOST_IN, and if in device mode, use
2190//! \b USB_EP_DEV_OUT or \b USB_EP_DEV_IN.
2191//!
2192//! \return None.
2193//
2194//*****************************************************************************
2195void USBFIFOConfigSet(uint32_t ui32Base, uint32_t ui32Endpoint,
2196                      uint32_t ui32FIFOAddress, uint32_t ui32FIFOSize,
2197                      uint32_t ui32Flags) {
2198    //
2199    // Check the arguments.
2200    //
2201    ASSERT(ui32Base == USB0_BASE);
2202    ASSERT((ui32Endpoint == USB_EP_1) || (ui32Endpoint == USB_EP_2) ||
2203           (ui32Endpoint == USB_EP_3) || (ui32Endpoint == USB_EP_4) ||
2204           (ui32Endpoint == USB_EP_5) || (ui32Endpoint == USB_EP_6) ||
2205           (ui32Endpoint == USB_EP_7));
2206
2207    //
2208    // See if the transmit or receive FIFO is being configured.
2209    //
2210    if (ui32Flags & (USB_EP_HOST_OUT | USB_EP_DEV_IN)) {
2211        //
2212        // Set the transmit FIFO location and size for this endpoint.
2213        //
2214        _USBIndexWrite(ui32Base, ui32Endpoint >> 4, USB_O_TXFIFOSZ,
2215                       ui32FIFOSize, 1);
2216        _USBIndexWrite(ui32Base, ui32Endpoint >> 4, USB_O_TXFIFOADD,
2217                       ui32FIFOAddress >> 3, 2);
2218    } else {
2219        //
2220        // Set the receive FIFO location and size for this endpoint.
2221        //
2222        _USBIndexWrite(ui32Base, ui32Endpoint >> 4, USB_O_RXFIFOSZ,
2223                       ui32FIFOSize, 1);
2224        _USBIndexWrite(ui32Base, ui32Endpoint >> 4, USB_O_RXFIFOADD,
2225                       ui32FIFOAddress >> 3, 2);
2226    }
2227}
2228
2229//*****************************************************************************
2230//
2231//! Returns the FIFO configuration for an endpoint.
2232//!
2233//! \param ui32Base specifies the USB module base address.
2234//! \param ui32Endpoint is the endpoint to access.
2235//! \param pui32FIFOAddress is the starting address for the FIFO.
2236//! \param pui32FIFOSize is the size of the FIFO as specified by one of the
2237//! \b USB_FIFO_SZ_ values.
2238//! \param ui32Flags specifies what information to retrieve from the FIFO
2239//! configuration.
2240//!
2241//! This function returns the starting address and size of the FIFO for a
2242//! specified endpoint.  Endpoint zero does not have a dynamically configurable
2243//! FIFO, so this function must not be called for endpoint zero.  The
2244//! \e ui32Flags parameter specifies whether the endpoint's OUT or IN FIFO must
2245//! be read.  If in host mode, the \e ui32Flags parameter must be
2246//! \b USB_EP_HOST_OUT or \b USB_EP_HOST_IN, and if in device mode, the
2247//! \e ui32Flags parameter must be either \b USB_EP_DEV_OUT or
2248//! \b USB_EP_DEV_IN.
2249//!
2250//! \return None.
2251//
2252//*****************************************************************************
2253void USBFIFOConfigGet(uint32_t ui32Base, uint32_t ui32Endpoint,
2254                      uint32_t* pui32FIFOAddress, uint32_t* pui32FIFOSize,
2255                      uint32_t ui32Flags) {
2256    //
2257    // Check the arguments.
2258    //
2259    ASSERT(ui32Base == USB0_BASE);
2260    ASSERT((ui32Endpoint == USB_EP_1) || (ui32Endpoint == USB_EP_2) ||
2261           (ui32Endpoint == USB_EP_3) || (ui32Endpoint == USB_EP_4) ||
2262           (ui32Endpoint == USB_EP_5) || (ui32Endpoint == USB_EP_6) ||
2263           (ui32Endpoint == USB_EP_7));
2264
2265    //
2266    // See if the transmit or receive FIFO is being configured.
2267    //
2268    if (ui32Flags & (USB_EP_HOST_OUT | USB_EP_DEV_IN)) {
2269        //
2270        // Get the transmit FIFO location and size for this endpoint.
2271        //
2272        *pui32FIFOAddress = (_USBIndexRead(ui32Base, ui32Endpoint >> 4,
2273                                           (uint32_t)USB_O_TXFIFOADD, 2))
2274                            << 3;
2275        *pui32FIFOSize = _USBIndexRead(ui32Base, ui32Endpoint >> 4,
2276                                       (uint32_t)USB_O_TXFIFOSZ, 1);
2277    } else {
2278        //
2279        // Get the receive FIFO location and size for this endpoint.
2280        //
2281        *pui32FIFOAddress = (_USBIndexRead(ui32Base, ui32Endpoint >> 4,
2282                                           (uint32_t)USB_O_RXFIFOADD, 2))
2283                            << 3;
2284        *pui32FIFOSize = _USBIndexRead(ui32Base, ui32Endpoint >> 4,
2285                                       (uint32_t)USB_O_RXFIFOSZ, 1);
2286    }
2287}
2288
2289//*****************************************************************************
2290//
2291//! Configure the DMA settings for an endpoint.
2292//!
2293//! \param ui32Base specifies the USB module base address.
2294//! \param ui32Endpoint is the endpoint to access.
2295//! \param ui32Config specifies the configuration options for an endpoint.
2296//!
2297//! This function configures the DMA settings for a specified endpoint without
2298//! changing other options that may already be configured.  In order for the
2299//! DMA transfer to be enabled, the USBEndpointDMAEnable() function must be
2300//! called before starting the DMA transfer.  The configuration
2301//! options are passed in the \e ui32Config parameter and can have the values
2302//! described below.
2303//!
2304//! One of the following values to specify direction:
2305//! - \b USB_EP_HOST_OUT or \b USB_EP_DEV_IN - This setting is used with
2306//!   DMA transfers from memory to the USB controller.
2307//! - \b USB_EP_HOST_IN or \b USB_EP_DEV_OUT - This setting is used with
2308//!   DMA transfers from the USB controller to memory.
2309//!
2310//! One of the following values:
2311//! - \b USB_EP_DMA_MODE_0(default) - This setting is typically used for
2312//!   transfers that do not span multiple packets or when interrupts are
2313//!   required for each packet.
2314//! - \b USB_EP_DMA_MODE_1 - This setting is typically used for
2315//!   transfers that span multiple packets and do not require interrupts
2316//!   between packets.
2317//!
2318//! Values only used with \b USB_EP_HOST_OUT or \b USB_EP_DEV_IN:
2319//! - \b USB_EP_AUTO_SET - This setting is used to allow transmit DMA transfers
2320//!   to automatically be sent when a full packet is loaded into a FIFO.
2321//!   This is needed with \b USB_EP_DMA_MODE_1 to ensure that packets go
2322//!   out when the FIFO becomes full and the DMA has more data to send.
2323//!
2324//! Values only used with \b USB_EP_HOST_IN or \b USB_EP_DEV_OUT:
2325//! - \b USB_EP_AUTO_CLEAR - This setting is used to allow receive DMA
2326//!   transfers to automatically be acknowledged as they are received.  This is
2327//!   needed with \b USB_EP_DMA_MODE_1 to ensure that packets continue to
2328//!   be received and acknowledged when the FIFO is emptied by the DMA
2329//!   transfer.
2330//!
2331//! Values only used with \b USB_EP_HOST_IN:
2332//! - \b USB_EP_AUTO_REQUEST - This setting is used to allow receive DMA
2333//!   transfers to automatically request a new IN transaction when the
2334//!   previous transfer has emptied the FIFO.  This is typically used in
2335//!   conjunction with \b USB_EP_AUTO_CLEAR so that receive DMA transfers
2336//!   can continue without interrupting the main processor.
2337//!
2338//! \b Example: Set endpoint 1 receive endpoint to automatically acknowledge
2339//! request and automatically generate a new IN request in host mode.
2340//!
2341//! \verbatim
2342//! //
2343//! // Configure endpoint 1 for receiving multiple packets using DMA.
2344//! //
2345//! USBEndpointDMAConfigSet(USB0_BASE, USB_EP_1, USB_EP_HOST_IN |
2346//!                                              USB_EP_DMA_MODE_1 |
2347//!                                              USB_EP_AUTO_CLEAR |
2348//!                                              USB_EP_AUTO_REQUEST);
2349//! \endverbatim
2350//!
2351//! \b Example: Set endpoint 2 transmit endpoint to automatically send each
2352//! packet in host mode when spanning multiple packets.
2353//!
2354//! \verbatim
2355//! //
2356//! // Configure endpoint 1 for transmitting multiple packets using DMA.
2357//! //
2358//! USBEndpointDMAConfigSet(USB0_BASE, USB_EP_2, USB_EP_HOST_OUT |
2359//!                                              USB_EP_DMA_MODE_1 |
2360//!                                              USB_EP_AUTO_SET);
2361//! \endverbatim
2362//!
2363//! \return None.
2364//
2365//*****************************************************************************
2366void USBEndpointDMAConfigSet(uint32_t ui32Base, uint32_t ui32Endpoint,
2367                             uint32_t ui32Config) {
2368    uint32_t ui32NewConfig;
2369
2370    if (ui32Config & USB_EP_HOST_OUT) {
2371        //
2372        // Clear mode and DMA enable.
2373        //
2374        ui32NewConfig =
2375            (HWREGB(ui32Base + EP_OFFSET(ui32Endpoint) + USB_O_TXCSRH1) &
2376             ~(USB_TXCSRH1_DMAMOD | USB_TXCSRH1_AUTOSET));
2377
2378        if (ui32Config & USB_EP_DMA_MODE_1) {
2379            ui32NewConfig |= USB_TXCSRH1_DMAMOD;
2380        }
2381
2382        if (ui32Config & USB_EP_AUTO_SET) {
2383            ui32NewConfig |= USB_TXCSRH1_AUTOSET;
2384        }
2385
2386        HWREGB(ui32Base + EP_OFFSET(ui32Endpoint) + USB_O_TXCSRH1) =
2387            ui32NewConfig;
2388    } else {
2389        ui32NewConfig =
2390            (HWREGB(ui32Base + EP_OFFSET(ui32Endpoint) + USB_O_RXCSRH1) &
2391             ~(USB_RXCSRH1_AUTORQ | USB_RXCSRH1_AUTOCL | USB_RXCSRH1_DMAMOD));
2392
2393        if (ui32Config & USB_EP_DMA_MODE_1) {
2394            ui32NewConfig |= USB_RXCSRH1_DMAMOD;
2395        }
2396
2397        if (ui32Config & USB_EP_AUTO_CLEAR) {
2398            ui32NewConfig |= USB_RXCSRH1_AUTOCL;
2399        }
2400        if (ui32Config & USB_EP_AUTO_REQUEST) {
2401            ui32NewConfig |= USB_RXCSRH1_AUTORQ;
2402        }
2403        HWREGB(ui32Base + EP_OFFSET(ui32Endpoint) + USB_O_RXCSRH1) =
2404            ui32NewConfig;
2405    }
2406}
2407
2408//*****************************************************************************
2409//
2410//! Enable DMA on a specified endpoint.
2411//!
2412//! \param ui32Base specifies the USB module base address.
2413//! \param ui32Endpoint is the endpoint to access.
2414//! \param ui32Flags specifies which direction and what mode to use when
2415//! enabling DMA.
2416//!
2417//! This function enables DMA on a specified endpoint and configures the mode
2418//! according to the values in the \e ui32Flags parameter.  The \e ui32Flags
2419//! parameter must have \b USB_EP_DEV_IN or \b USB_EP_DEV_OUT set.  Once this
2420//! function is called the only DMA or error interrupts are generated by the
2421//! USB controller.
2422//!
2423//! \note If this function is called when an endpoint is configured in DMA
2424//! mode 0 the USB controller does not generate an interrupt.
2425//!
2426//! \return None.
2427//
2428//*****************************************************************************
2429void USBEndpointDMAEnable(uint32_t ui32Base, uint32_t ui32Endpoint,
2430                          uint32_t ui32Flags) {
2431    //
2432    // See if the transmit DMA is being enabled.
2433    //
2434    if (ui32Flags & USB_EP_DEV_IN) {
2435        //
2436        // Enable DMA on the transmit endpoint.
2437        //
2438        HWREGB(ui32Base + EP_OFFSET(ui32Endpoint) + USB_O_TXCSRH1) |=
2439            USB_TXCSRH1_DMAEN;
2440    } else {
2441        //
2442        // Enable DMA on the receive endpoint.
2443        //
2444        HWREGB(ui32Base + EP_OFFSET(ui32Endpoint) + USB_O_RXCSRH1) |=
2445            USB_RXCSRH1_DMAEN;
2446    }
2447}
2448
2449//*****************************************************************************
2450//
2451//! Disable DMA on a specified endpoint.
2452//!
2453//! \param ui32Base specifies the USB module base address.
2454//! \param ui32Endpoint is the endpoint to access.
2455//! \param ui32Flags specifies which direction to disable.
2456//!
2457//! This function disables DMA on a specified endpoint to allow non-DMA USB
2458//! transactions to generate interrupts normally.  The \e ui32Flags parameter
2459//! must be \b USB_EP_DEV_IN or \b USB_EP_DEV_OUT; all other bits are ignored.
2460//!
2461//! \return None.
2462//
2463//*****************************************************************************
2464void USBEndpointDMADisable(uint32_t ui32Base, uint32_t ui32Endpoint,
2465                           uint32_t ui32Flags) {
2466    //
2467    // If this was a request to disable DMA on the IN portion of the endpoint
2468    // then handle it.
2469    //
2470    if (ui32Flags & USB_EP_DEV_IN) {
2471        //
2472        // Just disable DMA leave the mode setting.
2473        //
2474        HWREGB(ui32Base + EP_OFFSET(ui32Endpoint) + USB_O_TXCSRH1) &=
2475            ~USB_TXCSRH1_DMAEN;
2476    } else {
2477        //
2478        // Just disable DMA leave the mode setting.
2479        //
2480        HWREGB(ui32Base + EP_OFFSET(ui32Endpoint) + USB_O_RXCSRH1) &=
2481            ~USB_RXCSRH1_DMAEN;
2482    }
2483}
2484
2485//*****************************************************************************
2486//
2487//! Determines the number of bytes of data available in a specified endpoint's
2488//! FIFO.
2489//!
2490//! \param ui32Base specifies the USB module base address.
2491//! \param ui32Endpoint is the endpoint to access.
2492//!
2493//! This function returns the number of bytes of data currently available in
2494//! the FIFO for the specified receive (OUT) endpoint.  It may be used prior to
2495//! calling USBEndpointDataGet() to determine the size of buffer required to
2496//! hold the newly-received packet.
2497//!
2498//! \return This call returns the number of bytes available in a specified
2499//! endpoint FIFO.
2500//
2501//*****************************************************************************
2502uint32_t USBEndpointDataAvail(uint32_t ui32Base, uint32_t ui32Endpoint) {
2503    uint32_t ui32Register;
2504
2505    //
2506    // Check the arguments.
2507    //
2508    ASSERT(ui32Base == USB0_BASE);
2509    ASSERT((ui32Endpoint == USB_EP_0) || (ui32Endpoint == USB_EP_1) ||
2510           (ui32Endpoint == USB_EP_2) || (ui32Endpoint == USB_EP_3) ||
2511           (ui32Endpoint == USB_EP_4) || (ui32Endpoint == USB_EP_5) ||
2512           (ui32Endpoint == USB_EP_6) || (ui32Endpoint == USB_EP_7));
2513
2514    //
2515    // Get the address of the receive status register to use, based on the
2516    // endpoint.
2517    //
2518    if (ui32Endpoint == USB_EP_0) {
2519        ui32Register = USB_O_CSRL0;
2520    } else {
2521        ui32Register = USB_O_RXCSRL1 + EP_OFFSET(ui32Endpoint);
2522    }
2523
2524    //
2525    // Is there a packet ready in the FIFO?
2526    //
2527    if ((HWREGH(ui32Base + ui32Register) & USB_CSRL0_RXRDY) == 0) {
2528        return (0);
2529    }
2530
2531    //
2532    // Return the byte count in the FIFO.
2533    //
2534    return (HWREGH(ui32Base + USB_O_COUNT0 + ui32Endpoint));
2535}
2536
2537//*****************************************************************************
2538//
2539//! Retrieves data from the specified endpoint's FIFO.
2540//!
2541//! \param ui32Base specifies the USB module base address.
2542//! \param ui32Endpoint is the endpoint to access.
2543//! \param pui8Data is a pointer to the data area used to return the data from
2544//! the FIFO.
2545//! \param pui32Size is initially the size of the buffer passed into this call
2546//! via the \e pui8Data parameter.  It is set to the amount of data returned in
2547//! the buffer.
2548//!
2549//! This function returns the data from the FIFO for the specified endpoint.
2550//! The \e pui32Size parameter indicates the size of the buffer passed in
2551//! the \e pui32Data parameter.  The data in the \e pui32Size parameter is
2552//! changed to match the amount of data returned in the \e pui8Data parameter.
2553//! If a zero-byte packet is received, this call does not return an error but
2554//! instead just returns a zero in the \e pui32Size parameter.  The only error
2555//! case occurs when there is no data packet available.
2556//!
2557//! \return This call returns 0, or -1 if no packet was received.
2558//
2559//*****************************************************************************
2560int32_t USBEndpointDataGet(uint32_t ui32Base, uint32_t ui32Endpoint,
2561                           uint8_t* pui8Data, uint32_t* pui32Size) {
2562    uint32_t ui32Register, ui32ByteCount, ui32FIFO;
2563
2564    //
2565    // Check the arguments.
2566    //
2567    ASSERT(ui32Base == USB0_BASE);
2568    ASSERT((ui32Endpoint == USB_EP_0) || (ui32Endpoint == USB_EP_1) ||
2569           (ui32Endpoint == USB_EP_2) || (ui32Endpoint == USB_EP_3) ||
2570           (ui32Endpoint == USB_EP_4) || (ui32Endpoint == USB_EP_5) ||
2571           (ui32Endpoint == USB_EP_6) || (ui32Endpoint == USB_EP_7));
2572
2573    //
2574    // Get the address of the receive status register to use, based on the
2575    // endpoint.
2576    //
2577    if (ui32Endpoint == USB_EP_0) {
2578        ui32Register = USB_O_CSRL0;
2579    } else {
2580        ui32Register = USB_O_RXCSRL1 + EP_OFFSET(ui32Endpoint);
2581    }
2582
2583    //
2584    // Don't allow reading of data if the RxPktRdy bit is not set.
2585    //
2586    if ((HWREGH(ui32Base + ui32Register) & USB_CSRL0_RXRDY) == 0) {
2587        //
2588        // Can't read the data because none is available.
2589        //
2590        *pui32Size = 0;
2591
2592        //
2593        // Return a failure since there is no data to read.
2594        //
2595        return (-1);
2596    }
2597
2598    //
2599    // Get the byte count in the FIFO.
2600    //
2601    ui32ByteCount = HWREGH(ui32Base + USB_O_COUNT0 + ui32Endpoint);
2602
2603    //
2604    // Determine how many bytes are copied.
2605    //
2606    ui32ByteCount = (ui32ByteCount < *pui32Size) ? ui32ByteCount : *pui32Size;
2607
2608    //
2609    // Return the number of bytes we are going to read.
2610    //
2611    *pui32Size = ui32ByteCount;
2612
2613    //
2614    // Calculate the FIFO address.
2615    //
2616    ui32FIFO = ui32Base + USB_O_FIFO0 + (ui32Endpoint >> 2);
2617
2618    //
2619    // Read the data out of the FIFO.
2620    //
2621    for (; ui32ByteCount > 0; ui32ByteCount--) {
2622        //
2623        // Read a byte at a time from the FIFO.
2624        //
2625        *pui8Data++ = HWREGB(ui32FIFO);
2626    }
2627
2628    //
2629    // Success.
2630    //
2631    return (0);
2632}
2633
2634//*****************************************************************************
2635//
2636//! Acknowledge that data was read from the specified endpoint's FIFO in device
2637//! mode.
2638//!
2639//! \param ui32Base specifies the USB module base address.
2640//! \param ui32Endpoint is the endpoint to access.
2641//! \param bIsLastPacket indicates if this packet is the last one.
2642//!
2643//! This function acknowledges that the data was read from the endpoint's FIFO.
2644//! The \e bIsLastPacket parameter is set to a \b true value if this is the
2645//! last in a series of data packets on endpoint zero.  The \e bIsLastPacket
2646//! parameter is not used for endpoints other than endpoint zero.  This call
2647//! can be used if processing is required between reading the data and
2648//! acknowledging that the data has been read.
2649//!
2650//! \note This function must only be called in device mode.
2651//!
2652//! \return None.
2653//
2654//*****************************************************************************
2655void USBDevEndpointDataAck(uint32_t ui32Base, uint32_t ui32Endpoint,
2656                           bool bIsLastPacket) {
2657    //
2658    // Check the arguments.
2659    //
2660    ASSERT(ui32Base == USB0_BASE);
2661    ASSERT((ui32Endpoint == USB_EP_0) || (ui32Endpoint == USB_EP_1) ||
2662           (ui32Endpoint == USB_EP_2) || (ui32Endpoint == USB_EP_3) ||
2663           (ui32Endpoint == USB_EP_4) || (ui32Endpoint == USB_EP_5) ||
2664           (ui32Endpoint == USB_EP_6) || (ui32Endpoint == USB_EP_7));
2665
2666    //
2667    // Determine which endpoint is being acked.
2668    //
2669    if (ui32Endpoint == USB_EP_0) {
2670        //
2671        // Clear RxPktRdy, and optionally DataEnd, on endpoint zero.
2672        //
2673        HWREGB(ui32Base + USB_O_CSRL0) =
2674            USB_CSRL0_RXRDYC | (bIsLastPacket ? USB_CSRL0_DATAEND : 0);
2675    } else {
2676        //
2677        // Clear RxPktRdy on all other endpoints.
2678        //
2679        HWREGB(ui32Base + USB_O_RXCSRL1 + EP_OFFSET(ui32Endpoint)) &=
2680            ~(USB_RXCSRL1_RXRDY);
2681    }
2682}
2683
2684//*****************************************************************************
2685//
2686//! Acknowledge that data was read from the specified endpoint's FIFO in host
2687//! mode.
2688//!
2689//! \param ui32Base specifies the USB module base address.
2690//! \param ui32Endpoint is the endpoint to access.
2691//!
2692//! This function acknowledges that the data was read from the endpoint's FIFO.
2693//! This call is used if processing is required between reading the data and
2694//! acknowledging that the data has been read.
2695//!
2696//! \note This function must only be called in host mode.
2697//!
2698//! \return None.
2699//
2700//*****************************************************************************
2701void USBHostEndpointDataAck(uint32_t ui32Base, uint32_t ui32Endpoint) {
2702    //
2703    // Check the arguments.
2704    //
2705    ASSERT(ui32Base == USB0_BASE);
2706    ASSERT((ui32Endpoint == USB_EP_0) || (ui32Endpoint == USB_EP_1) ||
2707           (ui32Endpoint == USB_EP_2) || (ui32Endpoint == USB_EP_3) ||
2708           (ui32Endpoint == USB_EP_4) || (ui32Endpoint == USB_EP_5) ||
2709           (ui32Endpoint == USB_EP_6) || (ui32Endpoint == USB_EP_7));
2710
2711    //
2712    // Clear RxPktRdy.
2713    //
2714    if (ui32Endpoint == USB_EP_0) {
2715        HWREGB(ui32Base + USB_O_CSRL0) &= ~USB_CSRL0_RXRDY;
2716    } else {
2717        HWREGB(ui32Base + USB_O_RXCSRL1 + EP_OFFSET(ui32Endpoint)) &=
2718            ~(USB_RXCSRL1_RXRDY);
2719    }
2720}
2721
2722//*****************************************************************************
2723//
2724//! Puts data into the specified endpoint's FIFO.
2725//!
2726//! \param ui32Base specifies the USB module base address.
2727//! \param ui32Endpoint is the endpoint to access.
2728//! \param pui8Data is a pointer to the data area used as the source for the
2729//! data to put into the FIFO.
2730//! \param ui32Size is the amount of data to put into the FIFO.
2731//!
2732//! This function puts the data from the \e pui8Data parameter into the FIFO
2733//! for this endpoint.  If a packet is already pending for transmission, then
2734//! this call does not put any of the data into the FIFO and returns -1.  Care
2735//! must be taken to not write more data than can fit into the FIFO
2736//! allocated by the call to USBFIFOConfigSet().
2737//!
2738//! \return This call returns 0 on success, or -1 to indicate that the FIFO
2739//! is in use and cannot be written.
2740//
2741//*****************************************************************************
2742int32_t USBEndpointDataPut(uint32_t ui32Base, uint32_t ui32Endpoint,
2743                           uint8_t* pui8Data, uint32_t ui32Size) {
2744    uint32_t ui32FIFO;
2745    uint8_t ui8TxPktRdy;
2746
2747    //
2748    // Check the arguments.
2749    //
2750    ASSERT(ui32Base == USB0_BASE);
2751    ASSERT((ui32Endpoint == USB_EP_0) || (ui32Endpoint == USB_EP_1) ||
2752           (ui32Endpoint == USB_EP_2) || (ui32Endpoint == USB_EP_3) ||
2753           (ui32Endpoint == USB_EP_4) || (ui32Endpoint == USB_EP_5) ||
2754           (ui32Endpoint == USB_EP_6) || (ui32Endpoint == USB_EP_7));
2755
2756    //
2757    // Get the bit position of TxPktRdy based on the endpoint.
2758    //
2759    if (ui32Endpoint == USB_EP_0) {
2760        ui8TxPktRdy = USB_CSRL0_TXRDY;
2761    } else {
2762        ui8TxPktRdy = USB_TXCSRL1_TXRDY;
2763    }
2764
2765    //
2766    // Don't allow transmit of data if the TxPktRdy bit is already set.
2767    //
2768    if (HWREGB(ui32Base + USB_O_CSRL0 + ui32Endpoint) & ui8TxPktRdy) {
2769        return (-1);
2770    }
2771
2772    //
2773    // Calculate the FIFO address.
2774    //
2775    ui32FIFO = ui32Base + USB_O_FIFO0 + (ui32Endpoint >> 2);
2776
2777    //
2778    // Write the data to the FIFO.
2779    //
2780    for (; ui32Size > 0; ui32Size--) { HWREGB(ui32FIFO) = *pui8Data++; }
2781
2782    //
2783    // Success.
2784    //
2785    return (0);
2786}
2787
2788//*****************************************************************************
2789//
2790//! Starts the transfer of data from an endpoint's FIFO.
2791//!
2792//! \param ui32Base specifies the USB module base address.
2793//! \param ui32Endpoint is the endpoint to access.
2794//! \param ui32TransType is set to indicate what type of data is being sent.
2795//!
2796//! This function starts the transfer of data from the FIFO for a specified
2797//! endpoint.  This function is called if the \b USB_EP_AUTO_SET bit was
2798//! not enabled for the endpoint.  Setting the \e ui32TransType parameter
2799//! allows the appropriate signaling on the USB bus for the type of transaction
2800//! being requested.  The \e ui32TransType parameter must be one of the
2801//! following:
2802//!
2803//! - \b USB_TRANS_OUT for OUT transaction on any endpoint in host mode.
2804//! - \b USB_TRANS_IN for IN transaction on any endpoint in device mode.
2805//! - \b USB_TRANS_IN_LAST for the last IN transaction on endpoint zero in a
2806//!   sequence of IN transactions.
2807//! - \b USB_TRANS_SETUP for setup transactions on endpoint zero.
2808//! - \b USB_TRANS_STATUS for status results on endpoint zero.
2809//!
2810//! \return This call returns 0 on success, or -1 if a transmission is already
2811//! in progress.
2812//
2813//*****************************************************************************
2814int32_t USBEndpointDataSend(uint32_t ui32Base, uint32_t ui32Endpoint,
2815                            uint32_t ui32TransType) {
2816    uint32_t ui32TxPktRdy;
2817
2818    //
2819    // Check the arguments.
2820    //
2821    ASSERT(ui32Base == USB0_BASE);
2822    ASSERT((ui32Endpoint == USB_EP_0) || (ui32Endpoint == USB_EP_1) ||
2823           (ui32Endpoint == USB_EP_2) || (ui32Endpoint == USB_EP_3) ||
2824           (ui32Endpoint == USB_EP_4) || (ui32Endpoint == USB_EP_5) ||
2825           (ui32Endpoint == USB_EP_6) || (ui32Endpoint == USB_EP_7));
2826
2827    //
2828    // Get the bit position of TxPktRdy based on the endpoint.
2829    //
2830    if (ui32Endpoint == USB_EP_0) {
2831        //
2832        // Don't allow transmit of data if the TxPktRdy bit is already set.
2833        //
2834        if (HWREGB(ui32Base + USB_O_CSRL0) & USB_CSRL0_TXRDY) {
2835            return (-1);
2836        }
2837
2838        ui32TxPktRdy = ui32TransType & 0xff;
2839    } else {
2840        //
2841        // Don't allow transmit of data if the TxPktRdy bit is already set.
2842        //
2843        if (HWREGB(ui32Base + USB_O_CSRL0 + ui32Endpoint) & USB_TXCSRL1_TXRDY) {
2844            return (-1);
2845        }
2846
2847        ui32TxPktRdy = (ui32TransType >> 8) & 0xff;
2848    }
2849
2850    //
2851    // Set TxPktRdy in order to send the data.
2852    //
2853    HWREGB(ui32Base + USB_O_CSRL0 + ui32Endpoint) = ui32TxPktRdy;
2854
2855    //
2856    // Success.
2857    //
2858    return (0);
2859}
2860
2861//*****************************************************************************
2862//
2863//! Forces a flush of an endpoint's FIFO.
2864//!
2865//! \param ui32Base specifies the USB module base address.
2866//! \param ui32Endpoint is the endpoint to access.
2867//! \param ui32Flags specifies if the IN or OUT endpoint is accessed.
2868//!
2869//! This function forces the USB controller to flush out the data in the FIFO.
2870//! The function can be called with either host or device controllers and
2871//! requires the \e ui32Flags parameter be one of \b USB_EP_HOST_OUT,
2872//! \b USB_EP_HOST_IN, \b USB_EP_DEV_OUT, or \b USB_EP_DEV_IN.
2873//!
2874//! \return None.
2875//
2876//*****************************************************************************
2877void USBFIFOFlush(uint32_t ui32Base, uint32_t ui32Endpoint,
2878                  uint32_t ui32Flags) {
2879    //
2880    // Check the arguments.
2881    //
2882    ASSERT(ui32Base == USB0_BASE);
2883    ASSERT((ui32Endpoint == USB_EP_0) || (ui32Endpoint == USB_EP_1) ||
2884           (ui32Endpoint == USB_EP_2) || (ui32Endpoint == USB_EP_3) ||
2885           (ui32Endpoint == USB_EP_4) || (ui32Endpoint == USB_EP_5) ||
2886           (ui32Endpoint == USB_EP_6) || (ui32Endpoint == USB_EP_7));
2887
2888    //
2889    // Endpoint zero has a different register set for FIFO flushing.
2890    //
2891    if (ui32Endpoint == USB_EP_0) {
2892        //
2893        // Nothing in the FIFO if neither of these bits are set.
2894        //
2895        if ((HWREGB(ui32Base + USB_O_CSRL0) &
2896             (USB_CSRL0_RXRDY | USB_CSRL0_TXRDY)) != 0) {
2897            //
2898            // Hit the Flush FIFO bit.
2899            //
2900            HWREGB(ui32Base + USB_O_CSRH0) = USB_CSRH0_FLUSH;
2901        }
2902    } else {
2903        //
2904        // Only reset the IN or OUT FIFO.
2905        //
2906        if (ui32Flags & (USB_EP_HOST_OUT | USB_EP_DEV_IN)) {
2907            //
2908            // Make sure the FIFO is not empty.
2909            //
2910            if (HWREGB(ui32Base + USB_O_TXCSRL1 + EP_OFFSET(ui32Endpoint)) &
2911                USB_TXCSRL1_TXRDY) {
2912                //
2913                // Hit the Flush FIFO bit.
2914                //
2915                HWREGB(ui32Base + USB_O_TXCSRL1 + EP_OFFSET(ui32Endpoint)) |=
2916                    USB_TXCSRL1_FLUSH;
2917            }
2918        } else {
2919            //
2920            // Make sure that the FIFO is not empty.
2921            //
2922            if (HWREGB(ui32Base + USB_O_RXCSRL1 + EP_OFFSET(ui32Endpoint)) &
2923                USB_RXCSRL1_RXRDY) {
2924                //
2925                // Hit the Flush FIFO bit.
2926                //
2927                HWREGB(ui32Base + USB_O_RXCSRL1 + EP_OFFSET(ui32Endpoint)) |=
2928                    USB_RXCSRL1_FLUSH;
2929            }
2930        }
2931    }
2932}
2933
2934//*****************************************************************************
2935//
2936//! Schedules a request for an IN transaction on an endpoint in host mode.
2937//!
2938//! \param ui32Base specifies the USB module base address.
2939//! \param ui32Endpoint is the endpoint to access.
2940//!
2941//! This function schedules a request for an IN transaction.  When the USB
2942//! device being communicated with responds with the data, the data can be
2943//! retrieved by calling USBEndpointDataGet() or via a DMA transfer.
2944//!
2945//! \note This function must only be called in host mode and only for IN
2946//! endpoints.
2947//!
2948//! \return None.
2949//
2950//*****************************************************************************
2951void USBHostRequestIN(uint32_t ui32Base, uint32_t ui32Endpoint) {
2952    uint32_t ui32Register;
2953
2954    //
2955    // Check the arguments.
2956    //
2957    ASSERT(ui32Base == USB0_BASE);
2958    ASSERT((ui32Endpoint == USB_EP_0) || (ui32Endpoint == USB_EP_1) ||
2959           (ui32Endpoint == USB_EP_2) || (ui32Endpoint == USB_EP_3) ||
2960           (ui32Endpoint == USB_EP_4) || (ui32Endpoint == USB_EP_5) ||
2961           (ui32Endpoint == USB_EP_6) || (ui32Endpoint == USB_EP_7));
2962
2963    //
2964    // Endpoint zero uses a different offset than the other endpoints.
2965    //
2966    if (ui32Endpoint == USB_EP_0) {
2967        ui32Register = USB_O_CSRL0;
2968    } else {
2969        ui32Register = USB_O_RXCSRL1 + EP_OFFSET(ui32Endpoint);
2970    }
2971
2972    //
2973    // Set the request for an IN transaction.
2974    //
2975    HWREGB(ui32Base + ui32Register) = USB_RXCSRL1_REQPKT;
2976}
2977
2978//*****************************************************************************
2979//
2980//! Clears a scheduled IN transaction for an endpoint in host mode.
2981//!
2982//! \param ui32Base specifies the USB module base address.
2983//! \param ui32Endpoint is the endpoint to access.
2984//!
2985//! This function clears a previously scheduled IN transaction if it is still
2986//! pending.  This function is used to safely disable any scheduled IN
2987//! transactions if the endpoint specified by \e ui32Endpoint is reconfigured
2988//! for communications with other devices.
2989//!
2990//! \note This function must only be called in host mode and only for IN
2991//! endpoints.
2992//!
2993//! \return None.
2994//
2995//*****************************************************************************
2996void USBHostRequestINClear(uint32_t ui32Base, uint32_t ui32Endpoint) {
2997    uint32_t ui32Register;
2998
2999    //
3000    // Check the arguments.
3001    //
3002    ASSERT(ui32Base == USB0_BASE);
3003    ASSERT((ui32Endpoint == USB_EP_0) || (ui32Endpoint == USB_EP_1) ||
3004           (ui32Endpoint == USB_EP_2) || (ui32Endpoint == USB_EP_3) ||
3005           (ui32Endpoint == USB_EP_4) || (ui32Endpoint == USB_EP_5) ||
3006           (ui32Endpoint == USB_EP_6) || (ui32Endpoint == USB_EP_7));
3007
3008    //
3009    // Endpoint zero uses a different offset than the other endpoints.
3010    //
3011    if (ui32Endpoint == USB_EP_0) {
3012        ui32Register = USB_O_CSRL0;
3013    } else {
3014        ui32Register = USB_O_RXCSRL1 + EP_OFFSET(ui32Endpoint);
3015    }
3016
3017    //
3018    // Clear the request for an IN transaction.
3019    //
3020    HWREGB(ui32Base + ui32Register) &= ~USB_RXCSRL1_REQPKT;
3021}
3022
3023//*****************************************************************************
3024//
3025//! Issues a request for a status IN transaction on endpoint zero.
3026//!
3027//! \param ui32Base specifies the USB module base address.
3028//!
3029//! This function is used to cause a request for a status IN transaction from
3030//! a device on endpoint zero.  This function can only be used with endpoint
3031//! zero as that is the only control endpoint that supports this ability.  This
3032//! function is used to complete the last phase of a control transaction to a
3033//! device and an interrupt is signaled when the status packet has been
3034//! received.
3035//!
3036//! \note This function must only be called in host mode.
3037//!
3038//! \return None.
3039//
3040//*****************************************************************************
3041void USBHostRequestStatus(uint32_t ui32Base) {
3042    //
3043    // Check the arguments.
3044    //
3045    ASSERT(ui32Base == USB0_BASE);
3046
3047    //
3048    // Set the request for a status IN transaction.
3049    //
3050    HWREGB(ui32Base + USB_O_CSRL0) = USB_CSRL0_REQPKT | USB_CSRL0_STATUS;
3051}
3052
3053//*****************************************************************************
3054//
3055//! Sets the functional address for the device that is connected to an
3056//! endpoint in host mode.
3057//!
3058//! \param ui32Base specifies the USB module base address.
3059//! \param ui32Endpoint is the endpoint to access.
3060//! \param ui32Addr is the functional address for the controller to use for
3061//! this endpoint.
3062//! \param ui32Flags determines if this is an IN or an OUT endpoint.
3063//!
3064//! This function configures the functional address for a device that is using
3065//! this endpoint for communication.  This \e ui32Addr parameter is the address
3066//! of the target device that this endpoint is communicating with.  The
3067//! \e ui32Flags parameter indicates if the IN or OUT endpoint is set.
3068//!
3069//! \note This function must only be called in host mode.
3070//!
3071//! \return None.
3072//
3073//*****************************************************************************
3074void USBHostAddrSet(uint32_t ui32Base, uint32_t ui32Endpoint, uint32_t ui32Addr,
3075                    uint32_t ui32Flags) {
3076    //
3077    // Check the arguments.
3078    //
3079    ASSERT(ui32Base == USB0_BASE);
3080    ASSERT((ui32Endpoint == USB_EP_0) || (ui32Endpoint == USB_EP_1) ||
3081           (ui32Endpoint == USB_EP_2) || (ui32Endpoint == USB_EP_3) ||
3082           (ui32Endpoint == USB_EP_4) || (ui32Endpoint == USB_EP_5) ||
3083           (ui32Endpoint == USB_EP_6) || (ui32Endpoint == USB_EP_7));
3084
3085    //
3086    // See if the transmit or receive address is set.
3087    //
3088    if (ui32Flags & USB_EP_HOST_OUT) {
3089        //
3090        // Set the transmit address.
3091        //
3092        HWREGB(ui32Base + USB_O_TXFUNCADDR0 + (ui32Endpoint >> 1)) = ui32Addr;
3093    } else {
3094        //
3095        // Set the receive address.
3096        //
3097        HWREGB(ui32Base + USB_O_TXFUNCADDR0 + 4 + (ui32Endpoint >> 1)) =
3098            ui32Addr;
3099    }
3100}
3101
3102//*****************************************************************************
3103//
3104//! Gets the current functional device address for an endpoint.
3105//!
3106//! \param ui32Base specifies the USB module base address.
3107//! \param ui32Endpoint is the endpoint to access.
3108//! \param ui32Flags determines if this is an IN or an OUT endpoint.
3109//!
3110//! This function returns the current functional address that an endpoint is
3111//! using to communicate with a device.  The \e ui32Flags parameter determines
3112//! if the IN or OUT endpoint's device address is returned.
3113//!
3114//! \note This function must only be called in host mode.
3115//!
3116//! \return Returns the current function address being used by an endpoint.
3117//
3118//*****************************************************************************
3119uint32_t USBHostAddrGet(uint32_t ui32Base, uint32_t ui32Endpoint,
3120                        uint32_t ui32Flags) {
3121    //
3122    // Check the arguments.
3123    //
3124    ASSERT(ui32Base == USB0_BASE);
3125    ASSERT((ui32Endpoint == USB_EP_0) || (ui32Endpoint == USB_EP_1) ||
3126           (ui32Endpoint == USB_EP_2) || (ui32Endpoint == USB_EP_3) ||
3127           (ui32Endpoint == USB_EP_4) || (ui32Endpoint == USB_EP_5) ||
3128           (ui32Endpoint == USB_EP_6) || (ui32Endpoint == USB_EP_7));
3129
3130    //
3131    // See if the transmit or receive address is returned.
3132    //
3133    if (ui32Flags & USB_EP_HOST_OUT) {
3134        //
3135        // Return this endpoint's transmit address.
3136        //
3137        return (HWREGB(ui32Base + USB_O_TXFUNCADDR0 + (ui32Endpoint >> 1)));
3138    } else {
3139        //
3140        // Return this endpoint's receive address.
3141        //
3142        return (HWREGB(ui32Base + USB_O_TXFUNCADDR0 + 4 + (ui32Endpoint >> 1)));
3143    }
3144}
3145
3146//*****************************************************************************
3147//
3148//! Sets the hub address for the device that is connected to an endpoint.
3149//!
3150//! \param ui32Base specifies the USB module base address.
3151//! \param ui32Endpoint is the endpoint to access.
3152//! \param ui32Addr is the hub address and port for the device using this
3153//! endpoint.  The hub address must be defined in bits 0 through 6 with the
3154//! port number in bits 8 through 14.
3155//! \param ui32Flags determines if this is an IN or an OUT endpoint.
3156//!
3157//! This function configures the hub address for a device that is using this
3158//! endpoint for communication.  The \e ui32Flags parameter determines if the
3159//! device address for the IN or the OUT endpoint is configured by this call
3160//! and sets the speed of the downstream device.  Valid values are one of
3161//! \b USB_EP_HOST_OUT or \b USB_EP_HOST_IN optionally ORed with
3162//! \b USB_EP_SPEED_LOW.
3163//!
3164//! \note This function must only be called in host mode.
3165//!
3166//! \return None.
3167//
3168//*****************************************************************************
3169void USBHostHubAddrSet(uint32_t ui32Base, uint32_t ui32Endpoint,
3170                       uint32_t ui32Addr, uint32_t ui32Flags) {
3171    //
3172    // Check the arguments.
3173    //
3174    ASSERT(ui32Base == USB0_BASE);
3175    ASSERT((ui32Endpoint == USB_EP_0) || (ui32Endpoint == USB_EP_1) ||
3176           (ui32Endpoint == USB_EP_2) || (ui32Endpoint == USB_EP_3) ||
3177           (ui32Endpoint == USB_EP_4) || (ui32Endpoint == USB_EP_5) ||
3178           (ui32Endpoint == USB_EP_6) || (ui32Endpoint == USB_EP_7));
3179
3180    //
3181    // See if the hub transmit or receive address is being set.
3182    //
3183    if (ui32Flags & USB_EP_HOST_OUT) {
3184        //
3185        // Set the hub transmit address and port number for this endpoint.
3186        //
3187        HWREGH(ui32Base + USB_O_TXHUBADDR0 + (ui32Endpoint >> 1)) = ui32Addr;
3188    } else {
3189        //
3190        // Set the hub receive address and port number for this endpoint.
3191        //
3192        HWREGH(ui32Base + USB_O_TXHUBADDR0 + 4 + (ui32Endpoint >> 1)) =
3193            ui32Addr;
3194    }
3195
3196    //
3197    // Set the speed of communication for endpoint 0.  This configuration is
3198    // done here because it changes on a transaction-by-transaction basis for
3199    // EP0.  For other endpoints, this is set in USBHostEndpointConfig().
3200    //
3201    if (ui32Endpoint == USB_EP_0) {
3202        if (ui32Flags & USB_EP_SPEED_FULL) {
3203            HWREGB(ui32Base + USB_O_TYPE0) = USB_TYPE0_SPEED_FULL;
3204        } else if (ui32Flags & USB_EP_SPEED_HIGH) {
3205            HWREGB(ui32Base + USB_O_TYPE0) = USB_TYPE0_SPEED_HIGH;
3206        } else {
3207            HWREGB(ui32Base + USB_O_TYPE0) = USB_TYPE0_SPEED_LOW;
3208        }
3209    }
3210}
3211
3212//*****************************************************************************
3213//
3214//! Gets the current device hub address for this endpoint.
3215//!
3216//! \param ui32Base specifies the USB module base address.
3217//! \param ui32Endpoint is the endpoint to access.
3218//! \param ui32Flags determines if this is an IN or an OUT endpoint.
3219//!
3220//! This function returns the current hub address that an endpoint is using
3221//! to communicate with a device.  The \e ui32Flags parameter determines if the
3222//! device address for the IN or OUT endpoint is returned.
3223//!
3224//! \note This function must only be called in host mode.
3225//!
3226//! \return This function returns the current hub address being used by an
3227//! endpoint.
3228//
3229//*****************************************************************************
3230uint32_t USBHostHubAddrGet(uint32_t ui32Base, uint32_t ui32Endpoint,
3231                           uint32_t ui32Flags) {
3232    //
3233    // Check the arguments.
3234    //
3235    ASSERT(ui32Base == USB0_BASE);
3236    ASSERT((ui32Endpoint == USB_EP_0) || (ui32Endpoint == USB_EP_1) ||
3237           (ui32Endpoint == USB_EP_2) || (ui32Endpoint == USB_EP_3) ||
3238           (ui32Endpoint == USB_EP_4) || (ui32Endpoint == USB_EP_5) ||
3239           (ui32Endpoint == USB_EP_6) || (ui32Endpoint == USB_EP_7));
3240
3241    //
3242    // See if the hub transmit or receive address is returned.
3243    //
3244    if (ui32Flags & USB_EP_HOST_OUT) {
3245        //
3246        // Return the hub transmit address for this endpoint.
3247        //
3248        return (HWREGB(ui32Base + USB_O_TXHUBADDR0 + (ui32Endpoint >> 1)));
3249    } else {
3250        //
3251        // Return the hub receive address for this endpoint.
3252        //
3253        return (HWREGB(ui32Base + USB_O_TXHUBADDR0 + 4 + (ui32Endpoint >> 1)));
3254    }
3255}
3256
3257//*****************************************************************************
3258//
3259//! Sets the configuration for USB power fault.
3260//!
3261//! \param ui32Base specifies the USB module base address.
3262//! \param ui32Flags specifies the configuration of the power fault.
3263//!
3264//! This function controls how the USB controller uses its external power
3265//! control pins (USBnPFLT and USBnEPEN).  The flags specify the power
3266//! fault level sensitivity, the power fault action, and the power enable level
3267//! and source.
3268//!
3269//! One of the following can be selected as the power fault level sensitivity:
3270//!
3271//! - \b USB_HOST_PWRFLT_LOW - An external power fault is indicated by the pin
3272//!                            being driven low.
3273//! - \b USB_HOST_PWRFLT_HIGH - An external power fault is indicated by the pin
3274//!                             being driven high.
3275//!
3276//! One of the following can be selected as the power fault action:
3277//!
3278//! - \b USB_HOST_PWRFLT_EP_NONE - No automatic action when power fault
3279//!   detected.
3280//! - \b USB_HOST_PWRFLT_EP_TRI - Automatically tri-state the USBnEPEN pin on a
3281//!                               power fault.
3282//! - \b USB_HOST_PWRFLT_EP_LOW - Automatically drive USBnEPEN pin low on a
3283//!                               power fault.
3284//! - \b USB_HOST_PWRFLT_EP_HIGH - Automatically drive USBnEPEN pin high on a
3285//!                                power fault.
3286//!
3287//! One of the following can be selected as the power enable level and source:
3288//!
3289//! - \b USB_HOST_PWREN_MAN_LOW - USBnEPEN is driven low by the USB controller
3290//!                               when USBHostPwrEnable() is called.
3291//! - \b USB_HOST_PWREN_MAN_HIGH - USBnEPEN is driven high by the USB
3292//!                                controller when USBHostPwrEnable() is
3293//!                                called.
3294//! - \b USB_HOST_PWREN_AUTOLOW - USBnEPEN is driven low by the USB controller
3295//!                               automatically if USBOTGSessionRequest() has
3296//!                               enabled a session.
3297//! - \b USB_HOST_PWREN_AUTOHIGH - USBnEPEN is driven high by the USB
3298//!                                controller automatically if
3299//!                                USBOTGSessionRequest() has enabled a
3300//!                                session.
3301//!
3302//! When using the VBUS glitch filter, the \b USB_HOST_PWREN_FILTER can be
3303//! addded to ignore small, short drops in VBUS level caused by high power
3304//! consumption.  This feature is mainly used to avoid causing VBUS errors
3305//! caused by devices with high in-rush current.
3306//!
3307//! \note This function must only be called on microcontrollers that support
3308//! host mode or OTG operation. The \b USB_HOST_PWREN_AUTOLOW and
3309//! \b USB_HOST_PWREN_AUTOHIGH parameters can only be specified on devices that
3310//! support OTG operation.
3311//!
3312//! \return None.
3313//
3314//*****************************************************************************
3315void USBHostPwrConfig(uint32_t ui32Base, uint32_t ui32Flags) {
3316    //
3317    // Check the arguments.
3318    //
3319    ASSERT(ui32Base == USB0_BASE);
3320    ASSERT((ui32Flags &
3321            ~(USB_HOST_PWREN_FILTER | USB_EPC_PFLTACT_M | USB_EPC_PFLTAEN |
3322              USB_EPC_PFLTSEN_HIGH | USB_EPC_EPEN_M)) == 0);
3323
3324    //
3325    // If requested, enable VBUS droop detection on parts that support this
3326    // feature.
3327    //
3328    HWREG(ui32Base + USB_O_VDC) = ui32Flags >> 16;
3329
3330    //
3331    // Set the power fault configuration as specified.  This configuration
3332    // does not change whether fault detection is enabled or not.
3333    //
3334    HWREGH(ui32Base + USB_O_EPC) =
3335        (ui32Flags | (HWREGH(ui32Base + USB_O_EPC) &
3336                      ~(USB_EPC_PFLTACT_M | USB_EPC_PFLTAEN |
3337                        USB_EPC_PFLTSEN_HIGH | USB_EPC_EPEN_M)));
3338}
3339
3340//*****************************************************************************
3341//
3342//! Enables power fault detection.
3343//!
3344//! \param ui32Base specifies the USB module base address.
3345//!
3346//! This function enables power fault detection in the USB controller.  If the
3347//! USBnPFLT pin is not in use, this function must not be used.
3348//!
3349//! \note This function must only be called in host mode.
3350//!
3351//! \return None.
3352//
3353//*****************************************************************************
3354void USBHostPwrFaultEnable(uint32_t ui32Base) {
3355    //
3356    // Check the arguments.
3357    //
3358    ASSERT(ui32Base == USB0_BASE);
3359
3360    //
3361    // Enable power fault input.
3362    //
3363    HWREGH(ui32Base + USB_O_EPC) |= USB_EPC_PFLTEN;
3364}
3365
3366//*****************************************************************************
3367//
3368//! Disables power fault detection.
3369//!
3370//! \param ui32Base specifies the USB module base address.
3371//!
3372//! This function disables power fault detection in the USB controller.
3373//!
3374//! \note This function must only be called in host mode.
3375//!
3376//! \return None.
3377//
3378//*****************************************************************************
3379void USBHostPwrFaultDisable(uint32_t ui32Base) {
3380    //
3381    // Check the arguments.
3382    //
3383    ASSERT(ui32Base == USB0_BASE);
3384
3385    //
3386    // Enable power fault input.
3387    //
3388    HWREGH(ui32Base + USB_O_EPC) &= ~USB_EPC_PFLTEN;
3389}
3390
3391//*****************************************************************************
3392//
3393//! Enables the external power pin.
3394//!
3395//! \param ui32Base specifies the USB module base address.
3396//!
3397//! This function enables the USBnEPEN signal, which enables an external power
3398//! supply in host mode operation.
3399//!
3400//! \note This function must only be called in host mode.
3401//!
3402//! \return None.
3403//
3404//*****************************************************************************
3405void USBHostPwrEnable(uint32_t ui32Base) {
3406    //
3407    // Check the arguments.
3408    //
3409    ASSERT(ui32Base == USB0_BASE);
3410
3411    //
3412    // Enable the external power supply enable signal.
3413    //
3414    HWREGH(ui32Base + USB_O_EPC) |= USB_EPC_EPENDE;
3415}
3416
3417//*****************************************************************************
3418//
3419//! Disables the external power pin.
3420//!
3421//! \param ui32Base specifies the USB module base address.
3422//!
3423//! This function disables the USBnEPEN signal, which disables an external
3424//! power supply in host mode operation.
3425//!
3426//! \note This function must only be called in host mode.
3427//!
3428//! \return None.
3429//
3430//*****************************************************************************
3431void USBHostPwrDisable(uint32_t ui32Base) {
3432    //
3433    // Check the arguments.
3434    //
3435    ASSERT(ui32Base == USB0_BASE);
3436
3437    //
3438    // Disable the external power supply enable signal.
3439    //
3440    HWREGH(ui32Base + USB_O_EPC) &= ~USB_EPC_EPENDE;
3441}
3442
3443//*****************************************************************************
3444//
3445//! Gets the current frame number.
3446//!
3447//! \param ui32Base specifies the USB module base address.
3448//!
3449//! This function returns the last frame number received.
3450//!
3451//! \return The last frame number received.
3452//
3453//*****************************************************************************
3454uint32_t USBFrameNumberGet(uint32_t ui32Base) {
3455    //
3456    // Check the arguments.
3457    //
3458    ASSERT(ui32Base == USB0_BASE);
3459
3460    //
3461    // Return the most recent frame number.
3462    //
3463    return (HWREGH(ui32Base + USB_O_FRAME));
3464}
3465
3466//*****************************************************************************
3467//
3468//! Starts or ends a session.
3469//!
3470//! \param ui32Base specifies the USB module base address.
3471//! \param bStart specifies if this call starts or ends a session.
3472//!
3473//! This function is used in OTG mode to start a session request or end a
3474//! session.  If the \e bStart parameter is set to \b true, then this function
3475//! starts a session and if it is \b false it ends a session.
3476//!
3477//! \return None.
3478//
3479//*****************************************************************************
3480void USBOTGSessionRequest(uint32_t ui32Base, bool bStart) {
3481    //
3482    // Check the arguments.
3483    //
3484    ASSERT(ui32Base == USB0_BASE);
3485
3486    //
3487    // Start or end the session as directed.
3488    //
3489    if (bStart) {
3490        HWREGB(ui32Base + USB_O_DEVCTL) |= USB_DEVCTL_SESSION;
3491    } else {
3492        HWREGB(ui32Base + USB_O_DEVCTL) &= ~USB_DEVCTL_SESSION;
3493    }
3494}
3495
3496//*****************************************************************************
3497//
3498//! Returns the absolute FIFO address for a specified endpoint.
3499//!
3500//! \param ui32Base specifies the USB module base address.
3501//! \param ui32Endpoint specifies which endpoint's FIFO address to return.
3502//!
3503//! This function returns the actual physical address of the FIFO.  This
3504//! address is needed when the USB is going to be used with the uDMA
3505//! controller and the source or destination address must be set to the
3506//! physical FIFO address for a specified endpoint. This function can also be
3507//! used to provide the physical address to manually read data from an
3508//! endpoints FIFO.
3509//!
3510//! \return None.
3511//
3512//*****************************************************************************
3513uint32_t USBFIFOAddrGet(uint32_t ui32Base, uint32_t ui32Endpoint) {
3514    //
3515    // Return the FIFO address for this endpoint.
3516    //
3517    return (ui32Base + USB_O_FIFO0 + (ui32Endpoint >> 2));
3518}
3519
3520//*****************************************************************************
3521//
3522//! Returns the current operating mode of the controller.
3523//!
3524//! \param ui32Base specifies the USB module base address.
3525//!
3526//! This function returns the current operating mode on USB controllers with
3527//! OTG or Dual mode functionality.
3528//!
3529//! For OTG controllers:
3530//!
3531//! The function returns one of the following values on OTG controllers:
3532//!
3533//! \b USB_OTG_MODE_ASIDE_HOST indicates that the controller is in host mode
3534//! on the A-side of the cable.
3535//!
3536//! \b USB_OTG_MODE_ASIDE_DEV indicates that the controller is in device mode
3537//! on the A-side of the cable.
3538//!
3539//! \b USB_OTG_MODE_BSIDE_HOST indicates that the controller is in host mode
3540//! on the B-side of the cable.
3541//!
3542//! \b USB_OTG_MODE_BSIDE_DEV indicates that the controller is in device mode
3543//! on the B-side of the cable.  If an OTG session request is started with no
3544//! cable in place, this mode is the default.
3545//!
3546//! \b USB_OTG_MODE_NONE indicates that the controller is not attempting to
3547//! determine its role in the system.
3548//!
3549//! For Dual Mode controllers:
3550//!
3551//! The function returns one of the following values:
3552//!
3553//! \b USB_DUAL_MODE_HOST indicates that the controller is acting as a host.
3554//!
3555//! \b USB_DUAL_MODE_DEVICE indicates that the controller acting as a device.
3556//!
3557//! \b USB_DUAL_MODE_NONE indicates that the controller is not active as
3558//! either a host or device.
3559//!
3560//! \return Returns \b USB_OTG_MODE_ASIDE_HOST, \b USB_OTG_MODE_ASIDE_DEV,
3561//! \b USB_OTG_MODE_BSIDE_HOST, \b USB_OTG_MODE_BSIDE_DEV,
3562//! \b USB_OTG_MODE_NONE, \b USB_DUAL_MODE_HOST, \b USB_DUAL_MODE_DEVICE, or
3563//! \b USB_DUAL_MODE_NONE.
3564//
3565//*****************************************************************************
3566uint32_t USBModeGet(uint32_t ui32Base) {
3567    //
3568    // Check the arguments.
3569    //
3570    ASSERT(ui32Base == USB0_BASE);
3571
3572    //
3573    // Checks the current mode in the USB_O_DEVCTL and returns the current
3574    // mode.
3575    //
3576    // USB_OTG_MODE_ASIDE_HOST:  USB_DEVCTL_HOST | USB_DEVCTL_SESSION
3577    // USB_OTG_MODE_ASIDE_DEV:   USB_DEVCTL_SESSION
3578    // USB_OTG_MODE_BSIDE_HOST:  USB_DEVCTL_DEV | USB_DEVCTL_SESSION |
3579    //                           USB_DEVCTL_HOST
3580    // USB_OTG_MODE_BSIDE_DEV:   USB_DEVCTL_DEV | USB_DEVCTL_SESSION
3581    // USB_OTG_MODE_NONE:        USB_DEVCTL_DEV
3582    //
3583    return (HWREGB(ui32Base + USB_O_DEVCTL) &
3584            (USB_DEVCTL_DEV | USB_DEVCTL_HOST | USB_DEVCTL_SESSION |
3585             USB_DEVCTL_VBUS_M));
3586}
3587
3588//*****************************************************************************
3589//
3590//! Sets the DMA channel to use for a specified endpoint.
3591//!
3592//! \param ui32Base specifies the USB module base address.
3593//! \param ui32Endpoint specifies which endpoint's FIFO address to return.
3594//! \param ui32Channel specifies which DMA channel to use for which endpoint.
3595//!
3596//! This function is used to configure which DMA channel to use with a specified
3597//! endpoint.  Receive DMA channels can only be used with receive endpoints
3598//! and transmit DMA channels can only be used with transmit endpoints.  As a
3599//! result, the 3 receive and 3 transmit DMA channels can be mapped to any
3600//! endpoint other than 0.  The values that are passed into the
3601//! \e ui32Channel value are the UDMA_CHANNEL_USBEP* values defined in udma.h.
3602//!
3603//! \note This function only has an effect on microcontrollers that have the
3604//! ability to change the DMA channel for an endpoint.  Calling this function
3605//! on other devices has no effect.
3606//!
3607//! \return None.
3608//!
3609//*****************************************************************************
3610void USBEndpointDMAChannel(uint32_t ui32Base, uint32_t ui32Endpoint,
3611                           uint32_t ui32Channel) {
3612    uint32_t ui32Mask;
3613
3614    //
3615    // Check the arguments.
3616    //
3617    ASSERT(ui32Base == USB0_BASE);
3618    ASSERT((ui32Endpoint == USB_EP_1) || (ui32Endpoint == USB_EP_2) ||
3619           (ui32Endpoint == USB_EP_3) || (ui32Endpoint == USB_EP_4) ||
3620           (ui32Endpoint == USB_EP_5) || (ui32Endpoint == USB_EP_6) ||
3621           (ui32Endpoint == USB_EP_7));
3622    ASSERT(ui32Channel <= UDMA_CHANNEL_USBEP3TX);
3623
3624    //
3625    // The input select mask must be shifted into the correct position
3626    // based on the channel.
3627    //
3628    ui32Mask = 0xf << (ui32Channel * 4);
3629
3630    //
3631    // Clear out the current selection for the channel.
3632    //
3633    ui32Mask = HWREG(ui32Base + USB_O_DMASEL) & (~ui32Mask);
3634
3635    //
3636    // The input select is now shifted into the correct position based on the
3637    // channel.
3638    //
3639    ui32Mask |= (USBEPToIndex(ui32Endpoint)) << (ui32Channel * 4);
3640
3641    //
3642    // Write the value out to the register.
3643    //
3644    HWREG(ui32Base + USB_O_DMASEL) = ui32Mask;
3645}
3646
3647//*****************************************************************************
3648//
3649//! Change the mode of the USB controller to host.
3650//!
3651//! \param ui32Base specifies the USB module base address.
3652//!
3653//! This function changes the mode of the USB controller to host mode.
3654//!
3655//! \note This function must only be called on microcontrollers that support
3656//! OTG operation.
3657//!
3658//! \return None.
3659//
3660//*****************************************************************************
3661void USBHostMode(uint32_t ui32Base) {
3662    //
3663    // Check the arguments.
3664    //
3665    ASSERT(ui32Base == USB0_BASE);
3666
3667    //
3668    // Force mode in OTG parts that support forcing USB controller mode.
3669    // This bit is not writable in USB controllers that do not support
3670    // forcing the mode.  Not setting the USB_GPCS_DEVMOD bit makes this a
3671    // force of host mode.
3672    //
3673    HWREGB(ui32Base + USB_O_GPCS) = USB_GPCS_DEVMODOTG;
3674}
3675
3676//*****************************************************************************
3677//
3678//! Change the mode of the USB controller to device.
3679//!
3680//! \param ui32Base specifies the USB module base address.
3681//!
3682//! This function changes the mode of the USB controller to device mode.
3683//!
3684//! \note This function must only be called on microcontrollers that support
3685//! OTG operation.
3686//!
3687//! \return None.
3688//
3689//*****************************************************************************
3690void USBDevMode(uint32_t ui32Base) {
3691    //
3692    // Check the arguments.
3693    //
3694    ASSERT(ui32Base == USB0_BASE);
3695
3696    //
3697    // Set the USB controller mode to device.
3698    //
3699    HWREGB(ui32Base + USB_O_GPCS) = USB_GPCS_DEVMODOTG | USB_GPCS_DEVMOD;
3700}
3701
3702//*****************************************************************************
3703//
3704//! Changes the mode of the USB controller to OTG.
3705//!
3706//! \param ui32Base specifies the USB module base address.
3707//!
3708//! This function changes the mode of the USB controller to OTG mode.  This
3709//! function is only valid on microcontrollers that have the OTG capabilities.
3710//!
3711//! \return None.
3712//
3713//*****************************************************************************
3714void USBOTGMode(uint32_t ui32Base) {
3715    //
3716    // Check the arguments.
3717    //
3718    ASSERT(ui32Base == USB0_BASE);
3719
3720    //
3721    // Disable the override of the USB controller mode when running on an OTG
3722    // device.
3723    //
3724    HWREGB(ui32Base + USB_O_GPCS) = 0;
3725}
3726
3727//*****************************************************************************
3728//
3729//! Change the operating mode of the USB controller.
3730//!
3731//! \param ui32Base specifies the USB module base address.
3732//! \param ui32Mode specifies the operating mode of the USB OTG pins.
3733//!
3734//! This function changes the operating modes of the USB controller.  When
3735//! operating in full OTG mode, the USB controller uses the VBUS and ID pins to
3736//! detect mode and voltage changes.  While these pins are primarily used in
3737//! OTG mode, they can also affect the operation of host and device modes.  In
3738//! device mode, the USB controller can be configured to monitor or ignore
3739//! VBUS. Monitoring VBUS allows the controller to determine if it has been
3740//! disconnected from the host.  In host mode, the USB controller uses the
3741//! VBUS pin to detect loss of VBUS caused by excessive power draw due to a
3742//! drop in the VBUS voltage.  This call takes the place of USBHostMode(),
3743//! USBDevMode(), and USBOTGMode().  The \e ui32Mode value should be one of
3744//! the following values:
3745//!
3746//! - \b USB_MODE_OTG enables operating in full OTG mode, VBUS and ID are
3747//!   used by the controller.
3748//! - \b USB_MODE_HOST enables operating only as a host with no monitoring of
3749//!   VBUS or ID pins.
3750//! - \b USB_MODE_HOST_VBUS enables operating only as a host with monitoring of
3751//!   VBUS pin.  This configuration enables detection of VBUS droop while still
3752//!   forcing host mode.
3753//! - \b USB_MODE_DEVICE enables operating only as a device with no monitoring
3754//!   of VBUS or ID pins.
3755//! - \b USB_MODE_DEVICE_VBUS enables operating only as a device with
3756//!   monitoring of VBUS pin.  This configuration enables disconnect detection
3757//!   while still forcing device mode.
3758//!
3759//! \note Some of the options above are not available on some Tiva devices.
3760//! Please check the data sheet to determine if the USB controller supports a
3761//! particular mode.
3762//!
3763//! \b Example: Force device mode but allow monitoring of the USB VBUS pin.
3764//!
3765//! \verbatim
3766//! //
3767//! // Force device mode but allow monitoring of VBUS for disconnect.
3768//! //
3769//! USBModeConfig(USB_MODE_DEVICE_VBUS);
3770//! \endverbatim
3771//!
3772//! \return None.
3773//
3774//*****************************************************************************
3775void USBModeConfig(uint32_t ui32Base, uint32_t ui32Mode) {
3776    //
3777    // Check the arguments.
3778    //
3779    ASSERT(ui32Base == USB0_BASE);
3780
3781    HWREG(ui32Base + USB_O_GPCS) = ui32Mode;
3782}
3783
3784//*****************************************************************************
3785//
3786//! Powers off the internal USB PHY.
3787//!
3788//! \param ui32Base specifies the USB module base address.
3789//!
3790//! This function powers off the internal USB PHY, reducing the current
3791//! consumption of the device.  While in the powered-off state, the USB
3792//! controller is unable to operate.
3793//!
3794//! \return None.
3795//
3796//*****************************************************************************
3797void USBPHYPowerOff(uint32_t ui32Base) {
3798    //
3799    // Set the PWRDNPHY bit in the PHY, putting it into its low power mode.
3800    //
3801    HWREGB(ui32Base + USB_O_POWER) |= USB_POWER_PWRDNPHY;
3802}
3803
3804//*****************************************************************************
3805//
3806//! Powers on the internal USB PHY.
3807//!
3808//! \param ui32Base specifies the USB module base address.
3809//!
3810//! This function powers on the internal USB PHY, enabling it return to normal
3811//! operation.  By default, the PHY is powered on, so this function must
3812//! only be called if USBPHYPowerOff() has previously been called.
3813//!
3814//! \return None.
3815//
3816//*****************************************************************************
3817void USBPHYPowerOn(uint32_t ui32Base) {
3818    //
3819    // Clear the PWRDNPHY bit in the PHY, putting it into normal operating
3820    // mode.
3821    //
3822    HWREGB(ui32Base + USB_O_POWER) &= ~USB_POWER_PWRDNPHY;
3823}
3824
3825//*****************************************************************************
3826//
3827//! Sets the number of packets to request when transferring multiple bulk
3828//! packets.
3829//!
3830//! \param ui32Base specifies the USB module base address.
3831//! \param ui32Endpoint is the endpoint index to target for this write.
3832//! \param ui32Count is the number of packets to request.
3833//!
3834//! This function sets the number of consecutive bulk packets to request
3835//! when transferring multiple bulk packets with DMA.
3836//!
3837//! \note This feature is not available on all Tiva devices.  Please
3838//! check the data sheet to determine if the USB controller has a DMA
3839//! controller or if it must use the uDMA controller for DMA transfers.
3840//!
3841//! \return None.
3842//
3843//*****************************************************************************
3844void USBEndpointPacketCountSet(uint32_t ui32Base, uint32_t ui32Endpoint,
3845                               uint32_t ui32Count) {
3846    HWREG(ui32Base + USB_O_RQPKTCOUNT1 +
3847          (0x4 * (USBEPToIndex(ui32Endpoint) - 1))) = ui32Count;
3848}
3849
3850//*****************************************************************************
3851//
3852//! Returns the number of USB endpoint pairs on the device.
3853//!
3854//! \param ui32Base specifies the USB module base address.
3855//!
3856//! This function returns the number of endpoint pairs supported by the USB
3857//! controller corresponding to the passed base address.  The value returned is
3858//! the number of IN or OUT endpoints available and does not include endpoint 0
3859//! (the control endpoint).  For example, if 15 is returned, there are 15 IN
3860//! and 15 OUT endpoints available in addition to endpoint 0.
3861//!
3862//! \return Returns the number of IN or OUT endpoints available.
3863//
3864//*****************************************************************************
3865uint32_t USBNumEndpointsGet(uint32_t ui32Base) {
3866    //
3867    // Read the number of endpoints from the hardware.  The number of TX and
3868    // RX endpoints are always the same.
3869    //
3870    return (HWREGB(ui32Base + USB_O_EPINFO) & USB_EPINFO_TXEP_M);
3871}
3872
3873//*****************************************************************************
3874//
3875//! Returns the version of the USB controller.
3876//!
3877//! \param ui32Base specifies the USB module base address.
3878//!
3879//! This function returns the version number of the USB controller, which can
3880//! be be used to adjust for slight differences between the USB controllers in
3881//! the Tiva family.  The values that are returned are
3882//! \b USB_CONTROLLER_VER_0 and \b USB_CONTROLLER_VER_1.
3883//!
3884//! \note The most significant difference between \b USB_CONTROLLER_VER_0 and
3885//! \b USB_CONTROLLER_VER_1 is that \b USB_CONTROLLER_VER_1 supports the USB
3886//! controller's own bus master DMA controller, while the
3887//! \b USB_CONTROLLER_VER_0 only supports using the uDMA controller with the
3888//! USB module.
3889//!
3890//! \b Example: Get the version of the Tiva USB controller.
3891//!
3892//! \verbatim
3893//! uint32_t ui32Version;
3894//!
3895//! //
3896//! // Retrieve the version of the Tiva USB controller.
3897//! //
3898//! ui32Version = USBControllerVersion(USB0_BASE);
3899//! \endverbatim
3900//!
3901//! \return This function returns one of the \b USB_CONTROLLER_VER_ values.
3902//
3903//*****************************************************************************
3904uint32_t USBControllerVersion(uint32_t ui32Base) {
3905    //
3906    // Return the type field of the peripheral properties.  This returns
3907    // zero for all parts that did not have a peripheral property.
3908    //
3909    return (HWREG(ui32Base + USB_O_PP) & USB_PP_TYPE_M);
3910}
3911
3912//*****************************************************************************
3913//
3914//! Configures and enables the clocking to the USB controller's PHY.
3915//!
3916//! \param ui32Base specifies the USB module base address.
3917//! \param ui32Div specifies the divider for the internal USB PHY clock.
3918//! \param ui32Flags configures the internal USB PHY clock and specifies the
3919//!  clock source for a ULPI-connected PHY.
3920//!
3921//! This function configures and enables the USB PHY clock. In addition, for
3922//! systems that use a ULPI-connected external PHY, this function configures
3923//! the source for the PHY clock. The \e ui32Flags parameter specifies the
3924//! clock source with the following values:
3925//!
3926//! - \b USB_CLOCK_INTERNAL uses the internal PLL combined with the \e ui32Div
3927//! value to generate the USB clock that is used by the internal USB PHY. In
3928//! addition, when using an external ULPI-connected USB PHY, the specified
3929//! clock is output on the USB0CLK pin.
3930//! - \b USB_CLOCK_EXTERNAL specifies that USB0CLK is an input from the
3931//! ULPI-connected external PHY.
3932//!
3933//! The \e ui32Div parameter is used to specify a divider for the internal
3934//! clock if the \b USB_CLOCK_INTERNAL is specified and is ignored if
3935//! \b USB_CLOCK_EXTERNAL is specified.  When the \b USB_CLOCK_INTERNAL is
3936//! specified, the \e ui32Div value must be set so that the PLL_VCO/\e ui32Div
3937//! results in a 60-MHz clock.
3938//!
3939//! \b Example: Enable the USB clock with a 480-MHz PLL setting.
3940//!
3941//! \verbatim
3942//! //
3943//! // Enable the USB clock using a 480-MHz PLL.
3944//! // (480-MHz/8 = 60-MHz)
3945//! //
3946//! USBClockEnable(USB0_BASE, 8, USB_CLOCK_INTERNAL);
3947//! \endverbatim
3948//!
3949//! \note The ability to configure the USB PHY clock is not available on
3950//! all Tiva devices.  Please consult the data sheet for the Tiva
3951//! device that you are using to determine if this feature is available.
3952//!
3953//! \return None.
3954//
3955//*****************************************************************************
3956void USBClockEnable(uint32_t ui32Base, uint32_t ui32Div, uint32_t ui32Flags) {
3957    ASSERT(ui32Base == USB0_BASE);
3958
3959    //
3960    // Configure and enable the USB clock input.
3961    //
3962    HWREG(ui32Base + USB_O_CC) = (ui32Div - 1) | ui32Flags;
3963}
3964
3965//*****************************************************************************
3966//
3967//! Disables the clocking of the USB controller's PHY.
3968//!
3969//! \param ui32Base specifies the USB module base address.
3970//!
3971//! This function disables the USB PHY clock. This function should not be
3972//! called in applications where the USB controller is used.
3973//!
3974//! \b Example: Disable the USB PHY clock input.
3975//!
3976//! \verbatim
3977//! //
3978//! // Disable clocking of the USB controller's PHY.
3979//! //
3980//! USBClockDisable(USB0_BASE);
3981//! \endverbatim
3982//!
3983//! \note The ability to configure the USB PHY clock is not available on all
3984//! Tiva devices.  Please consult the data sheet for the Tiva device
3985//! that you are using to determine if this feature is available.
3986//!
3987//! \return None.
3988//
3989//*****************************************************************************
3990void USBClockDisable(uint32_t ui32Base) {
3991    ASSERT(ui32Base == USB0_BASE);
3992
3993    //
3994    // Disable the USB clock input.
3995    //
3996    HWREG(ui32Base + USB_O_CC) = 0;
3997}
3998
3999//*****************************************************************************
4000//
4001// Close the Doxygen group.
4002//! @}
4003//
4004//*****************************************************************************
4005//*****************************************************************************
4006//
4007//! \addtogroup usb_dma
4008//! @{
4009//
4010//*****************************************************************************
4011
4012//*****************************************************************************
4013//
4014//! Enable interrupts for a specified integrated USB DMA channel.
4015//!
4016//! \param ui32Base specifies the USB module base address.
4017//! \param ui32Channel specifies which DMA channel interrupt to enable.
4018//!
4019//! This function enables the USB DMA channel interrupt based on the
4020//! \e ui32Channel parameter.  The \e ui32Channel value is a zero-based
4021//! index of the USB DMA channel.  Once enabled, the USBDMAChannelIntStatus()
4022//! function returns if a DMA channel has generated an interrupt.
4023//!
4024//! \b Example: Enable the USB DMA channel 3 interrupt.
4025//!
4026//! \verbatim
4027//! //
4028//! // Enable the USB DMA channel 3 interrupt
4029//! //
4030//! USBDMAChannelIntEnable(USB0_BASE, 3);
4031//! \endverbatim
4032//!
4033//! \note This feature is not available on all Tiva devices.  Please
4034//! check the data sheet to determine if the USB controller has a DMA
4035//! controller or if it must use the uDMA controller for DMA transfers.
4036//!
4037//! \return None.
4038//
4039//*****************************************************************************
4040void USBDMAChannelIntEnable(uint32_t ui32Base, uint32_t ui32Channel) {
4041    ASSERT(ui32Base == USB0_BASE);
4042    ASSERT(ui32Channel < 8);
4043
4044    //
4045    // Enable the specified DMA channel interrupts.
4046    //
4047    HWREG(ui32Base + USB_O_DMACTL0 + (0x10 * ui32Channel)) |= USB_DMACTL0_IE;
4048}
4049
4050//*****************************************************************************
4051//
4052//! Disable interrupts for a specified integrated USB DMA channel.
4053//!
4054//! \param ui32Base specifies the USB module base address.
4055//! \param ui32Channel specifies which USB DMA channel interrupt to disable.
4056//!
4057//! This function disables the USB DMA channel interrupt based on the
4058//! \e ui32Channel parameter.  The \e ui32Channel value is a zero-based
4059//! index of the USB DMA channel.
4060//!
4061//! \b Example: Disable the USB DMA channel 3 interrupt.
4062//!
4063//! \verbatim
4064//! //
4065//! // Disable the USB DMA channel 3 interrupt
4066//! //
4067//! USBDMAChannelIntDisable(USB0_BASE, 3);
4068//! \endverbatim
4069//!
4070//! \note This feature is not available on all Tiva devices.  Please
4071//! check the data sheet to determine if the USB controller has a DMA
4072//! controller or if it must use the uDMA controller for DMA transfers.
4073//!
4074//! \return None.
4075//
4076//*****************************************************************************
4077void USBDMAChannelIntDisable(uint32_t ui32Base, uint32_t ui32Channel) {
4078    ASSERT(ui32Base == USB0_BASE);
4079    ASSERT(ui32Channel < 8);
4080
4081    //
4082    // Enable the specified DMA channel interrupts.
4083    //
4084    HWREG(ui32Base + USB_O_DMACTL0 + (0x10 * ui32Channel)) &= ~USB_DMACTL0_IE;
4085}
4086
4087//*****************************************************************************
4088//
4089//! Return the current status of the integrated USB DMA interrupts.
4090//!
4091//! \param ui32Base specifies the USB module base address.
4092//!
4093//! This function returns the current bit-mapped interrupt status for all USB
4094//! DMA channel interrupt sources.  Calling this function automatically clears
4095//! all currently pending USB DMA interrupts.
4096//!
4097//! \note This feature is not available on all Tiva devices.  Please
4098//! check the data sheet to determine if the USB controller has a DMA
4099//! controller or if it must use the uDMA controller for DMA transfers.
4100//!
4101//! \b Example: Get the pending USB DMA interrupts.
4102//!
4103//! \verbatim
4104//! uint32_t ui32Ints;
4105//!
4106//! //
4107//! // Get the pending USB DMA interrupts.
4108//! //
4109//! ui32Ints = USBDMAChannelIntStatus(USB0_BASE);
4110//! \endverbatim
4111//!
4112//! \return The bit-mapped interrupts for the DMA channels.
4113//
4114//*****************************************************************************
4115uint32_t USBDMAChannelIntStatus(uint32_t ui32Base) {
4116    ASSERT(ui32Base == USB0_BASE);
4117
4118    return (HWREG(ui32Base + USB_O_DMAINTR));
4119}
4120
4121//*****************************************************************************
4122//
4123//! Enables integrated USB DMA for a specified channel.
4124//!
4125//! \param ui32Base specifies the USB module base address.
4126//! \param ui32Channel specifies the USB DMA channel to enable.
4127//!
4128//! This function enables the USB DMA channel passed in the \e ui32Channel
4129//! parameter.  The \e ui32Channel value is a zero-based index of the USB DMA
4130//! channel.
4131//!
4132//! \b Example: Enable USB DMA channel 2.
4133//!
4134//! \verbatim
4135//! //
4136//! // Enable USB DMA channel 2.
4137//! //
4138//! USBDMAChannelEnable(2);
4139//! \endverbatim
4140//!
4141//! \note This feature is not available on all Tiva devices.  Please
4142//! check the data sheet to determine if the USB controller has a DMA
4143//! controller or if it must use the uDMA controller for DMA transfers.
4144//!
4145//! \return None.
4146//
4147//*****************************************************************************
4148void USBDMAChannelEnable(uint32_t ui32Base, uint32_t ui32Channel) {
4149    ASSERT(ui32Base == USB0_BASE);
4150    ASSERT(ui32Channel < 8);
4151
4152    //
4153    // Enable the USB DMA channel.
4154    //
4155    HWREG(ui32Base + USB_O_DMACTL0 + (0x10 * ui32Channel)) |=
4156        USB_DMACTL0_ENABLE;
4157}
4158
4159//*****************************************************************************
4160//
4161//! Disables integrated USB DMA for a specified channel.
4162//!
4163//! \param ui32Base specifies the USB module base address.
4164//! \param ui32Channel specifies the USB DMA channel to disable.
4165//!
4166//! This function disables the USB DMA channel passed in the \e ui32Channel
4167//! parameter.  The \e ui32Channel parameter is a zero-based index of the DMA
4168//! channel.
4169//!
4170//! \b Example: Disable USB DMA channel 2.
4171//!
4172//! \verbatim
4173//! //
4174//! // Disable USB DMA channel 2.
4175//! //
4176//! USBDMAChannelDisable(2);
4177//! \endverbatim
4178//!
4179//! \note This feature is not available on all Tiva devices.  Please
4180//! check the data sheet to determine if the USB controller has a DMA
4181//! controller or if it must use the uDMA controller for DMA transfers.
4182//!
4183//! \return None.
4184//
4185//*****************************************************************************
4186void USBDMAChannelDisable(uint32_t ui32Base, uint32_t ui32Channel) {
4187    ASSERT(ui32Base == USB0_BASE);
4188    ASSERT(ui32Channel < 8);
4189
4190    //
4191    // Disable the USB DMA channel.
4192    //
4193    HWREG(ui32Base + USB_O_DMACTL0 + (0x10 * ui32Channel)) &=
4194        ~USB_DMACTL0_ENABLE;
4195}
4196
4197//*****************************************************************************
4198//
4199//! Assigns and configures an endpoint to a specified integrated USB DMA
4200//! channel.
4201//!
4202//! \param ui32Base specifies the USB module base address.
4203//! \param ui32Channel specifies which DMA channel to access.
4204//! \param ui32Endpoint is the endpoint to assign to the USB DMA channel.
4205//! \param ui32Config is used to specify the configuration of the USB DMA
4206//! channel.
4207//!
4208//! This function assigns an endpoint and configures the settings for a
4209//! USB DMA channel.  The \e ui32Endpoint parameter is one of the
4210//! \b USB_EP_* values and the \e ui32Channel value is a zero-based index of
4211//! the DMA channel to configure.  The \e ui32Config parameter is a combination
4212//! of the \b USB_DMA_CFG_* values using the following guidelines.
4213//!
4214//! Use one of the following to set the DMA burst mode:
4215//! - \b USB_DMA_CFG_BURST_NONE disables bursting.
4216//! - \b USB_DMA_CFG_BURST_4 sets the DMA burst size to 4 words.
4217//! - \b USB_DMA_CFG_BURST_8 sets the DMA burst size to 8 words.
4218//! - \b USB_DMA_CFG_BURST_16 sets the DMA burst size to 16 words.
4219//!
4220//! Use one of the following to set the DMA mode:
4221//! - \b USB_DMA_CFG_MODE_0 is typically used when only a single packet is
4222//!   being sent via DMA and triggers one completion interrupt per packet.
4223//! - \b USB_DMA_CFG_MODE_1 is typically used when multiple packets are being
4224//!   sent via DMA and triggers one completion interrupt per transfer.
4225//!
4226//! Use one of the following to set the direction of the transfer:
4227//! - \b USB_DMA_CFG_DIR_RX selects a DMA transfer from the endpoint to a
4228//!   memory location.
4229//! - \b USB_DMA_CFG_DIR_TX selects a DMA transfer to the endpoint from a
4230//!   memory location.
4231//!
4232//! The following two optional settings allow an application to immediately
4233//! enable the DMA transfer and/or DMA interrupts when configuring the DMA
4234//! channel:
4235//! - \b USB_DMA_CFG_INT_EN enables interrupts for this channel immediately so
4236//!   that an added call to USBDMAChannelIntEnable() is not necessary.
4237//! - \b USB_DMA_CFG_EN enables the DMA channel immediately so that an added
4238//!   call to USBDMAChannelEnable() is not necessary.
4239//!
4240//! \b Example: Assign channel 0 to endpoint 1 in DMA mode 0, 4 word burst,
4241//!    enable interrupts and immediately enable the transfer.
4242//!
4243//! \verbatim
4244//! //
4245//! // Assign channel 0 to endpoint 1 in DMA mode 0, 4 word bursts,
4246//! // enable interrupts and immediately enable the transfer.
4247//! //
4248//! USBDMAChannelConfigSet(USB0_BASE, 0, USB_EP_1,
4249//!                        (USB_DMA_CFG_BURST_4 | USB_DMA_CFG_MODE0 |
4250//!                         USB_DMA_CFG_DIR_RX | USB_DMA_CFG_INT_EN |
4251//!                         USB_DMA_CFG_EN));
4252//! \endverbatim
4253//!
4254//! \note This feature is not available on all Tiva devices.  Please
4255//! check the data sheet to determine if the USB controller has a DMA
4256//! controller or if it must use the uDMA controller for DMA transfers.
4257//!
4258//! \return None.
4259//
4260//*****************************************************************************
4261void USBDMAChannelConfigSet(uint32_t ui32Base, uint32_t ui32Channel,
4262                            uint32_t ui32Endpoint, uint32_t ui32Config) {
4263    ASSERT(ui32Base == USB0_BASE);
4264    ASSERT(ui32Channel < 8);
4265    ASSERT((ui32Endpoint & ~USB_EP_7) == 0);
4266
4267    //
4268    // Reset this USB DMA channel.
4269    //
4270    HWREG(ui32Base + USB_O_DMACTL0 + (0x10 * ui32Channel)) = 0;
4271
4272    //
4273    // Set the configuration of the requested channel.
4274    //
4275    HWREG(ui32Base + USB_O_DMACTL0 + (0x10 * ui32Channel)) =
4276        ui32Config | ui32Endpoint;
4277}
4278
4279//*****************************************************************************
4280//
4281//! Returns the current status for an integrated USB DMA channel.
4282//!
4283//! \param ui32Base specifies the USB module base address.
4284//! \param ui32Channel specifies which DMA channel to query.
4285//!
4286//! This function returns the current status for the USB DMA channel specified
4287//! by the \e ui32Channel parameter.  The \e ui32Channel value is a zero-based
4288//! index of the USB DMA channel to query.
4289//!
4290//! \b Example: Get the current USB DMA status for channel 2.
4291//!
4292//! \verbatim
4293//! uint32_t ui32Status;
4294//!
4295//! //
4296//! // Get the current USB DMA status for channel 2.
4297//! //
4298//! ui32Status = USBDMAChannelStatus(USB0_BASE, 2);
4299//! \endverbatim
4300//!
4301//! \note This feature is not available on all Tiva devices.  Please
4302//! check the data sheet to determine if the USB controller has a DMA
4303//! controller or if it must use the uDMA controller for DMA transfers.
4304//!
4305//! \return Returns zero or \b USB_DMACTL0_ERR if there is a pending error
4306//! condition on a DMA channel.
4307//
4308//*****************************************************************************
4309uint32_t USBDMAChannelStatus(uint32_t ui32Base, uint32_t ui32Channel) {
4310    ASSERT(ui32Base == USB0_BASE);
4311    ASSERT(ui32Channel < 8);
4312
4313    //
4314    // Return a non-zero value if there is a pending error condition.
4315    //
4316    return (HWREG(ui32Base + USB_O_DMACTL0 + (0x10 * ui32Channel)) &
4317            USB_DMACTL0_ERR);
4318}
4319
4320//*****************************************************************************
4321//
4322//! Clears the integrated USB DMA status for a specified channel.
4323//!
4324//! \param ui32Base specifies the USB module base address.
4325//! \param ui32Channel specifies which DMA channel to clear.
4326//! \param ui32Status holds the status bits to clear.
4327//!
4328//! This function clears the USB DMA channel status for the channel specified
4329//! by the \e ui32Channel parameter.  The \e ui32Channel value is a zero-based
4330//! index of the USB DMA channel to query.  The \e ui32Status parameter
4331//! specifies the status bits to clear and must be the valid values that are
4332//! returned from a call to the USBDMAChannelStatus() function.
4333//!
4334//! \b Example: Clear the current USB DMA status for channel 2.
4335//!
4336//! \verbatim
4337//! //
4338//! // Clear the any pending USB DMA status for channel 2.
4339//! //
4340//! USBDMAChannelStatusClear(USB0_BASE, 2, USBDMAChannelStatus(USB0_BASE, 2));
4341//! \endverbatim
4342//!
4343//! \note This feature is not available on all Tiva devices.  Please
4344//! check the data sheet to determine if the USB controller has a DMA
4345//! controller or if it must use the uDMA controller for DMA transfers.
4346//!
4347//! \return None.
4348//
4349//*****************************************************************************
4350void USBDMAChannelStatusClear(uint32_t ui32Base, uint32_t ui32Channel,
4351                              uint32_t ui32Status) {
4352    ASSERT(ui32Base == USB0_BASE);
4353    ASSERT(ui32Channel < 8);
4354
4355    //
4356    // The only status is the error bit.
4357    //
4358    ui32Status &= USB_DMACTL0_ERR;
4359
4360    //
4361    // Clear the specified error condition.
4362    //
4363    HWREG(ui32Base + USB_O_DMACTL0 + (0x10 * ui32Channel)) &= ~ui32Status;
4364}
4365
4366//*****************************************************************************
4367//
4368//! Sets the source or destination address for an integrated USB DMA transfer
4369//! on a specified channel.
4370//!
4371//! \param ui32Base specifies the USB module base address.
4372//! \param ui32Channel specifies which DMA channel to configure.
4373//! \param pvAddress specifies the source or destination address for the USB
4374//! DMA transfer.
4375//!
4376//! This function sets the source or destination address for the USB DMA
4377//! channel number specified in the \e ui32Channel parameter.  The
4378//! \e ui32Channel value is a zero-based index of the USB DMA channel.  The
4379//! \e pvAddress parameter is a source address if the transfer type for the DMA
4380//! channel is transmit and a destination address if the transfer type is
4381//! receive.
4382//!
4383//! \b Example: Set the transfer address for USB DMA channel 1.
4384//!
4385//! \verbatim
4386//! void *pvBuffer;
4387//!
4388//! //
4389//! // Set the address for USB DMA channel 1.
4390//! //
4391//! USBDMAChannelAddressSet(USB0_BASE, 1, pvBuffer);
4392//! \endverbatim
4393//!
4394//! \note This feature is not available on all Tiva devices.  Please
4395//! check the data sheet to determine if the USB controller has a DMA
4396//! controller or if it must use the uDMA controller for DMA transfers.
4397//!
4398//! \return None.
4399//
4400//*****************************************************************************
4401void USBDMAChannelAddressSet(uint32_t ui32Base, uint32_t ui32Channel,
4402                             void* pvAddress) {
4403    ASSERT(ui32Base == USB0_BASE);
4404    ASSERT(ui32Channel < 8);
4405
4406    //
4407    // Set the DMA address.
4408    //
4409    HWREG(ui32Base + USB_O_DMAADDR0 + (0x10 * ui32Channel)) =
4410        (uint32_t)pvAddress;
4411}
4412
4413//*****************************************************************************
4414//
4415//! Returns the source or destination address for the specified integrated USB
4416//! DMA channel.
4417//!
4418//! \param ui32Base specifies the USB module base address.
4419//! \param ui32Channel specifies the USB DMA channel.
4420//!
4421//! This function returns the DMA address for the channel number specified
4422//! in the \e ui32Channel parameter.  The \e ui32Channel value is a zero-based
4423//! index of the DMA channel to query.  This function must not be used on
4424//! devices that return \b USB_CONTROLLER_VER_0 from the USBControllerVersion()
4425//! function.
4426//!
4427//! \b Example: Get the transfer address for USB DMA channel 1.
4428//!
4429//! \verbatim
4430//! void *pvBuffer;
4431//!
4432//! //
4433//! // Retrieve the current DMA address for channel 1.
4434//! //
4435//! pvBuffer = USBDMAChannelAddressGet(USB0_BASE, 1);
4436//! \endverbatim
4437//!
4438//! \note This feature is not available on all Tiva devices.  Please
4439//! check the data sheet to determine if the USB controller has a DMA
4440//! controller or if it must use the uDMA controller for DMA transfers.
4441//!
4442//! \return The current DMA address for a USB DMA channel.
4443//
4444//*****************************************************************************
4445void* USBDMAChannelAddressGet(uint32_t ui32Base, uint32_t ui32Channel) {
4446    ASSERT(ui32Base == USB0_BASE);
4447    ASSERT(ui32Channel < 8);
4448
4449    //
4450    // Return the current DMA address.
4451    //
4452    return ((void*)HWREG(ui32Base + USB_O_DMAADDR0 + (0x10 * ui32Channel)));
4453}
4454
4455//*****************************************************************************
4456//
4457//! Sets the transfer count for an integrated USB DMA channel.
4458//!
4459//! \param ui32Base specifies the USB module base address.
4460//! \param ui32Channel specifies which DMA channel to access.
4461//! \param ui32Count specifies the number of bytes to transfer.
4462//!
4463//! This function sets the USB DMA transfer count in bytes for the channel
4464//! number specified in the \e ui32Channel parameter.  The \e ui32Channel
4465//! value is a zero-based index of the DMA channel.
4466//!
4467//! \b Example: Set the transfer count to 512 bytes for USB DMA channel 1.
4468//!
4469//! \verbatim
4470//! //
4471//! // Set the transfer count to 512 bytes for USB DMA channel 1.
4472//! //
4473//! USBDMAChannelCountSet(USB0_BASE, 1, 512);
4474//! \endverbatim
4475//!
4476//! \note This feature is not available on all Tiva devices.  Please
4477//! check the data sheet to determine if the USB controller has a DMA
4478//! controller or if it must use the uDMA controller for DMA transfers.
4479//!
4480//! \return None.
4481//
4482//*****************************************************************************
4483void USBDMAChannelCountSet(uint32_t ui32Base, uint32_t ui32Channel,
4484                           uint32_t ui32Count) {
4485    ASSERT(ui32Base == USB0_BASE);
4486    ASSERT(ui32Channel < 8);
4487
4488    //
4489    // Set the USB DMA count for the channel.
4490    //
4491    HWREG(ui32Base + USB_O_DMACOUNT0 + (0x10 * ui32Channel)) = ui32Count;
4492}
4493
4494//*****************************************************************************
4495//
4496//! Returns the transfer count for an integrated USB DMA channel.
4497//!
4498//! \param ui32Base specifies the USB module base address.
4499//! \param ui32Channel specifies which DMA channel to access.
4500//!
4501//! This function returns the USB DMA transfer count in bytes for the channel
4502//! number specified in the \e ui32Channel parameter.  The \e ui32Channel value
4503//! is a zero-based index of the DMA channel to query.
4504//!
4505//! \b Example: Get the transfer count for USB DMA channel 1.
4506//!
4507//! \verbatim
4508//! uint32_t ui32Count;
4509//!
4510//! //
4511//! // Get the transfer count for USB DMA channel 1.
4512//! //
4513//! ui32Count = USBDMAChannelCountGet(USB0_BASE, 1);
4514//! \endverbatim
4515//!
4516//! \note This feature is not available on all Tiva devices.  Please
4517//! check the data sheet to determine if the USB controller has a DMA
4518//! controller or if it must use the uDMA controller for DMA transfers.
4519//!
4520//! \return The current count for a USB DMA channel.
4521//
4522//*****************************************************************************
4523uint32_t USBDMAChannelCountGet(uint32_t ui32Base, uint32_t ui32Channel) {
4524    ASSERT(ui32Base == USB0_BASE);
4525    ASSERT(ui32Channel < 8);
4526
4527    //
4528    // Return the current DMA count.
4529    //
4530    return (HWREG(ui32Base + USB_O_DMACOUNT0 + (0x10 * ui32Channel)));
4531}
4532
4533//*****************************************************************************
4534//
4535//! Returns the available number of integrated USB DMA channels.
4536//!
4537//! \param ui32Base specifies the USB module base address.
4538//!
4539//! This function returns the total number of DMA channels available when using
4540//! the integrated USB DMA controller.  This function returns 0 if the
4541//! integrated controller is not present.
4542//!
4543//! \b Example: Get the number of integrated DMA channels.
4544//!
4545//! \verbatim
4546//! uint32_t ui32Count;
4547//!
4548//! //
4549//! // Get the number of integrated DMA channels.
4550//! //
4551//! ui32Count = USBDMANumChannels(USB0_BASE);
4552//! \endverbatim
4553//!
4554//! \return The number of integrated USB DMA channels or zero if the
4555//! integrated USB DMA controller is not present.
4556//
4557//*****************************************************************************
4558uint32_t USBDMANumChannels(uint32_t ui32Base) {
4559    ASSERT(ui32Base == USB0_BASE);
4560
4561    //
4562    // Return the number of DMA channels for the integrated DMA controller.
4563    //
4564    return (HWREG(ui32Base + USB_O_RAMINFO) >> USB_RAMINFO_DMACHAN_S);
4565}
4566//*****************************************************************************
4567//
4568// Close the Doxygen group.
4569//! @}
4570//
4571//*****************************************************************************
4572//*****************************************************************************
4573//
4574//! \addtogroup usb_ulpi
4575//! @{
4576//
4577//*****************************************************************************
4578
4579//*****************************************************************************
4580//
4581//! Configures the USB controller's ULPI function.
4582//!
4583//! \param ui32Base specifies the USB module base address.
4584//! \param ui32Config contains the configuration options.
4585//!
4586//! This function is used to configure the USB controller's ULPI function.
4587//! The configuration options are set in the \e ui32Config parameter and are a
4588//! logical OR of the following values:
4589//!
4590//! - \b USB_ULPI_EXTVBUS enables the external ULPI PHY as the source for VBUS
4591//!   signaling.
4592//! - \b USB_ULPI_EXTVBUS_IND enables the external ULPI PHY to detect external
4593//!   VBUS over-current condition.
4594//!
4595//! \b Example: Enable ULPI PHY with full VBUS control.
4596//!
4597//! \verbatim
4598//! //
4599//! // Enable ULPI PHY with full VBUS control.
4600//! //
4601//! USBULPIConfig(USB0_BASE, USB_ULPI_EXTVBUS | USB_ULPI_EXTVBUS_IND);
4602//! \endverbatim
4603//!
4604//! \note The USB ULPI feature is not available on all Tiva devices.
4605//! Please consult the data sheet for the Tiva device that you
4606//! are using to determine if this feature is available.
4607//!
4608//! \return None.
4609//
4610//*****************************************************************************
4611void USBULPIConfig(uint32_t ui32Base, uint32_t ui32Config) {
4612    HWREGB(ui32Base + USB_O_ULPIVBUSCTL) = (uint8_t)ui32Config;
4613}
4614
4615//*****************************************************************************
4616//
4617//! Enables the USB controller's ULPI function.
4618//!
4619//! \param ui32Base specifies the USB module base address.
4620//!
4621//! This function enables the USB controller's ULPI function and must be
4622//! called before attempting to access an external ULPI-connected USB PHY.
4623//!
4624//! \b Example: Enable ULPI function.
4625//!
4626//! \verbatim
4627//! //
4628//! // Enable ULPI function.
4629//! //
4630//! USBULPIEnable(USB0_BASE);
4631//! \endverbatim
4632//!
4633//! \note The USB ULPI feature is not available on all Tiva devices.
4634//! Please consult the data sheet for the Tiva device that you
4635//! are using to determine if this feature is available.
4636//!
4637//! \return None.
4638//
4639//*****************************************************************************
4640void USBULPIEnable(uint32_t ui32Base) {
4641    HWREG(ui32Base + USB_O_PC) |= USB_PC_ULPIEN;
4642}
4643
4644//*****************************************************************************
4645//
4646//! Disables the USB controller's ULPI function.
4647//!
4648//! \param ui32Base specifies the USB module base address.
4649//!
4650//! This function disables the USB controller's ULPI function.  Accesses to
4651//! the external ULPI-connected PHY cannot succeed after this function has been
4652//! called.
4653//!
4654//! \b Example: Disable ULPI function.
4655//!
4656//! \verbatim
4657//! //
4658//! // Disable ULPI function.
4659//! //
4660//! USBULPIDisable(USB0_BASE);
4661//! \endverbatim
4662//!
4663//! \note The USB ULPI feature is not available on all Tiva devices.
4664//! Please consult the data sheet for the Tiva device that you
4665//! are using to determine if this feature is available.
4666//!
4667//! \return None.
4668//
4669//*****************************************************************************
4670void USBULPIDisable(uint32_t ui32Base) {
4671    HWREG(ui32Base + USB_O_PC) &= ~USB_PC_ULPIEN;
4672}
4673
4674//*****************************************************************************
4675//
4676//! Reads a register from an external ULPI-connected USB PHY.
4677//!
4678//! \param ui32Base specifies the USB module base address.
4679//! \param ui8Reg specifies the register address to read.
4680//!
4681//! This function reads the register address specified in the \e ui8Reg
4682//! parameter using the ULPI function.  This function is blocking and only
4683//! returns when the read access completes.  The function does not return if
4684//! there is not a ULPI-connected USB PHY present.
4685//!
4686//! \b Example: Read a register from the ULPI PHY.
4687//!
4688//! \verbatim
4689//! uint8_t ui8Value;
4690//!
4691//! //
4692//! // Read a register from the ULPI PHY register at 0x10.
4693//! //
4694//! ui8Value = USBULPIRegRead(USB0_BASE, 0x10);
4695//! \endverbatim
4696//!
4697//! \note The USB ULPI feature is not available on all Tiva devices.
4698//! Please consult the data sheet for the Tiva device that you
4699//! are using to determine if this feature is available.
4700//!
4701//! \return The value of the requested ULPI register.
4702//
4703//*****************************************************************************
4704uint8_t USBULPIRegRead(uint32_t ui32Base, uint8_t ui8Reg) {
4705    ASSERT(ui32Base == USB0_BASE);
4706
4707    //
4708    // Set the register address and initiate a read access.
4709    //
4710    HWREGB(ui32Base + USB_O_ULPIREGADDR) = ui8Reg;
4711    HWREGB(ui32Base + USB_O_ULPIREGCTL) =
4712        USB_ULPIREGCTL_RDWR | USB_ULPIREGCTL_REGACC;
4713
4714    //
4715    // Wait for the access to complete.
4716    //
4717    while ((HWREGB(ui32Base + USB_O_ULPIREGCTL) & USB_ULPIREGCTL_REGCMPLT) ==
4718           0) {}
4719
4720    //
4721    // Clear the register access complete flag.
4722    //
4723    HWREGB(ui32Base + USB_O_ULPIREGCTL) = 0;
4724
4725    return (HWREGB(ui32Base + USB_O_ULPIREGDATA));
4726}
4727
4728//*****************************************************************************
4729//
4730//! Writes a value to a register on an external ULPI-connected USB PHY.
4731//!
4732//! \param ui32Base specifies the USB module base address.
4733//! \param ui8Reg specifies the register address to write.
4734//! \param ui8Data specifies the data to write.
4735//!
4736//! This function writes the register address specified in the \e ui8Reg
4737//! parameter with the value specified in the \e ui8Data parameter using the
4738//! ULPI function.  This function is blocking and only returns when the
4739//! write access completes.  The function does not return if there is not a
4740//! ULPI-connected USB PHY present.
4741//!
4742//! \b Example: Write a register from the external ULPI PHY.
4743//!
4744//! \verbatim
4745//! //
4746//! // Write the ULPI PHY register at 0x10 with 0x20.
4747//! //
4748//! USBULPIRegWrite(USB0_BASE, 0x10, 0x20);
4749//! \endverbatim
4750//!
4751//! \note The USB ULPI feature is not available on all Tiva devices.
4752//! Please consult the data sheet for the Tiva device that you
4753//! are using to determine if this feature is available.
4754//!
4755//! \return None.
4756//
4757//*****************************************************************************
4758void USBULPIRegWrite(uint32_t ui32Base, uint8_t ui8Reg, uint8_t ui8Data) {
4759    ASSERT(ui32Base == USB0_BASE);
4760
4761    //
4762    // Set the register address and initiate a read access.
4763    //
4764    HWREGB(ui32Base + USB_O_ULPIREGADDR) = ui8Reg;
4765    HWREGB(ui32Base + USB_O_ULPIREGDATA) = ui8Data;
4766    HWREGB(ui32Base + USB_O_ULPIREGCTL) = USB_ULPIREGCTL_REGACC;
4767
4768    //
4769    // Wait for the access to complete.
4770    //
4771    while ((HWREGB(ui32Base + USB_O_ULPIREGCTL) & USB_ULPIREGCTL_REGCMPLT) ==
4772           0) {}
4773
4774    //
4775    // Clear the register access complete flag.
4776    //
4777    HWREGB(ui32Base + USB_O_ULPIREGCTL) = 0;
4778}
4779
4780//*****************************************************************************
4781//
4782// Close the Doxygen group.
4783//! @}
4784//
4785//*****************************************************************************
4786//*****************************************************************************
4787//
4788//! \addtogroup usb_lpm
4789//! @{
4790//
4791//*****************************************************************************
4792
4793//*****************************************************************************
4794//
4795//! Sends an LPM request to a device at a specified address and endpoint number.
4796//!
4797//! \param ui32Base specifies the USB module base address.
4798//! \param ui32Address is the target device address for the LPM request.
4799//! \param ui32Endpoint is the target endpoint for the LPM request.
4800//!
4801//! This function sends an LPM request to a connected device in host mode.
4802//! The \e ui32Address parameter specifies the device address and has a range
4803//! of values from 1 to 127.   The \e ui32Endpoint parameter specifies the
4804//! endpoint on the device to which to send the LPM request and must be one of
4805//! the \b USB_EP_* values. The function returns before the LPM request is
4806//! sent, requiring the caller to poll the USBLPMIntStatus() function or wait
4807//! for an interrupt to signal completion of the LPM transaction.  This
4808//! function must only be called after the USBHostLPMConfig() has configured
4809//! the LPM transaction settings.
4810//!
4811//! \b Example: Send an LPM request to the device at address 1 on endpoint 0.
4812//!
4813//! \verbatim
4814//! //
4815//! // Send an LPM request to the device at address 1 on endpoint 0.
4816//! //
4817//! USBHostLPMSend(USB0_BASE, 1, USB_EP_0);
4818//! \endverbatim
4819//!
4820//! \note This function must only be called in host mode. The USB LPM feature
4821//! is not available on all Tiva devices. Please consult the data sheet for
4822//! the Tiva device that you are using to determine if this feature is
4823//! available.
4824//!
4825//! \return None.
4826//
4827//*****************************************************************************
4828void USBHostLPMSend(uint32_t ui32Base, uint32_t ui32Address,
4829                    uint32_t ui32Endpoint) {
4830    uint32_t ui32Reg;
4831
4832    ASSERT(ui32Base == USB0_BASE);
4833    ASSERT(ui32Address < 127);
4834
4835    //
4836    // Set the address and endpoint.
4837    //
4838    HWREGB(ui32Base + USB_O_LPMFADDR) = ui32Address;
4839
4840    ui32Reg = HWREGH(ui32Base + USB_O_LPMATTR) & ~USB_LPMATTR_ENDPT_M;
4841    ui32Reg |= (USBEPToIndex(ui32Endpoint) << USB_LPMATTR_ENDPT_S);
4842
4843    HWREGH(ui32Base + USB_O_LPMATTR) = ui32Reg;
4844
4845    //
4846    // Send the LPM transaction.
4847    //
4848    HWREGB(ui32Base + USB_O_LPMCNTRL) |= USB_LPMCNTRL_TXLPM;
4849}
4850
4851//*****************************************************************************
4852//
4853//! Sets the global configuration for all LPM requests.
4854//!
4855//! \param ui32Base specifies the USB module base address.
4856//! \param ui32ResumeTime specifies the resume signaling duration in 75us
4857//! increments.
4858//! \param ui32Config specifies the combination of configuration options for
4859//! LPM transactions.
4860//!
4861//! This function sets the global configuration options for LPM transactions
4862//! and must be called at least once before ever calling USBHostLPMSend().  The
4863//! \e ui32ResumeTime specifies the length of time that the host drives resume
4864//! signaling on the bus in microseconds.  The valid values
4865//! for \e ui32ResumeTime are from 50us to 1175us in 75us increments.  The
4866//! remaining configuration is specified by the \e ui32Config parameter and
4867//! includes the following options:
4868//!
4869//! - \b USB_HOST_LPM_RMTWAKE allows the device to signal a remote wake from
4870//!   the LPM state.
4871//! - \b USB_HOST_LPM_L1 is the LPM mode to enter and must always be included
4872//!   in the configuration.
4873//!
4874//! \b Example: Set the LPM configuration to allow remote wake with a resume
4875//! duration of 500us.
4876//!
4877//! \verbatim
4878//! //
4879//! // Set the LPM configuration to allow remote wake with a resume
4880//! // duration of 500us.
4881//! //
4882//! USBHostLPMConfig(USB0_BASE, 500, USB_HOST_LPM_RMTWAKE | USB_HOST_LPM_L1);
4883//! \endverbatim
4884//!
4885//! \note This function must only be called in host mode. The USB LPM feature
4886//! is not available on all Tiva devices. Please consult the data sheet for
4887//! the Tiva device that you are using to determine if this feature is
4888//! available.
4889//!
4890//! \return None.
4891//
4892//*****************************************************************************
4893void USBHostLPMConfig(uint32_t ui32Base, uint32_t ui32ResumeTime,
4894                      uint32_t ui32Config) {
4895    ASSERT(ui32Base == USB0_BASE);
4896    ASSERT(ui32ResumeTime <= 1175);
4897    ASSERT(ui32ResumeTime >= 50);
4898
4899    //
4900    // Set the Host Initiated Resume Duration, Remote wake and Suspend mode.
4901    //
4902    HWREGH(ui32Base + USB_O_LPMATTR) = ui32Config | ((ui32ResumeTime - 50) / 75)
4903                                                        << USB_LPMATTR_HIRD_S;
4904}
4905
4906//*****************************************************************************
4907//
4908//! Initiates resume signaling to wake a device from LPM suspend mode.
4909//!
4910//! \param ui32Base specifies the USB module base address.
4911//!
4912//! In host mode, this function initiates resume signaling to wake a device
4913//! that has entered an LPM-triggered low power mode.  This LPM-triggered low
4914//! power mode is entered when the USBHostLPMSend() is called to put a specific
4915//! device into a low power state.
4916//!
4917//! \b Example: Initiate resume signaling.
4918//!
4919//! \verbatim
4920//! //
4921//! // Initiate resume signaling.
4922//! //
4923//! USBHostLPMResume(USB0_BASE);
4924//! \endverbatim
4925//!
4926//! \note This function must only be called in host mode. The USB LPM feature
4927//! is not available on all Tiva devices. Please consult the data sheet for
4928//! the Tiva device that you are using to determine if this feature is
4929//! available.
4930//!
4931//! \return None.
4932//
4933//*****************************************************************************
4934void USBHostLPMResume(uint32_t ui32Base) {
4935    ASSERT(ui32Base == USB0_BASE);
4936
4937    //
4938    // Send Resume signaling.
4939    //
4940    HWREGB(ui32Base + USB_O_LPMCNTRL) |= USB_LPMCNTRL_RES;
4941}
4942
4943//*****************************************************************************
4944//
4945//! Initiates remote wake signaling to request the device to leave LPM
4946//! suspend mode.
4947//!
4948//! \param ui32Base specifies the USB module base address.
4949//!
4950//! This function initiates remote wake signaling to request that the host
4951//! wake a device that has entered an LPM-triggered low power mode.
4952//!
4953//! \b Example: Initiate remote wake signaling.
4954//!
4955//! \verbatim
4956//! //
4957//! // Initiate remote wake signaling.
4958//! //
4959//! USBDevLPMRemoteWake(USB0_BASE);
4960//! \endverbatim
4961//!
4962//! \note This function must only be called in device mode. The USB LPM feature
4963//! is not available on all Tiva devices. Please consult the data sheet for
4964//! the Tiva device that you are using to determine if this feature is
4965//! available.
4966//!
4967//! \return None.
4968//
4969//*****************************************************************************
4970void USBDevLPMRemoteWake(uint32_t ui32Base) {
4971    ASSERT(ui32Base == USB0_BASE);
4972
4973    //
4974    // Send remote wake signaling.
4975    //
4976    HWREGB(ui32Base + USB_O_LPMCNTRL) |= USB_LPMCNTRL_RES;
4977}
4978
4979//*****************************************************************************
4980//
4981//! Configures the USB device mode response to LPM requests.
4982//!
4983//! \param ui32Base specifies the USB module base address.
4984//! \param ui32Config is the combination of configuration options for LPM
4985//! transactions in device mode.
4986//!
4987//! This function sets the global configuration options for LPM
4988//! transactions in device mode and must be called before ever calling
4989//! USBDevLPMEnable() to set the configuration for LPM transactions.  The
4990//! configuration options in device mode are specified in the \e ui32Config
4991//! parameter and include one of the following:
4992//!
4993//! - \b USB_DEV_LPM_NONE disables the USB controller from responding to LPM
4994//!   transactions.
4995//! - \b USB_DEV_LPM_EN enables the USB controller to respond to LPM
4996//!   and extended transactions.
4997//! - \b USB_DEV_LPM_EXTONLY enables the USB controller to respond to
4998//!   extended transactions, but not LPM transactions.
4999//!
5000//! The \e ui32Config option can also optionally include the
5001//! \b USB_DEV_LPM_NAK value to cause the USB controller to NAK all
5002//! transactions other than an LPM transaction once the USB controller is in
5003//! LPM suspend mode.  If this value is not included in the \e ui32Config
5004//! parameter, the USB controller does not respond in suspend mode.
5005//!
5006//! The USB controller does not enter LPM suspend mode until the application
5007//! calls the USBDevLPMEnable() function.
5008//!
5009//! \b Example: Enable LPM transactions and NAK while in LPM suspend mode.
5010//!
5011//! \verbatim
5012//! //
5013//! // Enable LPM transactions and NAK while in LPM suspend mode.
5014//! //
5015//! USBDevLPMConfig(USB0_BASE, USB_DEV_LPM_NAK | USB_DEV_LPM_EN);
5016//! \endverbatim
5017//!
5018//! \note This function must only be called in device mode. The USB LPM feature
5019//! is not available on all Tiva devices. Please consult the data sheet for
5020//! the Tiva device that you are using to determine if this feature is
5021//! available.
5022//!
5023//! \return None.
5024//
5025//*****************************************************************************
5026void USBDevLPMConfig(uint32_t ui32Base, uint32_t ui32Config) {
5027    ASSERT(ui32Base == USB0_BASE);
5028
5029    //
5030    // Set the device LPM configuration.
5031    //
5032    HWREGB(ui32Base + USB_O_LPMCNTRL) = ui32Config;
5033}
5034
5035//*****************************************************************************
5036//
5037//! Enables the USB controller to respond to LPM suspend requests.
5038//!
5039//! \param ui32Base specifies the USB module base address.
5040//!
5041//! This function is used to automatically respond to an LPM sleep request from
5042//! the USB host controller.  If there is no data pending in any transmit
5043//! FIFOs, then the USB controller acknowledges the packet and enters the
5044//! LPM L1 state and generates the \b USB_INTLPM_ACK interrupt.  If the USB
5045//! controller has pending transmit data in at least one FIFO, then the USB
5046//! controller responds with NYET and signals the \b USB_INTLPM_INCOMPLETE or
5047//! \b USB_INTLPM_NYET depending on if data is pending in receive or transmit
5048//! FIFOs.  A call to USBDevLPMEnable() is required after every
5049//! LPM resume event to re-enable LPM mode.
5050//!
5051//! \b Example: Enable LPM suspend mode.
5052//!
5053//! \verbatim
5054//!     //
5055//!     // Enable LPM suspend mode.
5056//!     //
5057//!     USBDevLPMEnable(USB0_BASE);
5058//! \endverbatim
5059//!
5060//! \note This function must only be called in device mode. The USB LPM feature
5061//! is not available on all Tiva devices. Please consult the data sheet for
5062//! the Tiva device that you are using to determine if this feature is
5063//! available.
5064//!
5065//! \return None.
5066//
5067//*****************************************************************************
5068void USBDevLPMEnable(uint32_t ui32Base) {
5069    ASSERT(ui32Base == USB0_BASE);
5070
5071    //
5072    // Enable L1 mode on the next LPM transaction.
5073    //
5074    HWREGB(ui32Base + USB_O_LPMCNTRL) |=
5075        USB_LPMCNTRL_EN_LPMEXT | USB_LPMCNTRL_TXLPM;
5076}
5077
5078//*****************************************************************************
5079//
5080//! Disables the USB controller from responding to LPM suspend requests.
5081//!
5082//! \param ui32Base specifies the USB module base address.
5083//!
5084//! This function disables the USB controller from responding to LPM
5085//! transactions.  When the device enters LPM L1 mode, the USB controller
5086//! automatically disables responding to further LPM transactions.
5087//!
5088//! \note If LPM transactions were enabled before calling this function, then
5089//! an LPM request can still occur before this function returns.  As a result,
5090//! the application must continue to handle LPM requests until this function
5091//! returns.
5092//!
5093//! \b Example: Disable LPM suspend mode.
5094//!
5095//! \verbatim
5096//!     //
5097//!     // Disable LPM suspend mode.
5098//!     //
5099//!     USBDevLPMDisable(USB0_BASE);
5100//! \endverbatim
5101//!
5102//! \note This function must only be called in device mode. The USB LPM feature
5103//! is not available on all Tiva devices. Please consult the data sheet for
5104//! the Tiva device that you are using to determine if this feature is
5105//! available.
5106//!
5107//! \return None.
5108//
5109//*****************************************************************************
5110void USBDevLPMDisable(uint32_t ui32Base) {
5111    ASSERT(ui32Base == USB0_BASE);
5112
5113    //
5114    // Disable auto entering L1 mode on LPM transactions.
5115    //
5116    HWREGB(ui32Base + USB_O_LPMCNTRL) &= ~USB_LPMCNTRL_TXLPM;
5117}
5118
5119//*****************************************************************************
5120//
5121//! Returns the current link state setting.
5122//!
5123//! \param ui32Base specifies the USB module base address.
5124//!
5125//! This function returns the current link state setting for the USB
5126//! controller.  When the controller is operating as a host, this link
5127//! state is sent with an LPM request.  When the controller is acting
5128//! as a device, this link state was received by the last LPM transaction
5129//! whether it was acknowledged or stalled because the requested
5130//! LPM mode is not supported.
5131//!
5132//! \b Example: Get the link state for the last LPM transaction.
5133//!
5134//! \verbatim
5135//! uint32_t ui32LinkState;
5136//!
5137//! //
5138//! // Get the endpoint number that received the LPM request.
5139//! //
5140//! ui32LinkState = USBLPMLinkStateGet(USB0_BASE);
5141//!
5142//! //
5143//! // Check if this was a supported link state.
5144//! //
5145//! if(ui32LinkState == USB_HOST_LPM_L1)
5146//! {
5147//!     //
5148//!     // Handle the supported L1 link state.
5149//!     //
5150//! }
5151//! else
5152//! {
5153//!     //
5154//!     // Handle the unsupported link state.
5155//!     //
5156//! }
5157//! \endverbatim
5158//!
5159//! \note The USB LPM feature is not available on all Tiva devices.
5160//! Please consult the data sheet for the Tiva device that you
5161//! are using to determine if this feature is available.
5162//!
5163//! \return The current LPM link state.
5164//
5165//*****************************************************************************
5166uint32_t USBLPMLinkStateGet(uint32_t ui32Base) {
5167    ASSERT(ui32Base == USB0_BASE);
5168
5169    return (HWREGH(ui32Base + USB_O_LPMATTR) & USB_LPMATTR_LS_M);
5170}
5171
5172//*****************************************************************************
5173//
5174//! Returns the current LPM endpoint value.
5175//!
5176//! \param ui32Base specifies the USB module base address.
5177//!
5178//! This function returns the current LPM endpoint value.  The meaning of the
5179//! value depends on the mode of operation of the USB controller.  When in
5180//! device mode, the value returned is the endpoint that received the last
5181//! LPM transaction.  When in host mode this is the endpoint that was last
5182//! sent an LPM transaction, or the endpoint that is configured to be sent when
5183//! the LPM transaction is triggered.  The value returned is in the
5184//! \b USB_EP_[0-7] value and a direct endpoint index.
5185//!
5186//! \b Example: Get the endpoint for the last LPM transaction.
5187//!
5188//! \verbatim
5189//! uint32_t ui32Endpoint;
5190//!
5191//! //
5192//! // Get the endpoint number that received the LPM request.
5193//! //
5194//! ui32LinkState = USBLPMEndpointGet(USB0_BASE);
5195//!
5196//! \endverbatim
5197//!
5198//! \note The USB LPM feature is not available on all Tiva devices. Please
5199//! consult the data sheet for the Tiva device that you are using to determine
5200//! if this feature is available.
5201//!
5202//! \return The last endpoint to receive an LPM request in device mode or the
5203//! endpoint that the host sends an LPM request as one of the \b USB_EP_[0-7]
5204//! values.
5205//
5206//*****************************************************************************
5207uint32_t USBLPMEndpointGet(uint32_t ui32Base) {
5208    uint32_t ui32Endpoint;
5209
5210    ASSERT(ui32Base == USB0_BASE);
5211
5212    ui32Endpoint = (HWREGH(ui32Base + USB_O_LPMATTR) & USB_LPMATTR_ENDPT_M) >>
5213                   USB_LPMATTR_ENDPT_S;
5214
5215    return (IndexToUSBEP(ui32Endpoint));
5216}
5217
5218//*****************************************************************************
5219//
5220//! Returns if remote wake is currently enabled.
5221//!
5222//! \param ui32Base specifies the USB module base address.
5223//!
5224//! This function returns the current state of the remote wake setting for host
5225//! or device mode operation.  If the controller is acting as a host this
5226//! returns the current setting that is sent to devices when LPM requests are
5227//! sent to a device.  If the controller is in device mode, this function
5228//! returns the state of the last LPM request sent from the host and indicates
5229//! if the host enabled remote wakeup.
5230//!
5231//! \b Example: Issue remote wake if remote wake is enabled.
5232//!
5233//! \verbatim
5234//!
5235//! if(USBLPMRemoteWakeEnabled(USB0_BASE))
5236//! {
5237//!     USBDevLPMRemoteWake(USB0_BASE);
5238//! }
5239//!
5240//! \endverbatim
5241//!
5242//! \note The USB LPM feature is not available on all Tiva devices.
5243//! Please consult the data sheet for the Tiva device that you
5244//! are using to determine if this feature is available.
5245//!
5246//! \return The \b true if remote wake is enabled or \b false if it is not.
5247//
5248//*****************************************************************************
5249bool USBLPMRemoteWakeEnabled(uint32_t ui32Base) {
5250    ASSERT(ui32Base == USB0_BASE);
5251
5252    if (HWREGH(ui32Base + USB_O_LPMATTR) & USB_LPMATTR_RMTWAK) {
5253        return (true);
5254    }
5255    return (false);
5256}
5257
5258//*****************************************************************************
5259//
5260//! Returns the current LPM interrupt status.
5261//!
5262//! \param ui32Base specifies the USB module base address.
5263//!
5264//! This function returns the current LPM interrupt status for the USB
5265//! controller.
5266//!
5267//! The valid interrupt status bits when the USB controller is acting as a host
5268//! are the following:
5269//!
5270//! - \b USB_INTLPM_ERROR a bus error occurred in the transmission of an LPM
5271//!   transaction.
5272//! - \b USB_INTLPM_RESUME the USB controller has resumed from the LPM low
5273//!   power state.
5274//! - \b USB_INTLPM_INCOMPLETE the LPM transaction failed because a timeout
5275//!   occurred or there were bit errors in the response for three attempts.
5276//! - \b USB_INTLPM_ACK the device has acknowledged an LPM transaction.
5277//! - \b USB_INTLPM_NYET the device has responded with a NYET to an LPM
5278//!   transaction.
5279//! - \b USB_INTLPM_STALL the device has stalled an LPM transaction.
5280//!
5281//! The valid interrupt status bits when the USB controller is acting as a
5282//! device are the following:
5283//!
5284//! - \b USB_INTLPM_ERROR an LPM transaction was received that has an
5285//!   unsupported link state field.  The transaction was stalled, but the
5286//!   requested link state can still be read using the USBLPMLinkStateGet()
5287//!   function.
5288//! - \b USB_INTLPM_RESUME the USB controller has resumed from the LPM low
5289//!   power state.
5290//! - \b USB_INTLPM_INCOMPLETE the USB controller responded to an LPM
5291//!   transaction with a NYET because data was still in the transmit FIFOs.
5292//! - \b USB_INTLPM_ACK the USB controller acknowledged an LPM transaction and
5293//!   is now in the LPM suspend mode.
5294//! - \b USB_INTLPM_NYET the USB controller responded to an LPM transaction
5295//!   with a NYET because LPM transactions are not yet enabled by a call to
5296//!   USBDevLPMEnable().
5297//! - \b USB_INTLPM_STALL the USB controller has stalled an incoming LPM
5298//!   transaction.
5299//!
5300//! \note This call clears the source of all LPM status interrupts, so the
5301//! caller must take care to save the value returned because a subsequent call
5302//! to USBLPMIntStatus() does not return the previous value.
5303//!
5304//! \b Example: Get the current LPM interrupt status.
5305//!
5306//! \verbatim
5307//! uint32_t ui32LPMIntStatus;
5308//!
5309//! //
5310//! // Get the current LPM interrupt status.
5311//! //
5312//! ui32LPMIntStatus = USBLPMIntStatus(USB0_BASE);
5313//!
5314//! //
5315//! // Check if an LPM transaction was acknowledged.
5316//! //
5317//! if(ui32LPMIntStatus & USB_INTLPM_ACK)
5318//! {
5319//!     //
5320//!     // Handle entering LPM suspend mode.
5321//!     //
5322//!     ...
5323//! }
5324//! \endverbatim
5325//!
5326//! \note The USB LPM feature is not available on all Tiva devices.
5327//! Please consult the data sheet for the Tiva device that you
5328//! are using to determine if this feature is available.
5329//!
5330//! \return The current LPM interrupt status.
5331//
5332//*****************************************************************************
5333uint32_t USBLPMIntStatus(uint32_t ui32Base) {
5334    ASSERT(ui32Base == USB0_BASE);
5335
5336    //
5337    // Return the current raw interrupt status.
5338    //
5339    return (HWREGB(ui32Base + USB_O_LPMRIS));
5340}
5341
5342//*****************************************************************************
5343//
5344//! Enables LPM interrupts.
5345//!
5346//! \param ui32Base specifies the USB module base address.
5347//! \param ui32Ints specifies which LPM interrupts to enable.
5348//!
5349//! This function enables a set of LPM interrupts so that they can trigger a
5350//! USB interrupt.  The \e ui32Ints parameter specifies which of the
5351//! \b USB_INTLPM_* to enable.
5352//!
5353//! The valid interrupt status bits when the USB controller is acting as a host
5354//! are the following:
5355//!
5356//! - \b USB_INTLPM_ERROR a bus error occurred in the transmission of an LPM
5357//!   transaction.
5358//! - \b USB_INTLPM_RESUME the USB controller has resumed from LPM low power
5359//!   state.
5360//! - \b USB_INTLPM_INCOMPLETE the LPM transaction failed because a timeout
5361//!   occurred or there were bit errors in the response for three attempts.
5362//! - \b USB_INTLPM_ACK the device has acknowledged an LPM transaction.
5363//! - \b USB_INTLPM_NYET the device has responded with a NYET to an LPM
5364//!   transaction.
5365//! - \b USB_INTLPM_STALL the device has stalled an LPM transaction.
5366//!
5367//! The valid interrupt status bits when the USB controller is acting as a
5368//! device are the following:
5369//!
5370//! - \b USB_INTLPM_ERROR an LPM transaction was received that has an
5371//!   unsupported link state field.  The transaction was stalled, but the
5372//!   requested link state can still be read using the USBLPMLinkStateGet()
5373//!   function.
5374//! - \b USB_INTLPM_RESUME the USB controller has resumed from the LPM low
5375//!   power state.
5376//! - \b USB_INTLPM_INCOMPLETE the USB controller responded to an LPM
5377//!   transaction with a NYET because data was still in the transmit FIFOs.
5378//! - \b USB_INTLPM_ACK the USB controller acknowledged an LPM transaction and
5379//!   is now in the LPM suspend mode.
5380//! - \b USB_INTLPM_NYET the USB controller responded to an LPM transaction
5381//!   with a NYET because LPM transactions are not yet enabled by a call to
5382//!   USBDevLPMEnable().
5383//! - \b USB_INTLPM_STALL the USB controller has stalled an incoming LPM
5384//!   transaction.
5385//!
5386//! \b Example: Enable all LPM interrupt sources.
5387//!
5388//! \verbatim
5389//! //
5390//! // Enable all LPM interrupt sources.
5391//! //
5392//! USBLPMIntEnable(USB0_BASE, USB_INTLPM_ERROR | USB_INTLPM_RESUME |
5393//!                            USB_INTLPM_INCOMPLETE | USB_INTLPM_ACK |
5394//!                            USB_INTLPM_NYET | USB_INTLPM_STALL);
5395//! \endverbatim
5396//!
5397//! \note The USB LPM feature is not available on all Tiva devices.
5398//! Please consult the data sheet for the Tiva device that you
5399//! are using to determine if this feature is available.
5400//!
5401//! \return None.
5402//
5403//*****************************************************************************
5404void USBLPMIntEnable(uint32_t ui32Base, uint32_t ui32Ints) {
5405    ASSERT(ui32Base == USB0_BASE);
5406
5407    //
5408    // Enable the requested interrupts.
5409    //
5410    HWREGB(ui32Base + USB_O_LPMIM) |= ui32Ints;
5411}
5412
5413//*****************************************************************************
5414//
5415//! Disables LPM interrupts.
5416//!
5417//! \param ui32Base specifies the USB module base address.
5418//! \param ui32Ints specifies which LPM interrupts to disable.
5419//!
5420//! This function disables the LPM interrupts specified in the \e ui32Ints
5421//! parameter, preventing them from triggering a USB interrupt.
5422//!
5423//! The valid interrupt status bits when the USB controller is acting as a host
5424//! are the following:
5425//!
5426//! - \b USB_INTLPM_ERROR a bus error occurred in the transmission of an LPM
5427//!   transaction.
5428//! - \b USB_INTLPM_RESUME the USB controller has resumed from LPM low power
5429//!   state.
5430//! - \b USB_INTLPM_INCOMPLETE the LPM transaction failed because a timeout
5431//!   occurred or there were bit errors in the response for three attempts.
5432//! - \b USB_INTLPM_ACK the device has acknowledged an LPM transaction.
5433//! - \b USB_INTLPM_NYET the device has responded with a NYET to an LPM
5434//!   transaction.
5435//! - \b USB_INTLPM_STALL the device has stalled an LPM transaction.
5436//!
5437//! The valid interrupt status bits when the USB controller is acting as a
5438//! device are the following:
5439//!
5440//! - \b USB_INTLPM_ERROR an LPM transaction was received that has an
5441//!   unsupported link state field.  The transaction was stalled, but the
5442//!   requested link state can still be read using the USBLPMLinkStateGet()
5443//!   function.
5444//! - \b USB_INTLPM_RESUME the USB controller has resumed from the LPM low
5445//!   power state.
5446//! - \b USB_INTLPM_INCOMPLETE the USB controller responded to an LPM
5447//!   transaction with a NYET because data was still in the transmit FIFOs.
5448//! - \b USB_INTLPM_ACK the USB controller acknowledged an LPM transaction and
5449//!   is now in the LPM suspend mode.
5450//! - \b USB_INTLPM_NYET the USB controller responded to an LPM transaction
5451//!   with a NYET because LPM transactions are not yet enabled by a call to
5452//!   USBDevLPMEnable().
5453//! - \b USB_INTLPM_STALL the USB controller has stalled an incoming LPM
5454//!   transaction.
5455//!
5456//! \b Example: Disable all LPM interrupt sources.
5457//!
5458//! \verbatim
5459//! //
5460//! // Disable all LPM interrupt sources.
5461//! //
5462//! USBLPMIntDisable(USB0_BASE, USB_INTLPM_ERROR | USB_INTLPM_RESUME |
5463//!                             USB_INTLPM_INCOMPLETE | USB_INTLPM_ACK |
5464//!                             USB_INTLPM_NYET | USB_INTLPM_STALL);
5465//! \endverbatim
5466//!
5467//! \note The USB LPM feature is not available on all Tiva devices.
5468//! Please consult the data sheet for the Tiva device that you
5469//! are using to determine if this feature is available.
5470//!
5471//! \return None.
5472//
5473//*****************************************************************************
5474void USBLPMIntDisable(uint32_t ui32Base, uint32_t ui32Ints) {
5475    ASSERT(ui32Base == USB0_BASE);
5476
5477    //
5478    // Disable the requested interrupts.
5479    //
5480    HWREGB(ui32Base + USB_O_LPMIM) &= ~ui32Ints;
5481}
5482
5483//*****************************************************************************
5484//
5485// Close the Doxygen group.
5486//! @}
5487//
5488//*****************************************************************************