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

📄 main.c

📁 blackfin 的 usb devise 的代码 很基础
💻 C
📖 第 1 页 / 共 2 页
字号:
/************************************************************************** 
文件名称: main.c
作者: Andy
版本: 0.1 
创建时间 : 2007-11 
描述: 
	文件实现了USB枚举协议及测试枚举功能
修改历史: 
****************************************************************************/ 

#include <stdio.h>

#include "dm_bf5xx.h"
#include "isp1362.h"

#define ISP116x_BASE_ADDR (0x20330000)
#define HC_DATA_PORT (ISP116x_BASE_ADDR)
#define HC_CMD_PORT  (ISP116x_BASE_ADDR + 2)
#define DC_DATA_PORT (ISP116x_BASE_ADDR + 4)
#define DC_CMD_PORT  (ISP116x_BASE_ADDR + 6)

/**************************************************************
DC
**************************************************************/

#define DC_CHIP_ID 0x3630
//DC CMD define
#define DC_RD_CHIP_ID    0xb5
#define DC_RD_SCRATCH_ID 0xb3
#define DC_WR_SCRATCH_ID 0xb2
#define DC_RESET_DEVICE  0xf6
#define DC_CTRL_OUT_EP   0x20
#define DC_CTRL_IN_EP    0x21
#define DC_WR_CFG_EP1    0x22
#define DC_WR_CFG_EP2    0x23
#define DC_WR_CFG_EP3    0x24
#define DC_WR_CFG_EP4    0x25
#define DC_WR_CFG_EP5    0x26
#define DC_WR_CFG_EP6    0x27
#define DC_WR_CFG_EP7    0x28
#define DC_WR_CFG_EP8    0x29
#define DC_WR_CFG_EP9    0x2A
#define DC_WR_CFG_EPA    0x2B
#define DC_WR_CFG_EPB    0x2C
#define DC_WR_CFG_EPC    0x2D
#define DC_WR_CFG_EPD    0x2E
#define DC_WR_CFG_EPE    0x2F
#define DC_RD_ADDR_REG   0xb7
#define DC_WR_ADDR_REG   0xb6
#define DC_RD_MODE       0xb9
#define DC_WR_MODE       0xb8
#define DC_RD_HW_CFG     0xbb
#define DC_WR_HW_CFG     0xba
#define DC_RD_INTEN_REG  0xc3
#define DC_WR_INTEN_REG  0xc2
#define DC_RD_EP_BUF     0x10
#define DC_WR_EP_BUF     0x01
#define DC_RD_EP_STAT    0x50
#define DC_RD_EP_STAT_IMG 0xd0
#define DC_RD_INTT_REG   0xc0
#define DC_STALL_EP      0x40
#define DC_UNSTALL_EP    0x80
#define DC_VOLIDATE_EP   0x60
#define DC_CLEAR_EP      0x70
#define DC_ACK_SETUP     0xf4

/*定义标准的描述符类型,即bDescriptorType字段的值*/
enum {
	DEVICE_DESCRIPTOR = 0x1,
	CONFIGURATION_DESCRIPTOR,
	STRING_DESCRIPTOR,
	INTERFACE_DESCRIPTOR,
	ENDPOINT_DESCRIPTOR,
	POWER_DESCRIPTOR,
	OTG_DESCRIPTOR = 0x9
};
/*定义标准的usb请求命令,即usb分配的各种请求的编号,即bRequest字段的值*/
enum {
	GET_STATUS = 0x0,
	CLEAR_FEATURE,
	SET_FEATURE = 0x3,
	SET_ADDRESS = 0x5,
	GET_DESCRIPTOR,
	SET_DESCRIPTOR,
	GET_CONFIGURATION,
	SET_CONFIGURATION,
	GET_INTERFACE,
	SET_INTERFACE,
	SYNCH_FRAME
};
/*定义端点类型*/
enum {
	ENDPOINT_TYPE_CONTROL,
	ENDPOINT_TYPE_ISOCHRONOUS,
	ENDPOINT_TYPE_BULK,
	ENDPOINT_TYPE_INTERRUPT
};

#define NUM_INTERFACE 0x01
#define NUM_ENDPOINT  0x02

#pragma pack(1)
struct dev_descriptor {
	unsigned char  bLength;
	unsigned char  bDescriptorType;
	unsigned short bcdUSB;
	unsigned char  bDeviceClass;
	unsigned char  bDeviceSubClass;
	unsigned char  bDeviceProtocol;
	unsigned char  bMaxPacketize0;
	unsigned short idVendor;
	unsigned short idProduct;
	unsigned short bcdDevice;
	unsigned char  iManufacturer;
    unsigned char  iProduct;
    unsigned char  iSerialNumber;
    unsigned char  bNumConfigurations;
};

struct dev_qualifier_descriptor {
	unsigned char  bLength;
	unsigned char  bDescriptorType;
	unsigned short bcdUSB;
	unsigned char  bDeviceClass;
	unsigned char  bDeviceSubClass;
	unsigned char  bDeviceProtocol;
	unsigned char  bMaxPacketSize0;
	unsigned char  bNumConfigurations;
	unsigned char  bReserved;
};

struct string_descriptor {
	unsigned char bLength;
	unsigned char bDescriptorType;
	unsigned char SomeDescriptor[36];
};

struct endpoint_descriptor {
	unsigned char  bLength;
	unsigned char  bDescriptorType;
	unsigned char  bEndpointAddress;
	unsigned char  bmAttributes;
	unsigned short wMaxPacketSize;
	unsigned char  bInterval;
};

struct interface_descriptor {
	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;
	
	struct endpoint_descriptor endpoint[NUM_ENDPOINT];
};

struct configuration_descriptor {
	unsigned char  bLength;
	unsigned char  bDescriptorType;
	unsigned short wTotalLength;
	unsigned char  bNumInterfaces;
	unsigned char  bConfigurationValue;
	unsigned char  iConfiguration;
	unsigned char  bmAttributes;
	unsigned char  MaxPower;
	
	struct interface_descriptor interface[NUM_INTERFACE];
};
#pragma unpack()

struct device_request {
	unsigned char  bmRequestType;
	unsigned char  bRequest;
	unsigned short wValue;
	unsigned short wIndex;
	unsigned short wLength;
};

struct dev_descriptor device_descriptor = {
	sizeof(struct dev_descriptor),
	DEVICE_DESCRIPTOR,
	0x0200,
	0x00,
	0x00,
	0x00,
	0x40,
	0x0470,//vid
	0x0666,//pid
	0x0100,//v
	0x00,
	0x00,
	0x00,
	0x01,
};

struct dev_qualifier_descriptor device_qualifier_descriptor = {
	sizeof(struct dev_qualifier_descriptor),
	0x06,//Device Qualifier Type
	0x0110,
	0x00,
	0x00,
	0x00,
	0x40,
	0x01,
	0x00,
};

struct configuration_descriptor config_descriptor = {
	0x09,
	CONFIGURATION_DESCRIPTOR,
	0x09+0x9*NUM_INTERFACE+0x07*NUM_ENDPOINT*NUM_INTERFACE,
	NUM_INTERFACE,
	0x01,
	0x00,
	0xa0,
	0xfa,
	{
		0x09,
		INTERFACE_DESCRIPTOR,
		0x00,
		0x00,
		NUM_ENDPOINT,
		0x08,//MSD DEVICE
		0x06,//SCSI CMD SET
		0x50,//BULK-ONLY protocol
		0x00,
	
//		{
			0x07,
			ENDPOINT_DESCRIPTOR,
			0x82,//out endpoint
			0x02,
			0x0040,
			0x00,
//		}
//		{
			0x07,
			ENDPOINT_DESCRIPTOR,
			0x01,//in endpoint
			0x02,
			0x0040,
			0x00,
//		}
		
	},
		
};

struct string_descriptor product_string_descriptor = {
	sizeof(struct string_descriptor),
	STRING_DESCRIPTOR,
	's',
	0,
	't',
	0,
	'a',
	0,
	'r',
	0,
	't',
	0,
	'-',
	0,
	'k',
	0,
	'i',
	0,
	't',
	0,
};

// usb device controler driver
struct usb_dev {
	void *controler;
	struct device_request dev_request;
	void *priv_data;
	
	void (*set_address)(void *controler, int addr);
	void (*enable_address)(void *controler);
};

static void dc_set_address(struct isp1362 *isp1362, int addr)
{
	int tmp = 0;
	
	tmp = isp1362_read_reg16(isp1362, DC_RD_ADDR_REG);
	tmp &= 0x80;
	tmp |= addr;
	isp1362_write_reg16(isp1362, DC_WR_ADDR_REG, tmp);
}

static void dc_enable_address(struct isp1362 *isp1362)
{
	int tmp = 0;
	
	tmp = isp1362_read_reg16(isp1362, DC_RD_ADDR_REG);
	tmp |= 0x80;
	isp1362_write_reg16(isp1362, DC_WR_ADDR_REG, tmp);
}

static void dc_disable_address(struct isp1362 *isp1362)
{
	int tmp = 0;
	
	tmp = isp1362_read_reg16(isp1362, DC_RD_ADDR_REG);
	tmp &= ~0x80;
	isp1362_write_reg16(isp1362, DC_WR_ADDR_REG, tmp);
}

static void dc_soft_connect(struct isp1362 *isp1362)
{
	int tmp = 0;
	
	tmp = isp1362_read_reg16(isp1362, DC_RD_MODE);
	tmp |= 0x09;//enable all interrupt
	isp1362_write_reg16(isp1362, DC_WR_MODE, tmp);
}

static void dc_disconnect(struct isp1362 *isp1362)
{
	int tmp = 0;
	
	tmp = isp1362_read_reg16(isp1362, DC_RD_MODE);
	tmp &= ~0x01;
	isp1362_write_reg16(isp1362, DC_WR_MODE, tmp);
}

static void dc_set_clk_run(struct isp1362 *isp1362)
{
	int tmp = 0;
	
	tmp = isp1362_read_reg16(isp1362, DC_RD_HW_CFG);
	tmp |= 0x1002;//config interrupt,falling edge 
	isp1362_write_reg16(isp1362, DC_WR_HW_CFG, tmp);
}

static void dc_set_endpoint(struct isp1362 *isp1362, int epcfg, int epindex)
{
	isp1362_write_reg16(isp1362, epcfg, epindex);
}

static int dc_read_ep_status(struct isp1362 *isp1362, int epindex)
{
	return isp1362_read_reg16(isp1362, DC_RD_EP_STAT|epindex);
}

static int dc_read_ep_status_img(struct isp1362 *isp1362, int epindex)
{
	return isp1362_read_reg16(isp1362, DC_RD_EP_STAT_IMG|epindex);
}

static void dc_stall_ep(struct isp1362 *isp1362, int epindex)
{
	if ( (epindex < 0x0) && (epindex > 0xf) )
		return;
		
	isp1362_write_addr(isp1362, DC_STALL_EP|epindex);	
}

static void dc_unstall_ep(struct isp1362 *isp1362, int epindex)
{
	if ( (epindex < 0x0) && (epindex > 0xf) )
		return;
		
	isp1362_write_addr(isp1362, DC_UNSTALL_EP|epindex);
}

static void dc_validate_ep_buf(struct isp1362 *isp1362, int epindex)
{
	if ( (epindex < 0x1) && (epindex > 0xf) )
		return;
		
	isp1362_write_addr(isp1362, DC_VOLIDATE_EP|epindex);
}

static void dc_clear_ep_buf(struct isp1362 *isp1362, int epindex)
{
	if ( (epindex < 0x0) && (epindex > 0xf) )
		return;
	
	if (epindex == 0x1)
		return;
				
	isp1362_write_addr(isp1362, DC_CLEAR_EP|epindex);
}

static void dc_ack_setup(struct isp1362 *isp1362)
{
	isp1362_write_addr(isp1362, DC_ACK_SETUP);
}

static int dc_read_intt_status(struct isp1362 *isp1362)
{
	return isp1362_read_reg16(isp1362, DC_RD_INTT_REG);
}

static void dc_device_reset(struct isp1362 *isp1362)
{
	isp1362_write_addr(isp1362, DC_RESET_DEVICE);
}

static int dc_read_ep(struct isp1362 *isp1362, char *buf, int len, int epindex)
{
	int i = 0;
	int j = 0;
	int ep = 0;
	short *ptr = (short *)buf;
	
	if (epindex != 0)
		ep = epindex + 1;

⌨️ 快捷键说明

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