uchar CH376FileOpen(uchar * name) /* 在根目录或者当前目录下打开文件或者目录(文件夹) */ { /* 设置将要操作的文件的文件名 */ uchar c; xWriteCH376Cmd( CMD10_SET_FILE_NAME ); c = *name; xWriteCH376Data(c); while (c) { name++; c = *name; if (c == DEF_SEPAR_CHAR1 || c == DEF_SEPAR_CHAR2) c = 0; /* 强行将文件名截止 */ xWriteCH376Data(c); } if (name[0] == DEF_SEPAR_CHAR1 || name[0] == DEF_SEPAR_CHAR2) CH376WriteVar32( VAR_CURRENT_CLUST, 0 ); return(CH376SendCmdWaitInt(CMD0H_FILE_OPEN)); }
/* 其它BulkOnly传输协议的命令可以参考这个例子处理,修改前必须了解USB MassStorage和SCSI规范 */ UINT8 IsDiskWriteProtect( void ) { UINT8 s, SysBaseInfo; P_BULK_ONLY_CBW pCbw; SysBaseInfo = CH376ReadVar8( VAR_SYS_BASE_INFO ); /* 当前系统的基本信息 */ pCbw = (P_BULK_ONLY_CBW)buf; for ( s = 0; s != sizeof( pCbw -> CBW_CB_Buf ); s ++ ) pCbw -> CBW_CB_Buf[ s ] = 0; /* 默认清0 */ pCbw -> CBW_DataLen0 = 0x10; /* 数据传输长度 */ pCbw -> CBW_Flag = 0x80; /* 传输方向为输入 */ if ( SysBaseInfo & 0x40 ) { /* SubClass-Code子类别非6 */ pCbw -> CBW_CB_Len = 10; /* 命令块的长度 */ pCbw -> CBW_CB_Buf[0] = 0x5A; /* 命令块首字节, MODE SENSE(10) */ pCbw -> CBW_CB_Buf[2] = 0x3F; pCbw -> CBW_CB_Buf[8] = 0x10; } else { /* SubClass-Code子类别为6 */ pCbw -> CBW_CB_Len = 6; /* 命令块的长度 */ pCbw -> CBW_CB_Buf[0] = 0x1A; /* 命令块首字节, MODE SENSE(6) */ pCbw -> CBW_CB_Buf[2] = 0x3F; pCbw -> CBW_CB_Buf[4] = 0x10; } CH376WriteHostBlock( (PUINT8)pCbw, sizeof( BULK_ONLY_CBW ) ); /* 向USB主机端点的发送缓冲区写入数据块,剩余部分CH376自动填补 */ s = CH376SendCmdWaitInt( CMD0H_DISK_BOC_CMD ); /* 对U盘执行BulkOnly传输协议 */ if ( s == USB_INT_SUCCESS ) { s = CH376ReadBlock( buf ); /* 从当前主机端点的接收缓冲区读取数据块,返回长度 */ if ( s > 3 ) { /* MODE SENSE命令返回数据的长度有效 */ if ( SysBaseInfo & 0x40 ) s = buf[3]; /* MODE SENSE(10), device specific parameter */ else s = buf[2]; /* MODE SENSE(6), device specific parameter */ if ( s & 0x80 ) return( 0xFF ); /* U盘写保护 */ else return( USB_INT_SUCCESS ); /* U盘没有写保护 */ } return( USB_INT_DISK_ERR ); } CH376DiskReqSense( ); /* 检查USB存储器错误 */ return( s ); }
uchar CH376DiskMount(void) /* 初始化磁盘并测试磁盘是否就绪 */ { return(CH376SendCmdWaitInt(CMD0H_DISK_MOUNT)); }
uchar CH376DiskConnect(void)/*检查U盘是否连接*/ { if (Query376Interrupt( )) CH376GetIntStatus( ); /* 检测到中断 */ return(CH376SendCmdWaitInt(CMD0H_DISK_CONNECT)); }