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

📄 insusb.h

📁 单片机和D12通讯的C程序.实现了单片机通过USB口和电脑通讯.
💻 H
字号:
//
//******************************************************************************************
//					Interface Studio PDIUSBD12 USB1.1 Develop Board
//										C51 Demo					
//	
//									  ALL RIGHTS RESERVED
//******************************************************************************************
//	File name: InsUsb.h
//	Description:	This header file define hardware, constant, struct,and macro.
//				Caution:If user	want to modify them,be careful! Incorrect modification
//				will cause this firmware can not run as what you expect.
//	Version: 	3.0
//  Author:		ShuXian Feng
//	Date:		Aug 5,2004
//******************************************************************************************
//

#ifndef __INSUSB_H__
#define __INSUSB_H__


#include <reg51f.h>
#include <absacc.h>
#include <stdio.h>
                    //调试输出选择

#define _Debug_		//if define,printf statement will be compiled(some printf statement need
					//define _INT_MODE_ also)
#define _INT_MODE_	//if define, firmware will use interrupt mode to operate PDIUSBD12
					//else use inquiry mode


//
//******************************************************************************************
//		Interface Studio PDIUSBD12 USB1.1 Develop Board	hardware define
#define MCU_INNER_RAM		 	XBYTE[0X0000] 		//P89C51RD2 has 768 bytes interal RAM
#define D12_COMMAND_ADDRESS 	        XBYTE[0X1F03] 		//PDIUSBD12 command address		
#define D12_DATA_ADDRESS		XBYTE[0X1F02]		//PDIUSBD12 data address		
#define LED_ADDRESS		        XBYTE[0X5FFF]		//address of 8 led
#define SWITCH_ADDRESS			XBYTE[0X3FFF]		//address of switch 

sbit 	D12_RESET   = P1^1;
sbit 	D12_SUSPEND	= P1^2;
sbit 	WP 			= P1^5;
sbit 	SCL 		= P1^6;
sbit 	SDA 		= P1^7;
sbit 	U_INT 		= P3^2;

#define BOOL	unsigned char


//
//******************************************************************************************
//constant define
//modified from USB100.H,which povided in MicroSoft DDK

#define USB_ENDPOINT_DIRECTION_MASK               0x80

//Descriptor Types,see USB specification 9.4,Table 9-5
#define USB_DEVICE_DESCRIPTOR_TYPE                0x01
#define USB_CONFIGURATION_DESCRIPTOR_TYPE         0x02
#define USB_STRING_DESCRIPTOR_TYPE                0x03
#define USB_INTERFACE_DESCRIPTOR_TYPE             0x04
#define USB_ENDPOINT_DESCRIPTOR_TYPE              0x05

#define USB_ENDPOINT_TYPE_CONTROL                 0x00
#define USB_ENDPOINT_TYPE_ISOCHRONOUS             0x01
#define USB_ENDPOINT_TYPE_BULK                    0x02
#define USB_ENDPOINT_TYPE_INTERRUPT               0x03

#define USB_REQUEST_GET_STATUS                    0x00
#define USB_REQUEST_CLEAR_FEATURE                 0x01
#define USB_REQUEST_SET_FEATURE                   0x03
#define USB_REQUEST_SET_ADDRESS                   0x05
#define USB_REQUEST_GET_DESCRIPTOR                0x06
#define USB_REQUEST_SET_DESCRIPTOR                0x07
#define USB_REQUEST_GET_CONFIGURATION             0x08
#define USB_REQUEST_SET_CONFIGURATION             0x09
#define USB_REQUEST_GET_INTERFACE                 0x0A
#define USB_REQUEST_SET_INTERFACE                 0x0B
#define USB_REQUEST_SYNC_FRAME                    0x0C


typedef struct {
    unsigned char 	bLength;
    unsigned char 	bDescriptorType;
    unsigned int 	bcdUSB;
    unsigned char 	bDeviceClass;
    unsigned char 	bDeviceSubClass;
    unsigned char 	bDeviceProtocol;
    unsigned char 	bMaxPacketSize0;
    unsigned int 	idVendor;
    unsigned int 	idProduct;
    unsigned int 	bcdDevice;
    unsigned char 	iManufacturer;
    unsigned char 	iProduct;
    unsigned char 	iSerialNumber;
    unsigned char 	bNumConfigurations;
} USB_DEVICE_DESCRIPTOR;

typedef struct {
    unsigned char 	bLength;
    unsigned char 	bDescriptorType;
    unsigned char 	bEndpointAddress;
    unsigned char 	bmAttributes;
    unsigned int 	wMaxPacketSize;
    unsigned char 	bInterval;
} USB_ENDPOINT_DESCRIPTOR;

typedef struct {
    unsigned char 	bLength;
    unsigned char 	bDescriptorType;
    unsigned int 	wTotalLength;
    unsigned char 	bNumInterfaces;
    unsigned char 	bConfigurationValue;
    unsigned char 	iConfiguration;
    unsigned char 	bmAttributes;
    unsigned char 	MaxPower;
} USB_CONFIGURATION_DESCRIPTOR;

typedef struct {
    unsigned char 	bLength;
    unsigned char 	bDescriptorType;
    unsigned char 	bInterfaceNumber;
    unsigned char 	bAlternateSetting;
    unsigned char 	bNumEndpoints;
    unsigned char 	bInterfaceClass;
    unsigned char 	bInterfaceSubClass;
    unsigned char 	bInterfaceProtocol;
    unsigned char 	iInterface;
} USB_INTERFACE_DESCRIPTOR;

#define USB_CONFIGURATION_DESCRIPTOR_LENGTH sizeof(USB_CONFIGURATION_DESCRIPTOR)+\
											sizeof(USB_INTERFACE_DESCRIPTOR)+\
										 	(sizeof(USB_ENDPOINT_DESCRIPTOR)<<2)

//
//******************************************************************************************
//
#define USB_REQUEST_BMREQUESTTYPE_TPYE_MASK			0x60
#define USB_REQUEST_BMREQUESTTYPE_TPYE_STANDARD		0x00
#define USB_REQUEST_BMREQUESTTYPE_TPYE_VENDOR		0x40
#define USB_SETUP_PACKET_LENGTH						0x08
#define USB_DEFAULT_DEVICE_ADDRESS					0x00


//PIDUSBD12 command code
//initialization command
#define D12_SET_ADDRESS_ENABLE_DEVICE					0xD0
#define D12_SET_ENDPOINT_ENABLE_DEVICE					0xD8
#define D12_SET_MODE_DEVICE								0xF3
#define D12_SET_DMA_DEVICE								0xFB
//data flow commands
#define D12_READ_INTERRUPT_REGISTER_DEVICE				0xF4
#define D12_SELECT_ENDPOINT_CONTROL_OUT					0x00
#define D12_SELECT_ENDPOINT_CONTROL_IN					0x01
#define D12_SELECT_ENDPOINT_ENDPOINT1_OUT				0x02
#define D12_SELECT_ENDPOINT_ENDPOINT1_IN				0x03
#define D12_SELECT_ENDPOINT_ENDPOINT2_OUT				0x04
#define D12_SELECT_ENDPOINT_ENDPOINT2_IN				0x05
#define D12_READ_LAST_TRANSACTION_STATUS_CONTROL_OUT	0x40
#define D12_READ_LAST_TRANSACTION_STATUS_CONTROL_IN		0x41
#define D12_READ_LAST_TRANSACTION_STATUS_ENDPOINT1_OUT	0x42
#define D12_READ_LAST_TRANSACTION_STATUS_ENDPOINT1_IN	0x43
#define D12_READ_LAST_TRANSACTION_STATUS_ENDPOINT2_OUT	0x44
#define D12_READ_LAST_TRANSACTION_STATUS_ENDPOINT2_IN	0x45
#define D12_READ_BUFFER_SELECTED_ENDPOINT				0xF0
#define D12_WRITE_BUFFER_SELECTED_ENDPOINT				0xF0
#define D12_SET_ENDPOINT_STATUS_CONTROL_OUT				0x40
#define D12_SET_ENDPOINT_STATUS_CONTROL_IN				0x41
#define D12_SET_ENDPOINT_STATUS_ENDPOINT1_OUT			0x42
#define D12_SET_ENDPOINT_STATUS_ENDPOINT1_IN			0x43
#define D12_SET_ENDPOINT_STATUS_ENDPOINT2_OUT			0x44
#define D12_SET_ENDPOINT_STATUS_ENDPOINT2_IN			0x45
#define D12_ACKNOWLEDGE_SETUP_SELECTED_ENDPOINT			0xF1
#define D12_CLEAR_BUFFER_SELECTED_ENDPOINT				0xF2
#define D12_VALIDATE_BUFFER_SELECTED_ENDPOINT			0xFA
//gernal command
#define D12_SEND_RESUME									0xF6
#define D12_READ_CURRENT_FRAME_NUMBER					0xF5

///////////////////////////////////////////////////////////////////////
#define FIRMWARE_VERSION	0x30		//BCD code,3.0 is 0x30

#define D12_EP0_MAX_PACKET_SIZE		16	//16 bytes
#define D12_EP1_MAX_PACKET_SIZE		8	//1  bytes
#define D12_EP2_MAX_PACKET_SIZE		64	//64 bytes

#define WORD_SWAP(word) ((((word) & 0xFF) << 8) | (((word) >> 8) & 0xFF))
#define CONTROL_ENDPOINT_STALL			D12_COMMAND_ADDRESS = D12_SET_ENDPOINT_STATUS_CONTROL_OUT;\
										D12_DATA_ADDRESS = D12_ENDPOINT_STALL;\
										D12_COMMAND_ADDRESS = D12_SET_ENDPOINT_STATUS_CONTROL_IN;\
										D12_DATA_ADDRESS = D12_ENDPOINT_STALL;
#define GENERIC_ENDPOINT_STALL(bEndpointIndex)		D12_COMMAND_ADDRESS = bEndpointIndex;\
													D12_DATA_ADDRESS = D12_ENDPOINT_STALL;

#define IDLE				0x00
#define TRANSMIT			0x55
#define RECEIVE				0xAA

#define FALSE				0x00
#define TRUE				0xFF

#define D12_INT_BUS_RESET		0x40
#define D12_INT_SUSPEND_CHANGE	0x80
#define D12_INT_ENDP0_IN		0x02
#define D12_INT_ENDP0_OUT		0x01
#define D12_INT_ENDP1_IN		0x08
#define D12_INT_ENDP1_OUT		0x04
#define D12_INT_ENDP2_IN		0x20
#define D12_INT_ENDP2_OUT		0x10

#define D12_ENDPOINT_STALL			0x01
#define D12_ENDPOINT_NO_STALL		0x00
//select endpoint command,optional read one byte,
//bit 0 express the endpoint buffer is full or empty
#define D12_BUFFER_FULL_EMPTY_MASK	0x01

#define D12_BUFFER_LENGTH_ERROR		0xFF
//bit 5 of read last transaction status 
#define D12_SETUP_PACKET_MASK		0x20




#define EP0_OUT_BUFFER_SIZE			128
#define EP0_IN_BUFFER_SIZE			8
#define EP1_OUT_BUFFER_SIZE			1
#define EP1_IN_BUFFER_SIZE			1
#define RAM_BUFFER_SIZE				512
//vendor defined request code
#define RAM_COMMAND_LOOPBACK		0xA0
#define RAM_COMMAND_READ			0xA1
#define RAM_COMMAND_WRITE			0xA2
#define GET_FIRMWARE_VERSION		0xB0
#define SET_DEVICE_SERIAL_NUMBER	0xB1
#define NOTIFY_APP_START			0xB2

////////////////////////////////////////////////////////////////////////////////////
//USB device request structure
typedef struct _USB_DEVICE_REQUEST
{
	unsigned char bmRequestType;
	unsigned char bRequest;
	unsigned int  wValue;
	unsigned int  wIndex;
	unsigned int  wLength;
} USB_DEVICE_REQUEST;

//this structure is used for Control Read/Write 
typedef struct _USB_SETUP_DEALWITH
{

	BOOL			bControlOutDataComplete;
	BOOL			bControOutCommandIsPending;
	unsigned int  	wRemaindLength;
	unsigned char 	*pDataSourceAddress;
} USB_SETUP_DEALWITH;

typedef struct _RAM_CONTROL
{
	unsigned char  	bRamCommand;		//bRamCommand: 	0xA0: loopback test
										//				0xA1: Ram read
										//				0xA2: Ram write
	unsigned char	bRamRwStatus;		//bRamRwStatus:	IDLE or TRANSMIT or RECEIVE state
	unsigned int	iRamStartAddress;
	unsigned int	iRamRwLength;
	unsigned int	iRamRemaindLength;
} RAM_CONTROL;


//PDIUSBD12 Develop Board use this structure as global control
typedef struct _SYS_INFORMATION {
	BOOL			bRamError;
	BOOL			bTimer0;
	BOOL			bBusReset;
	BOOL		   	bSuspendChange;
	BOOL		   	bInSuspend;				//0x00:no suspend, 0x01:in suspend
	BOOL			bRemoteWakeUpEnable;	//0x00:disabled,0xff:enabled
	BOOL			bSetup;					//if 0x00,no Setup packet received,if 0x55,received a Setup packet
	unsigned char   bD12ConfigurationValue;	//value is 0x01 if D12 has already been configed,
											//otherwise ,it is 0x00,enpoint1/2 has not been enabled
	unsigned char   bUsbStatus;				//control transfer statemachine state: IDLE or TRANSMIT or RECEIVE
	USB_DEVICE_REQUEST	sUsbDeviceRequest;
	USB_SETUP_DEALWITH	sUsbSetUpDealwith;
	RAM_CONTROL		sRamControl;

} SYS_INFORMATION;


#endif

⌨️ 快捷键说明

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