我用CH375分别做master、slaver,成功对接,两片51的串口分别接PC的com口,可以通过com口收发数据,下面是我的程序,供大家参考 master.c /*************************************** ** USB 1.1 Host for CH375 ** ** 单片机用89C51 ** ****************************************/ /* 如果设备端不是CH37X,那么分析描述符 */ /* CH375中断为查询方式 */
#include #include #include #include #include "CH375INC.H"
#define DELAY_START_VALUE 1 /* 根据单片机的时钟选择延时初值 */ #define UNKNOWN_USB_DEVICE 0xF1 #define USB_INT_RET_NAK 0x2A /* 00101010B,返回NAK */
typedef struct _USB_DEVICE_DESCRIPTOR { unsigned char bLength; unsigned char bDescriptorType; unsigned short bcdUSB; unsigned char bDeviceClass; unsigned char bDeviceSubClass; unsigned char bDeviceProtocol; unsigned char bMaxPacketSize0; unsigned short idVendor; unsigned short idProduct; unsigned short bcdDevice; unsigned char iManufacturer; unsigned char iProduct; unsigned char iSerialNumber; unsigned char bNumConfigurations; } USB_DEV_DESCR, *PUSB_DEV_DESCR;
typedef struct _USB_CONFIG_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; } USB_CFG_DESCR, *PUSB_CFG_DESCR;
typedef struct _USB_INTERF_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; } USB_ITF_DESCR, *PUSB_ITF_DESCR;
typedef struct _USB_ENDPOINT_DESCRIPTOR { unsigned char bLength; unsigned char bDescriptorType; unsigned char bEndpointAddress; unsigned char bmAttributes; unsigned short wMaxPacketSize; unsigned char bInterval; } USB_ENDP_DESCR, *PUSB_ENDP_DESCR;
typedef struct _USB_CONFIG_DESCRIPTOR_LONG { USB_CFG_DESCR cfg_descr; USB_ITF_DESCR itf_descr; USB_ENDP_DESCR endp_descr[4]; } USB_CFG_DESCR_LONG, *PUSB_CFG_DESCR_LONG;
unsigned char RECV_LEN; /* 刚接收到的数据的长度 */ unsigned char idata RECV_BUFFER[ CH375_MAX_DATA_LEN ]; /* 数据缓冲区,用于保存接收到的下传数据,长度为0到64字节 */ unsigned char idata *cmd_buf; unsigned char idata *ret_buf;
#define p_dev_descr ((PUSB_DEV_DESCR)RECV_BUFFER) #define p_cfg_descr ((PUSB_CFG_DESCR_LONG)RECV_BUFFER)
unsigned char endp_out_addr; /* USB数据接收端点的端点地址 */ unsigned char endp_out_size; /* USB数据接收端点的端点尺寸 */ unsigned char endp_in_addr; /* USB状态发送端点的端点地址,为0则只支持单向接口 */ unsigned char endp6_mode, endp7_mode;
/* Ch375与51等的连接 CH375 51 D0 P1.0 . . . . . . D7 P1.7 A0 P3.7 RD P3.5 WR P3.4 CS 接地 INT P3.2(int0) */ #define CH375_DATA_PORT P1 /* CH375端口的I/O地址 */ sbit CH375_CMD_DAT = P3^7; /* CH375地址线输入A0,A0=1时写命令,A0=0时读写数据 */ sbit CH375_RD = P3^5; /* CH375读选通输入,低电平有效 */ sbit CH375_WR = P3^4; /* CH375写选通输入,低电平有效 */ sbit led = P3^3; /* 指示灯 */ sbit CH375_INT_WIRE = P3^2; /* CH375中断请求输出,低电平有效 */
void delay50ms(void); void delay1s(void); void flash_led(void); void CH375_WR_CMD_PORT( unsigned char cmd ); void CH375_WR_DAT_PORT( unsigned char dat ); unsigned char CH375_RD_DAT_PORT(void); unsigned char wait_interrupt(void); void set_usb_mode( unsigned char mode ); void toggle_recv(void); void toggle_send(void); unsigned char clr_stall6( void); unsigned char clr_stall7( void); unsigned char rd_usb_data( unsigned char *buf ); void wr_usb_data( unsigned char len, unsigned char *buf ); unsigned char issue_token( unsigned char endp_and_pid ); void host_send( unsigned char len, unsigned char *buf ); unsigned char host_recv( unsigned char *buf ); unsigned char get_descr( unsigned char type ); unsigned char set_addr( unsigned char addr ); unsigned char set_config( unsigned char cfg ); void CH375_Init( void ); unsigned char init_USB_device(void); void ComInit(void); void SendChar(unsigned char buff); //void SendChars( unsigned char *buff );
void delay2us(void) { unsigned char i; for ( i=DELAY_START_VALUE*2+1; i!=0; i-- ); }
void delay50ms(void) { unsigned char i, j; for( i=100; i!=0; i-- ) for( j=250; j!=0; j-- ); }
void delay1s(void) { unsigned char i,j,k;
for( i=10; i!=0; i-- ) for( j=200; j!=0; j-- ) for( k=250; k!=0; k-- ); }
void flash_led(void) { unsigned char i;
for( i=10;i>0;i-- ) { led=!led; delay1s(); } }
/* 与CH372/CH375有关的基本I/O操作 */ /* 向CH375的命令端口写入命令,周期不小于4uS,如果单片机较快则延时 */ void CH375_WR_CMD_PORT( unsigned char cmd ) { _nop_(); _nop_(); CH375_CMD_DAT = 1; /* 命令 */ CH375_DATA_PORT = cmd; CH375_RD = 1; CH375_WR = 0; _nop_(); _nop_(); CH375_WR = 1; CH375_CMD_DAT = 0; CH375_DATA_PORT = 0xFF; }
/* 向CH375的数据端口写入数据,周期不小于1.5uS,如果单片机较快则延时 */ void CH375_WR_DAT_PORT( unsigned char dat ) { _nop_(); CH375_CMD_DAT = 0; /* 数据 */ CH375_DATA_PORT = dat; CH375_RD = 1; CH375_WR = 0; _nop_(); CH375_WR = 1; CH375_DATA_PORT = 0xFF; }
/* 从CH375的数据端口读出数据,周期不小于1.5uS,如果单片机较快则延时 */ unsigned char CH375_RD_DAT_PORT(void) { unsigned char rev_data;
CH375_DATA_PORT = 0xFF; CH375_CMD_DAT = 0; /* 数据 */ CH375_WR = 1; CH375_RD = 0; _nop_(); rev_data = CH375_DATA_PORT; CH375_RD = 1;
return( rev_data ); }
/* 主机端等待操作完成, 返回操作状态 */ unsigned char wait_interrupt(void) { CH375_INT_WIRE = 1; while( CH375_INT_WIRE ) /* 查询等待CH375操作完成中断(INT#低电平) */ { if( RI==1 ) /* 串口接收到数据 */ { CH375_WR_CMD_PORT( CMD_ABORT_NAK ); /* 放弃当前操作 */ return( 0xEF ); } } CH375_WR_CMD_PORT( CMD_GET_STATUS ); /* 产生操作完成中断, 获取中断状态 */ return( CH375_RD_DAT_PORT() ); }
/* 设置CH37X的工作模式 */ void set_usb_mode( unsigned char mode ) { unsigned char i;
CH375_WR_CMD_PORT( CMD_SET_USB_MODE ); CH375_WR_DAT_PORT( mode ); endp6_mode=endp7_mode=0x80; /* 主机端复位USB数据同步标志 */
for( i=100; i!=0; i-- ) /* 等待设置模式操作完成,不超过30uS */ { if( CH375_RD_DAT_PORT() == CMD_RET_SUCCESS ) return; /* 成功 */ }
while(1) { flash_led(); /* CH375出错 */ } }
/************************************************************************************ 数据同步 USB的数据同步通过切换DATA0和DATA1实现: 在设备端, USB设备可以自动切换; 在主机端, 必须由SET_ENDP6和SET_ENDP7命令控制CH375切换DATA0与DATA1. 主机端的程序处理方法是为设备端的各个端点分别提供一个全局变量, 初始值均为DATA0, 每执行一次成功事务后取反, 每执行一次失败事务后将其复位为DATA1 *************************************************************************************/ void toggle_recv(void) /* 主机接收成功后,切换DATA0和DATA1实现数据同步 */ { CH375_WR_CMD_PORT( CMD_SET_ENDP6 ); CH375_WR_DAT_PORT( endp6_mode ); endp6_mode^=0x40; delay2us(); }
void toggle_send(void) /* 主机发送成功后,切换DATA0和DATA1实现数据同步 */ { CH375_WR_CMD_PORT( CMD_SET_ENDP7 ); CH375_WR_DAT_PORT( endp7_mode ); endp7_mode^=0x40; delay2us(); }
unsigned char clr_stall6(void) /* 主机接收失败后,复位设备端的数据同步到DATA0 */ { CH375_WR_CMD_PORT( CMD_CLR_STALL ); CH375_WR_DAT_PORT( 2 | 0x80 ); /* 如果设备端不是CH37X芯片,那么需要修改端点号 */ // CH375_WR_DAT_PORT( endp_out_addr | 0x80 ); /* 如果设备端不是CH37X芯片,那么需要修改端点号 */ endp6_mode=0x80; return( wait_interrupt() ); }
unsigned char clr_stall7(void) /* 主机发送失