📄 emu_usb_utilities.c
字号:
//-----------------------------------------------------------------------------
// F34x_USB_Utilities.c
//-----------------------------------------------------------------------------
// Copyright 2005 Silicon Laboratories, Inc.
// http://www.silabs.com
//
// Program Description:
//
// Source file for USB firmware. Includes the following support routines:
//
// - HaltEndpoint()
// - EnableEndpoint()
// - GetEpStatus()
// - SetConfiguration()
// - SetInterface()
//
//
// How To Test: See Readme.txt
//
//
// FID: 34X000014
// Target: C8051F34x
// Tool chain: Keil C51 7.50 / Keil EVAL C51
// Silicon Laboratories IDE version 2.6
// Command Line: See Readme.txt
// Project Name: F34x_USB_Bulk
//
//
// Release 1.3
// -All changes by GP
// -21 NOV 2005
// -Changed revision number to match project revision
// No content changes to this file
// -Modified file to fit new formatting guidelines
// -Changed file name from usb_utils.c
//
//
// Release 1.2
// -Initial Revision (JS)
// -XX AUG 2003
//
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include "c8051F340.h"
#include "emu_USB_Registers.h"
#include "emu_USB_Structs.h"
#include "emu_USB_Main.h"
#include "emu_USB_Descriptors.h"
#include "emu_USB_Request.h"
#include "common.h"
#include "interface.h"
//-----------------------------------------------------------------------------
// Extern Global Variables
//-----------------------------------------------------------------------------
extern struct DEVICE_STATUS gDeviceStatus;
extern code struct DESCRIPTORS gDescriptorMap;
extern struct DEVICE_STATUS gDeviceStatus;
extern struct EP_STATUS gEp0Status;
extern struct EP_STATUS gEp2OutStatus;
extern struct EP_STATUS gEp1InStatus;
//-----------------------------------------------------------------------------
// HaltEndpoint
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters :
// 1) UINT uEp
//
//-----------------------------------------------------------------------------
INT8 HaltEndpoint (INT16 uEp)
{
INT8 bReturnState, bIndex;
// Save current INDEX value and target selected endpoint
UREAD_DATA(INDEX, bIndex);
UWRITE_DATA (INDEX, (INT8)uEp & 0x00EF);
// Halt selected endpoint and update its status flag
switch (uEp)
{
case EP1_IN:
UWRITE_DATA (EINCSRL, rbInSDSTL);
gEp1InStatus.bEpState = EP_HALTED;
bReturnState = EP_IDLE; // Return success flag
break;
case EP2_OUT:
UWRITE_DATA (EOUTCSRL, rbOutSDSTL);
gEp2OutStatus.bEpState = EP_HALTED;
bReturnState = EP_IDLE; // Return success flag
break;
default:
bReturnState = EP_ERROR; // Return error flag
// if endpoint not found
break;
}
UWRITE_DATA (INDEX, bIndex); // Restore saved INDEX
return bReturnState;
}
//-----------------------------------------------------------------------------
// EnableEndpoint
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters :
// 1) UINT uEp
//
//-----------------------------------------------------------------------------
INT8 EnableEndpoint (INT16 uEp)
{
INT8 bReturnState, bIndex;
// Save current INDEX value and target selected endpoint
UREAD_DATA (INDEX, bIndex);
UWRITE_DATA (INDEX, (INT8)uEp & 0x00EF);
// Flag selected endpoint has HALTED
switch (uEp)
{
case EP1_IN:
// Disable STALL condition and clear the data toggle
UWRITE_DATA (EINCSRL, rbInCLRDT);
gEp1InStatus.bEpState = EP_IDLE; // Return success
bReturnState = EP_IDLE;
break;
case EP2_OUT:
// Disable STALL condition and clear the data toggle
UWRITE_DATA (EOUTCSRL, rbOutCLRDT);
gEp2OutStatus.bEpState = EP_IDLE;// Return success
bReturnState = EP_IDLE;
break;
default:
bReturnState = EP_ERROR; // Return error
// if no endpoint found
break;
}
UWRITE_DATA (INDEX, bIndex); // Restore INDEX
return bReturnState;
}
//-----------------------------------------------------------------------------
// GetEpStatus
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters :
// 1) UINT uEp
//
//-----------------------------------------------------------------------------
INT8 GetEpStatus (INT16 uEp)
{
INT8 bReturnState;
// Get selected endpoint status
switch (uEp)
{
case EP1_IN:
bReturnState = gEp1InStatus.bEpState;
break;
case EP2_OUT:
bReturnState = gEp2OutStatus.bEpState;
break;
default:
bReturnState = EP_ERROR;
break;
}
return bReturnState;
}
//-----------------------------------------------------------------------------
// SetConfiguration
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters :
// 1) BYTE SelectConfig
//
//-----------------------------------------------------------------------------
INT8 SetConfiguration(INT8 SelectConfig)
{
INT8 bReturnState = EP_IDLE; // Endpoint state return value
PIF_STATUS pIfStatus; // Pointer to interface status
// structure
// Store address of selected config desc
gDeviceStatus.pConfig = &gDescriptorMap.bCfg1;
// Confirm that this configuration descriptor matches the requested
// configuration value
if (gDeviceStatus.pConfig[cfg_bConfigurationValue] != SelectConfig)
{
bReturnState = EP_ERROR;
}
else
{
// Store number of interfaces for this configuration
gDeviceStatus.bNumInterf = gDeviceStatus.pConfig[cfg_bNumInterfaces];
// Store total number of interface descriptors for this configuration
gDeviceStatus.bTotalInterfDsc = MAX_IF;
// Get pointer to the interface status structure
pIfStatus = (PIF_STATUS)&gDeviceStatus.IfStatus[0];
// Build Interface status structure for Interface0
pIfStatus->bIfNumber = 0; // Set interface number
pIfStatus->bCurrentAlt = 0; // Select alternate number zero
pIfStatus->bNumAlts = 0; // No other alternates
SetInterface(pIfStatus); // Configure Interface0, Alternate0
gDeviceStatus.bDevState = DEV_CONFIG;// Set device state to configured
gDeviceStatus.bCurrentConfig = SelectConfig;// Store current config
}
return bReturnState;
}
//-----------------------------------------------------------------------------
// SetInterface
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters :
// 1) PIF_STATUS pIfStatus
//
//-----------------------------------------------------------------------------
INT8 SetInterface(PIF_STATUS pIfStatus)
{
INT8 bReturnState = EP_IDLE;
INT8 bIndex;
// Save current INDEX value
UREAD_DATA (INDEX, bIndex);
// Add actions for each possible interface alternate selections
switch(pIfStatus->bIfNumber)
{
// Configure endpoints for interface0
case 0:
// Configure Endpoint1 IN
UWRITE_DATA(INDEX, 1); // Index to Endpoint1 registers
// direction = IN ; Double-buffering enabled
UWRITE_DATA(EINCSRH, (rbInDIRSEL | rbInDBIEN));
gEp1InStatus.uNumBytes = 0; // Reset byte counter
gEp1InStatus.uMaxP = EP1_IN_MAXP;// Set maximum packet size
gEp1InStatus.bEp = EP1_IN; // Set endpoint address
gEp1InStatus.bEpState = EP_IDLE; // Set endpoint state
// Endpoint2 OUT
UWRITE_DATA(INDEX, 2); // Index to Endpoint2 registers
// Double-buffering enabled ; direction = OUT
UWRITE_DATA(EOUTCSRH, rbOutDBOEN);
gEp2OutStatus.uNumBytes = 0; // Reset byte counter
gEp2OutStatus.uMaxP = EP2_OUT_MAXP;// Set maximum packet size
gEp2OutStatus.bEp = EP2_OUT; // Set endpoint number
gEp2OutStatus.bEpState = EP_IDLE;// Set endpoint state
UWRITE_DATA(INDEX, 0); // Return to index 0
break;
// Configure endpoints for interface1
case 1:
// Configure endpoints for interface2
case 2:
// Default (error)
default:
bReturnState = EP_ERROR;
}
UWRITE_DATA (INDEX, bIndex); // Restore INDEX
return bReturnState;
}
//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -