| LPCOpen Platform v1.03 LPCOpen Platform for NXP LPC Microcontrollers

- Main Page
- Modules
- Data Structures
- Files
- Related Pages

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Macros | Enumerations | Functions | Variables
USB Interface Management
USB Core - software/LPCUSBLib/Drivers/USB/USB.h

## Detailed Description

USB Controller definitions for general USB controller management.

Functions, macros, variables, enums and types related to the setup and management of the USB interface.

## Macros
#define | USB_STREAM_TIMEOUT_MS 100

## Enumerations
enum | USB_Modes_t { USB_MODE_None = 0, USB_MODE_Device = 1, USB_MODE_Host = 2, USB_MODE_UID = 3 }

## Functions
static PRAGMA_ALWAYS_INLINE bool | USB_VBUS_GetStatus (void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE
| Determines if the VBUS line is currently high (i.e. the USB host is supplying power).
static PRAGMA_ALWAYS_INLINE void | USB_Detach (void) ATTR_ALWAYS_INLINE
static PRAGMA_ALWAYS_INLINE void | USB_Attach (void) ATTR_ALWAYS_INLINE
void | USB_Init (uint8_t corenum, uint8_t mode)
void | USB_Disable (uint8_t corenum, uint8_t mode)
void | USB_ResetInterface (uint8_t corenum, uint8_t mode)
void | USB_USBTask (uint8_t corenum, uint8_t mode)

## Variables
volatile uint8_t | USB_CurrentMode []
volatile uint8_t | USB_Options
volatile bool | USB_IsInitialized
USB_Request_Header_t | USB_ControlRequest

## Macro Definition Documentation

#define ENDPOINT_DIR_IN 0x80

Endpoint address direction mask for an IN direction (Device to Host) endpoint. This may be ORed with the index of the address within a device to obtain the full endpoint address.

Definition at line 75 of file USBController.h.

#define ENDPOINT_DIR_MASK 0x80

Endpoint direction mask, for masking against endpoint addresses to retrieve the endpoint's direction for comparing with the `ENDPOINT_DIR_*` masks.

Definition at line 65 of file USBController.h.

#define ENDPOINT_DIR_OUT 0x00

Endpoint address direction mask for an OUT direction (Host to Device) endpoint. This may be ORed with the index of the address within a device to obtain the full endpoint address.

Definition at line 70 of file USBController.h.

#define EP_TYPE_BULK 0x02

Mask for a BULK type endpoint or pipe.

Note
  See Endpoint Management and Pipe Management for endpoint/pipe functions.

Definition at line 101 of file USBController.h.

#define EP_TYPE_CONTROL 0x00

Mask for a CONTROL type endpoint or pipe.

Note
  See Endpoint Management and Pipe Management for endpoint/pipe functions.

Definition at line 89 of file USBController.h.

#define EP_TYPE_INTERRUPT 0x03

Mask for an INTERRUPT type endpoint or pipe.

Note
  See Endpoint Management and Pipe Management for endpoint/pipe functions.

Definition at line 107 of file USBController.h.

#define EP_TYPE_ISOCHRONOUS 0x01

Mask for an ISOCHRONOUS type endpoint or pipe.

Note
  See Endpoint Management and Pipe Management for endpoint/pipe functions.

Definition at line 95 of file USBController.h.

#define EP_TYPE_MASK 0x03

Mask for determining the type of an endpoint from an endpoint descriptor. This should then be compared with the `EP_TYPE_*` masks to determine the exact type of the endpoint.

Definition at line 83 of file USBController.h.

#define USB_OPT_AUTO_PLL (0 << 2)

Automatic PLL control option mask for USB_Init(). This indicates to the library that the library should take full responsibility for controlling the LPC's PLL (used to generate the high frequency clock that the USB controller requires) and ensuring that it is locked at the correct frequency for USB operations.

Definition at line 158 of file USBController.h.

#define USB_OPT_MANUAL_PLL (1 << 2)

Manual PLL control option mask for USB_Init(). This indicates to the library that the user application will take full responsibility for controlling the LPC's PLL (used to generate the high frequency clock that the USB controller requires) and ensuring that it is locked at the correct frequency for USB operations.

Definition at line 152 of file USBController.h.

#define USB_OPT_REG_DISABLED (1 << 1)

Regulator disable option mask for USB_Init(). This indicates that the internal 3.3V USB data pad regulator should be disabled and the LPC's VCC level used for the data pads.

Note
  See USB LPC data sheet for more information on the internal pad regulator.

Definition at line 138 of file USBController.h.

#define USB_OPT_REG_ENABLED (0 << 1)

Regulator enable option mask for USB_Init(). This indicates that the internal 3.3V USB data pad regulator should be enabled to regulate the data pin voltages from the VBUS level down to a level within the range allowable by the USB standard.

Note
  See USB LPC data sheet for more information on the internal pad regulator.

Definition at line 146 of file USBController.h.

#define USB_STREAM_TIMEOUT_MS 100

Constant for the maximum software timeout period of the USB data stream transfer functions (both control and standard) when in either device or host mode. If the next packet of a stream is not received or acknowledged within this time period, the stream function will fail.

This value may be overridden in the user project makefile as the value of the USB_STREAM_TIMEOUT_MS token, and passed to the compiler using the -D switch.

Definition at line 169 of file USBController.h.

## Enumeration Type Documentation

enum USB_Modes_t

Enum for the possible USB controller modes, for initialization via USB_Init() and indication back to the user application via USB_CurrentMode.

Enumerator:

  USB_MODE_None | Indicates that the controller is currently not initialized in any specific USB mode.
  USB_MODE_Device | Indicates that the controller is currently initialized in USB Device mode.
  USB_MODE_Host | Indicates that the controller is currently initialized in USB Host mode.
  USB_MODE_UID | Indicates that the controller should determine the USB mode from the UID pin of the USB connector.

Definition at line 285 of file USBController.h.

## Function Documentation

static void USB_Attach | ( | void |  | ) |
| inline static

Attaches the device to the USB bus. This announces the device's presence to any attached USB host, starting the enumeration process. If no host is present, attaching the device will allow for enumeration once a host is connected to the device.

This is inexplicably also required for proper operation while in host mode, to enable the attachment of a device to the host. This is despite the bit being located in the device-mode register and despite the datasheet making no mention of its requirement in host mode.

Definition at line 213 of file USBController.h.

static void USB_Detach | ( | void |  | ) |
| inline static

Detaches the device from the USB bus. This has the effect of removing the device from any attached host, ceasing USB communications. If no host is present, this prevents any host from enumerating the device once attached until USB_Attach() is called.

Definition at line 199 of file USBController.h.

void USB_Disable | ( | uint8_t | corenum,
|  | uint8_t | mode
| ) |  |

Shuts down the USB interface. This turns off the USB interface after deallocating all USB FIFO memory, endpoints and pipes. When turned off, no USB functionality can be used until the interface is restarted with the USB_Init() function.

Definition at line 55 of file USBController.c.

void USB_Init | ( | uint8_t | corenum,
|  | uint8_t | mode
| ) |  |

Main function to initialize and start the USB interface. Once active, the USB interface will allow for device connection to a host when in device mode, or for device enumeration while in host mode.

As the USB library relies on interrupts for the device and host mode enumeration processes, the user must enable global interrupts before or shortly after this function is called. In device mode, interrupts must be enabled within 500ms of this function being called to ensure that the host does not time out whilst enumerating the device. In host mode, interrupts may be enabled at the application's leisure however enumeration will not begin of an attached device until after this has occurred.

Calling this function when the USB interface is already initialized will cause a complete USB interface reset and re-enumeration.

See Also
  Device Management for the `USB_DEVICE_OPT_*` masks.

Definition at line 40 of file USBController.c.

void USB_ResetInterface | ( | uint8_t | corenum,
|  | uint8_t | mode
| ) |  |

Resets the interface, when already initialized. This will re-enumerate the device if already connected to a host, or re-enumerate an already attached device when in host mode.

Definition at line 82 of file USBController.c.

void USB_USBTask | ( | uint8_t | corenum,
|  | uint8_t | mode
| ) |  |

This is the main USB management task. The USB driver requires this task to be executed continuously when the USB system is active (device attached in host mode, or attached to a host in device mode) in order to manage USB communications. This task may be executed inside an RTOS, fast timer ISR or the main user application loop.

The USB task must be serviced within 30ms while in device mode, or within 1ms while in host mode. The task may be serviced at all times, or (for minimum CPU consumption):

- In device mode, it may be disabled at start-up, enabled on the firing of the EVENT_USB_Device_Connect() event and disabled again on the firing of the EVENT_USB_Device_Disconnect() event.

- In host mode, it may be disabled at start-up, enabled on the firing of the EVENT_USB_Host_DeviceAttached() event and disabled again on the firing of the EVENT_USB_Host_DeviceEnumerationComplete() or EVENT_USB_Host_DeviceEnumerationFailed() events.

If in device mode (only), the control endpoint can instead be managed via interrupts entirely by the library by defining the INTERRUPT_CONTROL_ENDPOINT token and passing it to the compiler via the -D switch.

See Also
  USB Events for more information on the USB events.

static bool USB_VBUS_GetStatus | ( | void |  | ) |
| inline static

Determines if the VBUS line is currently high (i.e. the USB host is supplying power).

Note
  This function is not available on some AVR models which do not support hardware VBUS monitoring.

Returns
  Boolean `true` if the VBUS line is currently detecting power from a host, `false` otherwise.

Definition at line 185 of file USBController.h.

## Variable Documentation

USB_Request_Header_t USB_ControlRequest

Structure containing the last received Control request when in Device mode (for use in user-applications inside of the EVENT_USB_Device_ControlRequest() event, or for filling up with a control request to issue when in Host mode before calling USB_Host_SendControlRequest().

Note
  The contents of this structure is automatically endian-corrected for the current CPU architecture.

volatile uint8_t USB_CurrentMode[]

Definition at line 36 of file USBController.c.

volatile bool USB_IsInitialized

Indicates if the USB interface is currently initialized but not necessarily connected to a host or device (i.e. if USB_Init() has been run). If this is false, all other library globals related to the USB driver are invalid.

Note
  This variable should be treated as read-only in the user application, and never manually changed in value.

Definition at line 38 of file USBTask.c.

volatile uint8_t USB_Options

Indicates the mode that the USB interface is currently initialized to, a value from the USB_Modes_t enum.

Note
  This variable should be treated as read-only in the user application, and never manually changed in value.
  When the controller is initialized into UID auto-detection mode, this variable will hold the currently selected USB mode (i.e. USB_MODE_Device or USB_MODE_Host). If the controller is fixed into a specific mode (either through the `USB_DEVICE_ONLY` or `USB_HOST_ONLY` compile time options, or a limitation of the USB controller in the chosen device model) this will evaluate to a constant of the appropriate value and will never evaluate to USB_MODE_None even when the USB interface is not initialized.Indicates the current USB options that the USB interface was initialized with when USB_Init() was called. This value will be one of the `USB_MODE_*` masks defined elsewhere in this module.
  This variable should be treated as read-only in the user application, and never manually changed in value.