//***************************************************************************** // //! \brief Initial CH376 as USB host. //! //! \param None //! //! \return error code defined in hw_CH376INC.h. // //***************************************************************************** UINT8 mInitCH376Host( void ) { UINT8 res; CH376PortInit( ); xWriteCH376Cmd( CMD11_CHECK_EXIST ); xWriteCH376Data( 0x65 ); res = xReadCH376Data( ); xEndCH376Cmd( ); if ( res != 0x9A ) return( ERR_USB_UNKNOWN ); xWriteCH376Cmd( CMD11_SET_USB_MODE ); xWriteCH376Data( 0x06 ); mDelayuS( 20 ); res = xReadCH376Data( ); xEndCH376Cmd( ); #ifdef CH376_USE_SOFTWARE_SPI #ifdef CH376_SPI_SDO_INT xWriteCH376Cmd( CMD20_SET_SDO_INT ); xWriteCH376Data( 0x16 ); xWriteCH376Data( 0x90 ); xEndCH376Cmd( ); #endif #endif if ( res == CMD_RET_SUCCESS ) return( USB_INT_SUCCESS ); else return( ERR_USB_UNKNOWN ); }
/* 查询CH376中断(INT#低电平) */ uchar Query376Interrupt(void) { return USART_Receive_1(); } uchar CH376DiskConnect(void)/*检查U盘是否连接*/ { if (Query376Interrupt( )) CH376GetIntStatus( ); /* 检测到中断 */ return(CH376SendCmdWaitInt(CMD0H_DISK_CONNECT)); } #define CMD50_WRITE_VAR32 0x0D /* 设置指定的32位文件系统变量 */ void CH376WriteVar32(uchar var, unsigned long dat ) /* 写CH376芯片内部的32位变量 */ { xWriteCH376Cmd(CMD50_WRITE_VAR32); xWriteCH376Data(var); xWriteCH376Data((uchar)dat); xWriteCH376Data((uchar)((uint)dat >> 8)); xWriteCH376Data((uchar)(dat >> 16)); xWriteCH376Data((uchar)(dat >> 24)); }
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)); }
UINT8 mInitCH376Host( void ) /* 初始化CH376 */ { UINT8 res; CH376_PORT_INIT( ); /* 接口硬件初始化 */ xWriteCH376Cmd( CMD11_CHECK_EXIST ); /* 测试单片机与CH376之间的通讯接口 */ xWriteCH376Data( 0x65 ); res = xReadCH376Data( ); // xEndCH376Cmd( ); // 并口方式不需要 if ( res != 0x9A ) return( ERR_USB_UNKNOWN ); /* 通讯接口不正常,可能原因有:接口连接异常,其它设备影响(片选不唯一),串口波特率,一直在复位,晶振不工作 */ xWriteCH376Cmd( CMD11_SET_USB_MODE ); /* 设备USB工作模式 */ xWriteCH376Data( 0x06 ); mDelayuS( 20 ); res = xReadCH376Data( ); // xEndCH376Cmd( ); // 并口方式不需要 if ( res == CMD_RET_SUCCESS ) return( USB_INT_SUCCESS ); else return( ERR_USB_UNKNOWN ); /* 设置模式错误 */ }
/* 设置将要操作的文件的文件名 */ 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)); } #define CMD1H_FILE_CLOSE 0x36 /* 主机文件模式: 关闭当前已经打开的文件或者目录(文件夹) */ uchar CH376FileClose(uchar UpdateSz) /* 关闭当前已经打开的文件或者目录(文件夹) */ { xWriteCH376Cmd(CMD1H_FILE_CLOSE); xWriteCH376Data(UpdateSz); return(Wait376Interrupt()); } #define CMD01_RD_USB_DATA0 0x27 /* 从当前USB中断的端点缓冲区或者主机端点的接收缓冲区读取数据块 */ uchar CH376ReadBlock(uchar * buf) /* 从当前主机端点的接收缓冲区读取数据块,返回长度 */ { uchar s, l; xWriteCH376Cmd(CMD01_RD_USB_DATA0); s = l = xReadCH376Data( ); /* 长度 */ if(l) { do { *buf = xReadCH376Data( ); buf ++; } while ( -- l ); } return( s ); } #define CMD2H_BYTE_READ 0x3A /* 主机文件模式: 以字节为单位从当前位置读取数据块 */ #define USB_INT_DISK_READ 0x1D /* USB存储器请求数据读出 */ #define CMD0H_BYTE_RD_GO 0x3B /* 主机文件模式: 继续字节读 */ uchar CH376ByteRead(uchar * buf, uint ReqCount, uint * RealCount ) /* 以字节为单位从当前位置读取数据块 */ { uchar s; xWriteCH376Cmd(CMD2H_BYTE_READ); xWriteCH376Data((uchar)ReqCount); xWriteCH376Data((uchar)(ReqCount>>8)); if (RealCount) *RealCount = 0; while ( 1 ) { s = Wait376Interrupt( ); if (s == USB_INT_DISK_READ) { s = CH376ReadBlock(buf); /* 从当前主机端点的接收缓冲区读取数据块,返回长度 */ xWriteCH376Cmd(CMD0H_BYTE_RD_GO); buf += s; if (RealCount) *RealCount += s; } else return(s); /* 错误 */ } }
//主程序 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处执行应用程序 }
/* 设置将要操作的文件的文件名 */ 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)); } #define CMD1H_FILE_CLOSE 0x36 /* 主机文件模式: 关闭当前已经打开的文件或者目录(文件夹) */ uchar CH376FileClose(uchar UpdateSz) /* 关闭当前已经打开的文件或者目录(文件夹) */ { xWriteCH376Cmd(CMD1H_FILE_CLOSE); xWriteCH376Data(UpdateSz); return(Wait376Interrupt()); } #define CMD01_RD_USB_DATA0 0x27 /* 从当前USB中断的端点缓冲区或者主机端点的接收缓冲区读取数据块 */ uchar CH376ReadBlock(uchar * buf) /* 从当前主机端点的接收缓冲区读取数据块,返回长度 */ { uchar s, l; xWriteCH376Cmd(CMD01_RD_USB_DATA0); s = l = xReadCH376Data( ); /* 长度 */ if(l) { do { *buf = xReadCH376Data( ); buf ++; } while ( -- l ); } return( s ); } #define CMD2H_BYTE_READ 0x3A /* 主机文件模式: 以字节为单位从当前位置读取数据块 */ #define USB_INT_DISK_READ 0x1D /* USB存储器请求数据读出 */ #define CMD0H_BYTE_RD_GO 0x3B /* 主机文件模式: 继续字节读 */ uchar CH376ByteRead(uchar * buf, uint ReqCount, uint * RealCount ) /* 以字节为单位从当前位置读取数据块 */ { uchar s; xWriteCH376Cmd(CMD2H_BYTE_READ); xWriteCH376Data((uchar)ReqCount); xWriteCH376Data((uchar)(ReqCount>>8)); if (RealCount) *RealCount = 0; while ( 1 ) { s = Wait376Interrupt( ); if (s == USB_INT_DISK_READ) { s = CH376ReadBlock(buf); /* 从当前主机端点的接收缓冲区读取数据块,返回长度 */ xWriteCH376Cmd(CMD0H_BYTE_RD_GO); buf += s; if (RealCount) *RealCount += s; } else return(s); /* 错误 */ } } unsigned long CH376Read32bitDat( void ) /* 从CH376芯片读取32位的数据并结束命令 */ { uchar c0, c1, c2, c3; c0 = xReadCH376Data( ); c1 = xReadCH376Data( ); c2 = xReadCH376Data( ); c3 = xReadCH376Data( ); return(((unsigned long)c3 << 24) | ((unsigned long)c2 << 16) | ((unsigned long)c1 << 8) | c0 ); } #define CMD14_READ_VAR32 0x0C /* 读取指定的32位文件系统变量 */ unsigned long CH376ReadVar32(uchar var) /* 读CH376芯片内部的32位变量 */ { xWriteCH376Cmd(CMD14_READ_VAR32); xWriteCH376Data(var); return(CH376Read32bitDat( ) ); /* 从CH376芯片读取32位的数据并结束命令 */ }
/* 设置将要操作的文件的文件名 */ 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)); } #define CMD1H_FILE_CLOSE 0x36 /* 主机文件模式: 关闭当前已经打开的文件或者目录(文件夹) */ uchar CH376FileClose(uchar UpdateSz) /* 关闭当前已经打开的文件或者目录(文件夹) */ { xWriteCH376Cmd(CMD1H_FILE_CLOSE); xWriteCH376Data(UpdateSz); return(Wait376Interrupt()); }