?? ch375ev0.c
字號(hào):
/* 2004.03.05
****************************************
** Copyright (C) W.ch 1999-2004 **
** Web: http://www.winchiphead.com **
****************************************
** USB 1.1 Host Examples for CH375 **
** KC7.0@MCS-51 **
****************************************
*/
/* CH375作為USB主機(jī)接口的程序示例 */
/* MCS-51單片機(jī)C語言的示例程序, U盤數(shù)據(jù)讀寫 */
#include <reg51.h>
#include <string.h>
#include <stdio.h>
/* 定義CH375命令代碼及返回狀態(tài) */
#include "CH375INC.H"
/* CH375特性 */
#define CH375_BLOCK_SIZE 64 /* CH375 maximum data block size */
#define CH375_BLK_PER_SEC 8 /* CH375 block per sector, SECTOR_SIZE/CH375_BLOCK_SIZE */
/* 以下定義適用于MCS-51單片機(jī),其它單片機(jī)參照修改,為了提供C語言的速度需要對本程序進(jìn)行優(yōu)化 */
#include <reg51.h>
unsigned char volatile xdata CH375_CMD_PORT _at_ 0xBDF1; /* CH375命令端口的I/O地址 */
unsigned char volatile xdata CH375_DAT_PORT _at_ 0xBCF0; /* CH375數(shù)據(jù)端口的I/O地址 */
unsigned char xdata DATA_BUFFER[512] _at_ 0x0000; /* 外部RAM數(shù)據(jù)緩沖區(qū)的起始地址,長度不少于一次讀寫的數(shù)據(jù)長度 */
sbit CH375_INT_WIRE = 0xB0^2; /* P3.2, INT0, 連接CH375的INT#引腳,用于查詢中斷狀態(tài) */
/* 在P1.4連接一個(gè)LED用于監(jiān)控演示程序的進(jìn)度,低電平LED亮,當(dāng)U盤插入后亮 */
sbit P1_4 = P1^4;
#define LED_OUT_ACT( ) { P1_4 = 0; } /* P1.4 低電平驅(qū)動(dòng)LED顯示 */
#define LED_OUT_INACT( ) { P1_4 = 1; } /* P1.4 低電平驅(qū)動(dòng)LED顯示 */
/* 延時(shí)2微秒,不精確 */
void delay2us( )
{
unsigned char i;
for ( i = 2; i != 0; i -- );
}
/* 延時(shí)1微秒,不精確 */
void delay1us( )
{
unsigned char i;
for ( i = 1; i != 0; i -- );
}
/* 延時(shí)100毫秒,不精確 */
void mDelay100mS( )
{
unsigned char i, j, c;
for ( i = 200; i != 0; i -- ) for ( j = 200; j != 0; j -- ) c+=3;
}
/* 基本操作 */
void CH375_WR_CMD_PORT( unsigned char cmd ) { /* 向CH375的命令端口寫入命令,周期不小于4uS,如果單片機(jī)較快則延時(shí) */
delay2us();
CH375_CMD_PORT=cmd;
delay2us();
}
void CH375_WR_DAT_PORT( unsigned char dat ) { /* 向CH375的數(shù)據(jù)端口寫入數(shù)據(jù),周期不小于1.5uS,如果單片機(jī)較快則延時(shí) */
CH375_DAT_PORT=dat;
delay1us(); /* 因?yàn)镸CS51單片機(jī)較慢所以實(shí)際上無需延時(shí) */
}
unsigned char CH375_RD_DAT_PORT() { /* 從CH375的數(shù)據(jù)端口讀出數(shù)據(jù),周期不小于1.5uS,如果單片機(jī)較快則延時(shí) */
delay1us(); /* 因?yàn)镸CS51單片機(jī)較慢所以實(shí)際上無需延時(shí) */
return( CH375_DAT_PORT );
}
/* 等待CH375中斷并獲取狀態(tài) */
unsigned char mWaitInterrupt() { /* 主機(jī)端等待操作完成, 返回操作狀態(tài) */
while( CH375_INT_WIRE ); /* 查詢等待CH375操作完成中斷(INT#低電平) */
CH375_WR_CMD_PORT( CMD_GET_STATUS ); /* 產(chǎn)生操作完成中斷, 獲取中斷狀態(tài) */
return( CH375_RD_DAT_PORT( ) );
/* c = CH375_RD_DAT_PORT( ); 返回中斷狀態(tài) */
/* if ( c == USB_INT_DISCONNECT ) ?; 檢測到USB設(shè)備斷開事件 */
/* else if ( c == USB_INT_CONNECT ) ?; 檢測到USB設(shè)備連接事件 */
}
/* 設(shè)置CH375為USB主機(jī)方式 */
unsigned char mCH375Init( )
{
unsigned char i;
#ifdef TEST_CH375_PORT
unsigned char c;
CH375_WR_CMD_PORT( CMD_CHECK_EXIST ); /* 測試工作狀態(tài) */
CH375_WR_DAT_PORT( 0x55 ); /* 測試數(shù)據(jù) */
c = CH375_RD_DAT_PORT( ); /* 返回?cái)?shù)據(jù)應(yīng)該是測試數(shù)據(jù)取反 */
if ( c != 0xaa ) { /* CH375出錯(cuò) */
for ( i = 100; i != 0; i -- ) { /* 強(qiáng)制數(shù)據(jù)同步 */
CH375_WR_CMD_PORT( CMD_RESET_ALL ); /* CH375執(zhí)行硬件復(fù)位 */
c = CH375_RD_DAT_PORT( ); /* 延時(shí) */
}
mDelay100mS( ); /* 延時(shí)至少30mS */
}
#endif
CH375_WR_CMD_PORT( CMD_SET_USB_MODE ); /* 設(shè)置USB工作模式 */
CH375_WR_DAT_PORT( 6 ); /* 模式代碼,自動(dòng)檢測USB設(shè)備連接 */
for ( i = 0xff; i != 0; i -- ) /* 等待操作成功,通常需要等待10uS-20uS */
if ( CH375_RD_DAT_PORT( ) == CMD_RET_SUCCESS ) break; /* 操作成功 */
if ( i != 0 ) return( 0 ); /* 操作成功 */
else return( 0xff ); /* CH375出錯(cuò),例如芯片型號(hào)錯(cuò)或者處于串口方式或者不支持 */
}
/* 初始化磁盤 */
unsigned char mInitDisk( )
{
unsigned char mIntStatus;
CH375_WR_CMD_PORT( CMD_GET_STATUS ); /* 產(chǎn)生操作完成中斷, 獲取中斷狀態(tài) */
mIntStatus = CH375_RD_DAT_PORT( );
if ( mIntStatus == USB_INT_DISCONNECT ) return( mIntStatus ); /* USB設(shè)備斷開 */
CH375_WR_CMD_PORT( CMD_DISK_INIT ); /* 初始化USB存儲(chǔ)器 */
mIntStatus = mWaitInterrupt( ); /* 等待中斷并獲取狀態(tài) */
if ( mIntStatus != USB_INT_SUCCESS ) return( mIntStatus ); /* 出現(xiàn)錯(cuò)誤 */
CH375_WR_CMD_PORT( CMD_DISK_SIZE ); /* 獲取USB存儲(chǔ)器的容量 */
mIntStatus = mWaitInterrupt( ); /* 等待中斷并獲取狀態(tài) */
if ( mIntStatus != USB_INT_SUCCESS ) { /* 出錯(cuò)重試 */
mDelay100mS( );
CH375_WR_CMD_PORT( CMD_DISK_SIZE ); /* 獲取USB存儲(chǔ)器的容量 */
mIntStatus = mWaitInterrupt( ); /* 等待中斷并獲取狀態(tài) */
}
if ( mIntStatus != USB_INT_SUCCESS ) return( mIntStatus ); /* 出現(xiàn)錯(cuò)誤 */
/* 可以由CMD_RD_USB_DATA命令將容量數(shù)據(jù)讀出 */
return( 0 ); /* U盤已經(jīng)成功初始化 */
}
/* 從U盤讀取多個(gè)扇區(qū)的數(shù)據(jù)塊到緩沖區(qū) */
unsigned char mReadSector( unsigned long iLbaStart, unsigned char iSectorCount )
/* iLbaStart 是準(zhǔn)備讀取的線性起始扇區(qū)號(hào), iSectorCount 是準(zhǔn)備讀取的扇區(qū)數(shù) */
{
unsigned char mIntStatus;
unsigned char *mBufferPoint;
unsigned int mBlockCount;
unsigned char mLength;
CH375_WR_CMD_PORT( CMD_DISK_READ ); /* 從USB存儲(chǔ)器讀數(shù)據(jù)塊 */
CH375_WR_DAT_PORT( (unsigned char)iLbaStart ); /* LBA的最低8位 */
CH375_WR_DAT_PORT( (unsigned char)( iLbaStart >> 8 ) );
CH375_WR_DAT_PORT( (unsigned char)( iLbaStart >> 16 ) );
CH375_WR_DAT_PORT( (unsigned char)( iLbaStart >> 24 ) ); /* LBA的最高8位 */
CH375_WR_DAT_PORT( iSectorCount ); /* 扇區(qū)數(shù) */
mBufferPoint = DATA_BUFFER; /* 指向緩沖區(qū)起始地址 */
for ( mBlockCount = iSectorCount * CH375_BLK_PER_SEC; mBlockCount != 0; mBlockCount -- ) { /* 數(shù)據(jù)塊計(jì)數(shù) */
mIntStatus = mWaitInterrupt( ); /* 等待中斷并獲取狀態(tài) */
if ( mIntStatus == USB_INT_DISK_READ ) { /* USB存儲(chǔ)器讀數(shù)據(jù)塊,請求數(shù)據(jù)讀出 */
CH375_WR_CMD_PORT( CMD_RD_USB_DATA ); /* 從CH375緩沖區(qū)讀取數(shù)據(jù)塊 */
mLength = CH375_RD_DAT_PORT( ); /* 后續(xù)數(shù)據(jù)的長度 */
while ( mLength ) { /* 根據(jù)長度讀取數(shù)據(jù) */
*mBufferPoint = CH375_RD_DAT_PORT( ); /* 讀出數(shù)據(jù)并保存 */
mBufferPoint ++;
mLength --;
}
CH375_WR_CMD_PORT( CMD_DISK_RD_GO ); /* 繼續(xù)執(zhí)行USB存儲(chǔ)器的讀操作 */
}
else break; /* 返回錯(cuò)誤狀態(tài) */
}
if ( mBlockCount == 0 ) {
mIntStatus = mWaitInterrupt( ); /* 等待中斷并獲取狀態(tài) */
if ( mIntStatus == USB_INT_SUCCESS ) return( 0 ); /* 操作成功 */
}
return( mIntStatus ); /* 操作失敗 */
}
/* 將緩沖區(qū)中的多個(gè)扇區(qū)的數(shù)據(jù)塊寫入U(xiǎn)盤 */
unsigned char mWriteSector( unsigned long iLbaStart, unsigned char iSectorCount )
/* iLbaStart 是寫入的線起始性扇區(qū)號(hào), iSectorCount 是寫入的扇區(qū)數(shù) */
{
unsigned char mIntStatus;
unsigned char *mBufferPoint;
unsigned int mBlockCount;
unsigned char mLength;
CH375_WR_CMD_PORT( CMD_DISK_WRITE ); /* 向USB存儲(chǔ)器寫數(shù)據(jù)塊 */
CH375_WR_DAT_PORT( (unsigned char)iLbaStart ); /* LBA的最低8位 */
CH375_WR_DAT_PORT( (unsigned char)( iLbaStart >> 8 ) );
CH375_WR_DAT_PORT( (unsigned char)( iLbaStart >> 16 ) );
CH375_WR_DAT_PORT( (unsigned char)( iLbaStart >> 24 ) ); /* LBA的最高8位 */
CH375_WR_DAT_PORT( iSectorCount ); /* 扇區(qū)數(shù) */
mBufferPoint = DATA_BUFFER; /* 指向緩沖區(qū)起始地址 */
for ( mBlockCount = iSectorCount * CH375_BLK_PER_SEC; mBlockCount != 0; mBlockCount -- ) { /* 數(shù)據(jù)塊計(jì)數(shù) */
mIntStatus = mWaitInterrupt( ); /* 等待中斷并獲取狀態(tài) */
if ( mIntStatus == USB_INT_DISK_WRITE ) { /* USB存儲(chǔ)器寫數(shù)據(jù)塊,請求數(shù)據(jù)寫入 */
CH375_WR_CMD_PORT( CMD_WR_USB_DATA7 ); /* 向CH375緩沖區(qū)寫入數(shù)據(jù)塊 */
mLength = CH375_BLOCK_SIZE;
CH375_WR_DAT_PORT( mLength ); /* 后續(xù)數(shù)據(jù)的長度 */
while ( mLength ) { /* 根據(jù)長度寫入數(shù)據(jù) */
CH375_WR_DAT_PORT( *mBufferPoint ); /* 將數(shù)據(jù)寫入 */
mBufferPoint ++;
mLength --;
}
/* do { 對于C51,這個(gè)DO+WHILE結(jié)構(gòu)比上面的WHILE效率高,速度快
CH375_WR_DAT_PORT( *mBufferPoint );
mBufferPoint ++;
} while ( -- mLength );*/
CH375_WR_CMD_PORT( CMD_DISK_WR_GO ); /* 繼續(xù)執(zhí)行USB存儲(chǔ)器的寫操作 */
}
else break; /* 返回錯(cuò)誤狀態(tài) */
}
if ( mBlockCount == 0 ) {
mIntStatus = mWaitInterrupt( ); /* 等待中斷并獲取狀態(tài) */
if ( mIntStatus == USB_INT_SUCCESS ) return( 0 ); /* 操作成功 */
}
return( mIntStatus ); /* 操作失敗 */
}
struct _HD_MBR_DPT {
unsigned char PartState;
unsigned char StartHead;
unsigned int StartSec;
unsigned char PartType;
unsigned char EndHead;
unsigned int EndSec;
unsigned long StartSector;
unsigned long TotalSector;
};
/* 為printf和getkey輸入輸出初始化串口 */
void mInitSTDIO( )
{
SCON = 0x50;
PCON = 0x80;
TMOD = 0x20;
TH1 = 0xf3; /* 24MHz晶振, 9600bps */
TR1 = 1;
TI = 1;
}
main( ) {
unsigned char c, mIntStatus;
LED_OUT_ACT( ); /* 開機(jī)后LED亮一下以示工作 */
mDelay100mS( ); /* 延時(shí)100毫秒 */
LED_OUT_INACT( );
mInitSTDIO( );
printf( "Start\n" );
c = mCH375Init( ); /* 初始化CH375 */
if ( c ) printf( "Error @CH375Init\n" );
printf( "Insert USB disk\n" );
do { /* 等待U盤連接 */
mIntStatus = mWaitInterrupt( ); /* 等待中斷并獲取狀態(tài) */
} while ( mIntStatus != USB_INT_CONNECT ); /* U盤沒有連接或者已經(jīng)拔出 */
mDelay100mS( ); /* 延時(shí)等待U盤進(jìn)入正常工作狀態(tài) */
mDelay100mS( );
printf( "InitDisk\n" );
c = mInitDisk( ); /* 初始化U盤,實(shí)際是識(shí)別U盤的類型,不影響U盤中的數(shù)據(jù),在所有讀寫操作之前必須進(jìn)行此步驟 */
if ( c ) printf( "Error @InitDisk, %02X\n", c );
LED_OUT_ACT( );
/* 檢查U盤是否準(zhǔn)備好,大多數(shù)U盤不需要這一步,但是某些U盤必須要執(zhí)行這一步才能工作 */
// do {
// mDelay100mS( );
// printf( "Disk Ready ?\n" );
// i = CH375DiskReady( ); /* 查詢磁盤是否準(zhǔn)備好,如果省掉這個(gè)子程序可以節(jié)約將近1KB的程序代碼 */
// } while ( i != ERR_SUCCESS );
/* CH375DiskReady 在CH375的U盤文件子程序庫中,因?yàn)榇a較多,所以此處省去 */
printf( "ReadSector 0# to buffer\n" );
c = mReadSector( 0, 1 );
if ( c ) printf( "Error @ReadSector, %02X\n", c );
if ( DATA_BUFFER[0x01FF] == 0xAA ) { /* 磁盤分區(qū)有效 */
printf( "WriteSector 1# from buffer\n" );
c = mWriteSector( 1, 1 );
if ( c ) printf( "Error @WriteSector, %02X\n", c );
memset( DATA_BUFFER, 0, 512 ); /* 清空數(shù)據(jù)緩沖區(qū),代替原來的分區(qū)信息 */
printf( "WriteSector 0# for clear\n" );
c = mWriteSector( 0, 1 );
if ( c ) printf( "Error @WriteSector, %02X\n", c );
}
else {
printf( "ReadSector 1# to buffer\n" );
c = mReadSector( 1, 1 );
if ( c ) printf( "Error @ReadSector, %02X\n", c );
printf( "WriteSector 0# from buffer\n" );
c = mWriteSector( 0, 1 );
if ( c ) printf( "Error @WriteSector, %02X\n", c );
}
printf( "Stop\n" );
while ( 1 ) {
mIntStatus = mWaitInterrupt( ); /* 等待中斷并獲取狀態(tài) */
if ( mIntStatus == USB_INT_DISCONNECT ) { /* U盤沒有連接或者已經(jīng)拔出 */
printf( "Out\n" );
LED_OUT_INACT( );
}
else if ( mIntStatus == USB_INT_CONNECT ) { /* U盤已經(jīng)連接 */
printf( "In\n" );
LED_OUT_ACT( );
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -