⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usb.h

📁 usbn9603的驱动程序
💻 H
字号:
/*----------------------------------------------------------------------------
 *  Copyright (c) 2001 by National Semiconductor Corporation
 *  National Semiconductor Corporation
 *  2900 Semiconductor Drive
 *  Santa Clara, California 95051
 *
 *  All rights reserved
 *
 *<<<-------------------------------------------------------------------------
 * File Contents:
 *	usb.c - USB related definitions
 *
 *  Project: UJA firmware
 *  Author : Lev Solar
 *  Date   : Jan 2001
 *----------------------------------------------------------------------->>>*/
#ifndef __usb_h__
#define __usb_h__

/*=================================================================*
 *					Definitions		 							   *
 *=================================================================*/

#define CLKDIV   5 
#define CLKDIV_SLOW   12
#define CLKDIV_FAST   5

#define USB_SPEC_VERSION 0x0110 /* 1.1 */
#define NSC_ID_VENDOR  0x0400	/* assigned by USB Forum */
#define NSC_ID_PRODUCT 0x0803	/* assigned by NSC */
#define NSC_BCDDEVICE 1			/* assigned by developer */

#define MANUFACTURER_STR	"N\0a\0t\0i\0o\0n\0a\0l\0 \0S\0e\0m\0i\0c\0o\0n\0d\0u\0c\0t\0o\0r"
#define PRODUCT_STR			"U\0S\0B\0 \0D\0e\0m\0o\0 \0D\0e\0v\0i\0c\0e" 
#define VERSION_STR			"1\0.\00" 

void USBN9604_init(void);

byte USB_Get_Data (int Length, BYTE Buffer[]);
__inline__ void USB_bulk_fast_read(byte data_size, byte* data_ptr);
__inline__ int USB_bulk_read_compare(byte data_size, byte data);
void USB_Send_Data (int Length, BYTE Buffer[]);
__inline__ void USB_bulk_write_data (int data_size, byte data);
void USB_Send_Interrupt (BYTE Pid);

/*read USB node data or address register*/
static __inline__ byte read_usb(byte adr)
{
	USBADDR = adr;
	return (USBDATA);
}
/*write to the USB node*/
static __inline__ void write_usb(byte adr,byte dta)
{
	
	USBADDR = adr;
	USBDATA = dta;
}

static __inline__ void USB_int_enable(void)
{
	ICU_UNMASK_INT(USB_NODE_INT);
}

static __inline__ void USB_int_disable(void)
{
	ICU_mask_int(USB_NODE_INT);
}


/*--------------------------------------------------------------------
 *
 * 					UJA Endpoints/Interface Descriptors
 *
 *--------------------------------------------------------------------*/

typedef struct {
    byte	bLength;
    byte	bDescriptorType;
    struct {
	byte address:4;
	byte reserved:3;
	byte direction:1;
    } bEndpointAddress;
    byte	bmAttributes;
    two_bytes	wMaxPacketSize;
    byte	bInterval;
} USB_endpoint_desc_t;

typedef struct {
    byte	bLength;
    byte	bDescriptorType;
    byte	bInterfaceNumber;
    byte	bAlternateSetting;
    byte	bNumEndpoints;
    byte	bInterfaceClass;
    byte	bInterfaceSubClass;
    byte	bInterfaceProtocol;
    byte	iInterface;
} USB_interface_desc_t;


#define MAX_NUM_OF_ENDPOINTS	6
#define CURR_NUM_OF_ENDPOINTS	3 /*current number of endpoints excluding endpoint zero*/

#define CONFIG_DESC_LENGTH		0x9
#define INTERFACE_DESC_LENGTH	0x9
#define TOTAL_CONF_DESC_LEN CONFIG_DESC_LENGTH+INTERFACE_DESC_LENGTH+sizeof(USB_endpoint_desc_t)*CURR_NUM_OF_ENDPOINTS

typedef struct {
    byte	bLength;
    byte	bDescriptorType;
    char	*bstring;
} USB_string_desc_t;
/* The length of a string descriptor is size of a string and 
 * two bytes: bLength, bDescriptorType */
#define STRING_DESC_LENGTH		2*sizeof(byte)

typedef struct {
    union {
	struct {
	    byte selfpowered:1;
	    byte wakeup:1;
	    byte:6;
	} device;
	struct {
	    byte value;
	} interface;
	struct {
	    byte stalled:1;
	    byte:7;
	} endpoint;
	byte as_byte;
    } msb;
    byte lsb;
} USB_device_status_t;

/* The language ID (LANGID) defined by Microsoft as described
 * in Developing Intrnational Software for Windows 95/NT.
 * The LANGID is a 16-bit value that is the combination of a
 * primary and sublanguage ID. The bits are:
 *	0-9   Primary language ID
 *	10-15 Sublanguage ID
 * The enum is defined in little-endian order (the first byte is lsb)
 */
typedef enum {
    NEUTRAL		= 0x0000,
    SYTEM_DEFAULT	= 0x0400,
    ENGLISH_NEUTRAL	= 0x0009,
    ENGLISH_US		= 0x0409,
    ENGLISH_UK		= 0x0809,
    RUSSIAN_NEUTRAL	= 0x0019
} LANGID_t;

#define LANGID_LENGTH	2


//===============================================================

typedef enum {
    DEV_DETACHED = 0,
    DEV_ATTACH_WAITING,
    DEV_ATTACHED,
    DEV_ADDRESS,
    DEV_CONFIGURED
} USB_device_state_t;

typedef enum {
    GET_STATUS = 0,
    CLEAR_FEATURE,
    RESERVED_REQ,
    SET_FEATURE = 3,
    SET_ADDRESS = 5,
    GET_DESCRIPTOR,
    SET_DESCRIPTOR,
    GET_CONFIG,
    SET_CONFIG,
    GET_INTERFACE,
    SET_INTERFACE,
    SYNC_FRAME
} USB_device_Request_t;


typedef struct {
    USB_device_state_t state;
    USB_device_Request_t last_req;
} DEVICE_status_t;


#define MAX_STRING_LENGTH  0xFF
typedef union {
	USB_device_status_t	status;		/* used in GET_STATUS request */
    byte		state;		/* used in GET_CONFIGURATION request */
    byte		zero_data;		/*used while data sending */ //in GET_DESCRIPTOR request 
	byte		desc;		
} Device_buffers_t;

typedef enum {
	VENDOR_UJA_ID
} VENDOR_Request_t;


typedef enum {
    STANDARD_REQ = 0,
    CLASS_REQ = 1,
    VENDOR_REQ = 2
} USB_request_type_t;

typedef enum {
    DEVICE_REQ = 0,
    INTERFACE_REQ,
    ENDPOINT_REQ,
    OTHER_REQ
} USB_request_recipient_t;

typedef enum {
    ENDPOINT_STALL = 	0,
    DEVICE_REMOTE_WAKEUP = 1
} USB_feature_selector_t;

typedef enum {
    OUT = 0,
    IN
} USB_ep_direction_t;

typedef struct {
    byte recipient:5;		/* Request Recipient */
    byte type:2;		/* Request Type */
    byte xfer:1;		/* Data xfer direction */
} bmRequestType_t;

typedef enum {
    DEVICE_DESCRIPTOR 	= 1,
    CONFIG_DESCRIPTOR,
    STRING_DESCRIPTOR,
    INTERFACE_DESCRIPTOR,
    ENDPOINT_DESCRIPTOR,
    UJA_DEVICE_DESCRIPTOR	= 0x21
} USB_descriptor_type_t;

typedef struct {
    bmRequestType_t bmRequestType;
    union {
	byte Device_req;
	byte Vendor_req; 
    } bRequest;
    union {
	struct {
	    byte bDescriptorIndex; //bStringIndex;
	    byte bDescriptorType;
	} descriptor;
	struct {
	    byte bSelector;
	    byte msb;
	} feature;
	struct {
	    byte lsb;
	    byte msb;
	} as_bytes;
	two_bytes lsw;
    } wValue;
    union {
	struct {
	    byte ep_num:4;
	    byte:3;
	    byte direction:1;
	    byte:8;
	} endpoint;
	struct {
	    byte inf_no;
	    byte msb;
	} interface;
	struct {
	    byte lsb;
	    byte msb;
	} as_bytes;
	two_bytes msw;
    } wIndex;
    two_bytes	wLength;
} USB_request_t;

#define REQ_DIRECTION(req)	((USB_xfer_direction_t)(req->bmRequestType.xfer))
#define REQ_TYPE(req)		((USB_request_type_t)(req->bmRequestType.type))
#define REQ_RECIPIENT(req)	((USB_request_recipient_t)(req->bmRequestType.recipient))
#define REQ_DEVICE(req)		((USB_device_Request_t)(req->bRequest.Device_req & 0x0F))
#define REQ_VENDOR_TYPE(req)((req->bRequest.Vendor_req & 0xF0)>>4)
#define REQ_VENDOR(req)		(req->bRequest.Vendor_req & 0xFF)
#define REQ_VALUE(req)		(req->wValue)
#define REQ_INDEX(req)		(req->wIndex)
#define REQ_LENGTH(req)		(req->wLength)




typedef enum {
    CONTROL_ENDPOINT 	= 0,
    ISOCHRONOUS_ENDPOINT,
    BULK_ENDPOINT,
    INTERRUPT_ENDPOINT
} Endpoint_type_t;

typedef enum {
    CLASS_NOT_DEFINED 	= 0x0,
    CLASS_AUDIO		= 0x01,
    CLASS_COMMUNICATION = 0x02,
    CLASS_HID 		= 0x03,
    CLASS_MONITOR 	= 0x04,
    CLASS_PRINTING 	= 0x07,
    CLASS_MASS_STORAGE 	= 0x08,
    CLASS_HUB	 	= 0x09,
    CLASS_VENDOR	= 0xFF
} Device_class_t;

typedef enum {
    BUS_POWERED		= 0x80,
    SELF_POWERED	= 0x40,
    REMOTE_WAKEUP	= 0x20
} Power_config_t;

typedef enum {
    STR_LANGID = 0,
    STR_MANUFACTURER = 1,
    STR_PRODUCT,
    STR_VERSION,
    STR_LAST_INDEX
} String_index_t;

typedef struct {
    byte	bLength;
    byte	bDescriptorType;
    two_bytes	bcdUSB;
    byte	bDeviceClass;
    byte	bDeviceSubClass;
    byte	bDeviceProtocol;
    byte	bMaxPacketSize;
    two_bytes	idVendor;
    two_bytes	idProduct;
    two_bytes	bcdDevice;
    byte	iManufacturer;
    byte	iProduct;
    byte	iSerialNumber;
    byte	bNumConfigs;
} USB_device_desc_t;

typedef struct {
    byte	bLength;
    byte	bDescriptorType;
    two_bytes	wTotalLength;
    byte	bNumInterfaces;
    byte	bConfigurationValue;
    byte	iConfiguration;
    byte	bmAttributes;
    byte	MaxPower;
} USB_config_desc_t;

typedef struct {
    USB_config_desc_t		uja_dev_config_desc;
	USB_interface_desc_t	uja_interface_desc;
	USB_endpoint_desc_t		uja_dev_endpoint1_desc;
	USB_endpoint_desc_t		uja_dev_endpoint2_desc;
	USB_endpoint_desc_t		uja_dev_endpoint5_desc;
} USB_long_config_desc_t;



/*=====================================================================
 * 					Vendor UJA Device Descriptor
 *=====================================================================*/

typedef struct {
    byte	bLength;
	byte	bDescriptorType;
	dword	bDeviceIdValue;
}UJA_vendor_device_desc_t;


typedef enum {
    SOFTWARE_UJA_ID,
    HARDWARE_UJA_ID,
    CURRENT_UJA_ID
} UJA_device_id_type_t;


/* Global struct for UJA ID */
typedef struct {
	byte UJA_Device_ID_Value;
	byte UJA_Device_ID_Source;
}UJA_Device_ID_t;


typedef void (*USB_req_handler_t)(USB_request_t *);

/*=================================================================*
 *					Externals		 							   *
 *=================================================================*/

/* Reserved requests handler */
extern void USB_req_reserved(USB_request_t *req);
extern void USB_req_undefined(USB_request_t *req);


/*=================================================================*
 *					Prototypes		 							   *
 *=================================================================*/


void usb_dev_get_status(USB_request_t *req);
void usb_dev_clear_feature(USB_request_t *req);
void usb_dev_set_feature(USB_request_t *req);
void usb_dev_set_address(USB_request_t *req);
void usb_std_dev_get_descriptor(USB_request_t *req);
void usb_std_dev_set_descriptor(USB_request_t *req);
void usb_dev_get_config(USB_request_t *req);
void usb_dev_set_config(USB_request_t *req);
void usb_dev_get_interface(USB_request_t *req);
void usb_dev_set_interface(USB_request_t *req);
void usb_dev_sync_frame(USB_request_t *req);

void usb_vendor_dev_get_descriptor(USB_request_t *req);
void usb_vendor_dev_set_descriptor(USB_request_t *req);

byte get_deep_switch_settings();
void set_uja_device_id_value();



#endif __usb_h__

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -