Remove USB_MODE_* macros, replace with a semantically linked USB_Modes_t enum.

Moved the USB device selection logic for ENDPOINT_TOTAL_ENDPOINTS further up in Endpoint.h to where the endpoint bank capabilities are determined, to reduce the total number of device-specific logic.

Change USB_Host_WaitMS() to test and disable the HSOFI interrupt before resuming the bus, so that it does not fire before the delay loop has run.

Add missing const qualifier to the parameter of USB_Host_ClearPipeStall().
pull/1469/head
Dean Camera 14 years ago
parent c1cfffd8eb
commit 7aaced1e8b

@ -51,7 +51,7 @@ int main(void)
for (;;) for (;;)
{ {
/* Determine which USB mode we are currently in */ /* Determine which USB mode we are currently in */
if (USB_CurrentMode == USB_MODE_HOST) if (USB_CurrentMode == USB_MODE_Host)
{ {
MouseHostTask(); MouseHostTask();
HID_Host_USBTask(&Mouse_HID_Host_Interface); HID_Host_USBTask(&Mouse_HID_Host_Interface);
@ -87,7 +87,7 @@ void SetupHardware(void)
void EVENT_USB_UIDChange(void) void EVENT_USB_UIDChange(void)
{ {
printf_P(PSTR(ESC_FG_YELLOW "UID Change to %S mode\r\n" ESC_FG_WHITE), printf_P(PSTR(ESC_FG_YELLOW "UID Change to %S mode\r\n" ESC_FG_WHITE),
(USB_CurrentMode == USB_MODE_DEVICE) ? PSTR("Device") : PSTR("Host")); (USB_CurrentMode == USB_MODE_Device) ? PSTR("Device") : PSTR("Host"));
LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY); LEDs_SetAllLEDs(LEDMASK_USB_NOTREADY);
} }

@ -50,9 +50,9 @@ void USB_USBTask(void)
#elif defined(USB_DEVICE_ONLY) #elif defined(USB_DEVICE_ONLY)
USB_DeviceTask(); USB_DeviceTask();
#else #else
if (USB_CurrentMode == USB_MODE_DEVICE) if (USB_CurrentMode == USB_MODE_Device)
USB_DeviceTask(); USB_DeviceTask();
else if (USB_CurrentMode == USB_MODE_HOST) else if (USB_CurrentMode == USB_MODE_Host)
USB_HostTask(); USB_HostTask();
#endif #endif
} }

@ -100,6 +100,8 @@
#define _ENDPOINT_GET_DOUBLEBANK3(MaxSize, DB) (DB) #define _ENDPOINT_GET_DOUBLEBANK3(MaxSize, DB) (DB)
#if defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR) #if defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR)
#define ENDPOINT_DETAILS_MAXEP 7
#define ENDPOINT_DETAILS_EP0 64, true #define ENDPOINT_DETAILS_EP0 64, true
#define ENDPOINT_DETAILS_EP1 256, true #define ENDPOINT_DETAILS_EP1 256, true
#define ENDPOINT_DETAILS_EP2 64, true #define ENDPOINT_DETAILS_EP2 64, true
@ -108,6 +110,8 @@
#define ENDPOINT_DETAILS_EP5 64, true #define ENDPOINT_DETAILS_EP5 64, true
#define ENDPOINT_DETAILS_EP6 64, true #define ENDPOINT_DETAILS_EP6 64, true
#else #else
#define ENDPOINT_DETAILS_MAXEP 5
#define ENDPOINT_DETAILS_EP0 64, true #define ENDPOINT_DETAILS_EP0 64, true
#define ENDPOINT_DETAILS_EP1 64, false #define ENDPOINT_DETAILS_EP1 64, false
#define ENDPOINT_DETAILS_EP2 64, false #define ENDPOINT_DETAILS_EP2 64, false
@ -145,78 +149,74 @@
/** Endpoint data direction mask for \ref Endpoint_ConfigureEndpoint(). This indicates that the endpoint /** Endpoint data direction mask for \ref Endpoint_ConfigureEndpoint(). This indicates that the endpoint
* should be initialized in the OUT direction - i.e. data flows from host to device. * should be initialized in the OUT direction - i.e. data flows from host to device.
*/ */
#define ENDPOINT_DIR_OUT (0 << EPDIR) #define ENDPOINT_DIR_OUT (0 << EPDIR)
/** Endpoint data direction mask for \ref Endpoint_ConfigureEndpoint(). This indicates that the endpoint /** Endpoint data direction mask for \ref Endpoint_ConfigureEndpoint(). This indicates that the endpoint
* should be initialized in the IN direction - i.e. data flows from device to host. * should be initialized in the IN direction - i.e. data flows from device to host.
*/ */
#define ENDPOINT_DIR_IN (1 << EPDIR) #define ENDPOINT_DIR_IN (1 << EPDIR)
/** Mask for the bank mode selection for the \ref Endpoint_ConfigureEndpoint() macro. This indicates /** Mask for the bank mode selection for the \ref Endpoint_ConfigureEndpoint() macro. This indicates
* that the endpoint should have one single bank, which requires less USB FIFO memory but results * that the endpoint should have one single bank, which requires less USB FIFO memory but results
* in slower transfers as only one USB device (the AVR or the host) can access the endpoint's * in slower transfers as only one USB device (the AVR or the host) can access the endpoint's
* bank at the one time. * bank at the one time.
*/ */
#define ENDPOINT_BANK_SINGLE (0 << EPBK0) #define ENDPOINT_BANK_SINGLE (0 << EPBK0)
/** Mask for the bank mode selection for the \ref Endpoint_ConfigureEndpoint() macro. This indicates /** Mask for the bank mode selection for the \ref Endpoint_ConfigureEndpoint() macro. This indicates
* that the endpoint should have two banks, which requires more USB FIFO memory but results * that the endpoint should have two banks, which requires more USB FIFO memory but results
* in faster transfers as one USB device (the AVR or the host) can access one bank while the other * in faster transfers as one USB device (the AVR or the host) can access one bank while the other
* accesses the second bank. * accesses the second bank.
*/ */
#define ENDPOINT_BANK_DOUBLE (1 << EPBK0) #define ENDPOINT_BANK_DOUBLE (1 << EPBK0)
/** Endpoint address for the default control endpoint, which always resides in address 0. This is /** Endpoint address for the default control endpoint, which always resides in address 0. This is
* defined for convenience to give more readable code when used with the endpoint macros. * defined for convenience to give more readable code when used with the endpoint macros.
*/ */
#define ENDPOINT_CONTROLEP 0 #define ENDPOINT_CONTROLEP 0
#if (!defined(FIXED_CONTROL_ENDPOINT_SIZE) || defined(__DOXYGEN__)) #if (!defined(FIXED_CONTROL_ENDPOINT_SIZE) || defined(__DOXYGEN__))
/** Default size of the default control endpoint's bank, until altered by the control endpoint bank size /** Default size of the default control endpoint's bank, until altered by the control endpoint bank size
* value in the device descriptor. Not available if the FIXED_CONTROL_ENDPOINT_SIZE token is defined. * value in the device descriptor. Not available if the FIXED_CONTROL_ENDPOINT_SIZE token is defined.
*/ */
#define ENDPOINT_CONTROLEP_DEFAULT_SIZE 8 #define ENDPOINT_CONTROLEP_DEFAULT_SIZE 8
#endif #endif
/** Endpoint number mask, for masking against endpoint addresses to retrieve the endpoint's /** Endpoint number mask, for masking against endpoint addresses to retrieve the endpoint's
* numerical address in the device. * numerical address in the device.
*/ */
#define ENDPOINT_EPNUM_MASK 0x07 #define ENDPOINT_EPNUM_MASK 0x07
/** Endpoint direction mask, for masking against endpoint addresses to retrieve the endpoint's /** Endpoint direction mask, for masking against endpoint addresses to retrieve the endpoint's
* direction for comparing with the ENDPOINT_DESCRIPTOR_DIR_* masks. * direction for comparing with the ENDPOINT_DESCRIPTOR_DIR_* masks.
*/ */
#define ENDPOINT_EPDIR_MASK 0x80 #define ENDPOINT_EPDIR_MASK 0x80
/** Endpoint bank size mask, for masking against endpoint addresses to retrieve the endpoint's /** Endpoint bank size mask, for masking against endpoint addresses to retrieve the endpoint's
* bank size in the device. * bank size in the device.
*/ */
#define ENDPOINT_EPSIZE_MASK 0x7F #define ENDPOINT_EPSIZE_MASK 0x7F
/** Maximum size in bytes of a given endpoint. /** Maximum size in bytes of a given endpoint.
* *
* \param[in] n Endpoint number, a value between 0 and (ENDPOINT_TOTAL_ENDPOINTS - 1) * \param[in] EPIndex Endpoint number, a value between 0 and (ENDPOINT_TOTAL_ENDPOINTS - 1)
*/ */
#define ENDPOINT_MAX_SIZE(n) _ENDPOINT_GET_MAXSIZE(n) #define ENDPOINT_MAX_SIZE(EPIndex) _ENDPOINT_GET_MAXSIZE(EPIndex)
/** Indicates if the given endpoint supports double banking. /** Indicates if the given endpoint supports double banking.
* *
* \param[in] n Endpoint number, a value between 0 and (ENDPOINT_TOTAL_ENDPOINTS - 1) * \param[in] EPIndex Endpoint number, a value between 0 and (ENDPOINT_TOTAL_ENDPOINTS - 1)
*/ */
#define ENDPOINT_DOUBLEBANK_SUPPORTED(n) _ENDPOINT_GET_DOUBLEBANK(n) #define ENDPOINT_DOUBLEBANK_SUPPORTED(EPIndex) _ENDPOINT_GET_DOUBLEBANK(EPIndex)
#if !defined(CONTROL_ONLY_DEVICE) #if !defined(CONTROL_ONLY_DEVICE) || defined(__DOXYGEN__)
#if defined(USB_SERIES_4_AVR) || defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR) || defined(__DOXYGEN__) /** Total number of endpoints (including the default control endpoint at address 0) which may
/** Total number of endpoints (including the default control endpoint at address 0) which may * be used in the device. Different USB AVR models support different amounts of endpoints,
* be used in the device. Different USB AVR models support different amounts of endpoints, * this value reflects the maximum number of endpoints for the currently selected AVR model.
* this value reflects the maximum number of endpoints for the currently selected AVR model. */
*/ #define ENDPOINT_TOTAL_ENDPOINTS ENDPOINT_DETAILS_MAXEP
#define ENDPOINT_TOTAL_ENDPOINTS 7
#else
#define ENDPOINT_TOTAL_ENDPOINTS 5
#endif
#else #else
#define ENDPOINT_TOTAL_ENDPOINTS 1 #define ENDPOINT_TOTAL_ENDPOINTS 1
#endif #endif
/* Enums: */ /* Enums: */

@ -196,14 +196,13 @@ uint8_t USB_Host_WaitMS(uint8_t MS)
{ {
bool BusSuspended = USB_Host_IsBusSuspended(); bool BusSuspended = USB_Host_IsBusSuspended();
uint8_t ErrorCode = HOST_WAITERROR_Successful; uint8_t ErrorCode = HOST_WAITERROR_Successful;
bool HSOFIEnabled = USB_INT_IsEnabled(USB_INT_HSOFI);
USB_Host_ResumeBus();
bool HSOFIEnabled = USB_INT_IsEnabled(USB_INT_HSOFI);
USB_INT_Disable(USB_INT_HSOFI); USB_INT_Disable(USB_INT_HSOFI);
USB_INT_Clear(USB_INT_HSOFI); USB_INT_Clear(USB_INT_HSOFI);
USB_Host_ResumeBus();
while (MS) while (MS)
{ {
if (USB_INT_HasOccurred(USB_INT_HSOFI)) if (USB_INT_HasOccurred(USB_INT_HSOFI))
@ -212,7 +211,7 @@ uint8_t USB_Host_WaitMS(uint8_t MS)
MS--; MS--;
} }
if ((USB_HostState == HOST_STATE_Unattached) || (USB_CurrentMode == USB_MODE_DEVICE)) if ((USB_HostState == HOST_STATE_Unattached) || (USB_CurrentMode != USB_MODE_Host))
{ {
ErrorCode = HOST_WAITERROR_DeviceDisconnect; ErrorCode = HOST_WAITERROR_DeviceDisconnect;
@ -236,12 +235,12 @@ uint8_t USB_Host_WaitMS(uint8_t MS)
} }
} }
if (HSOFIEnabled)
USB_INT_Enable(USB_INT_HSOFI);
if (BusSuspended) if (BusSuspended)
USB_Host_SuspendBus(); USB_Host_SuspendBus();
if (HSOFIEnabled)
USB_INT_Enable(USB_INT_HSOFI);
return ErrorCode; return ErrorCode;
} }
@ -336,7 +335,7 @@ uint8_t USB_Host_GetDeviceStringDescriptor(const uint8_t Index,
return USB_Host_SendControlRequest(Buffer); return USB_Host_SendControlRequest(Buffer);
} }
uint8_t USB_Host_ClearPipeStall(uint8_t EndpointNum) uint8_t USB_Host_ClearPipeStall(const uint8_t EndpointNum)
{ {
USB_ControlRequest = (USB_Request_Header_t) USB_ControlRequest = (USB_Request_Header_t)
{ {

@ -435,7 +435,7 @@
* *
* \return A value from the \ref USB_Host_SendControlErrorCodes_t enum to indicate the result. * \return A value from the \ref USB_Host_SendControlErrorCodes_t enum to indicate the result.
*/ */
uint8_t USB_Host_ClearPipeStall(uint8_t EndpointIndex); uint8_t USB_Host_ClearPipeStall(const uint8_t EndpointIndex);
/* Private Interface - For use in library only: */ /* Private Interface - For use in library only: */
#if !defined(__DOXYGEN__) #if !defined(__DOXYGEN__)

@ -118,7 +118,7 @@
static inline void USB_OTG_Device_InitiateSRP(const uint8_t SRPTypeMask) ATTR_ALWAYS_INLINE; static inline void USB_OTG_Device_InitiateSRP(const uint8_t SRPTypeMask) ATTR_ALWAYS_INLINE;
static inline void USB_OTG_Device_InitiateSRP(const uint8_t SRPTypeMask) static inline void USB_OTG_Device_InitiateSRP(const uint8_t SRPTypeMask)
{ {
OTGCON = ((OTGCON & ~(1 << SRPSEL)) | (SRPTypeMask | (1 << SRPREQ))); OTGCON = ((OTGCON & ~(1 << SRPSEL)) | (SRPTypeMask | (1 << SRPREQ)));
} }
/** Accepts a HNP from a connected device, indicating that both devices should exchange /** Accepts a HNP from a connected device, indicating that both devices should exchange

@ -855,7 +855,7 @@
* *
* \return Boolean true if the configuration succeeded, false otherwise. * \return Boolean true if the configuration succeeded, false otherwise.
*/ */
bool Pipe_ConfigurePipe(const uint8_t Number, bool Pipe_ConfigurePipe(const uint8_t Number,
const uint8_t Type, const uint8_t Type,
const uint8_t Token, const uint8_t Token,
const uint8_t EndpointNumber, const uint8_t EndpointNumber,

@ -33,7 +33,7 @@
#include "USBController.h" #include "USBController.h"
#if (!defined(USB_HOST_ONLY) && !defined(USB_DEVICE_ONLY)) #if (!defined(USB_HOST_ONLY) && !defined(USB_DEVICE_ONLY))
volatile uint8_t USB_CurrentMode = USB_MODE_NONE; volatile uint8_t USB_CurrentMode = USB_MODE_None;
#endif #endif
#if !defined(USE_STATIC_OPTIONS) #if !defined(USE_STATIC_OPTIONS)
@ -101,7 +101,7 @@ void USB_ShutDown(void)
#endif #endif
#if defined(USB_CAN_BE_BOTH) #if defined(USB_CAN_BE_BOTH)
USB_CurrentMode = USB_MODE_NONE; USB_CurrentMode = USB_MODE_None;
#endif #endif
USB_IsInitialized = false; USB_IsInitialized = false;
@ -138,7 +138,7 @@ void USB_ResetInterface(void)
USB_CLK_Unfreeze(); USB_CLK_Unfreeze();
if (USB_CurrentMode == USB_MODE_DEVICE) if (USB_CurrentMode == USB_MODE_Device)
{ {
#if defined(USB_CAN_BE_DEVICE) #if defined(USB_CAN_BE_DEVICE)
#if (defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR)) #if (defined(USB_SERIES_6_AVR) || defined(USB_SERIES_7_AVR))
@ -148,7 +148,7 @@ void USB_ResetInterface(void)
USB_Init_Device(); USB_Init_Device();
#endif #endif
} }
else else if (USB_CurrentMode == USB_MODE_Host)
{ {
#if defined(USB_CAN_BE_HOST) #if defined(USB_CAN_BE_HOST)
UHWCON &= ~(1 << UIMOD); UHWCON &= ~(1 << UIMOD);

@ -122,32 +122,7 @@
#endif #endif
/* Public Interface - May be used in end-application: */ /* Public Interface - May be used in end-application: */
/* Macros: */ /* Macros: */
/** Mode mask for the \ref USB_CurrentMode global. This indicates that the USB interface is currently not
* initialized into any mode.
*/
#define USB_MODE_NONE 0
/** Mode mask for the \ref USB_CurrentMode global and the \ref USB_Init() function. This indicates that the
* USB interface is or should be initialized in the USB device mode.
*/
#define USB_MODE_DEVICE 1
/** Mode mask for the \ref USB_CurrentMode global and the \ref USB_Init() function. This indicates that the
* USB interface is or should be initialized in the USB host mode.
*/
#define USB_MODE_HOST 2
#if defined(USB_CAN_BE_BOTH) || defined(__DOXYGEN__)
/** Mode mask for the the \ref USB_Init() function. This indicates that the USB interface should be
* initialized into whatever mode the UID pin of the USB AVR indicates, and that the device
* should swap over its mode when the level of the UID pin changes during operation.
*
* \note This token is not available on AVR models which do not support both host and device modes.
*/
#define USB_MODE_UID 3
#endif
/** Regulator disable option mask for \ref USB_Init(). This indicates that the internal 3.3V USB data pad /** Regulator disable option mask for \ref USB_Init(). This indicates that the internal 3.3V USB data pad
* regulator should be enabled to regulate the data pin voltages to within the USB standard. * regulator should be enabled to regulate the data pin voltages to within the USB standard.
* *
@ -262,8 +237,8 @@
* Calling this function when the USB interface is already initialized will cause a complete USB * Calling this function when the USB interface is already initialized will cause a complete USB
* interface reset and re-enumeration. * interface reset and re-enumeration.
* *
* \param[in] Mode This is a mask indicating what mode the USB interface is to be initialized to. * \param[in] Mode This is a mask indicating what mode the USB interface is to be initialized to, a value
* Valid mode masks are \ref USB_MODE_DEVICE, \ref USB_MODE_HOST or \ref USB_MODE_UID. * from the \ref USB_Modes_t enum.
* *
* \param[in] Options Mask indicating the options which should be used when initializing the USB * \param[in] Options Mask indicating the options which should be used when initializing the USB
* interface to control the USB interface's behaviour. This should be comprised of * interface to control the USB interface's behaviour. This should be comprised of
@ -319,17 +294,25 @@
/* Global Variables: */ /* Global Variables: */
#if (!defined(USB_HOST_ONLY) && !defined(USB_DEVICE_ONLY)) || defined(__DOXYGEN__) #if (!defined(USB_HOST_ONLY) && !defined(USB_DEVICE_ONLY)) || defined(__DOXYGEN__)
/** Indicates the mode that the USB interface is currently initialized to. This value will be /** Indicates the mode that the USB interface is currently initialized to, a value from the
* one of the USB_MODE_* masks defined elsewhere in this module. * \ref USB_Modes_t enum.
* *
* \note This variable should be treated as read-only in the user application, and never manually * \note This variable should be treated as read-only in the user application, and never manually
* changed in value. * changed in value.
* \n\n
*
* \note When the controller is initialized into UID autodetection mode, this variable will hold the
* currently selected USB mode (i.e. \ref USB_MODE_Device or \ref 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 \ref USB_MODE_None even when the
* USB interface is not initialized.
*/ */
extern volatile uint8_t USB_CurrentMode; extern volatile uint8_t USB_CurrentMode;
#elif defined(USB_HOST_ONLY) #elif defined(USB_HOST_ONLY)
#define USB_CurrentMode USB_MODE_HOST #define USB_CurrentMode USB_MODE_Host
#elif defined(USB_DEVICE_ONLY) #elif defined(USB_DEVICE_ONLY)
#define USB_CurrentMode USB_MODE_DEVICE #define USB_CurrentMode USB_MODE_Device
#endif #endif
#if !defined(USE_STATIC_OPTIONS) || defined(__DOXYGEN__) #if !defined(USE_STATIC_OPTIONS) || defined(__DOXYGEN__)
@ -344,6 +327,20 @@
#define USB_Options USE_STATIC_OPTIONS #define USB_Options USE_STATIC_OPTIONS
#endif #endif
/* Enums: */
/** Enum for the possible USB controller modes, for initialization via \ref USB_Init() and indication back to the
* user application via \ref USB_CurrentMode.
*/
enum USB_Modes_t
{
USB_MODE_None = 0, /**< Indicates that the controller is currently not initialized in any specific USB mode. */
USB_MODE_Device = 1, /**< Indicates that the controller is currently initialized in USB Device mode. */
USB_MODE_Host = 2, /**< Indicates that the controller is currently initialized in USB Host mode. */
USB_MODE_UID = 3, /**< Indicates that the controller should determine the USB mode from the UID pin of the
* USB connector.
*/
};
/* Private Interface - For use in library only: */ /* Private Interface - For use in library only: */
#if !defined(__DOXYGEN__) #if !defined(__DOXYGEN__)
/* Function Prototypes: */ /* Function Prototypes: */
@ -449,9 +446,9 @@
static inline uint8_t USB_GetUSBModeFromUID(void) static inline uint8_t USB_GetUSBModeFromUID(void)
{ {
if (USBSTA & (1 << ID)) if (USBSTA & (1 << ID))
return USB_MODE_DEVICE; return USB_MODE_Device;
else else
return USB_MODE_HOST; return USB_MODE_Host;
} }
#endif #endif

@ -47,6 +47,7 @@
* - Renamed and moved class driver common constant definitions to make the naming scheme more uniform * - Renamed and moved class driver common constant definitions to make the naming scheme more uniform
* - Changed default value for the reset polarity parameter in the AVRISP-MKII project so that it defaults to active low drive * - Changed default value for the reset polarity parameter in the AVRISP-MKII project so that it defaults to active low drive
* - Changed configuration descriptor parser for all host mode projects and class drivers to ensure better compatibility with devices * - Changed configuration descriptor parser for all host mode projects and class drivers to ensure better compatibility with devices
* - Moved the USB mode specifier constants into a new enum, so that they are semantically related to one another
* *
* <b>Fixed:</b> * <b>Fixed:</b>
* - Fixed USB_GetHIDReportItemInfo() function modifying the given report item's data when the report item does not exist * - Fixed USB_GetHIDReportItemInfo() function modifying the given report item's data when the report item does not exist

@ -19,6 +19,8 @@
* project makefiles using the USB driver of LUFA, or the makefile should be updated to use the new module source variables. * project makefiles using the USB driver of LUFA, or the makefile should be updated to use the new module source variables.
* - The EVENT_USB_InitFailure() event has been removed, as the USB_Init() function will no longer fail; if not USB mode is * - The EVENT_USB_InitFailure() event has been removed, as the USB_Init() function will no longer fail; if not USB mode is
* specified, the controller will default to UID selection mode. * specified, the controller will default to UID selection mode.
* - The USB mode specifier constants have been moved into a new enum and renamed. Existing projects should use the equivalent
* value in the new \ref USB_Modes_t enum.
* *
* <b>Device Mode</b> * <b>Device Mode</b>
* - Endpoints MUST be allocated in ascending order to ensure that bank corruption does not occur. Ensure that your user application * - Endpoints MUST be allocated in ascending order to ensure that bank corruption does not occur. Ensure that your user application

Loading…
Cancel
Save