+/rtos

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

Joseph RyanRe-add usblib files83915e3

master
34.6 KiB1100 linesraw
1//*****************************************************************************
2//
3// usbmode.c - Functions related to dual mode USB device/host operation.
4//
5// Copyright (c) 2008-2017 Texas Instruments Incorporated.  All rights reserved.
6// Software License Agreement
7//
8// Texas Instruments (TI) is supplying this software for use solely and
9// exclusively on TI's microcontroller products. The software is owned by
10// TI and/or its suppliers, and is protected under applicable copyright
11// laws. You may not combine this software with "viral" open-source
12// software in order to form a larger program.
13//
14// THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
15// NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
16// NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
17// A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. TI SHALL NOT, UNDER ANY
18// CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
19// DAMAGES, FOR ANY REASON WHATSOEVER.
20//
21// This is part of revision 2.1.4.178 of the Tiva USB Library.
22//
23//*****************************************************************************
24
25#include "tivaware/hw_ints.h"
26#include "tivaware/hw_memmap.h"
27#include "tivaware/hw_sysctl.h"
28#include "tivaware/hw_types.h"
29#include "tivaware/hw_usb.h"
30#include "tivaware/rom.h"
31#include "tivaware/rom_map.h"
32#include "tivaware/rtos_bindings.h"
33#include "tivaware/sysctl.h"
34#include "tivaware/usb.h"
35#include "usblib/device/usbdevice.h"
36#include "usblib/host/usbhost.h"
37#include "usblib/host/usbhostpriv.h"
38#include "usblib/usblib.h"
39#include "usblib/usblibpriv.h"
40#include <stdbool.h>
41#include <stdint.h>
42
43#define ASSERT(X)
44
45//*****************************************************************************
46//
47//! \addtogroup general_usblib_api
48//! @{
49//
50//*****************************************************************************
51
52//*****************************************************************************
53//
54// The following label defines interrupts that we will always pass to the host
55// interrupt handler even if we are in dual mode and not yet sure of which
56// mode we are operating in.
57//
58//*****************************************************************************
59#define USB_HOST_INTS (USB_INTCTRL_VBUS_ERR)
60
61//*****************************************************************************
62//
63// Global variable indicating which mode of operation the application has
64// requested.
65//
66//*****************************************************************************
67volatile tUSBMode g_iUSBMode = eUSBModeNone;
68
69//*****************************************************************************
70//
71// The default and the current polling rate for the USB OTG library.
72//
73//*****************************************************************************
74volatile uint32_t g_ui32PollRate;
75
76//*****************************************************************************
77//
78// The current time remaining in milliseconds before checking the cable
79// connection.
80//
81//*****************************************************************************
82volatile uint32_t g_ui32WaitTicks = 0;
83
84//*****************************************************************************
85//
86// This enum holds the various states that we can be in while performing
87// USB mode checking. This involves use of the OTG session request to poll
88// the USB ID pin to determine whether a device or a host is connected.
89//
90//*****************************************************************************
91typedef enum {
92    //
93    // No checking is currently pending.
94    //
95    eUSBOTGModeIdle,
96
97    //
98    // Waiting on ID mode detection.
99    //
100    eUSBOTGModeWaitID,
101
102    //
103    // Waiting for next poll interval.
104    //
105    eUSBOTGModeWait,
106
107    //
108    // Now in B-side wait for connect.
109    //
110    eUSBOTGModeBWaitCon,
111
112    //
113    // Now in A-side device mode.
114    //
115    eUSBOTGModeBDevice,
116
117    //
118    // Now in A-side host mode.
119    //
120    eUSBOTGModeAHost,
121} tUSBOTGState;
122
123volatile tUSBOTGState g_eOTGModeState;
124
125//*****************************************************************************
126//
127// Global variable indicating whether we are currently operating in host or
128// device mode if the user has requested Dual mode operation.
129//
130//*****************************************************************************
131static volatile tUSBMode g_iDualMode = eUSBModeNone;
132
133static void USBOTGRemovePower(uint32_t ui32Index);
134
135//*****************************************************************************
136//
137// Global variable holding a pointer to the callback function which will be
138// called when the USB mode changes between device and host.
139//
140//*****************************************************************************
141static tUSBModeCallback g_pfnUSBModeCallback;
142
143//*****************************************************************************
144//
145// This function is used to handle switching between host, device and
146// unconfigured modes.
147//
148// \param iUSBMode is one of eUSBModeHost, eUSBModeDevice, or eUSBModeNone.
149//
150// Based on the current state held in g_iDualMode variable this function will
151// handle the transition of the mode of operation in OTG mode and calling
152// the callback function if it is present.
153//
154// \return None.
155//
156//*****************************************************************************
157static void USBOTGSetMode(tUSBMode iUSBMode) {
158    if ((g_iDualMode != iUSBMode) || (g_iDualMode == eUSBModeNone)) {
159        //
160        // If going from host mode to unconfigured mode then remove power.
161        //
162        if ((g_iDualMode == eUSBModeHost) && (iUSBMode == eUSBModeNone)) {
163            //
164            // Take the steps to remove power in the of host mode OTG.
165            //
166            USBOTGRemovePower(0);
167        }
168
169        //
170        // If going from device mode to unconfigured mode then end the current
171        // session.
172        //
173        if ((g_iDualMode == eUSBModeDevice) && (iUSBMode == eUSBModeNone)) {
174            //
175            // End the current session.
176            //
177            USBOTGSessionRequest(USB0_BASE, false);
178        }
179
180        //
181        // Reset the delay whenever returning to eUSBModeNone.
182        //
183        if (iUSBMode == eUSBModeNone) {
184            g_ui32WaitTicks = g_ui32PollRate;
185        }
186
187        //
188        // Do we have a mode change callback installed?
189        //
190        if ((g_pfnUSBModeCallback) && (g_iDualMode != iUSBMode)) {
191            //
192            // Inform the callback of the new operating mode.
193            //
194            g_pfnUSBModeCallback(0, iUSBMode);
195        }
196
197        //
198        // Save the new mode.
199        //
200        g_iDualMode = iUSBMode;
201    }
202}
203
204//*****************************************************************************
205//
206//! Allows dual mode application to switch between USB device and host modes
207//! and provides a method to force the controller into the desired mode.
208//!
209//! \param ui32Index specifies the USB controller whose mode of operation is to
210//! be set.  This parameter must be set to 0.
211//! \param iUSBMode indicates the mode that the application wishes to operate
212//! in.  Valid values are \b eUSBModeDevice to operate as a USB device and
213//! \b eUSBModeHost to operate as a USB host.
214//! \param pfnCallback is a pointer to a function which the USB library will
215//! call each time the mode is changed to indicate the new operating mode.  In
216//! cases where \e iUSBMode is set to either \b eUSBModeDevice or
217//! \b eUSBModeHost, the callback will be made immediately to allow the
218//! application to perform any host or device specific initialization.
219//!
220//! This function allows a USB application that can operate in host
221//! or device mode to indicate to the USB stack the mode that it wishes to
222//! use.  The caller is responsible for cleaning up the interface and removing
223//! itself from the bus prior to making this call and reconfiguring afterwards.
224//! The \e pfnCallback function can be a NULL(0) value to indicate that no
225//! notification is required.
226//!
227//! For successful dual mode mode operation, an application must register
228//! USB0DualModeIntHandler() as the interrupt handler for the USB0 interrupt.
229//! This handler is responsible for steering interrupts to the device or host
230//! stack depending upon the chosen mode.  Devices which do not require dual
231//! mode capability should register either \e USB0DeviceIntHandler() or
232//! \e USB0HostIntHandler() instead.  Registering \e USB0DualModeIntHandler()
233//! for a single mode application will result in an application binary larger
234//! than required since library functions for both USB operating modes will be
235//! included even though only one mode is required.
236//!
237//! Single mode applications (those offering exclusively USB device or USB
238//! host functionality) are only required to call this function if they need to
239//! force the mode of the controller to Host or Device mode.  This is usually
240//! in the event that the application needs to reused the USBVBUS and/or USBID
241//! pins as GPIOs.
242//!
243//! \return None.
244//
245//*****************************************************************************
246void USBStackModeSet(uint32_t ui32Index, tUSBMode iUSBMode,
247                     tUSBModeCallback pfnCallback) {
248    //
249    // Check the arguments.
250    //
251    ASSERT(ui32Index == 0);
252
253    //
254    // Remember the mode so that we can steer the interrupts appropriately.
255    //
256    g_iUSBMode = iUSBMode;
257
258    //
259    // Remember the callback pointer.
260    //
261    g_pfnUSBModeCallback = pfnCallback;
262
263    //
264    // If we are being asked to be either a host or device, we will not be
265    // trying to auto-detect the mode so make the callback immediately.
266    //
267    if ((iUSBMode == eUSBModeDevice) || (iUSBMode == eUSBModeHost)) {
268        //
269        // Make sure that a callback was provided.
270        //
271        if (g_pfnUSBModeCallback) {
272            g_pfnUSBModeCallback(0, iUSBMode);
273        }
274    }
275}
276
277//*****************************************************************************
278//
279//! Steers USB interrupts from controller to the correct handler in the USB
280//! stack.
281//!
282//! This interrupt handler is used in applications which require to operate
283//! in both host and device mode.  It steers the USB hardware interrupt to the
284//! correct handler in the USB stack depending upon the current operating mode
285//! of the application, USB device or host.
286//!
287//! For successful dual mode operation, an application must register
288//! USB0DualModeIntHandler() in the CPU vector table as the interrupt handler
289//! for the USB0 interrupt.  This handler is responsible for steering
290//! interrupts to the device or host stack depending upon the chosen mode.
291//!
292//! \note Devices which do not require dual mode capability should register
293//! either USB0DeviceIntHandler() or USB0HostIntHandler() instead.  Registering
294//! USB0DualModeIntHandler() for a single mode application will result in an
295//! application binary larger than required since library functions for both
296//! USB operating modes will be included even though only one mode is actually
297//! required.
298//!
299//! \return None.
300//
301//*****************************************************************************
302void USB0DualModeIntHandler(void) {
303    uint32_t ui32Status;
304
305    //
306    // Read the USB interrupt status.
307    //
308    ui32Status = USBIntStatusControl(USB0_BASE);
309
310    //
311    // Pass through the subset of interrupts that we always want
312    // the host stack to see regardless of whether or not we
313    // are actually in host mode at this point.
314    //
315    if (ui32Status & USB_HOST_INTS) {
316        //
317        // Call the host's interrupt handler.
318        //
319        USBHostIntHandlerInternal(0, ui32Status & USB_HOST_INTS);
320
321        //
322        // We have already processed these interrupts so clear them
323        // from the status.
324        //
325        ui32Status &= ~USB_HOST_INTS;
326    }
327
328    //
329    // Steer the interrupt to the appropriate handler within the stack
330    // depending upon our current operating mode.  Note that we need to pass
331    // the ui32Status parameter since the USB interrupt register is
332    // clear-on-read.
333    //
334    switch (g_iUSBMode) {
335    case eUSBModeNone: {
336        //
337        // No mode is set yet so we have no idea what to do.  Just ignore
338        // the interrupt.
339        //
340        break;
341    }
342
343    //
344    // Operating in pure host mode.
345    //
346    case eUSBModeHost: {
347        //
348        // Call the host interrupt handler if there is anything still to
349        // process.
350        //
351        USBHostIntHandlerInternal(0, ui32Status);
352
353        break;
354    }
355
356    //
357    // Operating in pure device mode.
358    //
359    case eUSBModeDevice: {
360        //
361        // Call the device interrupt handler.
362        //
363        USBDeviceIntHandlerInternal(0, ui32Status);
364
365        break;
366    }
367
368    default: {
369        break;
370    }
371    }
372}
373
374//*****************************************************************************
375//
376// Close the Doxygen group general_usblib_api.
377//! @}
378//
379//*****************************************************************************
380
381//*****************************************************************************
382//
383//! \addtogroup dualmode_api
384//! @{
385//
386//*****************************************************************************
387
388//*****************************************************************************
389//
390//! Initializes the USB controller for dual mode operation.
391//!
392//! \param ui32Index specifies the USB controller that is to be initialized for
393//! dual mode operation.  This parameter must be set to 0.
394//!
395//! This function initializes the USB controller hardware into a state
396//! suitable for dual mode operation.  Applications may use this function to
397//! ensure that the controller is in a neutral state and able to receive
398//! appropriate interrupts before host or device mode is chosen using a call
399//! to USBStackModeSet().
400//!
401//! \return None.
402//
403//*****************************************************************************
404void USBDualModeInit(uint32_t ui32Index) {
405    //
406    // We only support a single USB controller.
407    //
408    ASSERT(ui32Index == 0);
409
410    //
411    // Configure the End point 0.
412    //
413    USBHostEndpointConfig(
414        USB0_BASE, USB_EP_0, 64, 0, 0,
415        (USB_EP_MODE_CTRL | USB_EP_SPEED_FULL | USB_EP_HOST_OUT));
416
417    //
418    // Enable USB Interrupts.
419    //
420    MAP_USBIntEnableControl(USB0_BASE,
421                            USB_INTCTRL_RESET | USB_INTCTRL_DISCONNECT |
422                                USB_INTCTRL_SESSION | USB_INTCTRL_BABBLE |
423                                USB_INTCTRL_CONNECT | USB_INTCTRL_RESUME |
424                                USB_INTCTRL_SUSPEND | USB_INTCTRL_VBUS_ERR);
425
426    //
427    // Enable all endpoint interrupts.
428    //
429    MAP_USBIntEnableEndpoint(USB0_BASE, USB_INTEP_ALL);
430
431    //
432    // Initialize the USB tick module.
433    //
434    InternalUSBTickInit();
435
436    //
437    // Enable the USB interrupt.
438    //
439    OS_INT_ENABLE(g_psDCDInst[0].ui32IntNum);
440    //
441    // Turn on session request to enable ID pin checking.
442    //
443    USBOTGSessionRequest(USB0_BASE, true);
444
445    //
446    // Initialize the power configuration.
447    //
448    USBHostPwrConfig(USB0_BASE, USBHCDPowerConfigGet(ui32Index));
449
450    //
451    // If power enable is automatic then then USBHostPwrEnable() has to be
452    // called to allow the USB controller to control the power enable pin.
453    //
454    if (USBHCDPowerAutomatic(ui32Index)) {
455        //
456        // This will not turn on power but instead will allow the USB
457        // controller to turn on power when needed.
458        //
459        USBHostPwrEnable(USB0_BASE);
460    }
461}
462
463//*****************************************************************************
464//
465//! Returns the USB controller to the default mode when in dual mode operation.
466//!
467//! \param ui32Index specifies the USB controller whose dual mode operation is
468//! to be ended.  This parameter must be set to 0.
469//!
470//! Applications using both host and device modes may call this function to
471//! disable interrupts in preparation for shutdown or a change of operating
472//! mode.
473//!
474//! \return None.
475//
476//*****************************************************************************
477void USBDualModeTerm(uint32_t ui32Index) {
478    //
479    // We only support a single USB controller.
480    //
481    ASSERT(ui32Index == 0);
482
483    //
484    // Disable the USB interrupt.
485    //
486    OS_INT_DISABLE(g_psDCDInst[0].ui32IntNum);
487
488    MAP_USBIntDisableControl(USB0_BASE, USB_INTCTRL_ALL);
489
490    MAP_USBIntDisableEndpoint(USB0_BASE, USB_INTEP_ALL);
491}
492
493//*****************************************************************************
494//
495// Close the Doxygen group dualmode_api.
496//! @}
497//
498//*****************************************************************************
499
500//*****************************************************************************
501//
502//! \addtogroup usblib_otg
503//! @{
504//
505//*****************************************************************************
506
507//*****************************************************************************
508//
509//! Returns the USB controller to and inactive state when in OTG mode
510//! operation.
511//!
512//! \param ui32Index specifies the USB controller to end OTG mode operations.
513//!
514//! Applications using OTG mode may call this function to disable interrupts
515//! in preparation for shutdown or a change of operating mode.
516//!
517//! \return None.
518//
519//*****************************************************************************
520void USBOTGModeTerm(uint32_t ui32Index) {
521    //
522    // We only support a single USB controller.
523    //
524    ASSERT(ui32Index == 0);
525
526    //
527    // Disable the USB interrupt.
528    //
529    OS_INT_DISABLE(g_psDCDInst[0].ui32IntNum);
530
531    //
532    // Disable all control interrupts.
533    //
534    MAP_USBIntDisableControl(USB0_BASE, USB_INTCTRL_ALL);
535
536    //
537    // Disable all endpoint interrupts.
538    //
539    MAP_USBIntDisableEndpoint(USB0_BASE, USB_INTEP_ALL);
540
541    //
542    // Set the mode to none if it is not already.
543    //
544    USBOTGSetMode(eUSBModeNone);
545}
546
547//*****************************************************************************
548//
549//! Initializes the USB controller for OTG mode operation.
550//!
551//! \param ui32Index specifies the USB controller that is to be initialized for
552//! OTG mode operation.
553//! \param ui32PollingRate is the rate in milliseconds to poll the controller
554//! for changes in mode.
555//! \param pvPool is a pointer to the data to use as a memory pool for this
556//! controller.
557//! \param ui32PoolSize is the size in bytes of the buffer passed in as
558//! \e pvPool.
559//!
560//! This function initializes the USB controller hardware into a state
561//! suitable for OTG mode operation.  Applications must use this function to
562//! ensure that the controller is in a neutral state and able to receive
563//! appropriate interrupts before host or device mode is chosen by OTG
564//! negotiation.  The \e ui32PollingRate parameter is used to set the rate at
565//! which the USB library will poll the controller to determine the mode.  This
566//! has the most effect on how quickly the USB library will detect changes when
567//! going to host mode.  The parameters \e pvPool and \e ui32PoolSize are
568//! passed on to the USB host library functions to provide memory for the USB
569//! library when it is acting as a  host. Any device and host initialization
570//! should have been called before calling this function to prevent the USB
571//! library from attempting to run in device or host mode before the USB
572//! library is fully configured.
573//!
574//! \return None.
575//
576//*****************************************************************************
577void USBOTGModeInit(uint32_t ui32Index, uint32_t ui32PollingRate, void* pvPool,
578                    uint32_t ui32PoolSize) {
579    //
580    // We only support a single USB controller.
581    //
582    ASSERT(ui32Index == 0);
583
584    //
585    // This should never be called if not in OTG mode.
586    //
587    ASSERT(g_iUSBMode == eUSBModeOTG);
588
589    //
590    // Force OTG mode in all cases since anything else is invalid, but a DEBUG
591    // build will still ASSERT above if this value is incorrect.
592    //
593    g_iUSBMode = eUSBModeOTG;
594
595    //
596    // Remember that we have not yet determined whether we are device or
597    // host.
598    //
599    g_iDualMode = eUSBModeNone;
600
601    //
602    // Set the default polling rate.
603    //
604    g_ui32PollRate = ui32PollingRate;
605
606    //
607    // Enable the USB controller.
608    //
609    MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_USB0);
610
611    //
612    // Turn on USB Phy clock.
613    //
614    MAP_SysCtlUSBPLLEnable();
615
616    //
617    // Initialize the host controller stack.
618    //
619    USBHCDInit(ui32Index, pvPool, ui32PoolSize);
620
621    //
622    // Configure the End point 0.
623    //
624    USBHostEndpointConfig(
625        USB0_BASE, USB_EP_0, 64, 0, 0,
626        (USB_EP_MODE_CTRL | USB_EP_SPEED_FULL | USB_EP_HOST_OUT));
627
628    //
629    // Enable control interrupts.
630    //
631    MAP_USBIntEnableControl(USB0_BASE,
632                            USB_INTCTRL_RESET | USB_INTCTRL_DISCONNECT |
633                                USB_INTCTRL_SESSION | USB_INTCTRL_BABBLE |
634                                USB_INTCTRL_CONNECT | USB_INTCTRL_RESUME |
635                                USB_INTCTRL_SUSPEND | USB_INTCTRL_VBUS_ERR |
636                                USB_INTCTRL_MODE_DETECT | USB_INTCTRL_SOF);
637
638    //
639    // Make sure the mode OTG mode and not forced device or host.
640    //
641    USBOTGMode(USB0_BASE);
642
643    //
644    // Enable all endpoint interrupts.
645    //
646    MAP_USBIntEnableEndpoint(USB0_BASE, USB_INTEP_ALL);
647
648    //
649    // Initialize the power configuration.
650    //
651    USBHCDPowerConfigSet(ui32Index, USBHCDPowerConfigGet(ui32Index));
652
653    //
654    // If power enable is automatic then then USBHostPwrEnable() has to be
655    // called to allow the USB controller to control the power enable pin.
656    //
657    if (USBHCDPowerAutomatic(ui32Index)) {
658        //
659        // This will not turn on power but instead will allow the USB
660        // controller to turn on power when needed.
661        //
662        USBHostPwrEnable(USB0_BASE);
663    }
664
665    //
666    // Enable the USB interrupt.
667    //
668    if (CLASS_IS_TM4C129) {
669        OS_INT_ENABLE(INT_USB0_TM4C129);
670    } else {
671        OS_INT_ENABLE(INT_USB0_TM4C123);
672    }
673}
674
675//*****************************************************************************
676//
677// This function handles the steps required to remove power in OTG mode.
678//
679// \param ui32Index specifies which USB controller should remove power.
680//
681// This function will perform the steps required to remove power from the USB
682// bus as required by the OTG specification.  This call will first issue a
683// bus suspend followed by clearing the current session and then removing
684// power.
685//
686// \return None.
687//
688//*****************************************************************************
689static void USBOTGRemovePower(uint32_t ui32Index) {
690    tEventInfo sEvent;
691
692    //
693    // Do suspend signaling.
694    //
695    USBHostSuspend(USB0_BASE);
696
697    //
698    // End the session in either device or host mode.
699    //
700    USBOTGSessionRequest(USB0_BASE, false);
701
702    //
703    // Check if the controller is automatically applying power or not.
704    //
705    if (USBHCDPowerAutomatic(ui32Index) == 0) {
706        //
707        // Call the registered event driver to allow it to disable power.
708        //
709        sEvent.ui32Event = USB_EVENT_POWER_DISABLE;
710        sEvent.ui32Instance = 0;
711        InternalUSBHCDSendEvent(0, &sEvent, USBHCD_EVFLAG_PWRDIS);
712    }
713}
714
715//*****************************************************************************
716//
717//! This call sets the USB OTG controllers poll rate when checking for the mode
718//! of the controller.
719//!
720//! \param ui32Index specifies which USB controller to set the polling rate.
721//! \param ui32PollRate is the rate in milliseconds to poll for changes in the
722//! controller mode.
723//!
724//! This function is called to set the USB OTG libraries polling rate when
725//! checking the status of the cable.  The \e ui32PollRate value used sets the
726//! rate in milliseconds that the USB OTG library will poll the cable to see
727//! if the controller should enter host mode.  This value has no effect on
728//! device detection rate as the controller will detect being connected to a
729//! host controller automatically.  The \e ui32PollRate can be set to 0 to
730//! disable polling.  The USB OTG library can still function with the polling
731//! rate set to zero, however it will fail to detect host mode properly when no
732//! device is present at the end of the USB OTG B side of the cable.
733//!
734//! \note This function should only be called on devices that support OTG
735//! functionality.
736//!
737//! \return None.
738//
739//*****************************************************************************
740void USBOTGPollRate(uint32_t ui32Index, uint32_t ui32PollRate) {
741    //
742    // Save the timeout.
743    //
744    g_ui32PollRate = ui32PollRate;
745}
746
747//*****************************************************************************
748//
749//! Handles OTG mode changes and also steers other USB interrupts from
750//! the controller to the correct handler in the USB stack.
751//!
752//! This interrupt handler is used in applications which require to operate
753//! in both host and device mode using OTG.  When in host or device mode, it
754//! steers the USB hardware interrupt to the correct handler in the USB stack
755//! depending upon the current operating mode.  It also handles other OTG
756//! related interrupt events.
757//!
758//! For successful OTG mode operation, an application must register
759//! USB0OTGModeIntHandler() in the CPU vector table as the interrupt handler
760//! for the USB0 interrupt.
761//!
762//! \note This interrupt handler should only be used on controllers that
763//! support OTG functionality.
764//!
765//! \return None.
766//
767//*****************************************************************************
768void USB0OTGModeIntHandler(void) {
769    uint32_t ui32Status;
770    tEventInfo sEvent;
771
772    //
773    // Read the USB interrupt status.
774    //
775    ui32Status = USBIntStatusControl(USB0_BASE);
776
777    //
778    // Check if this was an mode detect interrupt and under manual power
779    // control.
780    //
781    if ((ui32Status & USB_INTCTRL_MODE_DETECT) &&
782        (USBHCDPowerAutomatic(0) == 0)) {
783        uint32_t ui32Mode;
784
785        ui32Mode = USBModeGet(USB0_BASE);
786
787        switch (ui32Mode) {
788        //
789        // Device is on the A side of the cable and power needs to be
790        // applied.
791        //
792        case USB_OTG_MODE_ASIDE_NPWR:
793        case USB_OTG_MODE_ASIDE_SESS:
794        case USB_OTG_MODE_ASIDE_AVAL: {
795            //
796            // Since power is not automatically enabled, call the
797            // registered event handler to allow the application to turn
798            // on power.
799            //
800            sEvent.ui32Event = USB_EVENT_POWER_ENABLE;
801            sEvent.ui32Instance = 0;
802            InternalUSBHCDSendEvent(0, &sEvent, USBHCD_EVFLAG_PWREN);
803
804            break;
805        }
806
807        //
808        // Device is on the B side of the cable and powered.
809        //
810        case USB_OTG_MODE_BSIDE_DEV: {
811            //
812            // Now in device mode on the B side of the cable and will wait
813            // for a connect before becoming a device.
814            //
815            g_eOTGModeState = eUSBOTGModeBWaitCon;
816
817            break;
818        }
819
820        //
821        // Any other mode detect indicates eUSBModeNone.
822        //
823        default: {
824            //
825            // Just inform the application that the mode was not device
826            // or host.
827            //
828            USBOTGSetMode(eUSBModeNone);
829
830            break;
831        }
832        }
833    }
834
835    //
836    // If there was a VBUS error then the power should be shut off and the
837    // system is reset to waiting for detection again.
838    //
839    if (ui32Status & USB_INTCTRL_VBUS_ERR) {
840        //
841        // Just inform the application that the mode was not device
842        // or host.
843        //
844        USBOTGSetMode(eUSBModeNone);
845
846        //
847        // Return to idle mode.
848        //
849        g_eOTGModeState = eUSBOTGModeWait;
850    }
851
852    //
853    // If there is a disconnect interrupt and the controller was on the B side
854    // cable as a device then go back to the IDLE state.
855    //
856    if ((ui32Status & USB_INTCTRL_DISCONNECT) &&
857        (g_eOTGModeState == eUSBOTGModeBDevice)) {
858        //
859        // No longer a device so switch to unconfigured mode.
860        //
861        USBOTGSetMode(eUSBModeNone);
862
863        //
864        // Return to idle mode.
865        //
866        g_eOTGModeState = eUSBOTGModeWait;
867
868        return;
869    }
870
871    //
872    // Handle receiving a reset.
873    //
874    if ((ui32Status & USB_INTCTRL_RESET) &&
875        (g_eOTGModeState != eUSBOTGModeBDevice)) {
876        //
877        // Getting a reset interrupt when not already a b side device indicates
878        // that a host is resetting the device and the controller should
879        // move to device mode.
880        //
881        g_eOTGModeState = eUSBOTGModeBDevice;
882
883        //
884        // Save the new mode.
885        //
886        USBOTGSetMode(eUSBModeDevice);
887    }
888
889    //
890    // If there is a connect interrupt while the library is waiting for
891    // one then move to full host mode state.
892    //
893    if (ui32Status & USB_INTCTRL_CONNECT) {
894        //
895        // Move to A side host state.
896        //
897        g_eOTGModeState = eUSBOTGModeAHost;
898
899        //
900        // Inform the application that controller is in host mode.
901        //
902        USBOTGSetMode(eUSBModeHost);
903    }
904
905    //
906    // Call the correct device or host interrupt handler based on the current
907    // mode of operation.
908    //
909    switch (g_eOTGModeState) {
910    case eUSBOTGModeAHost: {
911        //
912        // Call the host interrupt handler if there is anything still to
913        // process.
914        //
915        USBHostIntHandlerInternal(0, ui32Status);
916
917        break;
918    }
919
920    //
921    // Operating in pure device mode.
922    //
923    case eUSBOTGModeBDevice: {
924        //
925        // Call the device interrupt handler.
926        //
927        USBDeviceIntHandlerInternal(0, ui32Status);
928
929        break;
930    }
931    default: {
932        break;
933    }
934    }
935}
936
937//*****************************************************************************
938//
939// This function is called by the USB host stack code to indicated that it
940// has completed handing the device disconnection.
941//
942// \param ui32Index specifies the USB controller that has completed disconnect.
943//
944// This internal library function is used when the hsot controller has
945// completed any deferred handling when it has detected a device has been
946// disconnected.  The functions main purpose is to return the OTG controller to
947// a state that allows for resuming normal OTG cable detection and negotiation.
948//
949// \note This function should not be called outside the library.
950//
951//*****************************************************************************
952void OTGDeviceDisconnect(uint32_t ui32Index) {
953    //
954    // This function is only valid when called in host mode.
955    //
956    if (g_eOTGModeState == eUSBOTGModeAHost) {
957        //
958        // No longer a host so switch to unconfigured mode.
959        //
960        USBOTGSetMode(eUSBModeNone);
961
962        g_eOTGModeState = eUSBOTGModeWait;
963    }
964}
965
966//*****************************************************************************
967//
968//! This function is the main routine for the OTG Controller Driver.
969//!
970//! \param ui32MsTicks is the number of milliseconds that have passed since the
971//! last time this function was called.
972//!
973//! This function is the main routine for the USB controller when using the
974//! library in OTG mode.  This routine must be called periodically by the main
975//! application outside of a callback context.  The \e ui32MsTicks value is
976//! used for basic timing needed by the USB library when operating in OTG mode.
977//! This allows for a simple cooperative system to access the the OTG
978//! controller driver interface without the need for an RTOS.  All time
979//! critical operations are handled in interrupt context but all longer
980//! operations are run from the this function to allow them to block and wait
981//! for completion without holding off other interrupts.
982//!
983//! \return None.
984//
985//*****************************************************************************
986void USBOTGMain(uint32_t ui32MsTicks) {
987    tEventInfo sEvent;
988
989    if (ui32MsTicks > g_ui32WaitTicks) {
990        g_ui32WaitTicks = 0;
991    } else {
992        g_ui32WaitTicks -= ui32MsTicks;
993    }
994
995    switch (g_eOTGModeState) {
996    case eUSBOTGModeIdle: {
997        g_eOTGModeState = eUSBOTGModeWaitID;
998
999        //
1000        // Initiate a session request and check the ID pin.
1001        //
1002        USBOTGSessionRequest(USB0_BASE, true);
1003        break;
1004    }
1005    case eUSBOTGModeWait:
1006    case eUSBOTGModeWaitID: {
1007        //
1008        // If reached the timeout and polling is enabled then look again.
1009        //
1010        if ((g_ui32WaitTicks == 0) && (g_ui32PollRate != 0)) {
1011            //
1012            // Remove the session request.
1013            //
1014            USBOTGSessionRequest(USB0_BASE, false);
1015
1016            //
1017            // Return to idle mode.
1018            //
1019            USBOTGSetMode(eUSBModeNone);
1020
1021            //
1022            // Check if the controller is automatically applying power or
1023            // not.
1024            //
1025            if (USBHCDPowerAutomatic(0) == 0) {
1026                //
1027                // Call the registered event driver to allow it to disable
1028                // power.
1029                //
1030                sEvent.ui32Event = USB_EVENT_POWER_DISABLE;
1031                sEvent.ui32Instance = 0;
1032                InternalUSBHCDSendEvent(0, &sEvent, USBHCD_EVFLAG_PWRDIS);
1033            }
1034
1035            //
1036            // Go back to the idle state.
1037            //
1038            g_eOTGModeState = eUSBOTGModeIdle;
1039        }
1040        break;
1041    }
1042    case eUSBOTGModeAHost: {
1043        //
1044        // Call the host main routine when acting as a host.
1045        //
1046        USBHCDMain();
1047        break;
1048    }
1049    case eUSBOTGModeBWaitCon:
1050    case eUSBOTGModeBDevice:
1051    default: {
1052        break;
1053    }
1054    }
1055}
1056
1057//*****************************************************************************
1058//
1059//! This function sets one of the \b USBLIB_FEATURE_ requests.
1060//!
1061//! \param ui32Index is the index of the USB controller to access.
1062//! \param ui32Feature is one of the \b USBLIB_FEATURE_ defines.
1063//! \param pvFeature is a pointer to the data for the \b USBLIB_FEATURE
1064//!        request.
1065//!
1066//! This function sends the requested feature request to the USB library.
1067//! Not all features are supported by all devices so see the documentation
1068//! for the \b USBLIB_FEATURE_ to determine if the feature is supported.
1069//!
1070//! \return Returns \b true if the feature was set and \b false if the feature
1071//! is not supported or could not be changed to the requested value.
1072//
1073//*****************************************************************************
1074bool USBOTGFeatureSet(uint32_t ui32Index, uint32_t ui32Feature,
1075                      void* pvFeature) {
1076    bool bRetCode;
1077
1078    bRetCode = true;
1079
1080    //
1081    // Pass this on to the host and device and indicate false if
1082    // either fails.
1083    //
1084    if (USBDCDFeatureSet(ui32Index, ui32Feature, pvFeature) == false) {
1085        bRetCode = false;
1086    }
1087
1088    if (USBHCDFeatureSet(ui32Index, ui32Feature, pvFeature) == false) {
1089        bRetCode = false;
1090    }
1091
1092    return (bRetCode);
1093}
1094
1095//*****************************************************************************
1096//
1097// Close the Doxygen group.
1098//! @}
1099//
1100//*****************************************************************************