2 * @brief Common USB Controller definitions for all architectures
\r
5 * Copyright(C) NXP Semiconductors, 2012
\r
6 * Copyright(C) Dean Camera, 2011, 2012
\r
7 * All rights reserved.
\r
10 * Software that is described herein is for illustrative purposes only
\r
11 * which provides customers with programming information regarding the
\r
12 * LPC products. This software is supplied "AS IS" without any warranties of
\r
13 * any kind, and NXP Semiconductors and its licensor disclaim any and
\r
14 * all warranties, express or implied, including all implied warranties of
\r
15 * merchantability, fitness for a particular purpose and non-infringement of
\r
16 * intellectual property rights. NXP Semiconductors assumes no responsibility
\r
17 * or liability for the use of the software, conveys no license or rights under any
\r
18 * patent, copyright, mask work right, or any other intellectual property rights in
\r
19 * or to any products. NXP Semiconductors reserves the right to make changes
\r
20 * in the software without notification. NXP Semiconductors also makes no
\r
21 * representation or warranty that such application will be suitable for the
\r
22 * specified use without further testing or modification.
\r
25 * Permission to use, copy, modify, and distribute this software and its
\r
26 * documentation is hereby granted, under NXP Semiconductors' and its
\r
27 * licensor's relevant copyrights in the software, without fee, provided that it
\r
28 * is used in conjunction with NXP Semiconductors microcontrollers. This
\r
29 * copyright, permission, and disclaimer notice must appear in all copies of
\r
33 /** @ingroup Group_USB
\r
34 * @defgroup Group_USBManagement USB Interface Management
\r
35 * @brief USB Controller definitions for general USB controller management.
\r
37 * Functions, macros, variables, enums and types related to the setup and management of the USB interface.
\r
42 #ifndef __USBCONTROLLER_H__
\r
43 #define __USBCONTROLLER_H__
\r
46 #include "../../../Common/Common.h"
\r
47 #include "USBMode.h"
\r
49 /* Enable C linkage for C++ Compilers: */
\r
50 #if defined(__cplusplus)
\r
54 /* Preprocessor Checks and Defines: */
\r
55 #if !defined(__INCLUDE_FROM_USB_DRIVER)
\r
56 #error Do not include this file directly. Include lpcroot/libraries/LPCUSBlib/Drivers/USB/USB.h instead.
\r
60 /** \name Endpoint Direction Masks */
\r
62 /** Endpoint direction mask, for masking against endpoint addresses to retrieve the endpoint's
\r
63 * direction for comparing with the \c ENDPOINT_DIR_* masks.
\r
65 #define ENDPOINT_DIR_MASK 0x80
\r
67 /** Endpoint address direction mask for an OUT direction (Host to Device) endpoint. This may be ORed with
\r
68 * the index of the address within a device to obtain the full endpoint address.
\r
70 #define ENDPOINT_DIR_OUT 0x00
\r
72 /** Endpoint address direction mask for an IN direction (Device to Host) endpoint. This may be ORed with
\r
73 * the index of the address within a device to obtain the full endpoint address.
\r
75 #define ENDPOINT_DIR_IN 0x80
\r
78 /** \name Endpoint/Pipe Type Masks */
\r
80 /** Mask for determining the type of an endpoint from an endpoint descriptor. This should then be compared
\r
81 * with the \c EP_TYPE_* masks to determine the exact type of the endpoint.
\r
83 #define EP_TYPE_MASK 0x03
\r
85 /** Mask for a CONTROL type endpoint or pipe.
\r
87 * @note See @ref Group_EndpointManagement and @ref Group_PipeManagement for endpoint/pipe functions.
\r
89 #define EP_TYPE_CONTROL 0x00
\r
91 /** Mask for an ISOCHRONOUS type endpoint or pipe.
\r
93 * @note See @ref Group_EndpointManagement and @ref Group_PipeManagement for endpoint/pipe functions.
\r
95 #define EP_TYPE_ISOCHRONOUS 0x01
\r
97 /** Mask for a BULK type endpoint or pipe.
\r
99 * @note See @ref Group_EndpointManagement and @ref Group_PipeManagement for endpoint/pipe functions.
\r
101 #define EP_TYPE_BULK 0x02
\r
103 /** Mask for an INTERRUPT type endpoint or pipe.
\r
105 * @note See @ref Group_EndpointManagement and @ref Group_PipeManagement for endpoint/pipe functions.
\r
107 #define EP_TYPE_INTERRUPT 0x03
\r
110 #include "Events.h"
\r
111 #include "USBTask.h"
\r
112 #include "USBInterrupt.h"
\r
114 #if defined(USB_CAN_BE_HOST) || defined(__DOXYGEN__)
\r
118 #include "HostStandardReq.h"
\r
119 #include "PipeStream.h"
\r
122 #if defined(USB_CAN_BE_DEVICE) || defined(__DOXYGEN__)
\r
123 #include "Device.h"
\r
124 #include "Endpoint.h"
\r
125 #include "DeviceStandardReq.h"
\r
126 #include "EndpointStream.h"
\r
129 /* Public Interface - May be used in end-application: */
\r
131 /** @name USB Controller Option Masks */
\r
133 /** Regulator disable option mask for @ref USB_Init(). This indicates that the internal 3.3V USB data pad
\r
134 * regulator should be disabled and the LPC's VCC level used for the data pads.
\r
136 * @note See USB LPC data sheet for more information on the internal pad regulator.
\r
138 #define USB_OPT_REG_DISABLED (1 << 1)
\r
140 /** Regulator enable option mask for @ref USB_Init(). This indicates that the internal 3.3V USB data pad
\r
141 * regulator should be enabled to regulate the data pin voltages from the VBUS level down to a level within
\r
142 * the range allowable by the USB standard.
\r
144 * @note See USB LPC data sheet for more information on the internal pad regulator.
\r
146 #define USB_OPT_REG_ENABLED (0 << 1)
\r
148 /** Manual PLL control option mask for @ref USB_Init(). This indicates to the library that the user application
\r
149 * will take full responsibility for controlling the LPC's PLL (used to generate the high frequency clock
\r
150 * that the USB controller requires) and ensuring that it is locked at the correct frequency for USB operations.
\r
152 #define USB_OPT_MANUAL_PLL (1 << 2)
\r
154 /** Automatic PLL control option mask for @ref USB_Init(). This indicates to the library that the library should
\r
155 * take full responsibility for controlling the LPC's PLL (used to generate the high frequency clock
\r
156 * that the USB controller requires) and ensuring that it is locked at the correct frequency for USB operations.
\r
158 #define USB_OPT_AUTO_PLL (0 << 2)
\r
161 #if !defined(USB_STREAM_TIMEOUT_MS) || defined(__DOXYGEN__)
\r
162 /** Constant for the maximum software timeout period of the USB data stream transfer functions
\r
163 * (both control and standard) when in either device or host mode. If the next packet of a stream
\r
164 * is not received or acknowledged within this time period, the stream function will fail.
\r
166 * This value may be overridden in the user project makefile as the value of the
\r
167 * @ref USB_STREAM_TIMEOUT_MS token, and passed to the compiler using the -D switch.
\r
169 #define USB_STREAM_TIMEOUT_MS 100
\r
172 /* Inline Functions: */
\r
173 #if defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR) || \
\r
174 defined(__DOXYGEN__)
\r
176 * @brief Determines if the VBUS line is currently high (i.e. the USB host is supplying power).
\r
178 * @note This function is not available on some AVR models which do not support hardware VBUS monitoring.
\r
180 * @return Boolean \c true if the VBUS line is currently detecting power from a host, \c false otherwise.
\r
182 PRAGMA_ALWAYS_INLINE
\r
183 static inline bool USB_VBUS_GetStatus(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
\r
185 static inline bool USB_VBUS_GetStatus(void)
\r
187 return (USBSTA & (1 << VBUS)) ? true : false;
\r
192 /** Detaches the device from the USB bus. This has the effect of removing the device from any
\r
193 * attached host, ceasing USB communications. If no host is present, this prevents any host from
\r
194 * enumerating the device once attached until @ref USB_Attach() is called.
\r
196 PRAGMA_ALWAYS_INLINE
\r
197 static inline void USB_Detach(void) ATTR_ALWAYS_INLINE;
\r
199 static inline void USB_Detach(void)
\r
202 /** Attaches the device to the USB bus. This announces the device's presence to any attached
\r
203 * USB host, starting the enumeration process. If no host is present, attaching the device
\r
204 * will allow for enumeration once a host is connected to the device.
\r
206 * This is inexplicably also required for proper operation while in host mode, to enable the
\r
207 * attachment of a device to the host. This is despite the bit being located in the device-mode
\r
208 * register and despite the datasheet making no mention of its requirement in host mode.
\r
210 PRAGMA_ALWAYS_INLINE
\r
211 static inline void USB_Attach(void) ATTR_ALWAYS_INLINE;
\r
213 static inline void USB_Attach(void)
\r
216 /* Function Prototypes: */
\r
217 /** Main function to initialize and start the USB interface. Once active, the USB interface will
\r
218 * allow for device connection to a host when in device mode, or for device enumeration while in
\r
221 * As the USB library relies on interrupts for the device and host mode enumeration processes,
\r
222 * the user must enable global interrupts before or shortly after this function is called. In
\r
223 * device mode, interrupts must be enabled within 500ms of this function being called to ensure
\r
224 * that the host does not time out whilst enumerating the device. In host mode, interrupts may be
\r
225 * enabled at the application's leisure however enumeration will not begin of an attached device
\r
226 * until after this has occurred.
\r
228 * Calling this function when the USB interface is already initialized will cause a complete USB
\r
229 * interface reset and re-enumeration.
\r
231 * \see @ref Group_Device for the \c USB_DEVICE_OPT_* masks.
\r
233 void USB_Init(uint8_t corenum, uint8_t mode);
\r
235 /** Shuts down the USB interface. This turns off the USB interface after deallocating all USB FIFO
\r
236 * memory, endpoints and pipes. When turned off, no USB functionality can be used until the interface
\r
237 * is restarted with the @ref USB_Init() function.
\r
239 void USB_Disable(uint8_t corenum, uint8_t mode);
\r
241 /** Resets the interface, when already initialized. This will re-enumerate the device if already connected
\r
242 * to a host, or re-enumerate an already attached device when in host mode.
\r
244 void USB_ResetInterface(uint8_t corenum, uint8_t mode);
\r
246 /* Global Variables: */
\r
247 extern volatile uint8_t USB_CurrentMode[];
\r
248 // #if (!defined(USB_HOST_ONLY) && !defined(USB_DEVICE_ONLY)) || defined(__DOXYGEN__)
\r
249 /** Indicates the mode that the USB interface is currently initialized to, a value from the
\r
250 * @ref USB_Modes_t enum.
\r
252 * @note This variable should be treated as read-only in the user application, and never manually
\r
253 * changed in value.
\r
255 * @note When the controller is initialized into UID auto-detection mode, this variable will hold the
\r
256 * currently selected USB mode (i.e. @ref USB_MODE_Device or @ref USB_MODE_Host). If the controller
\r
257 * is fixed into a specific mode (either through the \c USB_DEVICE_ONLY or \c USB_HOST_ONLY compile time
\r
258 * options, or a limitation of the USB controller in the chosen device model) this will evaluate to
\r
259 * a constant of the appropriate value and will never evaluate to @ref USB_MODE_None even when the
\r
260 * USB interface is not initialized.
\r
262 // extern volatile uint8_t USB_CurrentMode[];
\r
263 // #elif defined(USB_HOST_ONLY)
\r
264 // #define USB_CurrentMode[0] USB_MODE_Host
\r
265 // #elif defined(USB_DEVICE_ONLY)
\r
266 // #define USB_CurrentMode[0] USB_MODE_Device
\r
269 #if !defined(USE_STATIC_OPTIONS) || defined(__DOXYGEN__)
\r
270 /** Indicates the current USB options that the USB interface was initialized with when @ref USB_Init()
\r
271 * was called. This value will be one of the \c USB_MODE_* masks defined elsewhere in this module.
\r
273 * @note This variable should be treated as read-only in the user application, and never manually
\r
274 * changed in value.
\r
276 extern volatile uint8_t USB_Options;
\r
277 #elif defined(USE_STATIC_OPTIONS)
\r
278 #define USB_Options USE_STATIC_OPTIONS
\r
282 /** Enum for the possible USB controller modes, for initialization via @ref USB_Init() and indication back to the
\r
283 * user application via @ref USB_CurrentMode.
\r
286 USB_MODE_None = 0, /**< Indicates that the controller is currently not initialized in any specific USB mode. */
\r
287 USB_MODE_Device = 1, /**< Indicates that the controller is currently initialized in USB Device mode. */
\r
288 USB_MODE_Host = 2, /**< Indicates that the controller is currently initialized in USB Host mode. */
\r
289 USB_MODE_UID = 3, /**< Indicates that the controller should determine the USB mode from the UID pin of the
\r
294 /* Private Interface - For use in library only: */
\r
295 #if !defined(__DOXYGEN__)
\r
296 /* Function Prototypes: */
\r
297 #if defined(__INCLUDE_FROM_USB_CONTROLLER_C)
\r
298 #if defined(USB_CAN_BE_DEVICE)
\r
299 static void USB_Init_Device(uint8_t corenum);
\r
303 #if defined(USB_CAN_BE_HOST)
\r
304 static void USB_Init_Host(uint8_t corenum);
\r
309 /* Inline Functions: */
\r
310 PRAGMA_ALWAYS_INLINE
\r
311 static inline void USB_PLL_On(void) ATTR_ALWAYS_INLINE;
\r
313 static inline void USB_PLL_On(void)
\r
316 PRAGMA_ALWAYS_INLINE
\r
317 static inline void USB_PLL_Off(void) ATTR_ALWAYS_INLINE;
\r
319 static inline void USB_PLL_Off(void)
\r
322 PRAGMA_ALWAYS_INLINE
\r
323 static inline bool USB_PLL_IsReady(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
\r
325 static inline bool USB_PLL_IsReady(void)
\r
327 return true; // implement later if needed
\r
330 PRAGMA_ALWAYS_INLINE
\r
331 static inline void USB_REG_On(void) ATTR_ALWAYS_INLINE;
\r
333 static inline void USB_REG_On(void)
\r
336 PRAGMA_ALWAYS_INLINE
\r
337 static inline void USB_REG_Off(void) ATTR_ALWAYS_INLINE;
\r
339 static inline void USB_REG_Off(void)
\r
342 PRAGMA_ALWAYS_INLINE
\r
343 static inline void USB_OTGPAD_On(void) ATTR_ALWAYS_INLINE;
\r
345 static inline void USB_OTGPAD_On(void)
\r
348 PRAGMA_ALWAYS_INLINE
\r
349 static inline void USB_OTGPAD_Off(void) ATTR_ALWAYS_INLINE;
\r
351 static inline void USB_OTGPAD_Off(void)
\r
354 PRAGMA_ALWAYS_INLINE
\r
355 static inline void USB_CLK_Freeze(void) ATTR_ALWAYS_INLINE;
\r
357 static inline void USB_CLK_Freeze(void)
\r
360 PRAGMA_ALWAYS_INLINE
\r
361 static inline void USB_CLK_Unfreeze(void) ATTR_ALWAYS_INLINE;
\r
363 static inline void USB_CLK_Unfreeze(void)
\r
366 PRAGMA_ALWAYS_INLINE
\r
367 static inline void USB_Controller_Enable(void) ATTR_ALWAYS_INLINE;
\r
369 static inline void USB_Controller_Enable(void)
\r
372 PRAGMA_ALWAYS_INLINE
\r
373 static inline void USB_Controller_Disable(void) ATTR_ALWAYS_INLINE;
\r
375 static inline void USB_Controller_Disable(void)
\r
378 PRAGMA_ALWAYS_INLINE
\r
379 static inline void USB_Controller_Reset(void) ATTR_ALWAYS_INLINE;
\r
381 static inline void USB_Controller_Reset(void)
\r
384 #if defined(USB_CAN_BE_BOTH)
\r
385 PRAGMA_ALWAYS_INLINE
\r
386 static inline uint8_t USB_GetUSBModeFromUID(void) ATTR_WARN_UNUSED_RESULT ATTR_ALWAYS_INLINE;
\r
388 static inline uint8_t USB_GetUSBModeFromUID(void)
\r
390 // if (USBSTA & (1 << ID))
\r
391 // return USB_MODE_Device;
\r
393 // return USB_MODE_Host;
\r
402 /* Disable C linkage for C++ Compilers: */
\r
403 #if defined(__cplusplus)
\r