Esempio n. 1
0
void Sec_CREAT_CON_DIR(const char * BUF_Name)
{	
	bus_SPI_mutex_apply();
	strcpy( (char *)TarName,  BUF_Name );
	s=CH376DirCreate( TarName ); /* 在根目录下新建目录(文件夹)并打开,如果目录已经存在那么直接打开 */
	CH376FileClose( TRUE ); /* 关闭文件,对于字节读写建议自动更新文件长度 */
	memset( TarName, 0, 100 );
	bus_SPI_mutex_release();	
}
Esempio n. 2
0
//创建多级目录下的文件并写
void Byte_CREAT_WRITE_PATH(const char * BUF_Name, uint8_t * Content , uint32_t offset )
{	//													文件名                文件内容	
	bus_SPI_mutex_apply();
	strcpy( (char *)TarName,  BUF_Name ); /* 目标文件名 */
	s = CH376FileCreatePath( TarName );  	/* 新建多级目录下的文件,支持多级目录路径,输入缓冲区必须在RAM中 */
	s = CH376ByteWrite( Content , offset, NULL ); /* 以字节为单位向当前位置写入数据块 */
	s = CH376FileClose( TRUE );   /* 关闭文件,对于字节读写建议自动更新文件长度 */
	memset( TarName, 0, 100 );
	memset( buf, 0, 100 );
	bus_SPI_mutex_release();	
}
Esempio n. 3
0
//创建根目录下的文件并写
void Byte_CREAT_WRITE_DIR(const char * BUF_Name, uint8_t * Content, uint32_t offset )
{
	bus_SPI_mutex_apply();
	strcpy( (char *)TarName,  BUF_Name );/* 打开多级目录下的文件或者目录(文件夹),支持多级目录路径,支持路径分隔符,路径长度不超过255个字符 */
	CH376FileCreate( TarName );
	CH376ByteWrite( Content, offset, NULL );  /* 以字节为单位向当前位置写入数据块 */
	CH376FileClose( TRUE ); /* 关闭文件,对于字节读写建议自动更新文件长度 */
	memset( TarName, 0, 100 );
	memset( buf, 0, 100 );
	bus_SPI_mutex_release();	
}
Esempio n. 4
0
void Sec_CREAT_WRITE_PATH(const char * BUF_Name, uint8_t * Content , uint8_t offset )
{	//													文件名                文件内容               文件大小
	bus_SPI_mutex_apply();
	strcpy( (char *)TarName,  BUF_Name ); /* 目标文件名 */
	s = CH376FileCreatePath( TarName );  	/* 新建多级目录下的文件,支持多级目录路径,输入缓冲区必须在RAM中 */
// 	CH376SecLocate( 0 );
	s = CH376SecWrite( Content , offset, NULL ); /* 以扇区为单位向当前位置写入数据块 */
	s = CH376FileClose( TRUE );   /* 关闭文件,对于扇区读写建议自动更新文件长度 */
	memset( TarName, 0, 100 );
	bus_SPI_mutex_release();		
}
Esempio n. 5
0
void  Sec_OPEN_WRITE_DIR (const char * BUF_Name, uint8_t * Content , uint32_t Addr, uint8_t offset  )
{	
	bus_SPI_mutex_apply();
	strcpy( (char *)TarName,  BUF_Name );
	CH376FileOpen( TarName );  /* 在根目录或者当前目录下打开文件或者目录(文件夹) */
	CH376SecLocate( Addr );	
	CH376SecWrite( Content , offset, NULL );  /* 以扇区为单位向当前位置写入数据块 */
	CH376FileClose( TRUE ); /* 关闭文件,对于扇区读写建议自动更新文件长度 */
	memset( TarName, 0, 100 );
	bus_SPI_mutex_release();	
}
Esempio n. 6
0
uint8_t  Sec_OPEN_READ_PATH (const char * BUF_Name , uint32_t Addr , uint8_t * bufread , uint8_t offset )
{
	bus_SPI_mutex_apply();
	strcpy( (char *)TarName,  BUF_Name );
	CH376FileOpenPath( TarName );/* 打开多级目录下的文件或者目录(文件夹),支持多级目录路径,支持路径分隔符,路径长度不超过255个字符 */
	//bufoffset =	CH376GetFileSize( );
	CH376SecLocate( Addr );
	s = CH376SecRead(  bufread , offset ,NULL );/* 以扇区为单位从当前位置读取数据块 */
	CH376FileClose( TRUE ); /* 关闭文件,对于扇区读写建议自动更新文件长度 */
	bus_SPI_mutex_release();	
	return s;
}
Esempio n. 7
0
void  Sec_OPEN_READ_DIR (const char * BUF_Name , uint32_t Addr, uint8_t * bufread , uint8_t offset  )
{
	bus_SPI_mutex_apply();
	strcpy( (char *)TarName,  BUF_Name );
	CH376FileOpen( TarName );  /* 在根目录或者当前目录下打开文件或者目录(文件夹) */
	CH376SecLocate( Addr );
//	bufoffset =	CH376GetFileSize( );
	CH376SecRead( bufread, offset , NULL );/* 以扇区为单位从当前位置读取数据块 */
	CH376FileClose( TRUE ); /* 关闭文件,对于扇区读写建议自动更新文件长度 */
	memset( TarName, 0, 100 );
	bus_SPI_mutex_release();	
}
Esempio n. 8
0
//打开根目录下的文件并写
void  Byte_OPEN_WRITE_DIR (const char * BUF_Name, uint8_t * Content , uint32_t Addr, uint32_t offset  )
{
	bus_SPI_mutex_apply();
	strcpy( (char *)TarName,  BUF_Name );
	CH376FileOpen( TarName );  /* 在根目录或者当前目录下打开文件或者目录(文件夹) */
//	bufoffset =	CH376GetFileSize( );
	CH376ByteLocate( Addr );
	CH376ByteWrite( Content , offset, NULL );  /* 以字节为单位向当前位置写入数据块 */
	CH376FileClose( TRUE ); /* 关闭文件,对于字节读写建议自动更新文件长度 */
	memset( TarName, 0, 100 );
	memset( buf, 0, 100 );
	bus_SPI_mutex_release();	
}
Esempio n. 9
0
//打开多级目录下的文件并写
uint8_t Sec_OPEN_WRITE_PATH (const char * BUF_Name, uint8_t * Content , uint32_t Addr ,uint8_t offset)
{
	bus_SPI_mutex_apply();
	strcpy( (char *)TarName,  BUF_Name );
	CH376FileOpenPath( TarName );/* 打开多级目录下的文件或者目录(文件夹),支持多级目录路径,支持路径分隔符,路径长度不超过255个字符 */
//	bufoffset =	CH376GetFileSize( );
	CH376SecLocate( Addr );
	s = CH376SecWrite(Content, offset, NULL ); /* 以扇区为单位向当前位置写入数据块 */
	CH376FileClose( TRUE ); /* 关闭文件,对于扇区读写建议自动更新文件长度 */
	memset( TarName, 0, 100 );
	memset( USBbuf, 0, 100 );
	bus_SPI_mutex_release();	
	return s;
}
Esempio n. 10
0
//以扇区为单位填充
BOOL SecFill( const char * BUF_Name , uint32_t Addr , uint8_t size)
{										//		文件名称 			写起始地址					要写的数量	
	bus_SPI_mutex_apply();
	strcpy( (char *)TarName,  BUF_Name );
	CH376FileOpenPath( TarName );/* 打开多级目录下的文件或者目录(文件夹),支持多级目录路径,支持路径分隔符,路径长度不超过255个字符 */
	CH376ByteLocate( Addr );	
	memset( buf, 0xaa, 100 );
	s = CH376ByteWrite( buf, size, NULL ); /* 以字节为单位向当前位置写入数据块 */
	CH376FileClose( TRUE ); /* 关闭文件,对于字节读写建议自动更新文件长度 */
	memset( TarName, 0, 100 );
	memset( buf, 0, 100 );
	bus_SPI_mutex_release();
	if( s == USB_INT_SUCCESS )
		return TRUE;  
	else 
	if(s == USB_INT_BUF_OVER)
	{
		beep();
		return FALSE;
	}
	else
		return FALSE; 	
}
Esempio n. 11
0
int main( void )
{
    int i;
    int j;
    UINT8	res;
    UINT8	s;
    UINT8	NameBuf[20] = {0};
    UINT8       CountT = 0;
    UINT32      U_D;
    PUINT32     pU_D;
    pU_D = &U_D;
    // Stop watchdog timer to prevent time out reset
    WDTCTL = WDTPW + WDTHOLD;
    P1DIR_bit.P1DIR0 = 1;
    P1OUT_bit.P1OUT0 = 1;

    UartInit();
code_start:
    CountT ++;
    for(i = 0;i<30000;i++);
    printk("Code Start !\r\n");
    printk("程序开始 !\r\n");
    
    do{
        for(i = 0;i<30000;i++);
        res = mInitCH376Host();
    }while(res != USB_INT_SUCCESS);
    printk("USB_INT_SUCCESS !\r\n");
    
    do{
        for(i = 0;i<30000;i++);
        res = CH376DiskConnect( ); /* 查询U盘是否连接,返回USB_INT_SUCCESS则说明当前已连接 */ 
    }while(res!=USB_INT_SUCCESS);
    printk("  Connect SUCCESS\r\n");
    
    do{
        for(i = 0;i<30000;i++);
        res = CH376DiskMount( ); /* 查询U盘或SD卡是否准备好,有些U盘可能需多次调用才能成功 */ 
    }while(res!=USB_INT_SUCCESS);
    printk("  U is OK !\r\n");

    s = CH376DiskCapacity(pU_D);
    mStopIfError( s );
    printk("  U盘大小:");
    printHex((unsigned int)U_D);
    printHex((unsigned int)(U_D>>16));
    printk("\r\n");
    
    s = CH376DiskQuery(pU_D);
    mStopIfError( s );
    printk("  剩余大小:");
    printHex((unsigned int)U_D);
    printHex((unsigned int)(U_D>>16));
    printk("\r\n");
    strcpy( NameBuf, "\\2012.TXT" );  /* 目标文件名 */
    
#if 0
    
    printf( "Open\r\n" );
    s = CH376FileOpenPath( NameBuf );
    mStopIfError( s );
    if ( s == ERR_MISS_FILE ){
        printf( "Create\r\n" );
        s = CH376FileCreatePath( NameBuf );  /* 新建多级目录下的文件,支持多级目录路径,输入缓冲区必须在RAM中 */
        mStopIfError( s );
    }
    
    printf( "CH376ByteLocate\r\n" );
    s = CH376ByteLocate(0xFFFFFFFF);
    mStopIfError( s );
    
    printf( "Write\r\n" );
    strcpy( buf, "时间        数据  ---\xd\n" );
    s = CH376ByteWrite( buf, strlen(buf), NULL );  /* 以字节为单位向当前位置写入数据块 */
    mStopIfError( s );
    
    strcpy( buf, "2012-12-01  01    ---\xd\n" );
    buf[9] = '0' + CountT;
    
    for(i = 0; i < 100;i ++){
        buf[12] = '0' + i/10;
        buf[13] = '0' + i%10;
        printf( "Write %s" ,buf);
        s = CH376ByteWrite( buf, strlen(buf), NULL );  /* 以字节为单位向当前位置写入数据块 */
        mStopIfError( s );            
    }
    
    printf( "Close\r\n" );
    s = CH376FileClose( TRUE );  /* 关闭文件,对于字节读写建议自动更新文件长度 */
    mStopIfError( s );
#endif
/* 如果MY_ADC.TXT文件已经存在则添加数据到尾部,如果不存在则新建文件 */
        printf( "Open\n" );
        s = CH376FileOpen( NameBuf );  /* 打开文件,该文件在根目录下 */
        if ( s == USB_INT_SUCCESS ) {  /* 文件存在并且已经被打开,移动文件指针到尾部以便添加数据 */
                printf( "File size = %ld\n", CH376GetFileSize( ) );  /* 读取当前文件长度 */
                printf( "Locate tail\n" );
                s = CH376ByteLocate( 0xFFFFFFFF );  /* 移到文件的尾部 */
                mStopIfError( s );
        }
        else if ( s == ERR_MISS_FILE ) {  /* 没有找到文件,必须新建文件 */
                printf( "Create\n" );
                s = CH376FileCreate( NULL );  /* 新建文件并打开,如果文件已经存在则先删除后再新建,不必再提供文件名,刚才已经提供给CH376FileOpen */
                mStopIfError( s );
        }
        else mStopIfError( s );  /* 打开文件时出错 */
        printf( "Write begin\n" );
        s = sprintf( buf, "此前文件长度= %ld 字节\xd\xa", CH376GetFileSize( ) );  /* 注意字符串长度不能溢出buf,否则加大缓冲区或者分多次写入 */
        s = CH376ByteWrite( buf, s, NULL );  /* 以字节为单位向文件写入数据 */
        mStopIfError( s );
        printf( "Write ADC data\n" );
        printf( "Current offset ( file point ) is :        ");
        for(i = 0;i<1000;i++){
            s = sprintf( buf, "%02d.%02d.%02d.%d\xd\xa", 2012 + i/365, 1 + (i / 30) % 12, 1 + i % 30, i );  
            /* 将二制制数据格式为一行字符串 */
            s = CH376ByteWrite( buf, s, NULL );  /* 以字节为单位向文件写入数据 */
            /* 有些U盘可能会要求在写数据后等待一会才能继续操作,
            所以,如果在某些U盘中发生数据丢失现象,
            建议在每次写入数据后稍作延时再继续 */
            for(j = 0;j<30;j++);
            mStopIfError( s );
            printf( "\b\b\b\b\b\b" );
            printf( "%6ld", CH376ReadVar32( VAR_CURRENT_OFFSET ) );  
            /* 读取当前文件指针 */
        }
        
        printf( "Write end\n" );
        strcpy( buf, "今天的ADC数据到此结束\xd\xa" );
        s = CH376ByteWrite( buf, strlen( buf ), NULL );  /* 以字节为单位向文件写入数据 */
        mStopIfError( s );
/* 如果实际产品中有实时时钟,可以根据需要将文件的日期和时间修改为实际值,参考EXAM10用CH376DirInfoRead/CH376DirInfoSave实现 */
        printf( "Close\n" );
        s = CH376FileClose( TRUE );  /* 关闭文件,自动计算文件长度,以字节为单位写文件,建议让程序库关闭文件以便自动更新文件长度 */
        mStopIfError( s );

        
    do{
        for(i = 0;i<30000;i++);
        P1OUT_bit.P1OUT0 = 1;
        for(i = 0;i<30000;i++);
        P1OUT_bit.P1OUT0 = 0;            
        res = CH376DiskConnect( ); /* 查询U盘是否连接,返回USB_INT_SUCCESS则说明当前已连接 */ 
        
    }while(res!=ERR_DISK_DISCON);
    printk("  ERR_DISK_DISCON\r\n");
    goto code_start;

}
Esempio n. 12
0
main( ) {
    UINT8	s;
    mDelaymS( 100 );  /* 延时100毫秒 */
    mInitSTDIO( );  /* 为了让计算机通过串口监控演示过程 */
    printf( "Start\n" );

    s = mInitCH376Host( );  /* 初始化CH376 */
    mStopIfError( s );
    /* 其它电路初始化 */

    while ( 1 ) {
        printf( "Wait Udisk\n" );

//		while ( CH376DiskConnect( ) != USB_INT_SUCCESS ) {  /* 检查U盘是否连接,等待U盘插入,对于SD卡,可以由单片机直接查询SD卡座的插拔状态引脚 */
//			mDelaymS( 100 );
//		}
        while ( 1 ) {  /* 与前几行的方法不同,此处是等到中断通知后再查询USB设备连接 */
            if ( Query376Interrupt( ) ) {  /* 查询CH376中断(INT#引脚为低电平) */
                if ( CH376DiskConnect( ) == USB_INT_SUCCESS ) break;  /* USB设备连接 */
            }
            mDelaymS( 100 );
        }

        mDelaymS( 200 );  /* 延时,可选操作,有的USB存储器需要几十毫秒的延时 */

        /* 对于检测到USB设备的,最多等待10*50mS */
        for ( s = 0; s < 10; s ++ ) {  /* 最长等待时间,10*50mS */
            mDelaymS( 50 );
            printf( "Ready ?\n" );
            if ( CH376DiskMount( ) == USB_INT_SUCCESS ) break;  /* 初始化磁盘并测试磁盘是否就绪 */
        }
        s = CH376ReadBlock( buf );  /* 如果需要,可以读取数据块CH376_CMD_DATA.DiskMountInq,返回长度 */
        if ( s == sizeof( INQUIRY_DATA ) ) {  /* U盘的厂商和产品信息 */
            buf[ s ] = 0;
            printf( "UdiskInfo: %s\n", ((P_INQUIRY_DATA)buf) -> VendorIdStr );
        }

        printf( "Check Disk Write Protect ? ...\n" );
        s = IsDiskWriteProtect( );  /* 检查U盘是否写保护, 返回USB_INT_SUCCESS说明可以写,返回0xFF说明只读/写保护,返回其它值说明是错误代码 */
        if ( s != USB_INT_SUCCESS && s != 0xFF ) {  /* 操作失败 */
            printf( "Again ...\n" );
            mDelaymS( 100 );
            s = IsDiskWriteProtect( );  /* 再试一次 */
        }
        if ( s == USB_INT_SUCCESS ) {  /* 可以写 */
            printf( "... No !\n" );
            printf( "Create a File\n" );
            s = CH376FileCreate( "\\NEWFILE.TXT" );  /* 在根目录下新建文件并打开,如果文件已经存在则先删除后再新建 */
            mStopIfError( s );
            buf[0] = 'O';
            buf[1] = 'K';
            s = CH376ByteWrite( buf, 2, NULL );  /* 向文件写入数据 */
            mStopIfError( s );
            printf( "Close\n" );
            s = CH376FileClose( TRUE );  /* 自动计算文件长度 */
            mStopIfError( s );
            if ( SafeRemoveDisk( ) == USB_INT_SUCCESS ) {  /* 安全移除U盘 */
                printf( "Safe Remove !\n" );
            }
            else {
                printf( "Unsafe Remove !\n" );
            }
        }
        else if ( s == 0xFF ) {  /* 写保护 */
            printf( "... Yes !\n" );
        }
        else {
            mStopIfError( s );  /* 显示错误代码 */
        }

        printf( "Take out\n" );

//		while ( CH376DiskConnect( ) == USB_INT_SUCCESS ) {  /* 检查U盘是否连接,等待U盘拔出 */
//			mDelaymS( 100 );
//		}
        while ( 1 ) {  /* 与前几行的方法不同,此处是等到中断通知后再查询USB设备连接 */
            if ( Query376Interrupt( ) ) {  /* 查询CH376中断(INT#引脚为低电平) */
                if ( CH376DiskConnect( ) != USB_INT_SUCCESS ) break;  /* USB设备断开 */
            }
            mDelaymS( 100 );
        }

        mDelaymS( 200 );

    }
}
Esempio n. 13
0
void	host( ) {
	UINT8	i, s;
	UINT8	TotalCount;
	UINT16	RealCount;
	P_FAT_DIR_INFO	pDir;

	s = mInitCH376Host( );  /* 初始化CH376 */
	mStopIfError( s );
/* 其它电路初始化 */

	while ( 1 ) {
		printf( "Wait Udisk/SD\n" );
		while ( CH376DiskConnect( ) != USB_INT_SUCCESS ) {  /* 检查U盘是否连接,等待U盘插入,对于SD卡,可以由单片机直接查询SD卡座的插拔状态引脚 */
			if ( IsKeyPress( ) ) {  /* 有键按下 */
				printf( "Exit USB host mode\n" );
				return;
			}
			mDelaymS( 100 );  /* 没必要频繁查询 */
		}
		LED_UDISK_IN( );  /* LED亮 */
		mDelaymS( 200 );  /* 延时,可选操作,有的USB存储器需要几十毫秒的延时 */

/* 对于检测到USB设备的,最多等待100*50mS,主要针对有些MP3太慢,对于检测到USB设备并且连接DISK_MOUNTED的,最多等待5*50mS,主要针对DiskReady不过的 */
		for ( i = 0; i < 100; i ++ ) {  /* 最长等待时间,100*50mS */
			mDelaymS( 50 );
			printf( "Ready ?\n" );
			s = CH376DiskMount( );  /* 初始化磁盘并测试磁盘是否就绪 */
			if ( s == USB_INT_SUCCESS ) break;  /* 准备好 */
			else if ( s == ERR_DISK_DISCON ) break;  /* 检测到断开,重新检测并计时 */
			if ( CH376GetDiskStatus( ) >= DEF_DISK_MOUNTED && i >= 5 ) break;  /* 有的U盘总是返回未准备好,不过可以忽略,只要其建立连接MOUNTED且尝试5*50mS */
		}
		if ( s == ERR_DISK_DISCON ) {  /* 检测到断开,重新检测并计时 */
			printf( "Device gone\n" );
			continue;
		}
		if ( CH376GetDiskStatus( ) < DEF_DISK_MOUNTED ) {  /* 未知USB设备,例如USB键盘、打印机等 */
			printf( "Unknown device\n" );
			goto UnknownUsbDevice;
		}
		i = CH376ReadBlock( buf );  /* 如果需要,可以读取数据块CH376_CMD_DATA.DiskMountInq,返回长度 */
		if ( i == sizeof( INQUIRY_DATA ) ) {  /* U盘的厂商和产品信息 */
			buf[ i ] = 0;
			printf( "UdiskInfo: %s\n", ((P_INQUIRY_DATA)buf) -> VendorIdStr );
		}

/* 读取原文件 */
		printf( "Open\n" );
		strcpy( buf, "\\C51\\CH376HFT.C" );  /* 源文件名,多级目录下的文件名和路径名必须复制到RAM中再处理,而根目录或者当前目录下的文件名可以在RAM或者ROM中 */
		s = CH376FileOpenPath( buf );  /* 打开文件,该文件在C51子目录下 */
		if ( s == ERR_MISS_DIR || s == ERR_MISS_FILE ) {  /* 没有找到目录或者没有找到文件 */
/* 列出文件,完整枚举可以参考EXAM13全盘枚举 */
			if ( s == ERR_MISS_DIR ) strcpy( buf, "\\*" );  /* C51子目录不存在则列出根目录下的文件 */
			else strcpy( buf, "\\C51\\CH376*" );  /* CH376HFT.C文件不存在则列出\C51子目录下的以CH376开头的文件 */
			printf( "List file %s\n", buf );
			s = CH376FileOpenPath( buf );  /* 枚举多级目录下的文件或者目录,输入缓冲区必须在RAM中 */
			while ( s == USB_INT_DISK_READ ) {  /* 枚举到匹配的文件 */
				CH376ReadBlock( buf );  /* 读取枚举到的文件的FAT_DIR_INFO结构,返回长度总是sizeof( FAT_DIR_INFO ) */
				pDir = (P_FAT_DIR_INFO)buf;  /* 当前文件目录信息 */
				if ( pDir -> DIR_Name[0] != '.' ) {  /* 不是本级或者上级目录名则继续,否则必须丢弃不处理 */
					if ( pDir -> DIR_Name[0] == 0x05 ) pDir -> DIR_Name[0] = 0xE5;  /* 特殊字符替换 */
					pDir -> DIR_Attr = 0;  /* 强制文件名字符串结束以便打印输出 */
					printf( "*** EnumName: %s\n", pDir -> DIR_Name );  /* 打印名称,原始8+3格式,未整理成含小数点分隔符 */
				}
				xWriteCH376Cmd( CMD0H_FILE_ENUM_GO );  /* 继续枚举文件和目录 */
				xEndCH376Cmd( );
				s = Wait376Interrupt( );
			}
			if ( s != ERR_MISS_FILE ) mStopIfError( s );  /* 操作出错 */
		}
		else {  /* 找到文件或者出错 */
			mStopIfError( s );
			TotalCount = 200;  /* 准备读取总长度 */
			printf( "从文件中读出的前%d个字符是:\n",(UINT16)TotalCount );
			while ( TotalCount ) {  /* 如果文件比较大,一次读不完,可以再调用CH376ByteRead继续读取,文件指针自动向后移动 */
				if ( TotalCount > sizeof(buf) ) i = sizeof(buf);  /* 剩余数据较多,限制单次读写的长度不能超过缓冲区大小 */
				else i = TotalCount;  /* 最后剩余的字节数 */
				s = CH376ByteRead( buf, i, &RealCount );  /* 以字节为单位读取数据块,单次读写的长度不能超过缓冲区大小,第二次调用时接着刚才的向后读 */
				mStopIfError( s );
				TotalCount -= (UINT8)RealCount;  /* 计数,减去当前实际已经读出的字符数 */
				for ( s=0; s!=RealCount; s++ ) printf( "%C", buf[s] );  /* 显示读出的字符 */
				if ( RealCount < i ) {  /* 实际读出的字符数少于要求读出的字符数,说明已经到文件的结尾 */
					printf( "\n" );
					printf( "文件已经结束\n" );
					break;
				}
			}
			printf( "Close\n" );
			s = CH376FileClose( FALSE );  /* 关闭文件 */
			mStopIfError( s );
		}

UnknownUsbDevice:
		printf( "Take out\n" );
		while ( CH376DiskConnect( ) == USB_INT_SUCCESS ) {  /* 检查U盘是否连接,等待U盘拔出 */
			mDelaymS( 100 );
		}
		LED_UDISK_OUT( );  /* LED灭 */
		mDelaymS( 100 );
	}
}
Esempio n. 14
0
//*****************************************************************************
//
//! \brief CH376 test execute main body.
//!
//! \return None.
//
//*****************************************************************************
static void CH376Execute(void)
{
    unsigned char s;
    UINT16 usDataCnt;
    unsigned int ulFileSize, ulSetcorCnt;


    //waiting for USB device get ready
    for ( s = 0; s < 10; s ++ )
    {
        mDelaymS( 50 );
        printf( "Ready ?\n\r" );
        //
        // Initialize disk and check if disk is ready
        //
        if ( CH376DiskMount( ) == USB_INT_SUCCESS ) break;
    }
    s = CH376ReadBlock( ucReadData );
    TestAssert((s == sizeof( INQUIRY_DATA )), "CH376 API \"CH376ReadBlock()\"error!");
    if ( s == sizeof( INQUIRY_DATA ) )
    {
        //
        // get device information
        //
    	ucReadData[ s ] = 0;
        printf( "UdiskInfo: %s\n\r", ((P_INQUIRY_DATA)ucReadData)->VendorIdStr);
    }
    s = CH376DiskCapacity(&ulSetcorCnt);
    TestAssert((s == USB_INT_SUCCESS), "CH376 API \"CH376DiskCapacity()\"error!");
    printf("Capacity of U-disk: %dMB\n\r", ulSetcorCnt / (1048576 / DEF_SECTOR_SIZE));
    s = CH376DiskQuery(&ulSetcorCnt);
    TestAssert((s == USB_INT_SUCCESS), "CH376 API \"CH376DiskCapacity()\"error!");
    printf("Available capacity: %dMB\n\r", ulSetcorCnt / (1048576 / DEF_SECTOR_SIZE));

    s = CH376FileOpenPath("/CH376TST/TST.TXT");
    if ( s != USB_INT_SUCCESS )
    {
        //
        // if file path not found, firstly create the folder then create the file
        //
        s = CH376DirCreate("/CH376TST");
        TestAssert((s == USB_INT_SUCCESS), "CH376 API \"CH376DirCreate()\"error!");
        s = CH376FileCreate("TST.TXT");
        TestAssert((s == USB_INT_SUCCESS), "CH376 API \"CH376FileCreate()\"error!");
        s = CH376ByteWrite(ucWriteData, 35, &usDataCnt);
        TestAssert((s == USB_INT_SUCCESS), "CH376 API \"CH376ByteWrite()\"error!");
        //
        // After modifying a file, the parameter of CH376FileClose() must be TRUE to update
        // file size or you will find the file not available when you open the file on computer.
        //
        CH376FileClose(TRUE);
    }
    else
    {
        //
        // if file found, move file pointer to the end of file, then append data to the file
        //
        CH376ByteLocate(0x0);
        s = CH376ByteWrite(ucWriteData, 35, &usDataCnt);
        TestAssert((s == USB_INT_SUCCESS), "CH376 API \"CH376ByteWrite()\"error!");
        //
        // enable auto update file size
        //
        CH376FileClose(TRUE);
    }
    CH376FileOpenPath("/CH376TST/TST.TXT");
    ulFileSize = CH376GetFileSize();
    CH376ByteRead(ucReadData, 512, &usDataCnt);
    for(s = 0; s < ulFileSize; s++)
    {
        TestAssert((ucReadData[s] == ucWriteData[s]), "CH376 API \"CH376ByteRead()\"error!");
    }
    CH376FileClose(FALSE);
    printf("\n\r All test over!\n\r");

}
Esempio n. 15
0
//主程序
void main(void)
{
    uint i = 0;
    uint timercount = 0;
    uchar packNO = 1;
    uint bufferPoint = 0;
    uint crc;     
#ifdef  UPDATE_USB    
    uchar s;
    uint j;
    unsigned long UpdateSize = 0;
    uint LabCount = 0,lastdatanum = 0;     
    uchar string[50] = {0};
#endif   
 
//初始化M128的USART0   
    UBRR0L = BAUD_L;            //Set baud rate 
    UBRR0H = BAUD_H; 
    UCSR0B = ((1<<RXEN0)|(1<<TXEN0));        //接收器与发送器使能;
    UCSR0C = (1<<USBS0)|(3<<UCSZ00);        //设置帧格式: 8 个数据位, 1 个停止位;
#ifdef  UPDATE_USB    
//初始化M128的USART1    
    UBRR1L = 8;
    UBRR1H = 0;
    UCSR1B = ((1<<RXEN1)|(1<<TXEN1));        //接收器与发送器使能;
    UCSR1C = (1<<USBS1)|(3<<UCSZ10);        //设置帧格式: 8 个数据位, 1 个停止位;
#endif    
//初始化M128的T/C0,15ms自动重载
    OCR0 = 0x75;
    TCCR0 = 0x0F;                                                               
    TCNT0 = 0;  
    
    DDRB.0 = 1;
    PORTB.0 = 1;  
    /*
    USART_Send_string(a4String1);
    while(uart_getchar()!='O');
    while(uart_getchar()!='K');
    USART_Send_string(a4String2);  
    while(uart_getchar()!='O');
    while(uart_getchar()!='K');  
    */
    USART_Send_string(startupString);//向PC机发送开始提示信息  
    while(1)
    {    
        if(uart_getchar()=='d')break;
        if(TIFR&0x02)
        {
            if(++timercount>500) //若没有进入串口升级模式,则进入U盘升级模式 200*15ms=3s
            {     
#ifdef  UPDATE_USB
    
                sprintf((char*)string,"Enter the USB_Disk Update!\n",UpdateSize);
                USART_Send_string(string);      
                //++++++++++++++++初始化CH376S++++++++++++++++++++++++       
                //CH376_PORT_INIT( );  /* 接口硬件初始化 */
	            xWriteCH376Cmd(CMD11_CHECK_EXIST);  /* 测试单片机与CH376之间的通讯接口 */
	            xWriteCH376Data(0x65);  
	            s = xReadCH376Data( );
	            if (s != 0x9A) 
                    uart_putchar(ERR_USB_UNKNOWN);  /* 通讯接口不正常,可能原因有:接口连接异常,其它设备影响(片选不唯一),串口波特率,一直在复位,晶振不工作 */
	            xWriteCH376Cmd(CMD11_SET_USB_MODE);  /* 设备USB工作模式 */
	            xWriteCH376Data(0x06);
	            s = xReadCH376Data( );
	            if (s != CMD_RET_SUCCESS)  
                {
                    sprintf((char*)string,"USB_Disk is wrong init!\n",UpdateSize);
                    USART_Send_string(string); 
                    quit();
                }   
                //++++++++++++++++++++++END+++++++++++++++++++++++++++++++++
                //检查U盘是否连接好
                i = 0;
                while(CH376DiskConnect() != USB_INT_SUCCESS)
                {
                    if(++i > 5)
                    {   
                        sprintf((char*)string,"USB_Disk is not Connection!\n",UpdateSize);
                        USART_Send_string(string); 
                        quit(); 
                    }
                    delay_ms(100);
                }                  
                i = 0;
                // 对于检测到USB设备的,最多等待10*50mS 
                if(CH376DiskMount() != USB_INT_SUCCESS)
                {
                    if(++i > 5)
                    {
                        sprintf((char*)string,"USB_Disk Test Wrong!\n",UpdateSize);
                        USART_Send_string(string); 
                        quit();
                    }
                    delay_ms(100);   
                }        
                //打开升级文件
                s = CH376FileOpen("J8A-1.U");//每台机子,对应升级文件。
                if (s == ERR_MISS_FILE) //没有找到升级文件则退出
                {
                    CH376FileClose(TRUE);
                    sprintf((char*)string,"I can't fined the Update_File!\n",UpdateSize);
                    USART_Send_string(string);
                    quit();     
                }           
                UpdateSize = CH376GetFileSize(); 
                sprintf((char*)string,"The Update_File size is :%dl\n",UpdateSize); 
                USART_Send_string(string);   
                
                LabCount = UpdateSize/SPM_PAGESIZE;
                lastdatanum = UpdateSize%SPM_PAGESIZE;
                if(lastdatanum) 
                    LabCount++;  
                if(LabCount > (512-32))//mega128的flash页数 
                {
                    sprintf((char*)string,"The Update_File size is too big!",UpdateSize); 
                    USART_Send_string(string);
                    CH376FileClose(FALSE); 
                    quit();
                }
                //读取升级文件数据          
                for(i = 0; i < LabCount; i++)
                {     
                       
                    if(lastdatanum && (i == (LabCount - 1)))     
                    {                                    
                        CH376ByteRead(data, lastdatanum, NULL); 
                        for(j = lastdatanum; j < SPM_PAGESIZE; j++)
                            data[j] = 0xFF;
                    }
                    else
                        CH376ByteRead(data, SPM_PAGESIZE, NULL);        
                    write_one_page();   
                    address = address + SPM_PAGESIZE;    //Flash页加1
                }
                //write_one_page();         //收到256字节写入一页Flash中
                //address = address + SPM_PAGESIZE;    //Flash页加1
                //关闭文件                     
                CH376FileClose(FALSE);
#endif
                quit();
            }
            TIFR=TIFR|0x02;
        }
    }
    //每秒向PC机发送一个控制字符"C",等待控制字〈soh〉
    while(uart_getchar()!= XMODEM_SOH)        //receive the start of Xmodem
    {
         if(TIFR & 0x02)              //timer0 over flow
        {
            if(++timercount > 100)                   //wait about 1 second
            {
                uart_putchar(XMODEM_RECIEVING_WAIT_CHAR);   //send a "C"
                timercount = 0;
            }
            TIFR = TIFR&0x02;
        }
    }
    //开始接收数据块
    do
    {
        if ((packNO == uart_waitchar()) && (packNO ==(~uart_waitchar())))
        {    //核对数据块编号正确
            for(i=0;i<128;i++)             //接收128个字节数据
            {
                data[bufferPoint]= uart_waitchar();
                bufferPoint++;    
            }
            crc = (uint)(uart_waitchar())<<8;
            crc = crc | uart_waitchar();        //接收2个字节的CRC效验字
            if(calcrc(&data[bufferPoint-128],128) == crc)    //CRC校验验证
            {    //正确接收128个字节数据
                while(bufferPoint >= SPM_PAGESIZE)
                {    //正确接受256个字节的数据
                    write_one_page();         //收到256字节写入一页Flash中
                    address = address + SPM_PAGESIZE;    //Flash页加1
                    bufferPoint = 0;
                }    
                uart_putchar(XMODEM_ACK);      //正确收到一个数据块
                packNO++;                      //数据块编号加1
            }
            else
            {
                uart_putchar(XMODEM_NAK);     //要求重发数据块
            }
        }
        else
        {
            uart_putchar(XMODEM_NAK);           //要求重发数据块
        }
    }while(uart_waitchar()!=XMODEM_EOT);          //循环接收,直到全部发完
    uart_putchar(XMODEM_ACK);                    //通知PC机全部收到
    
    if(bufferPoint) write_one_page();        //把剩余的数据写入Flash中
    quit();                //退出Bootloader程序,从0x0000处执行应用程序       
}